@@ -2302,7 +2302,16 @@ var _ = Describe("Chain", func() {
2302
2302
Eventually (c .support .WriteConfigBlockCallCount , LongEventualTimeout ).Should (Equal (1 ))
2303
2303
})
2304
2304
c1 .setStepFunc (step1 )
2305
- network .elect (2 )
2305
+
2306
+ // elect node with higher index
2307
+ i2 , _ := c2 .storage .LastIndex () // err is always nil
2308
+ i3 , _ := c3 .storage .LastIndex ()
2309
+ candidate := uint64 (2 )
2310
+ if i3 > i2 {
2311
+ candidate = 3
2312
+ }
2313
+ network .chains [candidate ].cutter .CutNext = true
2314
+ network .elect (candidate )
2306
2315
2307
2316
_ , raftmetabytes := c1 .support .WriteConfigBlockArgsForCall (0 )
2308
2317
meta := & common.Metadata {Value : raftmetabytes }
@@ -2325,7 +2334,6 @@ var _ = Describe("Chain", func() {
2325
2334
Eventually (c4 .support .WriteConfigBlockCallCount , LongEventualTimeout ).Should (Equal (1 ))
2326
2335
2327
2336
By ("submitting new transaction to follower" )
2328
- c2 .cutter .CutNext = true
2329
2337
err = c4 .Order (env , 0 )
2330
2338
Expect (err ).NotTo (HaveOccurred ())
2331
2339
@@ -2337,12 +2345,7 @@ var _ = Describe("Chain", func() {
2337
2345
// node 1 has been stopped should not write any block
2338
2346
Consistently (c1 .support .WriteBlockCallCount ).Should (Equal (1 ))
2339
2347
2340
- network .connect (1 )
2341
-
2342
- c2 .clock .Increment (interval )
2343
- // check that former leader didn't get stuck and actually got resign signal,
2344
- // and once connected capable of communicating with rest of the replicas set
2345
- Eventually (c1 .observe , LongEventualTimeout ).Should (Receive (Equal (raft.SoftState {Lead : 2 , RaftState : raft .StateFollower })))
2348
+ network .join (1 , true )
2346
2349
Eventually (c1 .support .WriteBlockCallCount , LongEventualTimeout ).Should (Equal (2 ))
2347
2350
})
2348
2351
@@ -2400,8 +2403,15 @@ var _ = Describe("Chain", func() {
2400
2403
network .connect (i )
2401
2404
}
2402
2405
2403
- By ("re-elect node 2 to be a leader" )
2404
- network .elect (2 )
2406
+ // elect node with higher index
2407
+ i2 , _ := c2 .storage .LastIndex () // err is always nil
2408
+ i3 , _ := c3 .storage .LastIndex ()
2409
+ candidate := uint64 (2 )
2410
+ if i3 > i2 {
2411
+ candidate = 3
2412
+ }
2413
+ network .chains [candidate ].cutter .CutNext = true
2414
+ network .elect (candidate )
2405
2415
2406
2416
c4 .start ()
2407
2417
Expect (c4 .WaitReady ()).To (Succeed ())
@@ -2411,12 +2421,9 @@ var _ = Describe("Chain", func() {
2411
2421
Eventually (c4 .support .WriteConfigBlockCallCount , LongEventualTimeout ).Should (Equal (1 ))
2412
2422
2413
2423
By ("submitting new transaction to follower" )
2414
- c2 .cutter .CutNext = true
2415
2424
err = c4 .Order (env , 0 )
2416
2425
Expect (err ).NotTo (HaveOccurred ())
2417
2426
2418
- c2 .clock .Increment (interval )
2419
-
2420
2427
// rest nodes are alive include a newly added, hence should write 2 blocks
2421
2428
Eventually (c1 .support .WriteBlockCallCount , LongEventualTimeout ).Should (Equal (2 ))
2422
2429
Eventually (c2 .support .WriteBlockCallCount , LongEventualTimeout ).Should (Equal (2 ))
@@ -2456,11 +2463,17 @@ var _ = Describe("Chain", func() {
2456
2463
Eventually (c .support .WriteConfigBlockCallCount , LongEventualTimeout ).Should (Equal (1 ))
2457
2464
})
2458
2465
2459
- // electing new leader
2460
- network .elect (2 )
2466
+ // elect node with higher index
2467
+ i2 , _ := c2 .storage .LastIndex () // err is always nil
2468
+ i3 , _ := c3 .storage .LastIndex ()
2469
+ candidate := uint64 (2 )
2470
+ if i3 > i2 {
2471
+ candidate = 3
2472
+ }
2473
+ network .chains [candidate ].cutter .CutNext = true
2474
+ network .elect (candidate )
2461
2475
2462
2476
By ("submitting new transaction to follower" )
2463
- c2 .cutter .CutNext = true
2464
2477
err = c3 .Order (env , 0 )
2465
2478
Expect (err ).NotTo (HaveOccurred ())
2466
2479
@@ -3459,20 +3472,21 @@ func newChain(timeout time.Duration, channel string, dataDir string, id uint64,
3459
3472
fakeFields := newFakeMetricsFields ()
3460
3473
3461
3474
opts := etcdraft.Options {
3462
- RaftID : uint64 (id ),
3463
- Clock : clock ,
3464
- TickInterval : interval ,
3465
- ElectionTick : ELECTION_TICK ,
3466
- HeartbeatTick : HEARTBEAT_TICK ,
3467
- MaxSizePerMsg : 1024 * 1024 ,
3468
- MaxInflightBlocks : 256 ,
3469
- BlockMetadata : raftMetadata ,
3470
- Consenters : consenters ,
3471
- Logger : flogging .NewFabricLogger (zap .NewExample ()),
3472
- MemoryStorage : storage ,
3473
- WALDir : path .Join (dataDir , "wal" ),
3474
- SnapDir : path .Join (dataDir , "snapshot" ),
3475
- Metrics : newFakeMetrics (fakeFields ),
3475
+ RaftID : uint64 (id ),
3476
+ Clock : clock ,
3477
+ TickInterval : interval ,
3478
+ ElectionTick : ELECTION_TICK ,
3479
+ HeartbeatTick : HEARTBEAT_TICK ,
3480
+ MaxSizePerMsg : 1024 * 1024 ,
3481
+ MaxInflightBlocks : 256 ,
3482
+ BlockMetadata : raftMetadata ,
3483
+ LeaderCheckInterval : 500 * time .Millisecond ,
3484
+ Consenters : consenters ,
3485
+ Logger : flogging .NewFabricLogger (zap .NewExample ()),
3486
+ MemoryStorage : storage ,
3487
+ WALDir : path .Join (dataDir , "wal" ),
3488
+ SnapDir : path .Join (dataDir , "snapshot" ),
3489
+ Metrics : newFakeMetrics (fakeFields ),
3476
3490
}
3477
3491
3478
3492
support := & consensusmocks.FakeConsenterSupport {}
@@ -3901,6 +3915,7 @@ func (n *network) elect(id uint64) {
3901
3915
n .RUnlock ()
3902
3916
3903
3917
// Send node an artificial MsgTimeoutNow to emulate leadership transfer.
3918
+ fmt .Fprintf (GinkgoWriter , "Send artificial MsgTimeoutNow to elect node %d\n " , id )
3904
3919
candidate .Consensus (& orderer.ConsensusRequest {Payload : protoutil .MarshalOrPanic (& raftpb.Message {Type : raftpb .MsgTimeoutNow })}, 0 )
3905
3920
Eventually (candidate .observe , LongEventualTimeout ).Should (Receive (StateEqual (id , raft .StateLeader )))
3906
3921
0 commit comments