risingwave_connector/source/cdc/
split.rs

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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2024 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.

use std::marker::PhantomData;

use anyhow::Context;
use risingwave_common::types::JsonbVal;
use serde::{Deserialize, Serialize};

use crate::error::ConnectorResult;
use crate::source::cdc::external::DebeziumOffset;
use crate::source::cdc::{CdcSourceType, CdcSourceTypeTrait};
use crate::source::{SplitId, SplitMetaData};

/// The base states of a CDC split, which will be persisted to checkpoint.
/// CDC source only has single split, so we use the `source_id` to identify the split.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Hash)]
pub struct CdcSplitBase {
    pub split_id: u32,
    pub start_offset: Option<String>,
    pub snapshot_done: bool,
}

impl CdcSplitBase {
    pub fn new(split_id: u32, start_offset: Option<String>) -> Self {
        Self {
            split_id,
            start_offset,
            snapshot_done: false,
        }
    }
}

trait CdcSplitTrait: Send + Sync {
    fn split_id(&self) -> u32;
    fn start_offset(&self) -> &Option<String>;
    fn is_snapshot_done(&self) -> bool;
    fn update_offset(&mut self, last_seen_offset: String) -> ConnectorResult<()>;

    // MySQL and MongoDB shares the same logic to extract the snapshot flag
    fn extract_snapshot_flag(&self, start_offset: &str) -> ConnectorResult<bool> {
        // if snapshot_done is already true, it won't be changed
        let mut snapshot_done = self.is_snapshot_done();
        if snapshot_done {
            return Ok(snapshot_done);
        }

        let dbz_offset: DebeziumOffset = serde_json::from_str(start_offset).with_context(|| {
            format!(
                "invalid cdc offset: {}, split: {}",
                start_offset,
                self.split_id()
            )
        })?;

        // heartbeat event should not update the `snapshot_done` flag
        if !dbz_offset.is_heartbeat {
            snapshot_done = match dbz_offset.source_offset.snapshot {
                Some(val) => !val,
                None => true,
            };
        }
        Ok(snapshot_done)
    }
}

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Hash)]
pub struct MySqlCdcSplit {
    pub inner: CdcSplitBase,
}

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Hash)]
pub struct PostgresCdcSplit {
    pub inner: CdcSplitBase,
    // the hostname and port of a node that holding shard tables (for Citus)
    pub server_addr: Option<String>,
}

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Hash)]
pub struct MongoDbCdcSplit {
    pub inner: CdcSplitBase,
}

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Hash)]
pub struct SqlServerCdcSplit {
    pub inner: CdcSplitBase,
}

impl MySqlCdcSplit {
    pub fn new(split_id: u32, start_offset: Option<String>) -> Self {
        let split = CdcSplitBase {
            split_id,
            start_offset,
            snapshot_done: false,
        };
        Self { inner: split }
    }
}

impl CdcSplitTrait for MySqlCdcSplit {
    fn split_id(&self) -> u32 {
        self.inner.split_id
    }

    fn start_offset(&self) -> &Option<String> {
        &self.inner.start_offset
    }

    fn is_snapshot_done(&self) -> bool {
        self.inner.snapshot_done
    }

    fn update_offset(&mut self, last_seen_offset: String) -> ConnectorResult<()> {
        // if snapshot_done is already true, it won't be updated
        self.inner.snapshot_done = self.extract_snapshot_flag(last_seen_offset.as_str())?;
        self.inner.start_offset = Some(last_seen_offset);
        Ok(())
    }
}

impl PostgresCdcSplit {
    pub fn new(split_id: u32, start_offset: Option<String>, server_addr: Option<String>) -> Self {
        let split = CdcSplitBase {
            split_id,
            start_offset,
            snapshot_done: false,
        };
        Self {
            inner: split,
            server_addr,
        }
    }
}

impl CdcSplitTrait for PostgresCdcSplit {
    fn split_id(&self) -> u32 {
        self.inner.split_id
    }

    fn start_offset(&self) -> &Option<String> {
        &self.inner.start_offset
    }

    fn is_snapshot_done(&self) -> bool {
        self.inner.snapshot_done
    }

    fn update_offset(&mut self, last_seen_offset: String) -> ConnectorResult<()> {
        self.inner.snapshot_done = self.extract_snapshot_flag(last_seen_offset.as_str())?;
        self.inner.start_offset = Some(last_seen_offset);
        Ok(())
    }

    fn extract_snapshot_flag(&self, start_offset: &str) -> ConnectorResult<bool> {
        // if snapshot_done is already true, it won't be changed
        let mut snapshot_done = self.is_snapshot_done();
        if snapshot_done {
            return Ok(snapshot_done);
        }

        let dbz_offset: DebeziumOffset = serde_json::from_str(start_offset).with_context(|| {
            format!(
                "invalid postgres offset: {}, split: {}",
                start_offset, self.inner.split_id
            )
        })?;

        // heartbeat event should not update the `snapshot_done` flag
        if !dbz_offset.is_heartbeat {
            snapshot_done = dbz_offset
                .source_offset
                .last_snapshot_record
                .unwrap_or(false);
        }
        Ok(snapshot_done)
    }
}

impl MongoDbCdcSplit {
    pub fn new(split_id: u32, start_offset: Option<String>) -> Self {
        let split = CdcSplitBase {
            split_id,
            start_offset,
            snapshot_done: false,
        };
        Self { inner: split }
    }
}

impl CdcSplitTrait for MongoDbCdcSplit {
    fn split_id(&self) -> u32 {
        self.inner.split_id
    }

    fn start_offset(&self) -> &Option<String> {
        &self.inner.start_offset
    }

