{"record":{"id":"464a5ce21d90e67a","repo":"apache/pulsar","slug":"error-compacting-lasterror","errorCode":null,"errorMessage":"Error compacting: ${lastError}","messagePattern":"Error compacting: (.+?)","errorType":"exception","errorClass":"PulsarAdminException","httpStatus":null,"severity":"error","filePath":"pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java","lineNumber":1325,"sourceCode":"                while (wait && status.status == LongRunningProcessStatus.Status.RUNNING) {\n                    Thread.sleep(1000);\n                    status = getTopics().compactionStatus(persistentTopic);\n                }\n\n                switch (status.status) {\n                case NOT_RUN:\n                    System.out.println(\"Compaction has not been run for \" + persistentTopic\n                                       + \" since broker startup\");\n                    break;\n                case RUNNING:\n                    System.out.println(\"Compaction is currently running\");\n                    break;\n                case SUCCESS:\n                    System.out.println(\"Compaction was a success\");\n                    break;\n                case ERROR:\n                    System.out.println(\"Error in compaction\");\n                    throw new PulsarAdminException(\"Error compacting: \" + status.lastError);\n                }\n            } catch (InterruptedException e) {\n                throw new PulsarAdminException(e);\n            }\n        }\n    }\n\n    static MessageId findFirstLedgerWithinThreshold(List<PersistentTopicInternalStats.LedgerInfo> ledgers,\n                                                    long sizeThreshold) {\n        long suffixSize = 0L;\n\n        ledgers = Lists.reverse(ledgers);\n        long previousLedger = ledgers.get(0).ledgerId;\n        for (PersistentTopicInternalStats.LedgerInfo l : ledgers) {\n            suffixSize += l.size;\n            if (suffixSize > sizeThreshold) {\n                return new MessageIdImpl(previousLedger, 0L, -1);\n            }","sourceCodeStart":1307,"sourceCodeEnd":1343,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java#L1307-L1343","documentation":"During manual topic compaction, the command polls compaction status; if the status transitions to ERROR, it prints 'Error in compaction' and throws PulsarAdminException embedding the broker-reported lastError. This indicates the compaction itself failed server-side, not a CLI input problem.","triggerScenarios":"Running `pulsar-admin topics compact` (and waiting/`status`) when the broker-side compaction fails — e.g. bookkeeper write errors, ledger read failures, or the compaction worker being unavailable.","commonSituations":"BookKeeper cluster issues (disk full, ledger corruption); compaction service not deployed/misconfigured; topic ledgers unreadable during compaction.","solutions":["Inspect the message's ${lastError} suffix and the broker logs for the root cause","Check BookKeeper health (disk space, ledger availability) for the ledgers backing the topic","Verify the compaction worker/executor is configured and running on the broker","Fix the underlying issue and re-run the compaction command"],"exampleFix":"// before\npulsar-admin topics compact persistent://my-tenant/my-ns/my-topic   # fails with 'Error compacting: ...'\n// after\n# after fixing broker/BK issue\npulsar-admin topics compact persistent://my-tenant/my-ns/my-topic","handlingStrategy":"try-catch","validationCode":"// Check compaction status before/after triggering\nCompactionStatus status = admin.topics().compactionStatus(topic);\nif (status.getStatus() == CompactionStatus.Status.ERROR) {\n    log.error(\"Compaction failed: {}\", status.getLastError());\n}","typeGuard":null,"tryCatchPattern":"try {\n    admin.topics().compact(topic);\n    admin.topics().compactionStatus(topic).waitForCompletion();\n} catch (org.apache.pulsar.client.admin.PulsarAdminException e) {\n    log.error(\"Compaction failed server-side: {}\", e.getMessage(), e);\n    // inspect broker logs / BookKeeper health\n}","preventionTips":["Monitor BookKeeper disk space and ledger health before compacting","Ensure the compaction worker/service is configured on brokers","Check compaction status after triggering instead of assuming success"],"tags":["compaction","broker-error","cli"],"backgroundTag":"compaction-failed","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"}