From 7c50c06fb897ffcb77fc084d2a98725970123ac8 Mon Sep 17 00:00:00 2001 From: lorneli Date: Fri, 1 Sep 2017 19:13:32 +0800 Subject: [PATCH] wal: tiny refactor a. add comment of reopening file in cut function. b. add const frameSizeBytes in decoder. c. return directly if locked files empty in ReleaseLockTo function. --- wal/decoder.go | 7 +++++-- wal/wal.go | 7 ++++++- wal/wal_test.go | 7 +++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/wal/decoder.go b/wal/decoder.go index 0d9b4428c..97d87d378 100644 --- a/wal/decoder.go +++ b/wal/decoder.go @@ -29,6 +29,9 @@ import ( const minSectorSize = 512 +// frameSizeBytes is frame size in bytes, including record size and padding size. +const frameSizeBytes = 8 + type decoder struct { mu sync.Mutex brs []*bufio.Reader @@ -104,7 +107,7 @@ func (d *decoder) decodeRecord(rec *walpb.Record) error { } } // record decoded as valid; point last valid offset to end of record - d.lastValidOff += recBytes + padBytes + 8 + d.lastValidOff += frameSizeBytes + recBytes + padBytes return nil } @@ -126,7 +129,7 @@ func (d *decoder) isTornEntry(data []byte) bool { return false } - fileOff := d.lastValidOff + 8 + fileOff := d.lastValidOff + frameSizeBytes curOff := 0 chunks := [][]byte{} // split data on sector boundaries diff --git a/wal/wal.go b/wal/wal.go index 8f1141166..96d01a23a 100644 --- a/wal/wal.go +++ b/wal/wal.go @@ -455,6 +455,7 @@ func (w *WAL) cut() error { return err } + // reopen newTail with its new path so calls to Name() match the wal filename format newTail.Close() if newTail, err = fileutil.LockFile(fpath, os.O_WRONLY, fileutil.PrivateFileMode); err != nil { @@ -502,6 +503,10 @@ func (w *WAL) ReleaseLockTo(index uint64) error { w.mu.Lock() defer w.mu.Unlock() + if len(w.locks) == 0 { + return nil + } + var smaller int found := false @@ -519,7 +524,7 @@ func (w *WAL) ReleaseLockTo(index uint64) error { // if no lock index is greater than the release index, we can // release lock up to the last one(excluding). - if !found && len(w.locks) != 0 { + if !found { smaller = len(w.locks) - 1 } diff --git a/wal/wal_test.go b/wal/wal_test.go index 4528e2297..71fd7c177 100644 --- a/wal/wal_test.go +++ b/wal/wal_test.go @@ -554,6 +554,13 @@ func TestReleaseLockTo(t *testing.T) { if err != nil { t.Fatal(err) } + + // release nothing if no files + err = w.ReleaseLockTo(10) + if err != nil { + t.Errorf("err = %v, want nil", err) + } + // make 10 separate files for i := 0; i < 10; i++ { es := []raftpb.Entry{{Index: uint64(i)}}