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.
This commit is contained in:
lorneli 2017-09-01 19:13:32 +08:00
parent 7d4a8a6935
commit 7c50c06fb8
3 changed files with 18 additions and 3 deletions

View File

@ -29,6 +29,9 @@ import (
const minSectorSize = 512 const minSectorSize = 512
// frameSizeBytes is frame size in bytes, including record size and padding size.
const frameSizeBytes = 8
type decoder struct { type decoder struct {
mu sync.Mutex mu sync.Mutex
brs []*bufio.Reader 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 // record decoded as valid; point last valid offset to end of record
d.lastValidOff += recBytes + padBytes + 8 d.lastValidOff += frameSizeBytes + recBytes + padBytes
return nil return nil
} }
@ -126,7 +129,7 @@ func (d *decoder) isTornEntry(data []byte) bool {
return false return false
} }
fileOff := d.lastValidOff + 8 fileOff := d.lastValidOff + frameSizeBytes
curOff := 0 curOff := 0
chunks := [][]byte{} chunks := [][]byte{}
// split data on sector boundaries // split data on sector boundaries

View File

@ -455,6 +455,7 @@ func (w *WAL) cut() error {
return err return err
} }
// reopen newTail with its new path so calls to Name() match the wal filename format
newTail.Close() newTail.Close()
if newTail, err = fileutil.LockFile(fpath, os.O_WRONLY, fileutil.PrivateFileMode); err != nil { 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() w.mu.Lock()
defer w.mu.Unlock() defer w.mu.Unlock()
if len(w.locks) == 0 {
return nil
}
var smaller int var smaller int
found := false 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 // if no lock index is greater than the release index, we can
// release lock up to the last one(excluding). // release lock up to the last one(excluding).
if !found && len(w.locks) != 0 { if !found {
smaller = len(w.locks) - 1 smaller = len(w.locks) - 1
} }

View File

@ -554,6 +554,13 @@ func TestReleaseLockTo(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) 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 // make 10 separate files
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
es := []raftpb.Entry{{Index: uint64(i)}} es := []raftpb.Entry{{Index: uint64(i)}}