wal: change wal filename format

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.
This commit is contained in:
Yicheng Qin 2014-09-09 23:29:13 -07:00
parent 1a0ad54d3e
commit 2030ca202f
4 changed files with 23 additions and 19 deletions

View File

@ -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)
}

View File

@ -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]

View File

@ -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
@ -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)
@ -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

View File

@ -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) {
@ -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)
}
@ -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)
}
@ -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)
}
@ -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()
@ -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
}
@ -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)
}
}