Skip to content

Commit a0dfaaf

Browse files
committed
fix(udb): update backoff algo
1 parent ce30dce commit a0dfaaf

File tree

1 file changed

+18
-5
lines changed
  • engine/packages/universaldb/src/utils

1 file changed

+18
-5
lines changed

engine/packages/universaldb/src/utils/mod.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,26 @@ pub enum IsolationLevel {
2424
#[derive(Debug, Clone, Copy)]
2525
pub struct MaybeCommitted(pub bool);
2626

27+
/// Calculate exponential backoff based on attempt.
28+
///
29+
/// Ours:
30+
/// 0 -> 10ms + 0-1ms jitter
31+
/// 1 -> 20ms + 0-2ms jitter
32+
/// 2 -> 40ms + 0-4ms jitter
33+
/// ...
34+
/// 7 (max) -> 1280ms + 0-128ms jitter
35+
/// 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):
36+
/// 0 -> 10ms
37+
/// 1 -> 20ms
38+
/// 2 -> 40ms
39+
/// ...
40+
/// X -> max 1s
2741
pub fn calculate_tx_retry_backoff(attempt: usize) -> u64 {
28-
// TODO: Update this to mirror fdb 1:1:
29-
// https://github.com/apple/foundationdb/blob/21407341d9b49e1d343514a7a5f395bd5f232079/fdbclient/NativeAPI.actor.cpp#L3162
42+
let base = 2_u64.pow((attempt as u32).min(7));
43+
let base_backoff_ms = base * 10;
3044

31-
let base_backoff_ms = 2_u64.pow((attempt as u32).min(10)) * 10;
32-
33-
let jitter_ms = rand::random::<u64>() % 100;
45+
// Jitter is 0-10% of backoff ms
46+
let jitter_ms = rand::random::<u64>() % base;
3447

3548
base_backoff_ms + jitter_ms
3649
}

0 commit comments

Comments
 (0)