eBay / NuRaft

C++ implementation of Raft core logic as a replication library
Apache License 2.0
1.02k stars 241 forks source link

append_entries handling of multiple log records as batch #383

Closed kishorekrd closed 2 years ago

kishorekrd commented 2 years ago

end_of_append_batch() is called for disk flush after processing all the log records passed to append_entries(). I see that append_entries takes vector of log records. Is this operation atomic, as all or none? If I submit 10 records to it, what happens if 5 log records get appended and then 6th one fails? How is this handled?

greensky00 commented 2 years ago

From NuRaft's point of view, appending multiple logs doesn't need to be atomic as long as succeeded logs are consecutive.

"All or nothing" is up to you; if you want to make 10 logs atomic, you should make append_entries fail on the 6th log failure. Otherwise, it is OK for NuRaft to move forward with the succeeded 5 logs.

kishorekrd commented 2 years ago

Thanks for the response. Irrespective of the number of logs submitted to append_entries, handle_result() is called once. If partial number of logs append fails, How to handle it? What is the error that handle_result() gets in this case?

greensky00 commented 2 years ago

If failure is detected so that you want to make the call fail, you can return ReturnNull in the callback AppendLogs. https://github.com/eBay/NuRaft/blob/7ce0ef89775c0976c98381b3d5eda93cea6dca37/src/handle_client_request.cxx#L146-L147 then it will make append_entries call fail with BAD_REQUEST code.

kishorekrd commented 2 years ago

In case of raft_params::async_handler, // Async mode: // append_entries returns immediately. // handle_result will be invoked asynchronously, // after getting a consensus.

While appending 10 (multiple) logs, after appending 5 logs successfully, if leader change happens, remaining 5 logs will fail. How do the caller come to know this failure, so that he can either

  1. make first 5 logs also as failed and retry all again.
  2. retry the failed 5 logs again.

I see that caller comes to know result with handle_result(), which shows whether it succeeded or failed? https://github.com/eBay/NuRaft/blob/7ce0ef89775c0976c98381b3d5eda93cea6dca37/examples/calculator/calc_server.cxx#L47

Is there any other way to find this and return ReturnNull in the callback AppendLogs as you mentioned?

greensky00 commented 2 years ago
kishorekrd commented 2 years ago

OK that means, out of 10 logs, if leader change happens after appending 5 logs, logs 6 to 10 get the leader change error in the handle_result(), and also the new leader will rollback logs 1 to 5. Is this correct? If so, All I need to do is tell the caller that logs 1 to 10 failed and it can retry.

AppendLogs callback is for having custom checks on the appended logs.

Thanks for the help.

greensky00 commented 2 years ago

OK that means, out of 10 logs, if leader change happens after appending 5 logs, logs 6 to 10 get the leader change error in the handle_result(), and also the new leader will rollback logs 1 to 5. Is this correct?

Yes, that's correct.

kishorekrd commented 2 years ago

Thanks a lot for your help