{"record":{"id":"5bae9727b98f20c3","repo":"apache/cassandra","slug":"sampling-already-in-progress-5bae97","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/MaxSampler.java","lineNumber":43,"sourceCode":"import com.google.common.collect.MinMaxPriorityQueue;\n\nimport static java.util.concurrent.TimeUnit.MILLISECONDS;\n\n/**\n * Note: {@link Sampler#samplerExecutor} is single threaded but we still need to synchronize as we have access\n * from both internal and the external JMX context that can cause races.\n */\npublic abstract class MaxSampler<T> extends Sampler<T>\n{\n    private int capacity;\n    private MinMaxPriorityQueue<Sample<T>> queue;\n    private final Comparator<Sample<T>> comp = Collections.reverseOrder(Comparator.comparing(p -> p.count));\n\n    @Override\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        queue = MinMaxPriorityQueue.orderedBy(comp)\n                                   .maximumSize(Math.max(1, capacity))\n                                   .create();\n        this.capacity = capacity;\n    }\n\n    @Override\n    public synchronized List<Sample<T>> finishSampling(int count)\n    {\n        List<Sample<T>> result = new ArrayList<>(count);\n        if (isEnabled())\n        {\n            disable();\n            Sample<T> next;\n            while ((next = queue.poll()) != null && result.size() <= count)\n                result.add(next);\n        }","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/metrics/MaxSampler.java#L25-L61","documentation":"MaxSampler tracks top-K maximum values per sampling window. Its beginSampling is synchronized and throws a RuntimeException if a sampling session is already active (isActive() true because a prior beginSampling's end time has not elapsed). Only one sampling session may run at a time.","triggerScenarios":"Calling beginSampling (e.g. via JMX sampling of read/write latencies per partition) while another sampling session started earlier is still active and its end time has not passed.","commonSituations":"Running two sampling requests concurrently via JMX or nodetool; monitoring automation overlapping sampling windows; long durationMillis on the first call still in effect when a second call arrives.","solutions":["Wait until the active sampling window (durationMillis of the prior call) expires","Stop/finish the current sampling session before starting a new one","Serialize sampling requests so only one beginSampling runs at a time","Catch the RuntimeException and schedule a retry after the window ends"],"exampleFix":"// before\nmaxSampler.beginSampling(256, 5000);\nmaxSampler.beginSampling(256, 5000); // throws\n\n// after\nif (!maxSampler.isActive())\n    maxSampler.beginSampling(256, 5000);\n","handlingStrategy":"type-guard","validationCode":"if (!maxSampler.isActive())\n    maxSampler.beginSampling(capacity, durationMillis);","typeGuard":"function canBeginSampling(sampler) { return !sampler.isActive(); }","tryCatchPattern":"try { maxSampler.beginSampling(capacity, durationMillis); }\ncatch (RuntimeException e) { logger.info(\"Max sampling already active; skipping\"); }","preventionTips":["Guard with isActive() before calling beginSampling","Avoid overlapping JMX sampling invocations","Track the end time of the previous sampling window before starting a new one"],"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"}