risingwave_ctl/common/
meta_service.rs

1// Copyright 2025 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::env;
16use std::sync::Arc;
17
18use anyhow::{Result, bail};
19use risingwave_common::config::MetaConfig;
20use risingwave_common::util::addr::HostAddr;
21use risingwave_pb::common::WorkerType;
22use risingwave_pb::common::worker_node::Property;
23use risingwave_rpc_client::MetaClient;
24
25pub struct MetaServiceOpts {
26    pub meta_addr: String,
27}
28
29impl MetaServiceOpts {
30    /// Recover meta service options from env variable
31    ///
32    /// Currently, we will read these variables for meta:
33    ///
34    /// * `RW_META_ADDR`: meta service address
35    pub fn from_env() -> Result<Self> {
36        let meta_addr = match env::var("RW_META_ADDR") {
37            Ok(url) => {
38                tracing::info!("using meta addr from `RW_META_ADDR`: {}", url);
39                url
40            }
41            Err(_) => {
42                const MESSAGE: &str = "env variable `RW_META_ADDR` not found.
43
44For `./risedev d` use cases, please do the following:
45* use `./risedev ctl` to use risectl.
46
47For production use cases,
48* please set `RW_META_ADDR` to the address of the meta node.
49
50Note: the default value of `RW_META_ADDR` is 'http://127.0.0.1:5690'.";
51                bail!(MESSAGE);
52            }
53        };
54        Ok(Self { meta_addr })
55    }
56
57    /// Create meta client from options, and register as rise-ctl worker
58    pub async fn create_meta_client(&self) -> Result<MetaClient> {
59        let (client, _) = MetaClient::register_new(
60            self.meta_addr.parse()?,
61            WorkerType::RiseCtl,
62            &get_new_ctl_identity(),
63            Property::default(),
64            Arc::new(MetaConfig::default()),
65        )
66        .await;
67        let worker_id = client.worker_id();
68        tracing::info!("registered as RiseCtl worker, worker_id = {}", worker_id);
69        Ok(client)
70    }
71}
72
73fn get_new_ctl_identity() -> HostAddr {
74    HostAddr {
75        host: format!("risectl-{}", uuid::Uuid::new_v4()),
76        port: 0,
77    }
78}