raft: clearify that the firstIndex might not be available.

This commit is contained in:
Xiang Li 2014-12-02 14:27:52 -08:00
parent b7ca56e3c8
commit 2f5b748a90
3 changed files with 14 additions and 4 deletions

View File

@ -261,6 +261,8 @@ func (l *raftLog) slice(lo uint64, hi uint64) []pb.Entry {
// This should never fail because it has been checked before. // This should never fail because it has been checked before.
log.Panicf("entries[%d:%d) from storage is out of bound", lo, min(hi, l.unstable.offset)) log.Panicf("entries[%d:%d) from storage is out of bound", lo, min(hi, l.unstable.offset))
return nil return nil
} else if err == ErrUnavailable {
return nil
} else if err != nil { } else if err != nil {
panic(err) // TODO(bdarnell) panic(err) // TODO(bdarnell)
} }

View File

@ -34,10 +34,11 @@ type unstable struct {
offset uint64 offset uint64
} }
// maybeFirstIndex returns the first index if it has a snapshot. // maybeFirstIndex returns the index of the first possible entry in entries
// if it has a snapshot.
func (u *unstable) maybeFirstIndex() (uint64, bool) { func (u *unstable) maybeFirstIndex() (uint64, bool) {
if u.snapshot != nil { if u.snapshot != nil {
return u.snapshot.Metadata.Index, true return u.snapshot.Metadata.Index + 1, true
} }
return 0, false return 0, false
} }

View File

@ -28,6 +28,8 @@ import (
// index is unavailable because it predates the last snapshot. // index is unavailable because it predates the last snapshot.
var ErrCompacted = errors.New("requested index is unavailable due to compaction") var ErrCompacted = errors.New("requested index is unavailable due to compaction")
var ErrUnavailable = errors.New("requested entry at index is unavailable")
// Storage is an interface that may be implemented by the application // Storage is an interface that may be implemented by the application
// to retrieve log entries from storage. // to retrieve log entries from storage.
// //
@ -47,8 +49,9 @@ type Storage interface {
// LastIndex returns the index of the last entry in the log. // LastIndex returns the index of the last entry in the log.
LastIndex() (uint64, error) LastIndex() (uint64, error)
// FirstIndex returns the index of the first log entry that is // FirstIndex returns the index of the first log entry that is
// available via Entries (older entries have been incorporated // possibly available via Entries (older entries have been incorporated
// into the latest Snapshot). // into the latest Snapshot; if storage only contains the dummy entry the
// first log entry is not available).
FirstIndex() (uint64, error) FirstIndex() (uint64, error)
// Snapshot returns the most recent snapshot. // Snapshot returns the most recent snapshot.
Snapshot() (pb.Snapshot, error) Snapshot() (pb.Snapshot, error)
@ -95,6 +98,10 @@ func (ms *MemoryStorage) Entries(lo, hi uint64) ([]pb.Entry, error) {
if lo <= offset { if lo <= offset {
return nil, ErrCompacted return nil, ErrCompacted
} }
// only contains dummy entries.
if len(ms.ents) == 1 {
return nil, ErrUnavailable
}
return ms.ents[lo-offset : hi-offset], nil return ms.ents[lo-offset : hi-offset], nil
} }