From 267ddc0085ea609640bb6137b2c42c5f8e5890a7 Mon Sep 17 00:00:00 2001 From: MasterPtato Date: Thu, 13 Nov 2025 17:38:57 -0800 Subject: [PATCH] fix(udb): update backoff algo --- engine/packages/universaldb/src/utils/mod.rs | 23 +++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/engine/packages/universaldb/src/utils/mod.rs b/engine/packages/universaldb/src/utils/mod.rs index 8b6d673c87..a81a3ba99b 100644 --- a/engine/packages/universaldb/src/utils/mod.rs +++ b/engine/packages/universaldb/src/utils/mod.rs @@ -24,13 +24,26 @@ pub enum IsolationLevel { #[derive(Debug, Clone, Copy)] pub struct MaybeCommitted(pub bool); +/// Calculate exponential backoff based on attempt. +/// +/// Ours: +/// 0 -> 10ms + 0-1ms jitter +/// 1 -> 20ms + 0-2ms jitter +/// 2 -> 40ms + 0-4ms jitter +/// ... +/// 7 (max) -> 1280ms + 0-128ms jitter +/// FDB (see https://github.com/apple/foundationdb/blob/b1fbbd87a794b7c6c2f456925c45d8af339a8ae0/fdbclient/NativeAPI.actor.cpp#L4333 and https://github.com/apple/foundationdb/blob/b1fbbd87a794b7c6c2f456925c45d8af339a8ae0/fdbclient/ClientKnobs.cpp#L74-L76): +/// 0 -> 10ms +/// 1 -> 20ms +/// 2 -> 40ms +/// ... +/// X -> max 1s pub fn calculate_tx_retry_backoff(attempt: usize) -> u64 { - // TODO: Update this to mirror fdb 1:1: - // https://github.com/apple/foundationdb/blob/21407341d9b49e1d343514a7a5f395bd5f232079/fdbclient/NativeAPI.actor.cpp#L3162 + let base = 2_u64.pow((attempt as u32).min(7)); + let base_backoff_ms = base * 10; - let base_backoff_ms = 2_u64.pow((attempt as u32).min(10)) * 10; - - let jitter_ms = rand::random::() % 100; + // Jitter is 0-10% of backoff ms + let jitter_ms = rand::random::() % base; base_backoff_ms + jitter_ms }