• Jay Guo's avatar
    [FAB-12709] Use another way to elect leader in UT · 5c3e2fce
    Jay Guo authored
    In etcdraft UT, we often need to deterministically elect a leader.
    This was done by ticking ONLY one node in the network, so it is
    the only node that start campaign.
    HOWEVER, there are several problems with this approach:
    1. it's slow. We need real time interval between ticks due to the
       way fake clock is implemented: it drops tick on the floor in
       case of slow consumer.
    2. there is random factor in election timeout of etcd/raft. It is
       calculated as follow:
    randomElectionTimeout = electionTimeout + rand.Intn(electionTimeout)
       in another word, if we send electionTimeout ticks, it's not
       guaranteed to trigger a leader election
    3. if CheckQuorum is enabled, a lease is imposed on follower nodes
       which gets expired if
          electionTimeout <= elapsedTicks < randomElectionTimeout
       (if it's greater than randomElectionTimeout, it's reset to 0 and
       node starts campaign)
    In this CR, we send an artificial MsgTimeoutNow to the node to be
    elected. This message reliably triggers campaign and skip the lease
    This CR also fixes several potential data race and flakes in tests.
    Change-Id: I3c8e0bcadbb8cfa1ae3393de2ea711fdd0d8b7aa
    Signed-off-by: default avatarJay Guo <guojiannan1101@gmail.com>