forked from armon/go-chord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
1226 lines (1057 loc) · 28.7 KB
/
net.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package buddystore
import (
"encoding/json"
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"github.com/golang/glog"
)
/*
TCPTransport provides a TCP based Chord transport layer. This allows Chord
to be implemented over a network, instead of only using the LocalTransport. It is
meant to be a simple implementation, optimizing for simplicity instead of performance.
Messages are sent with a header frame, followed by a body frame. All data is encoded
using the GOB format for simplicity.
Internally, there is 1 Goroutine listening for inbound connections, 1 Goroutine PER
inbound connection.
*/
type TCPTransport struct {
sock *net.TCPListener
timeout time.Duration
maxIdle time.Duration
lock sync.RWMutex
local map[string]*localRPC
inbound map[*net.TCPConn]struct{}
poolLock sync.Mutex
pool map[string][]*tcpOutConn
shutdown int32
// Implements:
Transport
}
var _ Transport = new(TCPTransport)
type tcpOutConn struct {
host string
sock *net.TCPConn
header tcpHeader
enc *json.Encoder
dec *json.Decoder
used time.Time
}
const (
tcpPing = iota
tcpListReq
tcpGetPredReq
tcpGetPredListReq
tcpNotifyReq
tcpFindSucReq
tcpClearPredReq
tcpSkipSucReq
tcpGet
tcpSet
tcpList
tcpBulkSet
tcpSyncKeys
tcpMissingKeys
tcpPurgeVersions
tcpRLockReq
tcpWLockReq
tcpCommitWLockReq
tcpAbortWLockReq
tcpInvalidateRLockReq
tcpVersionMapUpdate
tcpJoinRingReq
)
type tcpHeader struct {
ReqType int
}
type tcpRequest interface {
}
type TCPResponseBody interface {
}
type TCPResponse interface {
Error() error
SetError(error)
}
type TCPResponseImpl struct {
Err string `json:"err,string,omitempty"`
// Implements:
TCPResponse `json:"-"`
}
func (t *TCPResponseImpl) Error() error {
if t.Err != "" {
var nerr BuddyStoreError
json.Unmarshal([]byte(t.Err), &nerr)
// fmt.Println("TCPResponseImpl: ", t.Err)
return nerr
}
return nil
}
func (t *TCPResponseImpl) SetError(err error) {
if err != nil {
var nerr BuddyStoreError
var ok bool
if nerr, ok = err.(BuddyStoreError); ok {
} else {
nerr = PermanentError(err.Error())
}
e, _ := json.Marshal(nerr)
t.Err = string(e)
// fmt.Println("Creating error: ", t.Err)
}
}
var _ TCPResponse = new(tcpBodyVnodeListError)
// Potential body types
type tcpBodyError struct {
Dummy bool
// Extends:
TCPResponseImpl
}
type tcpBodyString struct {
S string
}
type tcpBodyVnode struct {
Vn *Vnode
}
type tcpBodyTwoVnode struct {
Target *Vnode
Vn *Vnode
}
type tcpBodyFindSuc struct {
Target *Vnode
Num int
Key []byte
}
type tcpBodyVnodeError struct {
Vnode *Vnode
// Extends:
TCPResponseImpl
}
type tcpBodyVnodeListError struct {
Vnodes []*Vnode
// Extends:
TCPResponseImpl
}
type tcpBodyBoolError struct {
B bool
// Extends:
TCPResponseImpl
}
// Creates a new TCP transport on the given listen address with the
// configured timeout duration.
func InitTCPTransport(listen string, timeout time.Duration) (*TCPTransport, error) {
// Try to start the listener
sock, err := net.Listen("tcp", listen)
if err != nil {
return nil, err
}
// allocate maps
local := make(map[string]*localRPC)
inbound := make(map[*net.TCPConn]struct{})
pool := make(map[string][]*tcpOutConn)
// Maximum age of a connection
maxIdle := time.Duration(100 * time.Second)
// Setup the transport
tcp := &TCPTransport{sock: sock.(*net.TCPListener),
timeout: timeout,
maxIdle: maxIdle,
local: local,
inbound: inbound,
pool: pool}
// Listen for connections
go tcp.listen()
// Reap old connections
go tcp.reapOld()
// Done
return tcp, nil
}
// Checks for a local vnode
func (t *TCPTransport) get(vn *Vnode) (VnodeRPC, bool) {
key := vn.String()
t.lock.RLock()
defer t.lock.RUnlock()
w, ok := t.local[key]
if ok {
return w.obj, ok
} else {
return nil, ok
}
}
// Gets an outbound connection to a host
func (t *TCPTransport) getConn(host string) (*tcpOutConn, error) {
// Check if we have a conn cached
var out *tcpOutConn
t.poolLock.Lock()
if atomic.LoadInt32(&t.shutdown) == 1 {
t.poolLock.Unlock()
return nil, fmt.Errorf("TCP transport is shutdown")
}
list, ok := t.pool[host]
if ok && len(list) > 0 {
out = list[len(list)-1]
list = list[:len(list)-1]
t.pool[host] = list
}
t.poolLock.Unlock()
if out == nil {
// Try to establish a connection
conn, err := net.DialTimeout("tcp", host, t.timeout)
if err != nil {
return nil, err
}
// Setup the socket
sock := conn.(*net.TCPConn)
t.setupConn(sock)
out = &tcpOutConn{host: host, sock: sock}
}
out.enc = json.NewEncoder(out.sock)
out.dec = json.NewDecoder(out.sock)
out.used = time.Now()
// Wrap the sock
return out, nil
}
// Returns an outbound TCP connection to the pool
func (t *TCPTransport) returnConn(o *tcpOutConn) {
// Update the last used time
o.used = time.Now()
// Push back into the pool
t.poolLock.Lock()
defer t.poolLock.Unlock()
if atomic.LoadInt32(&t.shutdown) == 1 {
o.sock.Close()
return
}
list, _ := t.pool[o.host]
t.pool[o.host] = append(list, o)
}
// Setup a connection
func (t *TCPTransport) setupConn(c *net.TCPConn) {
c.SetNoDelay(true)
c.SetKeepAlive(true)
}
func (t *TCPTransport) networkCall(host string, tcpReqType int, req tcpRequest, resp TCPResponse) error {
// Get a conn
out, err := t.getConn(host)
if err != nil {
return err
}
// Response channels
respChan := make(chan bool, 1)
errChan := make(chan error, 1)
go func() {
out.header.ReqType = tcpReqType
if err := out.enc.Encode(&out.header); err != nil {
errChan <- err
return
}
if err := out.enc.Encode(req); err != nil {
errChan <- err
return
}
// Read in the response
if err := out.dec.Decode(resp); err != nil {
errChan <- err
}
// glog.Infof("Resp: %s", resp.Error())
// Return the connection
t.returnConn(out)
if resp.Error() == nil {
respChan <- true
} else {
errChan <- resp.Error()
}
}()
select {
case <-time.After(t.timeout):
return fmt.Errorf("Command timed out!")
case err := <-errChan:
return err
case <-respChan:
return nil
}
}
// Gets a list of the vnodes on the box
func (t *TCPTransport) ListVnodes(host string) ([]*Vnode, error) {
resp := tcpBodyVnodeListError{}
err := t.networkCall(host, tcpListReq, tcpBodyString{S: host}, &resp)
if err != nil {
return nil, err
} else {
return resp.Vnodes, nil
}
}
// Ping a Vnode, check for liveness
func (t *TCPTransport) Ping(vn *Vnode) (bool, error) {
resp := tcpBodyBoolError{}
err := t.networkCall(vn.Host, tcpPing, tcpBodyVnode{Vn: vn}, &resp)
if err != nil {
return false, err
} else {
return resp.B, nil
}
}
// Request a nodes predecessor
func (t *TCPTransport) GetPredecessor(vn *Vnode) (*Vnode, error) {
resp := tcpBodyVnodeError{}
err := t.networkCall(vn.Host, tcpGetPredReq, tcpBodyVnode{Vn: vn}, &resp)
if err != nil {
return nil, err
} else {
return resp.Vnode, nil
}
}
// Notify our successor of ourselves
func (t *TCPTransport) Notify(target, self *Vnode) ([]*Vnode, error) {
resp := tcpBodyVnodeListError{}
err := t.networkCall(target.Host, tcpNotifyReq, tcpBodyTwoVnode{Target: target, Vn: self}, &resp)
if err != nil {
return nil, err
} else {
return resp.Vnodes, nil
}
}
// Find a successor
func (t *TCPTransport) FindSuccessors(vn *Vnode, n int, k []byte) ([]*Vnode, error) {
resp := new(tcpBodyVnodeListError)
vn.fullLock.RLock()
str := vn.Host
req := tcpBodyFindSuc{Target: vn, Num: n, Key: k}
vn.fullLock.RUnlock()
err := t.networkCall(str, tcpFindSucReq, req, resp)
if err != nil {
return nil, err
} else {
return resp.Vnodes, nil
}
}
// Request a nodes predecessor list
func (t *TCPTransport) GetPredecessorList(vn *Vnode) ([]*Vnode, error) {
resp := tcpBodyVnodeListError{}
err := t.networkCall(vn.Host, tcpGetPredListReq, tcpBodyVnode{Vn: vn}, &resp)
if err != nil {
return nil, err
} else {
return resp.Vnodes, nil
}
}
/* Transport operation that gets the value of a given key - This operation is additional to what is there in the interface already
*/
func (t *TCPTransport) Get(target *Vnode, key string, version uint) ([]byte, error) {
resp := tcpBodyRespValue{}
err := t.networkCall(target.Host, tcpGet, tcpBodyGet{Vnode: target, Key: key, Version: version}, &resp)
if err != nil {
return nil, err
} else {
return resp.Value, nil
}
}
/* Transport operation that sets the value of a given key - This operation is additional to what is there in the interface already
*/
func (t *TCPTransport) Set(target *Vnode, key string, version uint, value []byte) error {
resp := tcpBodyError{}
err := t.networkCall(target.Host, tcpSet, tcpBodySet{Vnode: target, Key: key, Version: version, Value: value}, &resp)
if err != nil {
return err
} else {
return nil
}
}
/* Transport operation that lists the keys for a particular ring - This operation is additional to what is there in the interface already
*/
func (t *TCPTransport) List(target *Vnode) ([]string, error) {
resp := tcpBodyRespKeys{}
err := t.networkCall(target.Host, tcpList, tcpBodyList{Vnode: target}, &resp)
if err != nil {
return nil, err
} else {
return resp.Keys, nil
}
}
func (t *TCPTransport) BulkSet(target *Vnode, key string, valLst []KVStoreValue) error {
resp := tcpBodyError{}
err := t.networkCall(target.Host, tcpBulkSet, tcpBodyBulkSet{Vnode: target, Key: key, ValueLst: valLst}, &resp)
if err != nil {
return err
} else {
return nil
}
}
func (t *TCPTransport) SyncKeys(target *Vnode, ownerVn *Vnode, key string, ver []uint) error {
resp := tcpBodyError{}
err := t.networkCall(target.Host, tcpSyncKeys, tcpBodySyncKeys{Vnode: target, OwnerVn: ownerVn, Key: key, Version: ver}, &resp)
if err != nil {
return err
} else {
return nil
}
}
func (t *TCPTransport) MissingKeys(target *Vnode, replVn *Vnode, key string, ver []uint) error {
resp := tcpBodyError{}
err := t.networkCall(target.Host, tcpMissingKeys, tcpBodyMissingKeys{Vnode: target, ReplVn: replVn, Key: key, Version: ver}, &resp)
if err != nil {
return err
} else {
return nil
}
}
func (t *TCPTransport) PurgeVersions(target *Vnode, key string, maxVersion uint) error {
resp := tcpBodyError{}
err := t.networkCall(target.Host, tcpPurgeVersions, tcpBodyPurgeVersions{Vnode: target, Key: key, MaxVersion: maxVersion}, &resp)
if err != nil {
return err
} else {
return nil
}
}
func (t *TCPTransport) JoinRing(target *Vnode, ringId string, joiner *Vnode) ([]*Vnode, error) {
resp := tcpBodyJoinRingResp{}
err := t.networkCall(target.Host, tcpJoinRingReq, tcpBodyJoinRingReq{Target: target, RingId: ringId, Joiner: joiner}, &resp)
if err != nil {
return nil, err
} else {
return resp.Vnodes, nil
}
}
// Clears a predecessor if it matches a given vnode. Used to leave.
func (t *TCPTransport) ClearPredecessor(target, self *Vnode) error {
resp := tcpBodyError{}
err := t.networkCall(target.Host, tcpClearPredReq, tcpBodyTwoVnode{Target: target, Vn: self}, &resp)
if err != nil {
return err
} else {
return nil
}
}
// Instructs a node to skip a given successor. Used to leave.
func (t *TCPTransport) SkipSuccessor(target, self *Vnode) error {
resp := tcpBodyError{}
err := t.networkCall(target.Host, tcpSkipSucReq, tcpBodyTwoVnode{Target: target, Vn: self}, &resp)
if err != nil {
return err
} else {
return nil
}
}
// Register for an RPC callbacks
func (t *TCPTransport) Register(v *Vnode, o VnodeRPC) {
key := v.String()
t.lock.Lock()
t.local[key] = &localRPC{v, o}
t.lock.Unlock()
}
// Shutdown the TCP transport
func (t *TCPTransport) Shutdown() {
atomic.StoreInt32(&t.shutdown, 1)
t.sock.Close()
// Close all the inbound connections
t.lock.RLock()
for conn := range t.inbound {
conn.Close()
}
t.lock.RUnlock()
// Close all the outbound
t.poolLock.Lock()
for _, conns := range t.pool {
for _, out := range conns {
out.sock.Close()
}
}
t.pool = nil
t.poolLock.Unlock()
}
// Closes old outbound connections
func (t *TCPTransport) reapOld() {
for {
if atomic.LoadInt32(&t.shutdown) == 1 {
return
}
time.Sleep(30 * time.Second)
t.reapOnce()
}
}
func (t *TCPTransport) reapOnce() {
t.poolLock.Lock()
defer t.poolLock.Unlock()
for host, conns := range t.pool {
max := len(conns)
for i := 0; i < max; i++ {
if time.Since(conns[i].used) > t.maxIdle {
conns[i].sock.Close()
conns[i], conns[max-1] = conns[max-1], nil
max--
i--
}
}
// Trim any idle conns
t.pool[host] = conns[:max]
}
}
// Listens for inbound connections
func (t *TCPTransport) listen() {
for {
conn, err := t.sock.AcceptTCP()
if err != nil {
if atomic.LoadInt32(&t.shutdown) == 0 {
glog.Errorf("Error accepting TCP connection! %s", err)
continue
} else {
return
}
}
// Setup the conn
t.setupConn(conn)
// Register the inbound conn
t.lock.Lock()
t.inbound[conn] = struct{}{}
t.lock.Unlock()
// Start handler
go t.handleConn(conn)
}
}
/*
RLock tranasport layer implementation
Param Vnode : The destination Vnode i.e. the Lock Manager
Param key : The key for which the read lock should be obtained
*/
func (t *TCPTransport) RLock(target *Vnode, key string, nodeID string, opsLogEntry *OpsLogEntry) (string, uint, uint64, error) {
resp := tcpBodyLMRLockResp{}
for k, _ := range t.local {
nodeID = k
break // Think of a better way to get the local nodeID
}
err := t.networkCall(target.Host, tcpRLockReq, tcpBodyLMRLockReq{Vn: target, Key: key, SenderID: nodeID, SenderAddr: t.sock.Addr().String(), OpsLogEntryPrimary: opsLogEntry}, &resp)
if err != nil {
return "", 0, 0, resp.Error()
} else {
return resp.LockId, resp.Version, resp.CommitPoint, nil
}
}
/*
WLock transport layer implementation
Param Vnode : The destination Vnode i.e. the Vnode with the Lock Manager
Param key : The key for which the write lock should be obtained
Param version : The version of the key
Param timeout : Requested Timeout value.
Param NodeID : NodeID of the requesting node
*/
func (t *TCPTransport) WLock(target *Vnode, key string, version uint, timeout uint, nodeID string, opsLogEntry *OpsLogEntry) (string, uint, uint, uint64, error) {
resp := tcpBodyLMWLockResp{}
err := t.networkCall(target.Host, tcpWLockReq, tcpBodyLMWLockReq{Vn: target, Key: key, Version: version, Timeout: timeout, SenderID: nodeID, OpsLogEntryPrimary: opsLogEntry}, &resp)
if err != nil {
return "", 0, 0, 0, err
} else {
return resp.LockId, resp.Version, resp.Timeout, resp.CommitPoint, nil
}
}
/*
CommitWLock transport layer implementation
Param Vnode : The destination Vnode i.e. the Lock Manager
Param key : The key for which the read lock should be obtained
Param version : The version of the key to be committed
*/
func (t *TCPTransport) CommitWLock(target *Vnode, key string, version uint, nodeID string, opsLogEntry *OpsLogEntry) (uint64, error) {
resp := tcpBodyLMCommitWLockResp{}
body := tcpBodyLMCommitWLockReq{Vn: target, Key: key, Version: version, SenderID: nodeID, OpsLogEntryPrimary: opsLogEntry}
err := t.networkCall(target.Host, tcpCommitWLockReq, body, &resp)
if err != nil {
return 0, resp.Error()
} else {
return resp.CommitPoint, nil
}
}
/*
InvalidateRLock transport layer implementation
Param Vnode : The destination Vnode i.e. the Client where the RLock should be invalidated
Param lockID : The exact lock to be invalidated
*/
func (t *TCPTransport) InvalidateRLock(target *Vnode, lockID string) error {
// Get a conn
out, err := t.getConn(target.Host)
if err != nil {
return err
}
respChan := make(chan bool, 1)
errChan := make(chan error, 1)
resp := tcpBodyLMInvalidateRLockResp{}
go func() {
// Send a list command
out.header.ReqType = tcpInvalidateRLockReq
body := tcpBodyLMInvalidateRLockReq{Vn: target, LockID: lockID}
if err := out.enc.Encode(&out.header); err != nil {
errChan <- err
return
}
if err := out.enc.Encode(&body); err != nil {
errChan <- err
return
}
// Read in the response
if err := out.dec.Decode(&resp); err != nil {
errChan <- err
return
}
// Return the connection
t.returnConn(out)
if resp.Error() == nil {
respChan <- true
} else {
errChan <- resp.Error()
}
}()
select {
case <-time.After(t.timeout):
return fmt.Errorf("Command timed out!")
case _ = <-errChan:
return resp.Error()
case <-respChan:
return resp.Error()
}
}
/*
AbortWLock transport layer implementation
Param Vnode : The destination Vnode i.e. the Lock Manager
Param key : The key for which the read lock should be obtained
*/
func (t *TCPTransport) AbortWLock(target *Vnode, key string, version uint, nodeID string, opsLogEntry *OpsLogEntry) (uint64, error) {
resp := tcpBodyLMAbortWLockResp{}
body := tcpBodyLMAbortWLockReq{Vn: target, Key: key, Version: version, SenderID: nodeID, OpsLogEntryPrimary: opsLogEntry}
err := t.networkCall(target.Host, tcpAbortWLockReq, body, &resp)
if err != nil {
return 0, resp.Error()
} else {
return resp.CommitPoint, nil
}
}
// Handles inbound TCP connections
func (t *TCPTransport) handleConn(conn *net.TCPConn) {
// Defer the cleanup
defer func() {
t.lock.Lock()
delete(t.inbound, conn)
t.lock.Unlock()
conn.Close()
}()
dec := json.NewDecoder(conn)
enc := json.NewEncoder(conn)
header := tcpHeader{}
var sendResp TCPResponse
for {
// Get the header
if err := dec.Decode(&header); err != nil {
if atomic.LoadInt32(&t.shutdown) == 0 && err.Error() != "EOF" {
glog.Errorf("Failed to decode TCP header! Got %s", err)
}
return
}
// Read in the body and process request
switch header.ReqType {
case tcpPing:
body := tcpBodyVnode{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
_, ok := t.get(body.Vn)
if ok {
sendResp = &tcpBodyBoolError{B: ok}
} else {
sendResp = &tcpBodyBoolError{B: ok}
sendResp.SetError(fmt.Errorf("Target VN not found! Target %s:%s", body.Vn.Host, body.Vn.String()))
}
case tcpListReq:
body := tcpBodyString{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate all the local clients
res := make([]*Vnode, 0, len(t.local))
// Build list
t.lock.RLock()
for _, v := range t.local {
res = append(res, v.vnode)
}
t.lock.RUnlock()
// Make response
sendResp = &tcpBodyVnodeListError{Vnodes: trimSlice(res)}
case tcpGetPredReq:
body := tcpBodyVnode{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
obj, ok := t.get(body.Vn)
resp := tcpBodyVnodeError{}
sendResp = &resp
if ok {
node, err := obj.GetPredecessor()
resp.Vnode = node
resp.SetError(err)
} else {
resp.SetError(fmt.Errorf("Target VN not found! Target %s:%s",
body.Vn.Host, body.Vn.String()))
}
case tcpNotifyReq:
body := tcpBodyTwoVnode{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
obj, ok := t.get(body.Target)
resp := tcpBodyVnodeListError{}
sendResp = &resp
if ok {
nodes, err := obj.Notify(body.Vn)
resp.Vnodes = trimSlice(nodes)
resp.SetError(err)
} else {
resp.SetError(fmt.Errorf("Target VN not found! Target %s:%s",
body.Target.Host, body.Target.String()))
}
case tcpFindSucReq:
body := tcpBodyFindSuc{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
obj, ok := t.get(body.Target)
resp := tcpBodyVnodeListError{}
sendResp = &resp
if ok {
nodes, err := obj.FindSuccessors(body.Num, body.Key)
resp.Vnodes = trimSlice(nodes)
resp.SetError(err)
} else {
resp.SetError(fmt.Errorf("Target VN not found! Target %s:%s",
body.Target.Host, body.Target.String()))
}
case tcpClearPredReq:
body := tcpBodyTwoVnode{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
obj, ok := t.get(body.Target)
resp := tcpBodyError{}
sendResp = &resp
if ok {
resp.SetError(obj.ClearPredecessor(body.Vn))
} else {
resp.SetError(fmt.Errorf("Target VN not found! Target %s:%s",
body.Target.Host, body.Target.String()))
}
case tcpSkipSucReq:
body := tcpBodyTwoVnode{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
obj, ok := t.get(body.Target)
resp := tcpBodyError{}
sendResp = &resp
if ok {
resp.SetError(obj.SkipSuccessor(body.Vn))
} else {
resp.SetError(fmt.Errorf("Target VN not found! Target %s:%s",
body.Target.Host, body.Target.String()))
}
case tcpGetPredListReq:
body := tcpBodyVnode{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
obj, ok := t.get(body.Vn)
resp := tcpBodyVnodeListError{}
sendResp = &resp
if ok {
nodes, err := obj.GetPredecessorList()
resp.Vnodes = nodes
resp.SetError(err)
} else {
resp.SetError(fmt.Errorf("Target VN not found! Target %s:%s",
body.Vn.Host, body.Vn.String()))
}
case tcpGet:
body := tcpBodyGet{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
obj, ok := t.get(body.Vnode)
resp := tcpBodyRespValue{}
sendResp = &resp
if ok {
value, err := obj.Get(body.Key, body.Version)
resp.Value = value
resp.SetError(err)
} else {
resp.SetError(fmt.Errorf("Target VN not found! Target %s:%s",
body.Vnode.Host, body.Vnode.String()))
}
case tcpSet:
body := tcpBodySet{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
obj, ok := t.get(body.Vnode)
resp := tcpBodyError{}
sendResp = &resp
if ok {
err := obj.Set(body.Key, body.Version, body.Value)
resp.SetError(err)
} else {
resp.SetError(fmt.Errorf("Target VN not found! Target %s:%s",
body.Vnode.Host, body.Vnode.String()))
}
case tcpList:
body := tcpBodyList{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
obj, ok := t.get(body.Vnode)
resp := tcpBodyRespKeys{}
sendResp = &resp
if ok {
keys, err := obj.List()
resp.Keys = keys
resp.SetError(err)
} else {
resp.SetError(fmt.Errorf("Target VN not found! Target %s:%s",
body.Vnode.Host, body.Vnode.String()))
}
case tcpBulkSet:
body := tcpBodyBulkSet{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return
}
// Generate a response
obj, ok := t.get(body.Vnode)
resp := tcpBodyError{}
sendResp = &resp
if ok {
err := obj.BulkSet(body.Key, body.ValueLst)
resp.SetError(err)
} else {
resp.SetError(fmt.Errorf("Target VN not found! Target %s:%s",
body.Vnode.Host, body.Vnode.String()))
}
case tcpSyncKeys:
body := tcpBodySyncKeys{}
if err := dec.Decode(&body); err != nil {
glog.Errorf("Failed to decode TCP body! Got %s", err)
return