2015-09-10 19:12:00 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-12-10 18:35:10 -05:00
|
|
|
"errors"
|
2015-11-11 19:18:06 -05:00
|
|
|
"fmt"
|
2015-12-10 18:35:10 -05:00
|
|
|
"reflect"
|
2015-11-11 19:18:06 -05:00
|
|
|
"strconv"
|
2015-09-10 19:12:00 -04:00
|
|
|
"time"
|
|
|
|
|
2016-07-18 03:13:54 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-09-10 19:12:00 -04:00
|
|
|
"github.com/docker/docker/pkg/discovery"
|
|
|
|
|
|
|
|
// Register the libkv backends for discovery.
|
|
|
|
_ "github.com/docker/docker/pkg/discovery/kv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// defaultDiscoveryHeartbeat is the default value for discovery heartbeat interval.
|
|
|
|
defaultDiscoveryHeartbeat = 20 * time.Second
|
2015-11-11 19:18:06 -05:00
|
|
|
// defaultDiscoveryTTLFactor is the default TTL factor for discovery
|
|
|
|
defaultDiscoveryTTLFactor = 3
|
2015-09-10 19:12:00 -04:00
|
|
|
)
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
var errDiscoveryDisabled = errors.New("discovery is disabled")
|
|
|
|
|
|
|
|
type discoveryReloader interface {
|
|
|
|
discovery.Watcher
|
|
|
|
Stop()
|
|
|
|
Reload(backend, address string, clusterOpts map[string]string) error
|
2016-05-04 10:13:23 -04:00
|
|
|
ReadyCh() <-chan struct{}
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type daemonDiscoveryReloader struct {
|
|
|
|
backend discovery.Backend
|
|
|
|
ticker *time.Ticker
|
|
|
|
term chan bool
|
2016-05-04 10:13:23 -04:00
|
|
|
readyCh chan struct{}
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *daemonDiscoveryReloader) Watch(stopCh <-chan struct{}) (<-chan discovery.Entries, <-chan error) {
|
|
|
|
return d.backend.Watch(stopCh)
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:13:23 -04:00
|
|
|
func (d *daemonDiscoveryReloader) ReadyCh() <-chan struct{} {
|
|
|
|
return d.readyCh
|
|
|
|
}
|
|
|
|
|
2015-11-11 19:18:06 -05:00
|
|
|
func discoveryOpts(clusterOpts map[string]string) (time.Duration, time.Duration, error) {
|
|
|
|
var (
|
|
|
|
heartbeat = defaultDiscoveryHeartbeat
|
|
|
|
ttl = defaultDiscoveryTTLFactor * defaultDiscoveryHeartbeat
|
|
|
|
)
|
|
|
|
|
|
|
|
if hb, ok := clusterOpts["discovery.heartbeat"]; ok {
|
|
|
|
h, err := strconv.Atoi(hb)
|
|
|
|
if err != nil {
|
|
|
|
return time.Duration(0), time.Duration(0), err
|
|
|
|
}
|
2016-08-31 13:07:05 -04:00
|
|
|
|
|
|
|
if h <= 0 {
|
|
|
|
return time.Duration(0), time.Duration(0),
|
|
|
|
fmt.Errorf("discovery.heartbeat must be positive")
|
|
|
|
}
|
|
|
|
|
2015-11-11 19:18:06 -05:00
|
|
|
heartbeat = time.Duration(h) * time.Second
|
|
|
|
ttl = defaultDiscoveryTTLFactor * heartbeat
|
|
|
|
}
|
|
|
|
|
|
|
|
if tstr, ok := clusterOpts["discovery.ttl"]; ok {
|
|
|
|
t, err := strconv.Atoi(tstr)
|
|
|
|
if err != nil {
|
|
|
|
return time.Duration(0), time.Duration(0), err
|
|
|
|
}
|
2016-08-31 13:07:05 -04:00
|
|
|
|
|
|
|
if t <= 0 {
|
|
|
|
return time.Duration(0), time.Duration(0),
|
|
|
|
fmt.Errorf("discovery.ttl must be positive")
|
|
|
|
}
|
|
|
|
|
2015-11-11 19:18:06 -05:00
|
|
|
ttl = time.Duration(t) * time.Second
|
|
|
|
|
|
|
|
if _, ok := clusterOpts["discovery.heartbeat"]; !ok {
|
|
|
|
h := int(t / defaultDiscoveryTTLFactor)
|
|
|
|
heartbeat = time.Duration(h) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
if ttl <= heartbeat {
|
|
|
|
return time.Duration(0), time.Duration(0),
|
|
|
|
fmt.Errorf("discovery.ttl timer must be greater than discovery.heartbeat")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return heartbeat, ttl, nil
|
|
|
|
}
|
|
|
|
|
2016-03-11 10:02:17 -05:00
|
|
|
// initDiscovery initializes the nodes discovery subsystem by connecting to the specified backend
|
|
|
|
// and starts a registration loop to advertise the current node under the specified address.
|
2015-12-10 18:35:10 -05:00
|
|
|
func initDiscovery(backendAddress, advertiseAddress string, clusterOpts map[string]string) (discoveryReloader, error) {
|
|
|
|
heartbeat, backend, err := parseDiscoveryOptions(backendAddress, clusterOpts)
|
2015-11-11 19:18:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
reloader := &daemonDiscoveryReloader{
|
|
|
|
backend: backend,
|
|
|
|
ticker: time.NewTicker(heartbeat),
|
|
|
|
term: make(chan bool),
|
2016-05-04 10:13:23 -04:00
|
|
|
readyCh: make(chan struct{}),
|
2015-09-10 19:12:00 -04:00
|
|
|
}
|
|
|
|
// We call Register() on the discovery backend in a loop for the whole lifetime of the daemon,
|
|
|
|
// but we never actually Watch() for nodes appearing and disappearing for the moment.
|
2016-05-04 10:13:23 -04:00
|
|
|
go reloader.advertiseHeartbeat(advertiseAddress)
|
2015-12-10 18:35:10 -05:00
|
|
|
return reloader, nil
|
2015-09-10 19:12:00 -04:00
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
// advertiseHeartbeat registers the current node against the discovery backend using the specified
|
2015-09-10 19:12:00 -04:00
|
|
|
// address. The function never returns, as registration against the backend comes with a TTL and
|
|
|
|
// requires regular heartbeats.
|
2015-12-10 18:35:10 -05:00
|
|
|
func (d *daemonDiscoveryReloader) advertiseHeartbeat(address string) {
|
2016-05-04 10:13:23 -04:00
|
|
|
var ready bool
|
|
|
|
if err := d.initHeartbeat(address); err == nil {
|
|
|
|
ready = true
|
|
|
|
close(d.readyCh)
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-d.ticker.C:
|
2016-05-04 10:13:23 -04:00
|
|
|
if err := d.backend.Register(address); err != nil {
|
2016-07-18 03:13:54 -04:00
|
|
|
logrus.Warnf("Registering as %q in discovery failed: %v", address, err)
|
2016-05-04 10:13:23 -04:00
|
|
|
} else {
|
|
|
|
if !ready {
|
|
|
|
close(d.readyCh)
|
|
|
|
ready = true
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
case <-d.term:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:13:23 -04:00
|
|
|
// initHeartbeat is used to do the first heartbeat. It uses a tight loop until
|
|
|
|
// either the timeout period is reached or the heartbeat is successful and returns.
|
|
|
|
func (d *daemonDiscoveryReloader) initHeartbeat(address string) error {
|
|
|
|
// Setup a short ticker until the first heartbeat has succeeded
|
|
|
|
t := time.NewTicker(500 * time.Millisecond)
|
|
|
|
defer t.Stop()
|
|
|
|
// timeout makes sure that after a period of time we stop being so aggressive trying to reach the discovery service
|
|
|
|
timeout := time.After(60 * time.Second)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return errors.New("timeout waiting for initial discovery")
|
|
|
|
case <-d.term:
|
|
|
|
return errors.New("terminated")
|
|
|
|
case <-t.C:
|
|
|
|
if err := d.backend.Register(address); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
// Reload makes the watcher to stop advertising and reconfigures it to advertise in a new address.
|
|
|
|
func (d *daemonDiscoveryReloader) Reload(backendAddress, advertiseAddress string, clusterOpts map[string]string) error {
|
|
|
|
d.Stop()
|
|
|
|
|
|
|
|
heartbeat, backend, err := parseDiscoveryOptions(backendAddress, clusterOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.backend = backend
|
|
|
|
d.ticker = time.NewTicker(heartbeat)
|
2016-05-04 10:13:23 -04:00
|
|
|
d.readyCh = make(chan struct{})
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-05-04 10:13:23 -04:00
|
|
|
go d.advertiseHeartbeat(advertiseAddress)
|
2015-12-10 18:35:10 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop terminates the discovery advertising.
|
|
|
|
func (d *daemonDiscoveryReloader) Stop() {
|
|
|
|
d.ticker.Stop()
|
|
|
|
d.term <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDiscoveryOptions(backendAddress string, clusterOpts map[string]string) (time.Duration, discovery.Backend, error) {
|
|
|
|
heartbeat, ttl, err := discoveryOpts(clusterOpts)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
2015-09-10 19:12:00 -04:00
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
|
|
|
backend, err := discovery.New(backendAddress, heartbeat, ttl, clusterOpts)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
return heartbeat, backend, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// modifiedDiscoverySettings returns whether the discovery configuration has been modified or not.
|
|
|
|
func modifiedDiscoverySettings(config *Config, backendType, advertise string, clusterOpts map[string]string) bool {
|
|
|
|
if config.ClusterStore != backendType || config.ClusterAdvertise != advertise {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.ClusterOpts == nil && clusterOpts == nil) ||
|
|
|
|
(config.ClusterOpts == nil && len(clusterOpts) == 0) ||
|
|
|
|
(len(config.ClusterOpts) == 0 && clusterOpts == nil) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return !reflect.DeepEqual(config.ClusterOpts, clusterOpts)
|
2015-09-10 19:12:00 -04:00
|
|
|
}
|