From 7694a8bcd0fc3745e8b7fc11a5ef89b4b547fedd Mon Sep 17 00:00:00 2001 From: shuiyisong Date: Wed, 4 Nov 2026 17:17:08 +0800 Subject: [PATCH] fix(otlp): avoid waiting with body permits held Signed-off-by: shuiyisong --- src/frontend/AGENTS.md | 1 + src/frontend/src/instance/otlp.rs | 14 +++++++++- tests-integration/src/otlp.rs | 45 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/frontend/AGENTS.md b/src/frontend/AGENTS.md index ff9b3d174ed..2c8c16b4f84 100644 --- a/src/frontend/AGENTS.md +++ b/src/frontend/AGENTS.md @@ -81,6 +81,7 @@ including caches owned by pipeline and protocol handlers. the exemplar destination or reserving expansion memory. Reserve exemplar expansion with the transport's memory limiter before building rows; retain the permit through `WriteProgress` until writes complete. + Expansion must not wait while the request holds transport memory permits. - **Logical-table batching** (`instance/logical_batcher.rs`): `Services` initializes one shared batcher for opted-in HTTP Prom and OTLP metric-engine diff --git a/src/frontend/src/instance/otlp.rs b/src/frontend/src/instance/otlp.rs index 33ecdbd9d61..0c03d82e4e4 100644 --- a/src/frontend/src/instance/otlp.rs +++ b/src/frontend/src/instance/otlp.rs @@ -211,7 +211,19 @@ impl OpenTelemetryProtocolHandler for Instance { ) })?; limiter.check_request_fits(bytes, held_bytes)?; - let reservation = Arc::new(limiter.acquire(bytes).await?); + // Holding body permits while waiting for expansion can stall concurrent requests. + let reservation = Arc::new(if held_bytes > 0 { + limiter.try_acquire(bytes).ok_or_else(|| { + error::MemoryLimitExceededSnafu.into_error( + common_memory_manager::Error::MemoryLimitExceeded { + requested_bytes: bytes, + limit_bytes: limiter.limit_bytes(), + }, + ) + })? + } else { + limiter.acquire(bytes).await? + }); let mut ctx = ctx.fork(); // Queued and detached writes retain the reservation after caller cancellation. ctx.set_write_progress(Arc::new(WriteProgress::with_reservation(reservation))); diff --git a/tests-integration/src/otlp.rs b/tests-integration/src/otlp.rs index eb38a52e3fb..47e169e54ec 100644 --- a/tests-integration/src/otlp.rs +++ b/tests-integration/src/otlp.rs @@ -1974,6 +1974,51 @@ WITH( .await .unwrap(); let limit = 64 * 1024; + let limiter = ServerMemoryLimiter::new( + limit, + OnExhaustedPolicy::Wait { + timeout: Duration::from_secs(30), + }, + ); + let body = limiter.acquire(limit / 2).await.unwrap(); + let concurrent_body = limiter.acquire(limit / 2).await.unwrap(); + // Body permits cannot be released while their requests wait for expansion. + let error = tokio::time::timeout( + Duration::from_secs(5), + instance.metrics( + request.clone(), + ctx.clone(), + Some((limiter.clone(), body.granted_bytes())), + ), + ) + .await + .expect("requests holding body permits must not wait for expansion") + .unwrap_err(); + assert!(matches!( + error, + servers::error::Error::MemoryLimitExceeded { + source: common_memory_manager::Error::MemoryLimitExceeded { .. }, + .. + } + )); + assert_eq!(limiter.used_bytes(), limit); + drop(concurrent_body); + assert_eq!( + instance + .metrics( + request.clone(), + ctx.clone(), + Some((limiter.clone(), body.granted_bytes())), + ) + .await + .unwrap() + .accepted_data_points, + 1 + ); + assert_eq!(limiter.used_bytes(), body.granted_bytes()); + drop(body); + assert_eq!(limiter.used_bytes(), 0); + let limiter = ServerMemoryLimiter::new( limit, OnExhaustedPolicy::Wait {