: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
//! App layer, Kafka produce sequencing: the two-phase enqueue/await split. Depends on `domain`,
//! `kafka`, `store`, and `app::settings`; never imported by a lower layer.
//!
//! The sequence `enqueue_tiles → store.mark_produced → await_deliveries` (composed by
//! `execute`) enqueues every tile its run has no newer day for, honoring the in-flight bound and
//! draining overflow acks before the mark, then drains the remaining acks after it, folding all
//! delivery high-water marks into one accumulator carried across the mark by [`InFlight`].
use std::collections::VecDeque;
use std::time::{Duration, Instant};
use metrics::{counter, histogram};
use rdkafka::error::KafkaError;
use rdkafka::producer::DeliveryFuture;
use tokio_util::sync::CancellationToken;
use crate::domain::{
CancelCause, EnqueuedChunk, Halted, ProduceHwms, ProducedChunk, ScannedChunk, StreamedChunk,
};
use crate::kafka::pacing::TilePacer;
use crate::kafka::producer::{EnqueueError, SeedTileProducer};
use crate::observability::metrics::{
PRODUCE_ACK_SECONDS, TILE_PRODUCE_ERRORS, TILE_PRODUCE_QUEUE_FULL,
};
use crate::store::chunks::ChunkStoreError;
use super::newest_day::NewestDayIndex;
use super::settings::ProducerSettings;
/// The ceiling the queue-full backoff doubles up to; also clamps [`ProducerSettings`] at construction.
pub(super) const QUEUE_FULL_BACKOFF_CAP: Duration = Duration::from_secs(5);
/// The pending deliveries and folded high-water marks handed from the pre-mark enqueue phase to the
/// post-mark await phase, so both phases fold into the same accumulator in the same order.
pub(super) struct InFlight {
pending: VecDeque<PendingDelivery>,
hwms: ProduceHwms,
}
impl InFlight {
pub(super) fn new(max_inflight: usize) -> Self {
Self {
pending: VecDeque::with_capacity(max_inflight),
hwms: ProduceHwms::default(),
}
}
}
/// The per-message enqueue step both produce paths share: pacer waited exactly once per message
/// (never re-waited in the queue-full loop), backoff reset per message with the queue-full counter
/// emitted before each backoff wait, then pop-then-observe at the in-flight `>=` bound.
pub(super) async fn enqueue_one(
enqueue: impl Fn() -> Result<DeliveryFuture, EnqueueError>,
inflight: &mut InFlight,
pacer: &TilePacer,
settings: ProducerSettings,
lease_cancel: &CancellationToken,
shutdown: &CancellationToken,
) -> Result<(), ProduceStop> {
wait_for_pacer(pacer, shutdown, lease_cancel)
.await
.map_err(ProduceStop::Cancelled)?;
let mut backoff = settings.queue_full_backoff;
loop {
match enqueue() {
Ok(delivery) => {
inflight.pending.push_back(PendingDelivery {
delivery,
sent_at: Instant::now(),
});
break;
}
Err(EnqueueError::QueueFull) => {
counter!(TILE_PRODUCE_QUEUE_FULL).increment(1);
wait_for_backoff(backoff, shutdown, lease_cancel)
.await
.map_err(ProduceStop::Cancelled)?;
backoff = next_queue_full_backoff(backoff);
}
Err(EnqueueError::Fatal(error)) => {
counter!(TILE_PRODUCE_ERRORS).increment(1);
return Err(ProduceStop::Failed(ProduceError::Enqueue(error)));
}
}
}
if inflight.pending.len() >= settings.max_inflight.get() {
let delivery = inflight
.pending
.pop_front()
.expect("the in-flight bound guarantees one delivery");
if let Err(stop) =
observe_delivery_before_mark(delivery, &mut inflight.hwms, shutdown, lease_cancel).await
{
counter!(TILE_PRODUCE_ERRORS).increment(1);
return Err(stop);
}
}
Ok(())
}
/// Phase one (pre-mark): pace and enqueue every tile `newest_day` has no newer day for, honoring the
/// in-flight bound and draining overflow acks into the HWMs. Returns the still-`scanning` chunk (for
/// the store's mark) with the count sent, plus the in-flight deliveries. Every error hands the
/// [`ScannedChunk`] back so the caller can release or fail it.
pub(super) async fn enqueue_tiles(
producer: &SeedTileProducer,
chunk: ScannedChunk,
newest_day: &NewestDayIndex,
pacer: &TilePacer,
settings: ProducerSettings,
lease_cancel: &CancellationToken,
shutdown: &CancellationToken,
) -> Result<(StreamedChunk, InFlight), Halted<ScannedChunk, ProduceError>> {
let mut inflight = InFlight::new(settings.max_inflight.get());
let mut sent = 0;
for tile in chunk.tiles() {
// Checked before the pacer, so a newer day another chunk records while this one waits
// still skips this chunk's remaining tiles for that person. A skipped tile takes no pacer
// token, so the task budget makes it yield.
if newest_day.has_newer_day(tile) {
tokio::task::coop::consume_budget().await;
continue;
}
if let Err(stop) = enqueue_one(
|| producer.enqueue(tile),
&mut inflight,
pacer,
settings,
lease_cancel,
shutdown,
)
.await
{
return Err(stop.into_halted(chunk));
}
sent += 1;
}
Ok((chunk.into_streamed(sent), inflight))
}
/// Phase two (post-mark): drain the remaining acks into the HWMs and fold them onto the marked
/// chunk. A delivery failure hands the [`EnqueuedChunk`] back for fail-on-retry.
pub(super) async fn await_deliveries(
enqueued: EnqueuedChunk,
inflight: InFlight,
lease_cancel: &CancellationToken,
) -> Result<ProducedChunk, Halted<EnqueuedChunk, ProduceError>> {
let InFlight {
mut pending,
mut hwms,
} = inflight;
while let Some(delivery) = pending.pop_front() {
if let Err(stop) = observe_delivery_after_mark(delivery, &mut hwms, lease_cancel).await {
counter!(TILE_PRODUCE_ERRORS).increment(1);
return Err(stop.into_halted(enqueued));
}
}
Ok(enqueued.into_produced(hwms))
}
struct PendingDelivery {
delivery: DeliveryFuture,
sent_at: Instant,
}
/// A stop signal from one produce step: a cancellation cause or a terminal delivery error.
pub(super) enum ProduceStop {
Cancelled(CancelCause),
Failed(ProduceError),
}
impl ProduceStop {
pub(super) fn into_halted<S>(self, state: S) -> Halted<S, ProduceError> {
match self {
Self::Cancelled(cause) => Halted::cancelled(state, cause),
Self::Failed(error) => Halted::failed(state, error),
}
}
}
async fn observe_delivery_before_mark(
mut pending: PendingDelivery,
hwms: &mut ProduceHwms,
shutdown: &CancellationToken,
lease_cancel: &CancellationToken,
) -> Result<(), ProduceStop> {
let result = tokio::select! {
biased;
_ = shutdown.cancelled() => Err(ProduceStop::Cancelled(CancelCause::Shutdown)),
_ = lease_cancel.cancelled() => Err(ProduceStop::Cancelled(CancelCause::LeaseLost)),
delivery = resolve_delivery(&mut pending, hwms) => delivery.map_err(ProduceStop::Failed),
};
histogram!(PRODUCE_ACK_SECONDS).record(pending.sent_at.elapsed().as_secs_f64());
result
}
async fn observe_delivery_after_mark(
mut pending: PendingDelivery,
hwms: &mut ProduceHwms,
lease_cancel: &CancellationToken,
) -> Result<(), ProduceStop> {
let result = tokio::select! {
biased;
_ = lease_cancel.cancelled() => Err(ProduceStop::Cancelled(CancelCause::LeaseLost)),
delivery = resolve_delivery(&mut pending, hwms) => delivery.map_err(ProduceStop::Failed),
};
histogram!(PRODUCE_ACK_SECONDS).record(pending.sent_at.elapsed().as_secs_f64());
result
}
async fn resolve_delivery(
pending: &mut PendingDelivery,
hwms: &mut ProduceHwms,
) -> Result<(), ProduceError> {
match (&mut pending.delivery).await {
Ok(Ok((partition, offset))) => {
hwms.observe(partition, offset);
Ok(())
}
Ok(Err((error, _))) => Err(ProduceError::Delivery(error)),
Err(_) => Err(ProduceError::Delivery(KafkaError::Canceled)),
}
}
async fn wait_for_pacer(
pacer: &TilePacer,
shutdown: &CancellationToken,
lease_cancel: &CancellationToken,
) -> Result<(), CancelCause> {
tokio::select! {
biased;
_ = shutdown.cancelled() => Err(CancelCause::Shutdown),
_ = lease_cancel.cancelled() => Err(CancelCause::LeaseLost),
_ = pacer.until_ready() => Ok(()),
}
}
async fn wait_for_backoff(
backoff: Duration,
shutdown: &CancellationToken,
lease_cancel: &CancellationToken,
) -> Result<(), CancelCause> {
tokio::select! {
biased;
_ = shutdown.cancelled() => Err(CancelCause::Shutdown),
_ = lease_cancel.cancelled() => Err(CancelCause::LeaseLost),
_ = tokio::time::sleep(backoff) => Ok(()),
}
}
fn next_queue_full_backoff(current: Duration) -> Duration {
current.saturating_mul(2).min(QUEUE_FULL_BACKOFF_CAP)
}
#[derive(Debug, thiserror::Error)]
pub(super) enum ProduceError {
#[error("fatal tile enqueue error: {0}")]
Enqueue(KafkaError),
#[error("tile delivery failed: {0}")]
Delivery(KafkaError),
#[error("marking the chunk produced failed")]
MarkProduced(#[source] ChunkStoreError),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn queue_full_backoff_doubles_and_caps() {
assert_eq!(
next_queue_full_backoff(Duration::from_secs(4)),
QUEUE_FULL_BACKOFF_CAP
);
assert_eq!(
next_queue_full_backoff(QUEUE_FULL_BACKOFF_CAP),
QUEUE_FULL_BACKOFF_CAP
);
}
}