forked from databendlabs/openraft
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Introduce
RefLogId
as a reference to a log ID
Existing `LogIdOf<C>` provides a minimal storage implementation for a log ID with essential properties. In contrast, `RefLogId` offers the same information as `LogIdOf<C>` while adding additional system-defined properties. For example, in the future, `LogIdOf<C>` defined by the application will not need to implement `Ord`. However, `RefLogId`, used internally, will provide a system-defined `Ord` implementation. This change updates internal components to return `RefLogId` or accept it as an argument where possible, enabling more flexibility and consistency in handling log IDs. Change: refine the `RaftEntry` trait - The `RaftEntry` trait now requires `AsRef<LogIdOf<C>>` and `AsMut<LogIdOf<C>>`, providing a more standard API for accessing the log ID of a log entry. As a result, the `RaftEntry: RaftLogId` requirement is no longer needed. - A new method, `new_normal()`, has been added to the `RaftEntry` trait to replace the `FromAppData` trait. - Additional utility methods for working with entries are now provided in the `RaftEntryExt` trait. - Part of databendlabs#1278 Upgrade tips: 1. **For applications using a custom `RaftEntry` implementation** (e.g., declared with `declare_raft_types!(MyTypes: Entry = MyEntry)`): - Update the `RaftEntry` implementation for your custom entry type (`MyEntry`) by adding the `new_normal()` method. - Implement `AsRef<LogId>` and `AsMut<LogId>` for your custom entry type. 2. **For applications using the default `Entry` provided by OpenRaft**: - No changes are required. empty: try to remove log id from RaftTypeConfig
- Loading branch information
1 parent
3b76a7e
commit 1965b64
Showing
51 changed files
with
838 additions
and
387 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::fmt; | ||
|
||
use openraft::alias::LogIdOf; | ||
use openraft::entry::RaftEntry; | ||
use openraft::entry::RaftPayload; | ||
use openraft::EntryPayload; | ||
use openraft::Membership; | ||
|
||
use crate::protobuf as pb; | ||
use crate::TypeConfig; | ||
|
||
impl fmt::Display for pb::Entry { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "Entry{{term={},index={}}}", self.term, self.index) | ||
} | ||
} | ||
|
||
impl RaftPayload<TypeConfig> for pb::Entry { | ||
fn get_membership(&self) -> Option<Membership<TypeConfig>> { | ||
self.membership.clone().map(Into::into) | ||
} | ||
} | ||
|
||
impl RaftEntry<TypeConfig> for pb::Entry { | ||
fn new(log_id: LogIdOf<TypeConfig>, payload: EntryPayload<TypeConfig>) -> Self { | ||
let mut app_data = None; | ||
let mut membership = None; | ||
match payload { | ||
EntryPayload::Blank => {} | ||
EntryPayload::Normal(data) => app_data = Some(data), | ||
EntryPayload::Membership(m) => membership = Some(m.into()), | ||
} | ||
|
||
Self { | ||
term: log_id.leader_id, | ||
index: log_id.index, | ||
app_data, | ||
membership, | ||
} | ||
} | ||
|
||
fn log_id_parts(&self) -> (&u64, u64) { | ||
(&self.term, self.index) | ||
} | ||
|
||
fn set_log_id(&mut self, new: LogIdOf<TypeConfig>) { | ||
self.term = new.leader_id; | ||
self.index = new.index; | ||
} | ||
} |
Oops, something went wrong.