Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion engine/packages/api-peer/src/actors/kv_get.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::*;
use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use base64::prelude::BASE64_STANDARD;
use pegboard_actor_kv as actor_kv;
use rivet_api_builder::ApiCtx;
use rivet_util::Id;
Expand Down
6 changes: 5 additions & 1 deletion engine/packages/api-public/src/actors/kv_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ async fn kv_get_inner(ctx: ApiCtx, path: KvGetPath) -> Result<Response> {
request_remote_datacenter_raw(
&ctx,
path.actor_id.label(),
&format!("/actors/{}/kv/keys/{}", path.actor_id, urlencoding::encode(&path.key)),
&format!(
"/actors/{}/kv/keys/{}",
path.actor_id,
urlencoding::encode(&path.key)
),
axum::http::Method::GET,
Option::<&()>::None,
Option::<&()>::None,
Expand Down
23 changes: 18 additions & 5 deletions engine/packages/universaldb/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u64>() % 100;
// Jitter is 0-10% of backoff ms
let jitter_ms = rand::random::<u64>() % base;

base_backoff_ms + jitter_ms
}
Expand Down
13 changes: 4 additions & 9 deletions engine/packages/universaldb/tests/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use anyhow::Context;
use futures_util::StreamExt;
use rivet_test_deps_docker::TestDatabase;
use rocksdb::{OptimisticTransactionDB, Options, WriteOptions};
use universaldb::{Database, utils::IsolationLevel::*};
use universaldb::{
Database,
utils::{IsolationLevel::*, calculate_tx_retry_backoff},
};
use uuid::Uuid;

#[tokio::test]
Expand Down Expand Up @@ -136,11 +139,3 @@ async fn rocksdb_udb() {
})
.await;
}

pub fn calculate_tx_retry_backoff(attempt: usize) -> u64 {
let base_backoff_ms = 2_u64.pow((attempt as u32).min(10)) * 10;

let jitter_ms = rand::random::<u64>() % 100;

base_backoff_ms + jitter_ms
}
11 changes: 4 additions & 7 deletions engine/sdks/typescript/runner-protocol/src/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading