Skip to content

Commit 8283eec

Browse files
committed
refactor(server): simplify sandbox settings cleanup
Signed-off-by: Piotr Mlocek <[email protected]>
1 parent 0f18d50 commit 8283eec

6 files changed

Lines changed: 56 additions & 484 deletions

File tree

‎architecture/compute-runtimes.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ deleted events are idempotent, and snapshots for absent rows are ignored.
4343

4444
An accepted delete (`deleted = true`) is finalized by the watcher. If the
4545
backend is already absent (`deleted = false`), the request removes gateway state
46-
synchronously. Sandbox rows and name-scoped settings are removed atomically;
47-
SSH sessions, indexes, and watch/log buses are cleaned only after confirmed
48-
removal.
46+
synchronously. Sandbox row removal remains bound to the stable ID and resource
47+
version. Settings retain their existing best-effort name-based cleanup; SSH
48+
sessions, indexes, and watch/log buses are cleaned after confirmed removal.
4949

5050
The request workflow runs in an owned task so client cancellation cannot strand
5151
a mutation. A gateway restart does not resume or reissue a persisted `Deleting`

‎crates/openshell-server/src/compute/mod.rs‎

Lines changed: 53 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -870,10 +870,11 @@ impl ComputeRuntime {
870870
false
871871
}
872872

