forked from ha/doozerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.go
190 lines (159 loc) · 3.56 KB
/
boot.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"crypto/rand"
"encoding/base32"
"github.com/ha/doozer"
"time"
)
const attachTimeout = 1e9
func boot(name, id, laddr, buri string) *doozer.Conn {
b, err := doozer.DialUri(buri, "")
if err != nil {
panic(err)
}
err = b.Access(rwsk)
if err != nil {
panic(err)
}
cl := lookupAndAttach(b, name)
if cl == nil {
return elect(name, id, laddr, b)
}
return cl
}
// Elect chooses a seed node, and returns a connection to a cal.
// If this process is the seed, returns nil.
func elect(name, id, laddr string, b *doozer.Conn) *doozer.Conn {
// advertise our presence, since we might become a cal
nspath := "/ctl/ns/" + name + "/" + id
r, err := b.Set(nspath, 0, []byte(laddr))
if err != nil {
panic(err)
}
// fight to be the seed
_, err = b.Set("/ctl/boot/"+name, 0, []byte(id))
if err, ok := err.(*doozer.Error); ok && err.Err == doozer.ErrOldRev {
// we lost, lookup addresses again
cl := lookupAndAttach(b, name)
if cl == nil {
panic("failed to attach after losing election")
}
// also delete our entry, since we're not officially a cal yet.
// it gets set again in peer.Main when we become a cal.
err := b.Del(nspath, r)
if err != nil {
panic(err)
}
return cl
} else if err != nil {
panic(err)
}
return nil // we are the seed node -- don't attach
}
func lookupAndAttach(b *doozer.Conn, name string) *doozer.Conn {
as := lookup(b, name)
if len(as) > 0 {
cl := attach(name, as)
if cl != nil {
return cl
}
}
return nil
}
func attach(name string, addrs []string) *doozer.Conn {
ch := make(chan *doozer.Conn, 1)
for _, a := range addrs {
go func(a string) {
if c, _ := isCal(name, a); c != nil {
ch <- c
}
}(a)
}
go func() {
<-time.After(attachTimeout)
ch <- nil
}()
return <-ch
}
// IsCal checks if addr is a CAL in the cluster named name.
// Returns a client if so, nil if not.
func isCal(name, addr string) (*doozer.Conn, error) {
c, err := doozer.Dial(addr)
if err != nil {
return nil, err
}
err = c.Access(rwsk)
if err != nil {
return nil, err
}
v, _, _ := c.Get("/ctl/name", nil)
if string(v) != name {
return nil, nil
}
rev, err := c.Rev()
if err != nil {
return nil, err
}
var cals []string
names, err := c.Getdir("/ctl/cal", rev, 0, -1)
if err != nil {
return nil, err
}
for _, name := range names {
cals = append(cals, name)
}
for _, cal := range cals {
body, _, err := c.Get("/ctl/cal/"+cal, nil)
if err != nil || len(body) == 0 {
continue
}
id := string(body)
v, _, err := c.Get("/ctl/node/"+id+"/addr", nil)
if err != nil {
return nil, err
}
if string(v) == addr {
return c, nil
}
}
return nil, nil
}
// Find possible addresses for cluster named name.
func lookup(b *doozer.Conn, name string) (as []string) {
rev, err := b.Rev()
if err != nil {
panic(err)
}
path := "/ctl/ns/" + name
names, err := b.Getdir(path, rev, 0, -1)
if err == doozer.ErrNoEnt {
return nil
} else if err, ok := err.(*doozer.Error); ok && err.Err == doozer.ErrNoEnt {
return nil
} else if err != nil {
panic(err)
}
path += "/"
for _, name := range names {
body, _, err := b.Get(path+name, &rev)
if err != nil {
panic(err)
}
as = append(as, string(body))
}
return as
}
func randId() string {
const bits = 80 // enough for 10**8 ids with p(collision) < 10**-8
rnd := make([]byte, bits/8)
n, err := rand.Read(rnd)
if err != nil {
panic(err)
}
if n != len(rnd) {
panic("io.ReadFull len mismatch")
}
enc := make([]byte, base32.StdEncoding.EncodedLen(len(rnd)))
base32.StdEncoding.Encode(enc, rnd)
return string(enc)
}