@futdevpro/nts-dynamo 1.15.78 → 1.15.80
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/.dynamo/logs/cicd-pipeline/output.log +1639 -1555
- package/.dynamo/logs/cicd-pipeline/status.json +38 -38
- package/build/_collections/global-settings.const.d.ts.map +1 -1
- package/build/_collections/global-settings.const.js +11 -0
- package/build/_collections/global-settings.const.js.map +1 -1
- package/build/_models/interfaces/global-settings.interface.d.ts +18 -0
- package/build/_models/interfaces/global-settings.interface.d.ts.map +1 -1
- package/build/_services/base/data.service.d.ts +8 -0
- package/build/_services/base/data.service.d.ts.map +1 -1
- package/build/_services/base/data.service.js +17 -0
- package/build/_services/base/data.service.js.map +1 -1
- package/build/_services/core/collection-growth-monitor.service.d.ts +77 -0
- package/build/_services/core/collection-growth-monitor.service.d.ts.map +1 -0
- package/build/_services/core/collection-growth-monitor.service.js +147 -0
- package/build/_services/core/collection-growth-monitor.service.js.map +1 -0
- package/build/_services/server/app.server.d.ts.map +1 -1
- package/build/_services/server/app.server.js +12 -0
- package/build/_services/server/app.server.js.map +1 -1
- package/build/index.d.ts +1 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/_collections/global-settings.const.ts +12 -0
- package/src/_models/interfaces/global-settings.interface.ts +19 -0
- package/src/_services/base/data.service.spec.ts +56 -1
- package/src/_services/base/data.service.ts +23 -2
- package/src/_services/core/collection-growth-monitor.service.spec.ts +67 -0
- package/src/_services/core/collection-growth-monitor.service.ts +211 -0
- package/src/_services/server/app.server.ts +12 -0
- package/src/index.ts +1 -0
|
@@ -3,7 +3,7 @@ import { DyNTS_DataService } from './data.service';
|
|
|
3
3
|
import { DyNTS_DBService } from './db.service';
|
|
4
4
|
import { DyNTS_GlobalService } from '../core/global.service';
|
|
5
5
|
import { DyNTS_ArchiveDataService } from './archive-data.service';
|
|
6
|
-
import { DyFM_Metadata, DyFM_DataModel_Params, DyFM_Error, DyFM_DBFilter, DyFM_SearchQuery, DyFM_BasicProperty_Type, DyFM_EnvironmentFlag } from '@futdevpro/fsm-dynamo';
|
|
6
|
+
import { DyFM_Metadata, DyFM_DataModel_Params, DyFM_Error, DyFM_DBFilter, DyFM_SearchQuery, DyFM_BasicProperty_Type, DyFM_EnvironmentFlag, DyFM_Log } from '@futdevpro/fsm-dynamo';
|
|
7
7
|
import { DyNTS_global_settings } from '../../_collections/global-settings.const';
|
|
8
8
|
|
|
9
9
|
// Initialize global settings before any test runs
|
|
@@ -273,6 +273,61 @@ describe('| DyNTS_DataService', () => {
|
|
|
273
273
|
expect(result).toEqual(mockDataList);
|
|
274
274
|
expect(service.dataList).toEqual(mockDataList);
|
|
275
275
|
});
|
|
276
|
+
|
|
277
|
+
// FR-258 / SR-3 — over-fetch guard (unbounded find → heap → OOM precursor). Warns, never caps.
|
|
278
|
+
describe('| over-fetch warning (FR-258 / SR-3)', () => {
|
|
279
|
+
let originalThreshold: number;
|
|
280
|
+
|
|
281
|
+
beforeEach(() => {
|
|
282
|
+
originalThreshold = DyNTS_DataService.findDataListWarnThreshold;
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
afterEach(() => {
|
|
286
|
+
DyNTS_DataService.findDataListWarnThreshold = originalThreshold;
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
function listOf(count: number): TestMetadata[] {
|
|
290
|
+
return Array.from({ length: count }, (_v, i: number): TestMetadata =>
|
|
291
|
+
new TestMetadata({ _id: `id-${i}`, name: `n-${i}` } as TestMetadata));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
it('| warns when the result EXCEEDS the threshold', async () => {
|
|
295
|
+
DyNTS_DataService.findDataListWarnThreshold = 3;
|
|
296
|
+
const warnSpy = spyOn(DyFM_Log, 'warn');
|
|
297
|
+
mockDBService.find.and.returnValue(Promise.resolve(listOf(5)));
|
|
298
|
+
|
|
299
|
+
const result = await service.findDataList({ name: 'Test' });
|
|
300
|
+
|
|
301
|
+
expect(result.length).toBe(5);
|
|
302
|
+
const overFetchCall = warnSpy.calls.allArgs().find((args: any[]): boolean =>
|
|
303
|
+
typeof args[0] === 'string' && args[0].includes('[FR-258 SR-3]'));
|
|
304
|
+
expect(overFetchCall).toBeDefined();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it('| does NOT warn at exactly the threshold (strictly greater)', async () => {
|
|
308
|
+
DyNTS_DataService.findDataListWarnThreshold = 5;
|
|
309
|
+
const warnSpy = spyOn(DyFM_Log, 'warn');
|
|
310
|
+
mockDBService.find.and.returnValue(Promise.resolve(listOf(5)));
|
|
311
|
+
|
|
312
|
+
await service.findDataList({ name: 'Test' });
|
|
313
|
+
|
|
314
|
+
const overFetchCall = warnSpy.calls.allArgs().find((args: any[]): boolean =>
|
|
315
|
+
typeof args[0] === 'string' && args[0].includes('[FR-258 SR-3]'));
|
|
316
|
+
expect(overFetchCall).toBeUndefined();
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it('| disabled (threshold <= 0) → never warns regardless of size', async () => {
|
|
320
|
+
DyNTS_DataService.findDataListWarnThreshold = 0;
|
|
321
|
+
const warnSpy = spyOn(DyFM_Log, 'warn');
|
|
322
|
+
mockDBService.find.and.returnValue(Promise.resolve(listOf(50000)));
|
|
323
|
+
|
|
324
|
+
await service.findDataList({ name: 'Test' });
|
|
325
|
+
|
|
326
|
+
const overFetchCall = warnSpy.calls.allArgs().find((args: any[]): boolean =>
|
|
327
|
+
typeof args[0] === 'string' && args[0].includes('[FR-258 SR-3]'));
|
|
328
|
+
expect(overFetchCall).toBeUndefined();
|
|
329
|
+
});
|
|
330
|
+
});
|
|
276
331
|
});
|
|
277
332
|
|
|
278
333
|
describe('| updateData', () => {
|
|
@@ -81,6 +81,15 @@ export class DyNTS_DataService<T extends DyFM_Metadata> {
|
|
|
81
81
|
/** error code base */
|
|
82
82
|
ecBase: string = `${DyNTS_global_settings.systemShortCodeName}|`;
|
|
83
83
|
|
|
84
|
+
/**
|
|
85
|
+
* FR-258 / SR-3 — over-fetch küszöb. Ennél több dokumentumot visszaadó {@link findDataList} hívás
|
|
86
|
+
* az unbounded-growth → GC-thrash/OOM vektor (2026-06-23 incidens): a teljes (akár több-100k-s)
|
|
87
|
+
* collection a heap-be töltődik. NEM capeljük a query-t (az törné a hívókat) — csak WARNINGot
|
|
88
|
+
* logolunk, hogy az over-fetch LÁTHATÓ legyen és paginálható/szűkíthető. Runtime felülírható
|
|
89
|
+
* (`DyNTS_DataService.findDataListWarnThreshold = N`). Default: 10000.
|
|
90
|
+
*/
|
|
91
|
+
static findDataListWarnThreshold: number = 10000;
|
|
92
|
+
|
|
84
93
|
dataDBService: DyNTS_DBService<T>;
|
|
85
94
|
haveArchiveDataService: boolean;
|
|
86
95
|
|
|
@@ -676,11 +685,23 @@ export class DyNTS_DataService<T extends DyFM_Metadata> {
|
|
|
676
685
|
this.dataList = dataListExists;
|
|
677
686
|
}
|
|
678
687
|
|
|
688
|
+
// FR-258 / SR-3 — over-fetch guard. Egy szűretlen find, ami egy nagy collection EGÉSZÉT a heap-be
|
|
689
|
+
// tölti, a GC-thrash/OOM vektor (2026-06-23). NEM capeljük a query-t (az törné a hívókat), csak
|
|
690
|
+
// WARNINGot logolunk, hogy az over-fetch LÁTHATÓ legyen → paginate/szűkebb filter a megoldás.
|
|
691
|
+
const overFetchThreshold: number = DyNTS_DataService.findDataListWarnThreshold;
|
|
692
|
+
if (overFetchThreshold > 0 && dataListExists.length > overFetchThreshold) {
|
|
693
|
+
DyFM_Log.warn(
|
|
694
|
+
`[FR-258 SR-3] findDataList "${this.dataParams.dataName}" returned ${dataListExists.length} documents ` +
|
|
695
|
+
`(> ${overFetchThreshold}) — the whole result is held in heap. Consider a tighter filter or ` +
|
|
696
|
+
`pagination (skip/limit) to avoid unbounded memory growth.`,
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
|
|
679
700
|
return dataListExists;
|
|
680
701
|
} catch (error) {
|
|
681
|
-
throw new DyFM_Error({
|
|
702
|
+
throw new DyFM_Error({
|
|
682
703
|
...this._getDefaultErrorSettings('findDataList', error),
|
|
683
|
-
|
|
704
|
+
|
|
684
705
|
errorCode: `${this.ecBase}DyNTS-DS0-FDS0`,
|
|
685
706
|
});
|
|
686
707
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { DyNTS_CollectionGrowthAlert, DyNTS_CollectionGrowthMonitor } from './collection-growth-monitor.service';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* FR-258 / SR-4 — unit tests for the pure growth-evaluation core
|
|
5
|
+
* (`DyNTS_CollectionGrowthMonitor.evaluateGrowth`). No I/O, no timers — covers size-threshold,
|
|
6
|
+
* growth-rate, both/neither, and the first-poll (no prior snapshot) edge cases.
|
|
7
|
+
*/
|
|
8
|
+
describe('| DyNTS_CollectionGrowthMonitor.evaluateGrowth (FR-258 / SR-4)', () => {
|
|
9
|
+
|
|
10
|
+
const SIZE_THRESHOLD: number = 250000;
|
|
11
|
+
const GROWTH_PCT: number = 50;
|
|
12
|
+
|
|
13
|
+
function kinds(alerts: DyNTS_CollectionGrowthAlert[]): string[] {
|
|
14
|
+
return alerts.map((a: DyNTS_CollectionGrowthAlert): string => a.kind).sort();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
it('| under threshold + no prior snapshot → no alerts', () => {
|
|
18
|
+
const alerts: DyNTS_CollectionGrowthAlert[] =
|
|
19
|
+
DyNTS_CollectionGrowthMonitor.evaluateGrowth('logs', 100, undefined, SIZE_THRESHOLD, GROWTH_PCT);
|
|
20
|
+
expect(alerts).toEqual([]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('| over size threshold → a "size" alert with the right detail', () => {
|
|
24
|
+
const alerts: DyNTS_CollectionGrowthAlert[] =
|
|
25
|
+
DyNTS_CollectionGrowthMonitor.evaluateGrowth('cdp_report_details_archiveds', 300000, undefined, SIZE_THRESHOLD, GROWTH_PCT);
|
|
26
|
+
expect(kinds(alerts)).toEqual(['size']);
|
|
27
|
+
expect(alerts[0].dataName).toBe('cdp_report_details_archiveds');
|
|
28
|
+
expect(alerts[0].detail).toEqual({ collection: 'cdp_report_details_archiveds', docCount: 300000, threshold: SIZE_THRESHOLD });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('| exactly AT the threshold → no size alert (strictly greater)', () => {
|
|
32
|
+
const alerts: DyNTS_CollectionGrowthAlert[] =
|
|
33
|
+
DyNTS_CollectionGrowthMonitor.evaluateGrowth('logs', SIZE_THRESHOLD, undefined, SIZE_THRESHOLD, GROWTH_PCT);
|
|
34
|
+
expect(alerts).toEqual([]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('| grew more than the growth-rate % since last poll → a "growth" alert', () => {
|
|
38
|
+
const alerts: DyNTS_CollectionGrowthAlert[] =
|
|
39
|
+
DyNTS_CollectionGrowthMonitor.evaluateGrowth('pipeline_jobs', 200, { docCount: 100, atMs: 0 }, SIZE_THRESHOLD, GROWTH_PCT);
|
|
40
|
+
expect(kinds(alerts)).toEqual(['growth']);
|
|
41
|
+
expect(alerts[0].detail).toEqual({ collection: 'pipeline_jobs', oldCount: 100, newCount: 200, growthPct: 100 });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('| grew LESS than the growth-rate % → no growth alert', () => {
|
|
45
|
+
const alerts: DyNTS_CollectionGrowthAlert[] =
|
|
46
|
+
DyNTS_CollectionGrowthMonitor.evaluateGrowth('logs', 110, { docCount: 100, atMs: 0 }, SIZE_THRESHOLD, GROWTH_PCT);
|
|
47
|
+
expect(alerts).toEqual([]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('| both over-threshold AND fast-growth → BOTH alerts', () => {
|
|
51
|
+
const alerts: DyNTS_CollectionGrowthAlert[] =
|
|
52
|
+
DyNTS_CollectionGrowthMonitor.evaluateGrowth('big', 400000, { docCount: 100000, atMs: 0 }, SIZE_THRESHOLD, GROWTH_PCT);
|
|
53
|
+
expect(kinds(alerts)).toEqual(['growth', 'size']);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('| prior snapshot with zero docCount → no growth alert (avoid divide-by-zero)', () => {
|
|
57
|
+
const alerts: DyNTS_CollectionGrowthAlert[] =
|
|
58
|
+
DyNTS_CollectionGrowthMonitor.evaluateGrowth('fresh', 5000, { docCount: 0, atMs: 0 }, SIZE_THRESHOLD, GROWTH_PCT);
|
|
59
|
+
expect(alerts).toEqual([]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('| shrinking collection (negative growth) → no growth alert', () => {
|
|
63
|
+
const alerts: DyNTS_CollectionGrowthAlert[] =
|
|
64
|
+
DyNTS_CollectionGrowthMonitor.evaluateGrowth('pruned', 50, { docCount: 100, atMs: 0 }, SIZE_THRESHOLD, GROWTH_PCT);
|
|
65
|
+
expect(alerts).toEqual([]);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { DyFM_Error, DyFM_ErrorLevel, DyFM_Log } from '@futdevpro/fsm-dynamo';
|
|
2
|
+
|
|
3
|
+
import { DyNTS_global_settings } from '../../_collections/global-settings.const';
|
|
4
|
+
import { DyNTS_SingletonServiceBase } from '../base/singleton.service-base';
|
|
5
|
+
import { DyNTS_GlobalService } from './global.service';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* FR-258 / SR-4 — Collection Growth Monitor (proactive companion to DyNTS_MemoryGuard).
|
|
9
|
+
*
|
|
10
|
+
* The MemoryGuard is REACTIVE: it fires when the heap is already near OOM. This monitor is
|
|
11
|
+
* PROACTIVE: it periodically samples each registered collection's document count (via the cheap,
|
|
12
|
+
* scan-free `estimatedDocumentCount()`) and raises a warning into the Errors-sink BEFORE a bloated
|
|
13
|
+
* collection can be loaded into heap and GC-thrash the process (the 2026-06-23 overseer incident:
|
|
14
|
+
* `cdp_report_details_archiveds` reached 3 GB / `pipeline_jobs` 2 GB, unbounded).
|
|
15
|
+
*
|
|
16
|
+
* It pairs with SR-1/SR-2 (declarative retention TTL): TTL keeps collections bounded; this monitor
|
|
17
|
+
* surfaces the ones that AREN'T (no retention declared, or growing faster than retention prunes) so
|
|
18
|
+
* an operator can add `retention` before it becomes an OOM. Pure observation — never deletes.
|
|
19
|
+
*
|
|
20
|
+
* Like the MemoryGuard it is a singleton, idempotent, fully non-fatal (a monitoring layer must never
|
|
21
|
+
* crash a server), and its timer is `unref()`-ed so it can't keep the process alive on its own.
|
|
22
|
+
*/
|
|
23
|
+
export interface DyNTS_CollectionGrowthMonitor_Config {
|
|
24
|
+
/** Master switch. Default: `DyNTS_global_settings.collectionGrowthMonitor.enabled` (true). */
|
|
25
|
+
enabled?: boolean;
|
|
26
|
+
/** Poll cadence in ms. Default: 300000 (5 min) — collection growth is slow, no need to poll often. */
|
|
27
|
+
pollIntervalMs?: number;
|
|
28
|
+
/** Warn when a collection's document count exceeds this. Default: 250000. */
|
|
29
|
+
sizeThresholdDocCount?: number;
|
|
30
|
+
/** Warn when a collection grew by more than this percent since the previous poll. Default: 50. */
|
|
31
|
+
growthRateThresholdPct?: number;
|
|
32
|
+
/** Re-alert cooldown per collection per alert-kind, in ms (anti-flood). Default: 3600000 (1 h). */
|
|
33
|
+
reAlertCooldownMs?: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** One growth finding to surface to the Errors-sink. */
|
|
37
|
+
export interface DyNTS_CollectionGrowthAlert {
|
|
38
|
+
kind: 'size' | 'growth';
|
|
39
|
+
dataName: string;
|
|
40
|
+
message: string;
|
|
41
|
+
detail: Record<string, unknown>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface CollectionSnapshot {
|
|
45
|
+
docCount: number;
|
|
46
|
+
atMs: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export class DyNTS_CollectionGrowthMonitor extends DyNTS_SingletonServiceBase {
|
|
50
|
+
|
|
51
|
+
static getInstance(): DyNTS_CollectionGrowthMonitor {
|
|
52
|
+
return DyNTS_CollectionGrowthMonitor.getSingletonInstance() as DyNTS_CollectionGrowthMonitor;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private timer: ReturnType<typeof setInterval> | null = null;
|
|
56
|
+
private installed: boolean = false;
|
|
57
|
+
|
|
58
|
+
// Resolved (effective) config — filled at install().
|
|
59
|
+
private pollIntervalMs: number = 300000;
|
|
60
|
+
private sizeThresholdDocCount: number = 250000;
|
|
61
|
+
private growthRateThresholdPct: number = 50;
|
|
62
|
+
private reAlertCooldownMs: number = 3600000;
|
|
63
|
+
|
|
64
|
+
private readonly lastSnapshotByCollection: Map<string, CollectionSnapshot> = new Map();
|
|
65
|
+
/** Last time we alerted, keyed by `${dataName}:${kind}` — for the anti-flood cooldown. */
|
|
66
|
+
private readonly lastAlertAtByKey: Map<string, number> = new Map();
|
|
67
|
+
|
|
68
|
+
protected constructor() {
|
|
69
|
+
super();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Start the monitor. Idempotent (a second call is a no-op). `config` overrides
|
|
74
|
+
* `DyNTS_global_settings.collectionGrowthMonitor`, which overrides the built-in defaults. NEVER
|
|
75
|
+
* throws — every error is try/catch-ed and descriptively logged.
|
|
76
|
+
*/
|
|
77
|
+
install(config?: DyNTS_CollectionGrowthMonitor_Config): void {
|
|
78
|
+
if (this.installed) { return; }
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const g: NonNullable<typeof DyNTS_global_settings.collectionGrowthMonitor> =
|
|
82
|
+
DyNTS_global_settings.collectionGrowthMonitor ?? { enabled: true };
|
|
83
|
+
|
|
84
|
+
if (!(config?.enabled ?? g.enabled ?? true)) { return; }
|
|
85
|
+
|
|
86
|
+
this.pollIntervalMs = config?.pollIntervalMs ?? g.pollIntervalMs ?? 300000;
|
|
87
|
+
this.sizeThresholdDocCount = config?.sizeThresholdDocCount ?? g.sizeThresholdDocCount ?? 250000;
|
|
88
|
+
this.growthRateThresholdPct = config?.growthRateThresholdPct ?? g.growthRateThresholdPct ?? 50;
|
|
89
|
+
this.reAlertCooldownMs = config?.reAlertCooldownMs ?? g.reAlertCooldownMs ?? 3600000;
|
|
90
|
+
|
|
91
|
+
this.installed = true;
|
|
92
|
+
this.timer = setInterval((): void => { void this.poll(); }, this.pollIntervalMs);
|
|
93
|
+
if (typeof this.timer.unref === 'function') { this.timer.unref(); }
|
|
94
|
+
|
|
95
|
+
DyFM_Log.info(
|
|
96
|
+
`[DyNTS_CollectionGrowthMonitor] installed — poll ${Math.round(this.pollIntervalMs / 1000)}s, ` +
|
|
97
|
+
`size-threshold ${this.sizeThresholdDocCount} docs, growth-rate ${this.growthRateThresholdPct}%/poll`,
|
|
98
|
+
);
|
|
99
|
+
} catch (err: unknown) {
|
|
100
|
+
DyFM_Log.warn('[DyNTS_CollectionGrowthMonitor] install failed (non-fatal):', err);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Pure decision core (unit-tested): given a fresh count + the previous snapshot, return the alerts
|
|
106
|
+
* to raise. NEVER does I/O. A `size` alert fires when `count > sizeThreshold`; a `growth` alert
|
|
107
|
+
* fires when the collection grew by more than `growthRatePct` since the last snapshot (needs a
|
|
108
|
+
* non-zero previous count).
|
|
109
|
+
*/
|
|
110
|
+
static evaluateGrowth(
|
|
111
|
+
dataName: string,
|
|
112
|
+
count: number,
|
|
113
|
+
last: CollectionSnapshot | undefined,
|
|
114
|
+
sizeThreshold: number,
|
|
115
|
+
growthRatePct: number,
|
|
116
|
+
): DyNTS_CollectionGrowthAlert[] {
|
|
117
|
+
const alerts: DyNTS_CollectionGrowthAlert[] = [];
|
|
118
|
+
|
|
119
|
+
if (count > sizeThreshold) {
|
|
120
|
+
alerts.push({
|
|
121
|
+
kind: 'size',
|
|
122
|
+
dataName: dataName,
|
|
123
|
+
message:
|
|
124
|
+
`Collection "${dataName}" has ${count} documents (> ${sizeThreshold} threshold) — ` +
|
|
125
|
+
`unbounded-growth risk. Consider declaring \`retention\` on this model (FR-258 / SR-1).`,
|
|
126
|
+
detail: { collection: dataName, docCount: count, threshold: sizeThreshold },
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (last && last.docCount > 0) {
|
|
131
|
+
const growthPct: number = ((count - last.docCount) / last.docCount) * 100;
|
|
132
|
+
if (growthPct > growthRatePct) {
|
|
133
|
+
alerts.push({
|
|
134
|
+
kind: 'growth',
|
|
135
|
+
dataName: dataName,
|
|
136
|
+
message:
|
|
137
|
+
`Collection "${dataName}" grew ${Math.round(growthPct)}% since the last poll ` +
|
|
138
|
+
`(${last.docCount} → ${count} docs) — fast unbounded growth.`,
|
|
139
|
+
detail: { collection: dataName, oldCount: last.docCount, newCount: count, growthPct: Math.round(growthPct) },
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return alerts;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* One poll cycle: sample every registered collection's `estimatedDocumentCount()` (scan-free) and
|
|
149
|
+
* raise cooldown-gated warnings. Fully non-fatal — a failure on one collection never aborts the rest.
|
|
150
|
+
*/
|
|
151
|
+
private async poll(): Promise<void> {
|
|
152
|
+
try {
|
|
153
|
+
const services: ReturnType<typeof DyNTS_GlobalService.getAllDBServices> = DyNTS_GlobalService.getAllDBServices();
|
|
154
|
+
const nowMs: number = Date.now();
|
|
155
|
+
|
|
156
|
+
for (const service of services) {
|
|
157
|
+
const dataName: string | undefined = service?.dataParams?.dataName;
|
|
158
|
+
const model: { estimatedDocumentCount?: () => Promise<number> } | undefined =
|
|
159
|
+
service?.dataModel as unknown as { estimatedDocumentCount?: () => Promise<number> };
|
|
160
|
+
if (!dataName || !model || typeof model.estimatedDocumentCount !== 'function') { continue; }
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
const count: number = await model.estimatedDocumentCount();
|
|
164
|
+
const last: CollectionSnapshot | undefined = this.lastSnapshotByCollection.get(dataName);
|
|
165
|
+
|
|
166
|
+
const alerts: DyNTS_CollectionGrowthAlert[] = DyNTS_CollectionGrowthMonitor.evaluateGrowth(
|
|
167
|
+
dataName, count, last, this.sizeThresholdDocCount, this.growthRateThresholdPct,
|
|
168
|
+
);
|
|
169
|
+
for (const alert of alerts) {
|
|
170
|
+
this.emitAlert(alert, nowMs);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
this.lastSnapshotByCollection.set(dataName, { docCount: count, atMs: nowMs });
|
|
174
|
+
} catch (collErr: unknown) {
|
|
175
|
+
DyFM_Log.warn(
|
|
176
|
+
`[DyNTS_CollectionGrowthMonitor] poll for "${dataName}" failed (non-fatal): ` +
|
|
177
|
+
`${collErr instanceof Error ? collErr.message : String(collErr)}`,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
} catch (err: unknown) {
|
|
182
|
+
DyFM_Log.warn('[DyNTS_CollectionGrowthMonitor] poll failed (non-fatal):', err);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Raise one alert to the Errors-sink, gated by the per-(collection,kind) cooldown to avoid flooding. */
|
|
187
|
+
private emitAlert(alert: DyNTS_CollectionGrowthAlert, nowMs: number): void {
|
|
188
|
+
const key: string = `${alert.dataName}:${alert.kind}`;
|
|
189
|
+
const lastAt: number | undefined = this.lastAlertAtByKey.get(key);
|
|
190
|
+
if (lastAt !== undefined && nowMs - lastAt < this.reAlertCooldownMs) { return; }
|
|
191
|
+
this.lastAlertAtByKey.set(key, nowMs);
|
|
192
|
+
|
|
193
|
+
DyFM_Log.warn(`[DyNTS_CollectionGrowthMonitor] ${alert.message}`);
|
|
194
|
+
DyNTS_GlobalService.globalErrorHandler?.(
|
|
195
|
+
new DyFM_Error({
|
|
196
|
+
errorCode: `${DyNTS_global_settings.systemShortCodeName ?? 'DyNTS'}|DyNTS-CGM-${alert.kind === 'size' ? 'SIZE' : 'GROWTH'}`,
|
|
197
|
+
message: alert.message,
|
|
198
|
+
level: DyFM_ErrorLevel.warning,
|
|
199
|
+
additionalContent: alert.detail,
|
|
200
|
+
}),
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Test-only teardown: stop the timer + clear state so specs don't leak between runs. */
|
|
205
|
+
_teardownForTesting(): void {
|
|
206
|
+
if (this.timer) { clearInterval(this.timer); this.timer = null; }
|
|
207
|
+
this.installed = false;
|
|
208
|
+
this.lastSnapshotByCollection.clear();
|
|
209
|
+
this.lastAlertAtByKey.clear();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -55,6 +55,7 @@ import {
|
|
|
55
55
|
import { DyNTS_DBService } from '../base/db.service';
|
|
56
56
|
import { DyNTS_SingletonService } from '../base/singleton.service';
|
|
57
57
|
import { DyNTS_GlobalService } from '../core/global.service';
|
|
58
|
+
import { DyNTS_CollectionGrowthMonitor } from '../core/collection-growth-monitor.service';
|
|
58
59
|
import { DyNTS_MemoryGuard } from '../core/memory-guard.service';
|
|
59
60
|
import { DyNTS_RoutingModule } from '../route/routing-module.service';
|
|
60
61
|
import { DyNTS_getStarRoute } from '../../_collections/star.controller';
|
|
@@ -579,6 +580,17 @@ export abstract class DyNTS_App extends DyNTS_SingletonService {
|
|
|
579
580
|
DyFM_Log.warn('[DyNTS_MemoryGuard] auto-install skipped (non-fatal):', memoryGuardError);
|
|
580
581
|
}
|
|
581
582
|
|
|
583
|
+
// FR-258 / SR-4 — proactive collection-growth monitor (companion to the MemoryGuard). Warns
|
|
584
|
+
// into the Errors-sink when a collection grows unbounded, BEFORE it can be loaded into heap and
|
|
585
|
+
// OOM. Default-on, observation-only, never throws.
|
|
586
|
+
try {
|
|
587
|
+
if (DyNTS_global_settings.collectionGrowthMonitor?.enabled) {
|
|
588
|
+
DyNTS_CollectionGrowthMonitor.getInstance().install();
|
|
589
|
+
}
|
|
590
|
+
} catch (cgmError: unknown) {
|
|
591
|
+
DyFM_Log.warn('[DyNTS_CollectionGrowthMonitor] auto-install skipped (non-fatal):', cgmError);
|
|
592
|
+
}
|
|
593
|
+
|
|
582
594
|
if (!extended) {
|
|
583
595
|
await this.ready();
|
|
584
596
|
|
package/src/index.ts
CHANGED
|
@@ -71,6 +71,7 @@ export * from './_services/core/api.service';
|
|
|
71
71
|
export * from './_services/core/auth.service';
|
|
72
72
|
|
|
73
73
|
export * from './_services/core/email.service';
|
|
74
|
+
export * from './_services/core/collection-growth-monitor.service';
|
|
74
75
|
export * from './_services/core/global.service';
|
|
75
76
|
export * from './_services/core/memory-guard.service';
|
|
76
77
|
|