backport of commit 776a26bce7cf3a320fc7e7f4a6bf9da2b30f3da7 (#18375)

Co-authored-by: James Rasell <jrasell@users.noreply.github.com>
This commit is contained in:
hc-github-team-nomad-core 2023-09-01 04:25:08 -05:00 committed by GitHub
parent ef780825d4
commit 1b2237d6a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 15 additions and 9 deletions

3
.changelog/18352.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
raft: remove use of deprecated Leader func
```

View File

@ -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
}

View File

@ -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))

View File

@ -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)"

View File

@ -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
}
@ -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 != "")
}
}

View File

@ -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
}
@ -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))),
},

View File

@ -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 = ""
}