Make sure we use the leader's term to create AppendEntriesRequest.
We do not stop the heartbeats before increases the term of the leader. It is possible that the even after the state check, the leader stepsdown and use the new term to send out a AppendEntries request. We fixed this by recording the term when it is the leader(if it is not the leader at that time, flush() will return in the if staitement).