Skip to content

Commit 234aee8

Browse files
committed
backport of commit a7a6cdf
1 parent 0fb03da commit 234aee8

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

client/client.go

+39-31
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import (
5858
vaultapi "github.com/hashicorp/vault/api"
5959
"github.com/shirou/gopsutil/v3/host"
6060
"golang.org/x/exp/maps"
61-
"golang.org/x/exp/slices"
6261
)
6362

6463
const (
@@ -1944,7 +1943,7 @@ func (c *Client) retryRegisterNode() {
19441943
}
19451944

19461945
retryIntv := registerRetryIntv
1947-
if err == noServersErr {
1946+
if err == noServersErr || structs.IsErrNoRegionPath(err) {
19481947
c.logger.Debug("registration waiting on servers")
19491948
c.triggerDiscovery()
19501949
retryIntv = noServerRetryIntv
@@ -1971,6 +1970,11 @@ func (c *Client) registerNode() error {
19711970
return err
19721971
}
19731972

1973+
err := c.handleNodeUpdateResponse(resp)
1974+
if err != nil {
1975+
return err
1976+
}
1977+
19741978
// Update the node status to ready after we register.
19751979
c.UpdateConfig(func(c *config.Config) {
19761980
c.Node.Status = structs.NodeStatusReady
@@ -1985,6 +1989,7 @@ func (c *Client) registerNode() error {
19851989
defer c.heartbeatLock.Unlock()
19861990
c.heartbeatStop.setLastOk(time.Now())
19871991
c.heartbeatTTL = resp.HeartbeatTTL
1992+
19881993
return nil
19891994
}
19901995

@@ -2036,6 +2041,25 @@ func (c *Client) updateNodeStatus() error {
20362041
}
20372042
})
20382043

2044+
err := c.handleNodeUpdateResponse(resp)
2045+
if err != nil {
2046+
return fmt.Errorf("heartbeat response returned no valid servers")
2047+
}
2048+
2049+
// Begin polling Consul if there is no Nomad leader. We could be
2050+
// heartbeating to a Nomad server that is in the minority of a
2051+
// partition of the Nomad server quorum, but this Nomad Agent still
2052+
// has connectivity to the existing majority of Nomad Servers, but
2053+
// only if it queries Consul.
2054+
if resp.LeaderRPCAddr == "" {
2055+
c.triggerDiscovery()
2056+
}
2057+
2058+
c.EnterpriseClient.SetFeatures(resp.Features)
2059+
return nil
2060+
}
2061+
2062+
func (c *Client) handleNodeUpdateResponse(resp structs.NodeUpdateResponse) error {
20392063
// Update the number of nodes in the cluster so we can adjust our server
20402064
// rebalance rate.
20412065
c.servers.SetNumNodes(resp.NumNodes)
@@ -2052,20 +2076,9 @@ func (c *Client) updateNodeStatus() error {
20522076
nomadServers = append(nomadServers, e)
20532077
}
20542078
if len(nomadServers) == 0 {
2055-
return fmt.Errorf("heartbeat response returned no valid servers")
2079+
return noServersErr
20562080
}
20572081
c.servers.SetServers(nomadServers)
2058-
2059-
// Begin polling Consul if there is no Nomad leader. We could be
2060-
// heartbeating to a Nomad server that is in the minority of a
2061-
// partition of the Nomad server quorum, but this Nomad Agent still
2062-
// has connectivity to the existing majority of Nomad Servers, but
2063-
// only if it queries Consul.
2064-
if resp.LeaderRPCAddr == "" {
2065-
c.triggerDiscovery()
2066-
}
2067-
2068-
c.EnterpriseClient.SetFeatures(resp.Features)
20692082
return nil
20702083
}
20712084

@@ -2907,9 +2920,6 @@ func (c *Client) consulDiscoveryImpl() error {
29072920
dcs = dcs[0:helper.Min(len(dcs), datacenterQueryLimit)]
29082921
}
29092922

2910-
// Query for servers in this client's region only
2911-
region := c.Region()
2912-
29132923
serviceName := c.GetConfig().ConsulConfig.ServerServiceName
29142924
var mErr multierror.Error
29152925
var nomadServers servers.Servers
@@ -2929,21 +2939,19 @@ DISCOLOOP:
29292939
}
29302940

29312941
for _, s := range consulServices {
2932-
if slices.Contains(s.ServiceTags, region) {
2933-
port := strconv.Itoa(s.ServicePort)
2934-
addrstr := s.ServiceAddress
2935-
if addrstr == "" {
2936-
addrstr = s.Address
2937-
}
2938-
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(addrstr, port))
2939-
if err != nil {
2940-
mErr.Errors = append(mErr.Errors, err)
2941-
continue
2942-
}
2943-
2944-
srv := &servers.Server{Addr: addr}
2945-
nomadServers = append(nomadServers, srv)
2942+
port := strconv.Itoa(s.ServicePort)
2943+
addrstr := s.ServiceAddress
2944+
if addrstr == "" {
2945+
addrstr = s.Address
29462946
}
2947+
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(addrstr, port))
2948+
if err != nil {
2949+
mErr.Errors = append(mErr.Errors, err)
2950+
continue
2951+
}
2952+
2953+
srv := &servers.Server{Addr: addr}
2954+
nomadServers = append(nomadServers, srv)
29472955
}
29482956

29492957
if len(nomadServers) > 0 {

command/agent/agent.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,7 @@ func (a *Agent) setupServer() error {
891891
rpcServ := &structs.Service{
892892
Name: a.config.Consul.ServerServiceName,
893893
PortLabel: a.config.AdvertiseAddrs.RPC,
894-
Tags: append([]string{consul.ServiceTagRPC, a.config.Region},
895-
a.config.Consul.Tags...),
894+
Tags: append([]string{consul.ServiceTagRPC}, a.config.Consul.Tags...),
896895
Checks: []*structs.ServiceCheck{
897896
{
898897
Name: a.config.Consul.ServerRPCCheckName,

0 commit comments

Comments
 (0)