{"record":{"id":"0d6036cc200d015f","repo":"dgraph-io/badger","slug":"filesizes-cannot-be-zero-targets-are-not-set","errorCode":null,"errorMessage":"Filesizes cannot be zero. Targets are not set","messagePattern":"Filesizes cannot be zero\\. Targets are not set","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"levels.go","lineNumber":1486,"sourceCode":"\t\t\t}\n\t\t\treturn true\n\t\t}\n\t\tcd.nextRange = getKeyRange(cd.bot...)\n\n\t\tif s.cstatus.overlapsWith(cd.nextLevel.level, cd.nextRange) {\n\t\t\tcontinue\n\t\t}\n\t\tif !s.cstatus.compareAndAdd(thisAndNextLevelRLocked{}, *cd) {\n\t\t\tcontinue\n\t\t}\n\t\treturn true\n\t}\n\treturn false\n}\n\nfunc (s *levelsController) runCompactDef(id, l int, cd compactDef) (err error) {\n\tif len(cd.t.fileSz) == 0 {\n\t\treturn errors.New(\"Filesizes cannot be zero. Targets are not set\")\n\t}\n\n\tif cd.thisLevel.level == 0 {\n\t\tfor i := cd.nextLevel.level - 1; i > 0; i-- {\n\t\t\tif s.levels[i].getTotalSize() > 0 {\n\t\t\t\treturn fmt.Errorf(\"there is a non-empty level %d above base level %d\", i, cd.nextLevel.level)\n\t\t\t}\n\t\t}\n\t}\n\n\ttimeStart := time.Now()\n\n\tthisLevel := cd.thisLevel\n\tnextLevel := cd.nextLevel\n\n\ty.AssertTrue(len(cd.splits) == 0)\n\tif thisLevel.level == nextLevel.level {\n\t\t// don't do anything for L0 -> L0 and Lmax -> Lmax.","sourceCodeStart":1468,"sourceCodeEnd":1504,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/levels.go#L1468-L1504","documentation":"runCompactDef requires cd.t.fileSz (per-level target file sizes from levelTargets) to be populated before running a compaction; an empty slice means targets were never computed, which would break output table sizing. The error surfaces when a compaction definition is executed without proper initialization.","triggerScenarios":"Calling runCompactDef/doCompact with a compactDef whose targets (t) were never set via levelTargets(); triggering compactions manually before the levels controller computed size targets; state where opt.MaxLevels-based targets are empty.","commonSituations":"Custom tooling that drives internal compaction APIs; tests or scripts calling doCompact with partial compactionPriority; controller initialization skipped (e.g. using a DB handle before full open completes).","solutions":["Initialize cd.t with s.levelTargets() before running the compaction definition","Avoid invoking internal compaction methods directly; rely on automatic compaction (db.Opts.CompactL0OnClose, etc.)","Ensure the DB is fully opened (levelsController built) before triggering compactions"],"exampleFix":"// before\ncd := compactDef{thisLevel: s.levels[0], nextLevel: s.levels[1]}\ns.runCompactDef(id, 0, cd) // error: targets not set\n// after\ncd := compactDef{thisLevel: s.levels[0], nextLevel: s.levels[1]}\ncd.t = s.levelTargets() // populate file sizes first\ns.runCompactDef(id, 0, cd)","handlingStrategy":"try-catch","validationCode":"// Go: check targets before running a def yourself\nif len(cd.t.fileSz) == 0 {\n    cd.t = s.levelTargets()\n}","typeGuard":null,"tryCatchPattern":"// Go\nif err := s.runCompactDef(id, l, cd); err != nil {\n    if strings.Contains(err.Error(), \"Filesizes cannot be zero\") {\n        cd.t = s.levelTargets()\n        err = s.runCompactDef(id, l, cd)\n    }\n    return err\n}","preventionTips":["Always initialize compactDef.t via levelTargets()","Prefer public compaction entry points over runCompactDef","Only run compactions after the DB is fully opened","Write a unit test around any custom compaction driver"],"tags":["compaction","targets","internal-api"],"backgroundTag":"compaction-targets-unset","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"}