xraft icon indicating copy to clipboard operation
xraft copied to clipboard

Handle with duplicate message in leader election.

Open Icysandwich opened this issue 2 years ago • 1 comments

See issue 27. I add a field replyNode in RequestVoteResult, and maintain a set votesGranted in NodeImpl for recording which nodes already votes. Besides, I add a test case.

Icysandwich avatar Mar 28 '22 10:03 Icysandwich

Thanks for your pull request.

As I said, server id not included in the messages because we know which node we are connecting to when we create the connection. Thus it is unnecessary to modify the Raft messages and it'll change a lot of code.

There are 2 places to handle the duplicated messages. One is the connection handler.

  • in.xnnyygn.xraft.core.rpc.nio.AbstractHandler
  • in.xnnyygn.xraft.core.rpc.nio.FromRemoteHandler

The other is the core Raft algorithm part, namely NodeImpl. I'd rather not propagate the node id to the core Raft algorithm because it is an edge case and can be handled by the connection handler. Checking the node id in the core Raft algorithm will make the code unclear for those who try to learn something from the Raft implementation.

You can find some special code for AppendEntriesResult in AbstractHandler which intends to ignore AppendEntriesResult if current node doesn't send an AppendEntriesRpc before. I expect to do the check for duplicated vote response in a similar way.

  • to add a variable termLastVoted in FromRemoteHandler or AbstractHandler
  • check the termLastVoted when encountering RequestVoteResult

This approach would be simple and easy to understand.

Please let me know if you have any thought about this approach.

Thank you.

xnnyygn avatar Mar 29 '22 08:03 xnnyygn