Skip to main content

risingwave_connector/source/cdc/enumerator/
mod.rs

1// Copyright 2022 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::collections::BTreeMap;
16use std::marker::PhantomData;
17use std::ops::Deref;
18use std::sync::Arc;
19
20use anyhow::{Context, anyhow};
21use async_trait::async_trait;
22use mysql_async::Row;
23use mysql_async::prelude::*;
24use prost::Message;
25use risingwave_common::global_jvm::Jvm;
26use risingwave_common::id::SourceId;
27use risingwave_common::metrics::{LabelGuardedIntGauge, LabelGuardedIntGaugeVec};
28use risingwave_jni_core::call_static_method;
29use risingwave_jni_core::jvm_runtime::execute_with_jni_env;
30use risingwave_pb::connector_service::{SourceType, ValidateSourceRequest, ValidateSourceResponse};
31use tiberius::Config;
32use tokio_postgres::types::PgLsn;
33
34use crate::connector_common::{SslMode, create_pg_client, pg_connection_config_from_properties};
35use crate::error::ConnectorResult;
36use crate::sink::sqlserver::SqlServerClient;
37use crate::source::cdc::external::mysql::{
38    build_mariadb_connection_pool, build_mysql_connection_pool,
39};
40use crate::source::cdc::split::{extract_binlog_file_seq, parse_sql_server_lsn_str};
41use crate::source::cdc::{
42    CdcProperties, CdcSourceTypeTrait, DebeziumCdcSplit, Mariadb, Mongodb, Mysql, Oracle, Postgres,
43    SqlServer, table_schema_exclude_additional_columns,
44};
45use crate::source::monitor::metrics::EnumeratorMetrics;
46use crate::source::{SourceEnumeratorContextRef, SplitEnumerator};
47
48#[derive(Debug)]
49pub struct DebeziumSplitEnumerator<T: CdcSourceTypeTrait> {
50    /// The `source_id` in the catalog
51    source_id: SourceId,
52    metrics: Arc<EnumeratorMetrics>,
53    pg_cdc_upstream_max_lsn: Option<LabelGuardedIntGauge>,
54    pg_cdc_confirmed_flush_lsn: Option<LabelGuardedIntGauge>,
55    mysql_cdc_binlog_file_seq_min: Option<LabelGuardedIntGauge>,
56    mysql_cdc_binlog_file_seq_max: Option<LabelGuardedIntGauge>,
57    mariadb_cdc_binlog_file_seq_min: Option<LabelGuardedIntGauge>,
58    mariadb_cdc_binlog_file_seq_max: Option<LabelGuardedIntGauge>,
59    sqlserver_cdc_upstream_min_lsn: Option<LabelGuardedIntGauge>,
60    sqlserver_cdc_upstream_max_lsn: Option<LabelGuardedIntGauge>,
61    /// Properties specified in the WITH clause by user for database connection
62    properties: Arc<BTreeMap<String, String>>,
63    _phantom: PhantomData<T>,
64}
65
66fn get_or_create_guarded_int_gauge<'a>(
67    metric: &'a mut Option<LabelGuardedIntGauge>,
68    metric_vec: &LabelGuardedIntGaugeVec,
69    labels: &[String],
70) -> &'a LabelGuardedIntGauge {
71    metric.get_or_insert_with(|| metric_vec.with_guarded_label_values(labels))
72}
73
74#[async_trait]
75impl<T: CdcSourceTypeTrait> SplitEnumerator for DebeziumSplitEnumerator<T>
76where
77    Self: ListCdcSplits<CdcSourceType = T> + CdcMonitor,
78{
79    type Properties = CdcProperties<T>;
80    type Split = DebeziumCdcSplit<T>;
81
82    async fn new(
83        props: CdcProperties<T>,
84        context: SourceEnumeratorContextRef,
85    ) -> ConnectorResult<Self> {
86        assert_eq!(
87            props.get_source_type_pb(),
88            SourceType::from(T::source_type())
89        );
90
91        let jvm = Jvm::get_or_init()?;
92        let source_id = context.info.source_id;
93
94        // Extract fields before moving props
95        let source_type_pb = props.get_source_type_pb();
96
97        // Create Arc once and share it
98        let properties_arc = Arc::new(props.properties);
99        let properties_arc_for_validation = properties_arc.clone();
100        let table_schema_for_validation = props.table_schema;
101
102        tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
103            execute_with_jni_env(jvm, |env| {
104                let validate_source_request = ValidateSourceRequest {
105                    source_id: source_id.as_raw_id() as u64,
106                    source_type: source_type_pb as _,
107                    properties: (*properties_arc_for_validation).clone(),
108                    table_schema: Some(table_schema_exclude_additional_columns(
109                        &table_schema_for_validation,
110                    )),
111                    is_source_job: props.is_cdc_source_job,
112                    is_backfill_table: props.is_backfill_table,
113                };
114
115                let validate_source_request_bytes =
116                    env.byte_array_from_slice(&Message::encode_to_vec(&validate_source_request))?;
117
118                let validate_source_response_bytes = call_static_method!(
119                    env,
120                    {com.risingwave.connector.source.JniSourceValidateHandler},
121                    {byte[] validate(byte[] validateSourceRequestBytes)},
122                    &validate_source_request_bytes
123                )?;
124
125                let validate_source_response: ValidateSourceResponse = Message::decode(
126                    risingwave_jni_core::to_guarded_slice(&validate_source_response_bytes, env)?
127                        .deref(),
128                )?;
129
130                if let Some(error) = validate_source_response.error {
131                    return Err(anyhow!(error.error_message).context("source validation failed"));
132                }
133
134                Ok(())
135            })
136        })
137        .await
138        .context("failed to validate source")??;
139
140        tracing::debug!("validate cdc source properties success");
141        Ok(Self {
142            source_id,
143            metrics: context.metrics.clone(),
144            pg_cdc_upstream_max_lsn: None,
145            pg_cdc_confirmed_flush_lsn: None,
146            mysql_cdc_binlog_file_seq_min: None,
147            mysql_cdc_binlog_file_seq_max: None,
148            mariadb_cdc_binlog_file_seq_min: None,
149            mariadb_cdc_binlog_file_seq_max: None,
150            sqlserver_cdc_upstream_min_lsn: None,
151            sqlserver_cdc_upstream_max_lsn: None,
152            properties: properties_arc,
153            _phantom: PhantomData,
154        })
155    }
156
157    async fn list_splits(&mut self) -> ConnectorResult<Vec<DebeziumCdcSplit<T>>> {
158        Ok(self.list_cdc_splits())
159    }
160
161    async fn on_tick(&mut self) -> ConnectorResult<()> {
162        self.monitor_cdc().await
163    }
164}
165
166impl<T: CdcSourceTypeTrait> DebeziumSplitEnumerator<T> {
167    fn sql_server_lsn_to_i64(lsn: &str) -> Option<i64> {
168        parse_sql_server_lsn_str(lsn).map(|v| v.min(i64::MAX as u128) as i64)
169    }
170
171    fn pg_cdc_lsn_metric_labels(&self, slot_name: &str) -> Vec<String> {
172        vec![self.source_id.to_string(), slot_name.to_owned()]
173    }
174
175    async fn monitor_postgres_confirmed_flush_lsn(&mut self) -> ConnectorResult<()> {
176        // Query upstream LSNs and update metrics.
177        let lsns = self.query_postgres_lsns().await.with_context(|| {
178            format!(
179                "failed to query PostgreSQL LSNs for source {}",
180                self.source_id
181            )
182        })?;
183        match lsns {
184            Some((confirmed_flush_lsn, upstream_max_lsn, slot_name)) => {
185                let labels = self.pg_cdc_lsn_metric_labels(&slot_name);
186
187                get_or_create_guarded_int_gauge(
188                    &mut self.pg_cdc_upstream_max_lsn,
189                    &self.metrics.pg_cdc_upstream_max_lsn,
190                    &labels,
191                )
192                .set(upstream_max_lsn as i64);
193
194                if let Some(lsn) = confirmed_flush_lsn {
195                    get_or_create_guarded_int_gauge(
196                        &mut self.pg_cdc_confirmed_flush_lsn,
197                        &self.metrics.pg_cdc_confirmed_flush_lsn,
198                        &labels,
199                    )
200                    .set(lsn as i64);
201                    tracing::debug!(
202                        "Updated confirmed_flush_lsn for source {} slot {}: {}",
203                        self.source_id,
204                        slot_name,
205                        lsn
206                    );
207                } else {
208                    tracing::warn!(
209                        "confirmed_flush_lsn is NULL for source {} slot {}",
210                        self.source_id,
211                        slot_name
212                    );
213                }
214            }
215            None => {
216                tracing::warn!(
217                    "no replication slot was found while querying LSNs for source {}",
218                    self.source_id
219                );
220            }
221        };
222        Ok(())
223    }
224
225    /// Query LSNs from PostgreSQL, return (`confirmed_flush_lsn`, `upstream_max_lsn`, `slot_name`).
226    async fn query_postgres_lsns(&self) -> ConnectorResult<Option<(Option<u64>, u64, String)>> {
227        let pg_conn = pg_connection_config_from_properties(&self.properties)?;
228
229        let slot_name = self
230            .properties
231            .get("slot.name")
232            .ok_or_else(|| anyhow::anyhow!("missing `slot.name` in CDC properties"))?;
233
234        // No TCP keepalive for CDC enumerator
235        let application_name = format!("risingwave-postgres-source-enumerator-{}", self.source_id);
236        let client = create_pg_client(&pg_conn, None, Some(&application_name))
237            .await
238            .context("failed to create the PostgreSQL client")?;
239
240        let query = "SELECT confirmed_flush_lsn, pg_current_wal_lsn() \
241            FROM pg_replication_slots WHERE slot_name = $1";
242        let row = client
243            .query_opt(query, &[&slot_name])
244            .await
245            .context("failed to query PostgreSQL LSNs")?;
246        match row {
247            Some(row) => {
248                let confirmed_flush_lsn: Option<PgLsn> = row.get(0);
249                let upstream_max_lsn: PgLsn = row.get(1);
250                Ok(Some((
251                    confirmed_flush_lsn.map(Into::into),
252                    upstream_max_lsn.into(),
253                    slot_name.clone(),
254                )))
255            }
256            None => {
257                tracing::warn!("no replication slot found with name: {}", slot_name);
258                Ok(None)
259            }
260        }
261    }
262
263    /// Query min/max LSNs from SQL Server CDC.
264    async fn query_sql_server_lsns(&self) -> ConnectorResult<Option<(String, String)>> {
265        let hostname = self
266            .properties
267            .get("hostname")
268            .ok_or_else(|| anyhow!("missing `hostname` in CDC properties"))?;
269        let port = self
270            .properties
271            .get("port")
272            .ok_or_else(|| anyhow!("missing `port` in CDC properties"))?
273            .parse::<u16>()
274            .context("failed to parse `port` as a u16")?;
275        let username = self
276            .properties
277            .get("username")
278            .ok_or_else(|| anyhow!("missing `username` in CDC properties"))?;
279        let password = self
280            .properties
281            .get("password")
282            .ok_or_else(|| anyhow!("missing `password` in CDC properties"))?;
283        let database = self
284            .properties
285            .get("database.name")
286            .ok_or_else(|| anyhow!("missing `database.name` in CDC properties"))?;
287
288        let mut config = Config::new();
289        config.host(hostname);
290        config.port(port);
291        config.database(database);
292        config.authentication(tiberius::AuthMethod::sql_server(username, password));
293        config.trust_cert();
294
295        let mut client = SqlServerClient::new_with_config(config).await?;
296        let row = client
297            .inner_client
298            .simple_query(
299                "SELECT \
300                    sys.fn_cdc_get_max_lsn() AS max_lsn, \
301                    (SELECT MIN(sys.fn_cdc_get_min_lsn(capture_instance)) FROM cdc.change_tables) AS min_lsn"
302                    .to_owned(),
303            )
304            .await?
305            .into_row()
306            .await?
307            .ok_or_else(|| anyhow!("No result returned when querying SQL Server max/min LSN"))?;
308
309        let lsn_bytes_to_hex = |bytes: &[u8]| -> ConnectorResult<String> {
310            if bytes.len() != 10 {
311                return Err(anyhow!(
312                    "SQL Server LSN should be 10 bytes, got {} bytes",
313                    bytes.len()
314                )
315                .into());
316            }
317            let mut hex_string = String::with_capacity(22);
318            for byte in &bytes[0..4] {
319                hex_string.push_str(&format!("{:02x}", byte));
320            }
321            hex_string.push(':');
322            for byte in &bytes[4..8] {
323                hex_string.push_str(&format!("{:02x}", byte));
324            }
325            hex_string.push(':');
326            for byte in &bytes[8..10] {
327                hex_string.push_str(&format!("{:02x}", byte));
328            }
329            Ok(hex_string)
330        };
331
332        let max_lsn = row
333            .try_get::<&[u8], usize>(0)?
334            .map(lsn_bytes_to_hex)
335            .transpose()?
336            .ok_or_else(|| anyhow!("SQL Server max_lsn is NULL"))?;
337        let min_lsn = row
338            .try_get::<&[u8], usize>(1)?
339            .map(lsn_bytes_to_hex)
340            .transpose()?
341            .ok_or_else(|| anyhow!("SQL Server min_lsn is NULL"))?;
342
343        Ok(Some((min_lsn, max_lsn)))
344    }
345
346    async fn monitor_sql_server_lsns(&mut self) -> ConnectorResult<()> {
347        let lsns = self.query_sql_server_lsns().await.with_context(|| {
348            format!(
349                "failed to query SQL Server LSNs for source {}",
350                self.source_id
351            )
352        })?;
353        if let Some((min_lsn, max_lsn)) = lsns {
354            let labels = vec![self.source_id.to_string()];
355
356            if let Some(value) = Self::sql_server_lsn_to_i64(&min_lsn) {
357                get_or_create_guarded_int_gauge(
358                    &mut self.sqlserver_cdc_upstream_min_lsn,
359                    &self.metrics.sqlserver_cdc_upstream_min_lsn,
360                    &labels,
361                )
362                .set(value);
363            }
364            if let Some(value) = Self::sql_server_lsn_to_i64(&max_lsn) {
365                get_or_create_guarded_int_gauge(
366                    &mut self.sqlserver_cdc_upstream_max_lsn,
367                    &self.metrics.sqlserver_cdc_upstream_max_lsn,
368                    &labels,
369                )
370                .set(value);
371            }
372        }
373
374        Ok(())
375    }
376}
377
378pub trait ListCdcSplits {
379    type CdcSourceType: CdcSourceTypeTrait;
380    /// Generates a single split for shared source.
381    fn list_cdc_splits(&mut self) -> Vec<DebeziumCdcSplit<Self::CdcSourceType>>;
382}
383
384/// Trait for CDC-specific monitoring behavior
385#[async_trait]
386pub trait CdcMonitor {
387    async fn monitor_cdc(&mut self) -> ConnectorResult<()>;
388}
389
390#[async_trait]
391impl<T: CdcSourceTypeTrait> CdcMonitor for DebeziumSplitEnumerator<T> {
392    default async fn monitor_cdc(&mut self) -> ConnectorResult<()> {
393        Ok(())
394    }
395}
396
397impl DebeziumSplitEnumerator<Mysql> {
398    async fn monitor_mysql_binlog_files(&mut self) -> ConnectorResult<()> {
399        // Get hostname and port for metrics labels
400        let hostname = self
401            .properties
402            .get("hostname")
403            .map(|s| s.as_str())
404            .ok_or_else(|| {
405                anyhow::anyhow!("missing required property 'hostname' for MySQL CDC source")
406            })?;
407        let port = self
408            .properties
409            .get("port")
410            .map(|s| s.as_str())
411            .ok_or_else(|| {
412                anyhow::anyhow!("missing required property 'port' for MySQL CDC source")
413            })?;
414
415        // Query binlog files and update metrics
416        let binlog_files = self.query_binlog_files().await.with_context(|| {
417            format!(
418                "failed to query binlog files for MySQL CDC source {} ({}:{})",
419                self.source_id, hostname, port
420            )
421        })?;
422        if let Some((oldest_file, oldest_size)) = binlog_files.first()
423            && let Some(seq) = extract_binlog_file_seq(oldest_file)
424        {
425            let labels = vec![
426                self.source_id.to_string(),
427                hostname.to_owned(),
428                port.to_owned(),
429            ];
430            get_or_create_guarded_int_gauge(
431                &mut self.mysql_cdc_binlog_file_seq_min,
432                &self.metrics.mysql_cdc_binlog_file_seq_min,
433                &labels,
434            )
435            .set(seq as i64);
436            tracing::debug!(
437                "MySQL CDC source {} ({}:{}): oldest binlog = {}, seq = {}, size = {}",
438                self.source_id,
439                hostname,
440                port,
441                oldest_file,
442                seq,
443                oldest_size
444            );
445        }
446        if let Some((newest_file, newest_size)) = binlog_files.last()
447            && let Some(seq) = extract_binlog_file_seq(newest_file)
448        {
449            let labels = vec![
450                self.source_id.to_string(),
451                hostname.to_owned(),
452                port.to_owned(),
453            ];
454            get_or_create_guarded_int_gauge(
455                &mut self.mysql_cdc_binlog_file_seq_max,
456                &self.metrics.mysql_cdc_binlog_file_seq_max,
457                &labels,
458            )
459            .set(seq as i64);
460            tracing::debug!(
461                "MySQL CDC source {} ({}:{}): newest binlog = {}, seq = {}, size = {}",
462                self.source_id,
463                hostname,
464                port,
465                newest_file,
466                seq,
467                newest_size
468            );
469        }
470        tracing::debug!(
471            "MySQL CDC source {} ({}:{}): total {} binlog files",
472            self.source_id,
473            hostname,
474            port,
475            binlog_files.len()
476        );
477        Ok(())
478    }
479
480    /// Query binlog files from MySQL, returns Vec<(filename, size)>
481    async fn query_binlog_files(&self) -> ConnectorResult<Vec<(String, u64)>> {
482        // Extract connection parameters from CDC properties
483        let hostname = self
484            .properties
485            .get("hostname")
486            .ok_or_else(|| anyhow::anyhow!("missing `hostname` in CDC properties"))?;
487        let port = self
488            .properties
489            .get("port")
490            .ok_or_else(|| anyhow::anyhow!("missing `port` in CDC properties"))?
491            .parse::<u16>()
492            .context("failed to parse `port` as a u16")?;
493        let username = self
494            .properties
495            .get("username")
496            .ok_or_else(|| anyhow::anyhow!("missing `username` in CDC properties"))?;
497        let password = self
498            .properties
499            .get("password")
500            .ok_or_else(|| anyhow::anyhow!("missing `password` in CDC properties"))?;
501        // Get SSL mode configuration (default to Disabled if not specified)
502        let ssl_mode = self
503            .properties
504            .get("ssl.mode")
505            .and_then(|s| s.parse().ok())
506            .unwrap_or(SslMode::Preferred);
507
508        // SHOW BINARY LOGS is server-wide. Do not select a default database: database.name
509        // can be a comma-separated list for a shared source.
510        let pool = build_mysql_connection_pool(hostname, port, username, password, None, ssl_mode);
511        let mut conn = pool
512            .get_conn()
513            .await
514            .context("failed to connect to MySQL")?;
515
516        // Query binlog files using SHOW BINARY LOGS.
517        // MySQL 8.0+ may return 3 columns (Log_name, File_size, Encrypted), while some variants
518        // only return the first 2. Decode the row manually so we don't panic on column-count
519        // differences.
520        let rows: Vec<Row> = conn
521            .query("SHOW BINARY LOGS")
522            .await
523            .context("failed to execute `SHOW BINARY LOGS`")?;
524        let query_result = rows
525            .into_iter()
526            .map(|mut row| -> ConnectorResult<(String, u64)> {
527                let log_name = row
528                    .take_opt::<String, _>(0)
529                    .transpose()
530                    .context("`SHOW BINARY LOGS`: failed to decode `Log_name`")?
531                    .ok_or_else(|| anyhow!("`SHOW BINARY LOGS`: missing `Log_name` column"))?;
532                let file_size = row
533                    .take_opt::<u64, _>(1)
534                    .transpose()
535                    .context("`SHOW BINARY LOGS`: failed to decode `File_size`")?
536                    .ok_or_else(|| anyhow!("`SHOW BINARY LOGS`: missing `File_size` column"))?;
537                Ok((log_name, file_size))
538            })
539            .collect::<ConnectorResult<Vec<_>>>()?;
540
541        drop(conn);
542        pool.disconnect().await.ok();
543
544        Ok(query_result)
545    }
546}
547
548impl ListCdcSplits for DebeziumSplitEnumerator<Mysql> {
549    type CdcSourceType = Mysql;
550
551    fn list_cdc_splits(&mut self) -> Vec<DebeziumCdcSplit<Self::CdcSourceType>> {
552        // CDC source only supports single split
553        vec![DebeziumCdcSplit::<Self::CdcSourceType>::new(
554            self.source_id.as_raw_id(),
555            None,
556        )]
557    }
558}
559
560impl ListCdcSplits for DebeziumSplitEnumerator<Mariadb> {
561    type CdcSourceType = Mariadb;
562
563    fn list_cdc_splits(&mut self) -> Vec<DebeziumCdcSplit<Self::CdcSourceType>> {
564        vec![DebeziumCdcSplit::<Self::CdcSourceType>::new(
565            self.source_id.as_raw_id(),
566            None,
567        )]
568    }
569}
570
571impl DebeziumSplitEnumerator<Mariadb> {
572    async fn monitor_mariadb_binlog_files(&mut self) -> ConnectorResult<()> {
573        let hostname = self.properties.get("hostname").ok_or_else(|| {
574            anyhow!("missing required property 'hostname' for MariaDB CDC source")
575        })?;
576        let port = self
577            .properties
578            .get("port")
579            .ok_or_else(|| anyhow!("missing required property 'port' for MariaDB CDC source"))?;
580        let binlog_files = query_mariadb_binlog_files(&self.properties)
581            .await
582            .with_context(|| {
583                format!(
584                    "failed to query binlog files for MariaDB CDC source {} ({}:{})",
585                    self.source_id, hostname, port
586                )
587            })?;
588        let labels = vec![
589            self.source_id.to_string(),
590            hostname.to_owned(),
591            port.to_owned(),
592        ];
593        if let Some((oldest_file, _)) = binlog_files.first()
594            && let Some(seq) = extract_binlog_file_seq(oldest_file)
595        {
596            get_or_create_guarded_int_gauge(
597                &mut self.mariadb_cdc_binlog_file_seq_min,
598                &self.metrics.mariadb_cdc_binlog_file_seq_min,
599                &labels,
600            )
601            .set(seq as i64);
602        }
603        if let Some((newest_file, _)) = binlog_files.last()
604            && let Some(seq) = extract_binlog_file_seq(newest_file)
605        {
606            get_or_create_guarded_int_gauge(
607                &mut self.mariadb_cdc_binlog_file_seq_max,
608                &self.metrics.mariadb_cdc_binlog_file_seq_max,
609                &labels,
610            )
611            .set(seq as i64);
612        }
613        Ok(())
614    }
615}
616
617async fn query_mariadb_binlog_files(
618    properties: &BTreeMap<String, String>,
619) -> ConnectorResult<Vec<(String, u64)>> {
620    let hostname = properties
621        .get("hostname")
622        .ok_or_else(|| anyhow!("missing `hostname` in CDC properties"))?;
623    let port = properties
624        .get("port")
625        .ok_or_else(|| anyhow!("missing `port` in CDC properties"))?
626        .parse::<u16>()
627        .context("failed to parse `port` as a u16")?;
628    let username = properties
629        .get("username")
630        .ok_or_else(|| anyhow!("missing `username` in CDC properties"))?;
631    let password = properties
632        .get("password")
633        .ok_or_else(|| anyhow!("missing `password` in CDC properties"))?;
634    let ssl_mode = properties
635        .get("ssl.mode")
636        .or_else(|| properties.get("debezium.database.sslmode"))
637        .and_then(|s| s.parse().ok())
638        .unwrap_or(SslMode::Preferred);
639    let ssl_root_cert = properties
640        .get("ssl.root.cert")
641        .or_else(|| properties.get("debezium.database.sslrootcert"))
642        .map(String::as_str);
643    // SHOW BINARY LOGS is server-wide. `database.name` can contain multiple databases.
644    let pool = build_mariadb_connection_pool(
645        hostname,
646        port,
647        username,
648        password,
649        None,
650        ssl_mode,
651        ssl_root_cert,
652    );
653    let mut conn = pool
654        .get_conn()
655        .await
656        .context("failed to connect to MariaDB")?;
657    let rows: Vec<Row> = conn
658        .query("SHOW BINARY LOGS")
659        .await
660        .context("failed to execute `SHOW BINARY LOGS`")?;
661    let result = rows
662        .into_iter()
663        .map(|mut row| -> ConnectorResult<(String, u64)> {
664            let name = row
665                .take_opt::<String, _>(0)
666                .transpose()
667                .context("failed to decode MariaDB binlog name")?
668                .ok_or_else(|| anyhow!("missing MariaDB binlog name"))?;
669            let size = row
670                .take_opt::<u64, _>(1)
671                .transpose()
672                .context("failed to decode MariaDB binlog size")?
673                .ok_or_else(|| anyhow!("missing MariaDB binlog size"))?;
674            Ok((name, size))
675        })
676        .collect::<ConnectorResult<Vec<_>>>()?;
677    drop(conn);
678    pool.disconnect().await.ok();
679    Ok(result)
680}
681
682#[async_trait]
683impl CdcMonitor for DebeziumSplitEnumerator<Mariadb> {
684    async fn monitor_cdc(&mut self) -> ConnectorResult<()> {
685        self.monitor_mariadb_binlog_files().await
686    }
687}
688
689#[async_trait]
690impl CdcMonitor for DebeziumSplitEnumerator<Mysql> {
691    async fn monitor_cdc(&mut self) -> ConnectorResult<()> {
692        // For MySQL CDC, query the upstream MySQL binlog files and monitor them.
693        self.monitor_mysql_binlog_files().await?;
694        Ok(())
695    }
696}
697
698impl ListCdcSplits for DebeziumSplitEnumerator<Postgres> {
699    type CdcSourceType = Postgres;
700
701    fn list_cdc_splits(&mut self) -> Vec<DebeziumCdcSplit<Self::CdcSourceType>> {
702        // CDC source only supports single split
703        vec![DebeziumCdcSplit::<Self::CdcSourceType>::new(
704            self.source_id.as_raw_id(),
705            None,
706        )]
707    }
708}
709
710#[async_trait]
711impl CdcMonitor for DebeziumSplitEnumerator<Postgres> {
712    async fn monitor_cdc(&mut self) -> ConnectorResult<()> {
713        // For PostgreSQL CDC, query the upstream Postgres confirmed flush lsn and monitor it.
714        self.monitor_postgres_confirmed_flush_lsn().await?;
715        Ok(())
716    }
717}
718
719impl ListCdcSplits for DebeziumSplitEnumerator<Mongodb> {
720    type CdcSourceType = Mongodb;
721
722    fn list_cdc_splits(&mut self) -> Vec<DebeziumCdcSplit<Self::CdcSourceType>> {
723        // CDC source only supports single split
724        vec![DebeziumCdcSplit::<Self::CdcSourceType>::new(
725            self.source_id.as_raw_id(),
726            None,
727        )]
728    }
729}
730
731impl ListCdcSplits for DebeziumSplitEnumerator<SqlServer> {
732    type CdcSourceType = SqlServer;
733
734    fn list_cdc_splits(&mut self) -> Vec<DebeziumCdcSplit<Self::CdcSourceType>> {
735        vec![DebeziumCdcSplit::<Self::CdcSourceType>::new(
736            self.source_id.as_raw_id(),
737            None,
738        )]
739    }
740}
741
742impl ListCdcSplits for DebeziumSplitEnumerator<Oracle> {
743    type CdcSourceType = Oracle;
744
745    fn list_cdc_splits(&mut self) -> Vec<DebeziumCdcSplit<Self::CdcSourceType>> {
746        vec![DebeziumCdcSplit::<Self::CdcSourceType>::new(
747            self.source_id.as_raw_id(),
748            None,
749        )]
750    }
751}
752
753#[async_trait]
754impl CdcMonitor for DebeziumSplitEnumerator<SqlServer> {
755    async fn monitor_cdc(&mut self) -> ConnectorResult<()> {
756        self.monitor_sql_server_lsns().await
757    }
758}
759
760#[cfg(test)]
761mod tests {
762    use prometheus::core::Collector;
763    use risingwave_common::metrics::LabelGuardedIntGaugeVec;
764
765    use super::get_or_create_guarded_int_gauge;
766
767    #[test]
768    fn cached_guarded_metric_survives_repeated_collections() {
769        let metric_vec = LabelGuardedIntGaugeVec::test_int_gauge_vec::<2>();
770        let labels = vec!["source_id".to_owned(), "slot_name".to_owned()];
771        let mut metric = None;
772
773        get_or_create_guarded_int_gauge(&mut metric, &metric_vec, &labels).set(1);
774
775        assert_eq!(1, metric_vec.collect().pop().unwrap().get_metric().len());
776        assert_eq!(1, metric_vec.collect().pop().unwrap().get_metric().len());
777
778        drop(metric);
779        assert_eq!(1, metric_vec.collect().pop().unwrap().get_metric().len());
780        assert_eq!(0, metric_vec.collect().pop().unwrap().get_metric().len());
781    }
782}