Skip to content

Commit

Permalink
chore: do some small refactor
Browse files Browse the repository at this point in the history
Signed-off-by: themanforfree <[email protected]>
  • Loading branch information
themanforfree authored and Phoenix500526 committed Feb 16, 2024
1 parent 7f249e7 commit 1f17be2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
30 changes: 18 additions & 12 deletions crates/curp/src/server/storage/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ impl<C: Command> StorageApi for DB<C> {
ops.push(WriteOperation::new_put(
CF,
CLUSTER_ID.to_vec(),
bincode::serialize(&cluster_info.cluster_id())?,
cluster_info.cluster_id().to_be_bytes().to_vec(),
));
ops.push(WriteOperation::new_put(
CF,
MEMBER_ID.to_vec(),
bincode::serialize(&cluster_info.self_id())?,
cluster_info.self_id().to_be_bytes().to_vec(),
));
for m in cluster_info.all_members_vec() {
ops.push(WriteOperation::new_put(
Expand All @@ -102,16 +102,22 @@ impl<C: Command> StorageApi for DB<C> {

#[inline]
fn recover_cluster_info(&self) -> Result<Option<ClusterInfo>, StorageError> {
let cluster_id = self
.db
.get(CF, CLUSTER_ID)?
.map(|bytes| bincode::deserialize::<u64>(&bytes))
.transpose()?;
let member_id = self
.db
.get(CF, MEMBER_ID)?
.map(|bytes| bincode::deserialize::<u64>(&bytes))
.transpose()?;
let cluster_id = self.db.get(CF, CLUSTER_ID)?.map(|bytes| {
u64::from_be_bytes(
bytes
.as_slice()
.try_into()
.unwrap_or_else(|e| unreachable!("cannot decode index from backend, {e:?}")),
)
});
let member_id = self.db.get(CF, MEMBER_ID)?.map(|bytes| {
u64::from_be_bytes(
bytes
.as_slice()
.try_into()
.unwrap_or_else(|e| unreachable!("cannot decode index from backend, {e:?}")),
)
});
let mut members = vec![];
for (_k, v) in self.db.get_all(MEMBERS_CF)? {
let member = Member::decode(v.as_ref())?;
Expand Down
2 changes: 1 addition & 1 deletion crates/simulation/src/curp_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl CurpGroup {
pub async fn crash(&mut self, id: ServerId) {
let handle = madsim::runtime::Handle::current();
handle.kill(id.to_string());
madsim::time::sleep(Duration::from_secs(2)).await;
madsim::time::sleep(Duration::from_secs(10)).await;
if !handle.is_exit(id.to_string()) {
panic!("failed to crash node: {id}");
}
Expand Down
19 changes: 9 additions & 10 deletions crates/simulation/src/xline_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use madsim::runtime::NodeHandle;
use tonic::transport::Channel;
use tracing::debug;
use utils::config::{
default_quota, AuthConfig, ClientConfig, ClientConfig, ClusterConfig, CompactConfig,
CompactConfig, CurpConfig, CurpConfig, EngineConfig, InitialClusterState, ServerTimeout,
ServerTimeout, StorageConfig, StorageConfig, TlsConfig,
AuthConfig, ClientConfig, ClusterConfig, CompactConfig, CurpConfig, InitialClusterState,
ServerTimeout, StorageConfig, TlsConfig,
};
use xline::server::XlineServer;
use xline_client::{
Expand Down Expand Up @@ -123,18 +122,18 @@ impl XlineGroup {
self.nodes.get(name).unwrap()
}

pub async fn crash(&mut self, id: ServerId) {
pub async fn crash(&mut self, name: &str) {
let handle = madsim::runtime::Handle::current();
handle.kill(id.to_string());
madsim::time::sleep(Duration::from_secs(2)).await;
if !handle.is_exit(id.to_string()) {
panic!("failed to crash node: {id}");
handle.kill(name);
madsim::time::sleep(Duration::from_secs(10)).await;
if !handle.is_exit(name) {
panic!("failed to crash node: {name}");
}
}

pub async fn restart(&mut self, id: ServerId) {
pub async fn restart(&mut self, name: &str) {
let handle = madsim::runtime::Handle::current();
handle.restart(id.to_string());
handle.restart(name);
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/simulation/tests/it/xline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ async fn watch_compacted_revision_should_receive_canceled_response() {
async fn xline_members_restore() {
init_logger();
let mut group = XlineGroup::new(3).await;
let node = group.get_node_by_name("S1");
let node_id = node.id;
let node = group.get_node("S1");
let addr = node.addr.clone();
let client = SimEtcdClient::new(addr, group.client_handle.clone()).await;

Expand All @@ -70,10 +69,10 @@ async fn xline_members_restore() {
.await
.unwrap();
assert_eq!(members.members.len(), 4);
group.crash(node_id).await;
group.crash("S1").await;
sleep(Duration::from_secs(10)).await;

group.restart(node_id).await;
group.restart("S1").await;
sleep(Duration::from_secs(10)).await;
let members = client
.member_list(MemberListRequest::new(false))
Expand Down

0 comments on commit 1f17be2

Please sign in to comment.