@yarkivaev/scada 2.6.6 → 2.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -12,7 +12,9 @@ export default function validateCatalog(catalog) {
|
|
|
12
12
|
for (const task of catalog.tasks) {
|
|
13
13
|
if (typeof task.taskId !== 'string' || tasks.has(task.taskId) || !task.spec?.adapter || !Number.isSafeInteger(task.copies ?? 2) || (task.copies ?? 2) < 1
|
|
14
14
|
|| !Number.isFinite(task.cost ?? 1) || (task.cost ?? 1) <= 0 || (task.pinnedNode && !nodes.has(task.pinnedNode))
|
|
15
|
-
|| (task.eligibleNodes && (!Array.isArray(task.eligibleNodes) || task.eligibleNodes.some((id) => { return !nodes.has(id); })))
|
|
15
|
+
|| (task.eligibleNodes && (!Array.isArray(task.eligibleNodes) || task.eligibleNodes.some((id) => { return !nodes.has(id); })))
|
|
16
|
+
|| (task.preference && (!Array.isArray(task.preference) || new Set(task.preference).size !== task.preference.length || task.preference.some((id) => { return !nodes.has(id) || (task.eligibleNodes && !task.eligibleNodes.includes(id)); })))
|
|
17
|
+
|| (task.near && (!Array.isArray(task.near) || new Set(task.near).size !== task.near.length || task.near.some((id) => { return !identifier.test(id); })))) {
|
|
16
18
|
throw new TypeError('Invalid control task');
|
|
17
19
|
}
|
|
18
20
|
if ((task.spec.adapter === 'serial' || String(task.spec.from ?? '').startsWith('rtu:')) && !task.pinnedNode) { throw new TypeError('Serial task requires pinnedNode'); }
|
|
@@ -7,12 +7,42 @@ function clockSkew(value) {
|
|
|
7
7
|
if (!Number.isFinite(value) || value <= 0) { throw new TypeError('Positive finite clock skew bound required'); }
|
|
8
8
|
}
|
|
9
9
|
function copies(task) { return task.pinnedNode ? 1 : task.copies ?? 2; }
|
|
10
|
+
function preferenceRank(task, id) {
|
|
11
|
+
if (!task.preference) { return 0; }
|
|
12
|
+
const index = task.preference.indexOf(id);
|
|
13
|
+
return index < 0 ? task.preference.length : index;
|
|
14
|
+
}
|
|
15
|
+
function hasCache(task, node) {
|
|
16
|
+
if (!task.near?.length) { return 0; }
|
|
17
|
+
const held = new Set(node.assignments.map((row) => { return row.taskId; }));
|
|
18
|
+
return task.near.some((id) => { return held.has(id); }) ? 0 : 1;
|
|
19
|
+
}
|
|
20
|
+
function byPlace(task) {
|
|
21
|
+
return (left, right) => {
|
|
22
|
+
return hasCache(task, left) - hasCache(task, right) || preferenceRank(task, left.id) - preferenceRank(task, right.id) || left.load / left.capacity - right.load / right.capacity || left.id.localeCompare(right.id);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
10
25
|
function freshRead(report, input, { now, freshnessMs, controlEpoch, clockSkewMs }) {
|
|
11
26
|
return inEpoch(report, controlEpoch) && report?.tasks?.some((row) => {
|
|
12
27
|
return row.taskId === input.taskId && row.generation === input.generation && row.state === 'running' && !['degraded', 'failed'].includes(row.health)
|
|
13
28
|
&& Number.isFinite(row.lastRead) && row.lastRead <= now + clockSkewMs && now - row.lastRead <= freshnessMs;
|
|
14
29
|
});
|
|
15
30
|
}
|
|
31
|
+
function retain(slots, tasks, owners, observed, timing) {
|
|
32
|
+
for (const task of tasks) {
|
|
33
|
+
const confirmed = owners(task).filter((node) => {
|
|
34
|
+
const input = node.assignments.find((row) => { return row.taskId === task.taskId; });
|
|
35
|
+
return wanted(task, node) && node.assignments.some((row) => { return row.taskId === task.taskId && same(row.spec, task.spec); }) && freshRead(observed.get(node.id), input, timing);
|
|
36
|
+
});
|
|
37
|
+
if (confirmed.length >= copies(task)) {
|
|
38
|
+
const ordered = task.preference ? [...confirmed].sort(byPlace(task)) : confirmed;
|
|
39
|
+
const keep = new Set(ordered.slice(0, copies(task)).map((node) => { return node.id; }));
|
|
40
|
+
slots.filter((node) => { return node.live && !keep.has(node.id); }).forEach((node) => {
|
|
41
|
+
node.assignments = node.assignments.filter((row) => { return row.taskId !== task.taskId; });
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
16
46
|
function failedRead(report, input, controlEpoch) {
|
|
17
47
|
return input && inEpoch(report, controlEpoch) && report?.tasks?.some((row) => {
|
|
18
48
|
return row.taskId === input.taskId && row.generation === input.generation && row.state === 'failed';
|
|
@@ -64,7 +94,7 @@ export default function schedule(catalog, reports, current, { now = Date.now(),
|
|
|
64
94
|
const assignments = previous.assignments.filter((row) => { return !live(node.id) || taskMap.has(row.taskId); });
|
|
65
95
|
return { ...node, previous, assignments: [...assignments], live: live(node.id), load: assignments.reduce((total, row) => { return total + (taskMap.get(row.taskId)?.cost ?? 1); }, 0) };
|
|
66
96
|
});
|
|
67
|
-
const tasks = [...catalog.tasks].sort((a, b) => { return Number(Boolean(b.pinnedNode)) - Number(Boolean(a.pinnedNode)) || a.taskId.localeCompare(b.taskId); });
|
|
97
|
+
const tasks = [...catalog.tasks].sort((a, b) => { return Number(Boolean(b.pinnedNode)) - Number(Boolean(a.pinnedNode)) || Number(Boolean(a.near?.length)) - Number(Boolean(b.near?.length)) || a.taskId.localeCompare(b.taskId); });
|
|
68
98
|
function owners(task) {
|
|
69
99
|
return slots.filter((node) => {
|
|
70
100
|
const input = node.assignments.find((row) => { return row.taskId === task.taskId; });
|
|
@@ -81,7 +111,7 @@ export default function schedule(catalog, reports, current, { now = Date.now(),
|
|
|
81
111
|
const surviving = active.some((node) => { return freshRead(observed.get(node.id), node.assignments.find((row) => { return row.taskId === task.taskId; }), { now, freshnessMs, controlEpoch, clockSkewMs }); });
|
|
82
112
|
const waiting = surviving && missing.some((node) => { return now - (observed.get(node.id)?.receivedAt ?? 0) < replacementDelayMs; });
|
|
83
113
|
const candidates = slots.filter((node) => { return node.live && wanted(task, node) && !active.includes(node) && !node.assignments.some((row) => { return row.taskId === task.taskId; }) && node.load + (task.cost ?? 1) <= node.capacity; });
|
|
84
|
-
candidates.sort((
|
|
114
|
+
candidates.sort(byPlace(task));
|
|
85
115
|
if (active.length <= round && active.length < copies(task) && !waiting && candidates.length) {
|
|
86
116
|
const node = candidates[0];
|
|
87
117
|
node.assignments.push({ taskId: task.taskId, specVersion: 1, generation: 0, assignedAt: now, spec: task.spec });
|
|
@@ -89,15 +119,6 @@ export default function schedule(catalog, reports, current, { now = Date.now(),
|
|
|
89
119
|
}
|
|
90
120
|
}
|
|
91
121
|
}
|
|
92
|
-
|
|
93
|
-
const active = owners(task);
|
|
94
|
-
const confirmed = active.filter((node) => { return wanted(task, node) && node.assignments.some((row) => { return row.taskId === task.taskId && same(row.spec, task.spec); }) && freshRead(observed.get(node.id), node.assignments.find((row) => { return row.taskId === task.taskId; }), { now, freshnessMs, controlEpoch, clockSkewMs }); });
|
|
95
|
-
if (confirmed.length >= copies(task)) {
|
|
96
|
-
const keep = new Set(confirmed.slice(0, copies(task)).map((node) => { return node.id; }));
|
|
97
|
-
slots.filter((node) => { return node.live && !keep.has(node.id); }).forEach((node) => {
|
|
98
|
-
node.assignments = node.assignments.filter((row) => { return row.taskId !== task.taskId; });
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
}
|
|
122
|
+
retain(slots, tasks, owners, observed, { now, freshnessMs, controlEpoch, clockSkewMs });
|
|
102
123
|
return snapshots(slots, taskMap, catalog, observed, { now, freshnessMs, clockSkewMs });
|
|
103
124
|
}
|