Reuse settled suffixes after verified closed child edits
This commit is contained in:
1 parent
55c497dccd
commit
e4d6e049f8
11 files changed
+565
-72
No files matched your search
+122
-22
@@ -1,5 +1,5 @@
|
||||
//! Reuse a recorded entry prefix only after validating the current job host.
|
||||
use super::checkpoint_probe::{Call, RecordingHost};
|
||||
use super::checkpoint_probe::{Call, ClosedSourceUsageOffset, RecordingHost, closed_source_reads};
|
||||
use super::*;
|
||||
#[cfg(test)]
|
||||
use std::fs;
|
||||
@@ -10,7 +10,7 @@ use std::{
|
||||
time::Instant,
|
||||
};
|
||||
use texrra_tex_core::{
|
||||
execution::{CheckpointDecision, CheckpointSource, EntryCheckpoint, Event, RelocatedShipoutSuffix, StatisticsPhase, StatisticsReport},
|
||||
execution::{CheckpointDecision, CheckpointSource, ClosedSourceChange, EntryCheckpoint, Event, RelocatedShipoutSuffix, StatisticsPhase, StatisticsReport},
|
||||
resources::{ResourceKind, ResourceLookup, ResourceReadRange},
|
||||
};
|
||||
#[cfg(test)]
|
||||
@@ -59,9 +59,17 @@ struct CompletedExecution {
|
||||
result: Arc<RevisionExecution>,
|
||||
guard: PrefixInputGuard,
|
||||
calls: Arc<[Call]>,
|
||||
trace: ResourceTrace,
|
||||
settled: BTreeMap<usize, SettledCheckpoint>,
|
||||
}
|
||||
|
||||
struct ClosedSourceSuffix {
|
||||
source_id: usize,
|
||||
prefix_resource_reads: usize,
|
||||
version: ResourceVersion,
|
||||
usage: ClosedSourceUsageOffset,
|
||||
}
|
||||
|
||||
struct SettledCheckpoint {
|
||||
entry: Arc<EntryCheckpoint>,
|
||||
host: crate::native_job::ReplayBoundary,
|
||||
@@ -417,6 +425,7 @@ impl<'a> CapturedEntries<'a> {
|
||||
prior: &SettledCheckpoint,
|
||||
completed: &CompletedExecution,
|
||||
end_statistics: Option<&StatisticsReport>,
|
||||
closed_source: Option<&ClosedSourceSuffix>,
|
||||
host: &mut RecordingHost,
|
||||
cancellation: &Cancellation,
|
||||
) -> Result<(), String> {
|
||||
@@ -426,9 +435,10 @@ impl<'a> CapturedEntries<'a> {
|
||||
let previous_bytes = completed.guard.project.file(path).expect("prior settled entry").data.len();
|
||||
let stride = bytes.div_ceil(SOURCE_INTERVALS).max(1);
|
||||
let mut later = [const { None }; SOURCE_INTERVALS];
|
||||
// Untranslated live continuations and their source intervals still
|
||||
// refer to the old entry when its length changed.
|
||||
if previous_bytes == bytes
|
||||
// Carried checkpoints still carry the donor's scanner and source
|
||||
// allocations. An exhausted child edit also changes that history.
|
||||
if closed_source.is_none()
|
||||
&& previous_bytes == bytes
|
||||
&& let Some(mut rebase) = current.prepare_suffix_rebase(&prior.entry, &completed.result.execution)
|
||||
{
|
||||
// Keep the deepest compatible settled continuation per source interval.
|
||||
@@ -451,7 +461,7 @@ impl<'a> CapturedEntries<'a> {
|
||||
let Some(calls) = completed.calls.get(replayed..anchor.call_count) else {
|
||||
continue;
|
||||
};
|
||||
host.replay_suffix(calls, end_statistics, cancellation)?;
|
||||
host.replay_suffix(calls, end_statistics, None, cancellation)?;
|
||||
replayed = anchor.call_count;
|
||||
if anchor.host == host.files().replay_boundary() {
|
||||
let regular = CaptureSlots {
|
||||
@@ -464,7 +474,16 @@ impl<'a> CapturedEntries<'a> {
|
||||
self.capture(Capture::Settled { page, regular: Some(regular) }, Arc::new(entry), host);
|
||||
}
|
||||
}
|
||||
host.replay_suffix(&completed.calls[replayed..], end_statistics, cancellation)?;
|
||||
host.replay_suffix(
|
||||
&completed.calls[replayed..], end_statistics, closed_source.map(|child| child.usage),
|
||||
cancellation,
|
||||
)?;
|
||||
if let Some(child) = closed_source {
|
||||
let (trace, calls) = host.prefix().ok_or("child suffix host observations are unavailable")?;
|
||||
if closed_source_reads(&child.version, trace, calls) != Some(child.prefix_resource_reads) {
|
||||
return Err("completed suffix observed the changed child again".into());
|
||||
}
|
||||
}
|
||||
completed.guard.verify_replayed_inputs(host.files(), project).map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
@@ -521,17 +540,18 @@ impl<'a> CapturedEntries<'a> {
|
||||
recording.generated_outputs(),
|
||||
self.initial?,
|
||||
).ok()?;
|
||||
Some(guard)
|
||||
Some((guard, trace.clone()))
|
||||
}).flatten();
|
||||
let mut calls = recording.into_calls()?;
|
||||
if completion.is_none() {
|
||||
calls.truncate(replay_count);
|
||||
}
|
||||
let calls = Arc::<[Call]>::from(calls);
|
||||
let completion = completion.map(|guard| Arc::new(CompletedExecution {
|
||||
let completion = completion.map(|(guard, trace)| Arc::new(CompletedExecution {
|
||||
result: result.clone(),
|
||||
guard,
|
||||
calls: calls.clone(),
|
||||
trace,
|
||||
settled: self.settled,
|
||||
}));
|
||||
Some(Arc::new(CheckpointSet {
|
||||
@@ -996,23 +1016,38 @@ impl SelectedCheckpoint {
|
||||
let Some(new) = captures.settled.get(&page).filter(|_| allow_suffix) else {
|
||||
return CheckpointDecision::Continue;
|
||||
};
|
||||
let Some((completed, old, execution, layout)) = suffixes.clone().find_map(|completed| {
|
||||
let Some((completed, old, execution, layout, closed_source)) = suffixes.clone().find_map(|completed| {
|
||||
let old = completed.settled.get(&page)?;
|
||||
let host_matches = measure(&mut timings.suffix_host_validation, timed, || {
|
||||
old.call_count == new.call_count
|
||||
&& new.host.matches_for_suffix(&old.host)
|
||||
let closed_source = measure(&mut timings.suffix_host_validation, timed, || {
|
||||
if old.call_count != new.call_count {
|
||||
return None;
|
||||
}
|
||||
if new.host.matches_for_suffix(&old.host)
|
||||
&& can_replay_completed_suffix(completed, request, host.files())
|
||||
});
|
||||
if !host_matches {
|
||||
return None;
|
||||
}
|
||||
{
|
||||
return Some(None);
|
||||
}
|
||||
let change = entry.closed_source_change(&old.entry, &completed.result.execution)?;
|
||||
prove_closed_source_suffix(change, old, new, completed, request, host)
|
||||
.map(Some)
|
||||
})?;
|
||||
let kernel_timings = timed.then_some(&mut timings.suffix_kernel);
|
||||
let (execution, layout) = measure(&mut timings.suffix_kernel_validation, timed, || {
|
||||
entry.splice_settled_suffix_observed(
|
||||
&old.entry, &completed.result.execution, kernel_timings,
|
||||
)
|
||||
if let Some(child) = &closed_source {
|
||||
entry.splice_settled_closed_source_suffix_observed(
|
||||
&old.entry,
|
||||
&completed.result.execution,
|
||||
child.source_id,
|
||||
child.prefix_resource_reads,
|
||||
kernel_timings,
|
||||
)
|
||||
} else {
|
||||
entry.splice_settled_suffix_observed(
|
||||
&old.entry, &completed.result.execution, kernel_timings,
|
||||
)
|
||||
}
|
||||
})?;
|
||||
Some((completed, old, execution, layout))
|
||||
Some((completed, old, execution, layout, closed_source))
|
||||
}) else {
|
||||
return CheckpointDecision::Continue;
|
||||
};
|
||||
@@ -1024,7 +1059,8 @@ impl SelectedCheckpoint {
|
||||
});
|
||||
let replayed = measure(&mut timings.suffix_host_replay, timed, || {
|
||||
captures.replay_settled_suffix(
|
||||
page, &entry, old, completed, end_statistics, host, &cancellation,
|
||||
page, &entry, old, completed, end_statistics,
|
||||
closed_source.as_ref(), host, &cancellation,
|
||||
)
|
||||
});
|
||||
if let Err(error) = replayed {
|
||||
@@ -1090,6 +1126,61 @@ fn can_replay_completed_suffix(
|
||||
completed.guard.verify(request, files.replay_world(), files.preparation()).is_ok()
|
||||
}
|
||||
|
||||
fn prove_closed_source_suffix(
|
||||
change: ClosedSourceChange<'_>,
|
||||
old: &SettledCheckpoint,
|
||||
current: &SettledCheckpoint,
|
||||
completed: &CompletedExecution,
|
||||
request: &CompileRequest,
|
||||
host: &RecordingHost<'_>,
|
||||
) -> Option<ClosedSourceSuffix> {
|
||||
let path = Path::new(change.name);
|
||||
let guard = &completed.guard;
|
||||
let next_project = request.project.as_deref()?;
|
||||
if path == guard.entry || !guard.immutable.contains(path) || guard.generated.contains(path)
|
||||
|| !guard.project.immutable_paths().eq(next_project.immutable_paths())
|
||||
{
|
||||
return None;
|
||||
}
|
||||
// The child route cannot simultaneously relocate or replace the entry.
|
||||
if guard.project.file(&guard.entry)?.version != next_project.file(&guard.entry)?.version {
|
||||
return None;
|
||||
}
|
||||
let previous = &guard.project.file(path)?.version;
|
||||
let current_version = &next_project.file(path)?.version;
|
||||
if previous == current_version || previous.path != path || current_version.path != path
|
||||
|| previous.bytes != u64::try_from(change.previous_bytes).ok()?
|
||||
|| current_version.bytes != u64::try_from(change.current_bytes).ok()?
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let (trace, calls) = host.prefix()?;
|
||||
if calls.len() != current.call_count || trace.project_version.as_ref() != Some(next_project.version()) {
|
||||
return None;
|
||||
}
|
||||
let prefix_resource_reads = closed_source_reads(
|
||||
previous, old.host.trace_for_suffix(), completed.calls.get(..old.call_count)?,
|
||||
)?;
|
||||
// In the donor's whole job, the only observations of the child must
|
||||
// be the reads already present at the splice. The live prefix has
|
||||
// actually made the same count of allowed observations of its new version.
|
||||
if closed_source_reads(previous, &completed.trace, &completed.calls)? != prefix_resource_reads
|
||||
|| closed_source_reads(current_version, trace, calls)? != prefix_resource_reads
|
||||
|| !current.host.matches_for_closed_source_suffix(&old.host, previous, current_version)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let files = host.files();
|
||||
let project = guard.verify_project_with_closed_source(request, Some(path)).ok()?;
|
||||
guard.verify_host(project, files.replay_world(), files.preparation()).ok()?;
|
||||
Some(ClosedSourceSuffix {
|
||||
source_id: change.source_id,
|
||||
prefix_resource_reads,
|
||||
version: current_version.clone(),
|
||||
usage: ClosedSourceUsageOffset::new(previous.bytes, current_version.bytes, prefix_resource_reads)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn changed_line_start(previous: &[u8], current: &[u8]) -> Option<usize> {
|
||||
if previous == current {
|
||||
return None;
|
||||
@@ -1317,6 +1408,14 @@ impl PrefixInputGuard {
|
||||
fn verify_project<'a>(
|
||||
&self,
|
||||
request: &'a CompileRequest,
|
||||
) -> Result<&'a ProjectSnapshot, String> {
|
||||
self.verify_project_with_closed_source(request, None)
|
||||
}
|
||||
|
||||
fn verify_project_with_closed_source<'a>(
|
||||
&self,
|
||||
request: &'a CompileRequest,
|
||||
closed_source: Option<&Path>,
|
||||
) -> Result<&'a ProjectSnapshot, String> {
|
||||
let old = &self.original;
|
||||
if old.input != request.input
|
||||
@@ -1354,6 +1453,7 @@ impl PrefixInputGuard {
|
||||
let same_bytes = self.project.file(path).zip(current.file(path)).is_some_and(
|
||||
|(old, new)| {
|
||||
old.version == new.version
|
||||
|| closed_source == Some(path.as_path())
|
||||
|| self.nested_source.as_ref().is_some_and(|source| &source.path == path)
|
||||
|| self.immutable_sizes.get(path) == Some(&new.version.bytes)
|
||||
|| self.immutable_stream_prefixes.get(path).is_some_and(|&end| {
|
||||
|
||||
Reference in new issue
Block a user