Skip to content

Commit 650a53e

Browse files
committed
fix(udb): update backoff algo
1 parent 3c6e472 commit 650a53e

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

engine/packages/api-peer/src/actors/kv_get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::*;
2-
use base64::prelude::BASE64_STANDARD;
32
use base64::Engine;
3+
use base64::prelude::BASE64_STANDARD;
44
use pegboard_actor_kv as actor_kv;
55
use rivet_api_builder::ApiCtx;
66
use rivet_util::Id;

engine/packages/api-public/src/actors/kv_get.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ async fn kv_get_inner(ctx: ApiCtx, path: KvGetPath) -> Result<Response> {
6565
request_remote_datacenter_raw(
6666
&ctx,
6767
path.actor_id.label(),
68-
&format!("/actors/{}/kv/keys/{}", path.actor_id, urlencoding::encode(&path.key)),
68+
&format!(
69+
"/actors/{}/kv/keys/{}",
70+
path.actor_id,
71+
urlencoding::encode(&path.key)
72+
),
6973
axum::http::Method::GET,
7074
Option::<&()>::None,
7175
Option::<&()>::None,

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
}

engine/packages/universaldb/tests/rocksdb.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use anyhow::Context;
44
use futures_util::StreamExt;
55
use rivet_test_deps_docker::TestDatabase;
66
use rocksdb::{OptimisticTransactionDB, Options, WriteOptions};
7-
use universaldb::{Database, utils::IsolationLevel::*};
7+
use universaldb::{
8+
Database,
9+
utils::{IsolationLevel::*, calculate_tx_retry_backoff},
10+
};
811
use uuid::Uuid;
912

1013
#[tokio::test]
@@ -136,11 +139,3 @@ async fn rocksdb_udb() {
136139
})
137140
.await;
138141
}
139-
140-
pub fn calculate_tx_retry_backoff(attempt: usize) -> u64 {
141-
let base_backoff_ms = 2_u64.pow((attempt as u32).min(10)) * 10;
142-
143-
let jitter_ms = rand::random::<u64>() % 100;
144-
145-
base_backoff_ms + jitter_ms
146-
}

engine/sdks/typescript/runner-protocol/src/index.ts

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)