Browse Source

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).
Ben Darnell 11 years ago
parent
commit
9ddd8ee539
3 changed files with 18 additions and 9 deletions
  1. 12 3
      raft/raft.go
  2. 1 1
      raft/raft_test.go
  3. 5 5
      raft/storage.go

+ 12 - 3
raft/raft.go

@@ -145,10 +145,19 @@ func newRaft(id uint64, peers []uint64, election, heartbeat int, storage Storage
 		panic("cannot use none id")
 		panic("cannot use none id")
 	}
 	}
 	log := newLog(storage)
 	log := newLog(storage)
-	st, err := storage.HardState()
+	hs, cs, err := storage.InitialState()
 	if err != nil {
 	if err != nil {
 		panic(err) // TODO(bdarnell)
 		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{
 	r := &raft{
 		id:               id,
 		id:               id,
 		lead:             None,
 		lead:             None,
@@ -161,8 +170,8 @@ func newRaft(id uint64, peers []uint64, election, heartbeat int, storage Storage
 	for _, p := range peers {
 	for _, p := range peers {
 		r.prs[p] = &progress{next: 1}
 		r.prs[p] = &progress{next: 1}
 	}
 	}
-	if !isHardStateEqual(st, emptyState) {
-		r.loadState(st)
+	if !isHardStateEqual(hs, emptyState) {
+		r.loadState(hs)
 	}
 	}
 	r.becomeFollower(0, None)
 	r.becomeFollower(0, None)
 	return r
 	return r

+ 1 - 1
raft/raft_test.go

@@ -974,7 +974,7 @@ func TestBcastBeat(t *testing.T) {
 	}
 	}
 	storage := NewMemoryStorage()
 	storage := NewMemoryStorage()
 	storage.ApplySnapshot(s)
 	storage.ApplySnapshot(s)
-	sm := newRaft(1, []uint64{1, 2, 3}, 10, 1, storage)
+	sm := newRaft(1, nil, 10, 1, storage)
 	sm.Term = 1
 	sm.Term = 1
 
 
 	sm.becomeCandidate()
 	sm.becomeCandidate()

+ 5 - 5
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
 // become inoperable and refuse to participate in elections; the
 // application is responsible for cleanup and recovery in this case.
 // application is responsible for cleanup and recovery in this case.
 type Storage interface {
 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 returns a slice of log entries in the range [lo,hi).
 	Entries(lo, hi uint64) ([]pb.Entry, error)
 	Entries(lo, hi uint64) ([]pb.Entry, error)
 	// Term returns the term of entry i, which must be in the range
 	// 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.
 // SetHardState saves the current HardState.