Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/cumin-steps-named-on-their-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fnndsc/cumin": patch
---

Internal: logins, context moves, listings, uploads, PACS reads and checkpoint checks answer as before, in named steps.

How a failed login is classified, how a context move writes who it names, a PACS query's decoding and a patient's series, the peer store's search and listing, the page walks, a job's log stream, the context read at start, a feed's shares, the tag index, a download's stream and an upload, a folder proved empty, a local tree stored, the resource walks and the proc checkpoint's checks each have their steps named on their own. The calls made, their order and every answer and refusal are unchanged.
7 changes: 7 additions & 0 deletions .changeset/fond-names-and-listings-flattened.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fnndsc/fond": patch
---

Internal: placing a name, the trusted walk, a folder of mounts and a refusal's words answer as before, in named steps.

Placing a name that arrived, the trusted walk to a host's own folders, a listing of a folder of mounts and the words of a refused operation each have their steps named on their own. `wireName_place` judges an absolute name and the name's parts in steps of their own, and `placed_child` judges the parts and what stands on their way the same way; the trusted walk follows a link in a step of its own; the dispatcher's listing finds the names the mounts below a folder add, then lists that folder of mounts beside what the store holds there, in that order; a refused read or write is worded in one place. Every refusal, its words and its order are unchanged.
7 changes: 7 additions & 0 deletions .changeset/salsa-steps-named-on-their-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fnndsc/salsa": patch
---

Internal: feed data, copies, links, retrieves, DICOM slices, plugin checks and /proc answer as before, in named steps.

A feed's data facts, a proof's rows, a copy's target, a new folder, a link's resolution, a retrieve's progress and confirmation, a DICOM slice's gray levels and a series' summary, a plugin's image and upload rules, its sync plan and its adding, a pipeline parameter's value, the PACS folders and files, and /proc's paths, subtree, files, sweep and roster visits each have their steps named on their own. The calls made, their order and every answer and refusal are unchanged; the retrieve's task records live in a module of their own and are still reached through the watch.
243 changes: 188 additions & 55 deletions packages/cumin/src/cache/procCheckpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,44 @@ function procFeed_check(value: unknown): value is ProcFeed {
integer_check(feed.createdJobs);
}

/**
* Whether a persisted status is one an instance may be saved in: none yet,
* or a terminal one.
*
* @param status - The persisted status.
* @returns True when it is null or a terminal status.
*/
function procInstanceStatus_check(status: Partial<ProcInstance>['status']): boolean {
return status === null ||
(typeof status === 'string' && status_isTerminal(status));
}

/**
* Whether a persisted instance's run record is well formed: its start, end,
* output size, pipeline and workflow each absent or of its kind (the last
* two may be null).
*
* @param instance - The persisted instance.
* @returns True when every run field is absent or well formed.
*/
function procInstanceMetrics_check(instance: Partial<ProcInstance>): boolean {
return (instance.startedAt === undefined || typeof instance.startedAt === 'string') &&
(instance.finishedAt === undefined || typeof instance.finishedAt === 'string') &&
(instance.outputBytes === undefined || typeof instance.outputBytes === 'number') &&
(instance.pipelineName === undefined || instance.pipelineName === null || typeof instance.pipelineName === 'string') &&
(instance.workflowID === undefined || instance.workflowID === null || typeof instance.workflowID === 'number');
}

