-
Notifications
You must be signed in to change notification settings - Fork 845
Expand file tree
/
Copy pathmod.rs
More file actions
359 lines (320 loc) · 13.5 KB
/
Copy pathmod.rs
File metadata and controls
359 lines (320 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2022 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pub mod enumerator;
pub mod external;
pub mod jni_source;
pub mod source;
pub mod split;
use std::collections::{BTreeMap, HashMap};
use std::marker::PhantomData;
pub use enumerator::*;
use itertools::Itertools;
use risingwave_common::id::{ActorId, SourceId};
use risingwave_pb::catalog::PbSource;
use risingwave_pb::connector_service::{PbSourceType, PbTableSchema, SourceType, TableSchema};
use risingwave_pb::plan_common::ExternalTableDesc;
use risingwave_pb::plan_common::column_desc::GeneratedOrDefaultColumn;
use risingwave_pb::source::{PbCdcTableSnapshotSplit, PbCdcTableSnapshotSplitsWithGeneration};
use risingwave_pb::stream_plan::StreamCdcScanOptions;
use simd_json::prelude::ArrayTrait;
pub use source::*;
use crate::enforce_secret::EnforceSecret;
use crate::error::ConnectorResult;
use crate::source::{CdcTableSnapshotSplitRaw, SourceProperties, SplitImpl, TryFromBTreeMap};
use crate::{for_all_classified_sources, impl_cdc_source_type};
pub const CDC_CONNECTOR_NAME_SUFFIX: &str = "-cdc";
pub const CDC_SNAPSHOT_MODE_KEY: &str = "debezium.snapshot.mode";
pub const CDC_SNAPSHOT_BACKFILL: &str = "rw_cdc_backfill";
pub const CDC_SHARING_MODE_KEY: &str = "rw.sharing.mode.enable";
// User can set snapshot='false' to disable cdc backfill
pub const CDC_BACKFILL_ENABLE_KEY: &str = "snapshot";
pub const CDC_BACKFILL_SNAPSHOT_INTERVAL_KEY: &str = "snapshot.interval";
pub const CDC_BACKFILL_SNAPSHOT_BATCH_SIZE_KEY: &str = "snapshot.batch_size";
pub const CDC_BACKFILL_PARALLELISM: &str = "backfill.parallelism";
pub const CDC_BACKFILL_NUM_ROWS_PER_SPLIT: &str = "backfill.num_rows_per_split";
pub const CDC_BACKFILL_AS_EVEN_SPLITS: &str = "backfill.as_even_splits";
pub const CDC_BACKFILL_SPLIT_PK_COLUMN_INDEX: &str = "backfill.split_pk_column_index";
pub const CDC_BACKFILL_SPLIT_COLUMN_NAME: &str = "backfill.split_column_name";
// We enable transaction for shared cdc source by default
pub const CDC_TRANSACTIONAL_KEY: &str = "transactional";
pub const CDC_WAIT_FOR_STREAMING_START_TIMEOUT: &str = "cdc.source.wait.streaming.start.timeout";
pub const CDC_BACKFILL_MAX_PARALLELISM: u32 = 256;
// User can set strong-schema='true' to enable strong schema for mongo cdc source
pub const CDC_MONGODB_STRONG_SCHEMA_KEY: &str = "strong_schema";
pub const MYSQL_CDC_CONNECTOR: &str = Mysql::CDC_CONNECTOR_NAME;
pub const POSTGRES_CDC_CONNECTOR: &str = Postgres::CDC_CONNECTOR_NAME;
pub const CITUS_CDC_CONNECTOR: &str = Citus::CDC_CONNECTOR_NAME;
pub const MONGODB_CDC_CONNECTOR: &str = Mongodb::CDC_CONNECTOR_NAME;
pub const SQL_SERVER_CDC_CONNECTOR: &str = SqlServer::CDC_CONNECTOR_NAME;
/// Build a unique CDC table identifier from a source ID and external table name
pub fn build_cdc_table_id(source_id: SourceId, external_table_name: &str) -> String {
format!("{}.{}", source_id, external_table_name)
}
pub trait CdcSourceTypeTrait: Send + Sync + Clone + std::fmt::Debug + 'static {
const CDC_CONNECTOR_NAME: &'static str;
fn source_type() -> CdcSourceType;
}
for_all_classified_sources!(impl_cdc_source_type);
impl<'a> From<&'a str> for CdcSourceType {
fn from(name: &'a str) -> Self {
match name {
MYSQL_CDC_CONNECTOR => CdcSourceType::Mysql,
POSTGRES_CDC_CONNECTOR => CdcSourceType::Postgres,
CITUS_CDC_CONNECTOR => CdcSourceType::Citus,
MONGODB_CDC_CONNECTOR => CdcSourceType::Mongodb,
SQL_SERVER_CDC_CONNECTOR => CdcSourceType::SqlServer,
_ => CdcSourceType::Unspecified,
}
}
}
impl CdcSourceType {
pub fn as_str_name(&self) -> &str {
match self {
CdcSourceType::Mysql => "MySQL",
CdcSourceType::Postgres => "Postgres",
CdcSourceType::Citus => "Citus",
CdcSourceType::Mongodb => "MongoDB",
CdcSourceType::SqlServer => "SQL Server",
CdcSourceType::Unspecified => "Unspecified",
}
}
}
#[derive(Clone, Debug, Default)]
pub struct CdcProperties<T: CdcSourceTypeTrait> {
/// Properties specified in the WITH clause by user
pub properties: BTreeMap<String, String>,
/// Schema of the source specified by users
pub table_schema: TableSchema,
/// Whether it is created by a cdc source job
pub is_cdc_source_job: bool,
/// For validation purpose, mark if the table is a backfill cdc table
pub is_backfill_table: bool,
pub _phantom: PhantomData<T>,
}
pub fn table_schema_exclude_additional_columns(table_schema: &TableSchema) -> TableSchema {
TableSchema {
columns: table_schema
.columns
.iter()
.filter(|col| {
col.additional_column
.as_ref()
.is_some_and(|val| val.column_type.is_none())
})
.cloned()
.collect(),
pk_indices: table_schema.pk_indices.clone(),
}
}
impl<T: CdcSourceTypeTrait> TryFromBTreeMap for CdcProperties<T> {
fn try_from_btreemap(
properties: BTreeMap<String, String>,
_deny_unknown_fields: bool,
) -> ConnectorResult<Self> {
let is_share_source: bool = properties
.get(CDC_SHARING_MODE_KEY)
.is_some_and(|v| v == "true");
Ok(CdcProperties {
properties,
table_schema: Default::default(),
// TODO(siyuan): use serde to deserialize input hashmap
is_cdc_source_job: is_share_source,
is_backfill_table: false,
_phantom: PhantomData,
})
}
}
impl<T: CdcSourceTypeTrait> EnforceSecret for CdcProperties<T> {} // todo: enforce jdbc like properties
impl<T: CdcSourceTypeTrait> SourceProperties for CdcProperties<T>
where
DebeziumCdcSplit<T>: TryFrom<SplitImpl, Error = crate::error::ConnectorError> + Into<SplitImpl>,
DebeziumSplitEnumerator<T>: ListCdcSplits<CdcSourceType = T> + enumerator::CdcMonitor,
{
type Split = DebeziumCdcSplit<T>;
type SplitEnumerator = DebeziumSplitEnumerator<T>;
type SplitReader = CdcSplitReader<T>;
const SOURCE_NAME: &'static str = T::CDC_CONNECTOR_NAME;
fn init_from_pb_source(&mut self, source: &PbSource) {
let pk_indices = source
.pk_column_ids
.iter()
.map(|&id| {
source
.columns
.iter()
.position(|col| col.column_desc.as_ref().unwrap().column_id == id)
.unwrap() as u32
})
.collect_vec();
let table_schema = PbTableSchema {
columns: source
.columns
.iter()
.flat_map(|col| &col.column_desc)
.filter(|col| {
!matches!(
col.generated_or_default_column,
Some(GeneratedOrDefaultColumn::GeneratedColumn(_))
)
})
.cloned()
.collect(),
pk_indices,
};
self.table_schema = table_schema;
if let Some(info) = source.info.as_ref() {
self.is_cdc_source_job = info.is_shared();
}
}
fn init_from_pb_cdc_table_desc(&mut self, table_desc: &ExternalTableDesc) {
let table_schema = TableSchema {
columns: table_desc
.columns
.iter()
.filter(|col| {
!matches!(
col.generated_or_default_column,
Some(GeneratedOrDefaultColumn::GeneratedColumn(_))
)
})
.cloned()
.collect(),
pk_indices: table_desc.stream_key.clone(),
};
self.table_schema = table_schema;
self.is_cdc_source_job = false;
self.is_backfill_table = true;
}
}
impl<T: CdcSourceTypeTrait> crate::source::UnknownFields for CdcProperties<T> {
fn unknown_fields(&self) -> HashMap<String, String> {
// FIXME: CDC does not handle unknown fields yet
HashMap::new()
}
}
impl<T: CdcSourceTypeTrait> CdcProperties<T> {
pub fn get_source_type_pb(&self) -> SourceType {
SourceType::from(T::source_type())
}
}
pub const INVALID_CDC_SPLIT_ASSIGNMENT_GENERATION_ID: u64 = 0;
pub const INITIAL_CDC_SPLIT_ASSIGNMENT_GENERATION_ID: u64 = 1;
#[derive(Clone, Debug, PartialEq, Default)]
pub struct CdcTableSnapshotSplitAssignmentWithGeneration {
pub splits: HashMap<ActorId, (Vec<CdcTableSnapshotSplitRaw>, u64)>,
}
impl CdcTableSnapshotSplitAssignmentWithGeneration {
pub fn new(splits: HashMap<ActorId, (Vec<CdcTableSnapshotSplitRaw>, u64)>) -> Self {
Self { splits }
}
pub fn empty() -> Self {
Self {
splits: HashMap::default(),
}
}
}
pub fn build_cdc_table_snapshot_split(s: &CdcTableSnapshotSplitRaw) -> PbCdcTableSnapshotSplit {
PbCdcTableSnapshotSplit {
split_id: s.split_id,
left_bound_inclusive: s.left_bound_inclusive.clone(),
right_bound_exclusive: s.right_bound_exclusive.clone(),
}
}
pub fn build_actor_cdc_table_snapshot_splits_with_generation(
pb_cdc_table_snapshot_split_assignment: PbCdcTableSnapshotSplitsWithGeneration,
) -> CdcTableSnapshotSplitAssignmentWithGeneration {
let splits = pb_cdc_table_snapshot_split_assignment
.splits
.into_iter()
.map(|(actor_id, splits)| {
let generation = splits.generation;
let splits = splits
.splits
.into_iter()
.map(|s| CdcTableSnapshotSplitRaw {
split_id: s.split_id,
left_bound_inclusive: s.left_bound_inclusive,
right_bound_exclusive: s.right_bound_exclusive,
})
.collect();
(actor_id, (splits, generation))
})
.collect();
CdcTableSnapshotSplitAssignmentWithGeneration { splits }
}
#[derive(Debug, Clone, Hash, PartialEq)]
pub struct CdcScanOptions {
/// Used by Used in non-parallel backfill, i.e. backfill V1.
pub disable_backfill: bool,
/// Used by non-parallelized backfill. The frequency of snapshot read resets for consuming the WAL backlog.
pub snapshot_barrier_interval: u32,
/// Used by non-parallelized backfill. The number of rows to fetch in a single batch when reading from an external table.
pub snapshot_batch_size: u32,
/// Used by parallelized backfill, i.e. backfill V2. The initial parallelism of parallel backfill.
pub backfill_parallelism: u32,
/// Used by parallelized backfill. The estimated number of rows per split used in splits generation.
pub backfill_num_rows_per_split: u64,
/// Used by parallelized backfill. For supported split column data type, assume an uniform distribution and adopt a much faster splits generation method.
pub backfill_as_even_splits: bool,
/// Used by parallelized backfill. Specify the index of primary key column to use as split column.
pub backfill_split_pk_column_index: u32,
/// If set, it will override `backfill_split_pk_column_index`.
pub backfill_split_column_name: Option<String>,
}
impl Default for CdcScanOptions {
fn default() -> Self {
Self {
disable_backfill: false,
snapshot_barrier_interval: 1,
snapshot_batch_size: 1000,
// 0 means disable backfill v2.
backfill_parallelism: 0,
backfill_num_rows_per_split: 100_000,
backfill_as_even_splits: true,
backfill_split_pk_column_index: 0,
backfill_split_column_name: None,
}
}
}
impl CdcScanOptions {
pub fn to_proto(&self) -> StreamCdcScanOptions {
StreamCdcScanOptions {
disable_backfill: self.disable_backfill,
snapshot_barrier_interval: self.snapshot_barrier_interval,
snapshot_batch_size: self.snapshot_batch_size,
backfill_parallelism: self.backfill_parallelism,
backfill_num_rows_per_split: self.backfill_num_rows_per_split,
backfill_as_even_splits: self.backfill_as_even_splits,
backfill_split_pk_column_index: self.backfill_split_pk_column_index,
backfill_split_column_name: self.backfill_split_column_name.clone(),
}
}
pub fn from_proto(proto: &StreamCdcScanOptions) -> Self {
Self {
disable_backfill: proto.disable_backfill,
snapshot_barrier_interval: proto.snapshot_barrier_interval,
snapshot_batch_size: proto.snapshot_batch_size,
backfill_parallelism: proto.backfill_parallelism,
backfill_num_rows_per_split: proto.backfill_num_rows_per_split,
backfill_as_even_splits: proto.backfill_as_even_splits,
backfill_split_pk_column_index: proto.backfill_split_pk_column_index,
backfill_split_column_name: proto.backfill_split_column_name.clone(),
}
}
pub fn is_parallelized_backfill(&self) -> bool {
!self.disable_backfill
&& self.backfill_num_rows_per_split > 0
&& self.backfill_parallelism > 0
}
}