Skip to content

Commit dbb84cf

Browse files
committed
chore(meta): refactor meta-node create/open/initialize
1 parent 2be2c8a commit dbb84cf

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

src/meta/raft-store/src/state_machine/sm_kv_api_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl KVApi for StateMachine {
108108
let local_now_ms = SeqV::<()>::now_ms();
109109

110110
// Convert expired to None
111-
let x = x.map(|(k, v)| (k, Self::expire_seq_v(Some(v), local_now_ms)[1]));
111+
let x = x.map(|(k, v)| (k, Self::expire_seq_v(Some(v), local_now_ms).1));
112112
// Remove None
113113
let x = x.filter(|(_k, v)| v.is_some());
114114
// Extract from an Option

src/meta/service/src/meta_service/raftmeta.rs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -329,20 +329,18 @@ impl MetaNode {
329329
Ok(())
330330
}
331331

332-
/// Open or create a metasrv node.
333-
/// Optionally boot a single node cluster.
332+
/// Open or create a meta node.
334333
/// 1. If `open` is `Some`, try to open an existent one.
335334
/// 2. If `create` is `Some`, try to create an one in non-voter mode.
336335
#[tracing::instrument(level = "debug", skip_all)]
337-
pub async fn open_create_boot(
336+
pub async fn open_create(
338337
config: &RaftConfig,
339338
open: Option<()>,
340339
create: Option<()>,
341-
initialize_cluster: Option<Node>,
342340
) -> Result<Arc<MetaNode>, MetaStartupError> {
343341
info!(
344-
"open_create_boot, config: {:?}, open: {:?}, create: {:?}, initialize_cluster: {:?}",
345-
config, open, create, initialize_cluster
342+
"open_create_boot, config: {:?}, open: {:?}, create: {:?}",
343+
config, open, create
346344
);
347345

348346
let mut config = config.clone();
@@ -371,10 +369,21 @@ impl MetaNode {
371369

372370
info!("MetaNode started: {:?}", config);
373371

374-
// init_cluster with advertise_host other than listen_host
375-
if mn.is_opened() {
376-
return Ok(mn);
377-
}
372+
Ok(mn)
373+
}
374+
375+
/// Open or create a metasrv node.
376+
/// Optionally boot a single node cluster.
377+
/// 1. If `open` is `Some`, try to open an existent one.
378+
/// 2. If `create` is `Some`, try to create an one in non-voter mode.
379+
#[tracing::instrument(level = "debug", skip_all)]
380+
pub async fn open_create_boot(
381+
config: &RaftConfig,
382+
open: Option<()>,
383+
create: Option<()>,
384+
initialize_cluster: Option<Node>,
385+
) -> Result<Arc<MetaNode>, MetaStartupError> {
386+
let mn = Self::open_create(config, open, create).await?;
378387

379388
if let Some(node) = initialize_cluster {
380389
mn.init_cluster(node).await?;
@@ -718,47 +727,43 @@ impl MetaNode {
718727
async fn do_start(conf: &MetaConfig) -> Result<Arc<MetaNode>, MetaStartupError> {
719728
let raft_conf = &conf.raft_config;
720729

721-
let initialize_cluster = if raft_conf.single {
722-
Some(conf.get_node())
723-
} else {
724-
None
725-
};
726-
727730
if raft_conf.single {
728-
let mn = MetaNode::open_create_boot(raft_conf, Some(()), Some(()), initialize_cluster)
729-
.await?;
731+
let mn = MetaNode::open_create(raft_conf, Some(()), Some(())).await?;
732+
mn.init_cluster(conf.get_node()).await?;
730733
return Ok(mn);
731734
}
732735

733736
if !raft_conf.join.is_empty() {
734737
// Bring up a new node, join it into a cluster
735738

736-
let mn = MetaNode::open_create_boot(raft_conf, Some(()), Some(()), initialize_cluster)
737-
.await?;
739+
let mn = MetaNode::open_create(raft_conf, Some(()), Some(())).await?;
738740
return Ok(mn);
739741
}
740742
// open mode
741743

742-
let mn = MetaNode::open_create_boot(raft_conf, Some(()), None, initialize_cluster).await?;
744+
let mn = MetaNode::open_create(raft_conf, Some(()), None).await?;
743745
Ok(mn)
744746
}
745747

746748
/// Boot up the first node to create a cluster.
747749
/// For every cluster this func should be called exactly once.
748750
#[tracing::instrument(level = "debug", skip(config), fields(config_id=config.raft_config.config_id.as_str()))]
749751
pub async fn boot(config: &MetaConfig) -> Result<Arc<MetaNode>, MetaStartupError> {
750-
let mn =
751-
Self::open_create_boot(&config.raft_config, None, Some(()), Some(config.get_node()))
752-
.await?;
753-
752+
let mn = Self::open_create(&config.raft_config, None, Some(())).await?;
753+
mn.init_cluster(config.get_node()).await?;
754754
Ok(mn)
755755
}
756756

757-
// Initialized a single node cluster by:
758-
// - Initializing raft membership.
759-
// - Adding current node into the meta data.
757+
/// Initialized a single node cluster if this node is just created:
758+
/// - Initializing raft membership.
759+
/// - Adding current node into the meta data.
760760
#[tracing::instrument(level = "debug", skip(self))]
761761
pub async fn init_cluster(&self, node: Node) -> Result<(), MetaStartupError> {
762+
if self.is_opened() {
763+
info!("It is opened, skip initializing cluster");
764+
return Ok(());
765+
}
766+
762767
let node_id = self.sto.id;
763768

764769
let mut cluster_node_ids = BTreeSet::new();

src/meta/service/tests/it/grpc/metasrv_grpc_kv_api_restart_cluster.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,9 @@ async fn test_kv_api_restart_cluster_token_expired() -> anyhow::Result<()> {
238238

239239
Ok(())
240240
}
241+
241242
// Election timeout is 8~12 sec.
242243
// A raft node waits for a interval of election timeout before starting election
243-
// TODO: the raft should set the wait time by election_timeout.
244-
// For now, just use a large timeout.
245244
fn timeout() -> Option<Duration> {
246245
Some(Duration::from_millis(30_000))
247246
}

src/meta/service/tests/it/meta_node/meta_node_lifecycle.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ async fn test_meta_node_join() -> anyhow::Result<()> {
103103
let node_id = 2;
104104
let tc2 = MetaSrvTestContext::new(node_id);
105105

106-
let mn2 = MetaNode::open_create_boot(&tc2.config.raft_config, None, Some(()), None).await?;
106+
let mn2 = MetaNode::open_create(&tc2.config.raft_config, None, Some(())).await?;
107107

108108
info!("--- join non-voter 2 to cluster by leader");
109109

@@ -134,7 +134,7 @@ async fn test_meta_node_join() -> anyhow::Result<()> {
134134

135135
let node_id = 3;
136136
let tc3 = MetaSrvTestContext::new(node_id);
137-
let mn3 = MetaNode::open_create_boot(&tc3.config.raft_config, None, Some(()), None).await?;
137+
let mn3 = MetaNode::open_create(&tc3.config.raft_config, None, Some(())).await?;
138138

139139
info!("--- join node-3 by sending rpc `join` to a non-leader");
140140
{
@@ -171,10 +171,10 @@ async fn test_meta_node_join() -> anyhow::Result<()> {
171171

172172
info!("--- re-open all meta node");
173173

174-
let mn0 = MetaNode::open_create_boot(&tc0.config.raft_config, Some(()), None, None).await?;
175-
let mn1 = MetaNode::open_create_boot(&tc1.config.raft_config, Some(()), None, None).await?;
176-
let mn2 = MetaNode::open_create_boot(&tc2.config.raft_config, Some(()), None, None).await?;
177-
let mn3 = MetaNode::open_create_boot(&tc3.config.raft_config, Some(()), None, None).await?;
174+
let mn0 = MetaNode::open_create(&tc0.config.raft_config, Some(()), None).await?;
175+
let mn1 = MetaNode::open_create(&tc1.config.raft_config, Some(()), None).await?;
176+
let mn2 = MetaNode::open_create(&tc2.config.raft_config, Some(()), None).await?;
177+
let mn3 = MetaNode::open_create(&tc3.config.raft_config, Some(()), None).await?;
178178

179179
let all = vec![mn0, mn1, mn2, mn3];
180180

@@ -282,8 +282,8 @@ async fn test_meta_node_leave() -> anyhow::Result<()> {
282282
let tc0 = &tcs[0];
283283
let tc2 = &tcs[2];
284284

285-
let mn0 = MetaNode::open_create_boot(&tc0.config.raft_config, Some(()), None, None).await?;
286-
let mn2 = MetaNode::open_create_boot(&tc2.config.raft_config, Some(()), None, None).await?;
285+
let mn0 = MetaNode::open_create(&tc0.config.raft_config, Some(()), None).await?;
286+
let mn2 = MetaNode::open_create(&tc2.config.raft_config, Some(()), None).await?;
287287

288288
let all = vec![mn0, mn2];
289289

@@ -314,7 +314,7 @@ async fn test_meta_node_join_rejoin() -> anyhow::Result<()> {
314314
let node_id = 1;
315315
let tc1 = MetaSrvTestContext::new(node_id);
316316

317-
let mn1 = MetaNode::open_create_boot(&tc1.config.raft_config, None, Some(()), None).await?;
317+
let mn1 = MetaNode::open_create(&tc1.config.raft_config, None, Some(())).await?;
318318

319319
info!("--- join non-voter 1 to cluster");
320320

@@ -345,7 +345,7 @@ async fn test_meta_node_join_rejoin() -> anyhow::Result<()> {
345345
let node_id = 2;
346346
let tc2 = MetaSrvTestContext::new(node_id);
347347

348-
let mn2 = MetaNode::open_create_boot(&tc2.config.raft_config, None, Some(()), None).await?;
348+
let mn2 = MetaNode::open_create(&tc2.config.raft_config, None, Some(())).await?;
349349

350350
info!("--- join node-2 by sending rpc `join` to a non-leader");
351351
{
@@ -585,7 +585,7 @@ async fn test_meta_node_restart_single_node() -> anyhow::Result<()> {
585585

586586
let raft_conf = &tc.config.raft_config;
587587

588-
let leader = MetaNode::open_create_boot(raft_conf, Some(()), None, None).await?;
588+
let leader = MetaNode::open_create(raft_conf, Some(()), None).await?;
589589

590590
log_index += 1;
591591

src/meta/service/tests/it/tests/meta_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ pub(crate) async fn start_meta_node_non_voter(
190190

191191
let raft_conf = &tc.config.raft_config;
192192

193-
let mn = MetaNode::open_create_boot(raft_conf, None, Some(()), None).await?;
193+
let mn = MetaNode::open_create(raft_conf, None, Some(())).await?;
194194

195195
assert!(!mn.is_opened());
196196

0 commit comments

Comments
 (0)