Browse Source

Merge pull request #8484 from lorneli/dev

wal: tiny refactor
Anthony Romano 8 năm trước cách đây
mục cha
commit
3c1845604b
3 tập tin đã thay đổi với 18 bổ sung3 xóa
  1. 5 2
      wal/decoder.go
  2. 6 1
      wal/wal.go
  3. 7 0
      wal/wal_test.go

+ 5 - 2
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

+ 6 - 1
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
 	}
 

+ 7 - 0
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)}}