risingwave_frontend/
meta_client.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::collections::{BTreeMap, HashMap};
16
17use anyhow::Context;
18use risingwave_common::session_config::SessionConfig;
19use risingwave_common::system_param::reader::SystemParamsReader;
20use risingwave_common::util::cluster_limit::ClusterLimit;
21use risingwave_hummock_sdk::HummockVersionId;
22use risingwave_hummock_sdk::version::{HummockVersion, HummockVersionDelta};
23use risingwave_pb::backup_service::MetaSnapshotMetadata;
24use risingwave_pb::catalog::Table;
25use risingwave_pb::common::WorkerNode;
26use risingwave_pb::ddl_service::DdlProgress;
27use risingwave_pb::hummock::write_limits::WriteLimit;
28use risingwave_pb::hummock::{
29    BranchedObject, CompactTaskAssignment, CompactTaskProgress, CompactionGroupInfo,
30};
31use risingwave_pb::meta::cancel_creating_jobs_request::PbJobs;
32use risingwave_pb::meta::list_actor_splits_response::ActorSplit;
33use risingwave_pb::meta::list_actor_states_response::ActorState;
34use risingwave_pb::meta::list_cdc_progress_response::PbCdcProgress;
35use risingwave_pb::meta::list_iceberg_tables_response::IcebergTable;
36use risingwave_pb::meta::list_object_dependencies_response::PbObjectDependencies;
37use risingwave_pb::meta::list_rate_limits_response::RateLimitInfo;
38use risingwave_pb::meta::list_streaming_job_states_response::StreamingJobState;
39use risingwave_pb::meta::list_table_fragments_response::TableFragmentInfo;
40use risingwave_pb::meta::{
41    EventLog, FragmentDistribution, PbThrottleTarget, RecoveryStatus, RefreshRequest,
42    RefreshResponse,
43};
44use risingwave_pb::secret::PbSecretRef;
45use risingwave_rpc_client::error::Result;
46use risingwave_rpc_client::{HummockMetaClient, MetaClient};
47
48use crate::catalog::{DatabaseId, SinkId};
49
50/// A wrapper around the `MetaClient` that only provides a minor set of meta rpc.
51/// Most of the rpc to meta are delegated by other separate structs like `CatalogWriter`,
52/// `WorkerNodeManager`, etc. So frontend rarely needs to call `MetaClient` directly.
53/// Hence instead of to mock all rpc of `MetaClient` in tests, we aggregate those "direct" rpc
54/// in this trait so that the mocking can be simplified.
55#[async_trait::async_trait]
56pub trait FrontendMetaClient: Send + Sync {
57    async fn try_unregister(&self);
58
59    async fn flush(&self, database_id: DatabaseId) -> Result<HummockVersionId>;
60
61    async fn wait(&self) -> Result<()>;
62
63    async fn recover(&self) -> Result<()>;
64
65    async fn cancel_creating_jobs(&self, jobs: PbJobs) -> Result<Vec<u32>>;
66
67    async fn list_table_fragments(
68        &self,
69        table_ids: &[u32],
70    ) -> Result<HashMap<u32, TableFragmentInfo>>;
71
72    async fn list_streaming_job_states(&self) -> Result<Vec<StreamingJobState>>;
73
74    async fn list_fragment_distribution(&self) -> Result<Vec<FragmentDistribution>>;
75
76    async fn list_creating_fragment_distribution(&self) -> Result<Vec<FragmentDistribution>>;
77
78    async fn list_actor_states(&self) -> Result<Vec<ActorState>>;
79
80    async fn list_actor_splits(&self) -> Result<Vec<ActorSplit>>;
81
82    async fn list_object_dependencies(&self) -> Result<Vec<PbObjectDependencies>>;
83
84    async fn list_meta_snapshots(&self) -> Result<Vec<MetaSnapshotMetadata>>;
85
86    async fn set_system_param(
87        &self,
88        param: String,
89        value: Option<String>,
90    ) -> Result<Option<SystemParamsReader>>;
91
92    async fn get_session_params(&self) -> Result<SessionConfig>;
93
94    async fn set_session_param(&self, param: String, value: Option<String>) -> Result<String>;
95
96    async fn get_ddl_progress(&self) -> Result<Vec<DdlProgress>>;
97
98    async fn get_tables(
99        &self,
100        table_ids: &[u32],
101        include_dropped_table: bool,
102    ) -> Result<HashMap<u32, Table>>;
103
104    /// Returns vector of (`worker_id`, `min_pinned_version_id`)
105    async fn list_hummock_pinned_versions(&self) -> Result<Vec<(u32, u64)>>;
106
107    async fn get_hummock_current_version(&self) -> Result<HummockVersion>;
108
109    async fn get_hummock_checkpoint_version(&self) -> Result<HummockVersion>;
110
111    async fn list_version_deltas(&self) -> Result<Vec<HummockVersionDelta>>;
112
113    async fn list_branched_objects(&self) -> Result<Vec<BranchedObject>>;
114
115    async fn list_hummock_compaction_group_configs(&self) -> Result<Vec<CompactionGroupInfo>>;
116
117    async fn list_hummock_active_write_limits(&self) -> Result<HashMap<u64, WriteLimit>>;
118
119    async fn list_hummock_meta_configs(&self) -> Result<HashMap<String, String>>;
120
121    async fn list_event_log(&self) -> Result<Vec<EventLog>>;
122    async fn list_compact_task_assignment(&self) -> Result<Vec<CompactTaskAssignment>>;
123
124    async fn list_all_nodes(&self) -> Result<Vec<WorkerNode>>;
125
126    async fn list_compact_task_progress(&self) -> Result<Vec<CompactTaskProgress>>;
127
128    async fn apply_throttle(
129        &self,
130        kind: PbThrottleTarget,
131        id: u32,
132        rate_limit: Option<u32>,
133    ) -> Result<()>;
134
135    async fn get_cluster_recovery_status(&self) -> Result<RecoveryStatus>;
136
137    async fn get_cluster_limits(&self) -> Result<Vec<ClusterLimit>>;
138
139    async fn list_rate_limits(&self) -> Result<Vec<RateLimitInfo>>;
140
141    async fn list_cdc_progress(&self) -> Result<HashMap<u32, PbCdcProgress>>;
142
143    async fn get_meta_store_endpoint(&self) -> Result<String>;
144
145    async fn alter_sink_props(
146        &self,
147        sink_id: u32,
148        changed_props: BTreeMap<String, String>,
149        changed_secret_refs: BTreeMap<String, PbSecretRef>,
150        connector_conn_ref: Option<u32>,
151    ) -> Result<()>;
152
153    async fn alter_source_connector_props(
154        &self,
155        source_id: u32,
156        changed_props: BTreeMap<String, String>,
157        changed_secret_refs: BTreeMap<String, PbSecretRef>,
158        connector_conn_ref: Option<u32>,
159    ) -> Result<()>;
160
161    async fn list_hosted_iceberg_tables(&self) -> Result<Vec<IcebergTable>>;
162
163    async fn get_fragment_by_id(&self, fragment_id: u32) -> Result<Option<FragmentDistribution>>;
164
165    fn worker_id(&self) -> u32;
166
167    async fn set_sync_log_store_aligned(&self, job_id: u32, aligned: bool) -> Result<()>;
168
169    async fn compact_iceberg_table(&self, sink_id: SinkId) -> Result<u64>;
170
171    async fn expire_iceberg_table_snapshots(&self, sink_id: SinkId) -> Result<()>;
172
173    async fn refresh(&self, request: RefreshRequest) -> Result<RefreshResponse>;
174}
175
176pub struct FrontendMetaClientImpl(pub MetaClient);
177
178#[async_trait::async_trait]
179impl FrontendMetaClient for FrontendMetaClientImpl {
180    async fn try_unregister(&self) {
181        self.0.try_unregister().await;
182    }
183
184    async fn flush(&self, database_id: DatabaseId) -> Result<HummockVersionId> {
185        self.0.flush(database_id).await
186    }
187
188    async fn wait(&self) -> Result<()> {
189        self.0.wait().await
190    }
191
192    async fn recover(&self) -> Result<()> {
193        self.0.recover().await
194    }
195
196    async fn cancel_creating_jobs(&self, infos: PbJobs) -> Result<Vec<u32>> {
197        self.0.cancel_creating_jobs(infos).await
198    }
199
200    async fn list_table_fragments(
201        &self,
202        table_ids: &[u32],
203    ) -> Result<HashMap<u32, TableFragmentInfo>> {
204        self.0.list_table_fragments(table_ids).await
205    }
206
207    async fn list_streaming_job_states(&self) -> Result<Vec<StreamingJobState>> {
208        self.0.list_streaming_job_states().await
209    }
210
211    async fn list_fragment_distribution(&self) -> Result<Vec<FragmentDistribution>> {
212        self.0.list_fragment_distributions().await
213    }
214
215    async fn list_creating_fragment_distribution(&self) -> Result<Vec<FragmentDistribution>> {
216        self.0.list_creating_fragment_distribution().await
217    }
218
219    async fn list_actor_states(&self) -> Result<Vec<ActorState>> {
220        self.0.list_actor_states().await
221    }
222
223    async fn list_actor_splits(&self) -> Result<Vec<ActorSplit>> {
224        self.0.list_actor_splits().await
225    }
226
227    async fn list_object_dependencies(&self) -> Result<Vec<PbObjectDependencies>> {
228        self.0.list_object_dependencies().await
229    }
230
231    async fn list_meta_snapshots(&self) -> Result<Vec<MetaSnapshotMetadata>> {
232        let manifest = self.0.get_meta_snapshot_manifest().await?;
233        Ok(manifest.snapshot_metadata)
234    }
235
236    async fn set_system_param(
237        &self,
238        param: String,
239        value: Option<String>,
240    ) -> Result<Option<SystemParamsReader>> {
241        self.0.set_system_param(param, value).await
242    }
243
244    async fn get_session_params(&self) -> Result<SessionConfig> {
245        let session_config: SessionConfig =
246            serde_json::from_str(&self.0.get_session_params().await?)
247                .context("failed to parse session config")?;
248        Ok(session_config)
249    }
250
251    async fn set_session_param(&self, param: String, value: Option<String>) -> Result<String> {
252        self.0.set_session_param(param, value).await
253    }
254
255    async fn get_ddl_progress(&self) -> Result<Vec<DdlProgress>> {
256        let ddl_progress = self.0.get_ddl_progress().await?;
257        Ok(ddl_progress)
258    }
259
260    async fn get_tables(
261        &self,
262        table_ids: &[u32],
263        include_dropped_tables: bool,
264    ) -> Result<HashMap<u32, Table>> {
265        let tables = self.0.get_tables(table_ids, include_dropped_tables).await?;
266        Ok(tables)
267    }
268
269    async fn list_hummock_pinned_versions(&self) -> Result<Vec<(u32, u64)>> {
270        let pinned_versions = self
271            .0
272            .risectl_get_pinned_versions_summary()
273            .await?
274            .summary
275            .unwrap()
276            .pinned_versions;
277        let ret = pinned_versions
278            .into_iter()
279            .map(|v| (v.context_id, v.min_pinned_id))
280            .collect();
281        Ok(ret)
282    }
283
284    async fn get_hummock_current_version(&self) -> Result<HummockVersion> {
285        self.0.get_current_version().await
286    }
287
288    async fn get_hummock_checkpoint_version(&self) -> Result<HummockVersion> {
289        self.0
290            .risectl_get_checkpoint_hummock_version()
291            .await
292            .map(|v| HummockVersion::from_rpc_protobuf(&v.checkpoint_version.unwrap()))
293    }
294
295    async fn list_version_deltas(&self) -> Result<Vec<HummockVersionDelta>> {
296        // FIXME #8612: there can be lots of version deltas, so better to fetch them by pages and refactor `SysRowSeqScanExecutor` to yield multiple chunks.
297        self.0
298            .list_version_deltas(HummockVersionId::new(0), u32::MAX, u64::MAX)
299            .await
300    }
301
302    async fn list_branched_objects(&self) -> Result<Vec<BranchedObject>> {
303        self.0.list_branched_object().await
304    }
305
306    async fn list_hummock_compaction_group_configs(&self) -> Result<Vec<CompactionGroupInfo>> {
307        self.0.risectl_list_compaction_group().await
308    }
309
310    async fn list_hummock_active_write_limits(&self) -> Result<HashMap<u64, WriteLimit>> {
311        self.0.list_active_write_limit().await
312    }
313
314    async fn list_hummock_meta_configs(&self) -> Result<HashMap<String, String>> {
315        self.0.list_hummock_meta_config().await
316    }
317
318    async fn list_event_log(&self) -> Result<Vec<EventLog>> {
319        self.0.list_event_log().await
320    }
321
322    async fn list_compact_task_assignment(&self) -> Result<Vec<CompactTaskAssignment>> {
323        self.0.list_compact_task_assignment().await
324    }
325
326    async fn list_all_nodes(&self) -> Result<Vec<WorkerNode>> {
327        self.0.list_worker_nodes(None).await
328    }
329
330    async fn list_compact_task_progress(&self) -> Result<Vec<CompactTaskProgress>> {
331        self.0.list_compact_task_progress().await
332    }
333
334    async fn apply_throttle(
335        &self,
336        kind: PbThrottleTarget,
337        id: u32,
338        rate_limit: Option<u32>,
339    ) -> Result<()> {
340        self.0
341            .apply_throttle(kind, id, rate_limit)
342            .await
343            .map(|_| ())
344    }
345
346    async fn get_cluster_recovery_status(&self) -> Result<RecoveryStatus> {
347        self.0.get_cluster_recovery_status().await
348    }
349
350    async fn get_cluster_limits(&self) -> Result<Vec<ClusterLimit>> {
351        self.0.get_cluster_limits().await
352    }
353
354    async fn list_rate_limits(&self) -> Result<Vec<RateLimitInfo>> {
355        self.0.list_rate_limits().await
356    }
357
358    async fn list_cdc_progress(&self) -> Result<HashMap<u32, PbCdcProgress>> {
359        self.0.list_cdc_progress().await
360    }
361
362    async fn get_meta_store_endpoint(&self) -> Result<String> {
363        self.0.get_meta_store_endpoint().await
364    }
365
366    async fn alter_sink_props(
367        &self,
368        sink_id: u32,
369        changed_props: BTreeMap<String, String>,
370        changed_secret_refs: BTreeMap<String, PbSecretRef>,
371        connector_conn_ref: Option<u32>,
372    ) -> Result<()> {
373        self.0
374            .alter_sink_props(
375                sink_id,
376                changed_props,
377                changed_secret_refs,
378                connector_conn_ref,
379            )
380            .await
381    }
382
383    async fn alter_source_connector_props(
384        &self,
385        source_id: u32,
386        changed_props: BTreeMap<String, String>,
387        changed_secret_refs: BTreeMap<String, PbSecretRef>,
388        connector_conn_ref: Option<u32>,
389    ) -> Result<()> {
390        self.0
391            .alter_source_connector_props(
392                source_id,
393                changed_props,
394                changed_secret_refs,
395                connector_conn_ref,
396            )
397            .await
398    }
399
400    async fn list_hosted_iceberg_tables(&self) -> Result<Vec<IcebergTable>> {
401        self.0.list_hosted_iceberg_tables().await
402    }
403
404    async fn get_fragment_by_id(&self, fragment_id: u32) -> Result<Option<FragmentDistribution>> {
405        self.0.get_fragment_by_id(fragment_id).await
406    }
407
408    fn worker_id(&self) -> u32 {
409        self.0.worker_id()
410    }
411
412    async fn set_sync_log_store_aligned(&self, job_id: u32, aligned: bool) -> Result<()> {
413        self.0.set_sync_log_store_aligned(job_id, aligned).await
414    }
415
416    async fn compact_iceberg_table(&self, sink_id: SinkId) -> Result<u64> {
417        self.0.compact_iceberg_table(sink_id).await
418    }
419
420    async fn expire_iceberg_table_snapshots(&self, sink_id: SinkId) -> Result<()> {
421        self.0.expire_iceberg_table_snapshots(sink_id).await
422    }
423
424    async fn refresh(&self, request: RefreshRequest) -> Result<RefreshResponse> {
425        self.0.refresh(request).await
426    }
427}