{"record":{"id":"641eca3b1a6c7a6f","repo":"apache/pulsar","slug":"rebalance-already-in-progress","errorCode":null,"errorMessage":"Rebalance already in progress","messagePattern":"Rebalance already in progress","errorType":"http","errorClass":"SchedulerManager.RebalanceInProgressException","httpStatus":400,"severity":"warning","filePath":"pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/SchedulerManager.java","lineNumber":253,"sourceCode":"\n    private Future<?> rebalance() {\n        return scheduleInternal(() -> {\n            workerStatsManager.rebalanceTotalExecTimeStart();\n            invokeRebalance();\n            workerStatsManager.rebalanceTotalExecTimeEnd();\n        }, \"Encountered error when invoking rebalance\");\n    }\n\n    public Future<?> rebalanceIfNotInprogress() {\n        if (rebalanceInProgress.compareAndSet(false, true)) {\n            int numWorkers = getCurrentAvailableNumWorkers();\n            if (numWorkers <= 1) {\n                rebalanceInProgress.set(false);\n                throw new TooFewWorkersException();\n            }\n            return rebalance();\n        } else {\n            throw new RebalanceInProgressException();\n        }\n    }\n\n    private Future<?> drain(String workerId) {\n        return scheduleInternal(() -> {\n            workerStatsManager.drainTotalExecTimeStart();\n            assignmentsMovedInLastDrain = invokeDrain(workerId);\n            workerStatsManager.drainTotalExecTimeEnd();\n        }, \"Encountered error when invoking drain\");\n    }\n\n    public Future<?> drainIfNotInProgress(String workerId) {\n        if (drainInProgressFlag.compareAndSet(false, true)) {\n            try {\n                Set<String> availableWorkers = getCurrentAvailableWorkers();\n                if (availableWorkers.size() <= 1) {\n                    throw new TooFewWorkersException();\n                }","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/SchedulerManager.java#L235-L271","documentation":"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.","triggerScenarios":"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.","commonSituations":"Retry loops with too-short intervals; monitoring/alerting systems that fire rebalance repeatedly; two operators running the rebalance command simultaneously.","solutions":["Wait for the in-progress rebalance to finish before triggering another (poll or just retry after a delay).","Treat this exception as benign — the rebalance you wanted is already running.","Serialize rebalance invocations in automation (lock or single dispatcher)."],"exampleFix":"// before\ntry { worker.rebalanceIfNotInprogress(); } catch (RebalanceInProgressException e) { throw e; }\n// after\ntry {\n    worker.rebalanceIfNotInprogress();\n} catch (RebalanceInProgressException e) {\n    log.info(\"Rebalance already running, nothing to do\");\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    worker.rebalanceIfNotInprogress();\n} catch (RebalanceInProgressException e) {\n    // benign: rebalance already running; optionally wait and re-check\n}","preventionTips":["Serialize rebalance triggers through a single automation path","Debounce/alert-driven rebalance calls","Poll rebalance completion before issuing the next"],"tags":["pulsar-functions","rebalance","concurrency"],"backgroundTag":"operation-already-in-progress","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}