apache/pulsar · warning · SchedulerManager.RebalanceInProgressException

Rebalance already in progress

Error message

Rebalance already in progress

What it means

SchedulerManager.rebalanceIfNotInprogress() uses an atomic rebalanceInProgress flag; if a rebalance was already triggered and has not finished, a second call throws RebalanceInProgressException ('Rebalance already in progress'). This is a concurrency guard, not a fault.

Source

Thrown at pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/SchedulerManager.java:253

    private Future<?> rebalance() {
        return scheduleInternal(() -> {
            workerStatsManager.rebalanceTotalExecTimeStart();
            invokeRebalance();
            workerStatsManager.rebalanceTotalExecTimeEnd();
        }, "Encountered error when invoking rebalance");
    }

    public Future<?> rebalanceIfNotInprogress() {
        if (rebalanceInProgress.compareAndSet(false, true)) {
            int numWorkers = getCurrentAvailableNumWorkers();
            if (numWorkers <= 1) {
                rebalanceInProgress.set(false);
                throw new TooFewWorkersException();
            }
            return rebalance();
        } else {
            throw new RebalanceInProgressException();
        }
    }

    private Future<?> drain(String workerId) {
        return scheduleInternal(() -> {
            workerStatsManager.drainTotalExecTimeStart();
            assignmentsMovedInLastDrain = invokeDrain(workerId);
            workerStatsManager.drainTotalExecTimeEnd();
        }, "Encountered error when invoking drain");
    }

    public Future<?> drainIfNotInProgress(String workerId) {
        if (drainInProgressFlag.compareAndSet(false, true)) {
            try {
                Set<String> availableWorkers = getCurrentAvailableWorkers();
                if (availableWorkers.size() <= 1) {
                    throw new TooFewWorkersException();
                }

View on GitHub (pinned to 820761864e)

Solutions

  1. Wait for the in-progress rebalance to finish before triggering another (poll or just retry after a delay).
  2. Treat this exception as benign — the rebalance you wanted is already running.
  3. Serialize rebalance invocations in automation (lock or single dispatcher).

Example fix

// before
try { worker.rebalanceIfNotInprogress(); } catch (RebalanceInProgressException e) { throw e; }
// after
try {
    worker.rebalanceIfNotInprogress();
} catch (RebalanceInProgressException e) {
    log.info("Rebalance already running, nothing to do");
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
    worker.rebalanceIfNotInprogress();
} catch (RebalanceInProgressException e) {
    // benign: rebalance already running; optionally wait and re-check
}

Prevention

When it happens

Trigger: Calling the rebalance trigger twice concurrently or calling again before a previously triggered rebalance completes; multiple admin clients/scripts triggering rebalance at the same time.

Common situations: Retry loops with too-short intervals; monitoring/alerting systems that fire rebalance repeatedly; two operators running the rebalance command simultaneously.

Related errors


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