@yarkivaev/scada 2.6.2 → 2.6.3
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
CHANGED
|
@@ -751,6 +751,6 @@ Use `node --expose-gc scripts/measure-delivery-spool.mjs FIXTURE.json 120000` wi
|
|
|
751
751
|
|
|
752
752
|
No observation is merged, sampled or discarded by batching. An `accept()` promise succeeds only after durable commit. A sparse group flushes on its timer; capacity thresholds, reads and close force earlier flushing. The delay bounds the coalescing window, not disk latency or end-to-end completion. Queue status retains durable `pending`/`bytes` and adds `queuedOperations`, `queuedBytes` and `drainFull` diagnostics.
|
|
753
753
|
|
|
754
|
-
Receipt admission
|
|
754
|
+
The positive safe-integer `maxInFlight` option defaults to32. A drain initiates at most that many publications concurrently, in stored order per logical AMQP session. All concurrent writers await the same session opening; one mandatory return does not retire independent confirms. Receipt admission and serialized publication bytes are reserved before network publication, then the receipt is queued only after its individual mandatory confirmation. The retained publication bytes count toward `maxPendingBytes`, separately from receipt mutation bytes counted toward the group byte limit. Confirmed receipts accumulate in a bounded window; `drain()` counts them delivered only after their FULL commit. Graceful close rejects new source submissions, flushes existing submissions, waits for the active drain and its receipts, then closes sessions and persistence. Caller write deadlines abort the actual AMQP writer and retire its session. A caller timeout alone does not release a publication slot or its prospective ACK reservation; late actual success still commits its receipt, and late callbacks from an aborted AMQP generation cannot ACK a retry. A custom factory that ignores both AbortSignal and stop remains fenced and bounded until actual settlement. Close fails after its bounded session wait and retains spool ownership if that work cannot quiesce; retry close after settlement. The existing crash-after-confirm/before-local-receipt replay contract remains unchanged.
|
|
755
755
|
|
|
756
756
|
Healthy full drain pages with durable progress continue on subsequent event-loop turns; after four pages the scheduler pauses 25 ms. Failed, empty or zero-progress pages back off 1s. This removes the former 100-event/s scheduling ceiling without spinning on an offline recipient. Actual append and online catch-up throughput still require qualification on the target filesystem; a serial producer awaiting every individual acceptance cannot benefit from concurrent group formation.
|
package/package.json
CHANGED
|
@@ -27,7 +27,7 @@ export default function deliveryOperations(apply, options = {}) {
|
|
|
27
27
|
}
|
|
28
28
|
function release(bytes) { state.operations -= 1; state.bytes -= bytes; notifyIdle(); }
|
|
29
29
|
function settle(job, result) {
|
|
30
|
-
release(job.bytes);
|
|
30
|
+
release(job.reservedBytes ?? job.bytes);
|
|
31
31
|
if (result.ok) { job.resolve(result.value); } else { job.reject(result.error); }
|
|
32
32
|
}
|
|
33
33
|
function group() {
|
|
@@ -77,25 +77,27 @@ export default function deliveryOperations(apply, options = {}) {
|
|
|
77
77
|
schedule(job.kind === 'barrier' || state.operations >= Math.min(config.maxRecords, config.maxOperations) ? 0 : config.maxDelayMs);
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
|
-
function checkBytes(bytes) {
|
|
80
|
+
function checkBytes(bytes, retainedBytes = 0) {
|
|
81
81
|
positive(bytes, 'Mutation bytes', true);
|
|
82
|
-
|
|
82
|
+
positive(retainedBytes, 'Retained bytes', true);
|
|
83
|
+
if (state.bytes + bytes + retainedBytes > config.maxPendingBytes) { throw new Error('Delivery pending byte capacity exceeded'); }
|
|
83
84
|
if (bytes > config.maxBytes) { throw new Error('Delivery group byte capacity exceeded'); }
|
|
84
85
|
}
|
|
85
86
|
function prepare(build) {
|
|
86
87
|
reserve();
|
|
87
88
|
let value;
|
|
88
89
|
try {
|
|
89
|
-
value = build(checkBytes); checkBytes(value.bytes);
|
|
90
|
+
value = build(checkBytes); checkBytes(value.bytes, value.retainedBytes);
|
|
90
91
|
} catch (error) { release(0); throw error; }
|
|
91
|
-
|
|
92
|
+
const reservedBytes = value.bytes + (value.retainedBytes ?? 0);
|
|
93
|
+
state.bytes += reservedBytes;
|
|
92
94
|
let settled = false;
|
|
93
95
|
return {
|
|
94
96
|
commit() {
|
|
95
97
|
if (settled) { return Promise.reject(new Error('Delivery reservation already settled')); }
|
|
96
|
-
settled = true; return enqueue({ kind: 'mutation', ...value });
|
|
98
|
+
settled = true; return enqueue({ kind: 'mutation', ...value, reservedBytes });
|
|
97
99
|
},
|
|
98
|
-
cancel() { if (!settled) { settled = true; release(
|
|
100
|
+
cancel() { if (!settled) { settled = true; release(reservedBytes); } }
|
|
99
101
|
};
|
|
100
102
|
}
|
|
101
103
|
return {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** A slot and its prospective durable receipt stay reserved until the actual writer settles. */
|
|
2
|
+
/* eslint max-lines-per-function: ["error", 80], max-statements: ["error", 25] */
|
|
3
|
+
export default function deliveryPublications(commands, sessions, options) {
|
|
4
|
+
const limit = options.maxInFlight ?? 32;
|
|
5
|
+
if (!Number.isSafeInteger(limit) || limit <= 0) { throw new TypeError('Positive safe maxInFlight required'); }
|
|
6
|
+
const active = new Map();
|
|
7
|
+
function publish(record, id) {
|
|
8
|
+
const key = JSON.stringify([record.event.eventId, id]);
|
|
9
|
+
if (active.has(key) || active.size >= limit) { throw new Error('Delivery publication capacity exceeded'); }
|
|
10
|
+
const receipt = commands.prepare((checkBytes) => {
|
|
11
|
+
const operation = { kind: 'ack', eventId: record.event.eventId, recipient: id };
|
|
12
|
+
const bytes = Buffer.byteLength(JSON.stringify(operation));
|
|
13
|
+
const retainedBytes = Buffer.byteLength(JSON.stringify(record.event));
|
|
14
|
+
checkBytes(bytes, retainedBytes); return { operation, bytes, retainedBytes };
|
|
15
|
+
});
|
|
16
|
+
let write;
|
|
17
|
+
try {
|
|
18
|
+
const descriptor = record.event.destinations.find((item) => { return item.id === id; });
|
|
19
|
+
const event = record.event.observation;
|
|
20
|
+
write = sessions.write(descriptor, { ...event, options: { ...event.options, persistent: true, mandatory: true, messageId: event.eventId } });
|
|
21
|
+
} catch (error) { receipt.cancel(); throw error; }
|
|
22
|
+
const settled = write.settled.then(() => { return receipt.commit(); }, (error) => { receipt.cancel(); throw error; })
|
|
23
|
+
.finally(() => { active.delete(key); });
|
|
24
|
+
active.set(key, settled);
|
|
25
|
+
void settled.catch(() => {
|
|
26
|
+
// The caller reports failures; late rejection still releases only the actual reservation.
|
|
27
|
+
});
|
|
28
|
+
return write.result.then(() => { return settled; });
|
|
29
|
+
}
|
|
30
|
+
return { publish, limit };
|
|
31
|
+
}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import deliverySpool from '../../infrastructure/persistence/runtime/deliverySpool.js';
|
|
2
2
|
import deliverySessions from './deliverySessions.js';
|
|
3
3
|
import deliveryOperations from './deliveryOperations.js';
|
|
4
|
+
import deliveryPublications from './deliveryPublications.js';
|
|
4
5
|
|
|
5
|
-
function confirmed(event) {
|
|
6
|
-
return { ...event, options: { ...event.options, persistent: true, mandatory: true, messageId: event.eventId } };
|
|
7
|
-
}
|
|
8
6
|
function snapshot(operation, checkBytes) {
|
|
9
7
|
const json = JSON.stringify(operation); checkBytes(Buffer.byteLength(json));
|
|
10
8
|
const frozen = JSON.parse(json);
|
|
@@ -19,43 +17,37 @@ export default function deliveryQueue(directory, options) {
|
|
|
19
17
|
const sessions = deliverySessions(options.factory, options.writeTimeoutMs);
|
|
20
18
|
const batchSize = options.batchSize ?? 100;
|
|
21
19
|
if (!Number.isSafeInteger(batchSize) || batchSize <= 0) { throw new TypeError('Positive safe batchSize required'); }
|
|
22
|
-
const
|
|
20
|
+
const publications = deliveryPublications(commands, sessions, options);
|
|
21
|
+
const receiptWindow = Math.min(publications.limit, options.maxOperations ?? 128);
|
|
23
22
|
const state = { draining: false, closed: false, full: false };
|
|
24
|
-
async function publish(record, id) {
|
|
25
|
-
if (state.closed) { throw new Error('Delivery queue closed'); }
|
|
26
|
-
// Reserve the eventual receipt BEFORE publication; confirmation can never lose its admission slot.
|
|
27
|
-
const receipt = commands.prepare((checkBytes) => { return snapshot({ kind: 'ack', eventId: record.event.eventId, recipient: id }, checkBytes); });
|
|
28
|
-
try {
|
|
29
|
-
const descriptor = record.event.destinations.find((item) => { return item.id === id; });
|
|
30
|
-
await sessions.write(descriptor, confirmed(record.event.observation));
|
|
31
|
-
return { receipt: receipt.commit() };
|
|
32
|
-
} catch (error) { receipt.cancel(); throw error; }
|
|
33
|
-
}
|
|
34
23
|
async function drain() {
|
|
35
24
|
state.draining = true;
|
|
36
25
|
const result = { delivered: 0, failed: 0 };
|
|
37
|
-
const receipts =
|
|
26
|
+
const receipts = new Set();
|
|
38
27
|
function failure(error) {
|
|
39
28
|
result.failed += 1;
|
|
40
29
|
try { options.onError?.(error); } catch {
|
|
41
30
|
// Observers cannot prevent independent recipient progress or release.
|
|
42
31
|
}
|
|
43
32
|
}
|
|
44
|
-
async function flush() { commands.flush(); await Promise.all(receipts
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
33
|
+
async function flush() { commands.flush(); await Promise.all(receipts); }
|
|
34
|
+
function receive(row, id) {
|
|
35
|
+
const receipt = Promise.resolve().then(() => { return publications.publish(row, id); })
|
|
36
|
+
.then(() => { result.delivered += 1; }, failure).finally(() => { receipts.delete(receipt); });
|
|
37
|
+
receipts.add(receipt);
|
|
38
|
+
}
|
|
39
|
+
async function advance(row, id) {
|
|
40
|
+
if (!state.closed) { receive(row, id); }
|
|
41
|
+
if (receipts.size >= receiptWindow) { await Promise.race(receipts); }
|
|
51
42
|
}
|
|
52
43
|
try {
|
|
53
44
|
const rows = await commands.barrier(() => { return spool.pending(batchSize); });
|
|
54
45
|
Object.assign(state, { full: rows.length === batchSize });
|
|
55
46
|
for (const row of rows) {
|
|
56
47
|
for (const id of row.remaining) {
|
|
48
|
+
// Actual timed-out publications retain their global slots across page/caller completion.
|
|
57
49
|
// eslint-disable-next-line no-await-in-loop
|
|
58
|
-
await
|
|
50
|
+
await advance(row, id);
|
|
59
51
|
}
|
|
60
52
|
}
|
|
61
53
|
return result;
|
|
@@ -83,9 +75,10 @@ export default function deliveryQueue(directory, options) {
|
|
|
83
75
|
await state.activeDrain?.catch(() => {
|
|
84
76
|
// Drain caller receives the original error.
|
|
85
77
|
});
|
|
78
|
+
await sessions.close();
|
|
86
79
|
await commands.idle();
|
|
87
|
-
|
|
88
|
-
})();
|
|
80
|
+
await spool.close();
|
|
81
|
+
})().catch((error) => { state.closing = null; throw error; });
|
|
89
82
|
return state.closing;
|
|
90
83
|
},
|
|
91
84
|
status() {
|
|
@@ -1,28 +1,66 @@
|
|
|
1
1
|
import deadline from './deadline.js';
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/** Shared opening and actual settlement outlive caller deadlines; stop fences failed sessions. */
|
|
4
|
+
/* eslint max-lines-per-function: ["error", 100], max-statements: ["error", 25] */
|
|
4
5
|
export default function deliverySessions(factory, timeoutMs = 10000) {
|
|
5
6
|
const sessions = new Map();
|
|
7
|
+
const state = { closed: false };
|
|
8
|
+
function retire(key, entry) {
|
|
9
|
+
entry.closing ??= (async () => {
|
|
10
|
+
if (!entry.opened) { await entry.sink.stop?.(); }
|
|
11
|
+
await entry.opening.catch(() => {
|
|
12
|
+
// A failed opening must still be stopped; a late successful one may own new resources.
|
|
13
|
+
});
|
|
14
|
+
await entry.sink.stop?.();
|
|
15
|
+
await Promise.allSettled(entry.pending);
|
|
16
|
+
if (sessions.get(key) === entry) { sessions.delete(key); }
|
|
17
|
+
})();
|
|
18
|
+
return entry.closing;
|
|
19
|
+
}
|
|
20
|
+
function session(key, descriptor) {
|
|
21
|
+
let entry = sessions.get(key);
|
|
22
|
+
if (entry?.closing) { throw new Error('Delivery session is closing'); }
|
|
23
|
+
if (!entry) {
|
|
24
|
+
if (sessions.size >= 64) { throw new Error('Delivery session capacity exceeded'); }
|
|
25
|
+
const sink = factory(descriptor);
|
|
26
|
+
entry = { sink, pending: new Set(), opened: false };
|
|
27
|
+
entry.opening = Promise.resolve().then(() => { return sink.start?.(); }).finally(() => { entry.opened = true; });
|
|
28
|
+
sessions.set(key, entry);
|
|
29
|
+
void entry.opening.catch(() => { return retire(key, entry); }).catch(() => {
|
|
30
|
+
// Failed shutdown keeps the failed opening fenced.
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return entry;
|
|
34
|
+
}
|
|
6
35
|
return {
|
|
7
|
-
|
|
36
|
+
write(descriptor, event) {
|
|
37
|
+
if (state.closed) { throw new Error('Delivery sessions closed'); }
|
|
8
38
|
const key = JSON.stringify(descriptor);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
39
|
+
const entry = session(key, descriptor);
|
|
40
|
+
const controller = new AbortController();
|
|
41
|
+
const settled = entry.opening.then(() => {
|
|
42
|
+
if (controller.signal.aborted || entry.closing) { throw controller.signal.reason || new Error('Delivery session is closing'); }
|
|
43
|
+
return entry.sink.write([event], { signal: controller.signal });
|
|
44
|
+
});
|
|
45
|
+
entry.pending.add(settled);
|
|
46
|
+
let completed = false;
|
|
47
|
+
void settled.finally(() => { completed = true; entry.pending.delete(settled); }).catch(() => {
|
|
48
|
+
// A publication rejection is independent of other confirms on this session.
|
|
49
|
+
});
|
|
50
|
+
const result = deadline(() => { return settled; }, timeoutMs).catch((error) => {
|
|
51
|
+
if (!completed) {
|
|
52
|
+
controller.abort(error);
|
|
53
|
+
void retire(key, entry).catch(() => {
|
|
54
|
+
// Actual work and ownership remain fenced if stop cannot complete.
|
|
17
55
|
});
|
|
18
|
-
throw error;
|
|
19
56
|
}
|
|
20
|
-
|
|
21
|
-
|
|
57
|
+
throw error;
|
|
58
|
+
});
|
|
59
|
+
return { result, settled };
|
|
22
60
|
},
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
61
|
+
close() {
|
|
62
|
+
state.closed = true;
|
|
63
|
+
return deadline(() => { return Promise.all(Array.from(sessions, ([key, entry]) => { return retire(key, entry); })); }, timeoutMs);
|
|
26
64
|
}
|
|
27
65
|
};
|
|
28
66
|
}
|