Skip to main content

risedev/task/
task_starrocks_ready_check.rs

1// Copyright 2026 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::io::Write;
16use std::process::Command;
17use std::time::Duration;
18
19use anyhow::{Context, Result, ensure};
20
21use super::{ExecuteContext, Task};
22use crate::wait::wait;
23use crate::{STARROCKS_CONTAINER_HOST, STARROCKS_CONTAINER_QUERY_PORT, StarrocksConfig};
24
25pub struct StarrocksReadyCheckTask {
26    config: StarrocksConfig,
27}
28
29impl StarrocksReadyCheckTask {
30    pub fn new(config: StarrocksConfig) -> Self {
31        Self { config }
32    }
33}
34
35fn backend_is_alive(output: &str) -> bool {
36    let mut lines = output.lines();
37    let Some(alive_column) = lines
38        .next()
39        .and_then(|header| header.split('\t').position(|name| name == "Alive"))
40    else {
41        return false;
42    };
43
44    lines.any(|line| {
45        line.split('\t')
46            .nth(alive_column)
47            .is_some_and(|value| value.eq_ignore_ascii_case("true") || value == "1")
48    })
49}
50
51impl Task for StarrocksReadyCheckTask {
52    fn execute(&mut self, ctx: &mut ExecuteContext<impl Write>) -> Result<()> {
53        let Some(id) = ctx.id.clone() else {
54            panic!("Service should be set before executing StarrocksReadyCheckTask");
55        };
56
57        ctx.pb.set_message("waiting for FE and BE ready...");
58
59        wait(
60            || {
61                let mut command = if self.config.user_managed {
62                    let mut command = Command::new("mysql");
63                    command
64                        .arg("-h")
65                        .arg(&self.config.address)
66                        .arg("-P")
67                        .arg(self.config.query_port.to_string())
68                        .arg("-u")
69                        .arg(&self.config.admin_user);
70                    if !self.config.admin_password.is_empty() {
71                        command.env("MYSQL_PWD", &self.config.admin_password);
72                    }
73                    command
74                } else {
75                    let mut command = Command::new("docker");
76                    command.arg("exec");
77                    if !self.config.admin_password.is_empty() {
78                        command
79                            .arg("-e")
80                            .arg(format!("MYSQL_PWD={}", self.config.admin_password));
81                    }
82                    command
83                        .arg(format!("risedev-{}", self.config.id))
84                        .arg("mysql")
85                        .arg("-h")
86                        .arg(STARROCKS_CONTAINER_HOST)
87                        .arg("-P")
88                        .arg(STARROCKS_CONTAINER_QUERY_PORT.to_string())
89                        .arg("-u")
90                        .arg(&self.config.admin_user);
91                    command
92                };
93
94                let output = command
95                    .args(["-B", "--column-names", "-e", "SHOW BACKENDS"])
96                    .output()
97                    .context("failed to query StarRocks backends")?;
98                ensure!(
99                    output.status.success(),
100                    "failed to query StarRocks backends: {}",
101                    String::from_utf8_lossy(&output.stderr).trim()
102                );
103                ensure!(
104                    backend_is_alive(&String::from_utf8_lossy(&output.stdout)),
105                    "StarRocks BE is not alive yet"
106                );
107                Ok(())
108            },
109            &mut ctx.log,
110            ctx.status_file.as_ref().unwrap(),
111            &id,
112            Some(Duration::from_secs(120)),
113            !self.config.user_managed,
114        )
115        .with_context(|| format!("failed to wait for service `{id}` to be ready"))?;
116
117        ctx.complete_spin();
118        Ok(())
119    }
120}