-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove unnecessary locking and serverlist syncing in heartbeats #5654
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -315,6 +315,9 @@ func NewClient(cfg *config.Config, consulCatalog consul.CatalogAPI, consulServic | |||||
// Initialize the server manager | ||||||
c.servers = servers.New(c.logger, c.shutdownCh, c) | ||||||
|
||||||
// Start server manager rebalancing go routine | ||||||
go c.servers.Start() | ||||||
|
||||||
// Initialize the client | ||||||
if err := c.init(); err != nil { | ||||||
return nil, fmt.Errorf("failed to initialize client: %v", err) | ||||||
|
@@ -1345,7 +1348,6 @@ func (c *Client) registerAndHeartbeat() { | |||||
case <-c.shutdownCh: | ||||||
return | ||||||
} | ||||||
|
||||||
if err := c.updateNodeStatus(); err != nil { | ||||||
// The servers have changed such that this node has not been | ||||||
// registered before | ||||||
|
@@ -2342,13 +2344,6 @@ func (c *Client) consulDiscovery() { | |||||
func (c *Client) consulDiscoveryImpl() error { | ||||||
consulLogger := c.logger.Named("consul") | ||||||
|
||||||
// Acquire heartbeat lock to prevent heartbeat from running | ||||||
// concurrently with discovery. Concurrent execution is safe, however | ||||||
// discovery is usually triggered when heartbeating has failed so | ||||||
// there's no point in allowing it. | ||||||
c.heartbeatLock.Lock() | ||||||
defer c.heartbeatLock.Unlock() | ||||||
|
||||||
dcs, err := c.consulCatalog.Datacenters() | ||||||
if err != nil { | ||||||
return fmt.Errorf("client.consul: unable to query Consul datacenters: %v", err) | ||||||
|
@@ -2432,6 +2427,26 @@ DISCOLOOP: | |||||
|
||||||
consulLogger.Info("discovered following servers", "servers", nomadServers) | ||||||
|
||||||
// Check if the list of servers discovered is identical to the list we already have | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I think this code would be better placed in Not a big deal as there's no data race here, just the possibility of a comparison against stale data which shouldn't do much harm. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (It would be easily unit testable in server manager as well) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method for comparing whether the two server lists are equal in nomad/client/servers/manager.go Line 206 in 9da25ad
This approach avoids that sort step/modification. I can move over the code though and change the logic in SerServers to avoid the sort of existing servers, which modifies the observable server list. |
||||||
// If so, we don't need to reset the server list unnecessarily | ||||||
knownServers := make(map[string]struct{}) | ||||||
serverList := c.servers.GetServers() | ||||||
for _, s := range serverList { | ||||||
knownServers[s.Addr.String()] = struct{}{} | ||||||
} | ||||||
|
||||||
allFound := true | ||||||
for _, s := range nomadServers { | ||||||
_, known := knownServers[s.Addr.String()] | ||||||
if !known { | ||||||
allFound = false | ||||||
break | ||||||
} | ||||||
} | ||||||
if allFound && len(nomadServers) == len(serverList) { | ||||||
c.logger.Info("Not replacing server list, current server list is identical to servers discovered in Consul") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug log level at most. This should only matter when debugging heartbeating which is hopefully very rare!
Suggested change
|
||||||
return nil | ||||||
} | ||||||
// Fire the retry trigger if we have updated the set of servers. | ||||||
if c.servers.SetServers(nomadServers) { | ||||||
// Start rebalancing | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,5 +16,5 @@ server { | |
} | ||
|
||
# Self-elect, should be 3 or 5 for production | ||
bootstrap_expect = 3 | ||
bootstrap_expect = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This the dev cluster config, so bootstrap expect should match server2 and server3's. You can override the value on the command line to use this config for a single server cluster or we could add a new config for single server clusters. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious how this call was dropped? Was it a regression?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it was ever there so for long running clients only failed rpcs or discovering new servers would cause reshuffling.