raft: factor out payloadsSize helper

Signed-off-by: Pavel Kalinnikov <pavel@cockroachlabs.com>
This commit is contained in:
Pavel Kalinnikov 2022-10-27 14:52:55 +01:00 committed by Tobias Grieger
parent 7bda0d7773
commit 8c9c557d85

View File

@ -1790,11 +1790,7 @@ func (r *raft) responseToReadIndexReq(req pb.Message, readIndex uint64) pb.Messa
// Empty payloads are never refused. This is used both for appending an empty // Empty payloads are never refused. This is used both for appending an empty
// entry at a new leader's term, as well as leaving a joint configuration. // entry at a new leader's term, as well as leaving a joint configuration.
func (r *raft) increaseUncommittedSize(ents []pb.Entry) bool { func (r *raft) increaseUncommittedSize(ents []pb.Entry) bool {
var s uint64 s := payloadsSize(ents)
for _, e := range ents {
s += uint64(PayloadSize(e))
}
if r.uncommittedSize > 0 && s > 0 && r.uncommittedSize+s > r.maxUncommittedSize { if r.uncommittedSize > 0 && s > 0 && r.uncommittedSize+s > r.maxUncommittedSize {
// If the uncommitted tail of the Raft log is empty, allow any size // If the uncommitted tail of the Raft log is empty, allow any size
// proposal. Otherwise, limit the size of the uncommitted tail of the // proposal. Otherwise, limit the size of the uncommitted tail of the
@ -1816,12 +1812,7 @@ func (r *raft) reduceUncommittedSize(ents []pb.Entry) {
// Fast-path for followers, who do not track or enforce the limit. // Fast-path for followers, who do not track or enforce the limit.
return return
} }
if s := payloadsSize(ents); s > r.uncommittedSize {
var s uint64
for _, e := range ents {
s += uint64(PayloadSize(e))
}
if s > r.uncommittedSize {
// uncommittedSize may underestimate the size of the uncommitted Raft // uncommittedSize may underestimate the size of the uncommitted Raft
// log tail but will never overestimate it. Saturate at 0 instead of // log tail but will never overestimate it. Saturate at 0 instead of
// allowing overflow. // allowing overflow.
@ -1831,6 +1822,14 @@ func (r *raft) reduceUncommittedSize(ents []pb.Entry) {
} }
} }
func payloadsSize(ents []pb.Entry) uint64 {
var s uint64
for _, e := range ents {
s += uint64(PayloadSize(e))
}
return s
}
func numOfPendingConf(ents []pb.Entry) int { func numOfPendingConf(ents []pb.Entry) int {
n := 0 n := 0
for i := range ents { for i := range ents {