Skip to content

Commit

Permalink
raft: remove use of deprecated Leader func. (#18352)
Browse files Browse the repository at this point in the history
Co-authored-by: Luiz Aoqui <luiz@hashicorp.com>
  • Loading branch information
jrasell and lgfa29 committed Sep 1, 2023
1 parent 7466496 commit 776a26b
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/18352.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
raft: remove use of deprecated Leader func
```
2 changes: 1 addition & 1 deletion helper/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func makeRaft(t *testing.T, dir string) (*raft.Raft, *MockFSM) {

timeout := time.After(10 * time.Second)
for {
if raft.Leader() != "" {
if leaderAddr, _ := raft.LeaderWithID(); leaderAddr != "" {
break
}

Expand Down
3 changes: 2 additions & 1 deletion nomad/node_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ func equalDevices(n1, n2 *structs.Node) bool {

// constructNodeServerInfoResponse assumes the n.srv.peerLock is held for reading.
func (n *Node) constructNodeServerInfoResponse(nodeID string, snap *state.StateSnapshot, reply *structs.NodeUpdateResponse) error {
reply.LeaderRPCAddr = string(n.srv.raft.Leader())
leaderAddr, _ := n.srv.raft.LeaderWithID()
reply.LeaderRPCAddr = string(leaderAddr)

// Reply with config information required for future RPC requests
reply.Servers = make([]*structs.NodeServerInfo, 0, len(n.srv.localPeers))
Expand Down
2 changes: 1 addition & 1 deletion nomad/operator_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (op *Operator) RaftGetConfiguration(args *structs.GenericRequest, reply *st
}

// Fill out the reply.
leader := op.srv.raft.Leader()
leader, _ := op.srv.raft.LeaderWithID()
reply.Index = future.Index()
for _, server := range future.Configuration().Servers {
node := "(unknown)"
Expand Down
5 changes: 3 additions & 2 deletions nomad/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ func (s *Server) getLeader() (bool, *serverParts) {
}

// Get the leader
leader := s.raft.Leader()
leader, _ := s.raft.LeaderWithID()
if leader == "" {
return false, nil
}
Expand Down Expand Up @@ -793,7 +793,8 @@ func (r *rpcHandler) setQueryMeta(m *structs.QueryMeta) {
m.KnownLeader = true
} else {
m.LastContact = time.Since(r.raft.LastContact())
m.KnownLeader = (r.raft.Leader() != "")
leaderAddr, _ := r.raft.LeaderWithID()
m.KnownLeader = (leaderAddr != "")
}
}

Expand Down
5 changes: 3 additions & 2 deletions nomad/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ func (s *Server) setupBootstrapHandler() error {
// correct number of servers required for quorum are present).
bootstrapFn := func() error {
// If there is a raft leader, do nothing
if s.raft.Leader() != "" {
if leader, _ := s.raft.LeaderWithID(); leader != "" {
peersTimeout.Reset(maxStaleLeadership)
return nil
}
Expand Down Expand Up @@ -1948,11 +1948,12 @@ func (s *Server) Stats() map[string]map[string]string {
toString := func(v uint64) string {
return strconv.FormatUint(v, 10)
}
leader, _ := s.raft.LeaderWithID()
stats := map[string]map[string]string{
"nomad": {
"server": "true",
"leader": fmt.Sprintf("%v", s.IsLeader()),
"leader_addr": string(s.raft.Leader()),
"leader_addr": string(leader),
"bootstrap": fmt.Sprintf("%v", s.isSingleServerCluster()),
"known_regions": toString(uint64(len(s.peers))),
},
Expand Down
4 changes: 2 additions & 2 deletions nomad/status_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (s *Status) Leader(args *structs.GenericRequest, reply *string) error {
return err
}

leader := string(s.srv.raft.Leader())
leader, _ := s.srv.raft.LeaderWithID()
if leader != "" {
*reply = leader
*reply = string(leader)
} else {
*reply = ""
}
Expand Down

0 comments on commit 776a26b

Please sign in to comment.