apache/pulsar · warning · org.apache.pulsar.broker.service.AlreadyRunningException

Compaction already in progress

Error message

Compaction already in progress

What it means

A topic can run only one compaction at a time; PersistentTopic tracks the in-flight compaction via a currentCompaction CompletableFuture. If triggerCompaction is called while a previous compaction has not finished, it throws AlreadyRunningException('Compaction already in progress'). This prevents two compaction runs from racing on the same ledger/topic.

Source

Thrown at pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java:4925

                    return;
                }

                if (strategicCompactionMap.containsKey(topic)) {
                    currentCompaction = brokerService.pulsar().getStrategicCompactor()
                            .compact(topic, strategicCompactionMap.get(topic));
                } else {
                    currentCompaction = topicCompactionService.compact().thenApply(x -> null);
                }
            } finally {
                lock.readLock().unlock();
            }
            currentCompaction.whenComplete((ignore, ex) -> {
                if (ex != null) {
                    log.warn().exception(ex).log("Compaction failure.");
                }
            });
        } else {
            throw new AlreadyRunningException("Compaction already in progress");
        }
    }

    public synchronized LongRunningProcessStatus compactionStatus() {
        final CompletableFuture<Long> current;
        synchronized (this) {
            current = currentCompaction;
        }
        if (!current.isDone()) {
            return LongRunningProcessStatus.forStatus(LongRunningProcessStatus.Status.RUNNING);
        } else {
            try {
                if (Objects.equals(current.join(), COMPACTION_NEVER_RUN)) {
                    return LongRunningProcessStatus.forStatus(LongRunningProcessStatus.Status.NOT_RUN);
                } else {
                    return LongRunningProcessStatus.forStatus(LongRunningProcessStatus.Status.SUCCESS);
                }
            } catch (CancellationException | CompletionException e) {

View on GitHub (pinned to 820761864e)

Solutions

  1. Poll admin topics().compactionStatus() and only trigger when the status is NOT running (NOT_RUN/SUCCESS/ERROR)
  2. Skip triggering on AlreadyRunningException and treat it as 'compaction already handled'
  3. Reduce trigger frequency for scheduled compaction or add jitter/locking around the trigger call
  4. Investigate why compaction is slow (huge backlog, resource limits) if it seems permanently stuck

Example fix

// before
admin.topics().triggerCompaction(topic); // throws if already running
// after
if (!LongRunningProcessStatus.Status.RUNNING.equals(admin.topics().compactionStatus(topic).status)) {
    admin.topics().triggerCompaction(topic);
}
Defensive patterns

Strategy: try-catch

Validate before calling

LongRunningProcessStatus st = admin.topics().compactionStatus(topic);
if (st.status == LongRunningProcessStatus.Status.RUNNING) {
    return; // skip trigger
}

Type guard

boolean canTriggerCompaction(LongRunningProcessStatus s) {
    return s != null && s.status != LongRunningProcessStatus.Status.RUNNING;
}

Try / catch

try {
    admin.topics().triggerCompaction(topic);
} catch (PulsarAdminException e) {
    if (e.getCause() instanceof AlreadyRunningException) {
        log.info("Compaction already running on {}", topic);
    } else throw e;
}

Prevention

When it happens

Trigger: Calling admin topics().triggerCompaction() (REST or CLI) on a topic whose currentCompaction future is not yet done — e.g. invoking it twice in a row, or an automated job firing while a long-running compaction is still active.

Common situations: Scheduled compaction jobs overlapping with manually triggered compaction; very large topics where compaction takes hours; CI scripts retrying triggerCompaction without checking status; cron intervals shorter than compaction duration.

Related errors


AI-assisted analysis of apache/pulsar@820761864e (2026-09-06). Data as JSON: /api/errors/cecda634cbbbc7f1. Report an issue: GitHub.