Unverified Commit 8a473f45 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-tasks-backend: add persistence delete/tombstone plumbing for GC (#95975)

## What

The persistence-layer mechanism to tombstone (delete) tasks from the
on-disk cache. A GC-collected task is represented as a
`SnapshotItem::Delete` that rides the same streaming iterator
`save_snapshot` already consumes for puts, so tombstones are applied in
the same commit/batch as the snapshot with no side-channel.

- `SnapshotItem` becomes an enum: `Put { … }` (the existing path) and
`Delete(TaskDeletion)`.
- `TurboWriteBatch::delete/value_delete` passthrough to
`turbo_persistence`.
- `save_snapshot` applies deletes inline in the parallel put phase


## Note

`SnapshotItem::Delete` is `#[allow(dead_code)]` here: this PR lands the
persistence-side *handling* and its tests, but nothing *emits* a
`Delete` yet. The GC pass that soft-deletes tasks and serializes them to
`Delete` lands in the next PR in the stack. So this PR is a no-op at
runtime.

---------
Co-authored-by: 's avatarClaude Opus 4.8 (1M context) <noreply@anthropic.com>
parent 863a0ada
...@@ -1244,7 +1244,7 @@ impl TurboTasksBackend { ...@@ -1244,7 +1244,7 @@ impl TurboTasksBackend {
None None
}; };
SnapshotItem { SnapshotItem::Put {
task_id, task_id,
meta, meta,
data, data,
......
...@@ -1044,7 +1044,7 @@ mod tests { ...@@ -1044,7 +1044,7 @@ mod tests {
_: &super::TaskStorage, _: &super::TaskStorage,
_: &mut TurboBincodeBuffer, _: &mut TurboBincodeBuffer,
) -> SnapshotItem { ) -> SnapshotItem {
SnapshotItem { SnapshotItem::Put {
task_id, task_id,
meta: Some(TurboBincodeBuffer::default()), meta: Some(TurboBincodeBuffer::default()),
data: None, data: None,
...@@ -1112,7 +1112,7 @@ mod tests { ...@@ -1112,7 +1112,7 @@ mod tests {
// The pre-snapshot snapshot copy should have been encoded and returned. // The pre-snapshot snapshot copy should have been encoded and returned.
assert_eq!(items.len(), 1); assert_eq!(items.len(), 1);
assert_eq!(items[0].task_id, task_id); assert_eq!(items[0].task_id(), task_id);
{ {
let guard = storage.access_mut(task_id); let guard = storage.access_mut(task_id);
...@@ -1179,7 +1179,7 @@ mod tests { ...@@ -1179,7 +1179,7 @@ mod tests {
.collect(); .collect();
assert_eq!(items.len(), 1); assert_eq!(items.len(), 1);
assert_eq!(items[0].task_id, task_id); assert_eq!(items[0].task_id(), task_id);
{ {
let guard = storage.access_mut(task_id); let guard = storage.access_mut(task_id);
...@@ -1227,7 +1227,7 @@ mod tests { ...@@ -1227,7 +1227,7 @@ mod tests {
.collect(); .collect();
assert_eq!(items.len(), 1); assert_eq!(items.len(), 1);
assert_eq!(items[0].task_id, task_id); assert_eq!(items[0].task_id(), task_id);
// The entry must be gone from the map now that it has been persisted. // The entry must be gone from the map now that it has been persisted.
assert!( assert!(
...@@ -1324,7 +1324,7 @@ mod tests { ...@@ -1324,7 +1324,7 @@ mod tests {
.flat_map(|shard| shard.into_iter()) .flat_map(|shard| shard.into_iter())
.collect(); .collect();
assert_eq!(items.len(), 1); assert_eq!(items.len(), 1);
assert_eq!(items[0].task_id, modified_id); assert_eq!(items[0].task_id(), modified_id);
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
......
...@@ -8,15 +8,40 @@ use turbo_tasks_hash::Xxh3Hash64Hasher; ...@@ -8,15 +8,40 @@ use turbo_tasks_hash::Xxh3Hash64Hasher;
pub type TaskTypeHash = [u8; 8]; pub type TaskTypeHash = [u8; 8];
/// A single item yielded by the snapshot iterator during persistence. /// A single item yielded by the snapshot iterator during persistence: either a put (persist a
pub struct SnapshotItem { /// modified task's meta/data + optionally register a new task's type) or a delete (tombstone a
pub task_id: TaskId, /// GC-collected task's on-disk copy). Both ride the one iterator `save_snapshot` consumes, so
/// Serialized task meta data, if modified /// tombstones are applied in the same commit and batch as the puts.
pub meta: Option<TurboBincodeBuffer>, pub enum SnapshotItem {
/// Serialized task data, if modified Put {
pub data: Option<TurboBincodeBuffer>, task_id: TaskId,
/// Task type for new tasks that need to be added to the task cache /// Serialized task meta data, if modified
pub task_type_hash: Option<TaskTypeHash>, meta: Option<TurboBincodeBuffer>,
/// Serialized task data, if modified
data: Option<TurboBincodeBuffer>,
/// Task type for new tasks that need to be added to the task cache
task_type_hash: Option<TaskTypeHash>,
},
// Constructed by the GC pass that emits `Delete` for soft-deleted tasks, which lands in a
// later PR in the stack.
#[allow(dead_code)]
Delete {
task_id: TaskId,
/// The deleted task's `TaskCache` key. Always present: only persistent tasks are
/// collected, and those always have a task type.
task_type_hash: TaskTypeHash,
},
}
impl SnapshotItem {
/// The task this item persists or tombstones. (Currently only used by tests, which assert on
/// the id of items yielded by the snapshot iterator.)
#[cfg(test)]
pub fn task_id(&self) -> TaskId {
match self {
SnapshotItem::Put { task_id, .. } | SnapshotItem::Delete { task_id, .. } => *task_id,
}
}
} }
/// Computes a deterministic 64-bit hash of a CachedTaskType for use as a TaskCache key. /// Computes a deterministic 64-bit hash of a CachedTaskType for use as a TaskCache key.
......
...@@ -234,6 +234,25 @@ impl<'a> TurboWriteBatch<'a> { ...@@ -234,6 +234,25 @@ impl<'a> TurboWriteBatch<'a> {
.put(key_space as u32, key.into_static(), value.into()) .put(key_space as u32, key.into_static(), value.into())
} }
/// Writes a delete (tombstone) for `key` into the write batch.
///
/// Use [`Self::delete_value`] to remove a single mapping from a MultiValue KeySpace
pub fn delete(&self, key_space: KeySpace, key: WriteBuffer<'_>) -> Result<()> {
self.batch.delete(key_space as u32, key.into_static())
}
/// Writes a tombstone for a single `key` -> `value` mapping, leaving other values under `key`
/// intact. Only valid for `MultiValue` families (`TaskCache`).
pub fn delete_value(
&self,
key_space: KeySpace,
key: WriteBuffer<'_>,
value: WriteBuffer<'_>,
) -> Result<()> {
self.batch
.delete_value(key_space as u32, key.into_static(), value.into())
}
/// Flushes a key space of the write batch, reducing the amount of buffered memory used. /// Flushes a key space of the write batch, reducing the amount of buffered memory used.
/// Does not commit any data persistently. /// Does not commit any data persistently.
/// ///
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment