{"record":{"id":"f2b3325988f4fdf8","repo":"apache/cassandra","slug":"sstable-first-key-s-last-key-s","errorCode":null,"errorMessage":"SSTable first key %s > last key %s","messagePattern":"SSTable first key (.+?) > last key (.+?)","errorType":"exception","errorClass":"CorruptSSTableException","httpStatus":null,"severity":"critical","filePath":"src/java/org/apache/cassandra/io/sstable/format/SSTableReader.java","lineNumber":696,"sourceCode":"        }\n    }\n\n    /**\n     * This method is expected to close the components which occupy memory but are not needed when we just want to\n     * stream the components (for example, when SSTable is opened with SSTableLoader). The method should call\n     * {@link #closeInternalComponent(AutoCloseable)} for each such component. Leaving the implementation empty is\n     * valid given there are not such resources to release.\n     */\n    public abstract void releaseInMemoryComponents();\n\n    /**\n     * Perform any validation needed for the reader upon creation before returning it from the {@link Builder}.\n     */\n    public void validate()\n    {\n        if (this.first.compareTo(this.last) > 0 || bounds == null)\n        {\n            throw new CorruptSSTableException(new IllegalStateException(String.format(\"SSTable first key %s > last key %s\", this.first, this.last)), getFilename());\n        }\n    }\n\n    /**\n     * Returns the compression metadata for this sstable. Note that the compression metdata is a resource and should not\n     * be closed by the caller.\n     * TODO do not return a closeable resource or return a shared copy\n     *\n     * @throws IllegalStateException if the sstable is not compressed\n     */\n    public CompressionMetadata getCompressionMetadata()\n    {\n        if (!compression)\n            throw new IllegalStateException(this + \" is not compressed\");\n\n        return dfile.compressionMetadata().get();\n    }\n","sourceCodeStart":678,"sourceCodeEnd":714,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/io/sstable/format/SSTableReader.java#L678-L714","documentation":"SSTableReader.validate() is called when a reader is built and asserts that the SSTable's first decorated key sorts before (or equals) its last key and that the token bounds exist. A violation means the SSTable's index/stats metadata is internally inconsistent, so Cassandra wraps it in a CorruptSSTableException.","triggerScenarios":"Opening an SSTable whose Statistics.db / Summary component reports first key > last key — e.g. a truncated, partially-written, or hand-edited SSTable, or a corrupted -Statistics.db during startup, compaction, or validation when building the reader.","commonSituations":"Hardware failures or unclean shutdown corrupting SSTable components; manually copying SSTables between nodes; aborted/interfered compactions leaving inconsistent metadata; tampering with SSTable files.","solutions":["Run nodetool scrub on the affected keyspace/table to move bad data out or drop corrupted SSTables","Run nodetool repair (and sstableverify) after scrub to restore replication consistency","Restore the corrupted SSTable from a backup/snapshot if available","If the SSTable came from an external source, re-generate or re-load it correctly (proper sstableloader usage)"],"exampleFix":"// before: blindly copying sstables between nodes\ncp /var/lib/cassandra/data/ks/tbl-*/xx-Data.db /other/node/\n// after: use sstableloader\nsstableloader -d other.node /var/lib/cassandra/data/ks/tbl/","handlingStrategy":"validation","validationCode":"// pre-open sanity: verify components\nsstableverify -- ks tbl   // or: nodetool verify ks tbl\nsstablemetadata <path>/xx-big-Statistics.db  // check first/last tokens","typeGuard":null,"tryCatchPattern":"try { SSTableReader.open(descriptor, components, metadata); }\ncatch (CorruptSSTableException e) {\n    logger.error(\"SSTable {} has invalid first/last key metadata\", descriptor, e);\n    scheduleScrubAndRepair(keyspace, table);\n}","preventionTips":["Run periodic nodetool verify sweeps","Use ECC/RAID storage and monitor SMART data","Never hand-edit or truncate SSTable files","Always move SSTables via supported tools (sstableloader, streaming)"],"tags":["sstable","corruption","validation"],"backgroundTag":"checksum-mismatch","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}