Skip to content

Commit 6b08221

Browse files
committed
fix(pegboard): fix actor names not reserving on first reservation
1 parent 6ace4ef commit 6b08221

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

packages/services/pegboard/src/workflows/actor/actor_keys.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use futures_util::TryStreamExt;
66
use gas::prelude::*;
77
use rivet_key_data::converted::ActorByKeyKeyData;
88
use udb_util::prelude::*;
9-
use universaldb::{self as udb, FdbBindingError, options::StreamingMode};
9+
use universaldb::{self as udb, options::StreamingMode, FdbBindingError};
1010

1111
use crate::keys;
1212

@@ -52,7 +52,23 @@ pub async fn reserve_key(
5252
.await?;
5353

5454
match proposal_result {
55-
ProposalResult::Committed => Ok(ReserveKeyOutput::Success),
55+
ProposalResult::Committed => {
56+
let output = ctx
57+
.activity(ReserveActorKeyInput {
58+
namespace_id,
59+
name: name.clone(),
60+
key: key.clone(),
61+
actor_id,
62+
create_ts: ctx.create_ts(),
63+
})
64+
.await?;
65+
match output {
66+
ReserveActorKeyOutput::Success => Ok(ReserveKeyOutput::Success),
67+
ReserveActorKeyOutput::ExistingActor { existing_actor_id } => {
68+
Ok(ReserveKeyOutput::KeyExists { existing_actor_id })
69+
}
70+
}
71+
}
5672
ProposalResult::ConsensusFailed => {
5773
bail!("consensus failed")
5874
}

scripts/tests/actor_get_or_create.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
generateRandomKey,
88
} from "./utils";
99

10-
const COUNT = 5;
10+
const COUNT = 50;
11+
const PARALLEL = true;
1112

1213
async function main() {
1314
try {
@@ -21,28 +22,47 @@ async function main() {
2122
const sharedKey = generateRandomKey();
2223
console.log(`Using shared key: ${sharedKey}`);
2324

24-
// Create parallel calls to get or create actor with the same key
25+
// Create calls to get or create actor with the same key
2526
console.log(
26-
`Creating ${COUNT} parallel get-or-create calls with shared key...`,
27+
`Creating ${COUNT} ${PARALLEL ? "parallel" : "serial"} get-or-create calls with shared key...`,
2728
);
2829
let completedCount = 0;
29-
const promises = Array.from({ length: COUNT }, (_, index) =>
30-
getOrCreateActorById(
31-
namespaceName,
32-
actorName,
33-
sharedKey,
34-
runnerNameSelector,
35-
).then((response) => {
30+
31+
let results;
32+
if (PARALLEL) {
33+
const promises = Array.from({ length: COUNT }, (_, index) =>
34+
getOrCreateActorById(
35+
namespaceName,
36+
actorName,
37+
sharedKey,
38+
runnerNameSelector,
39+
).then((response) => {
40+
completedCount++;
41+
console.log(
42+
`Call ${index + 1}/${COUNT} completed ${JSON.stringify(response)} (${completedCount} total)`,
43+
);
44+
return { index, response };
45+
}),
46+
);
47+
results = await Promise.all(promises);
48+
} else {
49+
results = [];
50+
for (let index = 0; index < COUNT; index++) {
51+
const response = await getOrCreateActorById(
52+
namespaceName,
53+
actorName,
54+
sharedKey,
55+
runnerNameSelector,
56+
);
3657
completedCount++;
3758
console.log(
3859
`Call ${index + 1}/${COUNT} completed ${JSON.stringify(response)} (${completedCount} total)`,
3960
);
40-
return { index, response };
41-
}),
42-
);
43-
44-
const results = await Promise.all(promises);
45-
console.log(`✓ Completed all ${COUNT} parallel calls`);
61+
results.push({ index, response });
62+
}
63+
}
64+
65+
console.log(`✓ Completed all ${COUNT} ${PARALLEL ? "parallel" : "serial"} calls`);
4666

4767
// Extract all actor IDs and verify they're all the same
4868
const actorIds = results.map((result) => result.response.actor_id);

0 commit comments

Comments
 (0)