@eliware/elera-lib 0.1.4 → 0.1.6
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 +19 -2
- package/RELEASE_NOTES.md +44 -0
- package/package.json +4 -2
- package/src/client/create-db.mjs +9 -5
- package/src/client/telemetry-wrapper.mjs +15 -0
- package/src/index.d.ts +10 -4
- package/src/index.mjs +1 -0
- package/src/lifecycle/materializer.mjs +2 -2
- package/src/routing/stream-client.mjs +15 -6
- package/src/telemetry.mjs +9 -0
package/README.md
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
The alternative SQL client for Eliware applications. It provides generic
|
|
4
4
|
primary/balanced MySQL or MariaDB routing without embedding Elera, HAProxy,
|
|
5
|
-
backup, or GitOps policy. It is a v0.1.
|
|
6
|
-
existing package is intentionally unchanged.
|
|
5
|
+
backup, or GitOps policy. It is a v0.1.6 alternative to `@eliware/mysql`; the
|
|
6
|
+
existing package is intentionally unchanged. The current package version is
|
|
7
|
+
0.1.6.
|
|
7
8
|
|
|
8
9
|
`primary` is the preferred connection path. `balanced` is an optional alternate
|
|
9
10
|
path. Both may accept writes; automatic routing sends only conservative,
|
|
@@ -57,6 +58,15 @@ state. Only conservative, single-statement reads are eligible for automatic
|
|
|
57
58
|
retry after a connection failure; uncertain writes are never retried
|
|
58
59
|
automatically.
|
|
59
60
|
|
|
61
|
+
When an attached routing stream receives a `routing.shutdown` event, the
|
|
62
|
+
client drains the identified node, performs a REST bundle resynchronization,
|
|
63
|
+
and closes the retiring WebSocket with restart code `1012`. It then reconnects
|
|
64
|
+
through the configured endpoint, which should normally be the application's
|
|
65
|
+
load-balancer address. Reconnects, failovers, and measured reconnect delay are
|
|
66
|
+
included in telemetry. If the WebSocket remains unavailable, REST
|
|
67
|
+
resynchronization and bounded reconnect backoff continue until the caller
|
|
68
|
+
closes the stream.
|
|
69
|
+
|
|
60
70
|
The public client intentionally exposes SQL operations, health, routing,
|
|
61
71
|
lifecycle, and optional routing-event synchronization methods. REST and
|
|
62
72
|
WebSocket transports are adapters, not supervisor or CLI policy. Underlying
|
|
@@ -68,6 +78,13 @@ schema, account, and grant checks. Neither API transports or orchestrates dump
|
|
|
68
78
|
contents. The stream reports `websocket`, `rest`, or `disconnected` mode so
|
|
69
79
|
callers can observe transport health without implementing transport policy.
|
|
70
80
|
|
|
81
|
+
Applications may opt into generic in-memory telemetry with
|
|
82
|
+
`createDb({ ..., telemetry: true })`. Query counts, failures, retries,
|
|
83
|
+
in-flight work, and latency are exposed through `client.telemetry` and sent
|
|
84
|
+
over an attached routing stream once per second. Telemetry is observational
|
|
85
|
+
only; it does not carry SQL or credentials. Reconnect, failover, and cumulative
|
|
86
|
+
reconnect-delay counters are included in the telemetry snapshot.
|
|
87
|
+
|
|
71
88
|
`createMaterializer` supports bounded plaintext use for a caller-provided
|
|
72
89
|
operation. It creates a mode-restricted temporary file and removes its entire
|
|
73
90
|
temporary directory in a `finally` block; this limits lifetime and cleanup but
|
package/RELEASE_NOTES.md
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
1
|
# Release notes
|
|
2
2
|
|
|
3
|
+
## 0.1.6 — Graceful routing shutdown handoff
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Handles supervisor `routing.shutdown` events without exposing supervisor-
|
|
8
|
+
specific internals to applications.
|
|
9
|
+
- Drains the affected SQL node so in-flight work can finish while new work is
|
|
10
|
+
routed elsewhere.
|
|
11
|
+
- Closes the retiring WebSocket with restart code `1012` and reconnects through
|
|
12
|
+
the configured load-balancer endpoint.
|
|
13
|
+
- Performs an immediate REST routing-bundle resynchronization when the stream
|
|
14
|
+
is being retired or temporarily unavailable.
|
|
15
|
+
- Tracks intentional reconnects, failovers, and reconnect delay in telemetry.
|
|
16
|
+
- Adds regression coverage for shutdown events, close codes, reconnects, REST
|
|
17
|
+
fallback, node draining, and telemetry behavior.
|
|
18
|
+
|
|
19
|
+
### Validation
|
|
20
|
+
|
|
21
|
+
- 24 test suites pass with 116 tests.
|
|
22
|
+
- Syntax and diff validation pass.
|
|
23
|
+
|
|
24
|
+
## 0.1.5 — Telemetry and convention alignment
|
|
25
|
+
|
|
26
|
+
### Added
|
|
27
|
+
|
|
28
|
+
- Adds opt-in generic in-memory client telemetry for query counts, failures,
|
|
29
|
+
retries, in-flight work, and latency.
|
|
30
|
+
- Sends telemetry over an attached routing stream once per second without
|
|
31
|
+
sending SQL text or credentials.
|
|
32
|
+
- Adds public TypeScript declarations and smoke coverage for the telemetry and
|
|
33
|
+
public client surface.
|
|
34
|
+
- Uses Snowflake identifiers for non-security temporary materialization paths.
|
|
35
|
+
|
|
36
|
+
### Refactored
|
|
37
|
+
|
|
38
|
+
- Extracts telemetry timing into a focused client module.
|
|
39
|
+
- Reorganizes `create-db` tests under the mirrored `tests/client/create-db/`
|
|
40
|
+
hierarchy while retaining a small cross-cutting contract test.
|
|
41
|
+
|
|
42
|
+
### Validation
|
|
43
|
+
|
|
44
|
+
- Tests pass with 100% statements, branches, functions, and lines coverage.
|
|
45
|
+
- TypeScript typecheck passes with zero lint warnings.
|
|
46
|
+
|
|
3
47
|
## 0.1.4 — Explicit writer and failover routing
|
|
4
48
|
|
|
5
49
|
This release strengthens generic client-side routing for supervisor-provided
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eliware/elera-lib",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Generic MySQL and MariaDB client with resilient routing, client-side drains, and failover",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"description": "Generic MySQL and MariaDB client with resilient routing, telemetry, client-side drains, and failover",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eliware",
|
|
7
7
|
"elera",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"database",
|
|
12
12
|
"routing",
|
|
13
13
|
"failover",
|
|
14
|
+
"telemetry",
|
|
14
15
|
"websocket",
|
|
15
16
|
"connection-pool"
|
|
16
17
|
],
|
|
@@ -53,6 +54,7 @@
|
|
|
53
54
|
},
|
|
54
55
|
"dependencies": {
|
|
55
56
|
"@eliware/common": "^2.0.0",
|
|
57
|
+
"@eliware/snowflake": "^2.0.0",
|
|
56
58
|
"mysql2": "^3.24.2"
|
|
57
59
|
},
|
|
58
60
|
"devDependencies": {
|
package/src/client/create-db.mjs
CHANGED
|
@@ -8,10 +8,12 @@ import { createRouteFactory } from './route-factory.mjs';
|
|
|
8
8
|
import { classifyQuery, routeFor } from '../routing.mjs';
|
|
9
9
|
import { compareBundleVersions } from '../routing/bundle-version.mjs';
|
|
10
10
|
import { clientDrainTimeout } from '../lifecycle/drain-policy.mjs';
|
|
11
|
+
import { createTelemetry } from '../telemetry.mjs';
|
|
12
|
+
import { createTimedOperation } from './telemetry-wrapper.mjs';
|
|
11
13
|
|
|
12
14
|
const olderVersion = (candidate, current) => compareBundleVersions(candidate, current) < 0;
|
|
13
15
|
|
|
14
|
-
export async function createDb({ primary, balanced, bundle, credentialProvider, mysqlLib = mysql, log = defaultLog, routing = 'auto', identity, quarantineMs = 5000, drainTimeoutMs = 45000, now = () => Date.now() } = {}) {
|
|
16
|
+
export async function createDb({ primary, balanced, bundle, credentialProvider, mysqlLib = mysql, log = defaultLog, routing = 'auto', identity, quarantineMs = 5000, drainTimeoutMs = 45000, now = () => Date.now(), telemetry } = {}) {
|
|
15
17
|
if (!primary || typeof primary !== 'object') throw new TypeError('primary connection profile is required');
|
|
16
18
|
const credentials = await resolveCredentials(credentialProvider, credentialContext(primary, { identity }));
|
|
17
19
|
let primaryConfig = validateProfile({ ...primary, ...credentials }, 'primary');
|
|
@@ -26,20 +28,22 @@ export async function createDb({ primary, balanced, bundle, credentialProvider,
|
|
|
26
28
|
let primaryPool = makeRoute('primary', primaryConfig);
|
|
27
29
|
let balancedPool = balancedConfig || activeBundle?.routes?.balanced ? makeRoute('balanced', balancedConfig ?? primaryConfig) : null;
|
|
28
30
|
const choose = (sql, options = {}) => options.connection ?? (routeFor(sql, options.route ?? routing) === 'balanced' && balancedPool ? balancedPool : primaryPool);
|
|
31
|
+
const metrics = telemetry === true ? createTelemetry({ application: bundle?.application ?? identity ?? 'default', now }) : telemetry;
|
|
32
|
+
const timed = createTimedOperation({ metrics, now });
|
|
29
33
|
const client = {
|
|
30
|
-
async query(sql, values, options) { const selected = choose(sql, options); try { return await selected.query(sql, values); } catch (error) { const requestedRoute = options?.route ?? routing; if (error.retryable && balancedPool && routeFor(sql, requestedRoute) === 'balanced' && classifyQuery(sql) === 'balanced') return balancedPool.query(sql, values); throw error; } },
|
|
34
|
+
async query(sql, values, options) { return timed(async () => { const selected = choose(sql, options); try { return await selected.query(sql, values); } catch (error) { const requestedRoute = options?.route ?? routing; if (error.retryable && balancedPool && routeFor(sql, requestedRoute) === 'balanced' && classifyQuery(sql) === 'balanced') { metrics?.record?.({ retry: true }); return balancedPool.query(sql, values); } throw error; } }); },
|
|
31
35
|
async execute(sql, values, options) { return choose(sql, options).execute(sql, values); },
|
|
32
36
|
async transaction(callback) { const node = primaryPool.choose(); const connection = await node.getConnection(); try { await connection.beginTransaction(); const tx = { query: (sql, values) => connection.query(sql, values), execute: (sql, values) => connection.execute(sql, values) }; const result = await callback(tx); await connection.commit(); return result; } catch (error) { await connection.rollback().catch(() => {}); throw asSqlError(error); } finally { connection.release(); } },
|
|
33
37
|
async health(route = 'primary') { const started = now(); const selected = route === 'balanced' && balancedPool ? balancedPool : primaryPool; const nodes = await selected.health(); return { ok: nodes.some((node) => node.ok), route: selected === balancedPool ? 'balanced' : 'primary', nodes, latencyMs: now() - started }; },
|
|
34
38
|
async refresh(nextBundle) { const candidate = validateBundle(nextBundle); if (bundleExpired(candidate, now())) throw new Error('routing bundle is expired'); if (olderVersion(candidate.bundleVersion, activeBundle?.bundleVersion)) return { bundleVersion: activeBundle?.bundleVersion, refreshRequired: bundleNeedsRefresh(activeBundle, now()) }; const previous = [primaryPool, balancedPool]; const credentials = candidate.credentials ?? { username: primaryConfig.user, password: primaryConfig.password }; const writer = candidate.writer ?? candidate.routes.primary?.[0]; const reader = candidate.readers?.[0] ?? candidate.routes.balanced?.[0]; primaryConfig = validateProfile({ ...primaryConfig, host: writer?.host, port: writer?.port, user: credentials.username, password: credentials.password, database: candidate.database }, 'primary'); balancedConfig = reader ? validateProfile({ ...primaryConfig, host: reader.host, port: reader.port }, 'balanced') : undefined; activeBundle = candidate; primaryPool = makeRoute('primary', primaryConfig); balancedPool = balancedConfig ? makeRoute('balanced', balancedConfig) : null; await Promise.all(previous.filter(Boolean).map((pool) => pool.close())); return { bundleVersion: activeBundle.bundleVersion ?? null, refreshRequired: bundleNeedsRefresh(activeBundle, now()) }; },
|
|
35
|
-
async attachRoutingStream(stream) { if (!stream?.connect) throw new TypeError('routing stream is required'); stream.setOnUpdate?.(async (event) => { const update = event.type === 'routing.update' ? event : event.type === 'routing.resync' ? event.bundle : undefined; if (update && (update.writer || update.routes?.primary?.length)) await client.refresh({ ...activeBundle, ...update, database: update.database ?? activeBundle?.database ?? primaryConfig.database, credentials: update.credentials ?? activeBundle?.credentials, routes: update.routes ?? activeBundle?.routes, bundleVersion: update.bundleVersion ?? update.version ?? activeBundle?.bundleVersion, expiresAt: update.expiresAt ?? activeBundle?.expiresAt ?? new Date(now() + 60000).toISOString() }); if (event.type === 'routing.drain' || event.type === 'routing.
|
|
39
|
+
async attachRoutingStream(stream) { if (!stream?.connect) throw new TypeError('routing stream is required'); metrics?.start?.(stream); stream.setTelemetry?.(metrics); stream.setOnUpdate?.(async (event) => { const update = event.type === 'routing.update' ? event : event.type === 'routing.resync' ? event.bundle : undefined; if (update && (update.writer || update.routes?.primary?.length)) await client.refresh({ ...activeBundle, ...update, database: update.database ?? activeBundle?.database ?? primaryConfig.database, credentials: update.credentials ?? activeBundle?.credentials, routes: update.routes ?? activeBundle?.routes, bundleVersion: update.bundleVersion ?? update.version ?? activeBundle?.bundleVersion, expiresAt: update.expiresAt ?? activeBundle?.expiresAt ?? new Date(now() + 60000).toISOString() }); if (event.type === 'routing.drain' || event.type === 'routing.shutdown') for (const pool of [primaryPool, balancedPool].filter(Boolean)) (event.type === 'routing.drain' ? pool.drain : pool.drain)(event.node, drainTimeoutMs); if (event.type === 'routing.recovery') for (const pool of [primaryPool, balancedPool].filter(Boolean)) pool.recover(event.node, drainTimeoutMs); }); await stream.connect(); return () => stream.close?.(); },
|
|
36
40
|
drain(host, timeoutMs = drainTimeoutMs) { const effectiveTimeout = clientDrainTimeout(timeoutMs); const pools = [primaryPool, balancedPool].filter(Boolean); pools.forEach((pool) => pool.drain(host, effectiveTimeout)); return { host, timeoutMs: effectiveTimeout, wait: () => Promise.all(pools.map((pool) => pool.waitForIdle(effectiveTimeout))), forceClose: () => Promise.all(pools.map((pool) => pool.forceClose(host))) }; },
|
|
37
41
|
nodeStates() { return [primaryPool, balancedPool].filter(Boolean).flatMap((pool) => pool.nodes.map((node) => ({ host: node.host, port: node.port, route: pool === primaryPool ? 'primary' : 'balanced', state: node.state, active: node.active, available: node.available }))); },
|
|
38
42
|
setNodeAvailability(route, host, available) { const pool = route === 'balanced' ? balancedPool : primaryPool; pool?.setAvailability(host, available); },
|
|
39
43
|
bundle: () => activeBundle,
|
|
40
|
-
async close() { await Promise.all([primaryPool.close(), balancedPool?.close()]); },
|
|
44
|
+
async close() { metrics?.stop?.(); await Promise.all([primaryPool.close(), balancedPool?.close()]); },
|
|
41
45
|
classify: classifyQuery,
|
|
42
|
-
config: { primary: redactedProfile(primaryConfig), balanced: balancedConfig && redactedProfile(balancedConfig) }
|
|
46
|
+
telemetry: metrics, config: { primary: redactedProfile(primaryConfig), balanced: balancedConfig && redactedProfile(balancedConfig) }
|
|
43
47
|
};
|
|
44
48
|
log.debug?.('SQL client created', { balanced: Boolean(balancedPool), routing });
|
|
45
49
|
return client;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const emptyMetrics = { begin: () => undefined, record: () => {} };
|
|
2
|
+
|
|
3
|
+
export function createTimedOperation({ metrics = emptyMetrics, now = () => Date.now() } = {}) {
|
|
4
|
+
return async function timed(operation) {
|
|
5
|
+
const started = metrics.begin();
|
|
6
|
+
try {
|
|
7
|
+
const result = await operation();
|
|
8
|
+
metrics.record({ latencyMs: started === undefined ? 0 : now() - started });
|
|
9
|
+
return result;
|
|
10
|
+
} catch (error) {
|
|
11
|
+
metrics.record({ latencyMs: started === undefined ? 0 : now() - started, failed: true });
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -28,12 +28,14 @@ export interface CredentialProviderResult { user: string; password: string; }
|
|
|
28
28
|
export type CredentialProvider = (context: { database: string; identity: string | null; route: string }) => Promise<CredentialProviderResult> | CredentialProviderResult;
|
|
29
29
|
export type QueryFunction = (sql: string, values?: unknown) => Promise<any>;
|
|
30
30
|
export interface DbOptions { route?: 'auto' | 'primary' | 'balanced'; connection?: unknown; }
|
|
31
|
+
export interface TelemetrySnapshot { type: 'client.telemetry'; application: string; queries: number; failures: number; retries: number; reconnects: number; failoverCount: number; reconnectDelayMs: number; inflight: number; totalLatencyMs: number; maxLatencyMs: number; avgLatencyMs: number; sentAt: string; }
|
|
32
|
+
export interface Telemetry { begin(): number; record(event?: { latencyMs?: number; failed?: boolean; retry?: boolean; reconnect?: boolean; failover?: boolean }): void; recordReconnect(event?: { delayMs?: number; failover?: boolean }): void; snapshot(): TelemetrySnapshot; start(stream: Pick<RoutingStream, 'sendTelemetry'>): void; stop(): void; }
|
|
31
33
|
|
|
32
34
|
export interface DbClient {
|
|
33
35
|
query(sql: string, values?: unknown, options?: DbOptions): Promise<unknown>;
|
|
34
36
|
execute(sql: string, values?: unknown, options?: DbOptions): Promise<unknown>;
|
|
35
37
|
transaction<T>(callback: (transaction: Pick<DbClient, 'query' | 'execute'>) => Promise<T>): Promise<T>;
|
|
36
|
-
health(route?: 'primary' | 'balanced'): Promise<{ ok: boolean; route: string; latencyMs: number }>;
|
|
38
|
+
health(route?: 'primary' | 'balanced'): Promise<{ ok: boolean; route: string; latencyMs: number; telemetry?: TelemetrySnapshot }>;
|
|
37
39
|
close(): Promise<void>;
|
|
38
40
|
refresh(bundle: RoutingBundle): Promise<{ bundleVersion: number | string | null; refreshRequired: boolean }>;
|
|
39
41
|
bundle(): RoutingBundle | undefined;
|
|
@@ -43,17 +45,20 @@ export interface DbClient {
|
|
|
43
45
|
drain(host: string, timeoutMs?: number): { host: string; timeoutMs: number; wait(): Promise<unknown[]>; forceClose(): Promise<unknown[]> };
|
|
44
46
|
nodeStates(): Array<{ host: string; port: number; route: 'primary' | 'balanced'; state: 'ready' | 'draining' | 'unavailable' | 'recovering'; active: number; available: boolean }>;
|
|
45
47
|
config: { primary: ConnectionProfile; balanced?: ConnectionProfile };
|
|
48
|
+
telemetry?: Telemetry;
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
export interface RoutingStream {
|
|
49
52
|
connect(): Promise<void>;
|
|
50
53
|
setOnUpdate(handler: (event: unknown) => void | Promise<void>): void;
|
|
51
54
|
close(): void;
|
|
55
|
+
sendTelemetry(payload: unknown): void;
|
|
56
|
+
setTelemetry?(telemetry?: Pick<Telemetry, 'recordReconnect'>): void;
|
|
52
57
|
state(): { connected: boolean; mode: 'websocket' | 'rest' | 'disconnected'; expectedVersion: number | string };
|
|
53
58
|
}
|
|
54
59
|
|
|
55
|
-
export function createDb(options: { primary: ConnectionProfile; balanced?: Partial<ConnectionProfile>; bundle?: RoutingBundle; credentialProvider?: CredentialProvider; identity?: string; mysqlLib?: unknown; log?: unknown; routing?: 'auto' | 'primary' | 'balanced'; quarantineMs?: number; drainTimeoutMs?: number; now?: () => number }): Promise<DbClient>;
|
|
56
|
-
export function createDbFromEnvironment(options?: { env?: Record<string, string | undefined>; mysqlLib?: unknown; log?: unknown; routing?: 'auto' | 'primary' | 'balanced'; bundle?: RoutingBundle; credentialProvider?: CredentialProvider; identity?: string }): Promise<DbClient>;
|
|
60
|
+
export function createDb(options: { primary: ConnectionProfile; balanced?: Partial<ConnectionProfile>; bundle?: RoutingBundle; credentialProvider?: CredentialProvider; identity?: string; mysqlLib?: unknown; log?: unknown; routing?: 'auto' | 'primary' | 'balanced'; quarantineMs?: number; drainTimeoutMs?: number; now?: () => number; telemetry?: true | Telemetry }): Promise<DbClient>;
|
|
61
|
+
export function createDbFromEnvironment(options?: { env?: Record<string, string | undefined>; mysqlLib?: unknown; log?: unknown; routing?: 'auto' | 'primary' | 'balanced'; bundle?: RoutingBundle; credentialProvider?: CredentialProvider; identity?: string; telemetry?: true | Telemetry }): Promise<DbClient>;
|
|
57
62
|
export function classifyQuery(sql: unknown): 'primary' | 'balanced';
|
|
58
63
|
export function routeFor(sql: unknown, requested?: 'auto' | 'primary' | 'balanced'): 'primary' | 'balanced';
|
|
59
64
|
export function validateProfile(profile: ConnectionProfile, name?: string): ConnectionProfile;
|
|
@@ -67,7 +72,7 @@ export function bundleNeedsRefresh(bundle: RoutingBundle, now?: number): boolean
|
|
|
67
72
|
export function createAdminSql(options: { query: QueryFunction }): { transaction<T>(work: (context: { query: QueryFunction }) => Promise<T>): Promise<T>; migration(statements?: string[]): Promise<unknown> };
|
|
68
73
|
export function createMigrationRunner(options: { query: QueryFunction; migrations?: Array<{ version: number; name: string; statements: string[] }> }): { status(): Promise<{ applied: unknown[] }>; migrate(): Promise<unknown> };
|
|
69
74
|
export function selectRouteNodes(options: { bundle: RoutingBundle; route?: 'primary' | 'balanced'; now?: number }): RoutingNode[];
|
|
70
|
-
export function createRoutingStream(options: { endpoint: string; token?: string; application?: string; fetchBundle: (application: string) => Promise<RoutingBundle>; onUpdate?: (event: unknown) => void; onError?: (error: unknown) => void; reconnectMs?: number; maxReconnectMs?: number; heartbeatMs?: number }): RoutingStream;
|
|
75
|
+
export function createRoutingStream(options: { endpoint: string; token?: string; application?: string; fetchBundle: (application: string) => Promise<RoutingBundle>; onUpdate?: (event: unknown) => void; onError?: (error: unknown) => void; reconnectMs?: number; maxReconnectMs?: number; heartbeatMs?: number; telemetry?: Pick<Telemetry, 'recordReconnect'> }): RoutingStream;
|
|
71
76
|
export function writerAssignment(bundle: RoutingBundle): WriterAssignment;
|
|
72
77
|
export function failoverNodes(bundle: RoutingBundle): WriterAssignment[];
|
|
73
78
|
export function compareBundleVersions(left: number | string | undefined, right: number | string | undefined): number;
|
|
@@ -76,3 +81,4 @@ export function clientDrainTimeout(timeoutMs?: number): number;
|
|
|
76
81
|
export function createQuiesceController(options?: { close?: () => Promise<void>; onChange?: (state: string) => void }): { state(): { state: string; active: number }; enter(): () => void; begin(): Promise<void>; end(): Promise<void>; close(): Promise<void> };
|
|
77
82
|
export function createSqlVerifier(options: { query: QueryFunction }): { connectivity(): Promise<{ verified: boolean }>; schema(database: string): Promise<{ database: string; verified: boolean }>; account(user: string, host?: string): Promise<{ user: string; host: string; verified: boolean; grants: string[] }>; all(options?: { database?: string; user?: string; host?: string }): Promise<unknown> };
|
|
78
83
|
export function createMaterializer(options?: Record<string, unknown>): unknown;
|
|
84
|
+
export function createTelemetry(options?: { application?: string; intervalMs?: number; now?: () => number; setIntervalImpl?: typeof setInterval; clearIntervalImpl?: typeof clearInterval }): Telemetry;
|
package/src/index.mjs
CHANGED
|
@@ -15,3 +15,4 @@ export { CLIENT_DRAIN_TIMEOUT_MS, clientDrainTimeout } from './lifecycle/drain-p
|
|
|
15
15
|
export { createQuiesceController } from './lifecycle/quiesce.mjs';
|
|
16
16
|
export { createSqlVerifier } from './verification/sql.mjs';
|
|
17
17
|
export { createMaterializer } from './lifecycle/materializer.mjs';
|
|
18
|
+
export { createTelemetry } from './telemetry.mjs';
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { generate as generateSnowflake } from "@eliware/snowflake";
|
|
2
2
|
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
|
|
6
|
-
export function createMaterializer({ makeTemp = mkdtemp, write = writeFile, remove = rm, id =
|
|
6
|
+
export function createMaterializer({ makeTemp = mkdtemp, write = writeFile, remove = rm, id = generateSnowflake } = {}) {
|
|
7
7
|
return {
|
|
8
8
|
async withFile(content, operation) {
|
|
9
9
|
if (typeof operation !== "function") throw new TypeError("materializer operation is required");
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { log as defaultLog } from '@eliware/common';
|
|
2
2
|
import { compareBundleVersions } from './bundle-version.mjs';
|
|
3
3
|
|
|
4
|
-
export function createRoutingStream({ endpoint, token, application = 'default', fetchBundle, WebSocketImpl = globalThis.WebSocket, onUpdate, onError, reconnectMs = 1000, maxReconnectMs = 30000, heartbeatMs = 45000, now = () => Date.now() } = {}) {
|
|
4
|
+
export function createRoutingStream({ endpoint, token, application = 'default', fetchBundle, WebSocketImpl = globalThis.WebSocket, onUpdate, onError, reconnectMs = 1000, maxReconnectMs = 30000, heartbeatMs = 45000, now = () => Date.now(), telemetry } = {}) {
|
|
5
5
|
if (!endpoint || typeof fetchBundle !== 'function') throw new TypeError('endpoint and fetchBundle are required');
|
|
6
|
-
let socket; let closed = false; let timer; let heartbeat; let expectedVersion = 0; let delay = reconnectMs; let updateHandler = onUpdate; let mode = 'disconnected';
|
|
6
|
+
let socket; let closed = false; let timer; let heartbeat; let expectedVersion = 0; let delay = reconnectMs; let updateHandler = onUpdate; let mode = 'disconnected'; let plannedReconnect = false; let lastReconnectWasPlanned = false; let disconnectedAt;
|
|
7
7
|
const log = arguments[0]?.log ?? defaultLog;
|
|
8
8
|
const streamUrl = () => `${endpoint.replace(/^http/i, 'ws').replace(/\/$/, '')}/api/v1/routing/stream?application=${encodeURIComponent(application)}&token=${encodeURIComponent(token ?? '')}`;
|
|
9
9
|
async function fallback() { try { const bundle = await fetchBundle(application); if (closed) return; mode = 'rest'; updateHandler?.({ type: 'routing.resync', version: expectedVersion, bundle, receivedAt: now() }); } catch (error) { if (closed) return; mode = 'disconnected'; onError?.(error); log.warn?.('Routing REST fallback failed', { error }); } }
|
|
@@ -12,10 +12,19 @@ export function createRoutingStream({ endpoint, token, application = 'default',
|
|
|
12
12
|
if (closed || typeof WebSocketImpl !== 'function') { await fallback(); schedule(); return; }
|
|
13
13
|
try {
|
|
14
14
|
socket = new WebSocketImpl(streamUrl());
|
|
15
|
-
socket.onopen = () => { mode = 'websocket'; delay = reconnectMs; heartbeat = setInterval(() => socket?.send?.(JSON.stringify({ type: 'heartbeat', sentAt: now() })), heartbeatMs); };
|
|
15
|
+
socket.onopen = () => { mode = 'websocket'; if (disconnectedAt !== undefined) { telemetry?.recordReconnect?.({ delayMs: Math.max(0, now() - disconnectedAt), failover: lastReconnectWasPlanned }); disconnectedAt = undefined; lastReconnectWasPlanned = false; } delay = reconnectMs; heartbeat = setInterval(() => socket?.send?.(JSON.stringify({ type: 'heartbeat', sentAt: now() })), heartbeatMs); };
|
|
16
16
|
socket.onmessage = async ({ data }) => {
|
|
17
17
|
try {
|
|
18
|
-
const event = JSON.parse(data);
|
|
18
|
+
const event = JSON.parse(data);
|
|
19
|
+
if (event.type === 'routing.shutdown') {
|
|
20
|
+
updateHandler?.(event);
|
|
21
|
+
plannedReconnect = true;
|
|
22
|
+
delay = reconnectMs;
|
|
23
|
+
await fallback();
|
|
24
|
+
socket?.close?.(1012, 'supervisor restarting');
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const version = event.version;
|
|
19
28
|
if (version !== undefined && expectedVersion !== 0 && compareBundleVersions(version, expectedVersion) <= 0) return;
|
|
20
29
|
const numericVersion = Number(version); const numericExpected = Number(expectedVersion);
|
|
21
30
|
if (Number.isInteger(numericVersion) && Number.isInteger(numericExpected) && numericExpected > 0 && numericVersion > numericExpected + 1) await fallback();
|
|
@@ -24,8 +33,8 @@ export function createRoutingStream({ endpoint, token, application = 'default',
|
|
|
24
33
|
} catch (error) { onError?.(error); }
|
|
25
34
|
};
|
|
26
35
|
socket.onerror = (error) => { onError?.(error); };
|
|
27
|
-
socket.onclose = () => { clearInterval(heartbeat); heartbeat = undefined; socket = undefined; mode = 'disconnected'; if (!closed) { void fallback(); schedule(); } };
|
|
36
|
+
socket.onclose = () => { clearInterval(heartbeat); heartbeat = undefined; socket = undefined; mode = 'disconnected'; if (!closed) { disconnectedAt = now(); lastReconnectWasPlanned = plannedReconnect; if (!plannedReconnect) void fallback(); plannedReconnect = false; schedule(); } };
|
|
28
37
|
} catch (error) { mode = 'disconnected'; onError?.(error); await fallback(); schedule(); }
|
|
29
38
|
}
|
|
30
|
-
return { connect, setOnUpdate: (handler) => { updateHandler = handler; }, close: () => { closed = true; mode = 'disconnected'; clearTimeout(timer); clearInterval(heartbeat); socket?.close?.(); }, state: () => ({ connected: socket?.readyState === 1, mode, expectedVersion }) };
|
|
39
|
+
return { connect, sendTelemetry: (payload) => { if (socket?.readyState === 1) socket.send(JSON.stringify(payload)); }, setOnUpdate: (handler) => { updateHandler = handler; }, setTelemetry: (value) => { telemetry = value; }, close: () => { closed = true; mode = 'disconnected'; clearTimeout(timer); clearInterval(heartbeat); socket?.close?.(); }, state: () => ({ connected: socket?.readyState === 1, mode, expectedVersion }) };
|
|
31
40
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function createTelemetry({ application = 'default', intervalMs = 1000, now = () => Date.now(), setIntervalImpl = setInterval, clearIntervalImpl = clearInterval } = {}) {
|
|
2
|
+
const stats = { queries: 0, failures: 0, retries: 0, reconnects: 0, failoverCount: 0, reconnectDelayMs: 0, inflight: 0, totalLatencyMs: 0, maxLatencyMs: 0 };
|
|
3
|
+
let timer;
|
|
4
|
+
const begin = () => { stats.inflight += 1; return now(); };
|
|
5
|
+
const record = ({ latencyMs = 0, failed = false, retry = false, reconnect = false, failover = false } = {}) => { stats.queries += 1; stats.inflight = Math.max(0, stats.inflight - 1); stats.totalLatencyMs += latencyMs; stats.maxLatencyMs = Math.max(stats.maxLatencyMs, latencyMs); if (failed) stats.failures += 1; if (retry) stats.retries += 1; if (reconnect) stats.reconnects += 1; if (failover) stats.failoverCount += 1; };
|
|
6
|
+
const snapshot = () => ({ type: 'client.telemetry', application, ...stats, avgLatencyMs: stats.queries ? stats.totalLatencyMs / stats.queries : 0, sentAt: new Date(now()).toISOString() });
|
|
7
|
+
const recordReconnect = ({ delayMs = 0, failover = false } = {}) => { stats.reconnects += 1; stats.reconnectDelayMs += Math.max(0, Number(delayMs) || 0); if (failover) stats.failoverCount += 1; };
|
|
8
|
+
return { begin, record, recordReconnect, snapshot, start(stream) { if (timer) return; timer = setIntervalImpl(() => stream.sendTelemetry?.(snapshot()), intervalMs); timer.unref?.(); }, stop() { if (timer) clearIntervalImpl(timer); timer = undefined; } };
|
|
9
|
+
}
|