873-
/// Atomically removes the sandbox and its name-scoped settings only when
874-
/// the expected resource version still owns the row. The caller holds
875-
/// `sync_lock`; successful or already-completed durable removal also
876-
/// clears this replica's index, sessions, and watch/log buses.
873+
/// Removes the sandbox by stable ID only when the expected resource
874+
/// version still owns the row. The caller holds `sync_lock`; a successful
875+
/// delete also performs best-effort settings cleanup, while successful or
876+
/// already-completed removal clears this replica's index, sessions, and
877+
/// watch/log buses.
877878
async fn remove_sandbox_record_if_version_locked(
878879
&self,
879880
sandbox_id: &str,
@@ -901,28 +902,21 @@ impl ComputeRuntime {
901902

902903
match self
903904
.store
904-
.delete_with_name_scoped_if_version(
905+
.delete_if(
905906
Sandbox::object_type(),
906907
sandbox_id,
907908
expected_resource_version,
908-
SANDBOX_SETTINGS_OBJECT_TYPE,
909909
)
910910
.await
911911
{
912912
Ok(true) => {
913+
self.cleanup_sandbox_settings(sandbox_id, &record.name)
914+
.await;
913915
self.cleanup_removed_sandbox_state(sandbox_id).await;
914916
Ok(true)
915917
}
916918
Ok(false) => {
917-
if self
918-
.store
919-
.get(Sandbox::object_type(), sandbox_id)
920-
.await
921-
.map_err(|err| err.to_string())?
922-
.is_none()
923-
{
924-
self.cleanup_removed_sandbox_state(sandbox_id).await;
925-
}
919+
self.cleanup_removed_sandbox_state(sandbox_id).await;
926920
Ok(false)
927921
}
928922
Err(crate::persistence::PersistenceError::Conflict {
@@ -1649,14 +1643,20 @@ impl ComputeRuntime {
16491643
}
16501644

16511645
async fn apply_deleted_locked(&self, sandbox_id: &str) -> Result<(), String> {
1652-
self.store
1653-
.delete_with_name_scoped(
1654-
Sandbox::object_type(),
1655-
sandbox_id,
1656-
SANDBOX_SETTINGS_OBJECT_TYPE,
1657-
)
1646+
let sandbox = self
1647+
.store
1648+
.get_message::<Sandbox>(sandbox_id)
1649+
.await
1650+
.map_err(|e| e.to_string())?;
1651+
let removed = self
1652+
.store
1653+
.delete(Sandbox::object_type(), sandbox_id)
16581654
.await
16591655
.map_err(|e| e.to_string())?;
1656+
if removed && let Some(sandbox) = sandbox.as_ref() {
1657+
self.cleanup_sandbox_settings(sandbox_id, sandbox.object_name())
1658+
.await;
1659+
}
16601660
self.cleanup_removed_sandbox_state(sandbox_id).await;
16611661
Ok(())
16621662
}
@@ -1672,6 +1672,21 @@ impl ComputeRuntime {
16721672
Ok(())
16731673
}
16741674

1675+
async fn cleanup_sandbox_settings(&self, sandbox_id: &str, sandbox_name: &str) {
1676+
if let Err(err) = self
1677+
.store
1678+
.delete_by_name(SANDBOX_SETTINGS_OBJECT_TYPE, sandbox_name)
1679+
.await
1680+
{
1681+
warn!(
1682+
sandbox_id,
1683+
sandbox_name,
1684+
error = %err,
1685+
"Failed to delete sandbox settings during cleanup"
1686+
);
1687+
}
1688+
}
1689+
16751690
async fn cleanup_sandbox_ssh_sessions(&self, sandbox_id: &str) {
16761691
if let Ok(records) = self.store.list(SshSession::object_type(), 1000, 0).await {
16771692
for record in records {
@@ -3081,6 +3096,19 @@ mod tests {
30813096
session
30823097
}
30833098

3099+
async fn remove_sandbox_owned_records_from_store(runtime: &ComputeRuntime, sandbox: &Sandbox) {
3100+
runtime
3101+
.store
3102+
.delete_by_name(SANDBOX_SETTINGS_OBJECT_TYPE, sandbox.object_name())
3103+
.await
3104+
.unwrap();
3105+
runtime
3106+
.store
3107+
.delete(Sandbox::object_type(), sandbox.object_id())
3108+
.await
3109+
.unwrap();
3110+
}
3111+
30843112
async fn assert_sandbox_owned_records(
30853113
runtime: &ComputeRuntime,
30863114
sandbox: &Sandbox,
@@ -4086,17 +4114,7 @@ mod tests {
40864114
.expect("deleting notification was not sent")
40874115
.expect("watch bus closed before the deleting notification");
40884116

4089-
assert!(
4090-
runtime
4091-
.store
4092-
.delete_with_name_scoped(
4093-
Sandbox::object_type(),
4094-
"sb-1",
4095-
SANDBOX_SETTINGS_OBJECT_TYPE,
4096-
)
4097-
.await
4098-
.unwrap()
4099-
);
4117+
remove_sandbox_owned_records_from_store(&runtime, &sandbox).await;
41004118
driver.release_delete();
41014119

41024120
assert!(
@@ -4142,17 +4160,7 @@ mod tests {
41424160
.expect("deleting notification was not sent")
41434161
.expect("watch bus closed before the deleting notification");
41444162

4145-
assert!(
4146-
runtime
4147-
.store
4148-
.delete_with_name_scoped(
4149-
Sandbox::object_type(),
4150-
"sb-1",
4151-
SANDBOX_SETTINGS_OBJECT_TYPE,
4152-
)
4153-
.await
4154-
.unwrap()
4155-
);
4163+
remove_sandbox_owned_records_from_store(&runtime, &sandbox).await;
41564164
driver.release_delete();
41574165

41584166
assert!(
@@ -4220,17 +4228,7 @@ mod tests {
42204228
.expect("deleting notification was not sent")
42214229
.expect("watch bus closed before the deleting notification");
42224230

4223-
assert!(
4224-
runtime
4225-
.store
4226-
.delete_with_name_scoped(
4227-
Sandbox::object_type(),
4228-
"sb-1",
4229-
SANDBOX_SETTINGS_OBJECT_TYPE,
4230-
)
4231-
.await
4232-
.unwrap()
4233-
);
4231+
remove_sandbox_owned_records_from_store(&runtime, &sandbox).await;
42344232
driver.release_delete();
42354233

42364234
tokio::time::timeout(Duration::from_secs(1), delete)
@@ -4276,17 +4274,7 @@ mod tests {
42764274
.expect("deleting notification was not sent")
42774275
.expect("watch bus closed before the deleting notification");
42784276

4279-
assert!(
4280-
runtime
4281-
.store
4282-
.delete_with_name_scoped(
4283-
Sandbox::object_type(),
4284-
"sb-1",
4285-
SANDBOX_SETTINGS_OBJECT_TYPE,
4286-
)
4287-
.await
4288-
.unwrap()
4289-
);
4277+
remove_sandbox_owned_records_from_store(&runtime, &sandbox).await;
42904278
driver.release_get();
42914279

42924280
tokio::time::timeout(Duration::from_secs(1), delete)

‎crates/openshell-server/src/persistence/mod.rs‎

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -261,46 +261,6 @@ impl Store {
261261
store_dispatch!(self.delete_if(object_type, id, expected_resource_version))
262262
}
263263

264-
/// Atomically delete an object and the object of `scoped_type` that has
265-
/// the same name, subject to an exact parent resource-version match.
266-
///
267-
/// The parent name is captured by the same database statement that
268-
/// removes the records. This prevents a concurrent name reuse from
269-
/// causing the newly-created name-scoped object to be removed.
270-
///
271-
/// # Returns
272-
/// * `Ok(true)` - The parent and any matching name-scoped object were deleted
273-
/// * `Ok(false)` - The parent was not found
274-
/// * `Err(Conflict)` - The parent exists with a different resource version
275-
pub async fn delete_with_name_scoped_if_version(
276-
&self,
277-
parent_type: &str,
278-
parent_id: &str,
279-
expected_resource_version: u64,
280-
scoped_type: &str,
281-
) -> PersistenceResult<bool> {
282-
store_dispatch!(self.delete_with_name_scoped_if_version(
283-
parent_type,
284-
parent_id,
285-
expected_resource_version,
286-
scoped_type
287-
))
288-
}
289-
290-
/// Atomically delete an object and the object of `scoped_type` that has
291-
/// the same name.
292-
///
293-
/// The parent name is captured by the same database statement that
294-
/// removes the records, preventing cleanup from racing with name reuse.
295-
pub async fn delete_with_name_scoped(
296-
&self,
297-
parent_type: &str,
298-
parent_id: &str,
299-
scoped_type: &str,
300-
) -> PersistenceResult<bool> {
301-
store_dispatch!(self.delete_with_name_scoped(parent_type, parent_id, scoped_type))
302-
}
303-
304264
/// Insert or update a generic named object with an application-owned scope.
305265
pub async fn put_scoped(
306266
&self,

‎crates/openshell-server/src/persistence/postgres.rs‎

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -244,93 +244,6 @@ WHERE object_type = $1 AND id = $2 AND resource_version = $3
244244
}
245245
}
246246

247-
pub async fn delete_with_name_scoped_if_version(
248-
&self,
249-
parent_type: &str,
250-
parent_id: &str,
251-
expected_resource_version: u64,
252-
scoped_type: &str,
253-
) -> PersistenceResult<bool> {
254-
// Feed the sibling delete exclusively from the exact parent delete's
255-
// RETURNING output. A stale resource version therefore cannot remove
256-
// settings, and both deletes commit as one statement.
257-
let row = sqlx::query(
258-
r"
259-
WITH deleted_parent AS (
260-
DELETE FROM objects
261-
WHERE object_type = $1 AND id = $2 AND resource_version = $3
262-
RETURNING name
263-
),
264-
deleted_scoped AS (
265-
DELETE FROM objects
266-
WHERE object_type = $4
267-
AND name IN (SELECT name FROM deleted_parent)
268-
AND NOT (object_type = $1 AND id = $2)
269-
RETURNING id
270-
)
271-
SELECT
272-
EXISTS (SELECT 1 FROM deleted_parent) AS parent_deleted,
273-
(SELECT COUNT(*) FROM deleted_scoped) AS scoped_deleted
274-
",
275-
)
276-
.bind(parent_type)
277-
.bind(parent_id)
278-
.bind(i64::try_from(expected_resource_version).unwrap_or(i64::MAX))
279-
.bind(scoped_type)
280-
.fetch_one(&self.pool)
281-
.await
282-
.map_err(|e| map_db_error(&e))?;
283-
284-
let parent_deleted: bool = row.get("parent_deleted");
285-
if parent_deleted {
286-
Ok(true)
287-
} else {
288-
let existing = self.get(parent_type, parent_id).await?;
289-
if let Some(record) = existing {
290-
Err(PersistenceError::Conflict {
291-
current_resource_version: Some(record.resource_version),
292-
})
293-
} else {
294-
Ok(false)
295-
}
296-
}
297-
}
298-
299-
pub async fn delete_with_name_scoped(
300-
&self,
301-
parent_type: &str,
302-
parent_id: &str,
303-
scoped_type: &str,
304-
) -> PersistenceResult<bool> {
305-
let row = sqlx::query(
306-
r"
307-
WITH deleted_parent AS (
308-
DELETE FROM objects
309-
WHERE object_type = $1 AND id = $2
310-
RETURNING name
311-
),
312-
deleted_scoped AS (
313-
DELETE FROM objects
314-
WHERE object_type = $3
315-
AND name IN (SELECT name FROM deleted_parent)
316-
AND NOT (object_type = $1 AND id = $2)
317-
RETURNING id
318-
)
319-
SELECT
320-
EXISTS (SELECT 1 FROM deleted_parent) AS parent_deleted,
321-
(SELECT COUNT(*) FROM deleted_scoped) AS scoped_deleted
322-
",
323-
)
324-
.bind(parent_type)
325-
.bind(parent_id)
326-
.bind(scoped_type)
327-
.fetch_one(&self.pool)
328-
.await
329-
.map_err(|e| map_db_error(&e))?;
330-
331-
Ok(row.get("parent_deleted"))
332-
}
333-
334247
pub async fn put_scoped(
335248
&self,
336249
object_type: &str,

0 commit comments

Comments
 (0)