etcd: clean up snap

This commit is contained in:
Xiang Li 2014-08-22 14:33:29 -07:00 committed by Yicheng Qin
parent a28dc4559b
commit 5fdc124578
3 changed files with 12 additions and 14 deletions

View File

@ -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
if s != nil {
logIndex = s.Index
}
n, err := wal.Read(walDir, logIndex)
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.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))
var snapIndex int64
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)
snapIndex = s.Index
}
n, err := wal.Read(walDir, snapIndex)
if err != nil {
return nil, err
}
p.id = n.Id
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 w, err = wal.Open(walDir); err != nil {
return nil, err
}

View File

@ -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)

View File

@ -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)
}