{"record":{"id":"53bd7baa9153da33","repo":"apache/cassandra","slug":"sampling-already-in-progress","errorCode":null,"errorMessage":"Sampling already in progress","messagePattern":"Sampling already in progress","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"warning","filePath":"src/java/org/apache/cassandra/metrics/FrequencySampler.java","lineNumber":58,"sourceCode":" * @param <T>\n */\npublic abstract class FrequencySampler<T> extends Sampler<T>\n{\n    private static final Logger logger = LoggerFactory.getLogger(FrequencySampler.class);\n\n    private StreamSummary<T> summary;\n\n    /**\n     * Start to record samples\n     *\n     * @param capacity Number of sample items to keep in memory, the lower this is\n     *                 the less accurate results are. For best results use value\n     *                 close to cardinality, but understand the memory trade offs.\n     */\n    public synchronized void beginSampling(int capacity, long durationMillis)\n    {\n        if (isActive())\n            throw new RuntimeException(\"Sampling already in progress\");\n        updateEndTime(clock.now() + MILLISECONDS.toNanos(durationMillis));\n        summary = new StreamSummary<>(capacity);\n    }\n\n    /**\n     * Call to stop collecting samples, and gather the results\n     * @param count Number of most frequent items to return\n     */\n    public synchronized List<Sample<T>> finishSampling(int count)\n    {\n        List<Sample<T>> results = Collections.emptyList();\n        if (isEnabled())\n        {\n            disable();\n            results = summary.topK(count)\n                             .stream()\n                             .map(c -> new Sample<>(c.getItem(), c.getCount(), c.getError()))\n                             .collect(Collectors.toList());","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/metrics/FrequencySampler.java#L40-L76","documentation":"FrequencySampler (used for cardinality/latency sampling of table operations) only supports one sampling session at a time. beginSampling throws a RuntimeException if isActive() is true, i.e. a previous sampling window (set by a prior beginSampling whose end time has not yet passed) is still running.","triggerScenarios":"Calling beginSampling (e.g. via JMX/node tooling for table histogram sampling) while a prior sampling session started with beginSampling is still active — the clock-based end time set by the previous call has not yet elapsed.","commonSituations":"Invoking nodetool/JMX sampling operations twice in quick succession; overlapping monitoring scripts sampling the same or different tables concurrently; a very long durationMillis passed to the first beginSampling call.","solutions":["Wait for the current sampling window to expire (durationMillis from the previous beginSampling call) before starting a new one","Stop the current sampling session (via the sampler's finishSampling/stop path) before calling beginSampling again","Reduce the sampling duration so windows do not overlap with subsequent calls","Catch the RuntimeException and retry later, or serialize sampling calls"],"exampleFix":"// before\nsampler.beginSampling(256, 10000);\nsampler.beginSampling(256, 10000); // throws\n\n// after\nsampler.beginSampling(256, 10000);\n// wait until previous window elapsed or call finishSampling first\n","handlingStrategy":"type-guard","validationCode":"if (!sampler.isActive())\n    sampler.beginSampling(capacity, durationMillis);","typeGuard":"function canBeginSampling(sampler) { return !sampler.isActive(); }","tryCatchPattern":"try { sampler.beginSampling(capacity, durationMillis); }\ncatch (RuntimeException e) { logger.info(\"Sampling already active; skipping this window\"); }","preventionTips":["Serialize all sampling requests through a single scheduler","Check isActive() before each beginSampling call","Use sampling durations shorter than your sampling trigger interval"],"tags":["metrics","sampling","state-conflict"],"backgroundTag":"operation-already-in-progress","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}