Skip to main content

risingwave_stream/from_proto/
locality_provider.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
15use std::sync::Arc;
16
17use risingwave_pb::stream_plan::LocalityProviderNode;
18use risingwave_storage::StateStore;
19
20use super::*;
21use crate::common::table::state_table::{StateTableBuilder, StateTableOpConsistencyLevel};
22use crate::executor::Executor;
23use crate::executor::locality_provider::{LocalityProviderExecutor, SortBufferSettings};
24
25impl_stream_node_body!(LocalityProvider(LocalityProviderNode) => LocalityProviderBuilder);
26
27impl ExecutorBuilder for LocalityProviderBuilder {
28    type Node = LocalityProviderNode;
29
30    async fn new_boxed_executor(
31        params: ExecutorParams,
32        node: &Self::Node,
33        store: impl StateStore,
34    ) -> StreamResult<Executor> {
35        let [input]: [_; 1] = params.input.try_into().unwrap();
36
37        let locality_columns = node
38            .locality_columns
39            .iter()
40            .map(|&i| i as usize)
41            .collect::<Vec<_>>();
42
43        let input_schema = input.schema().clone();
44
45        let vnodes = Some(Arc::new(params.vnode_bitmap));
46
47        // Create state table for buffering input data
48        let state_table = StateTableBuilder::new(
49            node.get_state_table().unwrap(),
50            store.clone(),
51            vnodes.clone(),
52        )
53        .enable_preload_all_rows_by_config(&params.config)
54        .build()
55        .await;
56
57        // Create progress table for tracking backfill progress
58        let progress_table = StateTableBuilder::new(
59            node.get_progress_table().unwrap(),
60            store.clone(),
61            vnodes.clone(),
62        )
63        .enable_preload_all_rows_by_config(&params.config)
64        .build()
65        .await;
66
67        // Create the ephemeral sorted log table for the post-backfill sort buffer, if enabled at
68        // plan time. Writes to it are append-only with unique keys, so consistency checks on old
69        // values are unnecessary.
70        let sort_buffer_table = match &node.sort_buffer_table {
71            Some(table) => Some(
72                StateTableBuilder::new(table, store, vnodes)
73                    .with_op_consistency_level(StateTableOpConsistencyLevel::Inconsistent)
74                    .forbid_preload_all_rows()
75                    .build()
76                    .await,
77            ),
78            None => None,
79        };
80
81        let progress = params
82            .local_barrier_manager
83            .register_create_mview_progress(&params.actor_context);
84
85        let sort_buffer_settings = SortBufferSettings {
86            enabled: params.config.developer.enable_locality_sort_buffer,
87            activate_threshold: params
88                .config
89                .developer
90                .locality_sort_buffer_activate_threshold,
91        };
92
93        let exec = LocalityProviderExecutor::new(
94            input,
95            locality_columns,
96            state_table,
97            progress_table,
98            sort_buffer_table,
99            sort_buffer_settings,
100            input_schema,
101            progress,
102            params.executor_stats.clone(),
103            params.config.developer.chunk_size,
104            params.actor_context.fragment_id,
105            node.rate_limit.into(),
106        );
107
108        Ok((params.info, exec).into())
109    }
110}
111
112pub struct LocalityProviderBuilder;