Skip to content

Commit

Permalink
refactor(xlineapi): KeyRange refactor
Browse files Browse the repository at this point in the history
Signed-off-by: lxl66566 <[email protected]>
  • Loading branch information
lxl66566 committed Aug 8, 2024
1 parent 846d0e2 commit ee1ddac
Show file tree
Hide file tree
Showing 23 changed files with 492 additions and 428 deletions.
11 changes: 10 additions & 1 deletion crates/utils/src/interval_map/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::VecDeque;

use petgraph::graph::{DefaultIx, IndexType, NodeIndex};
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -979,7 +980,7 @@ where

/// The Interval stored in `IntervalMap`
/// Represents the interval [low, high)
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Interval<T> {
/// Low value
Expand All @@ -988,6 +989,14 @@ pub struct Interval<T> {
pub high: T,
}

impl<T> Interval<T> {
/// Transfer the interval into tuple
#[inline]
pub fn into_parts(self) -> (T, T) {
(self.low, self.high)
}
}

impl<T: Ord> Interval<T> {
/// Creates a new `Interval`
///
Expand Down
5 changes: 3 additions & 2 deletions crates/utils/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ pub fn parse_members(s: &str) -> Result<HashMap<String, Vec<String>>, ConfigPars
Ok(map)
}

/// Parse `ClusterRange` from the given string
/// Parse `ClusterRange` from the given string. The string should be in the
/// format of `start..end`, and start and end should be u64.
///
/// # Errors
///
Expand Down Expand Up @@ -185,7 +186,7 @@ pub fn parse_log_file(s: &str) -> Result<PathBuf, ConfigParseError> {
}
// This regular expression matches file paths in a specific format.
// Explanation of the regex pattern:
// ^(\.|\.\.)? - Matches an optional prefix consisting of a dot (.), two dots (..), or a tilde (~).
// ^(\.|\.\.)? - Matches an optional prefix consisting of a dot (.), two dots (..), or a tilde (~).
// (/[a-zA-Z0-9._-]+)+/? - Matches one or more occurrences of a forward slash (/) followed by one or more alphanumeric characters, dots (.), underscores (_), or hyphens (-).
// /?$ - Matches an optional trailing forward slash (/) at the end of the string.
// Overall, this regex pattern is used to validate file paths that follow a specific format commonly used in Linux filesystem
Expand Down
3 changes: 2 additions & 1 deletion crates/xline-client/src/clients/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use clippy_utilities::OverflowArithmetic;
use tokio::{task::JoinHandle, time::sleep};
use tonic::transport::Channel;
use xlineapi::{
command::{Command, CommandResponse, KeyRange, SyncResponse},
command::{Command, CommandResponse, SyncResponse},
keyrange::KeyRange,
Compare, CompareResult, CompareTarget, DeleteRangeRequest, EventType, PutRequest, RangeRequest,
RangeResponse, Request, RequestOp, RequestWrapper, Response, ResponseHeader, SortOrder,
SortTarget, TargetUnion, TxnRequest, TxnResponse,
Expand Down
2 changes: 1 addition & 1 deletion crates/xline-client/src/types/auth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use xlineapi::command::KeyRange;
use xlineapi::keyrange::KeyRange;
pub use xlineapi::{
AuthDisableResponse, AuthEnableResponse, AuthRoleAddResponse, AuthRoleDeleteResponse,
AuthRoleGetResponse, AuthRoleGrantPermissionResponse, AuthRoleListResponse,
Expand Down
2 changes: 1 addition & 1 deletion crates/xline-client/src/types/kv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use xlineapi::command::KeyRange;
use xlineapi::keyrange::KeyRange;
pub use xlineapi::{
CompactionResponse, CompareResult, CompareTarget, DeleteRangeResponse, PutResponse,
RangeResponse, Response, ResponseOp, SortOrder, SortTarget, TargetUnion, TxnResponse,
Expand Down
2 changes: 1 addition & 1 deletion crates/xline-client/src/types/range_end.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use xlineapi::command::KeyRange;
use xlineapi::keyrange::KeyRange;

/// Range end options, indicates how to set `range_end` from a key.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
Expand Down
2 changes: 1 addition & 1 deletion crates/xline-client/src/types/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl DerefMut for WatchStreaming {

#[cfg(test)]
mod tests {
use xlineapi::command::KeyRange;
use xlineapi::keyrange::KeyRange;

use super::*;

Expand Down
4 changes: 2 additions & 2 deletions crates/xline/src/conflict/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use curp::{
};
use utils::interval_map::Interval;
use xlineapi::{
command::{Command, KeyRange},
interval::BytesAffine,
command::Command,
keyrange::{BytesAffine, KeyRange},
RequestWrapper,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/xline/src/conflict/spec_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{collections::HashMap, sync::Arc};
use curp::server::conflict::CommandEntry;
use curp_external_api::conflict::{ConflictPoolOp, EntryId, SpeculativePoolOp};
use utils::interval_map::{Interval, IntervalMap};
use xlineapi::{command::Command, interval::BytesAffine};
use xlineapi::{command::Command, keyrange::BytesAffine};

use crate::storage::lease_store::LeaseCollection;

Expand Down
2 changes: 1 addition & 1 deletion crates/xline/src/conflict/uncommitted_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use curp::server::conflict::CommandEntry;
use curp_external_api::conflict::{ConflictPoolOp, EntryId, UncommittedPoolOp};
use itertools::Itertools;
use utils::interval_map::{Interval, IntervalMap};
use xlineapi::{command::Command, interval::BytesAffine};
use xlineapi::{command::Command, keyrange::BytesAffine};

use crate::storage::lease_store::LeaseCollection;

Expand Down
29 changes: 0 additions & 29 deletions crates/xline/src/server/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,6 @@ use crate::{
/// Key of applied index
pub(crate) const APPLIED_INDEX_KEY: &str = "applied_index";

/// Range start and end to get all keys
const UNBOUNDED: &[u8] = &[0_u8];
/// Range end to get one key
const ONE_KEY: &[u8] = &[];

/// Type of `KeyRange`
pub(crate) enum RangeType {
/// `KeyRange` contains only one key
OneKey,
/// `KeyRange` contains all keys
AllKeys,
/// `KeyRange` contains the keys in the range
Range,
}

impl RangeType {
/// Get `RangeType` by given `key` and `range_end`
#[inline]
pub(crate) fn get_range_type(key: &[u8], range_end: &[u8]) -> Self {
if range_end == ONE_KEY {
RangeType::OneKey
} else if key == UNBOUNDED && range_end == UNBOUNDED {
RangeType::AllKeys
} else {
RangeType::Range
}
}
}

/// Command Executor
#[derive(Debug)]
pub(crate) struct CommandExecutor {
Expand Down
3 changes: 2 additions & 1 deletion crates/xline/src/server/lock_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use utils::build_endpoint;
#[cfg(madsim)]
use utils::ClientTlsConfig;
use xlineapi::{
command::{Command, CommandResponse, CurpClient, KeyRange, SyncResponse},
command::{Command, CommandResponse, CurpClient, SyncResponse},
execute_error::ExecuteError,
keyrange::KeyRange,
AuthInfo, EventType,
};

Expand Down
4 changes: 2 additions & 2 deletions crates/xline/src/server/watch_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tokio::sync::mpsc;
use tokio_stream::{wrappers::ReceiverStream, Stream, StreamExt};
use tracing::{debug, warn};
use utils::task_manager::{tasks::TaskName, Listener, TaskManager};
use xlineapi::command::KeyRange;
use xlineapi::keyrange::KeyRange;

use crate::{
header_gen::HeaderGenerator,
Expand Down Expand Up @@ -207,7 +207,7 @@ where
return;
};

let key_range = KeyRange::new(req.key, req.range_end);
let key_range = KeyRange::new_etcd(req.key, req.range_end);
self.kv_watcher.watch(
watch_id,
key_range,
Expand Down
12 changes: 6 additions & 6 deletions crates/xline/src/storage/auth_store/perms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use jsonwebtoken::{
use merged_range::MergedRange;
use serde::{Deserialize, Serialize};
use utils::timestamp;
use xlineapi::{command::KeyRange, AuthInfo};
use xlineapi::{keyrange::KeyRange, AuthInfo};

use crate::rpc::{Permission, Type};

Expand Down Expand Up @@ -123,18 +123,18 @@ impl UserPermissions {

/// Insert a permission to `UserPermissions`
pub(super) fn insert(&mut self, perm: Permission) {
let range = KeyRange::new(perm.key, perm.range_end).unpack();
let range = KeyRange::new_etcd(perm.key, perm.range_end);
#[allow(clippy::unwrap_used)] // safe unwrap
match Type::try_from(perm.perm_type).unwrap() {
Type::Readwrite => {
self.read.insert(range.clone());
self.write.insert(range);
self.read.insert(range.clone().into_bounds());
self.write.insert(range.into_bounds());
}
Type::Write => {
self.write.insert(range);
self.write.insert(range.into_bounds());
}
Type::Read => {
self.read.insert(range);
self.read.insert(range.into_bounds());
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions crates/xline/src/storage/auth_store/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use pbkdf2::{
};
use utils::parking_lot_lock::RwLockMap;
use xlineapi::{
command::{CommandResponse, KeyRange, SyncResponse},
command::{CommandResponse, SyncResponse},
execute_error::ExecuteError,
keyrange::KeyRange,
AuthInfo,
};

Expand Down Expand Up @@ -1114,7 +1115,7 @@ impl AuthStore {
if user.has_role(ROOT_ROLE) {
return Ok(());
}
let key_range = KeyRange::new(key, range_end);
let key_range = KeyRange::new_etcd(key, range_end);
if let Some(permissions) = self.permission_cache.read().user_permissions.get(username) {
match perm_type {
Type::Read => {
Expand Down Expand Up @@ -1212,10 +1213,10 @@ mod test {
user_permissions: HashMap::from([(
"u".to_owned(),
UserPermissions {
read: MergedRange::from_iter(vec![KeyRange::new("foo", "")]),
read: MergedRange::from_iter(vec![KeyRange::new_etcd("foo", "")]),
write: MergedRange::from_iter(vec![
KeyRange::new("foo", ""),
KeyRange::new("fop", "foz")
KeyRange::new_etcd("foo", ""),
KeyRange::new_etcd("fop", "foz")
]),
},
)]),
Expand Down Expand Up @@ -1372,8 +1373,8 @@ mod test {
user_permissions: HashMap::from([(
"u".to_owned(),
UserPermissions {
read: MergedRange::from_iter(vec![KeyRange::new("foo", "")]),
write: MergedRange::from_iter(vec![KeyRange::new("foo", "")]),
read: MergedRange::from_iter(vec![KeyRange::new_etcd("foo", "")]),
write: MergedRange::from_iter(vec![KeyRange::new_etcd("foo", "")]),
},
)]),
role_to_users_map: HashMap::from([("r".to_owned(), vec!["u".to_owned()])]),
Expand Down
13 changes: 6 additions & 7 deletions crates/xline/src/storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use crossbeam_skiplist::{map::Entry, SkipMap};
use itertools::Itertools;
use parking_lot::{Mutex, RwLock};
use utils::parking_lot_lock::RwLockMap;
use xlineapi::command::KeyRange;
use xlineapi::keyrange::{EtcdKeyRange, KeyRange, RangeType, StdBoundRange};

use super::revision::{KeyRevision, Revision};
use crate::server::command::RangeType;

/// Operations for `Index`
pub(crate) trait IndexOperate {
Expand Down Expand Up @@ -163,7 +162,7 @@ impl Index {
.collect(),
RangeType::Range => self
.inner
.range(KeyRange::new(key, range_end))
.range(KeyRange::new_etcd(key, range_end))
.flat_map(|entry| {
entry
.value()
Expand Down Expand Up @@ -280,7 +279,7 @@ impl IndexOperate for Index {
.collect(),
RangeType::Range => self
.inner
.range(KeyRange::new(key, range_end))
.range(KeyRange::new_etcd(key, range_end))
.filter_map(fmap_value(|revs| Index::get_revision(revs, revision)))
.collect(),
}
Expand Down Expand Up @@ -374,7 +373,7 @@ impl IndexOperate for Index {
.unzip(),
RangeType::Range => self
.inner
.range(KeyRange::new(key, range_end))
.range(KeyRange::new_etcd(key, range_end))
.zip(0..)
.filter_map(|(entry, i)| {
entry.value().map_write(|mut revs| {
Expand Down Expand Up @@ -529,7 +528,7 @@ impl IndexOperate for IndexState<'_> {
.filter_map(|(_, revs)| Index::get_revision(revs.as_ref(), revision))
.collect(),
RangeType::Range => self
.range_key_revisions(KeyRange::new(key, range_end))
.range_key_revisions(KeyRange::new_etcd(key, range_end))
.into_iter()
.filter_map(|(_, revs)| Index::get_revision(revs.as_ref(), revision))
.collect(),
Expand Down Expand Up @@ -617,7 +616,7 @@ impl IndexOperate for IndexState<'_> {
.unzip(),
RangeType::AllKeys => self.delete_all(revision, sub_revision),
RangeType::Range => {
self.delete_range(KeyRange::new(key, range_end), revision, sub_revision)
self.delete_range(KeyRange::new_etcd(key, range_end), revision, sub_revision)
}
};

Expand Down
5 changes: 3 additions & 2 deletions crates/xline/src/storage/kv_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use tokio::sync::mpsc;
use tracing::{debug, warn};
use utils::table_names::{KV_TABLE, META_TABLE};
use xlineapi::{
command::{CommandResponse, KeyRange, SyncResponse},
command::{CommandResponse, SyncResponse},
execute_error::ExecuteError,
keyrange::{KeyRange, ONE_KEY},
};

use super::{
Expand Down Expand Up @@ -145,7 +146,7 @@ impl KvStoreInner {

/// Get previous `KeyValue` of a `KeyValue`
pub(crate) fn get_prev_kv(&self, kv: &KeyValue) -> Option<KeyValue> {
self.get_range(&kv.key, &[], kv.mod_revision.overflow_sub(1))
self.get_range(&kv.key, ONE_KEY, kv.mod_revision.overflow_sub(1))
.ok()?
.pop()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/xline/src/storage/kvwatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use utils::{
task_manager::{tasks::TaskName, Listener, TaskManager},
write_vec,
};
use xlineapi::command::KeyRange;
use xlineapi::keyrange::KeyRange;

use super::kv_store::KvStoreInner;
use crate::rpc::{Event, KeyValue};
Expand Down
Loading

0 comments on commit ee1ddac

Please sign in to comment.