mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 10:33:35 +00:00
Update to kube v1.17
Signed-off-by: Humble Chirammal <hchiramm@redhat.com>
This commit is contained in:
committed by
mergify[bot]
parent
327fcd1b1b
commit
3af1e26d7c
132
vendor/go.etcd.io/etcd/raft/tracker/inflights.go
generated
vendored
Normal file
132
vendor/go.etcd.io/etcd/raft/tracker/inflights.go
generated
vendored
Normal file
@ -0,0 +1,132 @@
|
||||
// Copyright 2019 The etcd Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package tracker
|
||||
|
||||
// Inflights limits the number of MsgApp (represented by the largest index
|
||||
// contained within) sent to followers but not yet acknowledged by them. Callers
|
||||
// use Full() to check whether more messages can be sent, call Add() whenever
|
||||
// they are sending a new append, and release "quota" via FreeLE() whenever an
|
||||
// ack is received.
|
||||
type Inflights struct {
|
||||
// the starting index in the buffer
|
||||
start int
|
||||
// number of inflights in the buffer
|
||||
count int
|
||||
|
||||
// the size of the buffer
|
||||
size int
|
||||
|
||||
// buffer contains the index of the last entry
|
||||
// inside one message.
|
||||
buffer []uint64
|
||||
}
|
||||
|
||||
// NewInflights sets up an Inflights that allows up to 'size' inflight messages.
|
||||
func NewInflights(size int) *Inflights {
|
||||
return &Inflights{
|
||||
size: size,
|
||||
}
|
||||
}
|
||||
|
||||
// Clone returns an *Inflights that is identical to but shares no memory with
|
||||
// the receiver.
|
||||
func (in *Inflights) Clone() *Inflights {
|
||||
ins := *in
|
||||
ins.buffer = append([]uint64(nil), in.buffer...)
|
||||
return &ins
|
||||
}
|
||||
|
||||
// Add notifies the Inflights that a new message with the given index is being
|
||||
// dispatched. Full() must be called prior to Add() to verify that there is room
|
||||
// for one more message, and consecutive calls to add Add() must provide a
|
||||
// monotonic sequence of indexes.
|
||||
func (in *Inflights) Add(inflight uint64) {
|
||||
if in.Full() {
|
||||
panic("cannot add into a Full inflights")
|
||||
}
|
||||
next := in.start + in.count
|
||||
size := in.size
|
||||
if next >= size {
|
||||
next -= size
|
||||
}
|
||||
if next >= len(in.buffer) {
|
||||
in.grow()
|
||||
}
|
||||
in.buffer[next] = inflight
|
||||
in.count++
|
||||
}
|
||||
|
||||
// grow the inflight buffer by doubling up to inflights.size. We grow on demand
|
||||
// instead of preallocating to inflights.size to handle systems which have
|
||||
// thousands of Raft groups per process.
|
||||
func (in *Inflights) grow() {
|
||||
newSize := len(in.buffer) * 2
|
||||
if newSize == 0 {
|
||||
newSize = 1
|
||||
} else if newSize > in.size {
|
||||
newSize = in.size
|
||||
}
|
||||
newBuffer := make([]uint64, newSize)
|
||||
copy(newBuffer, in.buffer)
|
||||
in.buffer = newBuffer
|
||||
}
|
||||
|
||||
// FreeLE frees the inflights smaller or equal to the given `to` flight.
|
||||
func (in *Inflights) FreeLE(to uint64) {
|
||||
if in.count == 0 || to < in.buffer[in.start] {
|
||||
// out of the left side of the window
|
||||
return
|
||||
}
|
||||
|
||||
idx := in.start
|
||||
var i int
|
||||
for i = 0; i < in.count; i++ {
|
||||
if to < in.buffer[idx] { // found the first large inflight
|
||||
break
|
||||
}
|
||||
|
||||
// increase index and maybe rotate
|
||||
size := in.size
|
||||
if idx++; idx >= size {
|
||||
idx -= size
|
||||
}
|
||||
}
|
||||
// free i inflights and set new start index
|
||||
in.count -= i
|
||||
in.start = idx
|
||||
if in.count == 0 {
|
||||
// inflights is empty, reset the start index so that we don't grow the
|
||||
// buffer unnecessarily.
|
||||
in.start = 0
|
||||
}
|
||||
}
|
||||
|
||||
// FreeFirstOne releases the first inflight. This is a no-op if nothing is
|
||||
// inflight.
|
||||
func (in *Inflights) FreeFirstOne() { in.FreeLE(in.buffer[in.start]) }
|
||||
|
||||
// Full returns true if no more messages can be sent at the moment.
|
||||
func (in *Inflights) Full() bool {
|
||||
return in.count == in.size
|
||||
}
|
||||
|
||||
// Count returns the number of inflight messages.
|
||||
func (in *Inflights) Count() int { return in.count }
|
||||
|
||||
// reset frees all inflights.
|
||||
func (in *Inflights) reset() {
|
||||
in.count = 0
|
||||
in.start = 0
|
||||
}
|
259
vendor/go.etcd.io/etcd/raft/tracker/progress.go
generated
vendored
Normal file
259
vendor/go.etcd.io/etcd/raft/tracker/progress.go
generated
vendored
Normal file
@ -0,0 +1,259 @@
|
||||
// Copyright 2019 The etcd Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package tracker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Progress represents a follower’s progress in the view of the leader. Leader
|
||||
// maintains progresses of all followers, and sends entries to the follower
|
||||
// based on its progress.
|
||||
//
|
||||
// NB(tbg): Progress is basically a state machine whose transitions are mostly
|
||||
// strewn around `*raft.raft`. Additionally, some fields are only used when in a
|
||||
// certain State. All of this isn't ideal.
|
||||
type Progress struct {
|
||||
Match, Next uint64
|
||||
// State defines how the leader should interact with the follower.
|
||||
//
|
||||
// When in StateProbe, leader sends at most one replication message
|
||||
// per heartbeat interval. It also probes actual progress of the follower.
|
||||
//
|
||||
// When in StateReplicate, leader optimistically increases next
|
||||
// to the latest entry sent after sending replication message. This is
|
||||
// an optimized state for fast replicating log entries to the follower.
|
||||
//
|
||||
// When in StateSnapshot, leader should have sent out snapshot
|
||||
// before and stops sending any replication message.
|
||||
State StateType
|
||||
|
||||
// PendingSnapshot is used in StateSnapshot.
|
||||
// If there is a pending snapshot, the pendingSnapshot will be set to the
|
||||
// index of the snapshot. If pendingSnapshot is set, the replication process of
|
||||
// this Progress will be paused. raft will not resend snapshot until the pending one
|
||||
// is reported to be failed.
|
||||
PendingSnapshot uint64
|
||||
|
||||
// RecentActive is true if the progress is recently active. Receiving any messages
|
||||
// from the corresponding follower indicates the progress is active.
|
||||
// RecentActive can be reset to false after an election timeout.
|
||||
//
|
||||
// TODO(tbg): the leader should always have this set to true.
|
||||
RecentActive bool
|
||||
|
||||
// ProbeSent is used while this follower is in StateProbe. When ProbeSent is
|
||||
// true, raft should pause sending replication message to this peer until
|
||||
// ProbeSent is reset. See ProbeAcked() and IsPaused().
|
||||
ProbeSent bool
|
||||
|
||||
// Inflights is a sliding window for the inflight messages.
|
||||
// Each inflight message contains one or more log entries.
|
||||
// The max number of entries per message is defined in raft config as MaxSizePerMsg.
|
||||
// Thus inflight effectively limits both the number of inflight messages
|
||||
// and the bandwidth each Progress can use.
|
||||
// When inflights is Full, no more message should be sent.
|
||||
// When a leader sends out a message, the index of the last
|
||||
// entry should be added to inflights. The index MUST be added
|
||||
// into inflights in order.
|
||||
// When a leader receives a reply, the previous inflights should
|
||||
// be freed by calling inflights.FreeLE with the index of the last
|
||||
// received entry.
|
||||
Inflights *Inflights
|
||||
|
||||
// IsLearner is true if this progress is tracked for a learner.
|
||||
IsLearner bool
|
||||
}
|
||||
|
||||
// ResetState moves the Progress into the specified State, resetting ProbeSent,
|
||||
// PendingSnapshot, and Inflights.
|
||||
func (pr *Progress) ResetState(state StateType) {
|
||||
pr.ProbeSent = false
|
||||
pr.PendingSnapshot = 0
|
||||
pr.State = state
|
||||
pr.Inflights.reset()
|
||||
}
|
||||
|
||||
func max(a, b uint64) uint64 {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func min(a, b uint64) uint64 {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// ProbeAcked is called when this peer has accepted an append. It resets
|
||||
// ProbeSent to signal that additional append messages should be sent without
|
||||
// further delay.
|
||||
func (pr *Progress) ProbeAcked() {
|
||||
pr.ProbeSent = false
|
||||
}
|
||||
|
||||
// BecomeProbe transitions into StateProbe. Next is reset to Match+1 or,
|
||||
// optionally and if larger, the index of the pending snapshot.
|
||||
func (pr *Progress) BecomeProbe() {
|
||||
// If the original state is StateSnapshot, progress knows that
|
||||
// the pending snapshot has been sent to this peer successfully, then
|
||||
// probes from pendingSnapshot + 1.
|
||||
if pr.State == StateSnapshot {
|
||||
pendingSnapshot := pr.PendingSnapshot
|
||||
pr.ResetState(StateProbe)
|
||||
pr.Next = max(pr.Match+1, pendingSnapshot+1)
|
||||
} else {
|
||||
pr.ResetState(StateProbe)
|
||||
pr.Next = pr.Match + 1
|
||||
}
|
||||
}
|
||||
|
||||
// BecomeReplicate transitions into StateReplicate, resetting Next to Match+1.
|
||||
func (pr *Progress) BecomeReplicate() {
|
||||
pr.ResetState(StateReplicate)
|
||||
pr.Next = pr.Match + 1
|
||||
}
|
||||
|
||||
// BecomeSnapshot moves the Progress to StateSnapshot with the specified pending
|
||||
// snapshot index.
|
||||
func (pr *Progress) BecomeSnapshot(snapshoti uint64) {
|
||||
pr.ResetState(StateSnapshot)
|
||||
pr.PendingSnapshot = snapshoti
|
||||
}
|
||||
|
||||
// MaybeUpdate is called when an MsgAppResp arrives from the follower, with the
|
||||
// index acked by it. The method returns false if the given n index comes from
|
||||
// an outdated message. Otherwise it updates the progress and returns true.
|
||||
func (pr *Progress) MaybeUpdate(n uint64) bool {
|
||||
var updated bool
|
||||
if pr.Match < n {
|
||||
pr.Match = n
|
||||
updated = true
|
||||
pr.ProbeAcked()
|
||||
}
|
||||
if pr.Next < n+1 {
|
||||
pr.Next = n + 1
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
// OptimisticUpdate signals that appends all the way up to and including index n
|
||||
// are in-flight. As a result, Next is increased to n+1.
|
||||
func (pr *Progress) OptimisticUpdate(n uint64) { pr.Next = n + 1 }
|
||||
|
||||
// MaybeDecrTo adjusts the Progress to the receipt of a MsgApp rejection. The
|
||||
// arguments are the index the follower rejected to append to its log, and its
|
||||
// last index.
|
||||
//
|
||||
// Rejections can happen spuriously as messages are sent out of order or
|
||||
// duplicated. In such cases, the rejection pertains to an index that the
|
||||
// Progress already knows were previously acknowledged, and false is returned
|
||||
// without changing the Progress.
|
||||
//
|
||||
// If the rejection is genuine, Next is lowered sensibly, and the Progress is
|
||||
// cleared for sending log entries.
|
||||
func (pr *Progress) MaybeDecrTo(rejected, last uint64) bool {
|
||||
if pr.State == StateReplicate {
|
||||
// The rejection must be stale if the progress has matched and "rejected"
|
||||
// is smaller than "match".
|
||||
if rejected <= pr.Match {
|
||||
return false
|
||||
}
|
||||
// Directly decrease next to match + 1.
|
||||
//
|
||||
// TODO(tbg): why not use last if it's larger?
|
||||
pr.Next = pr.Match + 1
|
||||
return true
|
||||
}
|
||||
|
||||
// The rejection must be stale if "rejected" does not match next - 1. This
|
||||
// is because non-replicating followers are probed one entry at a time.
|
||||
if pr.Next-1 != rejected {
|
||||
return false
|
||||
}
|
||||
|
||||
if pr.Next = min(rejected, last+1); pr.Next < 1 {
|
||||
pr.Next = 1
|
||||
}
|
||||
pr.ProbeSent = false
|
||||
return true
|
||||
}
|
||||
|
||||
// IsPaused returns whether sending log entries to this node has been throttled.
|
||||
// This is done when a node has rejected recent MsgApps, is currently waiting
|
||||
// for a snapshot, or has reached the MaxInflightMsgs limit. In normal
|
||||
// operation, this is false. A throttled node will be contacted less frequently
|
||||
// until it has reached a state in which it's able to accept a steady stream of
|
||||
// log entries again.
|
||||
func (pr *Progress) IsPaused() bool {
|
||||
switch pr.State {
|
||||
case StateProbe:
|
||||
return pr.ProbeSent
|
||||
case StateReplicate:
|
||||
return pr.Inflights.Full()
|
||||
case StateSnapshot:
|
||||
return true
|
||||
default:
|
||||
panic("unexpected state")
|
||||
}
|
||||
}
|
||||
|
||||
func (pr *Progress) String() string {
|
||||
var buf strings.Builder
|
||||
fmt.Fprintf(&buf, "%s match=%d next=%d", pr.State, pr.Match, pr.Next)
|
||||
if pr.IsLearner {
|
||||
fmt.Fprint(&buf, " learner")
|
||||
}
|
||||
if pr.IsPaused() {
|
||||
fmt.Fprint(&buf, " paused")
|
||||
}
|
||||
if pr.PendingSnapshot > 0 {
|
||||
fmt.Fprintf(&buf, " pendingSnap=%d", pr.PendingSnapshot)
|
||||
}
|
||||
if !pr.RecentActive {
|
||||
fmt.Fprintf(&buf, " inactive")
|
||||
}
|
||||
if n := pr.Inflights.Count(); n > 0 {
|
||||
fmt.Fprintf(&buf, " inflight=%d", n)
|
||||
if pr.Inflights.Full() {
|
||||
fmt.Fprint(&buf, "[full]")
|
||||
}
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// ProgressMap is a map of *Progress.
|
||||
type ProgressMap map[uint64]*Progress
|
||||
|
||||
// String prints the ProgressMap in sorted key order, one Progress per line.
|
||||
func (m ProgressMap) String() string {
|
||||
ids := make([]uint64, 0, len(m))
|
||||
for k := range m {
|
||||
ids = append(ids, k)
|
||||
}
|
||||
sort.Slice(ids, func(i, j int) bool {
|
||||
return ids[i] < ids[j]
|
||||
})
|
||||
var buf strings.Builder
|
||||
for _, id := range ids {
|
||||
fmt.Fprintf(&buf, "%d: %s\n", id, m[id])
|
||||
}
|
||||
return buf.String()
|
||||
}
|
42
vendor/go.etcd.io/etcd/raft/tracker/state.go
generated
vendored
Normal file
42
vendor/go.etcd.io/etcd/raft/tracker/state.go
generated
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright 2019 The etcd Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package tracker
|
||||
|
||||
// StateType is the state of a tracked follower.
|
||||
type StateType uint64
|
||||
|
||||
const (
|
||||
// StateProbe indicates a follower whose last index isn't known. Such a
|
||||
// follower is "probed" (i.e. an append sent periodically) to narrow down
|
||||
// its last index. In the ideal (and common) case, only one round of probing
|
||||
// is necessary as the follower will react with a hint. Followers that are
|
||||
// probed over extended periods of time are often offline.
|
||||
StateProbe StateType = iota
|
||||
// StateReplicate is the state steady in which a follower eagerly receives
|
||||
// log entries to append to its log.
|
||||
StateReplicate
|
||||
// StateSnapshot indicates a follower that needs log entries not available
|
||||
// from the leader's Raft log. Such a follower needs a full snapshot to
|
||||
// return to StateReplicate.
|
||||
StateSnapshot
|
||||
)
|
||||
|
||||
var prstmap = [...]string{
|
||||
"StateProbe",
|
||||
"StateReplicate",
|
||||
"StateSnapshot",
|
||||
}
|
||||
|
||||
func (st StateType) String() string { return prstmap[uint64(st)] }
|
288
vendor/go.etcd.io/etcd/raft/tracker/tracker.go
generated
vendored
Normal file
288
vendor/go.etcd.io/etcd/raft/tracker/tracker.go
generated
vendored
Normal file
@ -0,0 +1,288 @@
|
||||
// Copyright 2019 The etcd Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package tracker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"go.etcd.io/etcd/raft/quorum"
|
||||
pb "go.etcd.io/etcd/raft/raftpb"
|
||||
)
|
||||
|
||||
// Config reflects the configuration tracked in a ProgressTracker.
|
||||
type Config struct {
|
||||
Voters quorum.JointConfig
|
||||
// AutoLeave is true if the configuration is joint and a transition to the
|
||||
// incoming configuration should be carried out automatically by Raft when
|
||||
// this is possible. If false, the configuration will be joint until the
|
||||
// application initiates the transition manually.
|
||||
AutoLeave bool
|
||||
// Learners is a set of IDs corresponding to the learners active in the
|
||||
// current configuration.
|
||||
//
|
||||
// Invariant: Learners and Voters does not intersect, i.e. if a peer is in
|
||||
// either half of the joint config, it can't be a learner; if it is a
|
||||
// learner it can't be in either half of the joint config. This invariant
|
||||
// simplifies the implementation since it allows peers to have clarity about
|
||||
// its current role without taking into account joint consensus.
|
||||
Learners map[uint64]struct{}
|
||||
// When we turn a voter into a learner during a joint consensus transition,
|
||||
// we cannot add the learner directly when entering the joint state. This is
|
||||
// because this would violate the invariant that the intersection of
|
||||
// voters and learners is empty. For example, assume a Voter is removed and
|
||||
// immediately re-added as a learner (or in other words, it is demoted):
|
||||
//
|
||||
// Initially, the configuration will be
|
||||
//
|
||||
// voters: {1 2 3}
|
||||
// learners: {}
|
||||
//
|
||||
// and we want to demote 3. Entering the joint configuration, we naively get
|
||||
//
|
||||
// voters: {1 2} & {1 2 3}
|
||||
// learners: {3}
|
||||
//
|
||||
// but this violates the invariant (3 is both voter and learner). Instead,
|
||||
// we get
|
||||
//
|
||||
// voters: {1 2} & {1 2 3}
|
||||
// learners: {}
|
||||
// next_learners: {3}
|
||||
//
|
||||
// Where 3 is now still purely a voter, but we are remembering the intention
|
||||
// to make it a learner upon transitioning into the final configuration:
|
||||
//
|
||||
// voters: {1 2}
|
||||
// learners: {3}
|
||||
// next_learners: {}
|
||||
//
|
||||
// Note that next_learners is not used while adding a learner that is not
|
||||
// also a voter in the joint config. In this case, the learner is added
|
||||
// right away when entering the joint configuration, so that it is caught up
|
||||
// as soon as possible.
|
||||
LearnersNext map[uint64]struct{}
|
||||
}
|
||||
|
||||
func (c Config) String() string {
|
||||
var buf strings.Builder
|
||||
fmt.Fprintf(&buf, "voters=%s", c.Voters)
|
||||
if c.Learners != nil {
|
||||
fmt.Fprintf(&buf, " learners=%s", quorum.MajorityConfig(c.Learners).String())
|
||||
}
|
||||
if c.LearnersNext != nil {
|
||||
fmt.Fprintf(&buf, " learners_next=%s", quorum.MajorityConfig(c.LearnersNext).String())
|
||||
}
|
||||
if c.AutoLeave {
|
||||
fmt.Fprintf(&buf, " autoleave")
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// Clone returns a copy of the Config that shares no memory with the original.
|
||||
func (c *Config) Clone() Config {
|
||||
clone := func(m map[uint64]struct{}) map[uint64]struct{} {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
mm := make(map[uint64]struct{}, len(m))
|
||||
for k := range m {
|
||||
mm[k] = struct{}{}
|
||||
}
|
||||
return mm
|
||||
}
|
||||
return Config{
|
||||
Voters: quorum.JointConfig{clone(c.Voters[0]), clone(c.Voters[1])},
|
||||
Learners: clone(c.Learners),
|
||||
LearnersNext: clone(c.LearnersNext),
|
||||
}
|
||||
}
|
||||
|
||||
// ProgressTracker tracks the currently active configuration and the information
|
||||
// known about the nodes and learners in it. In particular, it tracks the match
|
||||
// index for each peer which in turn allows reasoning about the committed index.
|
||||
type ProgressTracker struct {
|
||||
Config
|
||||
|
||||
Progress ProgressMap
|
||||
|
||||
Votes map[uint64]bool
|
||||
|
||||
MaxInflight int
|
||||
}
|
||||
|
||||
// MakeProgressTracker initializes a ProgressTracker.
|
||||
func MakeProgressTracker(maxInflight int) ProgressTracker {
|
||||
p := ProgressTracker{
|
||||
MaxInflight: maxInflight,
|
||||
Config: Config{
|
||||
Voters: quorum.JointConfig{
|
||||
quorum.MajorityConfig{},
|
||||
nil, // only populated when used
|
||||
},
|
||||
Learners: nil, // only populated when used
|
||||
LearnersNext: nil, // only populated when used
|
||||
},
|
||||
Votes: map[uint64]bool{},
|
||||
Progress: map[uint64]*Progress{},
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// ConfState returns a ConfState representing the active configuration.
|
||||
func (p *ProgressTracker) ConfState() pb.ConfState {
|
||||
return pb.ConfState{
|
||||
Voters: p.Voters[0].Slice(),
|
||||
VotersOutgoing: p.Voters[1].Slice(),
|
||||
Learners: quorum.MajorityConfig(p.Learners).Slice(),
|
||||
LearnersNext: quorum.MajorityConfig(p.LearnersNext).Slice(),
|
||||
AutoLeave: p.AutoLeave,
|
||||
}
|
||||
}
|
||||
|
||||
// IsSingleton returns true if (and only if) there is only one voting member
|
||||
// (i.e. the leader) in the current configuration.
|
||||
func (p *ProgressTracker) IsSingleton() bool {
|
||||
return len(p.Voters[0]) == 1 && len(p.Voters[1]) == 0
|
||||
}
|
||||
|
||||
type matchAckIndexer map[uint64]*Progress
|
||||
|
||||
var _ quorum.AckedIndexer = matchAckIndexer(nil)
|
||||
|
||||
// AckedIndex implements IndexLookuper.
|
||||
func (l matchAckIndexer) AckedIndex(id uint64) (quorum.Index, bool) {
|
||||
pr, ok := l[id]
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
return quorum.Index(pr.Match), true
|
||||
}
|
||||
|
||||
// Committed returns the largest log index known to be committed based on what
|
||||
// the voting members of the group have acknowledged.
|
||||
func (p *ProgressTracker) Committed() uint64 {
|
||||
return uint64(p.Voters.CommittedIndex(matchAckIndexer(p.Progress)))
|
||||
}
|
||||
|
||||
func insertionSort(sl []uint64) {
|
||||
a, b := 0, len(sl)
|
||||
for i := a + 1; i < b; i++ {
|
||||
for j := i; j > a && sl[j] < sl[j-1]; j-- {
|
||||
sl[j], sl[j-1] = sl[j-1], sl[j]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Visit invokes the supplied closure for all tracked progresses in stable order.
|
||||
func (p *ProgressTracker) Visit(f func(id uint64, pr *Progress)) {
|
||||
n := len(p.Progress)
|
||||
// We need to sort the IDs and don't want to allocate since this is hot code.
|
||||
// The optimization here mirrors that in `(MajorityConfig).CommittedIndex`,
|
||||
// see there for details.
|
||||
var sl [7]uint64
|
||||
ids := sl[:]
|
||||
if len(sl) >= n {
|
||||
ids = sl[:n]
|
||||
} else {
|
||||
ids = make([]uint64, n)
|
||||
}
|
||||
for id := range p.Progress {
|
||||
n--
|
||||
ids[n] = id
|
||||
}
|
||||
insertionSort(ids)
|
||||
for _, id := range ids {
|
||||
f(id, p.Progress[id])
|
||||
}
|
||||
}
|
||||
|
||||
// QuorumActive returns true if the quorum is active from the view of the local
|
||||
// raft state machine. Otherwise, it returns false.
|
||||
func (p *ProgressTracker) QuorumActive() bool {
|
||||
votes := map[uint64]bool{}
|
||||
p.Visit(func(id uint64, pr *Progress) {
|
||||
if pr.IsLearner {
|
||||
return
|
||||
}
|
||||
votes[id] = pr.RecentActive
|
||||
})
|
||||
|
||||
return p.Voters.VoteResult(votes) == quorum.VoteWon
|
||||
}
|
||||
|
||||
// VoterNodes returns a sorted slice of voters.
|
||||
func (p *ProgressTracker) VoterNodes() []uint64 {
|
||||
m := p.Voters.IDs()
|
||||
nodes := make([]uint64, 0, len(m))
|
||||
for id := range m {
|
||||
nodes = append(nodes, id)
|
||||
}
|
||||
sort.Slice(nodes, func(i, j int) bool { return nodes[i] < nodes[j] })
|
||||
return nodes
|
||||
}
|
||||
|
||||
// LearnerNodes returns a sorted slice of learners.
|
||||
func (p *ProgressTracker) LearnerNodes() []uint64 {
|
||||
if len(p.Learners) == 0 {
|
||||
return nil
|
||||
}
|
||||
nodes := make([]uint64, 0, len(p.Learners))
|
||||
for id := range p.Learners {
|
||||
nodes = append(nodes, id)
|
||||
}
|
||||
sort.Slice(nodes, func(i, j int) bool { return nodes[i] < nodes[j] })
|
||||
return nodes
|
||||
}
|
||||
|
||||
// ResetVotes prepares for a new round of vote counting via recordVote.
|
||||
func (p *ProgressTracker) ResetVotes() {
|
||||
p.Votes = map[uint64]bool{}
|
||||
}
|
||||
|
||||
// RecordVote records that the node with the given id voted for this Raft
|
||||
// instance if v == true (and declined it otherwise).
|
||||
func (p *ProgressTracker) RecordVote(id uint64, v bool) {
|
||||
_, ok := p.Votes[id]
|
||||
if !ok {
|
||||
p.Votes[id] = v
|
||||
}
|
||||
}
|
||||
|
||||
// TallyVotes returns the number of granted and rejected Votes, and whether the
|
||||
// election outcome is known.
|
||||
func (p *ProgressTracker) TallyVotes() (granted int, rejected int, _ quorum.VoteResult) {
|
||||
// Make sure to populate granted/rejected correctly even if the Votes slice
|
||||
// contains members no longer part of the configuration. This doesn't really
|
||||
// matter in the way the numbers are used (they're informational), but might
|
||||
// as well get it right.
|
||||
for id, pr := range p.Progress {
|
||||
if pr.IsLearner {
|
||||
continue
|
||||
}
|
||||
v, voted := p.Votes[id]
|
||||
if !voted {
|
||||
continue
|
||||
}
|
||||
if v {
|
||||
granted++
|
||||
} else {
|
||||
rejected++
|
||||
}
|
||||
}
|
||||
result := p.Voters.VoteResult(p.Votes)
|
||||
return granted, rejected, result
|
||||
}
|
Reference in New Issue
Block a user