Skip to content
Merged
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
12 changes: 9 additions & 3 deletions src/db/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,13 @@ pub async fn renew_active_bot(
deadline: std::time::Instant,
refuse: std::sync::Arc<std::sync::atomic::AtomicBool>,
) -> Result<bool> {
let now = db
.lease_runtime
.as_ref()
.unwrap_or(&db.runtime)
.lease_clock();
anyhow::ensure!(
std::time::Instant::now() < deadline,
now() < deadline,
"active-bot lease authority window expired"
);
anyhow::ensure!(valid_bot_instance(instance), "invalid active-bot instance");
Expand All @@ -869,6 +874,7 @@ pub async fn renew_active_bot(
return claim_active_bot(db, instance, now_ms, refuse).await;
};
let instance = instance.to_owned();
let cas_now = now.clone();
let renewed = db
.with_lease_conn(move |conn| {
if refuse.load(std::sync::atomic::Ordering::Acquire) {
Expand All @@ -883,7 +889,7 @@ pub async fn renew_active_bot(
now_ms,
generation.to_string()
],
deadline.saturating_duration_since(std::time::Instant::now()),
deadline.saturating_duration_since(cas_now()),
)? == 1)
})
.await
Expand All @@ -899,7 +905,7 @@ pub async fn renew_active_bot(
})?;
// The runtime may poll a ready response before its deadline timer.
anyhow::ensure!(
std::time::Instant::now() < deadline,
now() < deadline,
"active-bot lease renewal completed too late"
);
Ok(renewed)
Expand Down
4 changes: 3 additions & 1 deletion src/db/guild_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,9 @@ impl GuildDataStore {
let tx = conn.transaction_primary_snapshot()?;
let value = Self::lease_row(&tx)?;
tx.finish_read()?;
crate::db_compat::reject_active_bot_lease(value.as_deref(), expected)
crate::db_compat::reject_active_bot_lease(value.as_deref(), expected, || {
core.runtime.lease_time_ms()
})
})
}

Expand Down
72 changes: 35 additions & 37 deletions src/db/guild_data/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,51 +1139,49 @@ async fn slow_guild_commit_still_lets_the_same_owner_renew() {
.with_core_write_fence(f.catalog.clone());
let placement = f.placement(&reg, 22).await;
let owner = "ab".repeat(16);
let started = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis() as i64;
let initial = 1_000_000;
let clock = crate::db_compat::ManualClock::with_epoch(Some(initial));
f.catalog.runtime.install_manual_clock(Some(clock.clone()));
let refuse = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
assert!(
claim_active_bot(
&f.catalog,
&owner,
started,
std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
)
.await
.unwrap()
claim_active_bot(&f.catalog, &owner, initial, refuse.clone())
.await
.unwrap()
);
raise_held(&store, &f.catalog).await;
let generation = f.catalog.leased_generation();
// Leave only one core connection available, so retaining the final core
// snapshot across the guild commit really would block this renewal.
let _occupied = f.catalog.permits.acquire().await.unwrap();
let (ready_tx, ready_rx) = tokio::sync::oneshot::channel();
let (release_tx, release_rx) = std::sync::mpsc::channel();
f.catalog.arm_during_leased_guild_commit(move || {
let _ = ready_tx.send(());
// Longer than the 5s renewal interval and shorter than the 20s TTL.
std::thread::sleep(std::time::Duration::from_secs(6));
ready_tx.send(()).unwrap();
release_rx.recv().unwrap();
});
let catalog = f.catalog.clone();
let renewal = tokio::spawn(async move {
ready_rx.await.unwrap();
let began = std::time::Instant::now();
let renewed = claim_active_bot(
&catalog,
&owner,
crate::control::now_ms(),
std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
)
.await
.unwrap();
(renewed, began.elapsed())
});
store
.bind_initial_placement(placement.clone())
.await
.unwrap();
let (renewed, elapsed) = renewal.await.unwrap();
assert!(renewed);
let write_store = store.clone();
let write_placement = placement.clone();
let mut writer =
tokio::spawn(async move { write_store.bind_initial_placement(write_placement).await });
tokio::select! {
ready = ready_rx => ready.expect("guild writer must enter its commit"),
result = &mut writer => panic!("guild writer finished before its commit hook: {result:?}"),
}
// One renewal interval has passed, but the original 20s lease is live.
clock.advance(std::time::Duration::from_secs(6));
let renewed = tokio::time::timeout(
std::time::Duration::from_secs(5),
claim_active_bot(&f.catalog, &owner, initial + 6_000, refuse),
)
.await;
release_tx.send(()).unwrap();
writer.await.unwrap().unwrap();
assert!(
elapsed < std::time::Duration::from_secs(5),
"renewal waited {elapsed:?} for the guild commit"
renewed
.expect("renewal waited for the guild commit")
.unwrap()
);
assert_eq!(f.catalog.leased_generation(), generation);
store.verify_placement(placement).await.unwrap();
}

Expand Down
Loading
Loading