    fn is_snapshot_done(&self) -> bool {
        self.inner.snapshot_done
    }

    fn update_offset(&mut self, last_seen_offset: String) -> ConnectorResult<()> {
        // if snapshot_done is already true, it will remain true
        self.inner.snapshot_done = self.extract_snapshot_flag(last_seen_offset.as_str())?;
        self.inner.start_offset = Some(last_seen_offset);
        Ok(())
    }
}

impl SqlServerCdcSplit {
    pub fn new(split_id: u32, start_offset: Option<String>) -> Self {
        let split = CdcSplitBase {
            split_id,
            start_offset,
            snapshot_done: false,
        };
        Self { inner: split }
    }
}

impl CdcSplitTrait for SqlServerCdcSplit {
    fn split_id(&self) -> u32 {
        self.inner.split_id
    }

    fn start_offset(&self) -> &Option<String> {
        &self.inner.start_offset
    }

    fn is_snapshot_done(&self) -> bool {
        self.inner.snapshot_done
    }

    fn update_offset(&mut self, last_seen_offset: String) -> ConnectorResult<()> {
        // if snapshot_done is already true, it will remain true
        self.inner.snapshot_done = self.extract_snapshot_flag(last_seen_offset.as_str())?;
        self.inner.start_offset = Some(last_seen_offset);
        Ok(())
    }
}

/// We use this struct to wrap the specific split, which act as an interface to other modules
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Hash)]
pub struct DebeziumCdcSplit<T: CdcSourceTypeTrait> {
    pub mysql_split: Option<MySqlCdcSplit>,

    #[serde(rename = "pg_split")] // backward compatibility
    pub postgres_split: Option<PostgresCdcSplit>,
    pub citus_split: Option<PostgresCdcSplit>,
    pub mongodb_split: Option<MongoDbCdcSplit>,
    pub sql_server_split: Option<SqlServerCdcSplit>,

    #[serde(skip)]
    pub _phantom: PhantomData<T>,
}

macro_rules! dispatch_cdc_split_inner {
    ($dbz_split:expr, $as_type:tt, {$({$cdc_source_type:tt, $cdc_source_split:tt}),*}, $body:expr) => {
        match T::source_type() {
            $(
                CdcSourceType::$cdc_source_type => {
                    $crate::paste! {
                        $dbz_split.[<$cdc_source_split>]
                            .[<as_ $as_type>]()
                            .expect(concat!(stringify!([<$cdc_source_type:lower>]), " split must exist"))
                            .$body
                    }
                }
            )*
            CdcSourceType::Unspecified => {
                unreachable!("invalid debezium split");
            }
        }
    }
}

// call corresponding split method of the specific cdc source type
macro_rules! dispatch_cdc_split {
    ($dbz_split:expr, $as_type:tt, $body:expr) => {
        dispatch_cdc_split_inner!($dbz_split, $as_type, {
            {Mysql, mysql_split},
            {Postgres, postgres_split},
            {Citus, citus_split},
            {Mongodb, mongodb_split},
            {SqlServer, sql_server_split}
        }, $body)
    }
}

impl<T: CdcSourceTypeTrait> SplitMetaData for DebeziumCdcSplit<T> {
    fn id(&self) -> SplitId {
        format!("{}", self.split_id()).into()
    }

    fn encode_to_json(&self) -> JsonbVal {
        serde_json::to_value(self.clone()).unwrap().into()
    }

    fn restore_from_json(value: JsonbVal) -> ConnectorResult<Self> {
        serde_json::from_value(value.take()).map_err(Into::into)
    }

    fn update_offset(&mut self, last_seen_offset: String) -> ConnectorResult<()> {
        self.update_offset_inner(last_seen_offset)
    }
}

impl<T: CdcSourceTypeTrait> DebeziumCdcSplit<T> {
    pub fn new(split_id: u32, start_offset: Option<String>, server_addr: Option<String>) -> Self {
        let mut ret = Self {
            mysql_split: None,
            postgres_split: None,
            citus_split: None,
            mongodb_split: None,
            sql_server_split: None,
            _phantom: PhantomData,
        };
        match T::source_type() {
            CdcSourceType::Mysql => {
                let split = MySqlCdcSplit::new(split_id, start_offset);
                ret.mysql_split = Some(split);
            }
            CdcSourceType::Postgres => {
                let split = PostgresCdcSplit::new(split_id, start_offset, None);
                ret.postgres_split = Some(split);
            }
            CdcSourceType::Citus => {
                let split = PostgresCdcSplit::new(split_id, start_offset, server_addr);
                ret.citus_split = Some(split);
            }
            CdcSourceType::Mongodb => {
                let split = MongoDbCdcSplit::new(split_id, start_offset);
                ret.mongodb_split = Some(split);
            }
            CdcSourceType::SqlServer => {
                let split = SqlServerCdcSplit::new(split_id, start_offset);
                ret.sql_server_split = Some(split);
            }
            CdcSourceType::Unspecified => {
                unreachable!("invalid debezium split")
            }
        }
        ret
    }

    pub fn split_id(&self) -> u32 {
        dispatch_cdc_split!(self, ref, split_id())
    }

    pub fn start_offset(&self) -> &Option<String> {
        dispatch_cdc_split!(self, ref, start_offset())
    }

    pub fn snapshot_done(&self) -> bool {
        dispatch_cdc_split!(self, ref, is_snapshot_done())
    }

    pub fn update_offset_inner(&mut self, last_seen_offset: String) -> ConnectorResult<()> {
        dispatch_cdc_split!(self, mut, update_offset(last_seen_offset)?);
        Ok(())
    }
}