Skip to main content

risingwave_meta/barrier/context/
mod.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
15mod context_impl;
16pub(crate) mod recovery;
17
18use std::future::Future;
19use std::sync::Arc;
20
21use arc_swap::ArcSwap;
22use risingwave_common::catalog::{DatabaseId, TableId};
23use risingwave_common::id::{JobId, PartialGraphId};
24use risingwave_meta_model::SinkId;
25use risingwave_pb::common::WorkerNode;
26use risingwave_pb::hummock::HummockVersionStats;
27use risingwave_pb::stream_service::barrier_complete_response::{
28    PbListFinishedSource, PbLoadFinishedSource,
29};
30use risingwave_rpc_client::StreamingControlHandle;
31
32use crate::MetaResult;
33use crate::barrier::checkpoint::independent_job::BatchRefreshJobTriggerContext;
34use crate::barrier::command::{PostCollectCommand, SinceTimestampResolvedEpoch};
35use crate::barrier::progress::TrackingJob;
36use crate::barrier::schedule::{MarkReadyOptions, ScheduledBarriers};
37use crate::barrier::{
38    BarrierManagerStatus, BarrierScheduler, BarrierWorkerRuntimeInfoSnapshot, BatchRefreshInfo,
39    CreateStreamingJobCommandInfo, CreateStreamingJobType, DatabaseRuntimeInfoSnapshot,
40    RecoveryReason, Scheduled, SnapshotBackfillInfo,
41};
42use crate::hummock::{CommitEpochInfo, HummockManagerRef};
43use crate::manager::iceberg_compaction::IcebergCompactionManagerRef;
44use crate::manager::iceberg_pk_index_sink::{
45    IcebergPkIndexPreCommitMetadata, IcebergPkIndexSinkManager,
46};
47use crate::manager::sink_coordination::SinkCoordinatorManager;
48use crate::manager::{MetaSrvEnv, MetadataManager};
49use crate::serving::ServingVnodeMappingRef;
50use crate::stream::source_manager::SplitAssignment;
51use crate::stream::{GlobalRefreshManagerRef, ScaleControllerRef, SourceManagerRef};
52
53#[derive(Debug)]
54pub(super) struct CreateSnapshotBackfillJobCommandInfo {
55    pub info: CreateStreamingJobCommandInfo,
56    pub snapshot_backfill_info: SnapshotBackfillInfo,
57    pub cross_db_snapshot_backfill_info: SnapshotBackfillInfo,
58    pub resolved_split_assignment: SplitAssignment,
59    /// If set, this is a batch refresh job rather than a regular snapshot backfill.
60    pub refresh_interval_sec: Option<u64>,
61}
62
63impl CreateSnapshotBackfillJobCommandInfo {
64    pub(super) fn into_post_collect(self) -> PostCollectCommand {
65        let job_type = if let Some(refresh_interval_sec) = self.refresh_interval_sec {
66            CreateStreamingJobType::BatchRefresh(BatchRefreshInfo {
67                snapshot_backfill_info: self.snapshot_backfill_info,
68                refresh_interval_sec,
69            })
70        } else {
71            CreateStreamingJobType::SnapshotBackfill {
72                snapshot_backfill_info: self.snapshot_backfill_info,
73                // `since_epoch` is only used before job creation barriers are injected, and
74                // post-collect snapshot backfill does not go through that path.
75                since_epoch: None,
76            }
77        };
78        PostCollectCommand::CreateStreamingJob {
79            info: self.info,
80            job_type,
81            cross_db_snapshot_backfill_info: self.cross_db_snapshot_backfill_info,
82            resolved_split_assignment: self.resolved_split_assignment,
83        }
84    }
85}
86
87pub(super) trait GlobalBarrierWorkerContext: Send + Sync + 'static {
88    fn commit_epoch(
89        &self,
90        commit_info: CommitEpochInfo,
91    ) -> impl Future<Output = MetaResult<HummockVersionStats>> + Send + '_;
92
93    async fn next_scheduled(&self) -> Scheduled;
94    fn abort_and_mark_blocked(
95        &self,
96        database_id: Option<DatabaseId>,
97        recovery_reason: RecoveryReason,
98    );
99    fn mark_ready(&self, options: MarkReadyOptions);
100    fn resolve_log_store_epoch<'a>(
101        &'a self,
102        upstream_table_ids: impl Iterator<Item = TableId> + Send + 'a,
103        since_epoch: u64,
104    ) -> impl Future<Output = MetaResult<SinceTimestampResolvedEpoch>> + Send + 'a;
105
106    async fn refresh_table_refill_runtime_state_after_recovery(&self) -> MetaResult<()> {
107        Ok(())
108    }
109
110    fn post_collect_command(
111        &self,
112        command: PostCollectCommand,
113    ) -> impl Future<Output = MetaResult<()>> + Send + '_;
114
115    fn notify_creating_job_failed(
116        &self,
117        database_id: Option<DatabaseId>,
118        err: String,
119    ) -> impl Future<Output = ()> + Send + '_;
120
121    fn finish_creating_job(
122        &self,
123        job: TrackingJob,
124    ) -> impl Future<Output = MetaResult<()>> + Send + '_;
125
126    fn finish_cdc_table_backfill(
127        &self,
128        job_id: JobId,
129    ) -> impl Future<Output = MetaResult<()>> + Send + '_;
130
131    fn new_control_stream<'a>(
132        &'a self,
133        node: &'a WorkerNode,
134    ) -> impl Future<Output = MetaResult<StreamingControlHandle>> + Send + 'a;
135
136    fn reload_runtime_info(
137        &self,
138    ) -> impl Future<Output = MetaResult<BarrierWorkerRuntimeInfoSnapshot>> + Send + '_;
139
140    async fn reload_database_runtime_info(
141        &self,
142        database_id: DatabaseId,
143    ) -> MetaResult<DatabaseRuntimeInfoSnapshot>;
144
145    fn handle_list_finished_source_ids(
146        &self,
147        list_finished_source_ids: Vec<PbListFinishedSource>,
148    ) -> impl Future<Output = MetaResult<()>> + Send + '_;
149
150    fn handle_load_finished_source_ids(
151        &self,
152        load_finished_source_ids: Vec<PbLoadFinishedSource>,
153    ) -> impl Future<Output = MetaResult<()>> + Send + '_;
154
155    fn handle_refresh_finished_table_ids(
156        &self,
157        refresh_finished_table_job_ids: Vec<JobId>,
158    ) -> impl Future<Output = MetaResult<()>> + Send + '_;
159
160    /// Load the trigger context for a batch refresh job: fragment metadata, job model,
161    /// upstream log epochs, and target upstream epoch — all bundled in one struct.
162    fn load_batch_refresh_trigger_context(
163        &self,
164        job_id: JobId,
165        database_id: DatabaseId,
166        last_committed_epoch: u64,
167    ) -> impl Future<Output = MetaResult<BatchRefreshJobTriggerContext>> + Send + '_;
168
169    fn pre_commit_iceberg_pk_index_sink_metadata(
170        &self,
171        metadata: Vec<IcebergPkIndexPreCommitMetadata>,
172    ) -> impl Future<Output = MetaResult<Vec<SinkId>>> + Send + '_;
173
174    fn commit_iceberg_pk_index_sink_metadata(
175        &self,
176        sink_ids: Vec<SinkId>,
177    ) -> impl Future<Output = MetaResult<()>> + Send + '_;
178
179    /// Advance per-database pk-index committed epochs after a checkpoint completion.
180    fn advance_iceberg_pk_index_sink_committed_epochs(
181        &self,
182        epochs: impl IntoIterator<Item = (PartialGraphId, u64)>,
183    );
184}
185
186pub(super) struct GlobalBarrierWorkerContextImpl {
187    scheduled_barriers: ScheduledBarriers,
188
189    status: Arc<ArcSwap<BarrierManagerStatus>>,
190
191    pub(super) metadata_manager: MetadataManager,
192
193    hummock_manager: HummockManagerRef,
194
195    serving_vnode_mapping: ServingVnodeMappingRef,
196
197    source_manager: SourceManagerRef,
198
199    _scale_controller: ScaleControllerRef,
200
201    pub(super) env: MetaSrvEnv,
202
203    /// Barrier scheduler for scheduling load finish commands
204    barrier_scheduler: BarrierScheduler,
205
206    pub(super) refresh_manager: GlobalRefreshManagerRef,
207
208    sink_manager: SinkCoordinatorManager,
209
210    pub(super) iceberg_pk_index_sink_manager: IcebergPkIndexSinkManager,
211
212    pub(super) iceberg_compaction_manager: IcebergCompactionManagerRef,
213}
214
215impl GlobalBarrierWorkerContextImpl {
216    #[expect(clippy::too_many_arguments)]
217    pub(super) fn new(
218        scheduled_barriers: ScheduledBarriers,
219        status: Arc<ArcSwap<BarrierManagerStatus>>,
220        metadata_manager: MetadataManager,
221        hummock_manager: HummockManagerRef,
222        serving_vnode_mapping: ServingVnodeMappingRef,
223        source_manager: SourceManagerRef,
224        scale_controller: ScaleControllerRef,
225        env: MetaSrvEnv,
226        barrier_scheduler: BarrierScheduler,
227        refresh_manager: GlobalRefreshManagerRef,
228        sink_manager: SinkCoordinatorManager,
229        iceberg_pk_index_sink_manager: IcebergPkIndexSinkManager,
230        iceberg_compaction_manager: IcebergCompactionManagerRef,
231    ) -> Self {
232        Self {
233            scheduled_barriers,
234            status,
235            metadata_manager,
236            hummock_manager,
237            serving_vnode_mapping,
238            source_manager,
239            _scale_controller: scale_controller,
240            env,
241            barrier_scheduler,
242            refresh_manager,
243            sink_manager,
244            iceberg_pk_index_sink_manager,
245            iceberg_compaction_manager,
246        }
247    }
248
249    pub(super) fn status(&self) -> Arc<ArcSwap<BarrierManagerStatus>> {
250        self.status.clone()
251    }
252}