Skip to content

Commit

Permalink
mitigation for nsqio#28 possible data corruption if the metadata for …
Browse files Browse the repository at this point in the history
…a queue was not synced on previous shutdown
  • Loading branch information
Fazal Majid committed Jul 2, 2021
1 parent 99f6193 commit fc5265b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions diskqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,29 @@ func (d *diskQueue) retrieveMetaData() error {
d.nextReadFileNum = d.readFileNum
d.nextReadPos = d.readPos

// if the metadata was not sync'd at the last shutdown of nsqd
// then the actual file size might actually be larger than the writePos,
// in which case the safest thing to do is skip to the next file for
// writes, and let the reader salvage what it can from the messages in the
// diskqueue beyond the metadata's likely also stale readPos
fileName = d.fileName(d.writeFileNum)
fileInfo, err := os.Stat(fileName)
if err != nil {
return err
}
fileSize := fileInfo.Size()
if d.writePos < fileSize {
d.logf(WARN,
"DISKQUEUE(%s) %s metadata writePos %d < file size of %d, skipping to new file",
d.name, fileName, d.writePos, fileSize)
d.writeFileNum += 1
d.writePos = 0
if d.writeFile != nil {
d.writeFile.Close()
d.writeFile = nil
}
}

return nil
}

Expand Down

0 comments on commit fc5265b

Please sign in to comment.