Skip to main content

risingwave_ctl/cmd_impl/hummock/
resize_cache.rs

1// Copyright 2024 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::process::exit;
16
17use futures::future::try_join_all;
18use risingwave_common::config::RpcClientConfig;
19use risingwave_pb::compute::ResizeCacheRequest;
20use risingwave_pb::meta::GetClusterInfoResponse;
21use risingwave_rpc_client::ComputeClient;
22use thiserror_ext::AsReport;
23
24use crate::common::CtlContext;
25
26macro_rules! fail {
27    ($($arg:tt)*) => {{
28        println!($($arg)*);
29        exit(1);
30    }};
31}
32
33pub async fn resize_cache(
34    context: &CtlContext,
35    meta_cache_capacity: Option<u64>,
36    data_cache_capacity: Option<u64>,
37    clear_meta_cache: bool,
38    clear_data_cache: bool,
39) -> anyhow::Result<()> {
40    let meta_client = context.meta_client().await?;
41
42    let GetClusterInfoResponse { worker_nodes, .. } = match meta_client.get_cluster_info().await {
43        Ok(resp) => resp,
44        Err(e) => {
45            fail!("Failed to get cluster info: {}", e.as_report());
46        }
47    };
48
49    let futures = worker_nodes.iter().map(|worker| async {
50        let addr = worker.get_host().expect("worker host must be set");
51        let client = ComputeClient::new(addr.into(), &RpcClientConfig::default())
52            .await
53            .unwrap_or_else(|_| panic!("Cannot open client to compute node {addr:?}"));
54        client
55            .resize_cache(ResizeCacheRequest {
56                meta_cache_capacity: meta_cache_capacity.unwrap_or(0),
57                data_cache_capacity: data_cache_capacity.unwrap_or(0),
58                clear_meta_cache,
59                clear_data_cache,
60            })
61            .await
62    });
63
64    if let Err(e) = try_join_all(futures).await {
65        fail!("Failed to resize cache: {}", e.as_report())
66    }
67
68    Ok(())
69}