Skip to content

Commit

Permalink
wal: change wal filename format
Browse files Browse the repository at this point in the history
Make raftIndex section to be expected raftIndex of next entry.

It makes filename more intuitive and straight-forward.

The commit also adds comments for filename format.
  • Loading branch information
yichengq committed Sep 12, 2014
1 parent 1a0ad54 commit 2030ca2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func startRaft(id int64, peerIDs []int64, waldir string) (raft.Node, *wal.WAL) {
}

// restart a node from previous wal
// TODO(xiangli): check snapshot; not open from zero
w, err := wal.OpenAtIndex(waldir, 0)
// TODO(xiangli): check snapshot; not open from one
w, err := wal.OpenAtIndex(waldir, 1)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions wal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func Exist(dirpath string) bool {
return len(names) != 0
}

// The input names should be sorted.
// serachIndex returns the array index of the last name that has
// a smaller raft index section than the given raft index.
// searchIndex returns the last array index of names whose raft index section is
// equal to or smaller than the given index.
// The given names MUST be sorted.
func searchIndex(names []string, index int64) (int, bool) {
for i := len(names) - 1; i >= 0; i-- {
name := names[i]
Expand Down
8 changes: 4 additions & 4 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Create(dirpath string) (*WAL, error) {
return nil, err
}

p := path.Join(dirpath, fmt.Sprintf("%016x-%016x.wal", 0, 0))
p := path.Join(dirpath, fmt.Sprintf("%016x-%016x.wal", 0, 1))
f, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return nil, err
Expand Down Expand Up @@ -153,8 +153,8 @@ func (w *WAL) ReadAll() (id int64, state raftpb.State, ents []raftpb.Entry, err
switch rec.Type {
case entryType:
e := mustUnmarshalEntry(rec.Data)
if e.Index > w.ri {
ents = append(ents[:e.Index-w.ri-1], e)
if e.Index >= w.ri {
ents = append(ents[:e.Index-w.ri], e)
}
case stateType:
state = mustUnmarshalState(rec.Data)
Expand Down Expand Up @@ -200,7 +200,7 @@ func (w *WAL) Cut(index int64) error {
log.Printf("wal.cut index=%d", index)

// create a new wal file with name sequence + 1
fpath := path.Join(w.dir, fmt.Sprintf("%016x-%016x.wal", w.seq+1, index))
fpath := path.Join(w.dir, fmt.Sprintf("%016x-%016x.wal", w.seq+1, index+1))
f, err := os.OpenFile(fpath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return err
Expand Down
24 changes: 14 additions & 10 deletions wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
infoData = []byte("\b\xef\xfd\x02")
infoRecord = append([]byte("\x0e\x00\x00\x00\x00\x00\x00\x00\b\x01\x10\x99\xb5\xe4\xd0\x03\x1a\x04"), infoData...)

firstWalName = "0000000000000000-0000000000000000.wal"
firstWalName = "0000000000000000-0000000000000001.wal"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestOpenAtIndex(t *testing.T) {
}
f.Close()

w, err := OpenAtIndex(dir, 0)
w, err := OpenAtIndex(dir, 1)
if err != nil {
t.Fatalf("err = %v, want nil", err)
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestCut(t *testing.T) {
if err := w.Cut(0); err != nil {
t.Fatal(err)
}
wname := fmt.Sprintf("%016x-%016x.wal", 1, 0)
wname := fmt.Sprintf("%016x-%016x.wal", 1, 1)
if g := path.Base(w.f.Name()); g != wname {
t.Errorf("name = %s, want %s", g, wname)
}
Expand All @@ -141,7 +141,7 @@ func TestCut(t *testing.T) {
if err := w.Cut(1); err != nil {
t.Fatal(err)
}
wname = fmt.Sprintf("%016x-%016x.wal", 2, 1)
wname = fmt.Sprintf("%016x-%016x.wal", 2, 2)
if g := path.Base(w.f.Name()); g != wname {
t.Errorf("name = %s, want %s", g, wname)
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestRecover(t *testing.T) {
}
w.Close()

if w, err = OpenAtIndex(p, 0); err != nil {
if w, err = OpenAtIndex(p, 1); err != nil {
t.Fatal(err)
}
id, state, entries, err := w.ReadAll()
Expand Down Expand Up @@ -296,15 +296,19 @@ func TestRecoverAfterCut(t *testing.T) {
}
w.Close()

if err := os.Remove(path.Join(p, "0000000000000004-0000000000000003.wal")); err != nil {
if err := os.Remove(path.Join(p, "0000000000000004-0000000000000004.wal")); err != nil {
t.Fatal(err)
}

for i := 0; i < 10; i++ {
w, err := OpenAtIndex(p, int64(i))
if i <= 3 {
if err != ErrNotFound {
t.Errorf("#%d: err = %v, want %v", i, err, ErrNotFound)
if err != nil {
if i <= 4 {
if err != ErrNotFound {
t.Errorf("#%d: err = %v, want %v", i, err, ErrNotFound)
}
} else {
t.Errorf("#%d: err = %v, want nil", i, err)
}
continue
}
Expand All @@ -317,7 +321,7 @@ func TestRecoverAfterCut(t *testing.T) {
t.Errorf("#%d: id = %d, want %d", i, id, info.Id)
}
for j, e := range entries {
if e.Index != int64(j+i+1) {
if e.Index != int64(j+i) {
t.Errorf("#%d: ents[%d].Index = %+v, want %+v", i, j, e.Index, j+i+1)
}
}
Expand Down

0 comments on commit 2030ca2

Please sign in to comment.