forked from armon/go-chord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chord.go
440 lines (367 loc) · 11.6 KB
/
chord.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
This package is used to provide an implementation of the
Chord network protocol.
*/
package buddystore
import (
"crypto/sha1"
"fmt"
"hash"
"sync"
"time"
"github.com/golang/glog"
)
const JOIN_STABILIZE_WAIT = 5
// Implements the methods needed for a Chord ring
type Transport interface {
// Gets a list of the vnodes on the box
ListVnodes(string) ([]*Vnode, error)
// Ping a Vnode, check for liveness
Ping(*Vnode) (bool, error)
// Request a nodes predecessor
GetPredecessor(*Vnode) (*Vnode, error)
// Notify our successor of ourselves
Notify(target, self *Vnode) ([]*Vnode, error)
// Find a successor
FindSuccessors(*Vnode, int, []byte) ([]*Vnode, error)
// Clears a predecessor if it matches a given vnode. Used to leave.
ClearPredecessor(target, self *Vnode) error
// Instructs a node to skip a given successor. Used to leave.
SkipSuccessor(target, self *Vnode) error
// Get the list of predecessors
GetPredecessorList(*Vnode) ([]*Vnode, error)
// Register for an RPC callbacks
Register(*Vnode, VnodeRPC)
// Lock Manager operations
RLock(*Vnode, string, string, *OpsLogEntry) (string, uint, uint64, error)
WLock(*Vnode, string, uint, uint, string, *OpsLogEntry) (string, uint, uint, uint64, error)
CommitWLock(*Vnode, string, uint, string, *OpsLogEntry) (uint64, error)
AbortWLock(*Vnode, string, uint, string, *OpsLogEntry) (uint64, error)
InvalidateRLock(*Vnode, string) error
// KV Store operations
Get(target *Vnode, key string, version uint) ([]byte, error)
Set(target *Vnode, key string, version uint, value []byte) error
List(target *Vnode) ([]string, error)
BulkSet(target *Vnode, key string, valLst []KVStoreValue) error
SyncKeys(target *Vnode, ownerVn *Vnode, key string, ver []uint) error
MissingKeys(target *Vnode, replVn *Vnode, key string, ver []uint) error
PurgeVersions(target *Vnode, key string, maxVersion uint) error
// Tracker operations
JoinRing(target *Vnode, ringId string, self *Vnode) ([]*Vnode, error)
LeaveRing(target *Vnode, ringId string) error
// TODO: Is this the right place?
IsLocalVnode(vn *Vnode) bool
}
// These are the methods to invoke on the registered vnodes
type VnodeRPC interface {
GetPredecessor() (*Vnode, error)
Notify(*Vnode) ([]*Vnode, error)
FindSuccessors(int, []byte) ([]*Vnode, error)
ClearPredecessor(*Vnode) error
SkipSuccessor(*Vnode) error
GetPredecessorList() ([]*Vnode, error)
GetId() (string, error)
// KV Store operations
Get(key string, version uint) ([]byte, error)
Set(key string, version uint, value []byte) error
List() ([]string, error)
BulkSet(key string, valLst []KVStoreValue) error
SyncKeys(ownerVn *Vnode, key string, ver []uint) error
MissingKeys(replVn *Vnode, key string, ver []uint) error
PurgeVersions(key string, maxVersion uint) error
// Lock Manager operations
RLock(key string, nodeID string, remoteAddr string, opsLogEntry *OpsLogEntry) (string, uint, uint64, error)
WLock(key string, version uint, timeout uint, nodeID string, opsLogEntry *OpsLogEntry) (string, uint, uint, uint64, error)
CommitWLock(key string, version uint, nodeID string, opsLogEntry *OpsLogEntry) (uint64, error)
AbortWLock(key string, version uint, nodeID string, opsLogEntry *OpsLogEntry) (uint64, error)
InvalidateRLock(lockID string) error
CheckWLock(key string) (bool, uint, error)
UpdateVersionMap(versionMap *map[string]uint)
// Tracker operations
JoinRing(ringId string, self *Vnode) ([]*Vnode, error)
LeaveRing(ringId string) error
}
// Delegate to notify on ring events
type Delegate interface {
NewPredecessor(local, remoteNew, remotePrev *Vnode)
Leaving(local, pred, succ *Vnode)
PredecessorLeaving(local, remote *Vnode)
SuccessorLeaving(local, remote *Vnode)
Shutdown()
}
// Configuration for Chord nodes
type Config struct {
Hostname string // Local host name
NumVnodes int // Number of vnodes per physical node
HashFunc func() hash.Hash // Hash function to use
StabilizeMin time.Duration // Minimum stabilization time
StabilizeMax time.Duration // Maximum stabilization time
NumSuccessors int // Number of successors to maintain
Delegate Delegate // Invoked to handle ring events
hashBits int // Bit size of the hash function
RingId string
}
// Represents an Vnode, local or remote
type Vnode struct {
Id []byte // Virtual ID
Host string // Host identifier
fullLock sync.RWMutex
}
type localVnodeIface interface {
Ring() RingIntf
Successors() []*Vnode
Predecessors() []*Vnode
Predecessor() *Vnode
localVnodeId() []byte
GetVnode() *Vnode
}
// Represents a local Vnode
type localVnode struct {
Vnode
ring *Ring
successors []*Vnode
successorsLock sync.RWMutex
predecessors []*Vnode
finger []*Vnode
fingerLock sync.RWMutex
last_finger int
predecessor *Vnode
predecessorLock sync.RWMutex
stabilized time.Time
timer *time.Timer
timerLock sync.Mutex
store *KVStore
lm *LManager
lm_client *LManagerClient
tracker Tracker
// Implements:
localVnodeIface
}
func (lvn *localVnode) Ring() RingIntf {
return lvn.ring
}
func (lvn *localVnode) CopyOfSuccessors() []*Vnode {
defer lvn.successorsLock.RUnlock()
lvn.successorsLock.RLock()
return copyOfVnodesList(lvn.successors, len(lvn.successors))
}
func (lvn *localVnode) Successors() []*Vnode {
defer lvn.successorsLock.RUnlock()
lvn.successorsLock.RLock()
return lvn.successors
}
func (lvn *localVnode) Predecessors() []*Vnode {
defer lvn.predecessorLock.RUnlock()
lvn.predecessorLock.RLock()
return lvn.predecessors
}
func (lvn *localVnode) Predecessor() *Vnode {
defer lvn.predecessorLock.RUnlock()
lvn.predecessorLock.RLock()
return lvn.predecessor
}
func (lvn *localVnode) localVnodeId() []byte {
return lvn.Id
}
func (lvn *localVnode) GetVnode() *Vnode {
return &lvn.Vnode
}
type RingIntf interface {
Leave() error
Shutdown()
Lookup(n int, key []byte) ([]*Vnode, error)
Transport() Transport
GetNumSuccessors() int
GetLocalVnode() *Vnode
GetLocalLocalVnode() *localVnode
GetRingId() string
GetHashFunc() func() hash.Hash
}
// Stores the state required for a Chord ring
type Ring struct {
config *Config
transport Transport
vnodes []*localVnode
delegateCh chan func()
shutdownComplete chan bool
shutdownRequested bool
shutdownLock sync.Mutex
// Implements:
RingIntf
}
// Returns the default Ring configuration
func DefaultConfig(hostname string) *Config {
return &Config{
hostname,
8, // 8 vnodes
sha1.New, // SHA1
time.Duration(5 * time.Second),
time.Duration(15 * time.Second),
8, // 8 successors
nil, // No delegate
160, // 160bit hash function
"",
}
}
// Creates a new Chord ring given the config and transport
func Create(conf *Config, trans Transport) (*Ring, error) {
// Initialize the hash bits
conf.hashBits = conf.HashFunc().Size() * 8
// Create and initialize a ring
ring := &Ring{}
ring.init(conf, trans)
ring.setLocalSuccessors()
ring.setLocalPredecessors()
ring.schedule()
return ring, nil
}
// Joins an existing Chord ring
func Join(conf *Config, trans Transport, existing string) (*Ring, error) {
// Initialize the hash bits
conf.hashBits = conf.HashFunc().Size() * 8
// Request a list of Vnodes from the remote host
hosts, err := trans.ListVnodes(existing)
if err != nil {
return nil, err
}
if hosts == nil || len(hosts) == 0 {
return nil, fmt.Errorf("Remote host has no vnodes!")
}
if glog.V(2) {
glog.Infof("Fetched hosts: %s", hosts)
}
// Create a ring
ring := &Ring{}
ring.init(conf, trans)
// Acquire a live successor for each Vnode
for _, vn := range ring.vnodes {
// Get the nearest remote vnode
nearest := nearestVnodeToKey(hosts, vn.Id)
// Query for a list of successors to this Vnode
succs, err := trans.FindSuccessors(nearest, conf.NumSuccessors, vn.Id)
if err != nil {
return nil, fmt.Errorf("Failed to find successor for vnodes! Got %s", err)
}
if succs == nil || len(succs) == 0 {
return nil, fmt.Errorf("Failed to find successor for vnodes! Got no vnodes!")
}
// Assign the successors
for idx, s := range succs {
vn.successors[idx] = s
}
}
// Do a fast stabilization, will schedule regular execution
for _, vn := range ring.vnodes {
vn.stabilize()
}
return ring, nil
}
/* BlockingJoin. Called by the buddynode that wants to block all operations until the network is healed.
Reason : All its operations should happen in its namespace. And its namespace i.e. the ring, specicifically the bootstrap members are present in the original ring
*/
func BlockingJoin(conf *Config, trans Transport, existing string) (*Ring, error) {
// Initialize the hash bits
conf.hashBits = conf.HashFunc().Size() * 8
// Request a list of Vnodes from the remote host
hosts, err := trans.ListVnodes(existing)
if err != nil {
return nil, err
}
if hosts == nil || len(hosts) == 0 {
return nil, fmt.Errorf("Remote host has no vnodes!")
}
if glog.V(2) {
glog.Infof("Fetched hosts: %s", hosts)
}
// Create a ring
ring := &Ring{}
ring.initBlockingLM(conf, trans)
// Acquire a live successor for each Vnode
for _, vn := range ring.vnodes {
// Get the nearest remote vnode
nearest := nearestVnodeToKey(hosts, vn.Id)
// Query for a list of successors to this Vnode
succs, err := trans.FindSuccessors(nearest, conf.NumSuccessors, vn.Id)
if err != nil {
return nil, fmt.Errorf("Failed to find successor for vnodes! Got %s", err)
}
if succs == nil || len(succs) == 0 {
return nil, fmt.Errorf("Failed to find successor for vnodes! Got no vnodes!")
}
// Assign the successors
for idx, s := range succs {
vn.successors[idx] = s
}
}
// Do a fast stabilization, will schedule regular execution
for _, vn := range ring.vnodes {
vn.stabilize()
vn.lm.cancelCheckStatus = time.AfterFunc(JOIN_STABILIZE_WAIT*time.Second, vn.lm.CheckStatus)
}
return ring, nil
}
// Leaves a given Chord ring and shuts down the local vnodes
func (r *Ring) Leave() error {
// Shutdown the vnodes first to avoid further stabilization runs
r.stopVnodes()
// Instruct each vnode to leave
var err error
for _, vn := range r.vnodes {
err = mergeErrors(err, vn.leave())
}
// Wait for the delegate callbacks to complete
r.stopDelegate()
return err
}
// Shutdown shuts down the local processes in a given Chord ring
// Blocks until all the vnodes terminate.
func (r *Ring) Shutdown() {
r.stopVnodes()
r.stopDelegate()
}
// Does a key lookup for up to N successors of a key
func (r *Ring) Lookup(n int, key []byte) ([]*Vnode, error) {
// Ensure that n is sane
if n > r.config.NumSuccessors {
return nil, fmt.Errorf("Cannot ask for more successors than NumSuccessors!")
}
// Hash the key
h := r.config.HashFunc()
h.Write(key)
key_hash := h.Sum(nil)
// Find the nearest local vnode
nearest := r.nearestVnode(key_hash)
// Use the nearest node for the lookup
successors, err := nearest.FindSuccessors(n, key_hash)
if err != nil {
return nil, err
}
// Trim the nil successors
for successors[len(successors)-1] == nil {
successors = successors[:len(successors)-1]
}
return successors, nil
}
func (r *Ring) Transport() Transport {
return r.transport
}
func (r *Ring) GetNumSuccessors() int {
return r.config.NumSuccessors
}
func (r *Ring) GetRingId() string {
return r.config.RingId
}
func (r *Ring) GetLocalLocalVnode() *localVnode {
return r.vnodes[0]
}
func (r *Ring) GetLocalVnode() *Vnode {
// TODO: Questionable code
// Is vnodes[0] always going to be a local node?
return &r.GetLocalLocalVnode().Vnode
}
func (r *Ring) GetHashFunc() func() hash.Hash {
return r.config.HashFunc
}
func (r *Ring) GetConfig() *Config {
return r.config
}