Make setting of mb.first.seq more reliable

Signed-off-by: Derek Collison <derek@nats.io>
This commit is contained in:
Derek Collison
2021-03-14 11:37:43 -07:00
parent ef82de2ef2
commit a2cdbdff2e

View File

@@ -463,6 +463,7 @@ func (mb *msgBlock) rebuildState() (*LostStreamData, error) {
// Clear state we need to rebuild.
mb.msgs, mb.bytes = 0, 0
mb.last.seq, mb.last.ts = 0, 0
mb.first.seq, mb.first.ts = 0, 0
buf, err := ioutil.ReadFile(mb.mfn)
if err != nil {
@@ -514,6 +515,8 @@ func (mb *msgBlock) rebuildState() (*LostStreamData, error) {
return &ld
}
firstNeedsSet := true
for index, lbuf := uint32(0), uint32(len(buf)); index < lbuf; {
if index+msgHdrSize >= lbuf {
truncate(index)
@@ -542,12 +545,6 @@ func (mb *msgBlock) rebuildState() (*LostStreamData, error) {
seq := le.Uint64(hdr[4:])
ts := int64(le.Uint64(hdr[12:]))
// If the first seq we read does not match our indexed first seq, reset.
if index == 0 && seq > mb.first.seq {
mb.first.seq = seq
mb.first.ts = ts
}
// This is an old erased message, or a new one that we can track.
if seq == 0 || seq&ebit != 0 || seq < mb.first.seq {
seq = seq &^ ebit
@@ -582,7 +579,8 @@ func (mb *msgBlock) rebuildState() (*LostStreamData, error) {
copy(mb.lchk[0:], checksum)
}
if mb.first.seq == 0 {
if firstNeedsSet {
firstNeedsSet = false
mb.first.seq = seq
mb.first.ts = ts
}