{"record":{"id":"403d51a4ec2cb5cb","repo":"apache/cassandra","slug":"can-only-initialize-cluster-identifier-once-but-i","errorCode":null,"errorMessage":"Can only initialize cluster identifier once, but it was already set to %d","messagePattern":"Can only initialize cluster identifier once, but it was already set to (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/tcm/ClusterMetadata.java","lineNumber":483,"sourceCode":"    }\n\n    /**\n     * Produce a ClusterMetadata suitable for use as the base state in the INITIALIZE_CMS transformation. This should\n     * only be used on the first CMS node when bootstrapping the CMS after upgrade or in a brand new cluster.\n     * @param clusterIdentifier Unique identifier for split brain detection & protection\n     * @param addresses The NodeAddresses of the first CMS node.\n     * @param version Version info for the first CMS node.\n     * @param location The rack & DC of the first CMS node.\n     * @return ClusterMetadata instance in the correct state to constitute the base state of the INITIALIZE_CMS\n     *         transformation\n     */\n    public ClusterMetadata forceInitializedState(int clusterIdentifier,\n                                                 NodeAddresses addresses,\n                                                 NodeVersion version,\n                                                 Location location)\n    {\n        if (this.metadataIdentifier != EMPTY_METADATA_IDENTIFIER)\n            throw new IllegalStateException(String.format(\"Can only initialize cluster identifier once, but it was already set to %d\", this.metadataIdentifier));\n\n        if (clusterIdentifier == EMPTY_METADATA_IDENTIFIER)\n            throw new IllegalArgumentException(\"Can not initialize cluster with empty cluster identifier\");\n\n        if (this.epoch.isAfter(Epoch.FIRST))\n            throw new IllegalStateException(String.format(\"Can only initialize cluster identifier during epoch %d, but current epoch is %d\", Epoch.FIRST.getEpoch(), epoch.getEpoch()));\n\n        // Maybe register the first CMS node. If upgrading from gossip, this should be a no-op\n        Directory withRegistered = directory.with(addresses, location, version);\n        NodeId firstNode = withRegistered.peerId(addresses.broadcastAddress);\n        if (firstNode == null)\n            throw new IllegalStateException(\"Failed to find first CMS node in directory\");\n\n        CMSMembership initialCMS = cmsMembership.startJoining(firstNode).finishJoining(firstNode);\n        return new ClusterMetadata(clusterIdentifier,\n                                   epoch,\n                                   partitioner,\n                                   schema,","sourceCodeStart":465,"sourceCodeEnd":501,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/tcm/ClusterMetadata.java#L465-L501","documentation":"Thrown by ClusterMetadata.forceInitializedState when the cluster identifier (metadataIdentifier) has already been initialized. The identifier can only be set once, transitioning metadata from the empty/uninitialized state; a second call would silently re-identify the cluster.","triggerScenarios":"Calling forceInitializedState a second time on a ClusterMetadata whose metadataIdentifier != EMPTY_METADATA_IDENTIFIER.","commonSituations":"Re-running cluster bootstrap or metadata initialization tooling twice, replaying an initialization transformation from a snapshot/log, or a retry path re-invoking initialization after a partially successful run.","solutions":["Skip the initialization if metadataIdentifier is already set (idempotency check before the call).","Load the existing cluster metadata instead of re-initializing.","Remove duplicate initialization calls from retry logic."],"exampleFix":"// before\nmetadata.forceInitializedState(id, addresses, version, location);\n// after\nif (metadata.metadataIdentifier == ClusterMetadata.EMPTY_METADATA_IDENTIFIER)\n    metadata.forceInitializedState(id, addresses, version, location);","handlingStrategy":"validation","validationCode":"if (metadata.metadataIdentifier != ClusterMetadata.EMPTY_METADATA_IDENTIFIER) { /* already initialized */ return; }","typeGuard":null,"tryCatchPattern":"try { metadata.forceInitializedState(id, addr, ver, loc); } catch (IllegalStateException e) { logger.info(\"Cluster already initialized: {}\", e.getMessage()); }","preventionTips":["Guard initialization with an identifier check","Avoid re-running bootstrap tooling against initialized clusters","Make initialization idempotent at the call site"],"tags":["tcm","cluster-bootstrap","invalid-state-transition"],"backgroundTag":"invalid-state-transition","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}