/** Reports whether a value is a structurally valid persisted instance. */
function procInstance_check(value: unknown): value is ProcInstance {
if (!value || typeof value !== 'object') return false;
const instance: Partial<ProcInstance> = value as Partial<ProcInstance>;
const parentValid: boolean = instance.parentID === null || integer_check(instance.parentID);
const pluginTypeValid: boolean = instance.pluginType === undefined || typeof instance.pluginType === 'string';
const statusValid: boolean = instance.status === null ||
(typeof instance.status === 'string' && status_isTerminal(instance.status));
const statusValid: boolean = procInstanceStatus_check(instance.status);
const joinsValid: boolean = instance.joinParentIDs === undefined ||
(Array.isArray(instance.joinParentIDs) && instance.joinParentIDs.every(integer_check));
const metricsValid: boolean =
(instance.startedAt === undefined || typeof instance.startedAt === 'string') &&
(instance.finishedAt === undefined || typeof instance.finishedAt === 'string') &&
(instance.outputBytes === undefined || typeof instance.outputBytes === 'number') &&
(instance.pipelineName === undefined || instance.pipelineName === null || typeof instance.pipelineName === 'string') &&
(instance.workflowID === undefined || instance.workflowID === null || typeof instance.workflowID === 'number');
const metricsValid: boolean = procInstanceMetrics_check(instance);
return metricsValid &&
integer_check(instance.id) &&
integer_check(instance.feedID) &&
Expand Down Expand Up @@ -225,23 +247,52 @@ function shardGraph_fault(feedID: number, instances: unknown[]): string | null {
byID.set(instance.id, instance);
}
for (const instance of byID.values()) {
if (instance.parentID !== null && !byID.has(instance.parentID)) {
return `instance ${instance.id} names parent ${instance.parentID}, which the shard does not hold`;
}
for (const id of instance.joinParentIDs ?? []) {
if (id === instance.id || !byID.has(id)) return `instance ${instance.id} names join parent ${id}, which the shard does not hold`;
}
const ancestors: Set<number> = new Set([instance.id]);
let ancestor: ProcInstance | undefined = instance.parentID === null ? undefined : byID.get(instance.parentID);
while (ancestor) {
if (ancestors.has(ancestor.id)) return `instance ${instance.id} sits on a cycle`;
ancestors.add(ancestor.id);
ancestor = ancestor.parentID === null ? undefined : byID.get(ancestor.parentID);
}
const fault: string | null = shardInstanceParents_fault(instance, byID);
if (fault !== null) return fault;
}
return null;
}

/**
* Why one instance of a shard does not stand on the shard's own instances,
* or null when it does: its parent and each join parent held (a join
* parent never the instance itself), and no cycle above it.
*
* @param instance - The instance.
* @param byID - The shard's instances, by id.
* @returns The first fault found, in words, or null.
*/
function shardInstanceParents_fault(instance: ProcInstance, byID: Map<number, ProcInstance>): string | null {
if (instance.parentID !== null && !byID.has(instance.parentID)) {
return `instance ${instance.id} names parent ${instance.parentID}, which the shard does not hold`;
}
for (const id of instance.joinParentIDs ?? []) {
if (id === instance.id || !byID.has(id)) return `instance ${instance.id} names join parent ${id}, which the shard does not hold`;
}
const parent: ProcInstance | undefined = instance.parentID === null ? undefined : byID.get(instance.parentID);
if (ancestry_isCyclic(instance.id, parent, byID)) return `instance ${instance.id} sits on a cycle`;
return null;
}

/**
* Whether the chain of parents above an instance comes back on itself.
*
* @param id - The instance's id.
* @param parent - Its parent, or undefined for a root.
* @param byID - The instances the chain is read through, by id.
* @returns True when an ancestor repeats (the instance's own id included).
*/
function ancestry_isCyclic(id: number, parent: ProcInstance | undefined, byID: Map<number, ProcInstance>): boolean {
const ancestors: Set<number> = new Set([id]);
let ancestor: ProcInstance | undefined = parent;
while (ancestor) {
if (ancestors.has(ancestor.id)) return true;
ancestors.add(ancestor.id);
ancestor = ancestor.parentID === null ? undefined : byID.get(ancestor.parentID);
}
return false;
}

