Skip to content

Commit

Permalink
Fix for test and small tweak to not set last to corrupted sequence. A…
Browse files Browse the repository at this point in the history
…lso small bug fix for leaking fd

Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison committed Aug 31, 2023
1 parent 2834142 commit 214a101
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
10 changes: 6 additions & 4 deletions server/filestore.go
Expand Up @@ -1032,7 +1032,7 @@ func (mb *msgBlock) rebuildStateLocked() (*LostStreamData, error) {
fd = mb.mfd
} else {
fd, err = os.OpenFile(mb.mfn, os.O_RDWR, defaultFilePerms)
if err != nil {
if err == nil {
defer fd.Close()
}
}
Expand Down Expand Up @@ -1114,9 +1114,11 @@ func (mb *msgBlock) rebuildStateLocked() (*LostStreamData, error) {
_, deleted = mb.dmap[seq]
}

// Always set last.
mb.last.seq = seq
mb.last.ts = ts
// Always set last if not > startLastSeq
if seq <= startLastSeq {
mb.last.seq = seq
mb.last.ts = ts
}

if !deleted {
data := buf[index+msgHdrSize : index+rl]
Expand Down
14 changes: 12 additions & 2 deletions server/filestore_test.go
Expand Up @@ -1278,7 +1278,10 @@ func TestFileStoreBitRot(t *testing.T) {
// Now twiddle some bits.
fs.mu.Lock()
lmb := fs.lmb
contents, _ := os.ReadFile(lmb.mfn)
contents, err := os.ReadFile(lmb.mfn)
require_NoError(t, err)
require_True(t, len(contents) > 0)

var index int
for {
index = rand.Intn(len(contents))
Expand All @@ -1296,6 +1299,10 @@ func TestFileStoreBitRot(t *testing.T) {
if len(ld.Msgs) > 0 {
break
}
// If our bitrot caused us to not be able to recover any messages we can break as well.
if state := fs.State(); state.Msgs == 0 {
break
}
// Fail the test if we have tried the 10 times and still did not
// get any corruption report.
if i == 9 {
Expand All @@ -1314,7 +1321,10 @@ func TestFileStoreBitRot(t *testing.T) {

// checkMsgs will repair the underlying store, so checkMsgs should be clean now.
if ld := fs.checkMsgs(); ld != nil {
t.Fatalf("Expected no errors restoring checked and fixed filestore, got %+v", ld)
// If we have no msgs left this will report the head msgs as lost again.
if state := fs.State(); state.Msgs > 0 {
t.Fatalf("Expected no errors restoring checked and fixed filestore, got %+v", ld)
}
}
})
}
Expand Down

0 comments on commit 214a101

Please sign in to comment.