@yarkivaev/scada 2.5.7 → 2.6.0
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/README.md +136 -2
- package/charts/scada/templates/_helpers.tpl +18 -3
- package/charts/scada/templates/postgres.yaml +6 -0
- package/charts/scada/templates/rabbitmq-config.yaml +5 -1
- package/charts/scada/templates/rabbitmq.yaml +12 -1
- package/charts/scada/templates/supervisor-sink.yaml +5 -0
- package/charts/scada/values.schema.json +46 -1
- package/charts/scada/values.yaml +7 -0
- package/db/migrations/E0005__historical_telemetry_grants.sql +5 -0
- package/db/migrations/E0006__historical_telemetry.sql +64 -0
- package/db/migrations/E0007__metric_receipts.sql +7 -0
- package/db/migrations/E0008__metric_receipts_grants.sql +1 -0
- package/db/migrations/E0009__history_continuation.sql +20 -0
- package/db/migrations/E0010__history_admissions.sql +8 -0
- package/db/migrations/E0011__history_classifier_checkpoints.sql +18 -0
- package/db/migrations/E0012__external_history_grants.sql +4 -0
- package/db/migrations/E0013__history_staged_corrections.sql +104 -0
- package/db/migrations/E0014__history_correction_grants.sql +5 -0
- package/db/migrations/E0015__history_retention_frontier.sql +7 -0
- package/db/migrations/E0016__history_retention_grants.sql +1 -0
- package/index.js +7 -0
- package/package.json +5 -3
- package/src/application/control/agent.js +61 -0
- package/src/application/control/catalog.js +22 -0
- package/src/application/control/controlClient.js +75 -0
- package/src/application/control/controlStore.js +55 -0
- package/src/application/control/controller.js +22 -0
- package/src/application/control/executionSnapshot.js +32 -0
- package/src/application/control/index.js +7 -0
- package/src/application/control/localAssignment.js +13 -0
- package/src/application/control/schedule.js +103 -0
- package/src/application/control/snapshotFile.js +24 -0
- package/src/application/foldedMetricsSink.js +1 -0
- package/src/application/mcp/mcpResources.js +1 -1
- package/src/application/mcp/mcpSchema.js +8 -3
- package/src/application/runtime/additionalAdapters.js +13 -9
- package/src/application/runtime/deliveryQueue.js +62 -0
- package/src/application/runtime/deliverySessions.js +28 -0
- package/src/application/runtime/modbusDelivery.js +36 -0
- package/src/application/runtime/pipelineAdapters.js +12 -8
- package/src/application/runtime/queuedDelivery.js +25 -0
- package/src/application/runtime/runCollector.js +34 -3
- package/src/application/runtime/runtime.js +2 -2
- package/src/application/runtime/runtimeHealth.js +1 -1
- package/src/application/runtime/runtimeServices.js +6 -2
- package/src/application/runtime/streamTask.js +13 -4
- package/src/application/runtime/taskContext.js +1 -0
- package/src/application/runtime/taskSlot.js +2 -2
- package/src/application/runtime/telemetrySink.js +18 -0
- package/src/application/siteServer.js +19 -4
- package/src/application/siteTelemetry.js +2 -2
- package/src/application/supervisorSink.js +32 -12
- package/src/application/sync/siteSyncBind.js +2 -2
- package/src/bin/controller.js +8 -0
- package/src/infrastructure/http/edge/edgeApi.js +2 -0
- package/src/infrastructure/http/edge/routes/history/historyRoutes.js +33 -0
- package/src/infrastructure/http/plant/routes/measurementRead.js +3 -0
- package/src/infrastructure/ingest/db/migrate.js +16 -0
- package/src/infrastructure/ingest/db/migrationProfile.js +6 -1
- package/src/infrastructure/ingest/db/retainMetricReceipts.js +8 -0
- package/src/infrastructure/ingest/db/runRetention.js +2 -0
- package/src/infrastructure/ingest/mqtt/metricsTransformer.js +11 -2
- package/src/infrastructure/ingest/mqtt/modbusMqtt.js +1 -1
- package/src/infrastructure/ingest/mqtt/modbusPublish.js +2 -0
- package/src/infrastructure/ingest/mqtt/mqttMetrics.js +1 -1
- package/src/infrastructure/ingest/telemetry/amqpMetricsIngest.js +7 -3
- package/src/infrastructure/ingest/telemetry/centralEnvelope.js +46 -0
- package/src/infrastructure/ingest/telemetry/centralMetricsSink.js +64 -0
- package/src/infrastructure/ingest/telemetry/centralPoint.js +14 -0
- package/src/infrastructure/ingest/telemetry/centralPolicy.js +51 -0
- package/src/infrastructure/ingest/telemetry/centralQueueSession.js +29 -0
- package/src/infrastructure/ingest/telemetry/centralTelemetryPreflight.js +31 -0
- package/src/infrastructure/ingest/telemetry/foldingSink.js +3 -0
- package/src/infrastructure/ingest/telemetry/historyTelemetry.js +32 -0
- package/src/infrastructure/persistence/clickhouse/canonicalMetrics.js +26 -0
- package/src/infrastructure/persistence/clickhouse/changes.js +4 -2
- package/src/infrastructure/persistence/clickhouse/connection.js +6 -8
- package/src/infrastructure/persistence/clickhouse/holds.js +10 -7
- package/src/infrastructure/persistence/clickhouse/latest.js +1 -1
- package/src/infrastructure/persistence/clickhouse/metricsQuery.js +70 -0
- package/src/infrastructure/persistence/clickhouse/metricsRelation.js +110 -0
- package/src/infrastructure/persistence/clickhouse/nativeMetricPoints.js +26 -0
- package/src/infrastructure/persistence/clickhouse/nativeMetricsAdmission.js +91 -0
- package/src/infrastructure/persistence/clickhouse/nativeMetricsClock.js +9 -0
- package/src/infrastructure/persistence/clickhouse/nativeMetricsCompaction.js +82 -0
- package/src/infrastructure/persistence/clickhouse/nativeMetricsInsert.js +31 -0
- package/src/infrastructure/persistence/clickhouse/nativeMetricsOwnership.js +8 -0
- package/src/infrastructure/persistence/clickhouse/nativeMetricsSchema.js +44 -0
- package/src/infrastructure/persistence/clickhouse/pollTopicCursors.js +3 -1
- package/src/infrastructure/persistence/clickhouse/rises.js +4 -2
- package/src/infrastructure/persistence/clickhouse/samples.js +10 -0
- package/src/infrastructure/persistence/clickhouse/sensor.js +4 -0
- package/src/infrastructure/persistence/clickhouse/series.js +38 -21
- package/src/infrastructure/persistence/pg/history.js +169 -0
- package/src/infrastructure/persistence/pg/historyAdmission.js +81 -0
- package/src/infrastructure/persistence/pg/historyCheckpoint.js +56 -0
- package/src/infrastructure/persistence/pg/historyConfigure.js +28 -0
- package/src/infrastructure/persistence/pg/historyContext.js +65 -0
- package/src/infrastructure/persistence/pg/historyCorrection.js +34 -0
- package/src/infrastructure/persistence/pg/historyObservations.js +56 -0
- package/src/infrastructure/persistence/pg/historySeed.js +27 -0
- package/src/infrastructure/persistence/pg/metrics.js +5 -4
- package/src/infrastructure/persistence/pg/metricsInsertSql.js +12 -0
- package/src/infrastructure/persistence/runtime/deliverySpool.js +124 -0
- package/src/infrastructure/persistence/stateDataFromPool.js +2 -0
- package/src/infrastructure/streams/amqpSink/session.js +6 -0
- package/src/infrastructure/streams/amqpSource.js +17 -4
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { mkdir, open, opendir, readFile, rename, stat, unlink } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
function filename(id) { return `${createHash('sha256').update(id).digest('hex')}.json`; }
|
|
6
|
+
async function syncDirectory(directory) {
|
|
7
|
+
const handle = await open(directory, 'r');
|
|
8
|
+
try { await handle.sync(); } finally { await handle.close(); }
|
|
9
|
+
}
|
|
10
|
+
async function replace(directory, name, value) {
|
|
11
|
+
const temporary = path.join(directory, `${name}.${randomUUID()}.tmp`);
|
|
12
|
+
const handle = await open(temporary, 'wx', 0o600);
|
|
13
|
+
try { await handle.writeFile(value); await handle.sync(); } finally { await handle.close(); }
|
|
14
|
+
await rename(temporary, path.join(directory, name));
|
|
15
|
+
await syncDirectory(directory);
|
|
16
|
+
}
|
|
17
|
+
function revive(key, value) {
|
|
18
|
+
return value?.type === 'Buffer' && Array.isArray(value.data) ? Buffer.from(value.data) : value;
|
|
19
|
+
}
|
|
20
|
+
async function restore(directory, entries, maxRecords, load) {
|
|
21
|
+
let bytes = 0;
|
|
22
|
+
const files = await opendir(directory);
|
|
23
|
+
for await (const file of files) {
|
|
24
|
+
const { name } = file;
|
|
25
|
+
if (/^[a-f0-9]{64}\.json$/u.test(name)) {
|
|
26
|
+
if (entries.size >= maxRecords) { throw new Error('Spool record capacity exceeded during recovery'); }
|
|
27
|
+
// Read one committed entry at a time; memory never holds the directory listing.
|
|
28
|
+
const record = await load(name);
|
|
29
|
+
if (typeof record.event?.eventId !== 'string' || filename(record.event.eventId) !== name
|
|
30
|
+
|| !Array.isArray(record.remaining) || !Array.isArray(record.recipients)) { throw new Error('Corrupt spool record'); }
|
|
31
|
+
const { size } = await stat(path.join(directory, name));
|
|
32
|
+
entries.set(record.event.eventId, { name, size }); bytes += size;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return bytes;
|
|
36
|
+
}
|
|
37
|
+
/* eslint max-lines-per-function: ["error", 100], max-statements: ["error", 25] */
|
|
38
|
+
/** Single writer on a persistent local volume. Failed/ambiguous acknowledgements retain data. */
|
|
39
|
+
export default function deliverySpool(directory, { maxBytes, maxRecords = 100000 } = {}) {
|
|
40
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) { throw new TypeError('Explicit positive spool maxBytes required'); }
|
|
41
|
+
if (!Number.isSafeInteger(maxRecords) || maxRecords <= 0) { throw new TypeError('Positive safe spool maxRecords required'); }
|
|
42
|
+
const entries = new Map();
|
|
43
|
+
const state = { bytes: 0, ready: false, busy: false };
|
|
44
|
+
async function exclusive(action) {
|
|
45
|
+
if (state.busy) { throw new Error('Spool operation in progress'); }
|
|
46
|
+
state.busy = true;
|
|
47
|
+
try { return await action(); } finally { Object.assign(state, { busy: false }); }
|
|
48
|
+
}
|
|
49
|
+
function current() { if (!state.ready) { throw new Error('Spool not opened'); } }
|
|
50
|
+
async function load(name) { return JSON.parse(await readFile(path.join(directory, name), 'utf8'), revive); }
|
|
51
|
+
return {
|
|
52
|
+
open() {
|
|
53
|
+
return exclusive(async () => {
|
|
54
|
+
Object.assign(state, { ready: false }); entries.clear(); state.bytes = 0;
|
|
55
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
56
|
+
const bytes = await restore(directory, entries, maxRecords, load);
|
|
57
|
+
Object.assign(state, { bytes });
|
|
58
|
+
Object.assign(state, { ready: true });
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
append(event, recipients) {
|
|
62
|
+
return exclusive(async () => {
|
|
63
|
+
current();
|
|
64
|
+
if (typeof event.eventId !== 'string' || !event.eventId || !Array.isArray(recipients) || !recipients.length
|
|
65
|
+
|| recipients.some((item) => { return typeof item !== 'string' || !item; })
|
|
66
|
+
|| new Set(recipients).size !== recipients.length) { throw new TypeError('Event identity and unique recipients required'); }
|
|
67
|
+
const existing = entries.get(event.eventId);
|
|
68
|
+
if (existing) {
|
|
69
|
+
const saved = await load(existing.name);
|
|
70
|
+
if (JSON.stringify(saved.event) !== JSON.stringify(event) || JSON.stringify(saved.recipients) !== JSON.stringify(recipients)) {
|
|
71
|
+
throw new Error('Conflicting event identity');
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (entries.size >= maxRecords) { throw new Error('Spool record capacity exceeded'); }
|
|
76
|
+
const value = JSON.stringify({ event, recipients, remaining: recipients });
|
|
77
|
+
const size = Buffer.byteLength(value);
|
|
78
|
+
if (state.bytes + size > maxBytes) { throw new Error('Spool capacity exceeded'); }
|
|
79
|
+
const name = filename(event.eventId);
|
|
80
|
+
// Reserve against ambiguous fsync outcomes; reopen reconciles actual files.
|
|
81
|
+
entries.set(event.eventId, { name, size }); state.bytes += size;
|
|
82
|
+
try { await replace(directory, name, value); } catch (error) { Object.assign(state, { ready: false }); throw error; }
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
ack(eventId, recipient) {
|
|
86
|
+
return exclusive(async () => {
|
|
87
|
+
current();
|
|
88
|
+
const entry = entries.get(eventId);
|
|
89
|
+
if (!entry) { return; }
|
|
90
|
+
const saved = await load(entry.name);
|
|
91
|
+
if (!saved.recipients.includes(recipient)) { throw new Error('Unknown recipient'); }
|
|
92
|
+
const remaining = saved.remaining.filter((item) => { return item !== recipient; });
|
|
93
|
+
if (remaining.length === saved.remaining.length) { return; }
|
|
94
|
+
try {
|
|
95
|
+
if (remaining.length === 0) {
|
|
96
|
+
await unlink(path.join(directory, entry.name)); await syncDirectory(directory);
|
|
97
|
+
entries.delete(eventId); state.bytes -= entry.size;
|
|
98
|
+
} else {
|
|
99
|
+
const value = JSON.stringify({ ...saved, remaining });
|
|
100
|
+
await replace(directory, entry.name, value);
|
|
101
|
+
const size = Buffer.byteLength(value); state.bytes += size - entry.size; entry.size = size;
|
|
102
|
+
}
|
|
103
|
+
} catch (error) { Object.assign(state, { ready: false }); throw error; }
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
pending(limit = 100) {
|
|
107
|
+
return exclusive(async () => {
|
|
108
|
+
current();
|
|
109
|
+
if (!Number.isSafeInteger(limit) || limit <= 0) { throw new TypeError('Positive pending limit required'); }
|
|
110
|
+
const rows = [];
|
|
111
|
+
for (const entry of Array.from(entries.values()).slice(0, limit)) {
|
|
112
|
+
// Read sequentially to bound decoded records in memory.
|
|
113
|
+
// eslint-disable-next-line no-await-in-loop
|
|
114
|
+
const { event, remaining } = await load(entry.name);
|
|
115
|
+
rows.push({ event, remaining });
|
|
116
|
+
// Rotate the window so an offline recipient cannot starve later destinations.
|
|
117
|
+
entries.delete(event.eventId); entries.set(event.eventId, entry);
|
|
118
|
+
}
|
|
119
|
+
return rows;
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
status() { return { bytes: state.bytes, maxBytes, recordLimit: maxRecords, pending: entries.size, ready: state.ready }; }
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import historyPg from './pg/history.js';
|
|
1
2
|
import alertsStatePg from './pg/alerts.js';
|
|
2
3
|
import checkpointStatePg from './pg/checkpoints.js';
|
|
3
4
|
import metricsStateDisabled from './memory/metricsDisabled.js';
|
|
@@ -12,6 +13,7 @@ export default function stateDataFromPool(pool, options = {}) {
|
|
|
12
13
|
segments: segmentStatePg(pool),
|
|
13
14
|
alerts: alertsStatePg(pool),
|
|
14
15
|
metrics,
|
|
16
|
+
history: metricsEnabled ? historyPg(pool, { retentionDays: options.historyRetentionDays ?? 30 }) : undefined,
|
|
15
17
|
checkpoints: checkpointStatePg(pool, metricsEnabled),
|
|
16
18
|
operations: operationStatePg(pool)
|
|
17
19
|
};
|
|
@@ -53,6 +53,12 @@ async function configure(state, live) {
|
|
|
53
53
|
await Promise.all((state.options.exchanges || []).map((item) => {
|
|
54
54
|
return live.channel.assertExchange(item.name, item.type || 'topic', item.options || {durable: true});
|
|
55
55
|
}));
|
|
56
|
+
await Promise.all((state.options.queues || []).map((item) => {
|
|
57
|
+
return live.channel.assertQueue(item.name, item.options || { durable: true, autoDelete: false });
|
|
58
|
+
}));
|
|
59
|
+
await Promise.all((state.options.bindings || []).map((item) => {
|
|
60
|
+
return live.channel.bindQueue(item.queue, item.exchange, item.pattern, item.args);
|
|
61
|
+
}));
|
|
56
62
|
}
|
|
57
63
|
async function current(state, live) {
|
|
58
64
|
if (live.epoch !== state.epoch || !state.wanted) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable no-await-in-loop */
|
|
1
2
|
import amqpSession from './amqpSource/session.js';
|
|
2
3
|
import budgetAccept from './budgetAccept.js';
|
|
3
4
|
|
|
@@ -14,6 +15,7 @@ function deliveryFor(channel, message, options) {
|
|
|
14
15
|
let settled = false;
|
|
15
16
|
return {
|
|
16
17
|
content: message.content, fields: message.fields, properties: message.properties,
|
|
18
|
+
active() {return !settled && options.active();},
|
|
17
19
|
ack() {
|
|
18
20
|
if (!settled && options.active()) { channel.ack(message); settled = true; }
|
|
19
21
|
},
|
|
@@ -48,6 +50,16 @@ function consume(channel, collector, options) {
|
|
|
48
50
|
}
|
|
49
51
|
};
|
|
50
52
|
}
|
|
53
|
+
async function rejectCapacity(channel,message,options) {
|
|
54
|
+
reportFailure(options,new Error(options.preserveOnCapacity ? 'AMQP payload capacity exceeded; retained for retry' : 'AMQP payload capacity exceeded; rejected without requeue'));
|
|
55
|
+
if (options.preserveOnCapacity) {
|
|
56
|
+
// Never NACK a canonical head into the ready queue behind later data.
|
|
57
|
+
while(options.active()) {await new Promise((resolve) => {setTimeout(resolve,250);});}
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
try {channel.nack(message,false,false);}
|
|
61
|
+
catch(error) {reportFailure(options,error);}
|
|
62
|
+
}
|
|
51
63
|
function trackedConsume(channel, collector, options, pending) {
|
|
52
64
|
const accept = consume(channel, collector, options);
|
|
53
65
|
const state = { bytes: 0 };
|
|
@@ -55,9 +67,7 @@ function trackedConsume(channel, collector, options, pending) {
|
|
|
55
67
|
if (!options.active()) { return Promise.resolve(); }
|
|
56
68
|
const bytes = message?.content?.length ?? 0;
|
|
57
69
|
if (bytes > options.maxMessageBytes || state.bytes + bytes > options.maxPendingBytes) {
|
|
58
|
-
|
|
59
|
-
reportFailure(options, new Error('AMQP payload capacity exceeded; rejected without requeue'));
|
|
60
|
-
return Promise.resolve();
|
|
70
|
+
return rejectCapacity(channel,message,options);
|
|
61
71
|
}
|
|
62
72
|
state.bytes += bytes;
|
|
63
73
|
const delivery = accept(message);
|
|
@@ -86,8 +96,11 @@ export default function amqpSource(url, queue, collector, options = {}) {
|
|
|
86
96
|
const state = {};
|
|
87
97
|
const session = amqpSession(url, async (channel) => {
|
|
88
98
|
const generation = state.session.generation();
|
|
99
|
+
const live = {closed:false};
|
|
100
|
+
channel.on('close',() => {live.closed=true;});
|
|
101
|
+
channel.on('error',() => {live.closed=true;});
|
|
89
102
|
const config = { ...settings, active() {
|
|
90
|
-
return state.session.active() && state.session.generation() === generation;
|
|
103
|
+
return !live.closed && state.session.active() && state.session.generation() === generation;
|
|
91
104
|
} };
|
|
92
105
|
const name = await topology(channel, queue, settings);
|
|
93
106
|
const result = await channel.consume(name, trackedConsume(channel, collector, config, pending), { noAck: false, ...(settings.exclusive ? { exclusive: true } : {}) });
|