raft: compact takes index and nodes parameters

Before this commit, compact always compact log at current appliedindex of raft.
This prevents us from doing non-blocking snapshot since we have to make snapshot
and compact atomically. To prepare for non-blocking snapshot, this commit make
compact supports index and nodes parameters. After completing snapshot, the applier
should call compact with the snapshot index and the nodes at snapshot index to do
a compaction at snapsohot index.
This commit is contained in:
Xiang Li
2014-10-06 10:12:31 +08:00
parent de024ec844
commit 5587e0d73f
6 changed files with 48 additions and 32 deletions

View File

@ -229,6 +229,7 @@ func (s *EtcdServer) run() {
var syncC <-chan time.Time
// snapi indicates the index of the last submitted snapshot request
var snapi, appliedi int64
var nodes []int64
for {
select {
case <-s.ticker:
@ -265,6 +266,19 @@ func (s *EtcdServer) run() {
appliedi = e.Index
}
if rd.SoftState != nil {
nodes = rd.SoftState.Nodes
if rd.RaftState == raft.StateLeader {
syncC = s.syncTicker
} else {
syncC = nil
}
if rd.SoftState.ShouldStop {
s.Stop()
return
}
}
if rd.Snapshot.Index > snapi {
snapi = rd.Snapshot.Index
}
@ -278,21 +292,9 @@ func (s *EtcdServer) run() {
}
if appliedi-snapi > s.snapCount {
s.snapshot()
s.snapshot(appliedi, nodes)
snapi = appliedi
}
if rd.SoftState != nil {
if rd.RaftState == raft.StateLeader {
syncC = s.syncTicker
} else {
syncC = nil
}
if rd.SoftState.ShouldStop {
s.Stop()
return
}
}
case <-syncC:
s.sync(defaultSyncTimeout)
case <-s.done:
@ -517,14 +519,14 @@ func (s *EtcdServer) apply(r pb.Request) Response {
}
// TODO: non-blocking snapshot
func (s *EtcdServer) snapshot() {
func (s *EtcdServer) snapshot(snapi int64, snapnodes []int64) {
d, err := s.store.Save()
// TODO: current store will never fail to do a snapshot
// what should we do if the store might fail?
if err != nil {
panic("TODO: this is bad, what do we do about it?")
}
s.node.Compact(d)
s.node.Compact(snapi, snapnodes, d)
s.storage.Cut()
}