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