{"record":{"id":"c110ec3c5c647a3b","repo":"apache/pulsar","slug":"too-few-workers-need-at-least-2","errorCode":null,"errorMessage":"Too few workers (need at least 2)","messagePattern":"Too few workers \\(need at least 2\\)","errorType":"http","errorClass":"SchedulerManager.TooFewWorkersException","httpStatus":400,"severity":"error","filePath":"pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/SchedulerManager.java","lineNumber":249,"sourceCode":"            invokeScheduler();\n            workerStatsManager.scheduleTotalExecTimeEnd();\n        }, \"Encountered error when invoking scheduler\");\n    }\n\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 {","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/SchedulerManager.java#L231-L267","documentation":"SchedulerManager.rebalanceIfNotInprogress() refuses to trigger a cluster rebalance when the number of currently available workers is <= 1, throwing TooFewWorkersException ('Too few workers (need at least 2)'). Rebalancing assignments requires at least two live workers to move anything.","triggerScenarios":"Calling the rebalance trigger (e.g. POST /rebalance on the worker REST API or CLI `pulsar-admin functions workers rebalance`) while only one (or zero) workers are registered/available.","commonSituations":"Single-node dev/test cluster where rebalance is meaningless; other workers crashed or are disconnected so only one worker is available; workers still initializing after a cluster restart.","solutions":["Start at least one more Functions worker and wait for it to register (it must be listed among available workers).","Check why other workers are down/disconnected (network, zookeeper/metadata store, config) and restore them.","Skip the rebalance call — with a single worker no redistribution is possible."],"exampleFix":"// before\nworker.rebalanceIfNotInprogress(); // throws when cluster has 1 worker\n// after\nif (worker.getCurrentAvailableNumWorkers() > 1) {\n    worker.rebalanceIfNotInprogress();\n} else {\n    log.info(\"Skipping rebalance: fewer than 2 workers available\");\n}","handlingStrategy":"validation","validationCode":"// check available worker count first\nif (numAvailableWorkers <= 1) {\n    log.warn(\"Rebalance skipped: need at least 2 available workers\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"try {\n    worker.rebalanceIfNotInprogress();\n} catch (TooFewWorkersException e) {\n    log.warn(\"Cannot rebalance with <=1 worker; start more workers\");\n}","preventionTips":["Ensure >=2 workers in any cluster where rebalance will be triggered","Monitor worker registration health","Skip rebalance automation on single-node clusters"],"tags":["pulsar-functions","rebalance","cluster-size"],"backgroundTag":"too-few-workers","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"}