use std::time::Duration;
use faucet_common_oracle::{OracleConnectionConfig, quote_ident_oracle, split_table};
use faucet_core::{DEFAULT_BATCH_SIZE, FaucetError, validate_batch_size};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
fn default_poll_interval() -> Duration {
Duration::from_secs(1)
}
fn default_idle_timeout() -> Duration {
Duration::from_secs(30)
}
fn default_batch_size() -> usize {
DEFAULT_BATCH_SIZE
}
fn default_max_connections() -> u32 {
2
}
fn default_statement_timeout_secs() -> u64 {
600
}
fn default_max_scn_window() -> u64 {
500_000
}
fn default_flush_table() -> String {
"FAUCET_LOGMNR_FLUSH".into()
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum StartPosition {
#[default]
Current,
Earliest,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum OnUnsupported {
#[default]
Fail,
Skip,
}
#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[schemars(extend("x-faucet-follows-servers" = true))]
pub struct OracleCdcSourceConfig {
#[serde(flatten)]
pub connection: OracleConnectionConfig,
pub tables: Vec<String>,
#[serde(default)]
pub start_position: StartPosition,
#[serde(
default = "default_poll_interval",
with = "faucet_core::config::duration_secs"
)]
#[schemars(with = "u64")]
pub poll_interval: Duration,
#[serde(
default = "default_idle_timeout",
with = "faucet_core::config::duration_secs"
)]
#[schemars(with = "u64")]
pub idle_timeout: Duration,
#[serde(default = "default_max_scn_window")]
pub max_scn_window: u64,
#[serde(default = "faucet_core::replication::default_max_staged_records")]
#[schemars(extend("x-faucet-caller-max" = 1_000_000))]
pub max_staged_records: Option<usize>,
#[serde(default)]
#[schemars(extend("x-faucet-caller-max" = 1_000_000, "x-faucet-caller-set" = true))]
pub max_staged_records_total: Option<usize>,
#[serde(default = "default_batch_size")]
pub batch_size: usize,
#[serde(default = "default_max_connections")]
pub max_connections: u32,
#[serde(default = "default_statement_timeout_secs")]
pub statement_timeout_secs: u64,
#[serde(default = "default_flush_table")]
pub flush_table: String,
#[serde(default)]
pub on_unsupported: OnUnsupported,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub state_key: Option<String>,
}
impl std::fmt::Debug for OracleCdcSourceConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OracleCdcSourceConfig")
.field("connection", &self.connection)
.field("tables", &self.tables)
.field("start_position", &self.start_position)
.field("poll_interval", &self.poll_interval)
.field("idle_timeout", &self.idle_timeout)
.field("max_scn_window", &self.max_scn_window)
.field("max_staged_records", &self.max_staged_records)
.field("max_staged_records_total", &self.max_staged_records_total)
.field("batch_size", &self.batch_size)
.field("max_connections", &self.max_connections)
.field("statement_timeout_secs", &self.statement_timeout_secs)
.field("flush_table", &self.flush_table)
.field("on_unsupported", &self.on_unsupported)
.field("state_key", &self.state_key)
.finish()
}
}
impl OracleCdcSourceConfig {
pub fn new(connection: OracleConnectionConfig, tables: Vec<String>) -> Self {
Self {
connection,
tables,
start_position: StartPosition::default(),
poll_interval: default_poll_interval(),
idle_timeout: default_idle_timeout(),
max_scn_window: default_max_scn_window(),
max_staged_records: faucet_core::replication::default_max_staged_records(),
max_staged_records_total: None,
batch_size: default_batch_size(),
max_connections: default_max_connections(),
statement_timeout_secs: default_statement_timeout_secs(),
flush_table: default_flush_table(),
on_unsupported: OnUnsupported::default(),
state_key: None,
}
}
pub fn validate(&self) -> Result<(), FaucetError> {
self.connection.validate()?;
if self.tables.is_empty() {
return Err(FaucetError::Config(
"oracle-cdc: `tables` must list at least one OWNER.TABLE".into(),
));
}
let mut seen = std::collections::BTreeSet::new();
for t in &self.tables {
let (owner, name) = split_table(t)?;
let Some(owner) = owner else {
return Err(FaucetError::Config(format!(
"oracle-cdc: table {t:?} must be owner-qualified (OWNER.TABLE)"
)));
};
quote_ident_oracle(&owner)?;
quote_ident_oracle(&name)?;
if !seen.insert(t) {
return Err(FaucetError::Config(format!(
"oracle-cdc: duplicate table {t:?} in `tables`"
)));
}
}
quote_ident_oracle(&self.flush_table)?;
if self.poll_interval.is_zero() || self.idle_timeout.is_zero() {
return Err(FaucetError::Config(
"oracle-cdc: poll_interval and idle_timeout must be > 0".into(),
));
}
if self.max_scn_window == 0 {
return Err(FaucetError::Config(
"oracle-cdc: max_scn_window must be > 0".into(),
));
}
validate_batch_size(self.batch_size)?;
faucet_core::state::validate_state_key(&self.resolved_state_key())?;
Ok(())
}
pub fn table_pairs(&self) -> Vec<(String, String)> {
self.tables
.iter()
.filter_map(|t| match split_table(t) {
Ok((Some(o), n)) => Some((o, n)),
_ => None,
})
.collect()
}
pub fn resolved_state_key(&self) -> String {
if let Some(k) = &self.state_key {
return k.clone();
}
self.key_for_scope(self.connection.scope_label())
}
pub fn legacy_state_keys(&self) -> Vec<String> {
if self.state_key.is_some() {
return Vec::new();
}
self.connection
.legacy_scope_label()
.map(|scope| vec![self.key_for_scope(scope)])
.unwrap_or_default()
}
fn key_for_scope(&self, scope: String) -> String {
let scope = if scope.is_empty() {
"oracle".into()
} else {
scope
};
if self.tables.len() == 1 {
let t: String = self.tables[0]
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.') {
c
} else {
'_'
}
})
.collect();
return format!("oracle-cdc:{scope}:{t}");
}
let mut sorted: Vec<&str> = self.tables.iter().map(String::as_str).collect();
sorted.sort_unstable();
format!("oracle-cdc:{scope}:{}", fnv1a_hex(&sorted.join(",")))
}
}
fn fnv1a_hex(s: &str) -> String {
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
for b in s.bytes() {
hash ^= u64::from(b);
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
format!("{hash:016x}")
}
#[cfg(test)]
mod tests;