Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 142 additions & 134 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ unused_async = "warn"

[dependencies]
wal-rus = { version = "0.4.2", default-features = false, features = ["s3", "gcs", "backup"] }
clickhouse-c-rs = { version = "0.2.2", default-features = false, features = ["tokio", "tls"] }
clickhouse-c-rs = { version = "0.3.0", default-features = false, features = ["tokio", "tls"] }
# hash keys are internal (relfilenodes, oids, xids, names), never network
# input, so SipHash's HashDoS margin buys nothing at 2-4x per probe
foldhash = "0.2"
Expand Down Expand Up @@ -93,7 +93,7 @@ opentelemetry_sdk = { version = "0.33", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "0.33", default-features = false, features = ["grpc-tonic", "trace"] }
tracing-opentelemetry = { version = "0.34", default-features = false }
mimalloc = "0.1.52"
prometheus-client = "0.25.1"
prometheus-client = "0.26"

# `harness = false` throughout: these take shape flags rather than libtest
# filters. Bootstrap pump + drain needs no PG and no CH; oracle batch needs
Expand Down
5 changes: 2 additions & 3 deletions src/catalog/type_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,9 @@ fn render_pg_timestamp(pg_micros: i64) -> AsciiBuf<32> {
out
}

fn datetime64_scale(ch_inner: &str) -> Option<i32> {
fn datetime64_scale(ch_inner: &str) -> Option<u32> {
let ast = TypeAst::parse(ch_inner, Allocator::global(&mimalloc::MiMalloc)).ok()?;
let view = ast.view();
(view.kind() == Some(Kind::DateTime64)).then(|| view.datetime64_scale())
ast.view().datetime64_scale()
}

fn format_uuid(b: &[u8; 16]) -> AsciiArray<36> {
Expand Down
13 changes: 7 additions & 6 deletions src/ch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::time::{Duration, Instant};
use backon::BackoffBuilder;
use clickhouse_c::tls::rustls::pki_types::ServerName;
use clickhouse_c::{
AsyncClient, Block, BoxedAsyncClient, ClientOpts, Codec, Compression, ErrorKind, Event,
Exception,
Allocator, AsyncClient, Block, BoxedAsyncClient, ClientOpts, Codec, Compression, ErrorKind,
Event, Exception, QueryOpts,
};
use thiserror::Error;
use tokio::net::TcpStream;
Expand Down Expand Up @@ -145,6 +145,7 @@ pub async fn connect_client(
.user(config.user())
.password(config.password());
opts.compression = compression.to_wire();
let alloc = Allocator::global(&mimalloc::MiMalloc);
let sock = TcpStream::connect((config.host(), config.port())).await?;
sock.set_nodelay(true)?;
let client = if config.secure() {
Expand All @@ -156,11 +157,11 @@ pub async fn connect_client(
.connect(server_name, sock)
.await
.map_err(|e| std::io::Error::new(e.kind(), format!("TLS handshake: {e}")))?;
AsyncClient::handshake_on(stream, opts, codec)
AsyncClient::handshake_on(stream, &opts, alloc, codec)
.await
.map(AsyncClient::boxed)
} else {
AsyncClient::handshake_on(sock, opts, codec)
AsyncClient::handshake_on(sock, &opts, alloc, codec)
.await
.map(AsyncClient::boxed)
};
Expand Down Expand Up @@ -225,7 +226,7 @@ pub async fn exec_drain(
timeout: Duration,
) -> Result<(), EmitterError> {
with_timeout(timeout, async {
client.send_query(sql, None).await?;
client.send_query(sql, &QueryOpts::new()).await?;
drain_to_end_of_stream(client).await?;
Ok(())
})
Expand All @@ -239,7 +240,7 @@ pub async fn query_strings(
timeout: Duration,
) -> Result<Vec<String>, EmitterError> {
with_timeout(timeout, async {
client.send_query(sql, None).await?;
client.send_query(sql, &QueryOpts::new()).await?;
let mut out = Vec::new();
loop {
match client.recv_event().await? {
Expand Down
27 changes: 15 additions & 12 deletions src/emit/ch_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ pub(crate) struct ColumnPlan {
pub(crate) type_repr: String,
pub(crate) ast: TypeAst,
pub(crate) decimal: Option<DecimalWire>,
pub(crate) datetime64_scale: i32,
pub(crate) datetime64_scale: u32,
pub(crate) non_finite: NonFinite,
pub(crate) encoding: ColumnEncoding,
}
Expand All @@ -1449,8 +1449,8 @@ impl NonFinite {
if decimal.is_none() && !matches!(inner.kind(), Some(Kind::Date32 | Kind::DateTime64)) {
return Ok(Self::default());
}
let precision =
usize::try_from(inner.decimal_precision()).map_err(|_| "bad Decimal precision")?;
let precision = usize::try_from(inner.decimal_precision().unwrap_or(0))
.map_err(|_| "bad Decimal precision")?;
let scale = usize::from(decimal.map_or(0, |w| w.scale));
let bound = format!("{}.{}", "9".repeat(precision - scale), "9".repeat(scale));
let cell = |key: &str, sub: &Option<Substitute>| {
Expand Down Expand Up @@ -1662,7 +1662,9 @@ impl TablePlan {
name: c.target_name.clone(),
type_repr: canonical_type(&ast, &c.target_type),
encoding: ColumnEncoding::choose(att, &ast),
datetime64_scale: types::strip_nullable(ast.view()).datetime64_scale(),
datetime64_scale: types::strip_nullable(ast.view())
.datetime64_scale()
.unwrap_or(0),
ast,
decimal,
non_finite: NonFinite::default(),
Expand All @@ -1679,7 +1681,8 @@ impl TablePlan {
type_repr: canonical_type(&oast, ty),
encoding: ColumnEncoding::choose(att, &oast),
datetime64_scale: types::strip_nullable(oast.view())
.datetime64_scale(),
.datetime64_scale()
.unwrap_or(0),
ast: oast,
decimal,
non_finite: NonFinite::default(),
Expand Down Expand Up @@ -2257,7 +2260,7 @@ fn decimal_wire_of(ast: &TypeAst) -> Option<DecimalWire> {
_ => return None,
};
Some(DecimalWire {
scale: u8::try_from(inner.decimal_scale()).ok()?,
scale: u8::try_from(inner.decimal_scale()?).ok()?,
width,
})
}
Expand Down Expand Up @@ -2488,23 +2491,23 @@ fn unsupported(kind: &'static str) -> EmitterError {
}
}

fn timestamp_ticks(pg_us: i64, scale: i32) -> Result<i64, EmitterError> {
fn timestamp_ticks(pg_us: i64, scale: u32) -> Result<i64, EmitterError> {
micros_to_ticks(
i128::from(pg_us) + i128::from(DATETIME64_PG_EPOCH_US),
scale,
)
}

/// Convert microseconds to `DateTime64(scale)` or `Time64(scale)` ticks
fn micros_to_ticks(us: i128, scale: i32) -> Result<i64, EmitterError> {
if !(0..=9).contains(&scale) {
fn micros_to_ticks(us: i128, scale: u32) -> Result<i64, EmitterError> {
if scale > 9 {
return Err(EmitterError::Type("invalid temporal precision".into()));
}
let ticks = if scale < 6 {
let divisor = 10i128.pow((6 - scale) as u32);
let divisor = 10i128.pow(6 - scale);
us / divisor
} else {
us * 10i128.pow((scale - 6) as u32)
us * 10i128.pow(scale - 6)
};
i64::try_from(ticks).map_err(|_| unsupported("finite temporal value overflows storage"))
}
Expand All @@ -2513,7 +2516,7 @@ fn encode_value(
buf: &mut ColumnBuf,
v: &ColumnValue,
decimal: Option<DecimalWire>,
timestamp_scale: i32,
timestamp_scale: u32,
) -> Result<(), EmitterError> {
match v {
// Non-Nullable target takes type default
Expand Down
6 changes: 3 additions & 3 deletions src/emit/pipeline/inserter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! dedups by `_lsn`). Retry-exhaustion is fatal: the watermark can't advance
//! without this batch.

use clickhouse_c::{Allocator, BlockBuilder, ColumnBuilder, TypeAst};
use clickhouse_c::{Allocator, BlockBuilder, ColumnBuilder, QueryOpts, TypeAst};
use tokio::task::JoinHandle;

use crate::ch::{ChConn, EmitterError, EndOfStream, drain_to_end_of_stream, with_timeout};
Expand Down Expand Up @@ -70,8 +70,8 @@ impl Inserter {
.retry(
|mut client| async move {
let result = with_timeout(insert_timeout, async {
client.send_query(sql, None).await?;
client.send_data(Some(bb)).await?;
client.send_query(sql, &QueryOpts::new()).await?;
client.send_data(bb).await?;
client.send_data_end().await?;
drain_to_end_of_stream(&mut client).await
})
Expand Down
82 changes: 25 additions & 57 deletions src/ops/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//! cadence, HTTP server reads a snapshot per request. Endpoint is read-only by
//! design (no `/quit`, no admin verbs); operator actions stay on the CLI.

use std::borrow::Cow;
use std::collections::{BTreeMap, VecDeque};
use std::fmt;
use std::io;
Expand Down Expand Up @@ -74,58 +73,48 @@ macro_rules! encode_metric {
/// [`encode_metric`] for a family every database carries: the `database=`
/// label comes first, a declared label array second
macro_rules! encode_db_metric {
($enc:expr, $dbs:expr, $names:expr, counter, $name:expr, $help:expr, $field:ident) => {
($enc:expr, $dbs:expr, counter, $name:expr, $help:expr, $field:ident) => {
counter_series(
$enc,
$name,
$help,
"database",
$names
.iter()
.zip($dbs)
.map(|(db, s)| (db.as_ref(), s.$field)),
$dbs.iter().map(|db| (db.database.as_str(), db.$field)),
)?
};
($enc:expr, $dbs:expr, $names:expr, gauge, $name:expr, $help:expr, $field:ident) => {
($enc:expr, $dbs:expr, gauge, $name:expr, $help:expr, $field:ident) => {
gauge_series(
$enc,
$name,
$help,
"database",
$names
.iter()
.zip($dbs)
.map(|(db, s)| (db.as_ref(), s.$field)),
$dbs.iter().map(|db| (db.database.as_str(), db.$field)),
)?
};
(
$enc:expr, $dbs:expr, $names:expr, counter, $name:expr, $help:expr, $field:ident,
$enc:expr, $dbs:expr, counter, $name:expr, $help:expr, $field:ident,
$key:literal, $labels:expr
) => {
counter_grid(
$enc,
$name,
$help,
$key,
$names
.iter()
.zip($dbs)
.map(|(db, s)| (db.as_ref(), $labels.into_iter().zip(s.$field))),
$dbs.iter()
.map(|db| (db.database.as_str(), $labels.into_iter().zip(db.$field))),
)?
};
(
$enc:expr, $dbs:expr, $names:expr, gauge, $name:expr, $help:expr, $field:ident,
$enc:expr, $dbs:expr, gauge, $name:expr, $help:expr, $field:ident,
$key:literal, $labels:expr
) => {
gauge_grid(
$enc,
$name,
$help,
$key,
$names
.iter()
.zip($dbs)
.map(|(db, s)| (db.as_ref(), $labels.into_iter().zip(s.$field))),
$dbs.iter()
.map(|db| (db.database.as_str(), $labels.into_iter().zip(db.$field))),
)?
};
}
Expand Down Expand Up @@ -555,12 +544,9 @@ macro_rules! db_snapshot {
}

fn encode_db_fields(dbs: &[DbSeries], enc: &mut DescriptorEncoder<'_>) -> fmt::Result {
let names: Vec<Cow<'_, str>> =
dbs.iter().map(|db| escape_label(&db.database)).collect();
$(encode_db_metric!(
enc,
dbs,
names,
$kind,
concat!("walshadow_", stringify!($field)),
$help,
Expand Down Expand Up @@ -921,24 +907,6 @@ where
Ok(())
}

/// The encoder writes label values through, and a database name is whatever
/// `CREATE DATABASE` took, so escape what OpenMetrics reserves inside quotes
fn escape_label(value: &str) -> Cow<'_, str> {
if !value.contains(['\\', '"', '\n']) {
return Cow::Borrowed(value);
}
let mut out = String::with_capacity(value.len() + 8);
for c in value.chars() {
match c {
'\\' => out.push_str("\\\\"),
'"' => out.push_str("\\\""),
'\n' => out.push_str("\\n"),
_ => out.push(c),
}
}
Cow::Owned(out)
}

/// LSN gauges carry their raw `u64`
impl<K> EncodeGaugeValue for Pos<K> {
fn encode(&self, encoder: &mut GaugeValueEncoder) -> fmt::Result {
Expand Down Expand Up @@ -1001,8 +969,6 @@ fn encode_snapshot(snap: &MetricsSnapshot, enc: &mut DescriptorEncoder<'_>) -> f
u64::from(!snap.crossing_blocked_on.is_empty()),
)?;

// rmgr names come out of the fixed resource-manager table, so label values
// need no escaping the encoder does not do
let mut records = declare(
enc,
"walshadow_filter_records_total",
Expand Down Expand Up @@ -1450,22 +1416,30 @@ mod tests {
assert!(body.contains("walshadow_pump_queue_depth 0"), "{body}");
}

/// A database name is a PG identifier, so a quote in it must not end the
/// label and truncate the scrape
#[test]
fn render_escapes_a_database_label() {
let body = render(MetricsSnapshot {
by_database: vec![DbSeries {
database: "we\"ird\\name".into(),
database: "we\"ird\\name\n".into(),
bridge_up: 1,
bridge_requests_by_op: [3, 0, 0, 0, 0],
desc_log_entries: 4,
config_backfills_pending_by_mode: [2, 0, 0],
..DbSeries::default()
}],
..MetricsSnapshot::default()
});
assert!(
body.contains(r#"walshadow_desc_log_entries{database="we\"ird\\name"} 4"#),
"{body}"
);
for want in [
r#"walshadow_bridge_up{database="we\"ird\\name\n"} 1"#,
r#"walshadow_desc_log_entries{database="we\"ird\\name\n"} 4"#,
&format!(
r#"walshadow_bridge_requests_total{{database="we\"ird\\name\n",op="{}"}} 3"#,
OP_LABELS[0]
),
r#"walshadow_config_backfills_pending{database="we\"ird\\name\n",mode="copy"} 2"#,
] {
assert!(body.contains(want), "missing {want}\n{body}");
}
}

#[tokio::test(flavor = "current_thread")]
Expand Down Expand Up @@ -1658,10 +1632,4 @@ mod tests {
let taken = serve(addr, MetricsRegistry::new()).await.unwrap_err();
assert!(matches!(taken, MetricsError::Bind { .. }), "{taken}");
}

#[test]
fn escape_label_quotes_openmetrics_reserved_chars() {
assert!(matches!(escape_label("plain"), Cow::Borrowed("plain")));
assert_eq!(escape_label("a\\b\"c\nd"), r#"a\\b\"c\nd"#);
}
}
8 changes: 4 additions & 4 deletions src/toast/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::Arc;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};

use async_trait::async_trait;
use clickhouse_c::{Allocator, Block, BlockBuilder, ColumnBuilder, Event, TypeAst};
use clickhouse_c::{Allocator, Block, BlockBuilder, ColumnBuilder, Event, QueryOpts, TypeAst};
use futures::stream::StreamExt;
use thiserror::Error;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -841,9 +841,9 @@ impl ClickHouseChunkStore {
|e| is_retryable(e) && !is_missing_mirror(e),
|mut client| async move {
let result = with_timeout(insert_timeout, async {
client.send_query(sql, None).await?;
client.send_query(sql, &QueryOpts::new()).await?;
if let Some(bb) = bb {
client.send_data(Some(bb)).await?;
client.send_data(bb).await?;
client.send_data_end().await?;
}
drain_to_end_of_stream(&mut client).await?;
Expand Down Expand Up @@ -934,7 +934,7 @@ impl ClickHouseChunkStore {
|e| is_retryable(e) && !is_missing_mirror(e),
|mut client| async {
let result = with_timeout(insert_timeout, async {
client.send_query(sql, None).await?;
client.send_query(sql, &QueryOpts::new()).await?;
let mut out = A::default();
loop {
match client.recv_event().await? {
Expand Down
Loading