{"record":{"id":"6ae93d8222ba6154","repo":"apache/cassandra","slug":"invalid-key-order-current-s-previous-s","errorCode":null,"errorMessage":"Invalid key order: current %s <= previous %s","messagePattern":"Invalid key order: current (.+?) <= previous (.+?)","errorType":"exception","errorClass":"CorruptSSTableException","httpStatus":null,"severity":"critical","filePath":"src/java/org/apache/cassandra/io/sstable/format/SSTableSimpleScanner.java","lineNumber":218,"sourceCode":"            }\n            else\n            {\n                throw e;\n            }\n        }\n    }\n\n    public UnfilteredRowIterator next()\n    {\n        if (!hasNext())\n            throw new NoSuchElementException();\n\n        currentIterator = SSTableIdentityIterator.create(sstable, tableMetadata, dfile, false);\n        DecoratedKey currentKey = currentIterator.partitionKey();\n        if (lastKey != null && lastKey.compareTo(currentKey) >= 0)\n        {\n            sstable.markSuspect();\n            throw new CorruptSSTableException(new IllegalStateException(String.format(\"Invalid key order: current %s <= previous %s\",\n                                                                                      currentKey,\n                                                                                      lastKey)),\n                                              sstable.getFilename());\n        }\n        lastKey = currentKey;\n        return currentIterator;\n    }\n\n    public void remove()\n    {\n        throw new UnsupportedOperationException();\n    }\n\n    @Override\n    public String toString()\n    {\n        return String.format(\"%s(sstable=%s)\", getClass().getSimpleName(), sstable);\n    }","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/io/sstable/format/SSTableSimpleScanner.java#L200-L236","documentation":"While iterating partitions in key order, SSTableSimpleScanner.next() checks that each newly read partition key sorts strictly after the previous one. If currentKey <= lastKey, the SSTable is not in valid sorted order, so the reader is marked suspect and a CorruptSSTableException is thrown naming both keys.","triggerScenarios":"Scanning an SSTable whose Data.db partition order violates the sort order guaranteed by the storage engine — caused by data corruption, truncation, or files written by broken/external tooling; detected during next() as the scanner walks partitions sequentially.","commonSituations":"Hardware corruption or bad disks altering SSTable contents; SSTables produced/modified by third-party tools; incomplete writes from crashed compactions being scanned directly.","solutions":["Run nodetool scrub on the table to isolate/discard the corrupt SSTable","Run sstableverify / nodetool verify to identify all corrupted files","Replace the bad SSTable by running nodetool repair so other replicas restore the data","Remove the suspect SSTable from external scanning; re-generate it via compaction or streaming from a healthy replica"],"exampleFix":"// before: directly scanning suspect sstable\nSSTableSimpleScanner sc = new SSTableSimpleScanner(sstable, ranges);\n// after: verify first\nVerifierOptions opts = new VerifierOptions.Builder().build();\nnew SortedTableVerifier(cfs, sstable, opts, new NoopOutputHandler(), false).verify();\nSSTableSimpleScanner sc = new SSTableSimpleScanner(sstable, ranges);","handlingStrategy":"validation","validationCode":"// verify integrity before scanning\nnew SortedTableVerifier(cfs, sstable, verifierOptions, outputHandler, false).verify();","typeGuard":null,"tryCatchPattern":"try { iterate(scanner); }\ncatch (CorruptSSTableException e) {\n    if (e.getMessage().startsWith(\"Invalid key order\")) {\n        sstable.markSuspect();\n        logger.error(\"SSTable {} out of key order, scheduling scrub\", sstable.getFilename(), e);\n        scheduleScrubAndRepair();\n    } else throw e;\n}","preventionTips":["Run nodetool verify periodically to catch key-order corruption early","Monitor disk health (SMART) to catch bit rot","Avoid third-party tools that write SSTables directly","Scrub after unclean shutdowns before heavy reads"],"tags":["sstable","corruption","iteration"],"backgroundTag":"checksum-mismatch","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"}