Skip to content

Commit fc5265b

Browse files
author
Fazal Majid
committed
mitigation for nsqio#28 possible data corruption if the metadata for a queue was not synced on previous shutdown
1 parent 99f6193 commit fc5265b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

diskqueue.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,29 @@ func (d *diskQueue) retrieveMetaData() error {
473473
d.nextReadFileNum = d.readFileNum
474474
d.nextReadPos = d.readPos
475475

476+
// if the metadata was not sync'd at the last shutdown of nsqd
477+
// then the actual file size might actually be larger than the writePos,
478+
// in which case the safest thing to do is skip to the next file for
479+
// writes, and let the reader salvage what it can from the messages in the
480+
// diskqueue beyond the metadata's likely also stale readPos
481+
fileName = d.fileName(d.writeFileNum)
482+
fileInfo, err := os.Stat(fileName)
483+
if err != nil {
484+
return err
485+
}
486+
fileSize := fileInfo.Size()
487+
if d.writePos < fileSize {
488+
d.logf(WARN,
489+
"DISKQUEUE(%s) %s metadata writePos %d < file size of %d, skipping to new file",
490+
d.name, fileName, d.writePos, fileSize)
491+
d.writeFileNum += 1
492+
d.writePos = 0
493+
if d.writeFile != nil {
494+
d.writeFile.Close()
495+
d.writeFile = nil
496+
}
497+
}
498+
476499
return nil
477500
}
478501

0 commit comments

Comments
 (0)