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