risingwave_meta/barrier/context/
mod.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
15mod context_impl;
16mod recovery;
17
18use std::future::Future;
19use std::sync::Arc;
20
21use arc_swap::ArcSwap;
22use risingwave_common::catalog::DatabaseId;
23use risingwave_pb::common::WorkerNode;
24use risingwave_pb::hummock::HummockVersionStats;
25use risingwave_pb::stream_service::streaming_control_stream_request::PbInitRequest;
26use risingwave_rpc_client::StreamingControlHandle;
27
28use crate::MetaResult;
29use crate::barrier::command::CommandContext;
30use crate::barrier::progress::TrackingJob;
31use crate::barrier::schedule::{MarkReadyOptions, ScheduledBarriers};
32use crate::barrier::{
33    BarrierManagerStatus, BarrierScheduler, BarrierWorkerRuntimeInfoSnapshot,
34    DatabaseRuntimeInfoSnapshot, RecoveryReason, Scheduled,
35};
36use crate::hummock::{CommitEpochInfo, HummockManagerRef};
37use crate::manager::{MetaSrvEnv, MetadataManager};
38use crate::stream::{ScaleControllerRef, SourceManagerRef};
39
40pub(super) trait GlobalBarrierWorkerContext: Send + Sync + 'static {
41    fn commit_epoch(
42        &self,
43        commit_info: CommitEpochInfo,
44    ) -> impl Future<Output = MetaResult<HummockVersionStats>> + Send + '_;
45
46    async fn next_scheduled(&self) -> Scheduled;
47    fn abort_and_mark_blocked(
48        &self,
49        database_id: Option<DatabaseId>,
50        recovery_reason: RecoveryReason,
51    );
52    fn mark_ready(&self, options: MarkReadyOptions);
53
54    fn post_collect_command<'a>(
55        &'a self,
56        command: &'a CommandContext,
57    ) -> impl Future<Output = MetaResult<()>> + Send + 'a;
58
59    async fn notify_creating_job_failed(&self, database_id: Option<DatabaseId>, err: String);
60
61    fn finish_creating_job(
62        &self,
63        job: TrackingJob,
64    ) -> impl Future<Output = MetaResult<()>> + Send + '_;
65
66    async fn new_control_stream(
67        &self,
68        node: &WorkerNode,
69        init_request: &PbInitRequest,
70    ) -> MetaResult<StreamingControlHandle>;
71
72    async fn reload_runtime_info(&self) -> MetaResult<BarrierWorkerRuntimeInfoSnapshot>;
73
74    async fn reload_database_runtime_info(
75        &self,
76        database_id: DatabaseId,
77    ) -> MetaResult<Option<DatabaseRuntimeInfoSnapshot>>;
78
79    fn handle_load_finished_source_ids(
80        &self,
81        load_finished_source_ids: Vec<u32>,
82    ) -> impl Future<Output = MetaResult<()>> + Send + '_;
83}
84
85pub(super) struct GlobalBarrierWorkerContextImpl {
86    scheduled_barriers: ScheduledBarriers,
87
88    status: Arc<ArcSwap<BarrierManagerStatus>>,
89
90    pub(super) metadata_manager: MetadataManager,
91
92    hummock_manager: HummockManagerRef,
93
94    source_manager: SourceManagerRef,
95
96    scale_controller: ScaleControllerRef,
97
98    pub(super) env: MetaSrvEnv,
99
100    /// Barrier scheduler for scheduling load finish commands
101    barrier_scheduler: BarrierScheduler,
102}
103
104impl GlobalBarrierWorkerContextImpl {
105    pub(super) fn new(
106        scheduled_barriers: ScheduledBarriers,
107        status: Arc<ArcSwap<BarrierManagerStatus>>,
108        metadata_manager: MetadataManager,
109        hummock_manager: HummockManagerRef,
110        source_manager: SourceManagerRef,
111        scale_controller: ScaleControllerRef,
112        env: MetaSrvEnv,
113        barrier_scheduler: BarrierScheduler,
114    ) -> Self {
115        Self {
116            scheduled_barriers,
117            status,
118            metadata_manager,
119            hummock_manager,
120            source_manager,
121            scale_controller,
122            env,
123            barrier_scheduler,
124        }
125    }
126
127    pub(super) fn status(&self) -> Arc<ArcSwap<BarrierManagerStatus>> {
128        self.status.clone()
129    }
130}