-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: add trait
RaftLogStorageExt
to provide additional raft-log…
… methods The `RaftLogReaderExt::blocking_append()` method enables the caller to append logs to storage in a blocking manner, eliminating the need to create and await a callback. This method simplifies the process of writing tests.
- Loading branch information
1 parent
273232c
commit 884f0da
Showing
9 changed files
with
61 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use anyerror::AnyError; | ||
use macros::add_async_trait; | ||
|
||
use crate::storage::LogFlushed; | ||
use crate::storage::RaftLogStorage; | ||
use crate::type_config::alias::AsyncRuntimeOf; | ||
use crate::AsyncRuntime; | ||
use crate::OptionalSend; | ||
use crate::RaftTypeConfig; | ||
use crate::StorageError; | ||
use crate::StorageIOError; | ||
|
||
/// Extension trait for RaftLogStorage to provide utility methods. | ||
/// | ||
/// All methods in this trait are provided with default implementation. | ||
#[add_async_trait] | ||
pub trait RaftLogStorageExt<C>: RaftLogStorage<C> | ||
where C: RaftTypeConfig | ||
{ | ||
/// Blocking mode append log entries to the storage. | ||
/// | ||
/// It blocks until the callback is called by the underlying storage implementation. | ||
async fn blocking_append<I>(&mut self, entries: I) -> Result<(), StorageError<C::NodeId>> | ||
where | ||
I: IntoIterator<Item = C::Entry> + OptionalSend, | ||
I::IntoIter: OptionalSend, | ||
{ | ||
let (tx, rx) = AsyncRuntimeOf::<C>::oneshot(); | ||
|
||
let callback = LogFlushed::new(None, tx); | ||
self.append(entries, callback).await?; | ||
rx.await | ||
.map_err(|e| StorageIOError::write_logs(AnyError::error(e)))? | ||
.map_err(|e| StorageIOError::write_logs(AnyError::error(e)))?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl<C, T> RaftLogStorageExt<C> for T | ||
where | ||
T: RaftLogStorage<C>, | ||
C: RaftTypeConfig, | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters