forked from armon/go-chord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker_client.go
81 lines (58 loc) · 1.66 KB
/
tracker_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package buddystore
import (
"fmt"
"github.com/golang/glog"
)
type TrackerClient interface {
JoinRing(string, bool) (*Ring, error)
LeaveRing(string) error
}
type TrackerClientImpl struct {
ring RingIntf
}
func NewTrackerClient(ring RingIntf) TrackerClient {
return &TrackerClientImpl{ring: ring}
}
const NUM_TRACKER_REPLICAS = 2
func (tr *TrackerClientImpl) JoinRing(ringId string, localOnly bool) (*Ring, error) {
trackerNodes, err := tr.ring.Lookup(NUM_TRACKER_REPLICAS, []byte(ringId))
if err != nil {
return nil, err
}
if len(trackerNodes) == 0 {
return nil, fmt.Errorf("Unable to get any successors while trying to join ring")
}
_, transport, conf := CreateNewTCPTransport(localOnly)
vnodes, err := tr.ring.Transport().JoinRing(trackerNodes[0], ringId, &Vnode{Host: conf.Hostname})
if err != nil {
return nil, err
}
if len(vnodes) == 0 {
// I'm the first person joining the ring.
// Create the ring and wait for others to join.
glog.Infof("**** No one in the ring right now. Bootstrapping the ring. ******")
ring, err := Create(conf, transport)
if err != nil {
return nil, err
}
return ring, nil
}
for _, vnode := range vnodes {
if glog.V(2) {
glog.Infof("[Ring: %s] Connecting to %s", ringId, vnode.Host)
}
ring, err := BlockingJoin(conf, transport, vnode.Host)
if err == nil {
return ring, nil
}
}
return nil, fmt.Errorf("Cannot connect to any existing nodes in the ring")
}
func (tr *TrackerClientImpl) LeaveRing(ringId string) error {
_, err := tr.ring.Lookup(NUM_TRACKER_REPLICAS, []byte(ringId))
if err != nil {
return err
}
panic("TODO: LeaveRing")
}
var _ TrackerClient = new(TrackerClientImpl)