mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-18 00:34:11 +00:00
fix(postgres): account for buffered InfluxDB and OTLP COPY bodies (#10450)
Signed-off-by: Ning Sun <sunning@greptime.com>
This commit is contained in:
@@ -251,7 +251,7 @@ pub(crate) struct CopyInState {
|
||||
query_ctx: QueryContextRef,
|
||||
payload: Payload,
|
||||
/// Reservations against the shared request-memory limiter for the
|
||||
/// bytes buffered by a Prometheus payload, acquired per CopyData
|
||||
/// bytes buffered by a table-less payload, acquired per CopyData
|
||||
/// chunk as it arrives and held until the buffer is flushed or the
|
||||
/// copy ends — the receive-stage counterpart of HTTP's
|
||||
/// body-accounting middleware, so concurrent unfinished COPY
|
||||
@@ -430,10 +430,14 @@ impl CopyInState {
|
||||
self.codec_format_code
|
||||
}
|
||||
|
||||
/// Prometheus bodies are charged against the shared request-memory limiter
|
||||
/// as they arrive, before decode and conversion acquire additional memory.
|
||||
/// Whether the payload buffers raw CopyData bytes until `CopyDone`
|
||||
/// (the table-less formats); such bytes are charged against the
|
||||
/// shared request-memory limiter as they arrive.
|
||||
fn buffers_payload(&self) -> bool {
|
||||
matches!(self.payload, Payload::PromWrite { .. })
|
||||
matches!(
|
||||
self.payload,
|
||||
Payload::Influxdb { .. } | Payload::Otlp { .. } | Payload::PromWrite { .. }
|
||||
)
|
||||
}
|
||||
|
||||
/// The payload bytes currently buffered (charged or not).
|
||||
@@ -542,14 +546,18 @@ impl CopyInState {
|
||||
}
|
||||
|
||||
/// Takes the buffered OTLP payload and its options, if any.
|
||||
fn take_otlp(&mut self) -> Option<(OtlpOptions, Vec<u8>)> {
|
||||
fn take_otlp(&mut self) -> Option<(OtlpOptions, Vec<u8>, BufferedReservation)> {
|
||||
let Payload::Otlp { options, data } = &mut self.payload else {
|
||||
return None;
|
||||
};
|
||||
if data.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some((options.clone(), std::mem::take(data)))
|
||||
Some((
|
||||
options.clone(),
|
||||
std::mem::take(data),
|
||||
self.take_reservation(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Takes the buffered remote-write body and its options, if any.
|
||||
@@ -570,19 +578,20 @@ impl CopyInState {
|
||||
/// Takes the buffered payload as one line-protocol request body, if
|
||||
/// any — the same bytes an HTTP write of the same records would
|
||||
/// carry.
|
||||
fn take_lines(&mut self) -> PgWireResult<Option<InfluxdbRequest>> {
|
||||
fn take_lines(&mut self) -> PgWireResult<Option<(InfluxdbRequest, BufferedReservation)>> {
|
||||
let Payload::Influxdb { data, precision } = &mut self.payload else {
|
||||
return Ok(None);
|
||||
};
|
||||
if data.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let lines = String::from_utf8(std::mem::take(data))
|
||||
.map_err(|e| bad_copy_data(format!("COPY FROM STDIN data is not valid UTF-8: {e}")))?;
|
||||
Ok(Some(InfluxdbRequest {
|
||||
let request = InfluxdbRequest {
|
||||
precision: *precision,
|
||||
lines,
|
||||
}))
|
||||
lines: String::from_utf8(std::mem::take(data)).map_err(|e| {
|
||||
bad_copy_data(format!("COPY FROM STDIN data is not valid UTF-8: {e}"))
|
||||
})?,
|
||||
};
|
||||
Ok(Some((request, self.take_reservation())))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -790,13 +799,23 @@ impl PostgresServerHandlerInner {
|
||||
// timestamp alignment and inserts are identical to an HTTP
|
||||
// write of the same bytes. The context stays this connection's
|
||||
// postgres context.
|
||||
if let Some((options, bytes)) = state.take_otlp() {
|
||||
if let Some((options, bytes, reservation)) = state.take_otlp() {
|
||||
let Some(otlp_handler) = &self.otlp_handler else {
|
||||
return Err(unsupported("this server is not wired for the otlp format"));
|
||||
};
|
||||
return self
|
||||
.flush_otlp(otlp_handler, options, bytes, query_ctx)
|
||||
.await;
|
||||
// The reservation outlives the payload: it is dropped when
|
||||
// the payload's bytes are consumed below.
|
||||
let rows = self
|
||||
.flush_otlp(
|
||||
otlp_handler,
|
||||
options,
|
||||
bytes,
|
||||
reservation.charged_bytes(),
|
||||
query_ctx,
|
||||
)
|
||||
.await?;
|
||||
drop(reservation);
|
||||
return Ok(rows);
|
||||
}
|
||||
if let Some((options, bytes, reservation)) = state.take_prom_write() {
|
||||
let Some(prom_write_state) = &self.prom_write_state else {
|
||||
@@ -808,7 +827,7 @@ impl PostgresServerHandlerInner {
|
||||
.flush_prom_write(prom_write_state, options, bytes, reservation, query_ctx)
|
||||
.await;
|
||||
}
|
||||
if let Some(request) = state.take_lines()? {
|
||||
if let Some((request, reservation)) = state.take_lines()? {
|
||||
let Some(influxdb_handler) = &self.influxdb_handler else {
|
||||
return Err(unsupported(
|
||||
"this server is not wired for the influxdb line protocol format",
|
||||
@@ -818,6 +837,7 @@ impl PostgresServerHandlerInner {
|
||||
.exec(request, query_ctx)
|
||||
.await
|
||||
.map_err(convert_err)?;
|
||||
drop(reservation);
|
||||
return match output.data {
|
||||
OutputData::AffectedRows(rows) => Ok(rows as u64),
|
||||
OutputData::RecordBatches(_) | OutputData::Stream(_) => Ok(0),
|
||||
@@ -851,6 +871,7 @@ impl PostgresServerHandlerInner {
|
||||
otlp_handler: &crate::query_handler::OpenTelemetryProtocolHandlerRef,
|
||||
options: OtlpOptions,
|
||||
bytes: Vec<u8>,
|
||||
body_reserved_bytes: u64,
|
||||
query_ctx: QueryContextRef,
|
||||
) -> PgWireResult<u64> {
|
||||
use opentelemetry_proto::tonic::collector::logs::v1::ExportLogsServiceRequest;
|
||||
@@ -886,7 +907,7 @@ impl PostgresServerHandlerInner {
|
||||
.metrics(
|
||||
request,
|
||||
Arc::new(ctx),
|
||||
Some((self.memory_limiter.clone(), 0)),
|
||||
Some((self.memory_limiter.clone(), body_reserved_bytes)),
|
||||
)
|
||||
.await
|
||||
.map_err(convert_err)?;
|
||||
@@ -1108,7 +1129,7 @@ impl CopyHandler for PostgresServerHandlerInner {
|
||||
// otherwise a pooled connection would retain failed-import
|
||||
// buffers until its next COPY. On success it is put back.
|
||||
//
|
||||
// Buffered Prometheus payloads charge their bytes against the
|
||||
// Buffered table-less payloads charge their bytes against the
|
||||
// shared request-memory limiter as they arrive (the
|
||||
// receive-stage counterpart of HTTP's body accounting); the
|
||||
// reservation is held until the buffer is flushed or the state
|
||||
|
||||
@@ -1952,6 +1952,110 @@ async fn test_postgres_copy_in_prom_write_held_body_quota() {
|
||||
server.shutdown().await.unwrap();
|
||||
}
|
||||
|
||||
/// Existing table-less formats enforce the same receive budget as Prometheus.
|
||||
/// An oversized body is rejected before either format's decoder runs.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_postgres_copy_in_buffered_receive_quota() {
|
||||
let server = quota_test_server(
|
||||
"pg_copy_buffered_receive_quota",
|
||||
common_memory_manager::OnExhaustedPolicy::Fail,
|
||||
)
|
||||
.await;
|
||||
let client = connect(server.bind_addr().unwrap().port()).await;
|
||||
for format in ["influxdb", "otlp"] {
|
||||
let error = feed_copy_in(
|
||||
&client,
|
||||
&format!("COPY FROM STDIN WITH (FORMAT {format})"),
|
||||
vec![Bytes::from(vec![b'x'; 65 * 1024])],
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
error
|
||||
.as_db_error()
|
||||
.unwrap()
|
||||
.message()
|
||||
.to_lowercase()
|
||||
.contains("memory")
|
||||
);
|
||||
assert!(!client.simple_query("SELECT 1").await.unwrap().is_empty());
|
||||
}
|
||||
server.shutdown().await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_postgres_copy_in_otlp_held_body_quota() {
|
||||
use std::time::Duration;
|
||||
|
||||
use otel_arrow_rust::proto::opentelemetry::collector::metrics::v1::ExportMetricsServiceRequest;
|
||||
use otel_arrow_rust::proto::opentelemetry::common::v1::{AnyValue, KeyValue, any_value};
|
||||
use otel_arrow_rust::proto::opentelemetry::metrics::v1::{
|
||||
Exemplar, Gauge, Metric, NumberDataPoint, ResourceMetrics, ScopeMetrics, exemplar, metric,
|
||||
number_data_point,
|
||||
};
|
||||
use prost::Message;
|
||||
|
||||
init_default_ut_logging();
|
||||
let server = quota_test_server(
|
||||
"pg_copy_in_otlp_held_body",
|
||||
common_memory_manager::OnExhaustedPolicy::Wait {
|
||||
timeout: Duration::from_secs(30),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
let client = connect(server.bind_addr().unwrap().port()).await;
|
||||
let request = ExportMetricsServiceRequest {
|
||||
resource_metrics: vec![ResourceMetrics {
|
||||
scope_metrics: vec![ScopeMetrics {
|
||||
metrics: vec![Metric {
|
||||
name: "copy_otlp_held_body".into(),
|
||||
data: Some(metric::Data::Gauge(Gauge {
|
||||
data_points: vec![NumberDataPoint {
|
||||
time_unix_nano: 1_700_000_000_000_000_000,
|
||||
value: Some(number_data_point::Value::AsDouble(1.0)),
|
||||
attributes: vec![KeyValue {
|
||||
key: "host".into(),
|
||||
value: Some(AnyValue {
|
||||
value: Some(any_value::Value::StringValue(
|
||||
"x".repeat(13 * 1024),
|
||||
)),
|
||||
}),
|
||||
}],
|
||||
exemplars: vec![Exemplar {
|
||||
time_unix_nano: 1_700_000_000_000_000_000,
|
||||
value: Some(exemplar::Value::AsDouble(1.0)),
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
}],
|
||||
})),
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
}],
|
||||
};
|
||||
let body = request.encode_to_vec();
|
||||
let conversion = servers::otlp::metrics::to_grpc_insert_requests(
|
||||
request,
|
||||
&mut session::protocol_ctx::OtlpMetricCtx {
|
||||
with_metric_engine: true,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(conversion.exemplars.len(), 1);
|
||||
let expansion =
|
||||
servers::exemplar::estimated_row_bytes([conversion.exemplars.as_slice()]).unwrap();
|
||||
assert!(body.len() < 64 * 1024);
|
||||
assert!(expansion < 64 * 1024);
|
||||
assert!(body.len() + expansion > 64 * 1024);
|
||||
|
||||
assert_copy_rejects_held_body(&client, "COPY FROM STDIN WITH (FORMAT otlp)", body).await;
|
||||
server.shutdown().await.unwrap();
|
||||
}
|
||||
|
||||
async fn assert_copy_rejects_held_body(client: &tokio_postgres::Client, sql: &str, body: Vec<u8>) {
|
||||
let error = tokio::time::timeout(
|
||||
std::time::Duration::from_secs(5),
|
||||
|
||||
Reference in New Issue
Block a user