Skip to content

Commit 0451a9c

Browse files
authored
chore: add meta stable check into integration test (#1202)
## Rationale Currently, we just left 20s for ceresmeta to do the initialization work in integration test. However, it will be not enough something... And the test failure will make us so panic... In this pr, I add a meta stable check to reduce the integration test failure. ## Detailed Changes Add a meta stable check by keep creating a radom named table utill succeeding(surely, retry max exists). ## Test Plan Test manually.
1 parent 395debb commit 0451a9c

File tree

4 files changed

+76
-10
lines changed

4 files changed

+76
-10
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration_tests/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ reqwest = { workspace = true }
2828
serde = { workspace = true }
2929
sqlness = "0.4.3"
3030
tokio = { workspace = true }
31+
uuid = { version = "1.3", features = ["v4"] }

integration_tests/src/database.rs

+72-8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const CERESDB_CONFIG_FILE_0_ENV: &str = "CERESDB_CONFIG_FILE_0";
4444
const CERESDB_CONFIG_FILE_1_ENV: &str = "CERESDB_CONFIG_FILE_1";
4545
const CLUSTER_CERESDB_STDOUT_FILE_0_ENV: &str = "CLUSTER_CERESDB_STDOUT_FILE_0";
4646
const CLUSTER_CERESDB_STDOUT_FILE_1_ENV: &str = "CLUSTER_CERESDB_STDOUT_FILE_1";
47+
const CLUSTER_CERESDB_HEALTH_CHECK_INTERVAL_SECONDS: usize = 5;
4748

4849
const CERESDB_SERVER_ADDR: &str = "CERESDB_SERVER_ADDR";
4950

@@ -63,9 +64,10 @@ impl HttpClient {
6364
}
6465
}
6566

67+
#[async_trait]
6668
pub trait Backend {
6769
fn start() -> Self;
68-
fn wait_for_ready(&self);
70+
async fn wait_for_ready(&self);
6971
fn stop(&mut self);
7072
}
7173

@@ -77,6 +79,10 @@ pub struct CeresDBCluster {
7779
server0: CeresDBServer,
7880
server1: CeresDBServer,
7981
ceresmeta_process: Child,
82+
83+
/// Used in meta health check
84+
db_client: Arc<dyn DbClient>,
85+
meta_stable_check_sql: String,
8086
}
8187

8288
impl CeresDBServer {
@@ -97,6 +103,7 @@ impl CeresDBServer {
97103
}
98104
}
99105

106+
#[async_trait]
100107
impl Backend for CeresDBServer {
101108
fn start() -> Self {
102109
let config = env::var(CERESDB_CONFIG_FILE_ENV).expect("Cannot parse ceresdb config env");
@@ -105,15 +112,33 @@ impl Backend for CeresDBServer {
105112
Self::spawn(bin, config, stdout)
106113
}
107114

108-
fn wait_for_ready(&self) {
109-
std::thread::sleep(Duration::from_secs(5));
115+
async fn wait_for_ready(&self) {
116+
tokio::time::sleep(Duration::from_secs(10)).await
110117
}
111118

112119
fn stop(&mut self) {
113120
self.server_process.kill().expect("Failed to kill server");
114121
}
115122
}
116123

124+
impl CeresDBCluster {
125+
async fn check_meta_stable(&self) -> bool {
126+
let query_ctx = RpcContext {
127+
database: Some("public".to_string()),
128+
timeout: None,
129+
};
130+
131+
let query_req = Request {
132+
tables: vec![],
133+
sql: self.meta_stable_check_sql.clone(),
134+
};
135+
136+
let result = self.db_client.sql_query(&query_ctx, &query_req).await;
137+
result.is_ok()
138+
}
139+
}
140+
141+
#[async_trait]
117142
impl Backend for CeresDBCluster {
118143
fn start() -> Self {
119144
let ceresmeta_bin =
@@ -149,16 +174,55 @@ impl Backend for CeresDBCluster {
149174
let server0 = CeresDBServer::spawn(ceresdb_bin.clone(), ceresdb_config_0, stdout0);
150175
let server1 = CeresDBServer::spawn(ceresdb_bin, ceresdb_config_1, stdout1);
151176

177+
// Meta stable check context
178+
let endpoint = env::var(SERVER_GRPC_ENDPOINT_ENV).unwrap_or_else(|_| {
179+
panic!("Cannot read server endpoint from env {SERVER_GRPC_ENDPOINT_ENV:?}")
180+
});
181+
let db_client = Builder::new(endpoint, Mode::Proxy).build();
182+
183+
let meta_stable_check_sql = format!(
184+
r#"CREATE TABLE `stable_check_{}`
185+
(`name` string TAG, `value` double NOT NULL, `t` timestamp NOT NULL, TIMESTAMP KEY(t))"#,
186+
uuid::Uuid::new_v4()
187+
);
188+
152189
Self {
153190
server0,
154191
server1,
155192
ceresmeta_process,
193+
db_client,
194+
meta_stable_check_sql,
156195
}
157196
}
158197

159-
fn wait_for_ready(&self) {
160-
println!("wait for cluster service ready...\n");
161-
std::thread::sleep(Duration::from_secs(20));
198+
async fn wait_for_ready(&self) {
199+
println!("wait for cluster service initialized...");
200+
tokio::time::sleep(Duration::from_secs(20_u64)).await;
201+
202+
println!("wait for cluster service stable begin...");
203+
let mut wait_cnt = 0;
204+
let wait_max = 6;
205+
loop {
206+
if wait_cnt >= wait_max {
207+
println!(
208+
"wait too long for cluster service stable, maybe somethings went wrong..."
209+
);
210+
return;
211+
}
212+
213+
if self.check_meta_stable().await {
214+
println!("wait for cluster service stable finished...");
215+
return;
216+
}
217+
218+
wait_cnt += 1;
219+
let has_waited = wait_cnt * CLUSTER_CERESDB_HEALTH_CHECK_INTERVAL_SECONDS;
220+
println!("waiting for cluster service stable, has_waited:{has_waited}s");
221+
tokio::time::sleep(Duration::from_secs(
222+
CLUSTER_CERESDB_HEALTH_CHECK_INTERVAL_SECONDS as u64,
223+
))
224+
.await;
225+
}
162226
}
163227

164228
fn stop(&mut self) {
@@ -226,9 +290,9 @@ impl<T: Send + Sync> Database for CeresDB<T> {
226290
}
227291

228292
impl<T: Backend> CeresDB<T> {
229-
pub fn create() -> CeresDB<T> {
293+
pub async fn create() -> CeresDB<T> {
230294
let backend = T::start();
231-
backend.wait_for_ready();
295+
backend.wait_for_ready().await;
232296

233297
let endpoint = env::var(SERVER_GRPC_ENDPOINT_ENV).unwrap_or_else(|_| {
234298
panic!("Cannot read server endpoint from env {SERVER_GRPC_ENDPOINT_ENV:?}")

integration_tests/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ impl EnvController for CeresDBController {
6060
async fn start(&self, env: &str, _config: Option<&Path>) -> Self::DB {
6161
println!("start with env {env}");
6262
let db = match env {
63-
"local" => Box::new(CeresDB::<CeresDBServer>::create()) as DbRef,
64-
"cluster" => Box::new(CeresDB::<CeresDBCluster>::create()) as DbRef,
63+
"local" => Box::new(CeresDB::<CeresDBServer>::create().await) as DbRef,
64+
"cluster" => Box::new(CeresDB::<CeresDBCluster>::create().await) as DbRef,
6565
_ => panic!("invalid env {env}"),
6666
};
6767

0 commit comments

Comments
 (0)