@yarkivaev/scada 2.6.16 → 2.6.18
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
|
@@ -4,6 +4,15 @@ function encoded(value) { return Buffer.from(value).toString('base64'); }
|
|
|
4
4
|
function decoded(value) { return Buffer.from(value, 'base64').toString(); }
|
|
5
5
|
function put(key, value, lease) { return { request_put: { key: encoded(key), value: encoded(JSON.stringify(value)), ...(lease && { lease }) } }; }
|
|
6
6
|
function revision(key, value) { return { key: encoded(key), target: 'MOD', result: 'EQUAL', mod_revision: String(value) }; }
|
|
7
|
+
function cursor(call, root, range) {
|
|
8
|
+
return {
|
|
9
|
+
async load(taskId) {
|
|
10
|
+
const row = await range(`${root}/cursors/${taskId}`);
|
|
11
|
+
return row ? JSON.parse(decoded(row.value)) : null;
|
|
12
|
+
},
|
|
13
|
+
save(taskId, value) { return call('/v3/kv/put', put(`${root}/cursors/${taskId}`, value).request_put); }
|
|
14
|
+
};
|
|
15
|
+
}
|
|
7
16
|
export default function controlStore(call, prefix = '/scada/control') {
|
|
8
17
|
const root = prefix.replace(/\/$/u, '');
|
|
9
18
|
const lock = `${root}/leader`;
|
|
@@ -41,6 +50,7 @@ export default function controlStore(call, prefix = '/scada/control') {
|
|
|
41
50
|
return Boolean(result.succeeded);
|
|
42
51
|
},
|
|
43
52
|
async assignment(id) { const row = await range(`${root}/assignments/${id}`); return row ? JSON.parse(decoded(row.value)) : null; },
|
|
53
|
+
cursor: cursor(call, root, range),
|
|
44
54
|
report(id, value) { return call('/v3/kv/put', put(`${root}/reports/${id}`, value).request_put); },
|
|
45
55
|
async publish(catalog, expectedRevision) {
|
|
46
56
|
validateCatalog(catalog);
|
|
@@ -28,6 +28,30 @@ function freshRead(report, input, { now, freshnessMs, controlEpoch, clockSkewMs
|
|
|
28
28
|
&& Number.isFinite(row.lastRead) && row.lastRead <= now + clockSkewMs && now - row.lastRead <= freshnessMs;
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
|
+
function holderOf(slots, task) {
|
|
32
|
+
const holders = slots.filter((node) => { return node.assignments.some((row) => { return row.taskId === task.taskId; }); });
|
|
33
|
+
return holders.length === 1 ? holders[0] : null;
|
|
34
|
+
}
|
|
35
|
+
function destination(slots, task, holder, timing) {
|
|
36
|
+
const input = holder.assignments.find((row) => { return row.taskId === task.taskId; });
|
|
37
|
+
if (timing.now - (input.assignedAt ?? 0) < timing.replacementDelayMs) { return null; }
|
|
38
|
+
const candidates = slots.filter((node) => {
|
|
39
|
+
return node.live && wanted(task, node) && (node.id === holder.id || node.load + (task.cost ?? 1) <= node.capacity);
|
|
40
|
+
});
|
|
41
|
+
const home = [...candidates].sort(byPlace(task))[0];
|
|
42
|
+
return home && home.id !== holder.id ? home : null;
|
|
43
|
+
}
|
|
44
|
+
function reclaim(slots, tasks, timing) {
|
|
45
|
+
tasks.filter((task) => { return copies(task) === 1; }).forEach((task) => {
|
|
46
|
+
const holder = holderOf(slots, task);
|
|
47
|
+
const home = holder ? destination(slots, task, holder, timing) : null;
|
|
48
|
+
if (!home) { return; }
|
|
49
|
+
holder.assignments = holder.assignments.filter((row) => { return row.taskId !== task.taskId; });
|
|
50
|
+
holder.load -= task.cost ?? 1;
|
|
51
|
+
home.assignments.push({ taskId: task.taskId, specVersion: 1, generation: 0, assignedAt: timing.now, spec: task.spec });
|
|
52
|
+
home.load += task.cost ?? 1;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
31
55
|
function retain(slots, tasks, owners, observed, timing) {
|
|
32
56
|
for (const task of tasks) {
|
|
33
57
|
const confirmed = owners(task).filter((node) => {
|
|
@@ -119,6 +143,7 @@ export default function schedule(catalog, reports, current, { now = Date.now(),
|
|
|
119
143
|
}
|
|
120
144
|
}
|
|
121
145
|
}
|
|
146
|
+
reclaim(slots, tasks, { now, replacementDelayMs });
|
|
122
147
|
retain(slots, tasks, owners, observed, { now, freshnessMs, controlEpoch, clockSkewMs });
|
|
123
148
|
return snapshots(slots, taskMap, catalog, observed, { now, freshnessMs, clockSkewMs });
|
|
124
149
|
}
|
|
@@ -28,12 +28,11 @@ function healthView(runtime, state) {
|
|
|
28
28
|
initialized: state.initialized && (!state.agent || state.agent.status().appliedGeneration !== null) };
|
|
29
29
|
} };
|
|
30
30
|
}
|
|
31
|
-
async function initialize(collector, owned, state, { env, registry }) {
|
|
31
|
+
async function initialize(collector, owned, state, { env, registry, store }) {
|
|
32
32
|
const { runtime } = collector;
|
|
33
33
|
await owned.open?.();
|
|
34
34
|
if (state.stopped) { throw new Error('Collector stopped during recovery'); }
|
|
35
|
-
if (
|
|
36
|
-
const store = controlStore(clientFromEnv(env, 'COLLECTOR_CONTROL'), env.COLLECTOR_CONTROL_PREFIX);
|
|
35
|
+
if (store) {
|
|
37
36
|
state.agent = assignmentAgent({ nodeId: env.COLLECTOR_NODE_ID, file: env.COLLECTOR_ASSIGNMENT_FILE, store, runtime,
|
|
38
37
|
validate(document) { return validateManifest(document, registry); },
|
|
39
38
|
onError(error) { process.stderr.write(`collector control: ${error.name}\n`); } });
|
|
@@ -45,7 +44,9 @@ async function initialize(collector, owned, state, { env, registry }) {
|
|
|
45
44
|
/** Local executable host. The external agent can instead use createRuntime directly. */
|
|
46
45
|
// eslint-disable-next-line max-statements
|
|
47
46
|
export default async function runCollector({ registry, env = process.env, argv = process.argv.slice(2), signals = true, manifestReader = readManifest, ...services }) {
|
|
48
|
-
const
|
|
47
|
+
const store = env.COLLECTOR_CONTROL_ENDPOINTS
|
|
48
|
+
? controlStore(clientFromEnv(env, 'COLLECTOR_CONTROL'), env.COLLECTOR_CONTROL_PREFIX) : null;
|
|
49
|
+
const owned = runtimeServices(env, { ...services, cursor: services.cursor ?? store?.cursor });
|
|
49
50
|
const runtime = createRuntime({ registry, ...owned });
|
|
50
51
|
const state = { initialized: false, failed: false, health: null, reloading: false, stopped: false, detach: null, agent: null };
|
|
51
52
|
const collector = {
|
|
@@ -73,7 +74,7 @@ export default async function runCollector({ registry, env = process.env, argv =
|
|
|
73
74
|
handle: spoolSurface(owned.deliveryQueue, env.STATE_HTTP_TOKEN) }) }); }
|
|
74
75
|
const recoveryMs = Number(env.COLLECTOR_RECOVERY_TIMEOUT_MS ?? 900000);
|
|
75
76
|
if (!Number.isSafeInteger(recoveryMs) || recoveryMs < 1) { throw new TypeError('Positive collector recovery timeout required'); }
|
|
76
|
-
await deadline(() => { return initialize(collector, owned, state, { env, registry }); }, recoveryMs);
|
|
77
|
+
await deadline(() => { return initialize(collector, owned, state, { env, registry, store }); }, recoveryMs);
|
|
77
78
|
} catch (error) {
|
|
78
79
|
Object.assign(state, { failed: true });
|
|
79
80
|
try { await collector.stop(); } catch (cleanup) { throw new AggregateError([error, cleanup], 'Collector startup cleanup failed'); }
|
|
@@ -75,7 +75,7 @@ export default function createRuntime(options) {
|
|
|
75
75
|
maxTasks: positive(options.maxTasks ?? 1000), maxPending: positive(options.maxPending ?? 100),
|
|
76
76
|
stopTimeoutMs: positive(options.stopTimeoutMs ?? 10000), startTimeoutMs: positive(options.startTimeoutMs ?? 10000),
|
|
77
77
|
budget: taskBudget(options.budgets ?? { maxBytes: 16777216, maxRecords: 16384 }),
|
|
78
|
-
services: { stateStore: options.stateStore, secrets: options.secrets, report: options.report, deliveryQueue: options.deliveryQueue } };
|
|
78
|
+
services: { stateStore: options.stateStore, secrets: options.secrets, report: options.report, deliveryQueue: options.deliveryQueue, cursor: options.cursor } };
|
|
79
79
|
if (!(ctx.registry instanceof Map)) { throw new TypeError('registry must be a Map'); }
|
|
80
80
|
return {
|
|
81
81
|
apply(input) { return enqueue(ctx, input, (slot) => { return apply(ctx, input, slot); }); },
|