xnnyygn / xraft

xnnyygn's raft implementation
MIT License
228 stars 107 forks source link

Handle with duplicate message in leader election. #28

Open Icysandwich opened 2 years ago

Icysandwich commented 2 years ago

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.

xnnyygn commented 2 years ago

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.

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.

This approach would be simple and easy to understand.

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

Thank you.