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.

chore(ingestion-consumer): attribute ledger rejections and rebalances (#94988)

Paweł Ledwoń8ba3d23b1d24
diff --git a/rust/common/kafka-consumer/src/topic_offset_ledger.rs b/rust/common/kafka-consumer/src/topic_offset_ledger.rs
index e34ab118fccd..75958d96d1bd 100644
--- a/rust/common/kafka-consumer/src/topic_offset_ledger.rs
+++ b/rust/common/kafka-consumer/src/topic_offset_ledger.rs
@@ -31,7 +31,13 @@ pub enum Rejection {
/// The work violated the ledger's contract. The partition's window is
/// unknown after this, so its ledger was reset to a new generation: work
/// in flight drops as stale, and the next delivery founds a fresh ledger.
- Violation(LedgerError),
+ /// `generation` and `held` describe the ledger before that reset.
+ Violation {
+ error: LedgerError,
+ stamp: u64,
+ generation: u64,
+ held: Held,
+ },
}
/// One batch's settled view of its partition ledger: the frontier after its
@@ -108,9 +114,15 @@ impl TopicOffsetLedger {
match ledger.charge(offset_charges) {
Ok(_) => Ok(ledger.held()),
Err(error) => {
+ let held = ledger.held();
*ledger = PartitionOffsetLedger::new(generation + 1);
self.generations_version.fetch_add(1, Ordering::Relaxed);
- Err(Rejection::Violation(error))
+ Err(Rejection::Violation {
+ error,
+ stamp,
+ generation,
+ held,
+ })
}
}
}
@@ -144,9 +156,15 @@ impl TopicOffsetLedger {
generation,
}),
Err(error) => {
+ let held = ledger.held();
*ledger = PartitionOffsetLedger::new(generation + 1);
self.generations_version.fetch_add(1, Ordering::Relaxed);
- Err(Rejection::Violation(error))
+ Err(Rejection::Violation {
+ error,
+ stamp,
+ generation,
+ held,
+ })
}
}
}
@@ -528,10 +546,18 @@ mod tests {
// violation, not a rebalance: the ledger cannot trust its window.
assert_eq!(
ledger.charge(&p0, 0, [(Offset(11), Charge::ZERO)]),
- Err(Rejection::Violation(LedgerError::OffsetNotAboveWindow {
- offset: Offset(11),
- next: Offset(12),
- }))
+ Err(Rejection::Violation {
+ error: LedgerError::OffsetNotAboveWindow {
+ offset: Offset(11),
+ next: Offset(12),
+ },
+ stamp: 0,
+ generation: 0,
+ held: Held {
+ offsets: 2,
+ charge: Charge::ZERO
+ },
+ })
);
assert_eq!(ledger.held(&p0).offsets, 0, "the window is discarded");
assert_eq!(ledger.generation(&p0), 1);
@@ -562,9 +588,15 @@ mod tests {
assert_eq!(
ledger.settle(&p0, 0, [Offset(15)]),
- Err(Rejection::Violation(LedgerError::CompletionUncharged {
- offset: Offset(15)
- }))
+ Err(Rejection::Violation {
+ error: LedgerError::CompletionUncharged { offset: Offset(15) },
+ stamp: 0,
+ generation: 0,
+ held: Held {
+ offsets: 1,
+ charge: Charge::ZERO
+ },
+ })
);
assert_eq!(ledger.held(&p0).offsets, 0);
assert_eq!(ledger.generation(&p0), 1);
diff --git a/rust/ingestion-consumer/src/ledger_shadow.rs b/rust/ingestion-consumer/src/ledger_shadow.rs
index 29f3af5467a8..974fa251949d 100644
--- a/rust/ingestion-consumer/src/ledger_shadow.rs
+++ b/rust/ingestion-consumer/src/ledger_shadow.rs
@@ -63,7 +63,12 @@ impl LedgerShadow {
};
match ledger.charge(topic_partition, stamp, charges.iter().copied()) {
Ok(held) => set_held_gauges(&topic_partition.topic, topic_partition.partition, held),
- Err(rejection) => count_rejection("charge", topic_partition, rejection),
+ Err(rejection) => count_rejection(
+ "charge",
+ topic_partition,
+ rejection,
+ RejectedSlice::charged(charges),
+ ),
}
}
@@ -84,7 +89,12 @@ impl LedgerShadow {
let settlement = match ledger.settle(topic_partition, stamp, offsets) {
Ok(settlement) => settlement,
Err(rejection) => {
- count_rejection("settle", topic_partition, rejection);
+ count_rejection(
+ "settle",
+ topic_partition,
+ rejection,
+ RejectedSlice::settled(span),
+ );
return None;
}
};
@@ -192,27 +202,101 @@ fn frontier_mismatch(
})
}
+#[derive(Debug, Clone, Copy)]
+enum RejectedSlice {
+ /// Exactly the offsets the charge submitted to the ledger.
+ Charged {
+ first: Option<i64>,
+ last: Option<i64>,
+ offsets: usize,
+ },
+ /// The batch's delivered span for the partition, not the settled slice.
+ /// A generation change mid-batch drops the old generation's charges but
+ /// leaves the span whole, so the span can cover offsets the settlement
+ /// never submitted. The settled offsets are an iterator the happy path
+ /// does not collect, so the span is the closest description available.
+ /// The error names the offending offset.
+ Settled { first: i64, last: i64 },
+}
+
+impl RejectedSlice {
+ fn charged(charges: &[(Offset, Charge)]) -> Self {
+ Self::Charged {
+ first: charges.first().map(|(offset, _)| offset.0),
+ last: charges.last().map(|(offset, _)| offset.0),
+ offsets: charges.len(),
+ }
+ }
+
+ fn settled(span: &OffsetSpan) -> Self {
+ Self::Settled {
+ first: span.first,
+ last: span.last,
+ }
+ }
+}
+
/// Count one charge or settlement the ledger rejected. A stale slice is
/// expected around a rebalance; a violation is a bug in the accounting.
-fn count_rejection(stage: &'static str, topic_partition: &TopicPartition, rejection: Rejection) {
+/// Callers must build `slice` inside their error arm so the happy path pays
+/// nothing for it.
+fn count_rejection(
+ stage: &'static str,
+ topic_partition: &TopicPartition,
+ rejection: Rejection,
+ slice: RejectedSlice,
+) {
match rejection {
Rejection::Stale { .. } => {
counter!("ingestion_consumer_ledger_stale_slices_total", "stage" => stage).increment(1);
}
- Rejection::Violation(error) => {
+ Rejection::Violation {
+ error,
+ stamp,
+ generation,
+ held,
+ } => {
counter!(
"ingestion_consumer_ledger_errors_total",
"stage" => stage,
"kind" => error.kind()
)
.increment(1);
- warn!(
- stage,
- topic = %topic_partition.topic,
- partition = topic_partition.partition,
- error = %error,
- "Offset ledger rejected a slice and reset its partition"
- );
+ // A tracing field name is fixed at the call site, so each variant
+ // needs its own `warn!`.
+ match slice {
+ RejectedSlice::Charged {
+ first,
+ last,
+ offsets,
+ } => warn!(
+ stage,
+ topic = %topic_partition.topic,
+ partition = topic_partition.partition,
+ error = %error,
+ kind = error.kind(),
+ batch_generation = stamp,
+ ledger_generation = generation,
+ depth = held.offsets,
+ slice_first = ?first,
+ slice_last = ?last,
+ slice_offsets = offsets,
+ "Offset ledger rejected a slice and reset its partition"
+ ),
+ RejectedSlice::Settled { first, last } => warn!(
+ stage,
+ topic = %topic_partition.topic,
+ partition = topic_partition.partition,
+ error = %error,
+ kind = error.kind(),
+ batch_generation = stamp,
+ ledger_generation = generation,
+ depth = held.offsets,
+ batch_first = first,
+ batch_last = last,
+ "Offset ledger rejected a slice and reset its partition"
+ ),
+ }
}
}
}
diff --git a/rust/ingestion-consumer/src/order_sentinel.rs b/rust/ingestion-consumer/src/order_sentinel.rs
index b7f7a2b8d4f3..8c7ce283cde8 100644
--- a/rust/ingestion-consumer/src/order_sentinel.rs
+++ b/rust/ingestion-consumer/src/order_sentinel.rs
@@ -632,6 +632,19 @@ impl SentinelContext {
}
}
+fn partition_names(tpl: &TopicPartitionList) -> Vec<String> {
+ let elements = tpl.elements();
+ let mut partitions: Vec<(&str, i32)> = elements
+ .iter()
+ .map(|element| (element.topic(), element.partition()))
+ .collect();
+ partitions.sort_unstable();
+ partitions
+ .into_iter()
+ .map(|(topic, partition)| format!("{topic}:{partition}"))
+ .collect()
+}
+
impl ClientContext for SentinelContext {
/// Fired on a librdkafka thread every `statistics.interval.ms`; disabled
/// when that is 0.
@@ -645,7 +658,11 @@ impl ConsumerContext for SentinelContext {
match rebalance {
Rebalance::Revoke(tpl) => {
counter!("ingestion_consumer_rebalances_total", "event" => "revoke").increment(1);
- info!(partitions = tpl.count(), "Rebalance: partitions revoked");
+ info!(
+ partitions = tpl.count(),
+ topic_partitions = ?partition_names(tpl),
+ "Rebalance: partitions revoked"
+ );
self.commit_sentinel
.forget_partitions(tpl.elements().iter().map(|e| (e.topic(), e.partition())));
self.forget_ledger_partitions(tpl);
@@ -665,7 +682,11 @@ impl ConsumerContext for SentinelContext {
fn post_rebalance(&self, _consumer: &BaseConsumer<Self>, rebalance: &Rebalance) {
if let Rebalance::Assign(tpl) = rebalance {
counter!("ingestion_consumer_rebalances_total", "event" => "assign").increment(1);
- info!(partitions = tpl.count(), "Rebalance: partitions assigned");
+ info!(
+ partitions = tpl.count(),
+ topic_partitions = ?partition_names(tpl),
+ "Rebalance: partitions assigned"
+ );
// An assign list names partitions that start a new assignment, so
// any surviving ledger for them is stale. The revoke callback
// normally dropped it already; this covers losses with no revoke
@@ -1013,4 +1034,17 @@ mod tests {
.note_sent("t:a", &[msg_at(0, 101)], SendKind::Fresh)
.is_empty());
}
+
+ #[test]
+ fn partition_names_sort_by_topic_then_partition_number() {
+ let mut tpl = TopicPartitionList::new();
+ tpl.add_partition("overflow", 2);
+ tpl.add_partition("events", 10);
+ tpl.add_partition("events", 9);
+
+ assert_eq!(
+ partition_names(&tpl),
+ vec!["events:9", "events:10", "overflow:2"]
+ );
+ }
}