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

Simplify TaskCache bucket encoding and snapshot reconciliation

Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 804ceca2
...@@ -10,7 +10,7 @@ mod update_collectible; ...@@ -10,7 +10,7 @@ mod update_collectible;
use std::{ use std::{
fmt::{Debug, Display, Formatter}, fmt::{Debug, Display, Formatter},
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
sync::{Arc, atomic::Ordering}, sync::Arc,
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result};
...@@ -198,10 +198,11 @@ pub trait ExecuteContext<'e>: Sized { ...@@ -198,10 +198,11 @@ pub trait ExecuteContext<'e>: Sized {
/// ///
/// Uses hash-based lookup which may return multiple candidates due to hash collisions, /// Uses hash-based lookup which may return multiple candidates due to hash collisions,
/// then verifies each candidate by comparing the stored `persistent_task_type`. /// then verifies each candidate by comparing the stored `persistent_task_type`.
/// Returns `(matching_task, disk_bucket_was_empty)`. The latter is true only if a guarded /// Returns `(matching_task, disk_bucket_was_definitely_empty)`. The latter is true only if a
/// lookup proved that the entire committed hash bucket was empty; a freshly allocated task /// guarded lookup proved that the entire committed hash bucket was empty; a freshly
/// may carry it into its first snapshot. It is false for collisions and noncanonical reads. /// allocated task may carry it into its first snapshot. It is false for collisions and
/// Accepts exploded components so the caller does not need to box the argument before calling. /// unguarded reads. Accepts exploded components so the caller does not need to box the
/// argument before calling.
fn task_by_type( fn task_by_type(
&mut self, &mut self,
native_fn: &'static NativeFunction, native_fn: &'static NativeFunction,
...@@ -1400,11 +1401,12 @@ impl<'e> ExecuteContext<'e> for ExecuteContextImpl<'e> { ...@@ -1400,11 +1401,12 @@ impl<'e> ExecuteContext<'e> for ExecuteContextImpl<'e> {
} }
// The exclusion phase waits for this guarded operation to finish or suspend. No suspend // The exclusion phase waits for this guarded operation to finish or suspend. No suspend
// point occurs before the caller publishes the newly allocated task/cache entry. If a // point occurs before the caller publishes the newly allocated task/cache entry. The
// snapshot has already released exclusion, its disk writes are still in flight, so a // snapshot mutex stays locked through commit, so reads during a snapshot cannot prove
// read during that interval cannot prove the next snapshot's disk baseline. // the next snapshot's committed disk baseline.
let canonical = matches!(self.phase, ExecutePhase::Normal { guard: Some(_) }) let disk_bucket_was_definitely_empty =
&& !self.backend.pending_db_commit.load(Ordering::Acquire); matches!(self.phase, ExecutePhase::Normal { guard: Some(_) })
&& !self.backend.snapshot_in_progress.is_locked();
let candidates = self let candidates = self
.backend .backend
.backing_storage .backing_storage
...@@ -1414,7 +1416,7 @@ impl<'e> ExecuteContext<'e> for ExecuteContextImpl<'e> { ...@@ -1414,7 +1416,7 @@ impl<'e> ExecuteContext<'e> for ExecuteContextImpl<'e> {
// Verify each candidate by comparing the stored persistent_task_type. // Verify each candidate by comparing the stored persistent_task_type.
// Only rarely is there more than one candidate, so no need for parallelization. // Only rarely is there more than one candidate, so no need for parallelization.
if candidates.is_empty() { if candidates.is_empty() {
return (None, canonical); return (None, disk_bucket_was_definitely_empty);
} }
for candidate_id in candidates { for candidate_id in candidates {
let task = self.task(candidate_id, TaskDataCategory::Data); let task = self.task(candidate_id, TaskDataCategory::Data);
......
...@@ -286,17 +286,18 @@ impl Storage { ...@@ -286,17 +286,18 @@ impl Storage {
/// Mark a newly allocated task as restored (skip DB queries) and new (include in persistence /// Mark a newly allocated task as restored (skip DB queries) and new (include in persistence
/// snapshots). Optionally sets the `persistent_task_type` eagerly so it's available for /// snapshots). Optionally sets the `persistent_task_type` eagerly so it's available for
/// persistence snapshots without needing to propagate it through `connect_child`. /// persistence snapshots without needing to propagate it through `connect_child`.
/// `disk_bucket_was_empty` is valid only for a guarded, canonical lookup of the whole hash. /// `disk_bucket_was_definitely_empty` comes from a guarded lookup of the whole hash outside
/// a snapshot.
pub fn initialize_new_task( pub fn initialize_new_task(
&self, &self,
task_id: TaskId, task_id: TaskId,
task_type: Option<CachedTaskTypeArc>, task_type: Option<CachedTaskTypeArc>,
disk_bucket_was_empty: bool, disk_bucket_was_definitely_empty: bool,
) { ) {
let mut task = self.access_mut(task_id); let mut task = self.access_mut(task_id);
task.flags.set_restored(TaskDataCategory::All); task.flags.set_restored(TaskDataCategory::All);
task.flags.set_new_task(true); task.flags.set_new_task(true);
if !task_id.is_transient() && !disk_bucket_was_empty { if !task_id.is_transient() && !disk_bucket_was_definitely_empty {
task.set_task_cache_needs_read(true); task.set_task_cache_needs_read(true);
} }
task.gc_pin_for_construction(); task.gc_pin_for_construction();
...@@ -684,8 +685,6 @@ impl Storage { ...@@ -684,8 +685,6 @@ impl Storage {
// All GC'd tasks were tombstoned during the snapshot (or are not persisted) so we // All GC'd tasks were tombstoned during the snapshot (or are not persisted) so we
// can drop them fully now. // can drop them fully now.
if task.flags.deleted() { if task.flags.deleted() {
// A sole Arc owner proves this type is already absent from TaskCache.
// Additional owners might not be cache entries, so still check the TaskId.
if let Some(task_type) = task.get_persistent_task_type() if let Some(task_type) = task.get_persistent_task_type()
&& task_type.count() > 1 && task_type.count() > 1
{ {
......
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