Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[store] refactor: impl RaftLog with SledVarTypeTree. Nothing really changed. #1241

Merged
merged 1 commit into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions fusestore/store/src/meta_service/raft_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,26 @@
//
// SPDX-License-Identifier: Apache-2.0.

use std::ops::Deref;
use std::ops::RangeBounds;

use async_raft::raft::Entry;
use common_tracing::tracing;

use crate::configs;
use crate::meta_service::sledkv;
use crate::meta_service::AsType;
use crate::meta_service::LogEntry;
use crate::meta_service::LogIndex;
use crate::meta_service::SledSerde;
use crate::meta_service::SledTree;
use crate::meta_service::SledValueToKey;
use crate::meta_service::SledVarTypeTree;

const TREE_RAFT_LOG: &str = "raft_log";

/// RaftLog stores the logs of a raft node.
/// It is part of MetaStore.
pub struct RaftLog {
inner: SledTree<LogIndex, Entry<LogEntry>>,
}

/// Allows to access directly the internal SledTree.
impl Deref for RaftLog {
type Target = SledTree<LogIndex, Entry<LogEntry>>;

fn deref(&self) -> &Self::Target {
&self.inner
}
pub(crate) inner: SledVarTypeTree,
}

impl SledSerde for Entry<LogEntry> {}
Expand All @@ -46,12 +38,23 @@ impl RaftLog {
db: &sled::Db,
config: &configs::Config,
) -> common_exception::Result<RaftLog> {
let rl = RaftLog {
inner: SledTree::open(db, TREE_RAFT_LOG, config.meta_sync()).await?,
};
let inner = SledVarTypeTree::open(db, TREE_RAFT_LOG, config.meta_sync()).await?;
let rl = RaftLog { inner };
Ok(rl)
}

pub fn contains_key(&self, key: &LogIndex) -> common_exception::Result<bool> {
self.logs().contains_key(key)
}

pub fn get(&self, key: &LogIndex) -> common_exception::Result<Option<Entry<LogEntry>>> {
self.logs().get(key)
}

pub fn last(&self) -> common_exception::Result<Option<(LogIndex, Entry<LogEntry>)>> {
self.logs().last()
}

/// Delete logs that are in `range`.
///
/// When this function returns the logs are guaranteed to be fsync-ed.
Expand All @@ -69,7 +72,17 @@ impl RaftLog {
///
pub async fn range_delete<R>(&self, range: R) -> common_exception::Result<()>
where R: RangeBounds<LogIndex> {
self.inner.range_delete(range, true).await
self.logs().range_delete(range, true).await
}

pub fn range_keys<R>(&self, range: R) -> common_exception::Result<Vec<LogIndex>>
where R: RangeBounds<LogIndex> {
self.logs().range_keys(range)
}

pub fn range_get<R>(&self, range: R) -> common_exception::Result<Vec<Entry<LogEntry>>>
where R: RangeBounds<LogIndex> {
self.logs().range_get(range)
}

/// Append logs into RaftLog.
Expand All @@ -78,7 +91,7 @@ impl RaftLog {
///
/// When this function returns the logs are guaranteed to be fsync-ed.
pub async fn append(&self, logs: &[Entry<LogEntry>]) -> common_exception::Result<()> {
self.inner.append_values(logs).await
self.logs().append_values(logs).await
}

/// Insert a single log.
Expand All @@ -87,6 +100,10 @@ impl RaftLog {
&self,
log: &Entry<LogEntry>,
) -> common_exception::Result<Option<Entry<LogEntry>>> {
self.inner.insert_value(log).await
self.logs().insert_value(log).await
}

fn logs(&self) -> AsType<sledkv::Logs> {
self.inner.as_type()
}
}
4 changes: 2 additions & 2 deletions fusestore/store/src/meta_service/raftmeta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ impl MetaStore {
) -> anyhow::Result<MembershipConfig> {
//TODO(xp): test it
let it = match upto_index {
Some(upto) => self.log.tree.range(0.ser()?..=upto.ser()?).rev(),
None => self.log.tree.range(0.ser()?..).rev(),
Some(upto) => self.log.inner.tree.range(0.ser()?..=upto.ser()?).rev(),
None => self.log.inner.tree.range(0.ser()?..).rev(),
};

for ivec in it {
Expand Down