Merge pull request #1048 from aluzzardi/leader-prefix

Leader Election: Use same path prefix as discovery.
This commit is contained in:
Alexandre Beslic 2015-07-15 21:14:00 -07:00
commit 1ab4129df6
2 changed files with 14 additions and 8 deletions

View File

@ -120,9 +120,10 @@ func setupReplication(c *cli.Context, cluster cluster.Cluster, server *api.Serve
log.Fatal("Leader election is only supported with consul, etcd and zookeeper discovery.") log.Fatal("Leader election is only supported with consul, etcd and zookeeper discovery.")
} }
client := kvDiscovery.Store() client := kvDiscovery.Store()
p := path.Join(kvDiscovery.Prefix(), leaderElectionPath)
candidate := leadership.NewCandidate(client, leaderElectionPath, addr) candidate := leadership.NewCandidate(client, p, addr)
follower := leadership.NewFollower(client, leaderElectionPath) follower := leadership.NewFollower(client, p)
primary := api.NewPrimary(cluster, tlsConfig, &statusHandler{cluster, candidate, follower}, c.Bool("cors")) primary := api.NewPrimary(cluster, tlsConfig, &statusHandler{cluster, candidate, follower}, c.Bool("cors"))
replica := api.NewReplica(primary, tlsConfig) replica := api.NewReplica(primary, tlsConfig)

View File

@ -22,6 +22,7 @@ type Discovery struct {
store store.Store store store.Store
heartbeat time.Duration heartbeat time.Duration
ttl time.Duration ttl time.Duration
prefix string
path string path string
} }
@ -41,18 +42,17 @@ func (s *Discovery) Initialize(uris string, heartbeat time.Duration, ttl time.Du
var ( var (
parts = strings.SplitN(uris, "/", 2) parts = strings.SplitN(uris, "/", 2)
addrs = strings.Split(parts[0], ",") addrs = strings.Split(parts[0], ",")
prefix = ""
err error err error
) )
// A custom prefix to the path can be optionally used. // A custom prefix to the path can be optionally used.
if len(parts) == 2 { if len(parts) == 2 {
prefix = parts[1] s.prefix = parts[1]
} }
s.heartbeat = heartbeat s.heartbeat = heartbeat
s.ttl = ttl s.ttl = ttl
s.path = path.Join(prefix, discoveryPath) s.path = path.Join(s.prefix, discoveryPath)
// Creates a new store, will ignore options given // Creates a new store, will ignore options given
// if not supported by the chosen store // if not supported by the chosen store
@ -139,3 +139,8 @@ func (s *Discovery) Register(addr string) error {
func (s *Discovery) Store() store.Store { func (s *Discovery) Store() store.Store {
return s.store return s.store
} }
// Prefix returns the store prefix
func (s *Discovery) Prefix() string {
return s.prefix
}