eBay / NuRaft

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

Async operation for writing append record #379

Open kishorekrd opened 2 years ago

kishorekrd commented 2 years ago

Currently Nuraft has blocking append call (end_of_append_batch) for flush/store the append log records. Is there any way to make this as async operation. At the Follower, append is executed by nuraft thread, but on the Leader, where you submit the log record to raft, it has to block on end_of_append_batch() after multiple append calls. Is there any way to handover this to Nuraft thread, so that Leader server thread doesn't need to block on this?

greensky00 commented 2 years ago

First of all, please note that appended logs should be durable on disk at the time append_entries finishes its job, otherwise data loss may happen.

There can be two options:

1) You can use async handler mode, https://github.com/eBay/NuRaft/blob/4d4d6fd89810a92a148d327bb53b4063cca9540c/include/libnuraft/raft_params.hxx#L39-L44 and let end_of_append_batch invoke another thread and make it flush the disk. You should regard the completion of append_entries only when a) append_entries gets the successful result AND b) the disk flush is done.

2) You can use this (parallel_log_appending) experimental feature: https://github.com/eBay/NuRaft/blob/4d4d6fd89810a92a148d327bb53b4063cca9540c/include/libnuraft/raft_params.hxx#L577-L606 To do this, your append, write_at, and end_of_append_batch should trigger asynchronous disk write, and also you should implement notify_log_append_completion and last_durable_index properly.