: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
feat(ingestion-consumer): separate the run cap from the pack target (#114489)
José Sequeirafdeaadf2cb6f
diff --git a/rust/ingestion-consumer/README.md b/rust/ingestion-consumer/README.mdindex feac8d7f1b87..49bf86c88f93 100644--- a/rust/ingestion-consumer/README.md+++ b/rust/ingestion-consumer/README.md@@ -53,7 +53,7 @@ Under `key_table`:- One task owns the state machine and applies polls, worker responses, revokes and retry wakeups one at a time.- Each key has a FIFO queue, and at most one request per key is in flight, which preserves per-key order.-- The packer builds a request only for a free worker slot, from ready keys up to a target size. Keys stay unclaimed until then, so a key's run keeps growing while every slot is busy, up to the target. A key's backlog past the target leaves in consecutive requests, one in flight at a time. A request never mixes assignment epochs or fresh and replayed messages.+- The packer builds a request only for a free worker slot, from ready keys up to a target size. Keys stay unclaimed until then, so a key's run keeps growing while every slot is busy, up to the run cap. A key's backlog past the run cap leaves in consecutive requests, one in flight at a time. The run cap is the target unless `INGESTION_RUN_CAP_*` sets it apart. A request never mixes assignment epochs or fresh and replayed messages.- A request is placed on a worker when it is sent, against the load at that moment. Each worker takes at most `INGESTION_WORKER_CONCURRENT_BATCHES` requests at a time.- A failed send returns its messages to the front of their keys' queues. They retry through the packer as replay after the retry delay.- A revoke drops the pending messages of the revoked partitions, because the new owner replays them.@@ -61,8 +61,10 @@ Under `key_table`:| Setting | Default | Effect under `key_table` || --- | --- | --- |-| `INGESTION_PACK_TARGET_EVENTS` | `500` | Send a request to a free slot once this many events are ready. It also caps one key's run. `0` disables the event target and the cap, and needs a byte target. |-| `INGESTION_PACK_TARGET_BYTES` | `0` | Send a request to a free slot once this many key-plus-value bytes are ready. It also caps one key's run. `0` disables the byte target and the cap, and needs an event target. |+| `INGESTION_PACK_TARGET_EVENTS` | `500` | Send a request to a free slot once this many events are ready. Unless a run cap is set, it also caps one key's run. `0` disables the event target and the cap, and needs a byte target. |+| `INGESTION_PACK_TARGET_BYTES` | `0` | Send a request to a free slot once this many key-plus-value bytes are ready. Unless a run cap is set, it also caps one key's run. `0` disables the byte target and the cap, and needs an event target. |+| `INGESTION_RUN_CAP_EVENTS` | `0` | The most events one key's run holds, which is the most of its backlog a key sends per round trip. With both run caps at `0`, the pack targets cap runs. When either is set, the run caps replace them, and a `0` cap leaves that dimension uncapped. |+| `INGESTION_RUN_CAP_BYTES` | `0` | One key's run stops growing once it holds this many key-plus-value bytes. Works with `INGESTION_RUN_CAP_EVENTS`. || `INGESTION_PACK_LATENCY_BUDGET_MS` | `0` | Once a slot is free, wait this long for ready events below the target to reach it. `0` waits for nothing: each free slot gets what is ready, packed up to the target. Replays never wait, because they already waited out a retry delay. || `INGESTION_PARKED_RETRY_INTERVAL_MS` | `200` | The delay before a failed send retries, and how often a request with no routable worker tries again. |diff --git a/rust/ingestion-consumer/src/batcher/packer.rs b/rust/ingestion-consumer/src/batcher/packer.rsindex a8f4cf82d5fc..6e85d1e3f893 100644--- a/rust/ingestion-consumer/src/batcher/packer.rs+++ b/rust/ingestion-consumer/src/batcher/packer.rs@@ -1,8 +1,8 @@//! Packs ready key runs into requests near a target size, one request per//! free worker slot.//!-//! Worst case: 2 × target − 1 events, or 2 × target − 2 bytes plus the largest-//! message. Keys join below the target, and a run is capped at the target.+//! Worst case: target − 1 events plus the run cap, or target + run cap − 2+//! bytes plus the largest message. Keys join below the target.use std::num::NonZeroUsize;use std::time::{Duration, Instant};@@ -53,6 +53,7 @@ impl PackReason {pub struct Packer {targets: PackTargets,+ run_cap: RunCap,reservation: Option<Instant>,}@@ -60,14 +61,24 @@ impl Packer {pub fn new(targets: PackTargets) -> Self {Self {targets,+ run_cap: targets.run_cap(),reservation: None,}}+ pub fn with_run_cap(mut self, run_cap: RunCap) -> Self {+ self.run_cap = run_cap;+ self+ }+pub fn targets(&self) -> PackTargets {self.targets}+ pub fn run_cap(&self) -> RunCap {+ self.run_cap+ }+pub fn latency_budget(&self) -> Duration {self.targets.latency_budget}diff --git a/rust/ingestion-consumer/src/batcher/state_machine.rs b/rust/ingestion-consumer/src/batcher/state_machine.rsindex ca7b3764e1cd..789388621a60 100644--- a/rust/ingestion-consumer/src/batcher/state_machine.rs+++ b/rust/ingestion-consumer/src/batcher/state_machine.rs@@ -101,7 +101,7 @@ impl BatcherStateMachine {return Err("set a pack target in events or bytes".to_string());}Ok(BatcherStateMachine::Running(ActiveState {- keys: KeyQueues::new(packer.targets().run_cap()),+ keys: KeyQueues::new(packer.run_cap()),packer,assigner,in_flight: InFlightRequests::new(),@@ -566,6 +566,7 @@ mod tests {use std::num::NonZeroUsize;use super::*;+ use crate::batcher::key_queues::RunCap;use crate::batcher::packer::PackTargets;use crate::batcher::test_support::{message, offsets};use crate::routing::{Router, RoutingStrategy};@@ -595,6 +596,14 @@ mod tests {bytes: None,latency_budget: budget,});+ batcher_with(packer, max_requests_per_worker, now)+ }++ fn batcher_with(+ packer: Packer,+ max_requests_per_worker: usize,+ now: Instant,+ ) -> BatcherStateMachine {let assigner = WorkerAssigner::new(Router::with_seed(RoutingStrategy::P2c, 0),max_requests_per_worker,@@ -1146,6 +1155,34 @@ mod tests {}}+ #[test]+ fn a_run_cap_above_the_target_sends_a_hot_keys_backlog_in_one_round_trip() {+ let now = Instant::now();+ let workers = pool(&["w"]);+ let packer = Packer::new(PackTargets {+ events: NonZeroUsize::new(2),+ bytes: None,+ latency_budget: Duration::ZERO,+ })+ .with_run_cap(RunCap {+ messages: NonZeroUsize::new(6),+ bytes: None,+ });+ let batcher = batcher_with(packer, 2, now);+ let (batcher, effects) = batcher.on_groups(now, &workers, 0, vec![run("hot", &[1])]);+ let request = effects.sends[0].request;++ let (batcher, effects) = batcher.on_groups(now, &workers, 0, vec![run("hot", &[2, 3, 4])]);+ assert!(effects.sends.is_empty());+ let (batcher, _) = batcher.on_groups(now, &workers, 0, vec![run("hot", &[5, 6, 7, 8])]);++ let (_, effects) = batcher.on_request_succeeded(now, &workers, request, 1);+ assert_eq!(+ effects.sends.iter().map(shape).collect::<Vec<_>>(),+ vec![vec![("hot", vec![2, 3, 4, 5, 6, 7])]]+ );+ }+#[test]fn the_pack_budget_starts_when_a_slot_frees_not_when_messages_arrive() {let now = Instant::now();diff --git a/rust/ingestion-consumer/src/config.rs b/rust/ingestion-consumer/src/config.rsindex 665d1aa3b20b..d95a5e6f8345 100644--- a/rust/ingestion-consumer/src/config.rs+++ b/rust/ingestion-consumer/src/config.rs@@ -6,6 +6,7 @@ use envconfig::Envconfig;use rdkafka::ClientConfig;use tracing::info;+use crate::batcher::key_queues::RunCap;use crate::batcher::packer::{PackTargets, Packer};use crate::batcher::retry_policy::RetryPolicy;use crate::batcher::state_machine::BatcherStateMachine;@@ -180,20 +181,29 @@ pub struct Config {pub parked_retry_interval_ms: u64,/// Target request size in events for the key-table packer: once this many- /// events are ready, a free worker slot gets a request at once. One key's- /// run never holds more, so a key's backlog leaves in consecutive requests.- /// `0` disables the event target and the cap, and needs a byte target.- /// Only read under `INGESTION_SCHEDULER=key_table`.+ /// events are ready, a free worker slot gets a request at once. Unless a+ /// run cap is set, one key's run never holds more, so a key's backlog+ /// leaves in consecutive requests. `0` disables the event target and the+ /// cap, and needs a byte target. Only read under+ /// `INGESTION_SCHEDULER=key_table`.#[envconfig(from = "INGESTION_PACK_TARGET_EVENTS", default = "500")]pub pack_target_events: usize,/// Target request size in key-plus-value bytes for the key-table packer.- /// One key's run never holds more. `0` (default) disables the byte target- /// and the cap, and needs an event target. Only read under- /// `INGESTION_SCHEDULER=key_table`.+ /// Unless a run cap is set, one key's run never holds more. `0` (default)+ /// disables the byte target and the cap, and needs an event target. Only+ /// read under `INGESTION_SCHEDULER=key_table`.#[envconfig(from = "INGESTION_PACK_TARGET_BYTES", default = "0")]pub pack_target_bytes: usize,+ /// With both run caps at `0` (default), the pack targets cap runs.+ /// Otherwise a `0` run cap leaves that dimension uncapped.+ #[envconfig(from = "INGESTION_RUN_CAP_EVENTS", default = "0")]+ pub run_cap_events: usize,++ #[envconfig(from = "INGESTION_RUN_CAP_BYTES", default = "0")]+ pub run_cap_bytes: usize,+/// How long ready events below the target wait for more, counted from/// when a worker slot is free for them, before the key-table packer sends/// them short of the target (milliseconds). `0` (default) waits for@@ -440,11 +450,18 @@ impl Config {/// un-acked cap as its per-worker request cap, the parked-retry interval/// for every retry, and the deferred-flush timeout as its stall timeout.pub fn batcher_state_machine(&self) -> Result<BatcherStateMachine, String> {- let packer = Packer::new(PackTargets {+ let mut packer = Packer::new(PackTargets {events: NonZeroUsize::new(self.pack_target_events),bytes: NonZeroUsize::new(self.pack_target_bytes),latency_budget: Duration::from_millis(self.pack_latency_budget_ms),});+ let run_cap = RunCap {+ messages: NonZeroUsize::new(self.run_cap_events),+ bytes: NonZeroUsize::new(self.run_cap_bytes),+ };+ if run_cap != RunCap::default() {+ packer = packer.with_run_cap(run_cap);+ }let assigner = WorkerAssigner::new(Router::new(self.routing_strategy),self.ingestion_worker_concurrent_batches,