Skip to content

Commit a1ee6b5

Browse files
committed
[FAB-14278] Fix etcdraft flaky UT
UT: lagged node can catch up using snapshot Even if snapshot interval is 1 Byte, it's NOT guaranteed that snapshot is taken for EVERY block, because snapshot is taken async on one go routine (for the exact purpose of not taking snapshot too excessively, i.e. per every block) Although, this is non-deterministic and our assertions should be more lenient. Change-Id: Iab9305482d683b00c1331e85df1a6dc78868d06d Signed-off-by: Jay Guo <guojiannan1101@gmail.com>
1 parent 54f4605 commit a1ee6b5

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

orderer/consensus/etcdraft/chain_test.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,48 +2443,41 @@ var _ = Describe("Chain", func() {
24432443

24442444
It("lagged node can catch up using snapshot", func() {
24452445
network.disconnect(2)
2446-
24472446
c1.cutter.CutNext = true
24482447

2449-
var lasti uint64
2450-
var indices []uint64
2451-
// Only produce 4 blocks here, so that snapshot pruning does not occur.
2452-
// Otherwise, a slow garbage collection may prevent snapshotting from
2453-
// being triggered on next block, and assertion on number of snapshots
2454-
// would fail nondeterministically.
2455-
for i := 1; i <= 4; i++ {
2456-
err := c1.Order(env, 0)
2457-
Expect(err).NotTo(HaveOccurred())
2458-
Eventually(c1.support.WriteBlockCallCount, LongEventualTimeout).Should(Equal(i))
2459-
Eventually(c3.support.WriteBlockCallCount, LongEventualTimeout).Should(Equal(i))
2460-
Eventually(func() uint64 {
2461-
indices = etcdraft.ListSnapshots(logger, c1.opts.SnapDir)
2462-
if len(indices) == 0 {
2463-
return 0
2464-
}
2465-
return indices[len(indices)-1]
2466-
}, LongEventualTimeout).Should(BeNumerically(">", lasti))
2467-
lasti = indices[len(indices)-1]
2468-
}
2448+
c2Lasti, _ := c2.opts.MemoryStorage.LastIndex()
2449+
var blockCnt int
2450+
// Order blocks until first index of c1 memory is greater than last index of c2,
2451+
// so a snapshot will be sent to c2 when it rejoins network
2452+
Eventually(func() bool {
2453+
c1Firsti, _ := c1.opts.MemoryStorage.FirstIndex()
2454+
if c1Firsti > c2Lasti+1 {
2455+
return true
2456+
}
2457+
2458+
Expect(c1.Order(env, 0)).To(Succeed())
2459+
blockCnt++
2460+
Eventually(c1.support.WriteBlockCallCount, LongEventualTimeout).Should(Equal(blockCnt))
2461+
Eventually(c3.support.WriteBlockCallCount, LongEventualTimeout).Should(Equal(blockCnt))
2462+
return false
2463+
}, LongEventualTimeout).Should(BeTrue())
24692464

24702465
Eventually(c2.support.WriteBlockCallCount, LongEventualTimeout).Should(Equal(0))
24712466

24722467
network.join(2, false)
24732468

2474-
Eventually(c2.puller.PullBlockCallCount, LongEventualTimeout).Should(Equal(4))
2475-
Eventually(c2.support.WriteBlockCallCount, LongEventualTimeout).Should(Equal(4))
2476-
2477-
files, err := ioutil.ReadDir(c2.opts.SnapDir)
2478-
Expect(err).NotTo(HaveOccurred())
2479-
Expect(files).To(HaveLen(1)) // expect to store exact 1 snapshot
2469+
Eventually(c2.support.WriteBlockCallCount, LongEventualTimeout).Should(Equal(blockCnt))
2470+
indices := etcdraft.ListSnapshots(logger, c2.opts.SnapDir)
2471+
Expect(indices).To(HaveLen(1))
2472+
gap := indices[0] - c2Lasti
2473+
Expect(c2.puller.PullBlockCallCount()).To(Equal(int(gap)))
24802474

24812475
// chain should keeps functioning
2482-
err = c1.Order(env, 0)
2483-
Expect(err).NotTo(HaveOccurred())
2476+
Expect(c2.Order(env, 0)).To(Succeed())
24842477

24852478
network.exec(
24862479
func(c *chain) {
2487-
Eventually(func() int { return c.support.WriteBlockCallCount() }, LongEventualTimeout).Should(Equal(5))
2480+
Eventually(func() int { return c.support.WriteBlockCallCount() }, LongEventualTimeout).Should(Equal(blockCnt + 1))
24882481
})
24892482
})
24902483
})

0 commit comments

Comments
 (0)