Skip to content

Commit

Permalink
Use Status.Peers instead of Status.Ping
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Jun 15, 2016
1 parent be503a2 commit 73d565a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
31 changes: 21 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,18 +1261,26 @@ func (c *Client) setupConsulSyncer() error {
dcs = append([]string{nearestDC}, otherDCs...)
}

// Forward RPCs to our region
nomadRPCArgs := structs.GenericRequest{
QueryOptions: structs.QueryOptions{
Region: c.Region(),
},
}

nomadServerServiceName := c.config.ConsulConfig.ServerServiceName
var mErr multierror.Error
const defaultMaxNumNomadServers = 8
nomadServerServices := make([]string, 0, defaultMaxNumNomadServers)
c.logger.Printf("[DEBUG] client.consul: bootstrap contacting following Consul DCs: %q", dcs)
for _, dc := range dcs {
opts := &consulapi.QueryOptions{
consulOpts := &consulapi.QueryOptions{
AllowStale: true,
Datacenter: dc,
Near: "_agent",
WaitTime: consul.DefaultQueryWaitDuration,
}
consulServices, _, err := consulCatalog.Service(nomadServerServiceName, consul.ServiceTagRPC, opts)
consulServices, _, err := consulCatalog.Service(nomadServerServiceName, consul.ServiceTagRPC, consulOpts)
if err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("unable to query service %+q from Consul datacenter %+q: %v", nomadServerServiceName, dc, err))
continue
Expand All @@ -1290,13 +1298,16 @@ func (c *Client) setupConsulSyncer() error {
mErr.Errors = append(mErr.Errors, err)
continue
}
var ok bool
if ok, err = c.connPool.PingNomadServer(c.Region(), c.RPCMajorVersion(), serverEndpoint); err != nil {
var peers []string
if err := c.connPool.RPC(c.Region(), serverEndpoint.Addr, c.RPCMajorVersion(), "Status.Peers", nomadRPCArgs, &peers); err != nil {
mErr.Errors = append(mErr.Errors, err)
continue
}
if ok {
nomadServerServices = append(nomadServerServices, serverAddr)
// Successfully received the Server peers list of the correct
// region
if len(peers) != 0 {
nomadServerServices = append(nomadServerServices, peers...)
break
}
}
// Break if at least one Nomad Server was successfully pinged
Expand All @@ -1309,12 +1320,12 @@ func (c *Client) setupConsulSyncer() error {
return mErr.ErrorOrNil()
}

for i := range dcs {
dcs[i] = fmt.Sprintf("%+q", dcs[i])
}
return fmt.Errorf("no Nomad Servers advertising service %+q in Consul datacenters: %+q", nomadServerServiceName, dcs)
return fmt.Errorf("no Nomad Servers advertising service %q in Consul datacenters: %q", nomadServerServiceName, dcs)
}

// Log the servers we are adding
c.logger.Printf("[DEBUG] client.consul: bootstrap adding following Servers: %q", nomadServerServices)

c.heartbeatLock.Lock()
if atomic.LoadInt32(&c.lastHeartbeatFromQuorum) == 1 && now.Before(c.consulPullHeartbeatDeadline) {
c.heartbeatLock.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions demo/vagrant/client1.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ data_dir = "/tmp/client1"

enable_debug = true

name = "client1"

# Enable the client
client {
enabled = true
Expand Down
30 changes: 30 additions & 0 deletions demo/vagrant/client1_consul_bootstrap.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Increase log verbosity
log_level = "DEBUG"

# Setup data dir
data_dir = "/tmp/client1"

enable_debug = true

name = "client1"

# Enable the client
client {
enabled = true

# For demo assume we are talking to server1. For production,
# this should be like "nomad.service.consul:4647" and a system
# like Consul used for service discovery.
node_class = "foo"
options {
"driver.raw_exec.enable" = "1"
}
reserved {
cpu = 500
}
}

# Modify our port to avoid a collision with server1
ports {
http = 5656
}

0 comments on commit 73d565a

Please sign in to comment.