From 7e986c0bd6c1da95a05fbe11f75272d829b8c463 Mon Sep 17 00:00:00 2001 From: Maurice van Veen Date: Sun, 28 Jul 2024 17:41:53 +0200 Subject: [PATCH] Fix 'checkSkipFirstBlock' with '_EMPTY_' filter Signed-off-by: Maurice van Veen --- server/filestore.go | 5 +++++ server/filestore_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/server/filestore.go b/server/filestore.go index e059d28bec4..a6b97b6f62b 100644 --- a/server/filestore.go +++ b/server/filestore.go @@ -2610,6 +2610,11 @@ func (fs *fileStore) FilteredState(sseq uint64, subj string) SimpleState { // This is used to see if we can selectively jump start blocks based on filter subject and a floor block index. // Will return -1 if no matches at all. func (fs *fileStore) checkSkipFirstBlock(filter string, wc bool) (int, int) { + if filter == _EMPTY_ { + filter = fwcs + wc = true + } + start, stop := uint32(math.MaxUint32), uint32(0) if wc { fs.psim.Match(stringToBytes(filter), func(_ []byte, psi *psi) { diff --git a/server/filestore_test.go b/server/filestore_test.go index e3f40a89c98..b7adb8d77de 100644 --- a/server/filestore_test.go +++ b/server/filestore_test.go @@ -7248,6 +7248,28 @@ func TestFileStoreCheckSkipFirstBlockBug(t *testing.T) { require_NoError(t, err) } +// https://github.com/nats-io/nats-server/issues/5705 +func TestFileStoreCheckSkipFirstBlockEmptyFilter(t *testing.T) { + sd := t.TempDir() + fs, err := newFileStore( + FileStoreConfig{StoreDir: sd, BlockSize: 128}, + StreamConfig{Name: "zzz", Subjects: []string{"foo.*.*"}, Storage: FileStorage}) + require_NoError(t, err) + defer fs.Stop() + + msg := []byte("hello") + // Create 4 blocks, each block holds 2 msgs + for i := 0; i < 4; i++ { + fs.StoreMsg("foo.22.bar", nil, msg) + fs.StoreMsg("foo.22.baz", nil, msg) + } + require_Equal(t, fs.numMsgBlocks(), 4) + + nbi, lbi := fs.checkSkipFirstBlock(_EMPTY_, false) + require_Equal(t, nbi, 0) + require_Equal(t, lbi, 3) +} + /////////////////////////////////////////////////////////////////////////// // Benchmarks ///////////////////////////////////////////////////////////////////////////