/**
* Validates all snapshot fields and graph relationships before restoration.
*
Expand All @@ -251,48 +302,82 @@ function shardGraph_fault(feedID: number, instances: unknown[]): string | null {
function procCacheSnapshot_check(value: unknown): value is ProcCacheSnapshot {
if (!value || typeof value !== 'object') return false;
const snapshot: Partial<ProcCacheSnapshot> = value as Partial<ProcCacheSnapshot>;
if (!Array.isArray(snapshot.feeds) ||
!Array.isArray(snapshot.instances) ||
!Array.isArray(snapshot.topologyLoaded) ||
!snapshot.feeds.every(procFeed_check) ||
!snapshot.instances.every(procInstance_check) ||
!snapshot.topologyLoaded.every(integer_check)) {
if (!snapshotRows_check(snapshot)) {
return false;
}

const feedIDs: Set<number> = new Set(snapshot.feeds.map((feed: ProcFeed): number => feed.id));
const instanceByID: Map<number, ProcInstance> = new Map(
snapshot.instances.map((instance: ProcInstance): [number, ProcInstance] => [instance.id, instance]),
);
if (feedIDs.size !== snapshot.feeds.length ||
instanceByID.size !== snapshot.instances.length ||
new Set(snapshot.topologyLoaded).size !== snapshot.topologyLoaded.length ||
snapshot.topologyLoaded.some((feedID: number): boolean => !feedIDs.has(feedID))) {
if (!snapshotIDs_check(snapshot, feedIDs, instanceByID)) {
return false;
}

for (const instance of snapshot.instances) {
if (!feedIDs.has(instance.feedID)) return false;
const parent: ProcInstance | undefined = instance.parentID === null
? undefined
: instanceByID.get(instance.parentID);
if (instance.parentID !== null && (!parent || parent.feedID !== instance.feedID)) return false;
if (instance.joinParentIDs?.some((id: number): boolean => {
const joinParent: ProcInstance | undefined = instanceByID.get(id);
return !joinParent || joinParent.feedID !== instance.feedID || id === instance.id;
})) return false;

const ancestors: Set<number> = new Set([instance.id]);
let ancestor: ProcInstance | undefined = parent;
while (ancestor) {
if (ancestors.has(ancestor.id)) return false;
ancestors.add(ancestor.id);
ancestor = ancestor.parentID === null ? undefined : instanceByID.get(ancestor.parentID);
}
if (!snapshotInstance_stands(instance, feedIDs, instanceByID)) return false;
}
return true;
}

/** A snapshot whose three lists are there, as `snapshotRows_check` found them. */
type SnapshotRows = Partial<ProcCacheSnapshot> & Pick<ProcCacheSnapshot, 'feeds' | 'instances' | 'topologyLoaded'>;

/**
* Whether a snapshot's feeds, instances and loaded marks are lists of
* well-formed entries.
*
* @param snapshot - The snapshot candidate.
* @returns True when all three are lists and every entry is well formed.
*/
function snapshotRows_check(snapshot: Partial<ProcCacheSnapshot>): snapshot is SnapshotRows {
return Array.isArray(snapshot.feeds) &&
Array.isArray(snapshot.instances) &&
Array.isArray(snapshot.topologyLoaded) &&
snapshot.feeds.every(procFeed_check) &&
snapshot.instances.every(procInstance_check) &&
snapshot.topologyLoaded.every(integer_check);
}

/**
* Whether a snapshot's ids are unique (feeds, instances and loaded marks)
* and every loaded mark names a feed it holds.
*
* @param snapshot - The snapshot, its lists well formed.
* @param feedIDs - Its feeds' ids.
* @param instanceByID - Its instances, by id.
* @returns True when no id repeats and no loaded mark is a stranger.
*/
function snapshotIDs_check(snapshot: SnapshotRows, feedIDs: Set<number>, instanceByID: Map<number, ProcInstance>): boolean {
return !(feedIDs.size !== snapshot.feeds.length ||
instanceByID.size !== snapshot.instances.length ||
new Set(snapshot.topologyLoaded).size !== snapshot.topologyLoaded.length ||
snapshot.topologyLoaded.some((feedID: number): boolean => !feedIDs.has(feedID)));
}

/**
* Whether one instance of a snapshot stands: its feed held, its parent and
* each join parent held in its own feed (a join parent never itself), and
* no cycle above it.
*
* @param instance - The instance.
* @param feedIDs - The snapshot's feeds' ids.
* @param instanceByID - The snapshot's instances, by id.
* @returns True when the instance stands.
*/
function snapshotInstance_stands(instance: ProcInstance, feedIDs: Set<number>, instanceByID: Map<number, ProcInstance>): boolean {
if (!feedIDs.has(instance.feedID)) return false;
const parent: ProcInstance | undefined = instance.parentID === null
? undefined
: instanceByID.get(instance.parentID);
if (instance.parentID !== null && (!parent || parent.feedID !== instance.feedID)) return false;
if (instance.joinParentIDs?.some((id: number): boolean => {
const joinParent: ProcInstance | undefined = instanceByID.get(id);
return !joinParent || joinParent.feedID !== instance.feedID || id === instance.id;
})) return false;
return !ancestry_isCyclic(instance.id, parent, instanceByID);
}

