dgraph-io/badger · error

Filesizes cannot be zero. Targets are not set

Error message

Filesizes cannot be zero. Targets are not set

What it means

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.

Source

Thrown at levels.go:1486

			}
			return true
		}
		cd.nextRange = getKeyRange(cd.bot...)

		if s.cstatus.overlapsWith(cd.nextLevel.level, cd.nextRange) {
			continue
		}
		if !s.cstatus.compareAndAdd(thisAndNextLevelRLocked{}, *cd) {
			continue
		}
		return true
	}
	return false
}

func (s *levelsController) runCompactDef(id, l int, cd compactDef) (err error) {
	if len(cd.t.fileSz) == 0 {
		return errors.New("Filesizes cannot be zero. Targets are not set")
	}

	if cd.thisLevel.level == 0 {
		for i := cd.nextLevel.level - 1; i > 0; i-- {
			if s.levels[i].getTotalSize() > 0 {
				return fmt.Errorf("there is a non-empty level %d above base level %d", i, cd.nextLevel.level)
			}
		}
	}

	timeStart := time.Now()

	thisLevel := cd.thisLevel
	nextLevel := cd.nextLevel

	y.AssertTrue(len(cd.splits) == 0)
	if thisLevel.level == nextLevel.level {
		// don't do anything for L0 -> L0 and Lmax -> Lmax.

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Initialize cd.t with s.levelTargets() before running the compaction definition
  2. Avoid invoking internal compaction methods directly; rely on automatic compaction (db.Opts.CompactL0OnClose, etc.)
  3. Ensure the DB is fully opened (levelsController built) before triggering compactions

Example fix

// before
cd := compactDef{thisLevel: s.levels[0], nextLevel: s.levels[1]}
s.runCompactDef(id, 0, cd) // error: targets not set
// after
cd := compactDef{thisLevel: s.levels[0], nextLevel: s.levels[1]}
cd.t = s.levelTargets() // populate file sizes first
s.runCompactDef(id, 0, cd)
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: check targets before running a def yourself
if len(cd.t.fileSz) == 0 {
    cd.t = s.levelTargets()
}

Try / catch

// Go
if err := s.runCompactDef(id, l, cd); err != nil {
    if strings.Contains(err.Error(), "Filesizes cannot be zero") {
        cd.t = s.levelTargets()
        err = s.runCompactDef(id, l, cd)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05). Data as JSON: /api/errors/0d6036cc200d015f. Report an issue: GitHub.