apache/cassandra · warning · RuntimeException

Sampling already in progress

Error message

Sampling already in progress

What it means

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.

Solutions

  1. Wait for the current sampling window to expire (durationMillis from the previous beginSampling call) before starting a new one
  2. Stop the current sampling session (via the sampler's finishSampling/stop path) before calling beginSampling again
  3. Reduce the sampling duration so windows do not overlap with subsequent calls
  4. Catch the RuntimeException and retry later, or serialize sampling calls

Example fix

// before
sampler.beginSampling(256, 10000);
sampler.beginSampling(256, 10000); // throws

// after
sampler.beginSampling(256, 10000);
// wait until previous window elapsed or call finishSampling first
Defensive patterns

Strategy: type-guard

Validate before calling

if (!sampler.isActive())
    sampler.beginSampling(capacity, durationMillis);

Type guard

function canBeginSampling(sampler) { return !sampler.isActive(); }

Try / catch

try { sampler.beginSampling(capacity, durationMillis); }
catch (RuntimeException e) { logger.info("Sampling already active; skipping this window"); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/53bd7baa9153da33. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/metrics/FrequencySampler.java:58

 * @param <T>
 */
public abstract class FrequencySampler<T> extends Sampler<T>
{
    private static final Logger logger = LoggerFactory.getLogger(FrequencySampler.class);

    private StreamSummary<T> summary;

    /**
     * Start to record samples
     *
     * @param capacity Number of sample items to keep in memory, the lower this is
     *                 the less accurate results are. For best results use value
     *                 close to cardinality, but understand the memory trade offs.
     */
    public synchronized void beginSampling(int capacity, long durationMillis)
    {
        if (isActive())
            throw new RuntimeException("Sampling already in progress");
        updateEndTime(clock.now() + MILLISECONDS.toNanos(durationMillis));
        summary = new StreamSummary<>(capacity);
    }

    /**
     * Call to stop collecting samples, and gather the results
     * @param count Number of most frequent items to return
     */
    public synchronized List<Sample<T>> finishSampling(int count)
    {
        List<Sample<T>> results = Collections.emptyList();
        if (isEnabled())
        {
            disable();
            results = summary.topK(count)
                             .stream()
                             .map(c -> new Sample<>(c.getItem(), c.getCount(), c.getError()))
                             .collect(Collectors.toList());

View on GitHub (pinned to 88fd0f6a0e)