Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eof edge case fix #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions diskqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,44 @@ func (d *diskQueue) skipToNextRWFile() error {
return err
}

func (d *diskQueue) readRollFile() {
d.nextReadFileNum++
d.nextReadPos = 0

oldReadFileNum := d.readFileNum
d.readFileNum = d.nextReadFileNum
d.readPos = d.nextReadPos

// sync every time we start reading from a new file
d.needSync = true

fn := d.fileName(oldReadFileNum)
err := os.Remove(fn)
if err != nil {
d.logf(ERROR, "DISKQUEUE(%s) failed to Remove(%s) - %s", d.name, fn, err)
}

d.checkTailCorruption(d.depth)
}

// readOne performs a low level filesystem read for a single []byte
// while advancing read positions and rolling files, if necessary
func (d *diskQueue) readOne() ([]byte, error) {
var err error
var msgSize int32

// we only consider rotating if we're reading a "complete" file
// and since we cannot know the size at which it was rotated, we
// rely on maxBytesPerFileRead rather than maxBytesPerFile
if d.readFileNum < d.writeFileNum && d.nextReadPos >= d.maxBytesPerFileRead && d.readFile != nil {
if d.readFile != nil {
d.readFile.Close()
d.readFile = nil
}

d.readRollFile()
}

if d.readFile == nil {
curFileName := d.fileName(d.readFileNum)
d.readFile, err = os.OpenFile(curFileName, os.O_RDONLY, 0600)
Expand Down Expand Up @@ -346,19 +378,6 @@ func (d *diskQueue) readOne() ([]byte, error) {
d.nextReadPos = d.readPos + totalBytes
d.nextReadFileNum = d.readFileNum

// we only consider rotating if we're reading a "complete" file
// and since we cannot know the size at which it was rotated, we
// rely on maxBytesPerFileRead rather than maxBytesPerFile
if d.readFileNum < d.writeFileNum && d.nextReadPos >= d.maxBytesPerFileRead {
if d.readFile != nil {
d.readFile.Close()
d.readFile = nil
}

d.nextReadFileNum++
d.nextReadPos = 0
}

return readBuf, nil
}

Expand Down
42 changes: 42 additions & 0 deletions diskqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ func NewTestLogger(tbl tbLog) AppLogFunc {
}
}

func NewTestLoggerFatalAtError(tbl tbLog, fatalChan chan error) AppLogFunc {
return func(lvl LogLevel, f string, args ...interface{}) {
if lvl == ERROR || lvl == FATAL {
// tbl.Fatal(fmt.Sprintf(lvl.String()+": "+f, args...))
fatalChan <- fmt.Errorf(lvl.String()+": "+f, args...)
}

tbl.Log(fmt.Sprintf(lvl.String()+": "+f, args...))
}
}

func TestDiskQueue(t *testing.T) {
l := NewTestLogger(t)

Expand Down Expand Up @@ -573,6 +584,37 @@ func TestDiskQueueResize(t *testing.T) {
}
}

func TestWriteRollReadEOF(t *testing.T) {
fatalChan := make(chan error, 1)
l := NewTestLoggerFatalAtError(t, fatalChan)

dqName := "test_disk_queue" + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
defer os.RemoveAll(tmpDir)
dq := New(dqName, tmpDir, 1024, 4, 1<<10, 2500, 2*time.Second, l)
defer dq.Close()
NotNil(t, dq)
Equal(t, int64(0), dq.Depth())

for i := 0; i < 205; i++ { // 204 messages fit, but message 205 will be too big
msg := []byte(fmt.Sprintf("%05d", i)) // 5 bytes
err = dq.Put(msg)

msgOut := <-dq.ReadChan()
Equal(t, msg, msgOut)
}

select {
case err = <-fatalChan:
t.Fatal(err)
default:
}

}

func BenchmarkDiskQueuePut16(b *testing.B) {
benchmarkDiskQueuePut(16, b)
}
Expand Down
Loading