From 9ddd8ee539081d1d11493b04f828401944664891 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Fri, 21 Nov 2014 17:22:20 -0500 Subject: [PATCH] Rename Storage.HardState back to InitialState and include ConfState. This fixes integration/migration_test.go (and highlights the fact that we need some more raft-level testing of restoring from snapshots). --- raft/raft.go | 15 ++++++++++++--- raft/raft_test.go | 2 +- raft/storage.go | 10 +++++----- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/raft/raft.go b/raft/raft.go index d5bbfba7e..fe31c1cd8 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -145,10 +145,19 @@ func newRaft(id uint64, peers []uint64, election, heartbeat int, storage Storage panic("cannot use none id") } log := newLog(storage) - st, err := storage.HardState() + hs, cs, err := storage.InitialState() if err != nil { panic(err) // TODO(bdarnell) } + if len(cs.Nodes) > 0 { + if len(peers) > 0 { + // TODO(bdarnell): the peers argument is always nil except in + // tests; the argument should be removed and these tests should be + // updated to specify their nodes through a snapshot. + panic("cannot specify both newRaft(peers) and ConfState.Nodes)") + } + peers = cs.Nodes + } r := &raft{ id: id, lead: None, @@ -161,8 +170,8 @@ func newRaft(id uint64, peers []uint64, election, heartbeat int, storage Storage for _, p := range peers { r.prs[p] = &progress{next: 1} } - if !isHardStateEqual(st, emptyState) { - r.loadState(st) + if !isHardStateEqual(hs, emptyState) { + r.loadState(hs) } r.becomeFollower(0, None) return r diff --git a/raft/raft_test.go b/raft/raft_test.go index c5d07387b..1d9003148 100644 --- a/raft/raft_test.go +++ b/raft/raft_test.go @@ -974,7 +974,7 @@ func TestBcastBeat(t *testing.T) { } storage := NewMemoryStorage() storage.ApplySnapshot(s) - sm := newRaft(1, []uint64{1, 2, 3}, 10, 1, storage) + sm := newRaft(1, nil, 10, 1, storage) sm.Term = 1 sm.becomeCandidate() diff --git a/raft/storage.go b/raft/storage.go index 403fca014..94dddc498 100644 --- a/raft/storage.go +++ b/raft/storage.go @@ -35,8 +35,8 @@ var ErrCompacted = errors.New("requested index is unavailable due to compaction" // become inoperable and refuse to participate in elections; the // application is responsible for cleanup and recovery in this case. type Storage interface { - // HardState returns the saved HardState information. - HardState() (pb.HardState, error) + // InitialState returns the saved HardState and ConfState information. + InitialState() (pb.HardState, pb.ConfState, error) // Entries returns a slice of log entries in the range [lo,hi). Entries(lo, hi uint64) ([]pb.Entry, error) // Term returns the term of entry i, which must be in the range @@ -79,9 +79,9 @@ func NewMemoryStorage() *MemoryStorage { } } -// HardState implements the Storage interface. -func (ms *MemoryStorage) HardState() (pb.HardState, error) { - return ms.hardState, nil +// InitialState implements the Storage interface. +func (ms *MemoryStorage) InitialState() (pb.HardState, pb.ConfState, error) { + return ms.hardState, ms.snapshot.Metadata.ConfState, nil } // SetHardState saves the current HardState.