From d1125c41994c50b8d75fab2ceaf59f1acc3014f2 Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Wed, 20 Jan 2021 21:12:43 +0530 Subject: [PATCH] fix(compaction): Set base level correctly after stream (#1631) When we use stream writer, all the data is written to the last level. On the restart, badger will try to figure out the correct base level. This computation of base level is incorrect right now because of which we pick the baseLevel which has levels below it that are empty. --- levels.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/levels.go b/levels.go index 34fc45b82..b34b28cc4 100644 --- a/levels.go +++ b/levels.go @@ -412,6 +412,22 @@ func (s *levelsController) levelTargets() targets { t.fileSz[i] = tsz } } + + // Bring the base level down to the last empty level. + for i := t.baseLevel + 1; i < len(s.levels)-1; i++ { + if s.levels[i].getTotalSize() > 0 { + break + } + t.baseLevel = i + } + + // If the base level is empty and the next level size is less than the + // target size, pick the next level as the base level. + b := t.baseLevel + lvl := s.levels + if b < len(lvl)-1 && lvl[b].getTotalSize() == 0 && lvl[b+1].getTotalSize() < t.targetSz[b+1] { + t.baseLevel++ + } return t }