: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
fix(capture): stop rate limiter blocking on evidence from failed reads (#107014)
Eli Reisman5f5acf87b1d3
diff --git a/rust/capture/src/config.rs b/rust/capture/src/config.rsindex 5fe2d0e61e13..ed5a7f994aa8 100644--- a/rust/capture/src/config.rs+++ b/rust/capture/src/config.rs@@ -194,6 +194,10 @@ pub struct Config {#[envconfig(default = "300")]pub global_rate_limit_local_cache_idle_timeout_secs: u64,+ /// Seconds reads (replica, then primary) must keep failing before a pod stops+ /// limiting on its own unconfirmed counts. Unset uses each limiter's window; `0` disables.+ pub global_rate_limit_max_read_outage_secs: Option<u64>,+/// Timeout for a single global rate limiter Redis read command (milliseconds).#[envconfig(default = "250")]pub global_rate_limit_read_timeout_ms: u64,diff --git a/rust/capture/src/global_rate_limiter.rs b/rust/capture/src/global_rate_limiter.rsindex 77ab8f1bafd6..91b787876a03 100644--- a/rust/capture/src/global_rate_limiter.rs+++ b/rust/capture/src/global_rate_limiter.rs@@ -312,6 +312,9 @@ impl GlobalRateLimiter {local_cache_max_entries: spec.local_cache_max_entries,metrics_scope: spec.metrics_scope.to_string(),min_sync_floor: spec.min_sync_floor,+ max_read_outage: config+ .global_rate_limit_max_read_outage_secs+ .map(Duration::from_secs),max_sync_keys_per_tick: config.global_rate_limit_max_sync_keys_per_tick,max_keys_per_command: config.global_rate_limit_max_keys_per_command,max_concurrent_commands: config.global_rate_limit_max_concurrent_commands,diff --git a/rust/capture/src/prometheus.rs b/rust/capture/src/prometheus.rsindex 78d7ae059757..1a043c629d10 100644--- a/rust/capture/src/prometheus.rs+++ b/rust/capture/src/prometheus.rs@@ -239,7 +239,8 @@ pub fn setup_metrics_recorder(role: String, capture_mode: &'static str) -> Prome&[0.001, 0.005, 0.01, 0.05, 0.1, 0.25, 0.5, 1.0];// Global rate limiter sync staleness (milliseconds)const GLOBAL_RATE_LIMITER_STALENESS_MS: &[f64] = &[- 100.0, 500.0, 1000.0, 5000.0, 10000.0, 15000.0, 30000.0, 60000.0,+ 100.0, 500.0, 1000.0, 5000.0, 10000.0, 15000.0, 30000.0, 60000.0, 90000.0, 120000.0,+ 300000.0, 3600000.0,];// Absolute client clock skew buckets (in seconds).diff --git a/rust/capture/src/v1/quota_limiter_shim.rs b/rust/capture/src/v1/quota_limiter_shim.rsindex 942d2d286444..317f2d7ef75f 100644--- a/rust/capture/src/v1/quota_limiter_shim.rs+++ b/rust/capture/src/v1/quota_limiter_shim.rs@@ -146,6 +146,7 @@ mod tests {global_rate_limit_token_local_cache_max_entries: 300_000,global_rate_limit_redis_url: None,global_rate_limit_redis_reader_url: None,+ global_rate_limit_max_read_outage_secs: None,global_rate_limit_redis_response_timeout_ms: None,global_rate_limit_redis_connection_timeout_ms: None,global_rate_limit_custom_threshold_key: None,diff --git a/rust/capture/tests/common/utils.rs b/rust/capture/tests/common/utils.rsindex d8d7e9e40698..17de5453e2ac 100644--- a/rust/capture/tests/common/utils.rs+++ b/rust/capture/tests/common/utils.rs@@ -66,6 +66,7 @@ pub static DEFAULT_CONFIG: Lazy<Config> = Lazy::new(|| Config {// Integration tests assert on exact limiter behavior at a threshold of// 10_000, so every key syncs and every tick drains fully.global_rate_limit_min_sync_floor: 0,+ global_rate_limit_max_read_outage_secs: None,global_rate_limit_max_sync_keys_per_tick: 20_000,global_rate_limit_max_keys_per_command: 2_000,global_rate_limit_max_concurrent_commands: 4,diff --git a/rust/common/limiters/GLOBAL_RATE_LIMITER.md b/rust/common/limiters/GLOBAL_RATE_LIMITER.mdindex 2e8850b36b1e..bc53be00fd01 100644--- a/rust/common/limiters/GLOBAL_RATE_LIMITER.md+++ b/rust/common/limiters/GLOBAL_RATE_LIMITER.md@@ -136,6 +136,38 @@ effective_level(entry, now) =This models the natural drain of events from the sliding window,keeping the local estimate useful for much longer than a simple stale/fresh binary.+**Once reads have failed for `max_read_outage`, `local_pending` cannot limit a key.**+`local_pending` is a correction to the last fleet count this node read, and only a successful read clears it.+Nothing else ages it, so while reads to Redis fail it grows without bound.+Given a long enough read outage, one node's count reaches the threshold on its own and limits a key that is under its limit.+Everywhere else the limiter fails open when Redis cannot answer, so this path would fail closed.++So this node records, for each Redis instance, when its current run of failed reads began.+A tick with any successful read clears the record, a tick whose every read failed starts it if it is not running, and a tick with no reads leaves it alone.+A quiet period before a failure therefore never counts. A quiet period after a failure does, because no read has confirmed the count since.+A read fails only when the client cannot complete it.+With the read/write split client, a replica read that the primary then answers counts as a success, so the guard trips only when both fail.+The record is wall-clock time, not a tick count, because during an outage one tick can wait seconds on timeouts and reconnects.+Once reads have failed for `max_read_outage`, a key that instance owns is judged by its decayed fleet count alone:++```text+limit_level = reads failing ? max(0, estimated_count - leak_rate × elapsed)+ : effective_level+```++The fleet count still drains as the window moves, so a key known to be over its limit keeps being limited until the evidence runs out.+The guard gates only the limit decision.+It does not reset the entry, change its counts, or change when it syncs, and it runs only for a key already over its threshold.++The signal is per instance, not global.+With more than one instance, a global signal would let a healthy instance hide a dead one, and the dead one's keys would fail closed again.+`max_read_outage` defaults to one `window_interval`.+A key under its limit sees fewer than `threshold` events in any one window, so this node's count for it can reach the threshold only once that count spans more than one window.+The count spans the outage plus the time since the key's last read before it, which is about one sync cadence of the key's tier.+So a false limit can happen only in that last cadence before the guard engages, and only for a key whose traffic lands mostly on this node.+For example, with steady traffic and a window eight times the sync interval, only a key above about 94% of its limit is exposed, for at most half a sync interval.+To close even that gap, set `max_read_outage` to `window_interval - sync_interval` or less. That bound holds while `sync_interval` is at most a third of `window_interval`.+```textLevel│@@ -265,6 +297,7 @@ running value so an alert can compare them.| `window_interval` | 60s | `GLOBAL_RATE_LIMIT_WINDOW_INTERVAL_SECS` | Sliding window size for the 2-epoch counter || `sync_interval` | 15s | `GLOBAL_RATE_LIMIT_SYNC_INTERVAL_SECS` | Base staleness before re-sync (adaptive tiers multiply this) || `tick_interval` | 1s | `GLOBAL_RATE_LIMIT_TICK_INTERVAL_MS` | Background pipeline cadence |+| `max_read_outage` | `window_interval` | `GLOBAL_RATE_LIMIT_MAX_READ_OUTAGE_SECS` | How long, in wall-clock time, reads to an instance must keep failing before its keys stop limiting on this node's unconfirmed counts. A replica read that the primary answers counts as a success. Derived from the configured window, not the default. `0` disables the guard || `custom_keys` | empty | `GLOBAL_RATE_LIMIT_OVERRIDES_CSV` | Per-key threshold overrides (`key=limit,...`) |#### Local cache (Moka)@@ -430,7 +463,9 @@ never hashes. Two things to know before that changes.| Metric | Type | Purpose ||---|---|---|| `global_rate_limiter_eval_counts_total` | Counter | Core allow/limit decisions |-| `global_rate_limiter_cache_counts_total` | Counter | Cache hit/miss/sync_queued |+| `global_rate_limiter_cache_counts_total` | Counter | Cache hit/miss/sync_queued, exactly one per evaluation |+| `global_rate_limiter_outage_limit_skipped_total` | Counter | Limits withheld because the key's instance was in a read outage. It stays at zero while reads work |+| `global_rate_limiter_read_failing_seconds` | Gauge | Seconds since reads to one instance began failing, per `redis_idx`, set on each tick that reads. Zero while reads succeed. The guard engages once it reaches `max_read_outage` || `global_rate_limiter_pipeline_ms` | Histogram | Redis pipeline latency || `global_rate_limiter_tick_ms` | Histogram | Full tick duration || `global_rate_limiter_pipeline_size` | Histogram | Entities per pipeline (read/write) |@@ -441,7 +476,7 @@ never hashes. Two things to know before that changes.| `global_rate_limiter_window_seconds` | Gauge | Configured `window_interval` per scope. Deployments that share a Redis key prefix must report the same value, or their epoch keys diverge and the shared counter splits || `global_rate_limiter_eviction_total` | Counter | Cache evictions by cause (size/expired/explicit) || `global_rate_limiter_estimate_drift` | Histogram | Local vs Redis accuracy |-| `global_rate_limiter_sync_staleness_ms` | Histogram | Real staleness at access time |+| `global_rate_limiter_sync_staleness_ms` | Histogram | Real staleness at access time. A large share sits past one minute in normal operation, from idle keys that stopped syncing below the floor, so read its tail as expected rather than as an outage || `global_rate_limiter_error_total` | Counter | Pipeline errors and timeouts || `global_rate_limiter_records_total` | Counter | Total Redis commands issued |diff --git a/rust/common/limiters/benches/global_rate_limiter.rs b/rust/common/limiters/benches/global_rate_limiter.rsindex 9316207c6a9d..0e1834483815 100644--- a/rust/common/limiters/benches/global_rate_limiter.rs+++ b/rust/common/limiters/benches/global_rate_limiter.rs@@ -90,6 +90,7 @@ fn bench_config() -> GlobalRateLimiterConfig {// The benchmark measures the hot path against a fully-syncing limiter, so// the floor and the per-tick bound are both left wide open.min_sync_floor: 0,+ max_read_outage: None,max_sync_keys_per_tick: 100_000,max_keys_per_command: 2_000,max_concurrent_commands: 4,diff --git a/rust/common/limiters/src/global_rate_limiter.rs b/rust/common/limiters/src/global_rate_limiter.rsindex 5437a55551f8..1f65337967a3 100644--- a/rust/common/limiters/src/global_rate_limiter.rs+++ b/rust/common/limiters/src/global_rate_limiter.rs@@ -1,6 +1,7 @@use std::collections::hash_map::DefaultHasher;use std::collections::HashMap;use std::hash::{Hash, Hasher};+use std::sync::atomic::{AtomicU64, Ordering};use std::sync::Arc;use std::time::{Duration, Instant};@@ -52,6 +53,13 @@ const GLOBAL_RATE_LIMITER_WRITE_DEFERRED_GAUGE: &str = "global_rate_limiter_writconst GLOBAL_RATE_LIMITER_SYNC_SKIPPED_COUNTER: &str = "global_rate_limiter_sync_skipped_total";/// Redis commands issued per tick, after chunking.const GLOBAL_RATE_LIMITER_COMMANDS_HISTOGRAM: &str = "global_rate_limiter_commands_per_tick";+/// Seconds since reads to one Redis instance began failing; zero while they succeed.+const GLOBAL_RATE_LIMITER_READ_FAILING_SECONDS_GAUGE: &str =+ "global_rate_limiter_read_failing_seconds";+/// Limits withheld because the key's Redis instance was in a read outage. A+/// separate counter, since `cache_counts_total` has one result per evaluation.+const GLOBAL_RATE_LIMITER_OUTAGE_LIMIT_SKIPPED_COUNTER: &str =+ "global_rate_limiter_outage_limit_skipped_total";/// Number of custom-key thresholds applied at the last successful refresh.const CUSTOM_THRESHOLDS_LOADED_GAUGE: &str = "global_rate_limiter_custom_thresholds_loaded";/// Unix timestamp of the last successful custom-key threshold refresh.@@ -207,6 +215,9 @@ pub struct GlobalRateLimiterConfig {////// Set to 0 to sync every key, restoring the pre-floor behavior.pub min_sync_floor: u64,+ /// Wall-clock time reads to an instance must keep failing before its keys stop+ /// limiting on this node's own counts. `None` uses `window_interval`; `Some(ZERO)` disables.+ pub max_read_outage: Option<Duration>,/// Maximum keys drained from `pending_sync` per tick. The remainder stays/// queued for the next tick, so a backlog degrades into staleness instead of/// a tick loop that overruns its own interval.@@ -301,6 +312,7 @@ impl Default for GlobalRateLimiterConfig {local_cache_max_entries: 300_000,channel_capacity: 1_000_000,min_sync_floor: 10,+ max_read_outage: None,max_sync_keys_per_tick: 20_000,max_keys_per_command: 2_000,max_concurrent_commands: 4,@@ -336,13 +348,18 @@ pub struct CacheEntry {/// locally observed events. This keeps the estimate conservative (includes all/// local events) while allowing the global contribution to drain away.pub fn effective_level(entry: &CacheEntry, leak_rate: f64, now: Instant) -> f64 {- // Never synced: nothing to decay yet, so the level is local counts alone.+ decayed_global(entry, leak_rate, now) + entry.local_pending as f64+}++/// The last fleet count read from Redis, decayed to `now`, excluding this node's+/// unconfirmed events; zero if never read.+pub fn decayed_global(entry: &CacheEntry, leak_rate: f64, now: Instant) -> f64 {let Some(synced_at) = entry.synced_at else {- return entry.local_pending as f64;+ return 0.0;};let elapsed = now.duration_since(synced_at).as_secs_f64();let drained = leak_rate * elapsed;- (entry.estimated_count - drained).max(0.0) + entry.local_pending as f64+ (entry.estimated_count - drained).max(0.0)}/// Compute the epoch number from a unix timestamp and window interval.@@ -433,23 +450,69 @@ enum CheckMode {Custom,}+/// When each instance's current run of failed reads began; a tick with no reads+/// leaves it alone. Time, not tick counts, because an outage can stall one tick for seconds.+struct ReadHealth {+ /// Reference point for `failing_since`, because an atomic cannot hold an `Instant`.+ base: Instant,+ /// Per instance: ms from `base` to the run's first failed read, plus one; zero while healthy.+ failing_since: Vec<AtomicU64>,+}++impl ReadHealth {+ fn new(instances: usize) -> Self {+ Self {+ base: Instant::now(),+ failing_since: (0..instances).map(|_| AtomicU64::new(0)).collect(),+ }+ }++ fn instance_count(&self) -> usize {+ self.failing_since.len()+ }++ /// Only the background task calls this, one tick at a time per instance, so+ /// a plain load and store cannot race.+ fn record_tick(&self, idx: usize, any_read_ok: bool, reads_started: Instant) {+ let slot = &self.failing_since[idx];+ if any_read_ok {+ slot.store(0, Ordering::Relaxed);+ } else if slot.load(Ordering::Relaxed) == 0 {+ let offset_ms = reads_started+ .saturating_duration_since(self.base)+ .as_millis() as u64;+ slot.store(offset_ms + 1, Ordering::Relaxed);+ }+ }++ fn failing_for(&self, idx: usize, now: Instant) -> Option<Duration> {+ let since = self.failing_since[idx].load(Ordering::Relaxed);+ if since == 0 {+ return None;+ }+ let started = self.base + Duration::from_millis(since - 1);+ Some(now.saturating_duration_since(started))+ }+}++/// Index of the Redis instance that owns `key`. SipHash-1-3, not `DefaultHasher`, because+/// every pod must agree on the owner across Rust releases or the key's counter splits.+fn instance_index(key: &str, instances: usize) -> usize {+ if instances <= 1 {+ return 0;+ }+ let mut hasher = SipHasher13::new();+ key.hash(&mut hasher);+ (hasher.finish() as usize) % instances+}+/// Select a Redis client from the pool based on consistent key hashing./// Returns (client_ref, index) tuple for metric tagging.-///-/// Uses SipHash-1-3 rather than `DefaultHasher`, whose algorithm the standard-/// library does not guarantee across releases. Every pod has to agree on the-/// owner of a key, so a toolchain bump mid-rollout would otherwise split one-/// entity's counter across two instances and under-enforce its limit.fn select_redis_client(key: &str,clients: &[Arc<dyn Client + Send + Sync>],) -> (Arc<dyn Client + Send + Sync>, usize) {- if clients.len() == 1 {- return (clients[0].clone(), 0);- }- let mut hasher = SipHasher13::new();- key.hash(&mut hasher);- let idx = (hasher.finish() as usize) % clients.len();+ let idx = instance_index(key, clients.len());(clients[idx].clone(), idx)}@@ -505,6 +568,10 @@ pub struct GlobalRateLimiterImpl {/// Drop-to-stop signal for the custom-key refresh task (mirrors `update_tx`)./// `None` when no `custom_key_source` was configured.custom_key_refresh_stop: Option<mpsc::Sender<()>>,+ /// One entry per Redis instance, indexed like `select_redis_client`.+ read_health: Arc<ReadHealth>,+ /// How long reads must fail before an instance counts as down; `None` disables the guard.+ read_outage_limit: Option<Duration>,}#[async_trait]@@ -639,13 +706,17 @@ impl GlobalRateLimiterImpl {stop_tx});+ let read_health = Arc::new(ReadHealth::new(redis_instances.len()));+let limiter = Self {+ read_outage_limit: Self::read_outage_limit(&config),config: config.clone(),cache: cache.clone(),update_tx: Some(update_tx),pending_sync: pending_sync.clone(),scope,custom_key_refresh_stop,+ read_health: read_health.clone(),};Self::spawn_background_task(@@ -654,6 +725,7 @@ impl GlobalRateLimiterImpl {update_rx,cache,pending_sync,+ read_health,scope,);@@ -689,7 +761,7 @@ impl GlobalRateLimiterImpl {}// Check local cache- let (level, entry_exists) = if let Some(mut entry) = self.cache.get(key) {+ let (level, global_level, entry_exists) = if let Some(mut entry) = self.cache.get(key) {let level = effective_level(&entry, leak_rate, now_instant);if let Some(synced_at) = entry.synced_at {@@ -734,9 +806,10 @@ impl GlobalRateLimiterImpl {// Increment local_pending and recompute level with this request includedentry.local_pending += count;let level = effective_level(&entry, leak_rate, now_instant);+ let global_level = decayed_global(&entry, leak_rate, now_instant);self.cache.insert(key.to_string(), entry);- (level, true)+ (level, global_level, true)} else {// Cache miss: no prior data, allow through and queue syncmetrics::counter!(GLOBAL_RATE_LIMITER_CACHE_COUNTER, "scope" => self.scope, "result" => "miss").increment(1);@@ -753,11 +826,18 @@ impl GlobalRateLimiterImpl {self.queue_sync(key);}- (count as f64, false)+ (count as f64, 0.0, false)};- // Determine if key is rate limited- let is_limited = entry_exists && level >= threshold as f64;+ // While reads fail, `local_pending` grows with nothing to correct it, so judge+ // the key by its decayed fleet count alone.+ let over_threshold = entry_exists && level >= threshold as f64;+ let read_outage = over_threshold && self.read_outage(key, now_instant);+ let is_limited = over_threshold && (!read_outage || global_level >= threshold as f64);+ if read_outage && !is_limited {+ metrics::counter!(GLOBAL_RATE_LIMITER_OUTAGE_LIMIT_SKIPPED_COUNTER, "scope" => self.scope)+ .increment(1);+ }if is_limited {metrics::counter!(GLOBAL_RATE_LIMITER_EVAL_COUNTER, "scope" => self.scope, "result" => "limited").increment(1);@@ -806,6 +886,21 @@ impl GlobalRateLimiterImpl {true}+ fn read_outage_limit(config: &GlobalRateLimiterConfig) -> Option<Duration> {+ let limit = config.max_read_outage.unwrap_or(config.window_interval);+ (!limit.is_zero()).then_some(limit)+ }++ fn read_outage(&self, key: &str, now: Instant) -> bool {+ let Some(limit) = self.read_outage_limit else {+ return false;+ };+ let idx = instance_index(key, self.read_health.instance_count());+ self.read_health+ .failing_for(idx, now)+ .is_some_and(|failing| failing >= limit)+ }+/// Queue a key for background Redis sync, bounded by/// `max_pending_sync_entries`. A dropped request fails open for one round:/// the key's next request re-queues it once the backlog drains, and its@@ -942,6 +1037,7 @@ impl GlobalRateLimiterImpl {mut update_rx: mpsc::Receiver<UpdateRequest>,cache: Cache<String, CacheEntry>,pending_sync: Arc<DashSet<String>>,+ read_health: Arc<ReadHealth>,scope: &'static str,) {tokio::spawn(async move {@@ -970,7 +1066,7 @@ impl GlobalRateLimiterImpl {if !write_batch.is_empty() {Self::tick(&config, &redis_instances, &cache,- &pending_sync, &mut write_batch, scope, tick_n,+ &pending_sync, &mut write_batch, &read_health, scope, tick_n,).await;}break;@@ -981,7 +1077,7 @@ impl GlobalRateLimiterImpl {tick_n = tick_n.wrapping_add(1);Self::tick(&config, &redis_instances, &cache,- &pending_sync, &mut write_batch, scope, tick_n,+ &pending_sync, &mut write_batch, &read_health, scope, tick_n,).await;}}@@ -1030,12 +1126,14 @@ impl GlobalRateLimiterImpl {////// Drains pending reads + writes, builds a single pipeline, executes it,/// and processes read responses to update cache entries.+ #[allow(clippy::too_many_arguments)]async fn tick(config: &GlobalRateLimiterConfig,redis_instances: &[Arc<dyn Client + Send + Sync>],cache: &Cache<String, CacheEntry>,pending_sync: &Arc<DashSet<String>>,write_batch: &mut HashMap<(String, i64), u64>,+ read_health: &ReadHealth,scope: &'static str,tick_n: u64,) {@@ -1125,12 +1223,21 @@ impl GlobalRateLimiterImpl {cache,&sync_keys,&writes,+ read_health,scope,).await;} else {- Self::tick_multi_instance(config, redis_instances, cache, &sync_keys, &writes, scope)- .await;+ Self::tick_multi_instance(+ config,+ redis_instances,+ cache,+ &sync_keys,+ &writes,+ read_health,+ scope,+ )+ .await;}metrics::histogram!(GLOBAL_RATE_LIMITER_TICK_HISTOGRAM, "scope" => scope)@@ -1146,6 +1253,7 @@ impl GlobalRateLimiterImpl {cache: &Cache<String, CacheEntry>,sync_keys: &[String],writes: &HashMap<(String, i64), u64>,+ read_health: &ReadHealth,scope: &'static str,) {let redis_idx_str: Arc<str> = Arc::from(redis_idx.to_string().as_str());@@ -1161,8 +1269,18 @@ impl GlobalRateLimiterImpl {// read before they land; that loss is bounded by the cap and fails// open, like every other overload path here.let writes_issued = Self::run_writes(config, redis, &redis_idx_str, writes, scope).await;- let reads_issued =- Self::run_reads(config, redis, &redis_idx_str, cache, sync_keys, now, scope).await;+ let reads_issued = Self::run_reads(+ config,+ redis,+ &redis_idx_str,+ cache,+ sync_keys,+ now,+ read_health,+ redis_idx,+ scope,+ )+ .await;metrics::histogram!(GLOBAL_RATE_LIMITER_COMMANDS_HISTOGRAM, "scope" => scope, "op" => "write").record(writes_issued as f64);@@ -1269,6 +1387,8 @@ impl GlobalRateLimiterImpl {cache: &Cache<String, CacheEntry>,sync_keys: &[String],now: DateTime<Utc>,+ read_health: &ReadHealth,+ redis_idx: usize,scope: &'static str,) -> usize {if sync_keys.is_empty() {@@ -1280,6 +1400,9 @@ impl GlobalRateLimiterImpl {let entities_per_chunk = (config.max_keys_per_command / 2).max(1);let chunks: Vec<&[String]> = sync_keys.chunks(entities_per_chunk).collect();let issued = chunks.len();+ let mut any_read_ok = false;+ // Start the outage clock when the reads went out, not when a stalled tick ends.+ let reads_started = Instant::now();// See `run_writes` for why this is waves of `join_all` rather than a// `buffer_unordered` stream.@@ -1315,6 +1438,7 @@ impl GlobalRateLimiterImpl {.record(started.elapsed().as_micros() as f64 / 1000.0);Self::process_read_results(config, cache, chunk, &results, now, scope);+ true}Ok(Err(e)) => {Self::record_pipeline_error(scope, &redis_idx_str, "redis_error");@@ -1322,17 +1446,35 @@ impl GlobalRateLimiterImpl {if e.is_unrecoverable_error() {redis.heal().await;}+ false}Err(_) => {Self::record_pipeline_error(scope, &redis_idx_str, "read_timeout");warn!(keys = chunk.len(), "Redis read timeout in pipeline");+ false}}}});- futures::future::join_all(futures).await;+ any_read_ok |= futures::future::join_all(futures)+ .await+ .into_iter()+ .any(|ok| ok);}+ // One success means the instance answers, so a tick of mixed results+ // counts as healthy.+ read_health.record_tick(redis_idx, any_read_ok, reads_started);+ let failing_for = read_health+ .failing_for(redis_idx, Instant::now())+ .unwrap_or_default();+ metrics::gauge!(+ GLOBAL_RATE_LIMITER_READ_FAILING_SECONDS_GAUGE,+ "scope" => scope,+ "redis_idx" => redis_idx_str.clone(),+ )+ .set(failing_for.as_secs_f64());+issued}@@ -1356,6 +1498,7 @@ impl GlobalRateLimiterImpl {cache: &Cache<String, CacheEntry>,sync_keys: &[String],writes: &HashMap<(String, i64), u64>,+ read_health: &ReadHealth,scope: &'static str,) {// Partition reads by Redis instance@@ -1395,6 +1538,7 @@ impl GlobalRateLimiterImpl {&cache,&reads,&writes_partition,+ read_health,scope,).await;@@ -1538,7 +1682,7 @@ fn parse_redis_count(value: &Option<Vec<u8>>) -> u64 {mod tests {use super::*;use crate::custom_key_source::testing::MockCustomKeyThresholdSource;- use common_redis::MockRedisClient;+ use common_redis::{CustomRedisError, MockRedisClient};fn test_config() -> GlobalRateLimiterConfig {GlobalRateLimiterConfig {@@ -1563,6 +1707,7 @@ mod tests {// suppress every sync. 0 keeps the pre-floor behavior; the floor's// own behavior is covered by the dedicated tests below.min_sync_floor: 0,+ max_read_outage: None,max_sync_keys_per_tick: 20_000,max_keys_per_command: 2_000,max_concurrent_commands: 4,@@ -2467,6 +2612,7 @@ mod tests {&cache,&pending,&mut writes,+ &ReadHealth::new(1),"test",1,)@@ -2551,6 +2697,7 @@ mod tests {&cache,&pending,&mut writes,+ &ReadHealth::new(1),"test",1,)@@ -2601,6 +2748,7 @@ mod tests {&cache,&pending,&mut writes,+ &ReadHealth::new(1),"test",1,)@@ -2615,6 +2763,290 @@ mod tests {);}+ /// Poll until the outcome shows. The tick loop runs on real time, so a test waits for+ /// what it can observe instead of a guessed delay.+ macro_rules! wait_until {+ ($why:expr, $cond:expr) => {{+ let deadline = tokio::time::Instant::now() + Duration::from_secs(5);+ while !$cond {+ assert!(tokio::time::Instant::now() < deadline, "{}", $why);+ tokio::time::sleep(Duration::from_millis(5)).await;+ }+ }};+ }++ /// The read-outage guard engages on the first failed read, so a test waits for ticks,+ /// not for a window of wall-clock time.+ fn outage_config(threshold: u64) -> GlobalRateLimiterConfig {+ GlobalRateLimiterConfig {+ global_threshold: threshold,+ max_read_outage: Some(Duration::from_nanos(1)),+ tick_interval: Duration::from_millis(10),+ ..config_with_floor(0)+ }+ }++ fn limited(result: EvalResult) -> bool {+ matches!(result, EvalResult::Limited(_))+ }++ fn failing_reads() -> Arc<dyn Client + Send + Sync> {+ Arc::new(MockRedisClient::new().mget_error(CustomRedisError::Timeout))+ }++ async fn wait_for_read_outage(limiter: &GlobalRateLimiterImpl, threshold: u64) {+ let _ = limiter.check_limit("outage_probe", threshold, None).await;+ wait_until!(+ "reads keep failing, so the guard must engage",+ !limited(limiter.check_limit("outage_probe", 1, None).await)+ );+ }++ struct FleetCountCase {+ name: &'static str,+ reads_failing: bool,+ last_read: Option<Instant>,+ fleet_count_at_last_read: f64,+ events_counted_here_since: u64,+ expect_limited: bool,+ }++ #[tokio::test]+ async fn test_read_outage_judges_a_key_by_its_fleet_count_alone() {+ let now = Instant::now();+ let cases = [+ FleetCountCase {+ name: "reads failing, never read, this pod's count alone reaches the limit",+ reads_failing: true,+ last_read: None,+ fleet_count_at_last_read: 0.0,+ events_counted_here_since: 10_000,+ expect_limited: false,+ },+ FleetCountCase {+ name: "reads failing, fleet count under, this pod's count pushes over",+ reads_failing: true,+ last_read: Some(now),+ fleet_count_at_last_read: 5_000.0,+ events_counted_here_since: 10_000,+ expect_limited: false,+ },+ FleetCountCase {+ name: "reads failing, fleet count itself over",+ reads_failing: true,+ last_read: Some(now),+ fleet_count_at_last_read: 12_000.0,+ events_counted_here_since: 0,+ expect_limited: true,+ },+ FleetCountCase {+ name: "reads healthy, never read, this pod's count alone reaches the limit",+ reads_failing: false,+ last_read: None,+ fleet_count_at_last_read: 0.0,+ events_counted_here_since: 10_000,+ expect_limited: true,+ },+ FleetCountCase {+ name: "reads healthy, fleet count under, this pod's count pushes over",+ reads_failing: false,+ last_read: Some(now),+ fleet_count_at_last_read: 5_000.0,+ events_counted_here_since: 10_000,+ expect_limited: true,+ },+ ];++ for case in cases {+ let redis: Arc<dyn Client + Send + Sync> = if case.reads_failing {+ failing_reads()+ } else {+ Arc::new(MockRedisClient::new())+ };+ let limiter = GlobalRateLimiterImpl::new(outage_config(10_000), vec![redis]).unwrap();+ if case.reads_failing {+ wait_for_read_outage(&limiter, 10_000).await;+ }+ limiter.cache.insert(+ "key".to_string(),+ CacheEntry {+ estimated_count: case.fleet_count_at_last_read,+ synced_at: case.last_read,+ local_pending: case.events_counted_here_since,+ pressure: 0.0,+ },+ );++ let result = limiter.check_limit("key", 1, None).await;++ assert_eq!(+ limited(result),+ case.expect_limited,+ "{}: while reads fail, only the fleet count this pod last read may limit the key",+ case.name+ );+ assert_eq!(+ limiter.cache.get("key").unwrap().local_pending,+ case.events_counted_here_since + 1,+ "{}: the guard withholds the limit and changes nothing else",+ case.name+ );+ }+ }++ #[tokio::test]+ async fn test_read_outage_engages_on_failed_reads_and_clears_on_the_next_success() {+ let mut redis = MockRedisClient::new();+ for call in 0..20 {+ redis = redis.mget_error_at_call(call, CustomRedisError::Timeout);+ }+ let limiter = GlobalRateLimiterImpl::new(outage_config(10), vec![Arc::new(redis)]).unwrap();+ let _ = limiter.check_limit("key", 10, None).await;++ assert!(+ limited(limiter.check_limit("key", 1, None).await),+ "before any read has failed, this pod's own count limits the key"+ );+ wait_until!(+ "failed reads must engage the guard",+ !limited(limiter.check_limit("key", 10, None).await)+ );+ wait_until!(+ "the first successful read must clear the guard, so this pod's count limits again",+ limited(limiter.check_limit("key", 10, None).await)+ );+ }++ #[tokio::test]+ async fn test_a_pod_that_never_reads_never_enters_a_read_outage() {+ let config = GlobalRateLimiterConfig {+ max_pending_sync_entries: 0,+ ..outage_config(10)+ };+ let limiter = GlobalRateLimiterImpl::new(config, vec![failing_reads()]).unwrap();+ let _ = limiter.check_limit("key", 10, None).await;+ // Ticks run with nothing to read. The claim holds whether zero or many ran.+ tokio::time::sleep(Duration::from_millis(50)).await;++ assert!(+ limited(limiter.check_limit("key", 1, None).await),+ "no read was attempted, so none failed, and this pod keeps limiting on its own count"+ );+ }++ #[tokio::test]+ async fn test_read_outage_is_judged_per_redis_instance() {+ let clients: Vec<Arc<dyn Client + Send + Sync>> =+ vec![failing_reads(), Arc::new(MockRedisClient::new())];+ let limiter = GlobalRateLimiterImpl::new(outage_config(10_000), clients).unwrap();+ // `instance_index` is the routing rule every pod shares; the test needs one key per instance.+ let key_on = |instance: usize| -> String {+ (0..)+ .map(|i| format!("key{i}"))+ .find(|k| instance_index(k, 2) == instance)+ .unwrap()+ };+ let (on_down, on_healthy) = (key_on(0), key_on(1));+ for key in [&on_down, &on_healthy] {+ let _ = limiter.check_limit(key, 10_000, None).await;+ }++ wait_until!(+ "the failing instance must enter a read outage",+ !limited(limiter.check_limit(&on_down, 1, None).await)+ );+ assert!(+ limited(limiter.check_limit(&on_healthy, 10_000, None).await),+ "an outage on one instance must not withhold limits on a key the healthy one owns"+ );+ }++ #[tokio::test]+ async fn test_one_successful_read_in_a_tick_keeps_the_instance_healthy() {+ let redis =+ Arc::new(MockRedisClient::new().mget_error_at_call(0, CustomRedisError::Timeout));+ let config = GlobalRateLimiterConfig {+ // One entity per read command, so two keys make two reads in one tick.+ max_keys_per_command: 2,+ ..outage_config(10_000)+ };+ let limiter = GlobalRateLimiterImpl::new(config, vec![redis.clone()]).unwrap();+ for key in ["a", "b"] {+ let _ = limiter.check_limit(key, 10_000, None).await;+ }++ wait_until!(+ "both reads must go out",+ redis.get_calls().iter().filter(|c| c.op == "mget").count() >= 2+ );+ for key in ["a", "b"] {+ assert!(+ limited(limiter.check_limit(key, 10_000, None).await),+ "one read failed and one succeeded, so the instance is reachable and {key} stays \+ limited on this pod's count"+ );+ }+ }++ #[test]+ fn test_read_health_keeps_the_start_of_the_current_run_of_failed_reads() {+ let health = ReadHealth::new(2);+ let at = |secs: u64| health.base + Duration::from_secs(secs);++ assert_eq!(+ health.failing_for(0, at(10)),+ None,+ "healthy until a read fails"+ );+ health.record_tick(0, false, at(10));+ health.record_tick(0, false, at(40));+ assert_eq!(+ health.failing_for(0, at(70)),+ Some(Duration::from_secs(60)),+ "a later failed tick must not restart the run"+ );+ assert_eq!(+ health.failing_for(1, at(70)),+ None,+ "each instance has its own run"+ );+ health.record_tick(0, true, at(80));+ assert_eq!(+ health.failing_for(0, at(90)),+ None,+ "one successful tick ends the run"+ );+ health.record_tick(0, false, at(100));+ assert_eq!(+ health.failing_for(0, at(105)),+ Some(Duration::from_secs(5)),+ "the next failure starts a new run"+ );+ }++ #[test]+ fn test_read_outage_limit_follows_the_window_unless_overridden() {+ // (window_secs, override, expected_secs)+ let cases = [+ (60, None, Some(60)),+ (600, None, Some(600)),+ (120, Some(Duration::from_secs(30)), Some(30)),+ (120, Some(Duration::ZERO), None),+ ];+ for (window_secs, max_read_outage, expected_secs) in cases {+ let config = GlobalRateLimiterConfig {+ window_interval: Duration::from_secs(window_secs),+ max_read_outage,+ ..test_config()+ };+ assert_eq!(+ GlobalRateLimiterImpl::read_outage_limit(&config),+ expected_secs.map(Duration::from_secs),+ "window {window_secs}s, override {max_read_outage:?}: unset follows the window, zero disables the guard"+ );+ }+ }+#[tokio::test]async fn test_idle_tier_key_above_floor_still_syncs() {let client = Arc::new(MockRedisClient::new());@@ -2669,6 +3101,7 @@ mod tests {&cache,&pending,&mut writes,+ &ReadHealth::new(1),"test",1,)diff --git a/rust/common/limiters/tests/global_rate_limiter_integration_tests.rs b/rust/common/limiters/tests/global_rate_limiter_integration_tests.rsindex dfbb7c03d5ce..e8faff88d419 100644--- a/rust/common/limiters/tests/global_rate_limiter_integration_tests.rs+++ b/rust/common/limiters/tests/global_rate_limiter_integration_tests.rs@@ -58,6 +58,7 @@ fn test_config(test_name: &str) -> GlobalRateLimiterConfig {// These tests assert exact Redis counter values against a threshold of// 1000, so every key must sync and every tick must drain fully.min_sync_floor: 0,+ max_read_outage: None,max_sync_keys_per_tick: 10_000,max_keys_per_command: 2_000,max_concurrent_commands: 4,diff --git a/rust/common/redis/src/mock.rs b/rust/common/redis/src/mock.rsindex 2a695cd0791c..16ec17116de9 100644--- a/rust/common/redis/src/mock.rs+++ b/rust/common/redis/src/mock.rs@@ -30,6 +30,9 @@ pub struct MockRedisClient {pipeline_errors_by_call: HashMap<usize, CustomRedisError>,/// Shared via `Arc` so clones of the mock agree on call sequencing.pipeline_call_counter: Arc<AtomicUsize>,+ mget_errors_by_call: HashMap<usize, CustomRedisError>,+ /// Shared via `Arc` so clones of the mock agree on call sequencing.+ mget_call_counter: Arc<AtomicUsize>,calls: Arc<Mutex<Vec<MockRedisCall>>>,}@@ -56,6 +59,8 @@ impl Default for MockRedisClient {pipeline_block: None,pipeline_errors_by_call: HashMap::new(),pipeline_call_counter: Arc::new(AtomicUsize::new(0)),+ mget_errors_by_call: HashMap::new(),+ mget_call_counter: Arc::new(AtomicUsize::new(0)),calls: Arc::new(Mutex::new(Vec::new())),}}@@ -209,6 +214,12 @@ impl MockRedisClient {self.pipeline_errors_by_call.insert(call_index, err);self.clone()}++ /// Fail the Nth `mget` call (0-indexed) with `err`; takes precedence over `mget_error`.+ pub fn mget_error_at_call(&mut self, call_index: usize, err: CustomRedisError) -> Self {+ self.mget_errors_by_call.insert(call_index, err);+ self.clone()+ }}#[derive(Debug, Clone)]@@ -586,6 +597,10 @@ impl Client for MockRedisClient {value: MockRedisValue::VecString(keys.clone()),});+ let call_index = self.mget_call_counter.fetch_add(1, Ordering::SeqCst);+ if let Some(err) = self.mget_errors_by_call.get(&call_index) {+ return Err(err.clone());+ }if let Some(err) = &self.mget_error {return Err(err.clone());}