open-nomad/command/agent/retry_join.go

91 lines
2.4 KiB
Go
Raw Normal View History

2018-04-18 16:18:18 +00:00
package agent
import (
"log"
2018-04-18 22:09:46 +00:00
"strings"
2018-04-18 16:18:18 +00:00
"time"
)
2018-04-18 22:25:11 +00:00
// DiscoverInterface is an interface for the Discover type in the go-discover
// library. Using an interface allows for ease of testing.
2018-04-18 16:18:18 +00:00
type DiscoverInterface interface {
2018-04-18 22:25:11 +00:00
// Addrs discovers ip addresses of nodes that match the given filter
// criteria.
// The config string must have the format 'provider=xxx key=val key=val ...'
// where the keys and values are provider specific. The values are URL
// encoded.
2018-04-18 16:18:18 +00:00
Addrs(string, *log.Logger) ([]string, error)
2018-04-18 22:25:11 +00:00
// Help describes the format of the configuration string for address
// discovery and the various provider specific options.
2018-04-18 16:18:18 +00:00
Help() string
2018-04-18 22:25:11 +00:00
// Names returns the names of the configured providers.
2018-04-18 16:18:18 +00:00
Names() []string
}
2018-04-18 22:25:11 +00:00
// retryJoiner is used to handle retrying a join until it succeeds or all of
// its tries are exhausted.
2018-04-18 16:18:18 +00:00
type retryJoiner struct {
2018-04-18 22:25:11 +00:00
// join adds the specified servers to the serf cluster
2018-04-18 16:18:18 +00:00
join func([]string) (int, error)
2018-04-18 22:25:11 +00:00
// discover is of type Discover, where this is either the go-discover
// implementation or a mock used for testing
2018-04-18 16:18:18 +00:00
discover DiscoverInterface
2018-04-18 22:25:11 +00:00
// errCh is used to communicate with the agent when the max retry attempt
// limit has been reached
2018-04-18 16:18:18 +00:00
errCh chan struct{}
2018-04-18 22:25:11 +00:00
// logger is the agent logger.
2018-04-18 16:18:18 +00:00
logger *log.Logger
}
2018-04-18 22:25:11 +00:00
// retryJoin is used to handle retrying a join until it succeeds or all retries
// are exhausted.
2018-04-18 16:18:18 +00:00
func (r *retryJoiner) RetryJoin(config *Config) {
if len(config.Server.RetryJoin) == 0 || !config.Server.Enabled {
return
}
2018-04-18 22:25:11 +00:00
attempt := 0
2018-04-18 16:18:18 +00:00
r.logger.Printf("[INFO] agent: Joining cluster...")
for {
2018-04-18 22:09:46 +00:00
var addrs []string
for _, addr := range config.Server.RetryJoin {
switch {
case strings.Contains(addr, "provider"):
servers, err := r.discover.Addrs(addr, r.logger)
if err != nil {
r.logger.Printf("[ERR] agent: Join error %s", err)
} else {
addrs = append(addrs, servers...)
}
default:
addrs = append(addrs, addr)
}
}
2018-04-18 16:18:18 +00:00
n, err := r.join(addrs)
if err == nil {
r.logger.Printf("[INFO] agent: Join completed. Synced with %d initial agents", n)
return
}
2018-04-18 22:25:11 +00:00
attempt++
if config.Server.RetryMaxAttempts > 0 && attempt > config.Server.RetryMaxAttempts {
2018-04-18 16:18:18 +00:00
r.logger.Printf("[ERR] agent: max join retry exhausted, exiting")
close(r.errCh)
return
}
r.logger.Printf("[WARN] agent: Join failed: %v, retrying in %v", err,
config.Server.RetryInterval)
time.Sleep(config.Server.retryInterval)
}
}