Under the right circumstances Compact could leave a danling last msg block leading to error opening msg block file [""]: open : no such file or directory

When the compaction would not uplevel to a normal purge, but after completion all msg blocks were empty the mb.lmb was not cleared or reset properly.

Signed-off-by: Derek Collison <derek@nats.io>
This commit is contained in:
Derek Collison
2022-12-15 06:59:54 -08:00
parent d937e92d55
commit a85318bc76
2 changed files with 30 additions and 0 deletions

View File

@@ -4911,6 +4911,11 @@ SKIP:
}
// Update blks slice.
fs.blks = copyMsgBlocks(fs.blks[deleted:])
if lb := len(fs.blks); lb == 0 {
fs.lmb = nil
} else {
fs.lmb = fs.blks[lb-1]
}
}
// Update top level accounting.

View File

@@ -4833,3 +4833,28 @@ func TestFileStoreBadFirstAndFailedExpireAfterRestart(t *testing.T) {
require_True(t, state.LastSeq == 9)
require_True(t, state.LastTime == ts)
}
func TestFileStoreCompactAllWithDanglingLMB(t *testing.T) {
storeDir := t.TempDir()
fs, err := newFileStore(
FileStoreConfig{StoreDir: storeDir},
StreamConfig{Name: "zzz", Subjects: []string{"foo"}, Storage: FileStorage},
)
require_NoError(t, err)
defer fs.Stop()
subj, msg := "foo", []byte("ZZ")
for i := 0; i < 100; i++ {
_, _, err := fs.StoreMsg(subj, nil, msg)
require_NoError(t, err)
}
fs.RemoveMsg(100)
purged, err := fs.Compact(100)
require_NoError(t, err)
require_True(t, purged == 99)
_, _, err = fs.StoreMsg(subj, nil, msg)
require_NoError(t, err)
}