Unverified Commit 86522a9c authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Preserve queued snapshot work

Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 3ecbba4e
...@@ -314,7 +314,10 @@ impl Storage { ...@@ -314,7 +314,10 @@ impl Storage {
) -> Vec<SnapshotShard<'l, P>> { ) -> Vec<SnapshotShard<'l, P>> {
let guard = Arc::new(guard); let guard = Arc::new(guard);
let modified_count = self.modified_count.swap(0, Ordering::Relaxed); let modified_count = self.modified_count.swap(0, Ordering::Relaxed);
if modified_count == 0 && !drain_entries { // `modified_count` is only a hint: an increment and its queue push are separate, and a
// crossing undo may saturating-decrement after a snapshot claimed its own increment. Only
// skip when both signals agree that there is no work.
if modified_count == 0 && self.dirty_tasks.is_empty() && !drain_entries {
return Vec::new(); return Vec::new();
} }
...@@ -392,7 +395,8 @@ impl Storage { ...@@ -392,7 +395,8 @@ impl Storage {
// Enter snapshot mode first so concurrent modifications switch to the during-snapshot // Enter snapshot mode first so concurrent modifications switch to the during-snapshot
// path and stop incrementing the pre-snapshot count. // path and stop incrementing the pre-snapshot count.
self.snapshot_mode.store(true, Ordering::SeqCst); self.snapshot_mode.store(true, Ordering::SeqCst);
let has_modifications = self.modified_count.load(Ordering::Relaxed) > 0; let has_modifications =
self.modified_count.load(Ordering::Relaxed) > 0 || !self.dirty_tasks.is_empty();
(SnapshotGuard::new(self), has_modifications) (SnapshotGuard::new(self), has_modifications)
} }
...@@ -1513,6 +1517,27 @@ mod tests { ...@@ -1513,6 +1517,27 @@ mod tests {
assert_eq!(storage.map.task_id_scan_count(), scans + 1); assert_eq!(storage.map.task_id_scan_count(), scans + 1);
} }
#[tokio::test(flavor = "multi_thread")]
async fn queued_dirty_task_is_not_hidden_by_zero_count_hint() {
let storage = Storage::new(true);
let task_id = non_transient_task(1);
{
let mut task = storage.access_mut(task_id);
task.flags.set_data_modified(true);
}
storage.dirty_tasks.push(task_id).unwrap();
assert_eq!(storage.modified_count.load(Ordering::Relaxed), 0);
let (guard, has_modifications) = storage.start_snapshot();
assert!(has_modifications);
let items: Vec<_> = storage
.take_snapshot(guard, &dummy_process, false)
.into_iter()
.flat_map(IntoIterator::into_iter)
.collect();
assert_eq!(items.len(), 1);
}
#[test] #[test]
fn dirty_counter_decrement_saturates_at_zero() { fn dirty_counter_decrement_saturates_at_zero() {
let storage = Storage::new(true); let storage = Storage::new(true);
...@@ -1558,12 +1583,18 @@ mod tests { ...@@ -1558,12 +1583,18 @@ mod tests {
assert!(!guard.flags.any_modified()); assert!(!guard.flags.any_modified());
} }
// Counter is back to zero: the next snapshot sees no modifications. // The counter is back to zero, but the bounded work queue intentionally tolerates stale
let (_guard, has_modifications) = storage.start_snapshot(); // IDs because it cannot remove by value. This may conservatively report work for one
assert!( // cycle; the scan must still emit nothing and drain that stale entry.
!has_modifications, assert_eq!(storage.modified_count.load(Ordering::Relaxed), 0);
"undo must decrement the dirty counter so no modifications remain" let (guard, has_modifications) = storage.start_snapshot();
); assert!(has_modifications);
let items: Vec<_> = storage
.take_snapshot(guard, &dummy_process, false)
.into_iter()
.flat_map(IntoIterator::into_iter)
.collect();
assert!(items.is_empty(), "undone work must not be persisted");
} }
/// A second track on an already-modified category returns `NoChange`; undoing it is a no-op and /// A second track on an already-modified category returns `NoChange`; undoing it is a no-op and
......
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