/** Shared header checks for any checkpoint file of the current schema. */
function header_check(value: unknown, identity: string): value is { identity: string; writtenAt: string } {
if (!value || typeof value !== 'object') return false;
Expand Down Expand Up @@ -365,6 +450,57 @@ async function dir_ensure(dir: string): Promise<void> {
await fs.chmod(dir, 0o700);
}

/**
* The feeds a roster marks unseen, whole ids only; none from a roster
* written before the mark existed.
*
* @param roster - The roster.
* @returns The unseen feeds' ids.
*/
function rosterUnseen_read(roster: RosterFile): number[] {
return Array.isArray(roster.unseen) ? roster.unseen.filter((id: unknown): id is number => Number.isInteger(id)) : [];
}

/**
* Whether parsed JSON is a compatible roster file for an identity whose
* every feed is well formed.
*
* @param value - The parsed roster.
* @param identity - Canonical ChRIS identity string.
* @returns True when the roster may be read.
*/
function rosterFile_fits(value: unknown, identity: string): value is RosterFile {
return rosterFile_check(value, identity) && value.feeds.every(procFeed_check);
}

/**
* Whether parsed JSON is a compatible shard file for an identity that
* holds the feed its file name says.
*
* @param value - The parsed shard.
* @param identity - Canonical ChRIS identity string.
* @param feedID - The feed its file name says.
* @returns True when the shard may be read as that feed's.
*/
function shardFile_fits(value: unknown, identity: string, feedID: number): value is ShardFile {
return shardFile_check(value, identity) && value.feedID === feedID;
}

/**
* Takes a shard that stands into a snapshot: its instances, its feed's
* loaded mark when the shard was loaded, and its data facts when they are
* well formed.
*
* @param snapshot - The snapshot being assembled; added to in place.
* @param feedID - The shard's feed.
* @param shard - The shard.
*/
function shard_take(snapshot: ProcCacheSnapshot, feedID: number, shard: ShardFile): void {
snapshot.instances.push(...shard.instances);
if (shard.loaded) snapshot.topologyLoaded.push(feedID);
if (shard.dataFacts !== undefined && dataFacts_check(shard.dataFacts) && snapshot.dataFacts !== undefined) snapshot.dataFacts[feedID] = shard.dataFacts;
}

/**
* Assembles the sharded checkpoint into one snapshot. Shards whose feed is
* absent from the roster are orphans (a feed removed after its shard was
Expand All @@ -388,11 +524,10 @@ async function shards_assemble(
): Promise<{ snapshot: ProcCacheSnapshot; writtenAt: string; torn: string[] } | null> {
const roster: unknown = await json_read(join(dir, ROSTER_FILE));
if (roster === null) return null;
if (!rosterFile_check(roster, identity)) throw new Error('incompatible checkpoint');
if (!roster.feeds.every(procFeed_check)) throw new Error('incompatible checkpoint');
if (!rosterFile_fits(roster, identity)) throw new Error('incompatible checkpoint');
const feedIDs: Set<number> = new Set(roster.feeds.map((feed: ProcFeed): number => feed.id));

const unseen: number[] = Array.isArray(roster.unseen) ? roster.unseen.filter((id: unknown): id is number => Number.isInteger(id)) : [];
const unseen: number[] = rosterUnseen_read(roster);
const snapshot: ProcCacheSnapshot = { feeds: roster.feeds, instances: [], topologyLoaded: [], dataFacts: {}, unseen };
let skipped: number = 0;
const torn: string[] = [];
Expand All @@ -408,12 +543,10 @@ async function shards_assemble(
torn.push(`feed ${feedID}: unreadable (${shardReadFault_name(error)})`);
continue;
}
if (!shardFile_check(shard, identity) || shard.feedID !== feedID) { skipped++; continue; }
if (!shardFile_fits(shard, identity, feedID)) { skipped++; continue; }
const fault: string | null = shardGraph_fault(feedID, shard.instances);
if (fault !== null) { torn.push(`feed ${feedID}: ${fault}`); continue; }
snapshot.instances.push(...shard.instances);
if (shard.loaded) snapshot.topologyLoaded.push(feedID);
if (shard.dataFacts !== undefined && dataFacts_check(shard.dataFacts) && snapshot.dataFacts !== undefined) snapshot.dataFacts[feedID] = shard.dataFacts;
shard_take(snapshot, feedID, shard);
}
if (skipped > 0) {
errorStack.stack_push('warning', `proc checkpoint: ${skipped} feed shard(s) skipped (orphaned or unreadable)`);
Expand Down
Loading
Loading