-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnode.go
587 lines (503 loc) · 16.4 KB
/
node.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// node package corresponds to what the CRAQ white paper refers to as a node.
package node
import (
"errors"
"log"
"sync"
"github.com/despreston/go-craq/store"
"github.com/despreston/go-craq/transport"
)
// neighbor is another node in the chain
type neighbor struct {
rpc transport.NodeClient
address string
}
// Opts is for passing options to the Node constructor.
type Opts struct {
// Storage layer to use
Store store.Storer
// Local listening address
Address string
// Address to advertise to other nodes and coordinator
PubAddress string
// Address of coordinator
CdrAddress string
// Transport creates new clients for communication with other nodes
Transport transport.NodeClientFactory
// For communication with the Coordinator
CoordinatorClient transport.CoordinatorClient
// Log
Log *log.Logger
}
type commitEvent struct {
Key string
Version uint64
}
// Node is what the white paper refers to as a node. This is the client that is
// responsible for storing data and handling reads/writes.
type Node struct {
// Other nodes in the chain
neighbors map[transport.NeighborPos]neighbor
// Storage layer
store store.Storer
// Latest version of a given key
latest map[string]uint64
// For listening to commit's. For testing.
committed chan commitEvent
cdrAddress, address, pubAddr string
cdr transport.CoordinatorClient
IsHead, IsTail bool
mu sync.Mutex
transport func() transport.NodeClient
log *log.Logger
}
// New creates a new Node.
func New(opts Opts) *Node {
logger := opts.Log
if opts.Log == nil {
logger = log.Default()
}
return &Node{
latest: make(map[string]uint64),
neighbors: make(map[transport.NeighborPos]neighbor, 3),
cdrAddress: opts.CdrAddress,
address: opts.Address,
store: opts.Store,
transport: opts.Transport,
pubAddr: opts.PubAddress,
cdr: opts.CoordinatorClient,
log: logger,
}
}
// ListenAndServe starts listening for messages and connects to the coordinator.
func (n *Node) Start() error {
if err := n.backfillLatest(); err != nil {
log.Fatalf("Failed to backfill latest versions.\n Error: %#v", err)
}
if err := n.connectToCoordinator(); err != nil {
log.Fatalf("Failed to connect to the chain.\n Error: %#v", err)
}
return nil
}
// backfillLatest queries the store for the latest committed version of
// everything it has in order to fill n.latest.
func (n *Node) backfillLatest() error {
c, err := n.store.AllCommitted()
if err != nil {
return err
}
for _, item := range c {
n.latest[item.Key] = item.Version
}
return nil
}
// ConnectToCoordinator let's the Node announce itself to the chain coordinator
// to be added to the chain. The coordinator responds with a message to tell the
// Node if it's the head or tail, and with the address of the previous node in the
// chain and the address to the tail node. The Node announces itself to the
// neighbor using the address given by the coordinator.
func (n *Node) connectToCoordinator() error {
err := n.cdr.Connect(n.cdrAddress)
if err != nil {
n.log.Println("Error connecting to the coordinator")
return err
}
n.log.Printf("Connected to coordinator at %s\n", n.cdrAddress)
// Announce self to the Coordinator
reply, err := n.cdr.AddNode(n.pubAddr)
if err != nil {
n.log.Println(err.Error())
return err
}
n.IsHead = reply.IsHead
n.IsTail = reply.IsTail
n.neighbors[transport.NeighborPosTail] = neighbor{address: reply.Tail}
// Connect to predecessor
if reply.Prev != "" {
if err := n.connectToNode(reply.Prev, transport.NeighborPosPrev); err != nil {
n.log.Printf("Failed to connect to node in ConnectToCoordinator. %v\n", err)
return err
}
if err := n.fullPropagate(); err != nil {
return err
}
} else if n.neighbors[transport.NeighborPosPrev].address != "" {
// Close the connection to the previous predecessor.
n.neighbors[transport.NeighborPosPrev].rpc.Close()
}
return nil
}
// send FwdPropagate and BackPropagate requests to new predecessor to get fully
// caught up. Forward propagation should go first so that it has all the dirty
// items needed before receiving backwards propagation response.
func (n *Node) fullPropagate() error {
prevNeighbor := n.neighbors[transport.NeighborPosPrev].rpc
if err := n.requestFwdPropagation(prevNeighbor); err != nil {
return err
}
return n.requestBackPropagation(prevNeighbor)
}
func (n *Node) connectToNode(address string, pos transport.NeighborPos) error {
newNbr := n.transport()
if err := newNbr.Connect(address); err != nil {
return err
}
n.log.Printf("connected to %s\n", address)
// Disconnect from current neighbor if there's one connected.
nbr := n.neighbors[pos]
if nbr.rpc != nil {
nbr.rpc.Close()
}
n.neighbors[pos] = neighbor{
rpc: newNbr,
address: address,
}
return nil
}
func (n *Node) writePropagated(reply *transport.PropagateResponse) error {
// Save items from reply to store.
for key, forKey := range *reply {
for _, item := range forKey {
if err := n.store.Write(key, item.Value, item.Version); err != nil {
n.log.Printf("Failed to write item %+v to store: %#v\n", item, err)
return err
}
n.log.Printf("wrote %s", key)
}
}
return nil
}
// Commit the version to the store, update n.latest for this key, and announce
// the commit to the n.committed channel if there is one.
func (n *Node) commit(key string, version uint64) error {
if err := n.store.Commit(key, version); err != nil {
n.log.Printf("Failed to commit. Key: %s Version: %d Error: %#v", key, version, err)
return err
}
n.latest[key] = version
if n.committed != nil {
n.committed <- commitEvent{Key: key, Version: version}
}
return nil
}
func (n *Node) commitPropagated(reply *transport.PropagateResponse) error {
// Commit items from reply to store.
for key, forKey := range *reply {
for _, item := range forKey {
// It's possible the item doesn't exist. In that case, add it first.
// This sort of a poor man's upsert, but it saves from having to
// deal w/ it in the storage layer, which should make it easier to
// write new storers.
if err := n.commit(key, item.Version); err != nil {
if err == store.ErrNotFound {
if err := n.store.Write(key, item.Value, item.Version); err != nil {
return err
}
if err := n.commit(key, item.Version); err != nil {
return err
}
} else {
return err
}
}
}
}
return nil
}
// Create a map of the highest versions for each key.
func propagateRequestFromItems(items []*store.Item) *transport.PropagateRequest {
req := transport.PropagateRequest{}
for _, item := range items {
if req[item.Key] < item.Version {
req[item.Key] = item.Version
}
}
return &req
}
// requestFwdPropagation asks client to respond with all uncommitted (dirty)
// items that this node either does not have or are newer than what this node
// has.
func (n *Node) requestFwdPropagation(client transport.NodeClient) error {
dirty, err := n.store.AllDirty()
if err != nil {
n.log.Printf("Failed to get all dirty items: %#v\n", err)
return err
}
reply, err := client.FwdPropagate(propagateRequestFromItems(dirty))
if err != nil {
n.log.Printf("Failed during forward propagation: %#v\n", err)
return err
}
return n.writePropagated(reply)
}
// requestBackPropagation asks client to respond with all committed items that
// this node either does not have or are newer than what this node has.
func (n *Node) requestBackPropagation(client transport.NodeClient) error {
committed, err := n.store.AllCommitted()
if err != nil {
n.log.Printf("Failed to get all committed items: %#v\n", err)
return err
}
reply, err := client.BackPropagate(propagateRequestFromItems(committed))
if err != nil {
n.log.Printf("Failed during back propagation: %#v\n", err)
return err
}
return n.commitPropagated(reply)
}
// resetNeighbor closes any open connection and resets the neighbor.
func (n *Node) resetNeighbor(pos transport.NeighborPos) {
n.neighbors[pos].rpc.Close()
n.neighbors[pos] = neighbor{}
}
// Ping responds to ping messages.
func (n *Node) Ping() error {
return nil
}
func (n *Node) connectToPredecessor(address string) error {
prev := n.neighbors[transport.NeighborPosPrev]
if prev.address == address {
n.log.Println("New predecessor same address as last one, keeping conn.")
return nil
} else if address == "" {
n.log.Println("Resetting predecessor")
n.resetNeighbor(transport.NeighborPosPrev)
return nil
}
n.log.Printf("connecting to new predecessor %s\n", address)
if err := n.connectToNode(address, transport.NeighborPosPrev); err != nil {
return err
}
prevC := n.neighbors[transport.NeighborPosPrev].rpc
return n.requestFwdPropagation(prevC)
}
func (n *Node) connectToSuccessor(address string) error {
next := n.neighbors[transport.NeighborPosNext]
if next.address == address {
n.log.Println("New successor same address as last one, keeping conn.")
return nil
} else if address == "" {
n.log.Println("Resetting successor")
n.resetNeighbor(transport.NeighborPosNext)
return nil
}
n.log.Printf("connecting to new successor %s\n", address)
if err := n.connectToNode(address, transport.NeighborPosNext); err != nil {
return err
}
nextC := n.neighbors[transport.NeighborPosNext].rpc
return n.requestBackPropagation(nextC)
}
// Update is for updating a node's metadata. If new neighbors are given, the
// Node will disconnect from the current neighbors before connecting to the new
// ones. Coordinator uses this method to update metadata of the node when there
// is a failure or re-organization of the chain.
func (n *Node) Update(meta *transport.NodeMeta) error {
n.log.Printf("Received metadata update: %+v\n", meta)
n.mu.Lock()
defer n.mu.Unlock()
n.IsHead = meta.IsHead
n.IsTail = meta.IsTail
if err := n.connectToPredecessor(meta.Prev); err != nil {
return err
}
// connect to tail if address is different
tail := n.neighbors[transport.NeighborPosTail]
if !meta.IsTail && tail.address != meta.Tail && meta.Tail != "" {
err := n.connectToNode(meta.Tail, transport.NeighborPosTail)
if err != nil {
return err
}
}
if err := n.connectToSuccessor(meta.Next); err != nil {
return err
}
// If this node is now the tail, commit all dirty versions, then forward
// commits to predecessor.
if meta.IsTail {
dirty, err := n.store.AllDirty()
if err != nil {
n.log.Println("Error fetching all dirty items during node Update")
return err
}
for i := range dirty {
go func(item *store.Item) {
if err := n.commitAndSend(item.Key, item.Version); err != nil {
n.log.Printf(
"Error during commit & send for item: %#v, error: %#v\n",
item,
err,
)
}
}(dirty[i])
}
}
return nil
}
// ClientWrite adds a new object to the chain and starts the process of
// replication.
func (n *Node) ClientWrite(key string, val []byte) error {
// Increment version based off any existing objects for this key.
var version uint64
old, err := n.store.Read(key)
if err == nil {
version = old.Version + 1
}
if err := n.store.Write(key, val, version); err != nil {
n.log.Printf("Failed to create during ClientWrite. %v\n", err)
return err
}
n.log.Printf("Node RPC ClientWrite() created version %d of key %s\n", version, key)
// Forward the new object to the successor node.
next := n.neighbors[transport.NeighborPosNext]
// If there's no successor, it means this is the only node in the chain, so
// mark the item as committed and return early.
if next.address == "" {
n.log.Println("No successor")
if err := n.commit(key, version); err != nil {
return err
}
return nil
}
if err := next.rpc.Write(key, val, version); err != nil {
n.log.Printf("Failed to send to successor during ClientWrite. %v\n", err)
return err
}
return nil
}
// Write adds an object to the chain. If the node is not the tail, the Write is
// forwarded to the next node in the chain. If the node is tail, the object is
// marked committed and a Commit message is sent to the predecessor in the
// chain.
func (n *Node) Write(key string, val []byte, version uint64) error {
n.log.Printf("Node RPC Write() %s version %d to store\n", key, version)
if err := n.store.Write(key, val, version); err != nil {
n.log.Printf("Failed to write. %v\n", err)
return err
}
// If this isn't the tail node, the write needs to be forwarded along the
// chain to the next node.
if !n.IsTail {
next := n.neighbors[transport.NeighborPosNext]
if err := next.rpc.Write(key, val, version); err != nil {
n.log.Printf("Failed to send to successor during Write. %v\n", err)
return err
}
return nil
}
// At this point it's assumed this node is the tail.
if err := n.commit(key, version); err != nil {
n.log.Printf("Failed to mark as committed in Write. %v\n", err)
return err
}
// Start telling predecessors to mark this version committed.
n.sendCommitToPrev(key, version)
return nil
}
// commitAndSend commits an item to the store and sends a message to the
// predecessor node to tell it to commit as well.
func (n *Node) commitAndSend(key string, version uint64) error {
if err := n.commit(key, version); err != nil {
return err
}
// if this node has a predecessor, send commit to previous node
if n.neighbors[transport.NeighborPosPrev].address != "" {
return n.sendCommitToPrev(key, version)
}
return nil
}
func (n *Node) sendCommitToPrev(key string, version uint64) error {
prev := n.neighbors[transport.NeighborPosPrev]
if err := prev.rpc.Commit(key, version); err != nil {
n.log.Printf("Failed to send Commit to predecessor. %v\n", err)
return err
}
return nil
}
// Commit marks an object as committed in storage.
func (n *Node) Commit(key string, version uint64) error {
return n.commitAndSend(key, version)
}
// Read returns values from the store. If the store returns ErrDirtyItem, ask
// the tail for the latest committed version for this key. That ensures that
// every node in the chain returns the same version.
func (n *Node) Read(key string) (string, []byte, error) {
item, err := n.store.Read(key)
switch err {
case store.ErrNotFound:
return "", nil, errors.New("key doesn't exist")
case store.ErrDirtyItem:
_, v, err := n.neighbors[transport.NeighborPosTail].rpc.LatestVersion(key)
if err != nil {
n.log.Printf(
"Failed to get latest version of %s from the tail. %v\n",
key,
err,
)
return "", nil, err
}
item, err = n.store.ReadVersion(key, v)
if err != nil {
return "", nil, err
}
}
return key, item.Value, nil
}
// ReadAll returns all committed key/value pairs in the store.
func (n *Node) ReadAll() (*[]transport.Item, error) {
fullItems, err := n.store.AllCommitted()
if err != nil {
return nil, err
}
items := []transport.Item{}
for _, itm := range fullItems {
items = append(items, transport.Item{
Key: itm.Key,
Value: itm.Value,
})
}
return &items, nil
}
// LatestVersion provides the latest committed version for a given key in the
// store.
func (n *Node) LatestVersion(key string) (string, uint64, error) {
return key, n.latest[key], nil
}
// BackPropagate let's another node ask this node to send it all the committed
// items it has in it's storage. The node requesting back propagation should
// send the key + latest version of all committed items it has. This node
// responds with all committed items that: have a newer version, weren't
// included in the request.
func (n *Node) BackPropagate(
verByKey *transport.PropagateRequest,
) (*transport.PropagateResponse, error) {
unseen, err := n.store.AllNewerCommitted(map[string]uint64(*verByKey))
if err != nil {
return nil, err
}
return makePropagateResponse(unseen), nil
}
// FwdPropagate let's another node ask this node to send it all the dirty items
// it has in it's storage. The node requesting forward propagation should send
// the key + latest version of all uncommitted items it has. This node responds
// with all uncommitted items that: have a newer version, weren't included in
// the request.
func (n *Node) FwdPropagate(
verByKey *transport.PropagateRequest,
) (*transport.PropagateResponse, error) {
unseen, err := n.store.AllNewerDirty(map[string]uint64(*verByKey))
if err != nil {
return nil, err
}
return makePropagateResponse(unseen), nil
}
func makePropagateResponse(items []*store.Item) *transport.PropagateResponse {
response := transport.PropagateResponse{}
for _, item := range items {
response[item.Key] = append(response[item.Key], transport.ValueVersion{
Value: item.Value,
Version: item.Version,
})
}
return &response
}