@@ -331,10 +331,14 @@ func (n *Node) doSendMessage(pool *Pool, data []byte) {
331
331
332
332
c := intern .NewRaftClient (client )
333
333
p := & api.Payload {Data : data }
334
+ batch := & intern.RaftBatch {
335
+ Context : n .RaftContext ,
336
+ Payload : p ,
337
+ }
334
338
335
339
ch := make (chan error , 1 )
336
340
go func () {
337
- _ , err := c .RaftMessage (ctx , p )
341
+ _ , err := c .RaftMessage (ctx , batch )
338
342
if err != nil {
339
343
x .Printf ("Error while sending message to node with addr: %s, err: %v\n " , pool .Addr , err )
340
344
}
@@ -464,6 +468,7 @@ var errReadIndex = x.Errorf("cannot get linearized read (time expired or no conf
464
468
465
469
func (n * Node ) WaitLinearizableRead (ctx context.Context ) error {
466
470
indexCh := make (chan uint64 , 1 )
471
+
467
472
select {
468
473
case n .requestCh <- linReadReq {indexCh : indexCh }:
469
474
case <- ctx .Done ():
@@ -489,7 +494,7 @@ func (n *Node) RunReadIndexLoop(closer *y.Closer, readStateCh <-chan raft.ReadSt
489
494
ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
490
495
defer cancel ()
491
496
492
- activeRctx := make ([ ]byte , 8 )
497
+ var activeRctx [ 8 ]byte
493
498
x .Check2 (n .Rand .Read (activeRctx [:]))
494
499
if err := n .Raft ().ReadIndex (ctx , activeRctx [:]); err != nil {
495
500
x .Errorf ("Error while trying to call ReadIndex: %v\n " , err )
@@ -543,6 +548,7 @@ func (n *Node) RunReadIndexLoop(closer *y.Closer, readStateCh <-chan raft.ReadSt
543
548
for _ , req := range requests {
544
549
req .indexCh <- index
545
550
}
551
+ break
546
552
}
547
553
requests = requests [:0 ]
548
554
}
@@ -625,17 +631,10 @@ var (
625
631
)
626
632
627
633
func (w * RaftServer ) applyMessage (ctx context.Context , msg raftpb.Message ) error {
628
- var rc intern.RaftContext
629
- x .Check (rc .Unmarshal (msg .Context ))
630
-
631
634
node := w .GetNode ()
632
635
if node == nil || node .Raft () == nil {
633
636
return errNoNode
634
637
}
635
- if rc .Group != node .RaftContext .Group {
636
- return errNoNode
637
- }
638
- node .Connect (msg .From , rc .Addr )
639
638
640
639
c := make (chan error , 1 )
641
640
go func () { c <- node .Raft ().Step (ctx , msg ) }()
@@ -648,24 +647,33 @@ func (w *RaftServer) applyMessage(ctx context.Context, msg raftpb.Message) error
648
647
}
649
648
}
650
649
func (w * RaftServer ) RaftMessage (ctx context.Context ,
651
- query * api. Payload ) (* api.Payload , error ) {
650
+ batch * intern. RaftBatch ) (* api.Payload , error ) {
652
651
if ctx .Err () != nil {
653
652
return & api.Payload {}, ctx .Err ()
654
653
}
655
654
656
- for idx := 0 ; idx < len (query .Data ); {
657
- x .AssertTruef (len (query .Data [idx :]) >= 4 ,
658
- "Slice left of size: %v. Expected at least 4." , len (query .Data [idx :]))
655
+ rc := batch .GetContext ()
656
+ if rc != nil {
657
+ w .GetNode ().Connect (rc .Id , rc .Addr )
658
+ }
659
+ if batch .GetPayload () == nil {
660
+ return & api.Payload {}, nil
661
+ }
662
+ data := batch .Payload .Data
663
+
664
+ for idx := 0 ; idx < len (data ); {
665
+ x .AssertTruef (len (data [idx :]) >= 4 ,
666
+ "Slice left of size: %v. Expected at least 4." , len (data [idx :]))
659
667
660
- sz := int (binary .LittleEndian .Uint32 (query . Data [idx : idx + 4 ]))
668
+ sz := int (binary .LittleEndian .Uint32 (data [idx : idx + 4 ]))
661
669
idx += 4
662
670
msg := raftpb.Message {}
663
- if idx + sz > len (query . Data ) {
671
+ if idx + sz > len (data ) {
664
672
return & api.Payload {}, x .Errorf (
665
673
"Invalid query. Specified size %v overflows slice [%v,%v)\n " ,
666
- sz , idx , len (query . Data ))
674
+ sz , idx , len (data ))
667
675
}
668
- if err := msg .Unmarshal (query . Data [idx : idx + sz ]); err != nil {
676
+ if err := msg .Unmarshal (data [idx : idx + sz ]); err != nil {
669
677
x .Check (err )
670
678
}
671
679
if err := w .applyMessage (ctx , msg ); err != nil {
0 commit comments