<commit_before>package raft

import (
	"errors"
	"fmt"
	"sort"

	pb "github.com/coreos/etcd/raft/raftpb"
)

const None int64 = 0

type messageType int64

const (
	msgHup int64 = iota
	msgBeat
	msgProp
	msgApp
	msgAppResp
	msgVote
	msgVoteResp
	msgSnap
	msgDenied
)

var mtmap = [...]string{
	msgHup:      "msgHup",
	msgBeat:     "msgBeat",
	msgProp:     "msgProp",
	msgApp:      "msgApp",
	msgAppResp:  "msgAppResp",
	msgVote:     "msgVote",
	msgVoteResp: "msgVoteResp",
	msgSnap:     "msgSnap",
	msgDenied:   "msgDenied",
}

func (mt messageType) String() string {
	return mtmap[int64(mt)]
}

var errNoLeader = errors.New("no leader")

const (
	StateFollower StateType = iota
	StateCandidate
	StateLeader
)

type StateType int64

var stmap = [...]string{
	StateFollower:  "StateFollower",
	StateCandidate: "StateCandidate",
	StateLeader:    "StateLeader",
}

func (st StateType) String() string {
	return stmap[int64(st)]
}

type progress struct {
	match, next int64
}

func (pr *progress) update(n int64) {
	pr.match = n
	pr.next = n + 1
}

func (pr *progress) maybeDecrTo(to int64) bool {
	// the rejection must be stale if the
	// progress has matched with follower
	// or "to" does not match next - 1
	if pr.match != 0 || pr.next-1 != to {
		return false
	}

	if pr.next--; pr.next < 1 {
		pr.next = 1
	}
	return true
}<commit_msg>raft: add comment to decrTo<commit_after>package raft

import (
	"errors"
	"fmt"
	"sort"

	pb "github.com/coreos/etcd/raft/raftpb"
)

const None int64 = 0

type messageType int64

const (
	msgHup int64 = iota
	msgBeat
	msgProp
	msgApp
	msgAppResp
	msgVote
	msgVoteResp
	msgSnap
	msgDenied
)

var mtmap = [...]string{
	msgHup:      "msgHup",
	msgBeat:     "msgBeat",
	msgProp:     "msgProp",
	msgApp:      "msgApp",
	msgAppResp:  "msgAppResp",
	msgVote:     "msgVote",
	msgVoteResp: "msgVoteResp",
	msgSnap:     "msgSnap",
	msgDenied:   "msgDenied",
}

func (mt messageType) String() string {
	return mtmap[int64(mt)]
}

var errNoLeader = errors.New("no leader")

const (
	StateFollower StateType = iota
	StateCandidate
	StateLeader
)

type StateType int64

var stmap = [...]string{
	StateFollower:  "StateFollower",
	StateCandidate: "StateCandidate",
	StateLeader:    "StateLeader",
}

func (st StateType) String() string {
	return stmap[int64(st)]
}

type progress struct {
	match, next int64
}

func (pr *progress) update(n int64) {
	pr.match = n
	pr.next = n + 1
}

// maybeDecrTo returns false if the given to index comes from an out of order message.
// Otherwise it decreases the progress next index and returns true.
func (pr *progress) maybeDecrTo(to int64) bool {
	// the rejection must be stale if the
	// progress has matched with follower
	// or "to" does not match next - 1
	if pr.match != 0 || pr.next-1 != to {
		return false
	}

	if pr.next--; pr.next < 1 {
		pr.next = 1
	}
	return true
}
{'fraction_non_alphanumeric': 0.12002815909890883, 'fraction_numerical': 0.017599436818021823, 'mean_word_length': 3.426791277258567, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2914876', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1096, 'n_words': 309}