wal: add ErrFileNotFound and ErrIndexNotFound

This commit is contained in:
Yicheng Qin 2014-09-17 15:58:06 -07:00
parent 7160b5ae26
commit 29f6d8a9e6
2 changed files with 15 additions and 14 deletions

View File

@ -42,10 +42,11 @@ const (
) )
var ( var (
ErrIDMismatch = errors.New("wal: unmatch id") ErrIDMismatch = errors.New("wal: unmatch id")
ErrNotFound = errors.New("wal: file is not found") ErrFileNotFound = errors.New("wal: file not found")
ErrCRCMismatch = errors.New("wal: crc mismatch") ErrIndexNotFound = errors.New("wal: index not found in file")
crcTable = crc32.MakeTable(crc32.Castagnoli) ErrCRCMismatch = errors.New("wal: crc mismatch")
crcTable = crc32.MakeTable(crc32.Castagnoli)
) )
// WAL is a logical repersentation of the stable storage. // WAL is a logical repersentation of the stable storage.
@ -107,14 +108,14 @@ func OpenAtIndex(dirpath string, index int64) (*WAL, error) {
} }
names = checkWalNames(names) names = checkWalNames(names)
if len(names) == 0 { if len(names) == 0 {
return nil, ErrNotFound return nil, ErrFileNotFound
} }
sort.Sort(sort.StringSlice(names)) sort.Sort(sort.StringSlice(names))
nameIndex, ok := searchIndex(names, index) nameIndex, ok := searchIndex(names, index)
if !ok || !isValidSeq(names[nameIndex:]) { if !ok || !isValidSeq(names[nameIndex:]) {
return nil, ErrNotFound return nil, ErrFileNotFound
} }
// open the wal files for reading // open the wal files for reading
@ -153,7 +154,7 @@ func OpenAtIndex(dirpath string, index int64) (*WAL, error) {
} }
// ReadAll reads out all records of the current WAL. // ReadAll reads out all records of the current WAL.
// If it cannot read out the expected entry, it will return ErrNotFound. // If it cannot read out the expected entry, it will return ErrIndexNotFound.
// After ReadAll, the WAL will be ready for appending new records. // After ReadAll, the WAL will be ready for appending new records.
func (w *WAL) ReadAll() (id int64, state raftpb.HardState, ents []raftpb.Entry, err error) { func (w *WAL) ReadAll() (id int64, state raftpb.HardState, ents []raftpb.Entry, err error) {
rec := &walpb.Record{} rec := &walpb.Record{}
@ -196,7 +197,7 @@ func (w *WAL) ReadAll() (id int64, state raftpb.HardState, ents []raftpb.Entry,
} }
if w.enti < w.ri { if w.enti < w.ri {
state.Reset() state.Reset()
return 0, state, nil, ErrNotFound return 0, state, nil, ErrIndexNotFound
} }
// close decoder, disable reading // close decoder, disable reading

View File

@ -111,8 +111,8 @@ func TestOpenAtIndex(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(emptydir) defer os.RemoveAll(emptydir)
if _, err = OpenAtIndex(emptydir, 0); err != ErrNotFound { if _, err = OpenAtIndex(emptydir, 0); err != ErrFileNotFound {
t.Errorf("err = %v, want %v", err, ErrNotFound) t.Errorf("err = %v, want %v", err, ErrFileNotFound)
} }
} }
@ -315,8 +315,8 @@ func TestRecoverAfterCut(t *testing.T) {
w, err := OpenAtIndex(p, int64(i)) w, err := OpenAtIndex(p, int64(i))
if err != nil { if err != nil {
if i <= 4 { if i <= 4 {
if err != ErrNotFound { if err != ErrFileNotFound {
t.Errorf("#%d: err = %v, want %v", i, err, ErrNotFound) t.Errorf("#%d: err = %v, want %v", i, err, ErrFileNotFound)
} }
} else { } else {
t.Errorf("#%d: err = %v, want nil", i, err) t.Errorf("#%d: err = %v, want nil", i, err)
@ -360,8 +360,8 @@ func TestOpenAtUncommittedIndex(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
// commit up to index 0, try to read index 1 // commit up to index 0, try to read index 1
if _, _, _, err := w.ReadAll(); err != ErrNotFound { if _, _, _, err := w.ReadAll(); err != ErrIndexNotFound {
t.Errorf("err = %v, want %v", err, ErrNotFound) t.Errorf("err = %v, want %v", err, ErrIndexNotFound)
} }
w.Close() w.Close()
} }