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, PbRefreshFinishedActor,
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, BarrierWorkerRuntimeInfoSnapshot, CreateStreamingJobCommandInfo,
39    CreateStreamingJobType, DatabaseRuntimeInfoSnapshot, IndependentStreamingJobType,
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::{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(&self, options: MarkReadyOptions) -> MetaResult<()>;
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_actors(
156        &self,
157        refresh_finished_actors: Vec<PbRefreshFinishedActor>,
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    pub(super) refresh_manager: GlobalRefreshManagerRef,
204
205    sink_manager: SinkCoordinatorManager,
206
207    pub(super) iceberg_pk_index_sink_manager: IcebergPkIndexSinkManager,
208
209    pub(super) iceberg_compaction_manager: IcebergCompactionManagerRef,
210}
211
212impl GlobalBarrierWorkerContextImpl {
213    #[expect(clippy::too_many_arguments)]
214    pub(super) fn new(
215        scheduled_barriers: ScheduledBarriers,
216        status: Arc<ArcSwap<BarrierManagerStatus>>,
217        metadata_manager: MetadataManager,
218        hummock_manager: HummockManagerRef,
219        serving_vnode_mapping: ServingVnodeMappingRef,
220        source_manager: SourceManagerRef,
221        scale_controller: ScaleControllerRef,
222        env: MetaSrvEnv,
223        refresh_manager: GlobalRefreshManagerRef,
224        sink_manager: SinkCoordinatorManager,
225        iceberg_pk_index_sink_manager: IcebergPkIndexSinkManager,
226        iceberg_compaction_manager: IcebergCompactionManagerRef,
227    ) -> Self {
228        Self {
229            scheduled_barriers,
230            status,
231            metadata_manager,
232            hummock_manager,
233            serving_vnode_mapping,
234            source_manager,
235            _scale_controller: scale_controller,
236            env,
237            refresh_manager,
238            sink_manager,
239            iceberg_pk_index_sink_manager,
240            iceberg_compaction_manager,
241        }
242    }
243
244    pub(super) fn status(&self) -> Arc<ArcSwap<BarrierManagerStatus>> {
245        self.status.clone()
246    }
247}