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 // restart a node from previous wal
// TODO(xiangli): check snapshot; not open from zero // TODO(xiangli): check snapshot; not open from one
w, err := wal.OpenAtIndex(waldir, 0) w, err := wal.OpenAtIndex(waldir, 1)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -14,9 +14,9 @@ func Exist(dirpath string) bool {
return len(names) != 0 return len(names) != 0
} }
// The input names should be sorted. // searchIndex returns the last array index of names whose raft index section is
// serachIndex returns the array index of the last name that has // equal to or smaller than the given index.
// a smaller raft index section than the given raft index. // The given names MUST be sorted.
func searchIndex(names []string, index int64) (int, bool) { func searchIndex(names []string, index int64) (int, bool) {
for i := len(names) - 1; i >= 0; i-- { for i := len(names) - 1; i >= 0; i-- {
name := names[i] name := names[i]

View File

@ -75,7 +75,7 @@ func Create(dirpath string) (*WAL, error) {
return nil, err 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) f, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
if err != nil { if err != nil {
return nil, err return nil, err
@ -153,8 +153,8 @@ func (w *WAL) ReadAll() (id int64, state raftpb.State, ents []raftpb.Entry, err
switch rec.Type { switch rec.Type {
case entryType: case entryType:
e := mustUnmarshalEntry(rec.Data) e := mustUnmarshalEntry(rec.Data)
if e.Index > w.ri { if e.Index >= w.ri {
ents = append(ents[:e.Index-w.ri-1], e) ents = append(ents[:e.Index-w.ri], e)
} }
case stateType: case stateType:
state = mustUnmarshalState(rec.Data) state = mustUnmarshalState(rec.Data)
@ -200,7 +200,7 @@ func (w *WAL) Cut(index int64) error {
log.Printf("wal.cut index=%d", index) log.Printf("wal.cut index=%d", index)
// create a new wal file with name sequence + 1 // 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) f, err := os.OpenFile(fpath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
if err != nil { if err != nil {
return err return err

View File

@ -32,7 +32,7 @@ var (
infoData = []byte("\b\xef\xfd\x02") 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...) 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) { func TestNew(t *testing.T) {
@ -78,7 +78,7 @@ func TestOpenAtIndex(t *testing.T) {
} }
f.Close() f.Close()
w, err := OpenAtIndex(dir, 0) w, err := OpenAtIndex(dir, 1)
if err != nil { if err != nil {
t.Fatalf("err = %v, want nil", err) t.Fatalf("err = %v, want nil", err)
} }
@ -129,7 +129,7 @@ func TestCut(t *testing.T) {
if err := w.Cut(0); err != nil { if err := w.Cut(0); err != nil {
t.Fatal(err) 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 { if g := path.Base(w.f.Name()); g != wname {
t.Errorf("name = %s, want %s", 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 { if err := w.Cut(1); err != nil {
t.Fatal(err) 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 { if g := path.Base(w.f.Name()); g != wname {
t.Errorf("name = %s, want %s", g, wname) t.Errorf("name = %s, want %s", g, wname)
} }
@ -176,7 +176,7 @@ func TestRecover(t *testing.T) {
} }
w.Close() w.Close()
if w, err = OpenAtIndex(p, 0); err != nil { if w, err = OpenAtIndex(p, 1); err != nil {
t.Fatal(err) t.Fatal(err)
} }
id, state, entries, err := w.ReadAll() id, state, entries, err := w.ReadAll()
@ -296,16 +296,20 @@ func TestRecoverAfterCut(t *testing.T) {
} }
w.Close() 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) t.Fatal(err)
} }
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
w, err := OpenAtIndex(p, int64(i)) w, err := OpenAtIndex(p, int64(i))
if i <= 3 { if err != nil {
if i <= 4 {
if err != ErrNotFound { if err != ErrNotFound {
t.Errorf("#%d: err = %v, want %v", i, err, ErrNotFound) t.Errorf("#%d: err = %v, want %v", i, err, ErrNotFound)
} }
} else {
t.Errorf("#%d: err = %v, want nil", i, err)
}
continue continue
} }
id, _, entries, err := w.ReadAll() id, _, entries, err := w.ReadAll()
@ -317,7 +321,7 @@ func TestRecoverAfterCut(t *testing.T) {
t.Errorf("#%d: id = %d, want %d", i, id, info.Id) t.Errorf("#%d: id = %d, want %d", i, id, info.Id)
} }
for j, e := range entries { 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) t.Errorf("#%d: ents[%d].Index = %+v, want %+v", i, j, e.Index, j+i+1)
} }
} }