Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(ingestion-consumer): take the order sentinel's lock once per batch (#112468)

Paweł Ledwońf28513e089b6
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index c7e6a24019e3..2b57ebc67d83 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -6347,6 +6347,7 @@ dependencies = [
name = "ingestion-consumer"
version = "0.1.0"
dependencies = [
+ "ahash 0.8.12",
"anyhow",
"axum 0.7.9",
"chrono",
diff --git a/rust/ingestion-consumer/Cargo.toml b/rust/ingestion-consumer/Cargo.toml
index 294da186dd23..80493d7b95c5 100644
--- a/rust/ingestion-consumer/Cargo.toml
+++ b/rust/ingestion-consumer/Cargo.toml
@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
+ahash = { workspace = true }
anyhow = { workspace = true }
axum = { workspace = true }
chrono = { workspace = true }
diff --git a/rust/ingestion-consumer/src/dispatcher.rs b/rust/ingestion-consumer/src/dispatcher.rs
index 512c8c6f2594..9b466061db02 100644
--- a/rust/ingestion-consumer/src/dispatcher.rs
+++ b/rust/ingestion-consumer/src/dispatcher.rs
@@ -600,9 +600,9 @@ impl Dispatcher {
/// the per-worker sub-batches.
fn note_and_assemble(&self, dispatches: Vec<Dispatch>) -> WorkerAssignments {
let mut assignments = WorkerAssignments::new();
+ let mut sentinel = self.key_sentinel.batch();
for dispatch in dispatches {
- self.key_sentinel
- .note_sent(&dispatch.routing_key, &dispatch.messages, dispatch.kind);
+ sentinel.note_sent(&dispatch.routing_key, &dispatch.messages, dispatch.kind);
assignments.add_dispatch(dispatch);
}
assignments
@@ -773,8 +773,9 @@ impl Dispatcher {
let mut inner = self.inner.lock().unwrap();
let effects = inner.scheduler.on_partitions_revoked(partitions);
debug_assert!(effects.dispatches.is_empty(), "a purge never dispatches");
+ let mut sentinel = self.key_sentinel.batch();
for key in &effects.evicted_keys {
- self.key_sentinel.evict(key);
+ sentinel.evict(key);
}
}
@@ -908,8 +909,9 @@ impl Dispatcher {
} = effects;
if !evicted_keys.is_empty() {
+ let mut sentinel = self.key_sentinel.batch();
for key in &evicted_keys {
- self.key_sentinel.evict(key);
+ sentinel.evict(key);
}
counter!(
"ingestion_consumer_dispatcher_pin_evictions_total",
@@ -963,9 +965,9 @@ impl Dispatcher {
/// its settle so the sentinel state isn't evicted first).
/// Advances each key's ACK high-water mark in the order sentinel.
pub fn on_sub_batch_acked(&self, key_offsets: &[KeyOffset]) {
+ let mut sentinel = self.key_sentinel.batch();
for key_offset in key_offsets {
- self.key_sentinel
- .note_acked(&key_offset.routing_key, key_offset.max_offset);
+ sentinel.note_acked(&key_offset.routing_key, key_offset.max_offset);
}
}
diff --git a/rust/ingestion-consumer/src/order_sentinel.rs b/rust/ingestion-consumer/src/order_sentinel.rs
index b7ee97fc2ed9..c1efd8b971a3 100644
--- a/rust/ingestion-consumer/src/order_sentinel.rs
+++ b/rust/ingestion-consumer/src/order_sentinel.rs
@@ -24,7 +24,7 @@
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
-use std::sync::{Arc, Mutex, OnceLock};
+use std::sync::{Arc, Mutex, MutexGuard, OnceLock};
use metrics::{counter, gauge};
use rdkafka::consumer::{BaseConsumer, ConsumerContext, Rebalance};
@@ -120,7 +120,9 @@ struct KeyState {
/// within a process: the key's messages arrive from its single partition in
/// offset order, and eviction requires every earlier send to have resolved.
pub struct KeyOrderSentinel {
- keys: Mutex<HashMap<String, KeyState>>,
+ /// Keyed by customer-chosen routing keys, so the hasher is seeded per
+ /// map to resist collision flooding.
+ keys: Mutex<HashMap<String, KeyState, ahash::RandomState>>,
/// Kill switch (`CONSUMER_ORDER_SENTINEL_ENABLED`). When off, checks
/// no-op and no state accumulates.
enabled: AtomicBool,
@@ -129,7 +131,7 @@ pub struct KeyOrderSentinel {
impl Default for KeyOrderSentinel {
fn default() -> Self {
Self {
- keys: Mutex::new(HashMap::new()),
+ keys: Mutex::new(HashMap::default()),
enabled: AtomicBool::new(true),
}
}
@@ -149,6 +151,36 @@ impl KeyOrderSentinel {
}
}
+ /// Takes the lock once for several calls and sets the key-count gauge
+ /// once, when the batch drops.
+ pub fn batch(&self) -> SentinelBatch<'_> {
+ let keys = self
+ .enabled
+ .load(Ordering::Relaxed)
+ .then(|| self.keys.lock().unwrap());
+ SentinelBatch { keys }
+ }
+
+ /// Drop all state. Called on rebalance: partitions may move to another
+ /// consumer and back, legitimately replaying uncommitted offsets, so every
+ /// baseline is stale.
+ pub fn clear(&self) {
+ self.keys.lock().unwrap().clear();
+ gauge!("ingestion_consumer_key_sentinel_keys").set(0.0);
+ }
+
+ /// Number of tracked keys (bounded by in-flight work; exposed for tests).
+ pub fn key_count(&self) -> usize {
+ self.keys.lock().unwrap().len()
+ }
+}
+
+/// A run of sentinel calls under one lock. Disabled, every call is a no-op.
+pub struct SentinelBatch<'a> {
+ keys: Option<MutexGuard<'a, HashMap<String, KeyState, ahash::RandomState>>>,
+}
+
+impl SentinelBatch<'_> {
/// Record that `messages` for `routing_key` are being handed to a worker.
/// `kind` distinguishes a fresh assignment (offsets must only move
/// forward) from a retry-path resend (repeating un-ACKed offsets is
@@ -158,40 +190,46 @@ impl KeyOrderSentinel {
/// (see module docs). Emits metrics and logs; returns violations for
/// tests.
pub fn note_sent(
- &self,
+ &mut self,
routing_key: &str,
messages: &[SerializedKafkaMessage],
kind: SendKind,
) -> Vec<KeyOrderViolation> {
- if !self.enabled.load(Ordering::Relaxed) {
+ let Some(keys) = self.keys.as_mut() else {
return Vec::new();
+ };
+ let mut violations = Vec::new();
+ let mut first: Option<&SerializedKafkaMessage> = None;
+ let mut last: Option<&SerializedKafkaMessage> = None;
+ let mut unkeyed = 0usize;
+ for message in messages {
+ if message.key.is_none() {
+ unkeyed += 1;
+ continue;
+ }
+ // Offsets within an assigned group must be strictly ascending:
+ // groups are built in batch order, and a batch preserves
+ // partition order.
+ if let Some(previous) = last {
+ if message.partition == previous.partition && message.offset <= previous.offset {
+ violations.push(KeyOrderViolation {
+ kind: KeyOrderViolationKind::IntraGroupDisorder,
+ routing_key: routing_key.to_string(),
+ partition: message.partition,
+ offset: message.offset,
+ });
+ }
+ }
+ first.get_or_insert(message);
+ last = Some(message);
}
- let keyed: Vec<&SerializedKafkaMessage> =
- messages.iter().filter(|m| m.key.is_some()).collect();
- let unkeyed = messages.len() - keyed.len();
if unkeyed > 0 {
counter!("ingestion_consumer_key_sentinel_unkeyed_total").increment(unkeyed as u64);
}
- let Some(first) = keyed.first() else {
- return Vec::new();
+ let (Some(first), Some(last)) = (first, last) else {
+ return violations;
};
- let last = keyed.last().expect("non-empty");
- let mut violations = Vec::new();
-
- // Offsets within an assigned group must be strictly ascending: groups
- // are built in batch order, and a batch preserves partition order.
- for pair in keyed.windows(2) {
- if pair[1].partition == pair[0].partition && pair[1].offset <= pair[0].offset {
- violations.push(KeyOrderViolation {
- kind: KeyOrderViolationKind::IntraGroupDisorder,
- routing_key: routing_key.to_string(),
- partition: pair[1].partition,
- offset: pair[1].offset,
- });
- }
- }
- let mut keys = self.keys.lock().unwrap();
match keys.get_mut(routing_key) {
None => {
keys.insert(
@@ -248,9 +286,6 @@ impl KeyOrderSentinel {
}
}
}
- let key_count = keys.len();
- drop(keys);
- gauge!("ingestion_consumer_key_sentinel_keys").set(key_count as f64);
for violation in &violations {
counter!(
@@ -273,11 +308,10 @@ impl KeyOrderSentinel {
/// Record that a worker ACKed this key's messages up to `max_offset`.
/// ACKs may arrive out of order across concurrent sub-batches (HTTP
/// completion order), so this only ever advances the high-water mark.
- pub fn note_acked(&self, routing_key: &str, max_offset: i64) {
- if !self.enabled.load(Ordering::Relaxed) {
+ pub fn note_acked(&mut self, routing_key: &str, max_offset: i64) {
+ let Some(keys) = self.keys.as_mut() else {
return;
- }
- let mut keys = self.keys.lock().unwrap();
+ };
if let Some(state) = keys.get_mut(routing_key) {
state.last_acked = Some(state.last_acked.map_or(max_offset, |a| a.max(max_offset)));
}
@@ -286,28 +320,18 @@ impl KeyOrderSentinel {
/// Drop a key's state. Call when its pin is evicted — every send has
/// resolved and nothing is deferred, so there is nothing left to order
/// against and future offsets are necessarily higher.
- pub fn evict(&self, routing_key: &str) {
- if !self.enabled.load(Ordering::Relaxed) {
- return;
+ pub fn evict(&mut self, routing_key: &str) {
+ if let Some(keys) = self.keys.as_mut() {
+ keys.remove(routing_key);
}
- let mut keys = self.keys.lock().unwrap();
- keys.remove(routing_key);
- let key_count = keys.len();
- drop(keys);
- gauge!("ingestion_consumer_key_sentinel_keys").set(key_count as f64);
- }
-
- /// Drop all state. Called on rebalance: partitions may move to another
- /// consumer and back, legitimately replaying uncommitted offsets, so every
- /// baseline is stale.
- pub fn clear(&self) {
- self.keys.lock().unwrap().clear();
- gauge!("ingestion_consumer_key_sentinel_keys").set(0.0);
}
+}
- /// Number of tracked keys (bounded by in-flight work; exposed for tests).
- pub fn key_count(&self) -> usize {
- self.keys.lock().unwrap().len()
+impl Drop for SentinelBatch<'_> {
+ fn drop(&mut self) {
+ if let Some(keys) = &self.keys {
+ gauge!("ingestion_consumer_key_sentinel_keys").set(keys.len() as f64);
+ }
}
}
@@ -508,9 +532,11 @@ mod tests {
fn forward_sends_pass() {
let sentinel = KeyOrderSentinel::new();
assert!(sentinel
+ .batch()
.note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Fresh)
.is_empty());
assert!(sentinel
+ .batch()
.note_sent("t:a", &[msg_at(0, 3), msg_at(0, 4)], SendKind::Fresh)
.is_empty());
}
@@ -518,7 +544,10 @@ mod tests {
#[test]
fn intra_group_disorder_is_detected() {
let sentinel = KeyOrderSentinel::new();
- let violations = sentinel.note_sent("t:a", &[msg_at(0, 2), msg_at(0, 1)], SendKind::Fresh);
+ let violations =
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 2), msg_at(0, 1)], SendKind::Fresh);
assert_eq!(violations.len(), 1);
assert_eq!(
violations[0].kind,
@@ -529,9 +558,12 @@ mod tests {
#[test]
fn replay_of_unacked_range_is_not_a_violation() {
let sentinel = KeyOrderSentinel::new();
- sentinel.note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Fresh);
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Fresh);
// Send failed (no ACK) → deferred flush re-sends the same messages.
assert!(sentinel
+ .batch()
.note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Resend)
.is_empty());
}
@@ -542,12 +574,18 @@ mod tests {
// newer batch's send overtook an older batch's — the race the
// consumer-loop assignment ordering exists to prevent.
let sentinel = KeyOrderSentinel::new();
- sentinel.note_sent("t:a", &[msg_at(0, 3), msg_at(0, 4)], SendKind::Fresh);
- let violations = sentinel.note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Fresh);
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 3), msg_at(0, 4)], SendKind::Fresh);
+ let violations =
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Fresh);
assert_eq!(violations.len(), 1);
assert_eq!(violations[0].kind, KeyOrderViolationKind::SendBelowLastSent);
// The watermark still advanced: the next in-order send is clean.
assert!(sentinel
+ .batch()
.note_sent("t:a", &[msg_at(0, 5)], SendKind::Fresh)
.is_empty());
}
@@ -555,10 +593,14 @@ mod tests {
#[test]
fn resend_after_ack_is_a_violation() {
let sentinel = KeyOrderSentinel::new();
- sentinel.note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Fresh);
- sentinel.note_acked("t:a", 2);
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Fresh);
+ sentinel.batch().note_acked("t:a", 2);
// Even the legal retry path must never repeat an ACKed offset.
- let violations = sentinel.note_sent("t:a", &[msg_at(0, 2)], SendKind::Resend);
+ let violations = sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 2)], SendKind::Resend);
assert_eq!(violations.len(), 1);
assert_eq!(violations[0].kind, KeyOrderViolationKind::ResendAfterAck);
}
@@ -569,9 +611,14 @@ mod tests {
// newer messages were sent and ACKed while its older ones were still
// deferred — flushing the older ones now is out-of-order processing.
let sentinel = KeyOrderSentinel::new();
- sentinel.note_sent("t:a", &[msg_at(0, 4), msg_at(0, 5)], SendKind::Fresh);
- sentinel.note_acked("t:a", 5);
- let violations = sentinel.note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Resend);
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 4), msg_at(0, 5)], SendKind::Fresh);
+ sentinel.batch().note_acked("t:a", 5);
+ let violations =
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Resend);
assert_eq!(violations.len(), 1);
assert_eq!(violations[0].kind, KeyOrderViolationKind::ResendAfterAck);
}
@@ -579,17 +626,24 @@ mod tests {
#[test]
fn out_of_order_acks_only_advance_the_watermark() {
let sentinel = KeyOrderSentinel::new();
- sentinel.note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Fresh);
- sentinel.note_sent("t:a", &[msg_at(0, 3), msg_at(0, 4)], SendKind::Fresh);
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 1), msg_at(0, 2)], SendKind::Fresh);
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 3), msg_at(0, 4)], SendKind::Fresh);
// Sub-batch ACKs arrive in reverse HTTP-completion order.
- sentinel.note_acked("t:a", 4);
- sentinel.note_acked("t:a", 2);
+ sentinel.batch().note_acked("t:a", 4);
+ sentinel.batch().note_acked("t:a", 2);
// Forward progress from the true high-water mark is still clean.
assert!(sentinel
+ .batch()
.note_sent("t:a", &[msg_at(0, 5)], SendKind::Fresh)
.is_empty());
// …and re-sending below it still fires.
- let violations = sentinel.note_sent("t:a", &[msg_at(0, 3)], SendKind::Resend);
+ let violations = sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 3)], SendKind::Resend);
assert_eq!(violations.len(), 1);
assert_eq!(violations[0].kind, KeyOrderViolationKind::ResendAfterAck);
}
@@ -597,12 +651,15 @@ mod tests {
#[test]
fn eviction_drops_state_and_rebaselines() {
let sentinel = KeyOrderSentinel::new();
- sentinel.note_sent("t:a", &[msg_at(0, 5)], SendKind::Fresh);
- sentinel.note_acked("t:a", 5);
- sentinel.evict("t:a");
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 5)], SendKind::Fresh);
+ sentinel.batch().note_acked("t:a", 5);
+ sentinel.batch().evict("t:a");
assert_eq!(sentinel.key_count(), 0);
// A rebaselined key doesn't compare against evicted history.
assert!(sentinel
+ .batch()
.note_sent("t:a", &[msg_at(0, 6)], SendKind::Fresh)
.is_empty());
}
@@ -610,12 +667,17 @@ mod tests {
#[test]
fn clear_resets_all_keys() {
let sentinel = KeyOrderSentinel::new();
- sentinel.note_sent("t:a", &[msg_at(0, 5)], SendKind::Fresh);
- sentinel.note_sent("t:b", &[msg_at(1, 7)], SendKind::Fresh);
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 5)], SendKind::Fresh);
+ sentinel
+ .batch()
+ .note_sent("t:b", &[msg_at(1, 7)], SendKind::Fresh);
sentinel.clear();
assert_eq!(sentinel.key_count(), 0);
// Post-rebalance redelivery of uncommitted offsets must not fire.
assert!(sentinel
+ .batch()
.note_sent("t:a", &[msg_at(0, 3)], SendKind::Fresh)
.is_empty());
}
@@ -624,8 +686,10 @@ mod tests {
fn a_disabled_key_sentinel_checks_nothing_and_holds_no_state() {
let keys = KeyOrderSentinel::new();
keys.set_enabled(false);
- keys.note_sent("t:a", &[msg_at(0, 5)], SendKind::Fresh);
+ keys.batch()
+ .note_sent("t:a", &[msg_at(0, 5)], SendKind::Fresh);
assert!(keys
+ .batch()
.note_sent("t:a", &[msg_at(0, 2), msg_at(0, 1)], SendKind::Fresh)
.is_empty());
assert_eq!(keys.key_count(), 0, "no state accumulates while disabled");
@@ -634,12 +698,14 @@ mod tests {
#[test]
fn disabling_key_sentinel_clears_stale_watermarks() {
let keys = KeyOrderSentinel::new();
- keys.note_sent("t:a", &[msg_at(0, 5)], SendKind::Fresh);
- keys.note_acked("t:a", 5);
+ keys.batch()
+ .note_sent("t:a", &[msg_at(0, 5)], SendKind::Fresh);
+ keys.batch().note_acked("t:a", 5);
keys.set_enabled(false);
keys.set_enabled(true);
// Re-enable rebaselines: no comparison against pre-disable history.
assert!(keys
+ .batch()
.note_sent("t:a", &[msg_at(0, 3)], SendKind::Fresh)
.is_empty());
}
@@ -647,9 +713,12 @@ mod tests {
#[test]
fn partition_move_rebaselines() {
let sentinel = KeyOrderSentinel::new();
- sentinel.note_sent("t:a", &[msg_at(0, 100)], SendKind::Fresh);
+ sentinel
+ .batch()
+ .note_sent("t:a", &[msg_at(0, 100)], SendKind::Fresh);
// Same key on a different partition: offsets aren't comparable.
assert!(sentinel
+ .batch()
.note_sent("t:a", &[msg_at(3, 1)], SendKind::Fresh)
.is_empty());
}
@@ -660,10 +729,12 @@ mod tests {
// Null-key production round-robins a key across partitions; there is
// no per-key order to check, even when offsets regress across sends.
assert!(sentinel
+ .batch()
.note_sent("t:a", &[unkeyed_msg_at(1, 5000)], SendKind::Fresh)
.is_empty());
assert_eq!(sentinel.key_count(), 0, "unkeyed sends hold no state");
assert!(sentinel
+ .batch()
.note_sent("t:a", &[unkeyed_msg_at(0, 3)], SendKind::Fresh)
.is_empty());
}
@@ -676,14 +747,16 @@ mod tests {
// send would fire a false resend_after_ack.
let sentinel = KeyOrderSentinel::new();
assert!(sentinel
+ .batch()
.note_sent(
"t:a",
&[msg_at(0, 100), unkeyed_msg_at(1, 5000)],
SendKind::Fresh
)
.is_empty());
- sentinel.note_acked("t:a", 100);
+ sentinel.batch().note_acked("t:a", 100);
assert!(sentinel
+ .batch()
.note_sent("t:a", &[msg_at(0, 101)], SendKind::Fresh)
.is_empty());
}