2015-09-10 19:12:00 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-11-11 19:18:06 -05:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2015-09-10 19:12:00 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"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-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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:12:00 -04:00
|
|
|
// initDiscovery initialized the nodes discovery subsystem by connecting to the specified backend
|
|
|
|
// and start a registration loop to advertise the current node under the specified address.
|
2015-09-28 19:22:57 -04:00
|
|
|
func initDiscovery(backend, address string, clusterOpts map[string]string) (discovery.Backend, error) {
|
2015-11-11 19:18:06 -05:00
|
|
|
|
|
|
|
heartbeat, ttl, err := discoveryOpts(clusterOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
discoveryBackend, err := discovery.New(backend, heartbeat, ttl, clusterOpts)
|
2015-11-09 17:03:29 -05:00
|
|
|
if err != nil {
|
2015-09-10 19:12:00 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-11-11 19:18:06 -05:00
|
|
|
go registrationLoop(discoveryBackend, address, heartbeat)
|
2015-09-10 19:12:00 -04:00
|
|
|
return discoveryBackend, nil
|
|
|
|
}
|
|
|
|
|
2015-11-09 17:03:29 -05:00
|
|
|
func registerAddr(backend discovery.Backend, addr string) {
|
|
|
|
if err := backend.Register(addr); err != nil {
|
|
|
|
log.Warnf("Registering as %q in discovery failed: %v", addr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:12:00 -04:00
|
|
|
// registrationLoop registers the current node against the discovery backend using the specified
|
|
|
|
// address. The function never returns, as registration against the backend comes with a TTL and
|
|
|
|
// requires regular heartbeats.
|
2015-11-11 19:18:06 -05:00
|
|
|
func registrationLoop(discoveryBackend discovery.Backend, address string, heartbeat time.Duration) {
|
2015-11-09 17:03:29 -05:00
|
|
|
registerAddr(discoveryBackend, address)
|
2015-11-11 19:18:06 -05:00
|
|
|
for range time.Tick(heartbeat) {
|
2015-11-09 17:03:29 -05:00
|
|
|
registerAddr(discoveryBackend, address)
|
2015-09-10 19:12:00 -04:00
|
|
|
}
|
|
|
|
}
|