@eliware/elera-lib 0.1.7 → 0.1.9
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 +16 -7
- package/RELEASE_NOTES.md +29 -0
- package/package.json +1 -1
- package/src/client/create-db.mjs +4 -3
- package/src/errors.mjs +14 -0
- package/src/index.d.ts +3 -0
- package/src/index.mjs +1 -1
- package/src/pools/route-pool.mjs +4 -2
package/README.md
CHANGED
|
@@ -2,9 +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.
|
|
5
|
+
backup, or GitOps policy. It is a v0.1.9 alternative to `@eliware/mysql`; the
|
|
6
6
|
existing package is intentionally unchanged. The current package version is
|
|
7
|
-
0.1.
|
|
7
|
+
0.1.9.
|
|
8
8
|
|
|
9
9
|
`primary` is the preferred connection path. `balanced` is an optional alternate
|
|
10
10
|
path. Both may accept writes; automatic routing sends only conservative,
|
|
@@ -58,6 +58,14 @@ state. Only conservative, single-statement reads are eligible for automatic
|
|
|
58
58
|
retry after a connection failure; uncertain writes are never retried
|
|
59
59
|
automatically.
|
|
60
60
|
|
|
61
|
+
`client.availability()` reports whether a primary route is usable. It returns
|
|
62
|
+
`state: 'cluster-unavailable'` when every primary candidate is draining or
|
|
63
|
+
unavailable; the `routes` fields report primary and balanced availability
|
|
64
|
+
independently. A single-node route fails with the exported
|
|
65
|
+
`ServerUnavailableError` using code `SERVER_UNAVAILABLE`; a multi-node route
|
|
66
|
+
with no eligible candidates fails with `ClusterUnavailableError` using code
|
|
67
|
+
`CLUSTER_UNAVAILABLE`.
|
|
68
|
+
|
|
61
69
|
When an attached routing stream receives a `routing.shutdown` event, the
|
|
62
70
|
client drains the identified node, performs a REST bundle resynchronization,
|
|
63
71
|
and closes the retiring WebSocket with restart code `1012`. If the event
|
|
@@ -88,11 +96,12 @@ contents. The stream reports `websocket`, `rest`, or `disconnected` mode so
|
|
|
88
96
|
callers can observe transport health without implementing transport policy.
|
|
89
97
|
|
|
90
98
|
Applications may opt into generic in-memory telemetry with
|
|
91
|
-
`createDb({ ..., telemetry: true })`. Query
|
|
92
|
-
in-flight work, and latency are exposed through
|
|
93
|
-
over an attached routing stream once per second.
|
|
94
|
-
only; it does not carry SQL or credentials.
|
|
95
|
-
reconnect-delay counters are included in
|
|
99
|
+
`createDb({ ..., telemetry: true })`. Query, execute, and transaction counts,
|
|
100
|
+
failures, retries, in-flight work, and latency are exposed through
|
|
101
|
+
`client.telemetry` and sent over an attached routing stream once per second.
|
|
102
|
+
Telemetry is observational only; it does not carry SQL or credentials.
|
|
103
|
+
Reconnect, failover, and cumulative reconnect-delay counters are included in
|
|
104
|
+
the telemetry snapshot.
|
|
96
105
|
|
|
97
106
|
`createMaterializer` supports bounded plaintext use for a caller-provided
|
|
98
107
|
operation. It creates a mode-restricted temporary file and removes its entire
|
package/RELEASE_NOTES.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
# Release notes
|
|
2
2
|
|
|
3
|
+
## 0.1.9 — Standalone outage classification and complete operation telemetry
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Exposes `ServerUnavailableError` with the stable `SERVER_UNAVAILABLE` code when a standalone SQL route has no eligible server.
|
|
8
|
+
- Keeps multi-node route exhaustion classified as `CLUSTER_UNAVAILABLE`.
|
|
9
|
+
- Records telemetry for `execute()` and transaction operations alongside query latency and failure metrics.
|
|
10
|
+
|
|
11
|
+
### Validation
|
|
12
|
+
|
|
13
|
+
- Adds regression coverage for standalone drain classification and operation telemetry.
|
|
14
|
+
- Maintains 100% statements, branches, functions, and lines coverage with zero lint warnings.
|
|
15
|
+
|
|
16
|
+
## 0.1.8 — Explicit outage state and recovery observability
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
|
|
20
|
+
- Exposes `ClusterUnavailableError` with the stable `CLUSTER_UNAVAILABLE` code
|
|
21
|
+
when no eligible SQL node can serve a route.
|
|
22
|
+
- Adds `DbClient.availability()` so applications can distinguish an available
|
|
23
|
+
client from a fully unavailable primary route, including per-route status.
|
|
24
|
+
- Covers standalone node drain, total primary-route outage, and route recovery.
|
|
25
|
+
- Updates the TypeScript declarations for the new runtime API and error type.
|
|
26
|
+
|
|
27
|
+
### Validation
|
|
28
|
+
|
|
29
|
+
- Maintains 100% statements, branches, functions, and lines coverage.
|
|
30
|
+
- Tests and TypeScript typechecking pass with zero lint warnings.
|
|
31
|
+
|
|
3
32
|
## 0.1.7 — Routing shutdown contract completion
|
|
4
33
|
|
|
5
34
|
### Added
|
package/package.json
CHANGED
package/src/client/create-db.mjs
CHANGED
|
@@ -32,12 +32,13 @@ export async function createDb({ primary, balanced, bundle, credentialProvider,
|
|
|
32
32
|
const timed = createTimedOperation({ metrics, now });
|
|
33
33
|
const client = {
|
|
34
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; } }); },
|
|
35
|
-
async execute(sql, values, options) { return choose(sql, options).execute(sql, values); },
|
|
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(); } },
|
|
35
|
+
async execute(sql, values, options) { return timed(async () => choose(sql, options).execute(sql, values)); },
|
|
36
|
+
async transaction(callback) { return timed(async () => { 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(); } }); },
|
|
37
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 }; },
|
|
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
|
|
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()) }; },
|
|
39
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?.(); },
|
|
40
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))) }; },
|
|
41
|
+
availability() { const states = this.nodeStates(); const primaryAvailable = states.some((node) => node.route === 'primary' && node.available); return { state: primaryAvailable ? 'available' : 'cluster-unavailable', routes: { primary: primaryAvailable, balanced: states.some((node) => node.route === 'balanced' && node.available) } }; },
|
|
41
42
|
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 }))); },
|
|
42
43
|
setNodeAvailability(route, host, available) { const pool = route === 'balanced' ? balancedPool : primaryPool; pool?.setAvailability(host, available); },
|
|
43
44
|
bundle: () => activeBundle,
|
package/src/errors.mjs
CHANGED
|
@@ -4,6 +4,20 @@ export class SqlClientError extends Error {
|
|
|
4
4
|
}
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
export class ClusterUnavailableError extends SqlClientError {
|
|
8
|
+
constructor(message = 'No eligible SQL nodes are available', options = {}) {
|
|
9
|
+
super(message, { code: 'CLUSTER_UNAVAILABLE', retryable: false, ...options });
|
|
10
|
+
this.name = 'ClusterUnavailableError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class ServerUnavailableError extends SqlClientError {
|
|
15
|
+
constructor(message = 'The SQL server is unavailable', options = {}) {
|
|
16
|
+
super(message, { code: 'SERVER_UNAVAILABLE', retryable: false, ...options });
|
|
17
|
+
this.name = 'ServerUnavailableError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
7
21
|
export function classifyError(error) {
|
|
8
22
|
const code = error?.code;
|
|
9
23
|
if (['ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT', 'PROTOCOL_CONNECTION_LOST', 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR'].includes(code)) return { code: 'CONNECTION_ERROR', retryable: true };
|
package/src/index.d.ts
CHANGED
|
@@ -42,6 +42,7 @@ export interface DbClient {
|
|
|
42
42
|
classify(sql: string): 'primary' | 'balanced';
|
|
43
43
|
attachRoutingStream(stream: RoutingStream): Promise<() => void>;
|
|
44
44
|
setNodeAvailability(route: 'primary' | 'balanced', host: string, available: boolean): void;
|
|
45
|
+
availability(): { state: 'available' | 'cluster-unavailable'; routes: { primary: boolean; balanced: boolean } };
|
|
45
46
|
drain(host: string, timeoutMs?: number): { host: string; timeoutMs: number; wait(): Promise<unknown[]>; forceClose(): Promise<unknown[]> };
|
|
46
47
|
nodeStates(): Array<{ host: string; port: number; route: 'primary' | 'balanced'; state: 'ready' | 'draining' | 'unavailable' | 'recovering'; active: number; available: boolean }>;
|
|
47
48
|
config: { primary: ConnectionProfile; balanced?: ConnectionProfile };
|
|
@@ -67,6 +68,8 @@ export function routeFor(sql: unknown, requested?: 'auto' | 'primary' | 'balance
|
|
|
67
68
|
export function validateProfile(profile: ConnectionProfile, name?: string): ConnectionProfile;
|
|
68
69
|
export function redactedProfile(profile: ConnectionProfile): ConnectionProfile;
|
|
69
70
|
export class SqlClientError extends Error { code?: string; retryable?: boolean; cause?: unknown; }
|
|
71
|
+
export class ClusterUnavailableError extends SqlClientError {}
|
|
72
|
+
export class ServerUnavailableError extends SqlClientError {}
|
|
70
73
|
export function classifyError(error: unknown): { retryable: boolean; code?: string };
|
|
71
74
|
export function asSqlError(error: unknown): SqlClientError;
|
|
72
75
|
export function validateBundle(bundle: RoutingBundle): RoutingBundle;
|
package/src/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ export { createDbFromBundle, profilesFromBundle } from './client/from-bundle.mjs
|
|
|
3
3
|
export { createDbFromEnvironment } from './client/environment.mjs';
|
|
4
4
|
export { classifyQuery, routeFor } from './routing.mjs';
|
|
5
5
|
export { validateProfile, redactedProfile } from './config.mjs';
|
|
6
|
-
export { SqlClientError, classifyError, asSqlError } from './errors.mjs';
|
|
6
|
+
export { SqlClientError, ClusterUnavailableError, ServerUnavailableError, classifyError, asSqlError } from './errors.mjs';
|
|
7
7
|
export { validateBundle, bundleExpired, bundleNeedsRefresh } from './bundle.mjs';
|
|
8
8
|
export { createAdminSql } from './admin/sql.mjs';
|
|
9
9
|
export { createMigrationRunner } from './admin/migrations.mjs';
|
package/src/pools/route-pool.mjs
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { ClusterUnavailableError, ServerUnavailableError } from '../errors.mjs';
|
|
2
|
+
|
|
3
|
+
export function createRoutePool(nodes, { preferred = false, unavailableError = nodes.length === 1 ? ServerUnavailableError : ClusterUnavailableError } = {}) {
|
|
2
4
|
let cursor = 0;
|
|
3
5
|
const candidates = () => nodes.filter((node) => node.available);
|
|
4
6
|
const choose = () => {
|
|
5
7
|
const available = candidates();
|
|
6
|
-
if (!available.length) throw new
|
|
8
|
+
if (!available.length) throw new unavailableError('no eligible SQL nodes available');
|
|
7
9
|
if (preferred) return available[0];
|
|
8
10
|
const total = available.reduce((sum, node) => sum + Math.max(0, node.weight), 0);
|
|
9
11
|
if (!total) return available[cursor++ % available.length];
|