Change heartbeat type to unsigned int

Check if heartbeat value is greater than zero

Signed-off-by: Sriram Natarajan <natarajan.sriram@gmail.com>
This commit is contained in:
Sriram Natarajan 2015-04-07 12:17:14 -07:00
parent caa5fd0351
commit b174aa26de
10 changed files with 28 additions and 17 deletions

View File

@ -7,5 +7,5 @@ type Options struct {
TLSConfig *tls.Config TLSConfig *tls.Config
OvercommitRatio float64 OvercommitRatio float64
Discovery string Discovery string
Heartbeat int Heartbeat uint64
} }

View File

@ -24,7 +24,7 @@ func init() {
} }
// Initialize is exported // Initialize is exported
func (s *ConsulDiscoveryService) Initialize(uris string, heartbeat int) error { func (s *ConsulDiscoveryService) Initialize(uris string, heartbeat uint64) error {
parts := strings.SplitN(uris, "/", 2) parts := strings.SplitN(uris, "/", 2)
if len(parts) < 2 { if len(parts) < 2 {
return fmt.Errorf("invalid format %q, missing <path>", uris) return fmt.Errorf("invalid format %q, missing <path>", uris)

View File

@ -33,7 +33,7 @@ type WatchCallback func(entries []*Entry)
// DiscoveryService is exported // DiscoveryService is exported
type DiscoveryService interface { type DiscoveryService interface {
Initialize(string, int) error Initialize(string, uint64) error
Fetch() ([]*Entry, error) Fetch() ([]*Entry, error)
Watch(WatchCallback) Watch(WatchCallback)
Register(string) error Register(string) error
@ -73,7 +73,7 @@ func parse(rawurl string) (string, string) {
} }
// New is exported // New is exported
func New(rawurl string, heartbeat int) (DiscoveryService, error) { func New(rawurl string, heartbeat uint64) (DiscoveryService, error) {
scheme, uri := parse(rawurl) scheme, uri := parse(rawurl)
if discovery, exists := discoveries[scheme]; exists { if discovery, exists := discoveries[scheme]; exists {

View File

@ -22,7 +22,7 @@ func init() {
} }
// Initialize is exported // Initialize is exported
func (s *EtcdDiscoveryService) Initialize(uris string, heartbeat int) error { func (s *EtcdDiscoveryService) Initialize(uris string, heartbeat uint64) error {
var ( var (
// split here because uris can contain multiples ips // split here because uris can contain multiples ips
// like `etcd://192.168.0.1,192.168.0.2,192.168.0.3/path` // like `etcd://192.168.0.1,192.168.0.2,192.168.0.3/path`

View File

@ -10,7 +10,7 @@ import (
// FileDiscoveryService is exported // FileDiscoveryService is exported
type FileDiscoveryService struct { type FileDiscoveryService struct {
heartbeat int heartbeat uint64
path string path string
} }
@ -19,7 +19,7 @@ func init() {
} }
// Initialize is exported // Initialize is exported
func (s *FileDiscoveryService) Initialize(path string, heartbeat int) error { func (s *FileDiscoveryService) Initialize(path string, heartbeat uint64) error {
s.path = path s.path = path
s.heartbeat = heartbeat s.heartbeat = heartbeat
return nil return nil

View File

@ -16,7 +16,7 @@ func init() {
} }
// Initialize is exported // Initialize is exported
func (s *NodesDiscoveryService) Initialize(uris string, _ int) error { func (s *NodesDiscoveryService) Initialize(uris string, _ uint64) error {
for _, input := range strings.Split(uris, ",") { for _, input := range strings.Split(uris, ",") {
for _, ip := range discovery.Generate(input) { for _, ip := range discovery.Generate(input) {
entry, err := discovery.NewEntry(ip) entry, err := discovery.NewEntry(ip)

View File

@ -17,7 +17,7 @@ const DiscoveryURL = "https://discovery-stage.hub.docker.com/v1"
// TokenDiscoveryService is exported // TokenDiscoveryService is exported
type TokenDiscoveryService struct { type TokenDiscoveryService struct {
heartbeat int heartbeat uint64
url string url string
token string token string
} }
@ -27,7 +27,7 @@ func init() {
} }
// Initialize is exported // Initialize is exported
func (s *TokenDiscoveryService) Initialize(urltoken string, heartbeat int) error { func (s *TokenDiscoveryService) Initialize(urltoken string, heartbeat uint64) error {
if i := strings.LastIndex(urltoken, "/"); i != -1 { if i := strings.LastIndex(urltoken, "/"); i != -1 {
s.url = "https://" + urltoken[:i] s.url = "https://" + urltoken[:i]
s.token = urltoken[i+1:] s.token = urltoken[i+1:]

View File

@ -15,7 +15,7 @@ import (
type ZkDiscoveryService struct { type ZkDiscoveryService struct {
conn *zk.Conn conn *zk.Conn
path []string path []string
heartbeat int heartbeat uint64
} }
func init() { func init() {
@ -41,7 +41,7 @@ func (s *ZkDiscoveryService) createFullpath() error {
} }
// Initialize is exported // Initialize is exported
func (s *ZkDiscoveryService) Initialize(uris string, heartbeat int) error { func (s *ZkDiscoveryService) Initialize(uris string, heartbeat uint64) error {
var ( var (
// split here because uris can contain multiples ips // split here because uris can contain multiples ips
// like `zk://192.168.0.1,192.168.0.2,192.168.0.3/path` // like `zk://192.168.0.1,192.168.0.2,192.168.0.3/path`

14
join.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"regexp" "regexp"
"strconv"
"time" "time"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
@ -20,7 +21,12 @@ func join(c *cli.Context) {
log.Fatalf("discovery required to join a cluster. See '%s join --help'.", c.App.Name) log.Fatalf("discovery required to join a cluster. See '%s join --help'.", c.App.Name)
} }
d, err := discovery.New(dflag, c.Int("heartbeat")) hb, err := strconv.ParseUint(c.String("heartbeat"), 0, 32)
if hb < 1 || err != nil {
log.Fatal("--heartbeat should be an unsigned integer and greater than 0")
}
d, err := discovery.New(dflag, hb)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -35,10 +41,10 @@ func join(c *cli.Context) {
log.Fatal(err) log.Fatal(err)
} }
hb := time.Duration(c.Int("heartbeat")) hbval := time.Duration(hb)
for { for {
log.WithFields(log.Fields{"addr": addr, "discovery": dflag}).Infof("Registering on the discovery service every %d seconds...", hb) log.WithFields(log.Fields{"addr": addr, "discovery": dflag}).Infof("Registering on the discovery service every %d seconds...", hbval)
time.Sleep(hb * time.Second) time.Sleep(hbval * time.Second)
if err := d.Register(addr); err != nil { if err := d.Register(addr); err != nil {
log.Error(err) log.Error(err)
} }

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path" "path"
"strconv"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
@ -120,11 +121,15 @@ func manage(c *cli.Context) {
sched := scheduler.New(s, fs) sched := scheduler.New(s, fs)
eventsHandler := api.NewEventsHandler() eventsHandler := api.NewEventsHandler()
hb, err := strconv.ParseUint(c.String("heartbeat"), 0, 32)
if hb < 1 || err != nil {
log.Fatal("--heartbeat should be an unsigned integer and greater than 0")
}
options := &cluster.Options{ options := &cluster.Options{
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
OvercommitRatio: c.Float64("overcommit"), OvercommitRatio: c.Float64("overcommit"),
Discovery: dflag, Discovery: dflag,
Heartbeat: c.Int("heartbeat"), Heartbeat: hb,
} }
cluster := swarm.NewCluster(sched, store, eventsHandler, options) cluster := swarm.NewCluster(sched, store, eventsHandler, options)