Skip to content

Commit

Permalink
Merge pull request #8484 from lorneli/dev
Browse files Browse the repository at this point in the history
wal: tiny refactor
  • Loading branch information
Anthony Romano committed Sep 6, 2017
2 parents 05d7dc3 + 7c50c06 commit 3c18456
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions wal/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand All @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}}
Expand Down

0 comments on commit 3c18456

Please sign in to comment.