Browse Source

etcd: clean up snap

Xiang Li 11 years ago
parent
commit
5fdc124578
3 changed files with 10 additions and 12 deletions
  1. 8 10
      etcd/participant.go
  2. 1 1
      raft/node.go
  3. 1 1
      raft/node_test.go

+ 8 - 10
etcd/participant.go

@@ -152,24 +152,22 @@ func newParticipant(c *conf.Config, client *v2client, peerHub *peerHub, tickDura
 			log.Printf("id=%x participant.snapload err=%s\n", p.id, err)
 			return nil, err
 		}
-		var logIndex int64
+		var snapIndex int64
 		if s != nil {
-			logIndex = s.Index
+			if err := p.Recovery(s.Data); err != nil {
+				panic(err)
+			}
+			log.Printf("id=%x participant.store.recovered index=%d\n", p.id, s.Index)
+			snapIndex = s.Index
 		}
-		n, err := wal.Read(walDir, logIndex)
+		n, err := wal.Read(walDir, snapIndex)
 		if err != nil {
 			return nil, err
 		}
 		p.id = n.Id
-		p.node.Node = raft.Recover(s, n.Id, n.Ents, n.State, defaultHeartbeat, defaultElection)
+		p.node.Node = raft.Recover(n.Id, s, n.Ents, n.State, defaultHeartbeat, defaultElection)
 		p.apply(p.node.Next())
 		log.Printf("id=%x participant.load path=%s snap=%+v state=\"%+v\" len(ents)=%d", p.id, p.cfg.DataDir, s, n.State, len(n.Ents))
-		if s != nil {
-			if err := p.Recovery(s.Data); err != nil {
-				panic(err)
-			}
-			log.Printf("id=%x participant.store.recovered index=%d\n", p.id, s.Index)
-		}
 		if w, err = wal.Open(walDir); err != nil {
 			return nil, err
 		}

+ 1 - 1
raft/node.go

@@ -52,7 +52,7 @@ func New(id int64, heartbeat, election tick) *Node {
 	return n
 }
 
-func Recover(s *Snapshot, id int64, ents []Entry, state State, heartbeat, election tick) *Node {
+func Recover(id int64, s *Snapshot, ents []Entry, state State, heartbeat, election tick) *Node {
 	n := New(id, heartbeat, election)
 	if s != nil {
 		n.sm.restore(*s)

+ 1 - 1
raft/node_test.go

@@ -192,7 +192,7 @@ func TestRecover(t *testing.T) {
 	ents := []Entry{{Term: 1}, {Term: 2}, {Term: 3}}
 	state := State{Term: 500, Vote: 1, Commit: 3}
 
-	n := Recover(nil, 0, ents, state, defaultHeartbeat, defaultElection)
+	n := Recover(0, nil, ents, state, defaultHeartbeat, defaultElection)
 	if g := n.Next(); !reflect.DeepEqual(g, ents) {
 		t.Errorf("ents = %+v, want %+v", g, ents)
 	}