mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: cleanup maybeSendAppend method
- avoid large indented blocks, leave the main block unindented - declare pb.Message inlined in the sending call Signed-off-by: Pavel Kalinnikov <pavel@cockroachlabs.com>
This commit is contained in:
parent
5619953f33
commit
d5ac7b833f
32
raft/raft.go
32
raft/raft.go
@ -437,8 +437,6 @@ func (r *raft) maybeSendAppend(to uint64, sendIfEmpty bool) bool {
|
|||||||
if pr.IsPaused() {
|
if pr.IsPaused() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
m := pb.Message{}
|
|
||||||
m.To = to
|
|
||||||
|
|
||||||
term, errt := r.raftLog.term(pr.Next - 1)
|
term, errt := r.raftLog.term(pr.Next - 1)
|
||||||
ents, erre := r.raftLog.entries(pr.Next, r.maxMsgSize)
|
ents, erre := r.raftLog.entries(pr.Next, r.maxMsgSize)
|
||||||
@ -452,7 +450,6 @@ func (r *raft) maybeSendAppend(to uint64, sendIfEmpty bool) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Type = pb.MsgSnap
|
|
||||||
snapshot, err := r.raftLog.snapshot()
|
snapshot, err := r.raftLog.snapshot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == ErrSnapshotTemporarilyUnavailable {
|
if err == ErrSnapshotTemporarilyUnavailable {
|
||||||
@ -464,23 +461,24 @@ func (r *raft) maybeSendAppend(to uint64, sendIfEmpty bool) bool {
|
|||||||
if IsEmptySnap(snapshot) {
|
if IsEmptySnap(snapshot) {
|
||||||
panic("need non-empty snapshot")
|
panic("need non-empty snapshot")
|
||||||
}
|
}
|
||||||
m.Snapshot = snapshot
|
|
||||||
sindex, sterm := snapshot.Metadata.Index, snapshot.Metadata.Term
|
sindex, sterm := snapshot.Metadata.Index, snapshot.Metadata.Term
|
||||||
r.logger.Debugf("%x [firstindex: %d, commit: %d] sent snapshot[index: %d, term: %d] to %x [%s]",
|
r.logger.Debugf("%x [firstindex: %d, commit: %d] sent snapshot[index: %d, term: %d] to %x [%s]",
|
||||||
r.id, r.raftLog.firstIndex(), r.raftLog.committed, sindex, sterm, to, pr)
|
r.id, r.raftLog.firstIndex(), r.raftLog.committed, sindex, sterm, to, pr)
|
||||||
pr.BecomeSnapshot(sindex)
|
pr.BecomeSnapshot(sindex)
|
||||||
r.logger.Debugf("%x paused sending replication messages to %x [%s]", r.id, to, pr)
|
r.logger.Debugf("%x paused sending replication messages to %x [%s]", r.id, to, pr)
|
||||||
} else {
|
|
||||||
m.Type = pb.MsgApp
|
r.send(pb.Message{To: to, Type: pb.MsgSnap, Snapshot: snapshot})
|
||||||
m.Index = pr.Next - 1
|
return true
|
||||||
m.LogTerm = term
|
}
|
||||||
m.Entries = ents
|
|
||||||
m.Commit = r.raftLog.committed
|
// Send the actual MsgApp otherwise, and update the progress accordingly.
|
||||||
if n := len(m.Entries); n != 0 {
|
// TODO(pavelkalinnikov): factor out the Progress update to a method
|
||||||
|
next := pr.Next // save Next for later, as the progress update can change it
|
||||||
|
if n := len(ents); n != 0 {
|
||||||
switch pr.State {
|
switch pr.State {
|
||||||
// optimistically increase the next when in StateReplicate
|
// optimistically increase the next when in StateReplicate
|
||||||
case tracker.StateReplicate:
|
case tracker.StateReplicate:
|
||||||
last := m.Entries[n-1].Index
|
last := ents[n-1].Index
|
||||||
pr.OptimisticUpdate(last)
|
pr.OptimisticUpdate(last)
|
||||||
pr.Inflights.Add(last)
|
pr.Inflights.Add(last)
|
||||||
case tracker.StateProbe:
|
case tracker.StateProbe:
|
||||||
@ -489,8 +487,14 @@ func (r *raft) maybeSendAppend(to uint64, sendIfEmpty bool) bool {
|
|||||||
r.logger.Panicf("%x is sending append in unhandled state %s", r.id, pr.State)
|
r.logger.Panicf("%x is sending append in unhandled state %s", r.id, pr.State)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
r.send(pb.Message{
|
||||||
r.send(m)
|
To: to,
|
||||||
|
Type: pb.MsgApp,
|
||||||
|
Index: next - 1,
|
||||||
|
LogTerm: term,
|
||||||
|
Entries: ents,
|
||||||
|
Commit: r.raftLog.committed,
|
||||||
|
})
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user