From 027e944985edc8625eb8300e85e3e9da8fd6dad6 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 29 Oct 2014 18:30:59 -0700 Subject: [PATCH] discovery: fix discovery for not working on customized discovery service --- discovery/discovery.go | 18 +++++++++++++++--- discovery/discovery_test.go | 9 +++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/discovery/discovery.go b/discovery/discovery.go index 786a0da4c..91a475fd6 100644 --- a/discovery/discovery.go +++ b/discovery/discovery.go @@ -31,6 +31,7 @@ import ( "github.com/coreos/etcd/Godeps/_workspace/src/github.com/jonboulle/clockwork" "github.com/coreos/etcd/client" + "github.com/coreos/etcd/pkg/strutil" ) var ( @@ -187,7 +188,7 @@ func (d *discovery) checkCluster() (client.Nodes, int, error) { nodes := make(client.Nodes, 0) // append non-config keys to nodes for _, n := range resp.Node.Nodes { - if !strings.Contains(n.Key, configKey) { + if !(path.Base(n.Key) == path.Base(configKey)) { nodes = append(nodes, n) } } @@ -197,7 +198,7 @@ func (d *discovery) checkCluster() (client.Nodes, int, error) { // find self position for i := range nodes { - if strings.Contains(nodes[i].Key, d.selfKey()) { + if path.Base(nodes[i].Key) == path.Base(d.selfKey()) { break } if i >= size-1 { @@ -241,8 +242,17 @@ func (d *discovery) waitNodes(nodes client.Nodes, size int) (client.Nodes, error w := d.c.RecursiveWatch(d.cluster, nodes[len(nodes)-1].ModifiedIndex+1) all := make(client.Nodes, len(nodes)) copy(all, nodes) + for _, n := range all { + if path.Base(n.Key) == path.Base(d.selfKey()) { + log.Printf("discovery: found self %s in the cluster", path.Base(d.selfKey())) + } else { + log.Printf("discovery: found peer %s in the cluster", path.Base(n.Key)) + } + } + // wait for others for len(all) < size { + log.Printf("discovery: found %d peer(s), waiting for %d more", len(all), size-len(all)) resp, err := w.Next() if err != nil { if err == client.ErrTimeout { @@ -250,13 +260,15 @@ func (d *discovery) waitNodes(nodes client.Nodes, size int) (client.Nodes, error } return nil, err } + log.Printf("discovery: found peer %s in the cluster", path.Base(resp.Node.Key)) all = append(all, resp.Node) } + log.Printf("discovery: found %d needed peer(s)", len(all)) return all, nil } func (d *discovery) selfKey() string { - return path.Join("/", d.cluster, fmt.Sprintf("%d", d.id)) + return path.Join("/", d.cluster, strutil.IDAsHex(d.id)) } func nodesToCluster(ns client.Nodes) string { diff --git a/discovery/discovery_test.go b/discovery/discovery_test.go index 20be66894..86bd9569a 100644 --- a/discovery/discovery_test.go +++ b/discovery/discovery_test.go @@ -88,7 +88,7 @@ func TestProxyFuncFromEnv(t *testing.T) { } func TestCheckCluster(t *testing.T) { - cluster := "1000" + cluster := "/prefix/1000" self := "/1000/1" tests := []struct { @@ -100,6 +100,7 @@ func TestCheckCluster(t *testing.T) { // self is in the size range []*client.Node{ {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1}, + {Key: "/1000/_config/"}, {Key: self, CreatedIndex: 2}, {Key: "/1000/2", CreatedIndex: 3}, {Key: "/1000/3", CreatedIndex: 4}, @@ -112,6 +113,7 @@ func TestCheckCluster(t *testing.T) { // self is in the size range []*client.Node{ {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1}, + {Key: "/1000/_config/"}, {Key: "/1000/2", CreatedIndex: 2}, {Key: "/1000/3", CreatedIndex: 3}, {Key: self, CreatedIndex: 4}, @@ -124,6 +126,7 @@ func TestCheckCluster(t *testing.T) { // self is out of the size range []*client.Node{ {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1}, + {Key: "/1000/_config/"}, {Key: "/1000/2", CreatedIndex: 2}, {Key: "/1000/3", CreatedIndex: 3}, {Key: "/1000/4", CreatedIndex: 4}, @@ -136,6 +139,7 @@ func TestCheckCluster(t *testing.T) { // self is not in the cluster []*client.Node{ {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1}, + {Key: "/1000/_config/"}, {Key: "/1000/2", CreatedIndex: 2}, {Key: "/1000/3", CreatedIndex: 3}, }, @@ -145,6 +149,7 @@ func TestCheckCluster(t *testing.T) { { []*client.Node{ {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1}, + {Key: "/1000/_config/"}, {Key: "/1000/2", CreatedIndex: 2}, {Key: "/1000/3", CreatedIndex: 3}, {Key: "/1000/4", CreatedIndex: 4}, @@ -175,7 +180,7 @@ func TestCheckCluster(t *testing.T) { rs = append(rs, &client.Response{ Node: &client.Node{ Key: cluster, - Nodes: tt.nodes, + Nodes: tt.nodes[1:], }, }) }