{"record":{"id":"30bfdc345f5248fe","repo":"dgraph-io/badger","slug":"keyrange-not-found","errorCode":null,"errorMessage":"keyRange not found","messagePattern":"keyRange not found","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compaction.go","lineNumber":231,"sourceCode":"\n\tthisLevel.delSize -= cd.thisSize\n\tfound := thisLevel.remove(cd.thisRange)\n\t// The following check makes sense only if we're compacting more than one\n\t// table. In case of the max level, we might rewrite a single table to\n\t// remove stale data.\n\tif cd.thisLevel != cd.nextLevel && !cd.nextRange.isEmpty() {\n\t\tfound = nextLevel.remove(cd.nextRange) && found\n\t}\n\n\tif !found {\n\t\tthis := cd.thisRange\n\t\tnext := cd.nextRange\n\t\tfmt.Printf(\"Looking for: %s in this level %d.\\n\", this, tl)\n\t\tfmt.Printf(\"This Level:\\n%s\\n\", thisLevel.debug())\n\t\tfmt.Println()\n\t\tfmt.Printf(\"Looking for: %s in next level %d.\\n\", next, cd.nextLevel.level)\n\t\tfmt.Printf(\"Next Level:\\n%s\\n\", nextLevel.debug())\n\t\tlog.Fatal(\"keyRange not found\")\n\t}\n\tfor _, t := range append(cd.top, cd.bot...) {\n\t\t_, ok := cs.tables[t.ID()]\n\t\ty.AssertTrue(ok)\n\t\tdelete(cs.tables, t.ID())\n\t}\n}\n","sourceCodeStart":213,"sourceCodeEnd":239,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/compaction.go#L213-L239","documentation":"During compaction, delete() looks up the keyRange of the ranges being compacted in the level's table set; if the expected keyRange cannot be found, badger prints debug dumps of both levels and calls log.Fatal, crashing the process. This is an internal invariant violation: the compaction's assumed table layout does not match actual level state.","triggerScenarios":"A compaction (doCompact/applyManifestChange) references tables whose key ranges are missing from the expected level — usually caused by a corrupt or inconsistent MANIFEST, concurrent uncoordinated access to the same badger directory, or memory/bug-induced drift between cs.tables and the levels.","commonSituations":"Two processes opening the same badger dir; a partially written MANIFEST after a crash; hand-editing/removing table files; running a badger version against a directory written by an incompatible version.","solutions":["Stop any other process using the same directory and reopen with a single instance","Restore from a known-good backup; the MANIFEST/table layout is inconsistent and manual repair is unsupported","Do not delete or rename .sst/MANIFEST files by hand; use DropAll/DropPrefix APIs instead","Check badger version compatibility with the on-disk data (upgrade via proper migration/backup-restore, not file copy)"],"exampleFix":"// before: two openers on one dir\nsvcA, _ := badger.Open(opt)\nsvcB, _ := badger.Open(opt) // races -> 'keyRange not found' log.Fatal\n// after: single owner\nif !acquiredDirLock(dir) {\n    return errors.New(\"badger directory already in use\")\n}\ndb, err := badger.Open(opt)","handlingStrategy":"try-catch","validationCode":"// before open: assert single ownership and healthy MANIFEST presence\nif fi, err := os.Stat(filepath.Join(dir, \"MANIFEST\")); err != nil || fi.IsDir() {\n    return fmt.Errorf(\"missing/corrupt MANIFEST in %s\", dir)\n}","typeGuard":null,"tryCatchPattern":"// this error path calls log.Fatal and crashes the process; guard at supervisor level\n// run badger under a supervisor (systemd/k8s) that captures stderr containing\n// 'keyRange not found' + level dumps, then restores from backup before restart","preventionTips":["Enforce one badger process per directory via OS-level locks","Never delete/rename .sst or MANIFEST files manually; use DropAll/DropPrefix","Pin badger versions; avoid opening directories written by incompatible versions","Take regular backups so an inconsistent MANIFEST can be answered with restore, not repair"],"tags":["badger","compaction","invariant-violation","manifest","fatal"],"backgroundTag":"compaction-state-inconsistent","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}