Skip to content

Commit 72b2f40

Browse files
authored
Merge pull request #3368 from drmingdrmer/master
[metasrv] refactor: simplify metasrv cluster starting up in tests.
2 parents 0d4a6e4 + ed31ea2 commit 72b2f40

File tree

10 files changed

+117
-105
lines changed

10 files changed

+117
-105
lines changed

common/meta/api/src/meta_api_test_suite.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ impl MetaApiTestSuite {
454454
// leader-follower tests
455455
// This is meant for testing distributed MetaApi impl, to ensure a read-after-write consistency.
456456
impl MetaApiTestSuite {
457-
pub async fn database_create_get_drop_leader_follower<MT: MetaApi>(
457+
pub async fn database_get_leader_follower<MT: MetaApi>(
458458
&self,
459459
leader: &MT,
460460
follower: &MT,
@@ -495,8 +495,6 @@ impl MetaApiTestSuite {
495495
assert_eq!("Code: 3, displayText = nonexistent.", format!("{}", err));
496496
}
497497

498-
// TODO(xp): test drop is replicated to follower
499-
500498
Ok(())
501499
}
502500

metasrv/tests/it/api/http/cluster_state_test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use poem::Route;
3636
use pretty_assertions::assert_eq;
3737

3838
use crate::init_meta_ut;
39-
use crate::tests::service::new_metasrv_test_context;
39+
use crate::tests::service::MetaSrvTestContext;
4040
use crate::tests::tls_constants::TEST_CA_CERT;
4141
use crate::tests::tls_constants::TEST_CN_NAME;
4242
use crate::tests::tls_constants::TEST_SERVER_CERT;
@@ -47,8 +47,8 @@ async fn test_cluster_nodes() -> common_exception::Result<()> {
4747
let (_log_guards, ut_span) = init_meta_ut!();
4848
let _ent = ut_span.enter();
4949

50-
let tc0 = new_metasrv_test_context(0);
51-
let mut tc1 = new_metasrv_test_context(1);
50+
let tc0 = MetaSrvTestContext::new(0);
51+
let mut tc1 = MetaSrvTestContext::new(1);
5252

5353
tc1.config.raft_config.single = false;
5454
tc1.config.raft_config.join = vec![tc0.config.raft_config.raft_api_addr()];
@@ -82,8 +82,8 @@ async fn test_cluster_state() -> common_exception::Result<()> {
8282
let (_log_guards, ut_span) = init_meta_ut!();
8383
let _ent = ut_span.enter();
8484

85-
let tc0 = new_metasrv_test_context(0);
86-
let mut tc1 = new_metasrv_test_context(1);
85+
let tc0 = MetaSrvTestContext::new(0);
86+
let mut tc1 = MetaSrvTestContext::new(1);
8787

8888
tc1.config.raft_config.single = false;
8989
tc1.config.raft_config.join = vec![tc0.config.raft_config.raft_api_addr()];
@@ -124,8 +124,8 @@ async fn test_http_service_cluster_state() -> common_exception::Result<()> {
124124
let (_log_guards, ut_span) = init_meta_ut!();
125125
let _ent = ut_span.enter();
126126

127-
let tc0 = new_metasrv_test_context(0);
128-
let mut tc1 = new_metasrv_test_context(1);
127+
let tc0 = MetaSrvTestContext::new(0);
128+
let mut tc1 = MetaSrvTestContext::new(1);
129129

130130
tc1.config.raft_config.single = false;
131131
tc1.config.raft_config.join = vec![tc0.config.raft_config.raft_api_addr()];

metasrv/tests/it/api/http_service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use databend_meta::configs::Config;
2323
use databend_meta::meta_service::MetaNode;
2424

2525
use crate::init_meta_ut;
26-
use crate::tests::service::new_metasrv_test_context;
26+
use crate::tests::service::MetaSrvTestContext;
2727
use crate::tests::tls_constants::TEST_CA_CERT;
2828
use crate::tests::tls_constants::TEST_CN_NAME;
2929
use crate::tests::tls_constants::TEST_SERVER_CERT;
@@ -41,7 +41,7 @@ async fn test_http_service_tls_server() -> Result<()> {
4141
conf.admin_tls_server_key = TEST_SERVER_KEY.to_owned();
4242
conf.admin_tls_server_cert = TEST_SERVER_CERT.to_owned();
4343
conf.admin_api_address = addr_str.to_owned();
44-
let tc = new_metasrv_test_context(0);
44+
let tc = MetaSrvTestContext::new(0);
4545
let meta_node = MetaNode::start(&tc.config.raft_config).await?;
4646

4747
let mut srv = HttpService::create(conf, meta_node);

metasrv/tests/it/flight/metasrv_flight_api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use pretty_assertions::assert_eq;
2828
use tokio::time::Duration;
2929

3030
use crate::init_meta_ut;
31-
use crate::tests::service::new_metasrv_test_context;
31+
use crate::tests::service::MetaSrvTestContext;
3232
use crate::tests::start_metasrv_with_context;
3333

3434
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
@@ -136,8 +136,8 @@ async fn test_join() -> anyhow::Result<()> {
136136
let (_log_guards, ut_span) = init_meta_ut!();
137137
let _ent = ut_span.enter();
138138

139-
let mut tc0 = new_metasrv_test_context(0);
140-
let mut tc1 = new_metasrv_test_context(1);
139+
let mut tc0 = MetaSrvTestContext::new(0);
140+
let mut tc1 = MetaSrvTestContext::new(1);
141141

142142
tc1.config.raft_config.single = false;
143143
tc1.config.raft_config.join = vec![tc0.config.raft_config.raft_api_addr()];

metasrv/tests/it/flight/metasrv_flight_meta_api_leader_follower.rs

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,22 @@
1414

1515
use common_base::tokio;
1616
use common_meta_api::MetaApiTestSuite;
17-
use common_meta_flight::MetaFlightClient;
1817

1918
use crate::init_meta_ut;
20-
use crate::tests::service::new_metasrv_test_context;
21-
use crate::tests::start_metasrv_with_context;
19+
use crate::tests::service::start_metasrv_cluster;
2220

2321
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
2422
async fn test_meta_api_database_create_get_drop() -> anyhow::Result<()> {
2523
let (_log_guards, ut_span) = init_meta_ut!();
2624
let _ent = ut_span.enter();
2725

28-
let mut tc0 = new_metasrv_test_context(0);
29-
let mut tc1 = new_metasrv_test_context(1);
26+
let tcs = start_metasrv_cluster(&[0, 1]).await?;
3027

31-
tc1.config.raft_config.single = false;
32-
tc1.config.raft_config.join = vec![tc0.config.raft_config.raft_api_addr()];
33-
34-
start_metasrv_with_context(&mut tc0).await?;
35-
start_metasrv_with_context(&mut tc1).await?;
36-
37-
let addr0 = tc0.config.flight_api_address.clone();
38-
let addr1 = tc1.config.flight_api_address.clone();
39-
40-
let client0 = MetaFlightClient::try_create(addr0.as_str(), "root", "xxx").await?;
41-
let client1 = MetaFlightClient::try_create(addr1.as_str(), "root", "xxx").await?;
28+
let client0 = tcs[0].flight_client().await?;
29+
let client1 = tcs[1].flight_client().await?;
4230

4331
MetaApiTestSuite {}
44-
.database_create_get_drop_leader_follower(&client0, &client1)
32+
.database_get_leader_follower(&client0, &client1)
4533
.await
4634
}
4735

@@ -50,20 +38,10 @@ async fn test_meta_api_list_table() -> anyhow::Result<()> {
5038
let (_log_guards, ut_span) = init_meta_ut!();
5139
let _ent = ut_span.enter();
5240

53-
let mut tc0 = new_metasrv_test_context(0);
54-
let mut tc1 = new_metasrv_test_context(1);
55-
56-
tc1.config.raft_config.single = false;
57-
tc1.config.raft_config.join = vec![tc0.config.raft_config.raft_api_addr()];
58-
59-
start_metasrv_with_context(&mut tc0).await?;
60-
start_metasrv_with_context(&mut tc1).await?;
61-
62-
let addr0 = tc0.config.flight_api_address.clone();
63-
let addr1 = tc1.config.flight_api_address.clone();
41+
let tcs = start_metasrv_cluster(&[0, 1]).await?;
6442

65-
let client0 = MetaFlightClient::try_create(addr0.as_str(), "root", "xxx").await?;
66-
let client1 = MetaFlightClient::try_create(addr1.as_str(), "root", "xxx").await?;
43+
let client0 = tcs[0].flight_client().await?;
44+
let client1 = tcs[1].flight_client().await?;
6745

6846
MetaApiTestSuite {}
6947
.list_table_leader_follower(&client0, &client1)

metasrv/tests/it/flight/metasrv_flight_tls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use common_meta_flight::MetaFlightClient;
2020
use pretty_assertions::assert_eq;
2121

2222
use crate::init_meta_ut;
23-
use crate::tests::service::new_metasrv_test_context;
23+
use crate::tests::service::MetaSrvTestContext;
2424
use crate::tests::start_metasrv_with_context;
2525
use crate::tests::tls_constants::TEST_CA_CERT;
2626
use crate::tests::tls_constants::TEST_CN_NAME;
@@ -32,7 +32,7 @@ async fn test_tls_server() -> anyhow::Result<()> {
3232
let (_log_guards, ut_span) = init_meta_ut!();
3333
let _ent = ut_span.enter();
3434

35-
let mut tc = new_metasrv_test_context(0);
35+
let mut tc = MetaSrvTestContext::new(0);
3636

3737
tc.config.flight_tls_server_key = TEST_SERVER_KEY.to_owned();
3838
tc.config.flight_tls_server_cert = TEST_SERVER_CERT.to_owned();
@@ -63,7 +63,7 @@ async fn test_tls_server_config_failure() -> anyhow::Result<()> {
6363
let (_log_guards, ut_span) = init_meta_ut!();
6464
let _ent = ut_span.enter();
6565

66-
let mut tc = new_metasrv_test_context(0);
66+
let mut tc = MetaSrvTestContext::new(0);
6767

6868
tc.config.flight_tls_server_key = "../tests/data/certs/not_exist.key".to_owned();
6969
tc.config.flight_tls_server_cert = "../tests/data/certs/not_exist.pem".to_owned();

metasrv/tests/it/meta_service/meta_node.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use pretty_assertions::assert_eq;
4343

4444
use crate::init_meta_ut;
4545
use crate::tests::assert_metasrv_connection;
46-
use crate::tests::service::new_metasrv_test_context;
4746
use crate::tests::service::MetaSrvTestContext;
4847

4948
#[tokio::test(flavor = "multi_thread", worker_threads = 5)]
@@ -54,7 +53,7 @@ async fn test_meta_node_boot() -> anyhow::Result<()> {
5453
let (_log_guards, ut_span) = init_meta_ut!();
5554
let _ent = ut_span.enter();
5655

57-
let tc = new_metasrv_test_context(0);
56+
let tc = MetaSrvTestContext::new(0);
5857
let addr = tc.config.raft_config.raft_api_addr();
5958

6059
let mn = MetaNode::boot(&tc.config.raft_config).await?;
@@ -279,7 +278,7 @@ async fn test_meta_node_snapshot_replication() -> anyhow::Result<()> {
279278
// Create a snapshot every 10 logs
280279
let snap_logs = 10;
281280

282-
let mut tc = new_metasrv_test_context(0);
281+
let mut tc = MetaSrvTestContext::new(0);
283282
tc.config.raft_config.snapshot_logs_since_last = snap_logs;
284283
tc.config.raft_config.install_snapshot_timeout = 10_1000; // milli seconds. In a CI multi-threads test delays async task badly.
285284
let addr = tc.config.raft_config.raft_api_addr();
@@ -408,7 +407,7 @@ async fn test_meta_node_join() -> anyhow::Result<()> {
408407
tracing::info!("--- bring up non-voter 2");
409408

410409
let node_id = 2;
411-
let tc2 = new_metasrv_test_context(node_id);
410+
let tc2 = MetaSrvTestContext::new(node_id);
412411
let mn2 = MetaNode::open_create_boot(&tc2.config.raft_config, None, Some(()), None).await?;
413412

414413
tracing::info!("--- join non-voter 2 to cluster by leader");
@@ -434,7 +433,7 @@ async fn test_meta_node_join() -> anyhow::Result<()> {
434433
tracing::info!("--- bring up non-voter 3");
435434

436435
let node_id = 3;
437-
let tc3 = new_metasrv_test_context(node_id);
436+
let tc3 = MetaSrvTestContext::new(node_id);
438437
let mn3 = MetaNode::open_create_boot(&tc3.config.raft_config, None, Some(()), None).await?;
439438

440439
tracing::info!("--- join node-3 by sending rpc `join` to a non-leader");
@@ -505,7 +504,7 @@ async fn test_meta_node_join_rejoin() -> anyhow::Result<()> {
505504
tracing::info!("--- bring up non-voter 1");
506505

507506
let node_id = 1;
508-
let tc1 = new_metasrv_test_context(node_id);
507+
let tc1 = MetaSrvTestContext::new(node_id);
509508
let mn1 = MetaNode::open_create_boot(&tc1.config.raft_config, None, Some(()), None).await?;
510509

511510
tracing::info!("--- join non-voter 1 to cluster");
@@ -530,7 +529,7 @@ async fn test_meta_node_join_rejoin() -> anyhow::Result<()> {
530529
tracing::info!("--- bring up non-voter 3");
531530

532531
let node_id = 2;
533-
let tc2 = new_metasrv_test_context(node_id);
532+
let tc2 = MetaSrvTestContext::new(node_id);
534533
let mn2 = MetaNode::open_create_boot(&tc2.config.raft_config, None, Some(()), None).await?;
535534

536535
tracing::info!("--- join node-2 by sending rpc `join` to a non-leader");
@@ -779,7 +778,7 @@ async fn start_meta_node_leader() -> anyhow::Result<(NodeId, MetaSrvTestContext)
779778
// asserts states are consistent
780779

781780
let nid = 0;
782-
let mut tc = new_metasrv_test_context(nid);
781+
let mut tc = MetaSrvTestContext::new(nid);
783782
let addr = tc.config.raft_config.raft_api_addr();
784783

785784
// boot up a single-node cluster
@@ -805,7 +804,7 @@ async fn start_meta_node_non_voter(
805804
leader: Arc<MetaNode>,
806805
id: NodeId,
807806
) -> anyhow::Result<(NodeId, MetaSrvTestContext)> {
808-
let mut tc = new_metasrv_test_context(id);
807+
let mut tc = MetaSrvTestContext::new(id);
809808
let addr = tc.config.raft_config.raft_api_addr();
810809

811810
let raft_config = tc.config.raft_config.clone();

metasrv/tests/it/meta_service/meta_service_impl.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ use pretty_assertions::assert_eq;
3030

3131
use crate::init_meta_ut;
3232
use crate::tests::assert_metasrv_connection;
33-
use crate::tests::service::new_metasrv_test_context;
33+
use crate::tests::service::MetaSrvTestContext;
3434

3535
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
3636
async fn test_metasrv_upsert_kv() -> anyhow::Result<()> {
3737
let (_log_guards, ut_span) = init_meta_ut!();
3838
let _ent = ut_span.enter();
3939

40-
let tc = new_metasrv_test_context(0);
40+
let tc = MetaSrvTestContext::new(0);
4141
let addr = tc.config.raft_config.raft_api_addr();
4242

4343
let _mn = MetaNode::boot(&tc.config.raft_config).await?;
@@ -82,7 +82,7 @@ async fn test_metasrv_incr_seq() -> anyhow::Result<()> {
8282
let (_log_guards, ut_span) = init_meta_ut!();
8383
let _ent = ut_span.enter();
8484

85-
let tc = new_metasrv_test_context(0);
85+
let tc = MetaSrvTestContext::new(0);
8686
let addr = tc.config.raft_config.raft_api_addr();
8787

8888
let _mn = MetaNode::boot(&tc.config.raft_config).await?;
@@ -122,8 +122,8 @@ async fn test_metasrv_cluster_write_on_non_leader() -> anyhow::Result<()> {
122122
let (_log_guards, ut_span) = init_meta_ut!();
123123
let _ent = ut_span.enter();
124124

125-
let tc0 = new_metasrv_test_context(0);
126-
let tc1 = new_metasrv_test_context(1);
125+
let tc0 = MetaSrvTestContext::new(0);
126+
let tc1 = MetaSrvTestContext::new(1);
127127

128128
let addr0 = tc0.config.raft_config.raft_api_addr();
129129
let addr1 = tc1.config.raft_config.raft_api_addr();

metasrv/tests/it/store.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use databend_meta::Opened;
3232
use maplit::btreeset;
3333

3434
use crate::init_meta_ut;
35-
use crate::tests::service::new_metasrv_test_context;
35+
use crate::tests::service::MetaSrvTestContext;
3636

3737
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
3838
async fn test_metasrv_restart() -> anyhow::Result<()> {
@@ -48,7 +48,7 @@ async fn test_metasrv_restart() -> anyhow::Result<()> {
4848
let _ent = ut_span.enter();
4949

5050
let id = 3;
51-
let tc = new_metasrv_test_context(id);
51+
let tc = MetaSrvTestContext::new(id);
5252

5353
tracing::info!("--- new metasrv");
5454
{
@@ -93,7 +93,7 @@ async fn test_metasrv_get_membership_from_log() -> anyhow::Result<()> {
9393
let _ent = ut_span.enter();
9494

9595
let id = 3;
96-
let tc = new_metasrv_test_context(id);
96+
let tc = MetaSrvTestContext::new(id);
9797

9898
tracing::info!("--- new metasrv");
9999
let ms = MetaRaftStore::open_create(&tc.config.raft_config, None, Some(())).await?;
@@ -193,7 +193,7 @@ async fn test_metasrv_do_log_compaction_empty() -> anyhow::Result<()> {
193193
let _ent = ut_span.enter();
194194

195195
let id = 3;
196-
let tc = new_metasrv_test_context(id);
196+
let tc = MetaSrvTestContext::new(id);
197197

198198
let ms = MetaRaftStore::open_create(&tc.config.raft_config, None, Some(())).await?;
199199

@@ -241,7 +241,7 @@ async fn test_metasrv_do_log_compaction_1_snap_ptr_1_log() -> anyhow::Result<()>
241241
let _ent = ut_span.enter();
242242

243243
let id = 3;
244-
let tc = new_metasrv_test_context(id);
244+
let tc = MetaSrvTestContext::new(id);
245245

246246
let ms = MetaRaftStore::open_create(&tc.config.raft_config, None, Some(())).await?;
247247

@@ -315,7 +315,7 @@ async fn test_metasrv_do_log_compaction_all_logs_with_memberchange() -> anyhow::
315315
let _ent = ut_span.enter();
316316

317317
let id = 3;
318-
let tc = new_metasrv_test_context(id);
318+
let tc = MetaSrvTestContext::new(id);
319319

320320
let ms = MetaRaftStore::open_create(&tc.config.raft_config, None, Some(())).await?;
321321

@@ -368,7 +368,7 @@ async fn test_metasrv_do_log_compaction_current_snapshot() -> anyhow::Result<()>
368368
let _ent = ut_span.enter();
369369

370370
let id = 3;
371-
let tc = new_metasrv_test_context(id);
371+
let tc = MetaSrvTestContext::new(id);
372372

373373
let ms = MetaRaftStore::open_create(&tc.config.raft_config, None, Some(())).await?;
374374

@@ -424,7 +424,7 @@ async fn test_metasrv_install_snapshot() -> anyhow::Result<()> {
424424
let id = 3;
425425
let snap;
426426
{
427-
let tc = new_metasrv_test_context(id);
427+
let tc = MetaSrvTestContext::new(id);
428428

429429
let ms = MetaRaftStore::open_create(&tc.config.raft_config, None, Some(())).await?;
430430

@@ -441,7 +441,7 @@ async fn test_metasrv_install_snapshot() -> anyhow::Result<()> {
441441

442442
tracing::info!("--- reopen a new metasrv to install snapshot");
443443
{
444-
let tc = new_metasrv_test_context(id);
444+
let tc = MetaSrvTestContext::new(id);
445445

446446
let ms = MetaRaftStore::open_create(&tc.config.raft_config, None, Some(())).await?;
447447

0 commit comments

Comments
 (0)