Make force state less log spammy

This commit is contained in:
Ian Gulliver
2023-06-11 19:38:39 -07:00
parent cee8ad6d5f
commit 24edab41b7

View File

@@ -25,16 +25,18 @@ type Candidate struct {
resp voteResponse resp voteResponse
c chan<- CandidateState c chan<- CandidateState
votes map[string]*vote votes map[string]*vote
state CandidateState state CandidateState
firstYes time.Time forceState CandidateState
mu sync.RWMutex firstYes time.Time
mu sync.RWMutex
} }
type CandidateState int type CandidateState int
const ( const (
StateLeader CandidateState = iota StateUndefined CandidateState = iota
StateLeader
StateNotLeader StateNotLeader
maxVotePeriod = 5 * time.Second maxVotePeriod = 5 * time.Second
@@ -60,6 +62,7 @@ func NewCandidate(numVoters int, signingKey string) *Candidate {
signingKey: []byte(signingKey), signingKey: []byte(signingKey),
votes: map[string]*vote{}, votes: map[string]*vote{},
state: StateNotLeader, state: StateNotLeader,
forceState: getForceState(),
stop: make(chan bool), stop: make(chan bool),
done: make(chan bool), done: make(chan bool),
c: change, c: change,
@@ -68,6 +71,11 @@ func NewCandidate(numVoters int, signingKey string) *Candidate {
}, },
} }
if c.forceState != StateUndefined {
log.Printf("[elect] state forced to %s", StateName[c.forceState])
c.state = c.forceState
}
go c.loop() go c.loop()
return c return c
@@ -210,30 +218,9 @@ func (c *Candidate) elect(v *vote) {
c.c <- state c.c <- state
}() }()
switch *electForceState { if c.forceState != StateUndefined {
case "": c.state = c.forceState
// Not forced
case "leader":
log.Printf("[elect] state forced to leader")
state = StateLeader
return return
case "not-leader":
fallthrough
case "not_leader":
fallthrough
case "notleader":
log.Printf("[elect] state forced to not leader")
state = StateNotLeader
return
default:
panic("invalid --elect-force-state")
} }
if v != nil { if v != nil {
@@ -299,3 +286,23 @@ func (c *Candidate) loop() {
} }
} }
} }
func getForceState() CandidateState {
switch *electForceState {
case "":
return StateUndefined
case "leader":
return StateLeader
case "not-leader":
fallthrough
case "not_leader":
fallthrough
case "notleader":
return StateNotLeader
default:
panic("invalid --elect-force-state")
}
}