@hoshinotatsuya/pg-replica-sync 0.2.2 → 0.3.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/CHANGELOG.md +8 -0
- package/README.md +1 -1
- package/dist/compare-lsn.d.mts +3 -3
- package/dist/compare-lsn.d.ts +3 -3
- package/dist/compare-lsn.js +3 -3
- package/dist/constants/exclusion-reason.constant.d.mts +12 -12
- package/dist/constants/exclusion-reason.constant.d.ts +12 -12
- package/dist/constants/exclusion-reason.constant.d.ts.map +1 -1
- package/dist/constants/exclusion-reason.constant.js +12 -12
- package/dist/constants/exclusion-reason.constant.js.map +1 -1
- package/dist/constants/node-role.constant.d.mts +1 -1
- package/dist/constants/node-role.constant.d.ts +1 -1
- package/dist/constants/node-role.constant.d.ts.map +1 -1
- package/dist/constants/node-role.constant.js +1 -1
- package/dist/constants/node-role.constant.js.map +1 -1
- package/dist/constants/probe-sql.constant.d.mts +25 -25
- package/dist/constants/probe-sql.constant.d.ts +25 -25
- package/dist/constants/probe-sql.constant.js +25 -25
- package/dist/constants/replica-sync-defaults.constant.d.mts +26 -26
- package/dist/constants/replica-sync-defaults.constant.d.ts +26 -26
- package/dist/constants/replica-sync-defaults.constant.d.ts.map +1 -1
- package/dist/constants/replica-sync-defaults.constant.js +26 -26
- package/dist/constants/replica-sync-defaults.constant.js.map +1 -1
- package/dist/constants/route-reason.constant.d.mts +13 -13
- package/dist/constants/route-reason.constant.d.ts +13 -13
- package/dist/constants/route-reason.constant.d.ts.map +1 -1
- package/dist/constants/route-reason.constant.js +13 -13
- package/dist/constants/route-reason.constant.js.map +1 -1
- package/dist/constants/token-format.constant.d.mts +11 -11
- package/dist/constants/token-format.constant.d.ts +11 -11
- package/dist/constants/token-format.constant.d.ts.map +1 -1
- package/dist/constants/token-format.constant.js +11 -11
- package/dist/constants/token-format.constant.js.map +1 -1
- package/dist/constants/wal-format.constant.d.mts +6 -6
- package/dist/constants/wal-format.constant.d.ts +6 -6
- package/dist/constants/wal-format.constant.d.ts.map +1 -1
- package/dist/constants/wal-format.constant.js +6 -6
- package/dist/constants/wal-format.constant.js.map +1 -1
- package/dist/decode-canonical-base64url.d.mts +1 -1
- package/dist/decode-canonical-base64url.d.ts +1 -1
- package/dist/decode-canonical-base64url.d.ts.map +1 -1
- package/dist/decode-canonical-base64url.js +1 -1
- package/dist/decode-canonical-base64url.js.map +1 -1
- package/dist/deep-freeze.d.mts +7 -7
- package/dist/deep-freeze.d.ts +7 -7
- package/dist/deep-freeze.js +9 -9
- package/dist/deep-freeze.js.map +1 -1
- package/dist/destroy-client.d.mts +3 -3
- package/dist/destroy-client.d.ts +3 -3
- package/dist/destroy-client.js +3 -3
- package/dist/evaluate-replica.d.mts +3 -3
- package/dist/evaluate-replica.d.ts +3 -3
- package/dist/evaluate-replica.js +3 -3
- package/dist/format-lsn.d.mts +1 -1
- package/dist/format-lsn.d.ts +1 -1
- package/dist/format-lsn.d.ts.map +1 -1
- package/dist/format-lsn.js +2 -2
- package/dist/format-lsn.js.map +1 -1
- package/dist/invalid-lsn.error.d.mts +1 -1
- package/dist/invalid-lsn.error.d.ts +1 -1
- package/dist/invalid-lsn.error.d.ts.map +1 -1
- package/dist/invalid-lsn.error.js +2 -2
- package/dist/invalid-lsn.error.js.map +1 -1
- package/dist/invalid-replica-sync-options.error.d.mts +1 -1
- package/dist/invalid-replica-sync-options.error.d.ts +1 -1
- package/dist/invalid-replica-sync-options.error.d.ts.map +1 -1
- package/dist/invalid-replica-sync-options.error.js +1 -1
- package/dist/invalid-replica-sync-options.error.js.map +1 -1
- package/dist/lag-bytes.d.mts +3 -3
- package/dist/lag-bytes.d.ts +3 -3
- package/dist/lag-bytes.js +3 -3
- package/dist/lsn-or-null.d.mts +1 -1
- package/dist/lsn-or-null.d.ts +1 -1
- package/dist/lsn-or-null.d.ts.map +1 -1
- package/dist/lsn-or-null.js +1 -1
- package/dist/lsn-or-null.js.map +1 -1
- package/dist/notify.d.mts +7 -7
- package/dist/notify.d.ts +7 -7
- package/dist/notify.js +8 -8
- package/dist/notify.js.map +1 -1
- package/dist/parse-lsn.d.mts +3 -3
- package/dist/parse-lsn.d.ts +3 -3
- package/dist/parse-lsn.js +3 -3
- package/dist/probe-connection-closed.error.d.mts +1 -1
- package/dist/probe-connection-closed.error.d.ts +1 -1
- package/dist/probe-connection-closed.error.d.ts.map +1 -1
- package/dist/probe-connection-closed.error.js +2 -2
- package/dist/probe-connection-closed.error.js.map +1 -1
- package/dist/probe-connection.d.mts +20 -20
- package/dist/probe-connection.d.ts +20 -20
- package/dist/probe-connection.d.ts.map +1 -1
- package/dist/probe-connection.js +25 -25
- package/dist/probe-connection.js.map +1 -1
- package/dist/probe-timeout.error.d.mts +1 -1
- package/dist/probe-timeout.error.d.ts +1 -1
- package/dist/probe-timeout.error.d.ts.map +1 -1
- package/dist/probe-timeout.error.js +2 -2
- package/dist/probe-timeout.error.js.map +1 -1
- package/dist/read-node-state.d.mts +3 -3
- package/dist/read-node-state.d.ts +3 -3
- package/dist/read-node-state.js +4 -4
- package/dist/read-node-state.js.map +1 -1
- package/dist/replica-router.d.mts +133 -133
- package/dist/replica-router.d.ts +133 -133
- package/dist/replica-router.d.ts.map +1 -1
- package/dist/replica-router.js +162 -162
- package/dist/replica-router.js.map +1 -1
- package/dist/require-integer.d.mts +1 -1
- package/dist/require-integer.d.ts +1 -1
- package/dist/require-integer.d.ts.map +1 -1
- package/dist/require-integer.js +3 -3
- package/dist/require-integer.js.map +1 -1
- package/dist/secret-weakness.d.mts +13 -13
- package/dist/secret-weakness.d.ts +13 -13
- package/dist/secret-weakness.js +16 -16
- package/dist/secret-weakness.js.map +1 -1
- package/dist/smallest-period.d.mts +4 -4
- package/dist/smallest-period.d.ts +4 -4
- package/dist/smallest-period.js +4 -4
- package/dist/sql-state-of.d.mts +6 -6
- package/dist/sql-state-of.d.ts +6 -6
- package/dist/sql-state-of.js +6 -6
- package/dist/state-cache.d.mts +14 -14
- package/dist/state-cache.d.ts +14 -14
- package/dist/state-cache.d.ts.map +1 -1
- package/dist/state-cache.js +15 -15
- package/dist/state-cache.js.map +1 -1
- package/dist/timeline-of.d.mts +2 -2
- package/dist/timeline-of.d.ts +2 -2
- package/dist/timeline-of.js +2 -2
- package/dist/to-error.d.mts +1 -1
- package/dist/to-error.d.ts +1 -1
- package/dist/to-error.d.ts.map +1 -1
- package/dist/to-error.js +1 -1
- package/dist/to-error.js.map +1 -1
- package/dist/token-codec.d.mts +10 -10
- package/dist/token-codec.d.ts +10 -10
- package/dist/token-codec.js +31 -31
- package/dist/token-codec.js.map +1 -1
- package/dist/token-lsn.d.mts +13 -13
- package/dist/token-lsn.d.ts +13 -13
- package/dist/token-lsn.js +13 -13
- package/dist/transaction-not-committed.error.d.mts +2 -2
- package/dist/transaction-not-committed.error.d.ts +2 -2
- package/dist/transaction-not-committed.error.d.ts.map +1 -1
- package/dist/transaction-not-committed.error.js +3 -3
- package/dist/transaction-not-committed.error.js.map +1 -1
- package/dist/transaction-not-open.error.d.mts +5 -5
- package/dist/transaction-not-open.error.d.ts +5 -5
- package/dist/transaction-not-open.error.js +6 -6
- package/dist/transaction-not-open.error.js.map +1 -1
- package/dist/types/cache-entry.type.d.mts +4 -4
- package/dist/types/cache-entry.type.d.ts +4 -4
- package/dist/types/clock.type.d.mts +3 -3
- package/dist/types/clock.type.d.ts +3 -3
- package/dist/types/confirm-event.type.d.mts +3 -3
- package/dist/types/confirm-event.type.d.ts +3 -3
- package/dist/types/decoded-token.type.d.mts +2 -2
- package/dist/types/decoded-token.type.d.ts +2 -2
- package/dist/types/evaluated.type.d.mts +1 -1
- package/dist/types/evaluated.type.d.ts +1 -1
- package/dist/types/evaluated.type.d.ts.map +1 -1
- package/dist/types/exclusion.type.d.mts +1 -1
- package/dist/types/exclusion.type.d.ts +1 -1
- package/dist/types/exclusion.type.d.ts.map +1 -1
- package/dist/types/node-state.type.d.mts +13 -13
- package/dist/types/node-state.type.d.ts +13 -13
- package/dist/types/node-state.type.d.ts.map +1 -1
- package/dist/types/not-a-pool.type.d.mts +5 -5
- package/dist/types/not-a-pool.type.d.ts +5 -5
- package/dist/types/probe-event.type.d.mts +1 -1
- package/dist/types/probe-event.type.d.ts +1 -1
- package/dist/types/probe-event.type.d.ts.map +1 -1
- package/dist/types/probe-outcome.type.d.mts +1 -1
- package/dist/types/probe-outcome.type.d.ts +1 -1
- package/dist/types/probe-outcome.type.d.ts.map +1 -1
- package/dist/types/probe-sample.type.d.mts +1 -1
- package/dist/types/probe-sample.type.d.ts +1 -1
- package/dist/types/probe-sample.type.d.ts.map +1 -1
- package/dist/types/replica-node.type.d.mts +7 -7
- package/dist/types/replica-node.type.d.ts +7 -7
- package/dist/types/replica-requirement.type.d.mts +2 -2
- package/dist/types/replica-requirement.type.d.ts +2 -2
- package/dist/types/replica-router-options.type.d.mts +25 -25
- package/dist/types/replica-router-options.type.d.ts +25 -25
- package/dist/types/replica-router-options.type.d.ts.map +1 -1
- package/dist/types/route-decision.type.d.mts +5 -5
- package/dist/types/route-decision.type.d.ts +5 -5
- package/dist/types/route-decision.type.d.ts.map +1 -1
- package/dist/types/router-observer.type.d.mts +5 -5
- package/dist/types/router-observer.type.d.ts +5 -5
- package/dist/types/router-observer.type.d.ts.map +1 -1
- package/dist/types/router-stats.type.d.mts +8 -8
- package/dist/types/router-stats.type.d.ts +8 -8
- package/dist/types/router-stats.type.d.ts.map +1 -1
- package/dist/types/sql-command-executor.type.d.mts +4 -4
- package/dist/types/sql-command-executor.type.d.ts +4 -4
- package/dist/types/sql-query-executor.type.d.mts +3 -3
- package/dist/types/sql-query-executor.type.d.ts +3 -3
- package/dist/types/wal-layout.type.d.mts +1 -1
- package/dist/types/wal-layout.type.d.ts +1 -1
- package/dist/types/wal-layout.type.d.ts.map +1 -1
- package/dist/types/wal-position.type.d.mts +5 -5
- package/dist/types/wal-position.type.d.ts +5 -5
- package/dist/types/wal-position.type.d.ts.map +1 -1
- package/dist/value-text.d.mts +5 -5
- package/dist/value-text.d.ts +5 -5
- package/dist/value-text.js +6 -6
- package/dist/value-text.js.map +1 -1
- package/dist/wal-layout-of.d.mts +1 -1
- package/dist/wal-layout-of.d.ts +1 -1
- package/dist/wal-layout-of.d.ts.map +1 -1
- package/dist/wal-layout-of.js +1 -1
- package/dist/wal-layout-of.js.map +1 -1
- package/package.json +11 -9
package/dist/replica-router.d.ts
CHANGED
|
@@ -5,35 +5,35 @@ import type { RouterStats } from './types/router-stats.type';
|
|
|
5
5
|
import type { SqlCommandExecutor } from './types/sql-command-executor.type';
|
|
6
6
|
import type { SqlQueryExecutor } from './types/sql-query-executor.type';
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* `commitAndIssueToken(client)`),
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
8
|
+
* A "read your writes" router: after a write, a token from the
|
|
9
|
+
* connection on which COMMIT went through (`issueToken(client)` or
|
|
10
|
+
* `commitAndIssueToken(client)`), a read with the token is `route(token)`. A replica
|
|
11
|
+
* is chosen only if it is proven that it has caught up with the token position on the same
|
|
12
|
+
* timeline of the same cluster; everything that could not be established goes to the primary. Without
|
|
13
|
+
* a token — a replica whose lag from the primary insert position is within the ceiling.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* The node state is taken by own probe connections (one per node) and
|
|
16
|
+
* cached for `stateTtlMs`; concurrent reads wait for one probe of a node, and a new
|
|
17
|
+
* probe of a node happens no more often than `minProbeIntervalMs`. Each `route()` fits into
|
|
18
18
|
* `probeTimeoutMs + waitForCatchUpMs`.
|
|
19
19
|
*/
|
|
20
20
|
export declare class ReplicaRouter<TTarget> {
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
22
|
+
* "Primary" reasons that after a failover or a cluster substitution no
|
|
23
|
+
* replica will lift: such a token holds the primary for `tokenTtlMs`, then it is handled as without a token.
|
|
24
|
+
* Expiry happens only if the reason is PROVEN by the primary state from a probe started
|
|
25
|
+
* after the process first met the token position (or by excluding each
|
|
26
|
+
* replica as another cluster). A state taken earlier says nothing about the token:
|
|
27
|
+
* a fresh legitimate token is legitimately ahead of it, and expiry based on it would
|
|
28
|
+
* send the read to a replica without the write. A token "without a position" does NOT
|
|
29
|
+
* belong here: its position is unknown, and "as without a token" could send the read
|
|
30
|
+
* to a replica without this write (with an apply delay it is within the lag ceiling) —
|
|
31
|
+
* it holds the primary until the client gets a new token.
|
|
32
32
|
*/
|
|
33
33
|
private static readonly UNSATISFIABLE;
|
|
34
|
-
/** SQLSTATE:
|
|
34
|
+
/** SQLSTATE: a transaction block command outside a transaction. */
|
|
35
35
|
private static readonly NO_ACTIVE_TRANSACTION;
|
|
36
|
-
/** SQLSTATE:
|
|
36
|
+
/** SQLSTATE: the transaction is in error, commands until its end are rejected. */
|
|
37
37
|
private static readonly IN_FAILED_TRANSACTION;
|
|
38
38
|
private readonly codec;
|
|
39
39
|
private readonly primary;
|
|
@@ -42,12 +42,12 @@ export declare class ReplicaRouter<TTarget> {
|
|
|
42
42
|
private readonly inflight;
|
|
43
43
|
private readonly inflightSeq;
|
|
44
44
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
45
|
+
* Token position → the probe number at the first encounter with it (the probe frequency
|
|
46
|
+
* ceiling and the proof of unsatisfiability), no more than `POSITION_MEMORY`
|
|
47
|
+
* entries, the one not seen for the longest time is evicted (LRU).
|
|
48
48
|
*/
|
|
49
49
|
private readonly seenPositions;
|
|
50
|
-
/**
|
|
50
|
+
/** The last timeline that the WAL receiver showed on the replica instance. */
|
|
51
51
|
private readonly lastTimeline;
|
|
52
52
|
private readonly requirements;
|
|
53
53
|
private readonly cache;
|
|
@@ -68,181 +68,181 @@ export declare class ReplicaRouter<TTarget> {
|
|
|
68
68
|
private readonly counters;
|
|
69
69
|
constructor(options: ReplicaRouterOptions<TTarget>);
|
|
70
70
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* COMMIT
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
71
|
+
* The position token after a write — from the `executor` on which COMMIT went through (or from
|
|
72
|
+
* any primary connection AFTER COMMIT returned: the insert position
|
|
73
|
+
* is monotonic). The argument is required: a probe connection or a pool does not see
|
|
74
|
+
* an uncommitted transaction of ANOTHER connection, and a token taken by them before
|
|
75
|
+
* COMMIT would be smaller than the commit position — a replica would "catch up" with it without
|
|
76
|
+
* this write. If the position could not be determined (the node is in recovery, a transaction with
|
|
77
|
+
* a write on this connection is not committed, a refusal, the time ceiling), the token is
|
|
78
|
+
* "primary only", not an exception: a client with it reads from the primary.
|
|
79
|
+
* The same after `close()`: the write has already gone through, and an exception would look like its
|
|
80
|
+
* refusal and push towards a retry.
|
|
81
81
|
*/
|
|
82
82
|
issueToken(executor: SqlQueryExecutor): Promise<string>;
|
|
83
83
|
/**
|
|
84
|
-
* COMMIT
|
|
85
|
-
* COMMIT,
|
|
84
|
+
* COMMIT on the connection of the write transaction and the token from it — the order "first
|
|
85
|
+
* COMMIT, then the position" is held by the package, not by the caller.
|
|
86
86
|
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* (25P02)
|
|
95
|
-
* COMMIT
|
|
87
|
+
* Before COMMIT the INVARIANT is checked, not the shape of the object: a transaction is open
|
|
88
|
+
* on this connection. The check is `SAVEPOINT`: outside a transaction Postgres answers
|
|
89
|
+
* with the refusal 25P01, inside one it creates a savepoint that COMMIT commits
|
|
90
|
+
* together with everything. A pool or a wrapper over a pool would send the query to another
|
|
91
|
+
* connection without a transaction — `TransactionNotOpenError`, and COMMIT is not
|
|
92
|
+
* sent at all (the caller's transaction stays open rather than
|
|
93
|
+
* "committed" with a warning on someone else's connection). A transaction in error
|
|
94
|
+
* (25P02) is finished with ROLLBACK — `TransactionNotCommittedError`; a refusal of the
|
|
95
|
+
* COMMIT itself is its exception. The SQLSTATE is also looked for in the `cause` chain (wrappers).
|
|
96
96
|
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
97
|
+
* An exception means "there is no write or it is unknown what happened to it": the caller
|
|
98
|
+
* must finish the transaction (`catch → ROLLBACK`), otherwise the connection goes to the
|
|
99
|
+
* pool with an open transaction. A closed router does not cancel COMMIT:
|
|
100
|
+
* an exception before COMMIT on a regular shutdown would leave the transaction open, and
|
|
101
|
+
* its write would be committed by someone else's COMMIT from the pool. After `close()` — COMMIT as
|
|
102
|
+
* usual and a "primary only" token. A transaction that SAVEPOINT found
|
|
103
|
+
* healthy is committed after a COMMIT without an exception — and a driver without the response
|
|
104
|
+
* tag (`command`) does not get "rolled back" (a retry would write a duplicate).
|
|
105
105
|
*/
|
|
106
106
|
commitAndIssueToken(client: SqlCommandExecutor & NotAPool): Promise<string>;
|
|
107
107
|
/**
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
108
|
+
* Where to read. No token (`undefined`, `null`, an empty string) —
|
|
109
|
+
* a replica with the lag within the ceiling. With a token — only a replica that has caught up with it;
|
|
110
|
+
* a token that did not parse — the primary. Does not throw on node refusals: a refusal is an
|
|
111
|
+
* exclusion of the replica and, in the end, the primary. Lasts no longer than
|
|
112
112
|
* `probeTimeoutMs + waitForCatchUpMs`.
|
|
113
113
|
*/
|
|
114
114
|
route(token?: string | null): Promise<RouteDecision<TTarget>>;
|
|
115
115
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* (`balanced: true`, `decision.mustConfirm`):
|
|
119
|
-
*
|
|
120
|
-
* `true`.
|
|
121
|
-
*
|
|
116
|
+
* Confirm the "replica" decision on the connection that the read will use: the same
|
|
117
|
+
* probe and the same conditions, but on the read server. Needed for balancer nodes
|
|
118
|
+
* (`balanced: true`, `decision.mustConfirm`): the router probe saw one
|
|
119
|
+
* server, while the read connection could get to another. A "primary" decision is
|
|
120
|
+
* `true`. A refusal, the time ceiling, "could not determine", a closed
|
|
121
|
+
* router — `false`: read from the primary.
|
|
122
122
|
*/
|
|
123
123
|
confirm(decision: RouteDecision<TTarget>, executor: SqlQueryExecutor): Promise<boolean>;
|
|
124
|
-
/**
|
|
124
|
+
/** Counters; the result is serialized by `JSON.stringify` (the lag is a number, not a `bigint`). */
|
|
125
125
|
stats(): RouterStats;
|
|
126
126
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
127
|
+
* Close the probe connections; they are not opened again. Ongoing `route()` calls
|
|
128
|
+
* stop waiting and probing and decide "primary" by what they already know.
|
|
129
|
+
* The `target` connections belong to the caller.
|
|
130
130
|
*/
|
|
131
131
|
close(): Promise<void>;
|
|
132
132
|
[Symbol.asyncDispose](): Promise<void>;
|
|
133
133
|
private static requireExecutor;
|
|
134
134
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
135
|
+
* The clocks must give a finite number already at creation: a cache clock that returned
|
|
136
|
+
* a non-number would make the cache eternal (or turn it off), an issue clock would give tokens
|
|
137
|
+
* without an expiry. A configuration error is visible at startup, not as silence of the ceilings.
|
|
138
138
|
*/
|
|
139
139
|
private static requireClock;
|
|
140
140
|
/**
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
* (
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
141
|
+
* The probe number at the moment the process first saw the token POSITION. A probe with
|
|
142
|
+
* a greater number is started after that — that is, after the token with this
|
|
143
|
+
* position was issued: the primary insert position by its start is not smaller than the token position
|
|
144
|
+
* (if the primary did not change), and "the replica lags" in it is not by a snapshot before the
|
|
145
|
+
* write. The key is the position, not the token string: tokens of one position (different
|
|
146
|
+
* issue moments) share one entry, and that is correct — by the issue of any of them
|
|
147
|
+
* the insert position had already reached this one. The memory is bounded (`POSITION_MEMORY`):
|
|
148
|
+
* the keys come from clients. The position not seen for the longest time is evicted (LRU),
|
|
149
|
+
* not the first inserted one: a hot position does not go away because of a stream of new ones.
|
|
150
|
+
* An evicted one is considered new at the next encounter — one extra probe and
|
|
151
|
+
* a conservative answer, not an error.
|
|
152
152
|
*/
|
|
153
153
|
private firstSeen;
|
|
154
154
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
155
|
+
* The issue moment by `wallClock`. A clock that started returning something wrong (not a number,
|
|
156
|
+
* a negative one) does not fail the issue of a token AFTER a write: the moment is 0, that is
|
|
157
|
+
* "long ago", and expiry still requires a proven unsatisfiability.
|
|
158
158
|
*/
|
|
159
159
|
private issuedAt;
|
|
160
|
-
/**
|
|
160
|
+
/** The token after COMMIT: the position from the write connection, and on a refusal or after `close()` — "primary only". */
|
|
161
161
|
private tokenAfterCommit;
|
|
162
162
|
private resolve;
|
|
163
|
-
/**
|
|
163
|
+
/** A token older than `tokenTtlMs` that after a failover no replica can satisfy: the read goes as without a token. */
|
|
164
164
|
private routeExpired;
|
|
165
165
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
166
|
+
* The decision by the token position and the flag "unsatisfiability is proven" (for
|
|
167
|
+
* expiry). `expired` means the token is older than the expiry: then the reason for going to the primary
|
|
168
|
+
* is established by a primary probe started after the first encounter with the position,
|
|
169
|
+
* even if the cache has an earlier state.
|
|
170
170
|
*/
|
|
171
171
|
private routeWithPosition;
|
|
172
172
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
173
|
+
* Without a token: the primary state (the probe is bounded by its own ceiling, and after
|
|
174
|
+
* an expired token by the rest of the read ceiling), then the replicas — up to the read ceiling.
|
|
175
175
|
*/
|
|
176
176
|
private routeWithoutToken;
|
|
177
177
|
/**
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
178
|
+
* Why the primary: by the exclusions and (if possible) by the primary state.
|
|
179
|
+
* "The token is ahead of the primary" is named only by a state from a probe started
|
|
180
|
+
* AFTER the process first met the token position: the token was issued before that, and
|
|
181
|
+
* a primary snapshot taken earlier is legitimately behind it. No fresh state (the read
|
|
182
|
+
* ceiling, a refusal) — `no-replica-eligible`, not a false alarm.
|
|
183
183
|
*
|
|
184
|
-
* `proven`
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
184
|
+
* `proven` means the reason is established by such a state (or each replica is
|
|
185
|
+
* another cluster): only then does an expired token expire. A primary cache
|
|
186
|
+
* taken before the first encounter with the position proves nothing, however old it
|
|
187
|
+
* is and however the process clocks diverge. `requireFresh` (an expired
|
|
188
|
+
* token) — such a state is obtained by a probe even if the cache says "behind".
|
|
189
189
|
*/
|
|
190
190
|
private fallbackReason;
|
|
191
|
-
/**
|
|
191
|
+
/** The lag in bytes as a number: JSON-serializable; above 2⁵³ (8 PiB of WAL) it is a ceiling, not a silent loss of precision. */
|
|
192
192
|
private static toSafeNumber;
|
|
193
193
|
private static ahead;
|
|
194
194
|
private evaluateAll;
|
|
195
195
|
/**
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* `unavailable`
|
|
201
|
-
*
|
|
196
|
+
* The state of each replica, in parallel: from the cache, or by a fresh probe if it is
|
|
197
|
+
* not in the cache or `needsProbe` (with the frequency ceiling). A refusal still living in the cache
|
|
198
|
+
* is not polled again until its ceiling. A probe that did not make it by the `deadline`
|
|
199
|
+
* gives the previous estimate of the node from `previous` (a poll during the wait), and without it
|
|
200
|
+
* `unavailable` for this read; the probe itself lives until its ceiling and
|
|
201
|
+
* gets into the cache.
|
|
202
202
|
*/
|
|
203
203
|
private evaluateFresh;
|
|
204
204
|
private recordLag;
|
|
205
205
|
/**
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
* `startedAfterSeq`
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
206
|
+
* The state of a node: from the cache or a probe. Concurrent calls for one node wait for
|
|
207
|
+
* ONE probe. `maxAgeMs`: the cache is usable only if its probe was started not earlier than
|
|
208
|
+
* that many ms ago (the frequency ceiling: a younger one gets no repeated probe), and with
|
|
209
|
+
* `startedAfterSeq` also only if the probe was started AFTER this
|
|
210
|
+
* process first saw the token: a probe taken earlier says nothing about the token's write,
|
|
211
|
+
* and a read right after its own write gets a fresh probe rather than
|
|
212
|
+
* "lags" by someone else's. The same for an ongoing probe: one started earlier is not usable,
|
|
213
|
+
* it is waited for and the node is probed again. The clocks do not take part: a divergence of the
|
|
214
|
+
* process clocks does not remove the ceiling and does not hand out someone else's probe.
|
|
215
|
+
* A refusal from the cache is always usable — until its ceiling.
|
|
216
216
|
*/
|
|
217
217
|
private state;
|
|
218
218
|
private probe;
|
|
219
219
|
/**
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
* (
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
220
|
+
* The timeline of a replica whose WAL receiver does not work (the primary is down or
|
|
221
|
+
* unreachable): the one the receiver showed on THE SAME server instance
|
|
222
|
+
* (a restart is another instance, and the previous knowledge does not carry over). A replica can
|
|
223
|
+
* change its timeline only by following a new primary — that is,
|
|
224
|
+
* after a failover, where a write lost in it is not visible on the new primary either.
|
|
225
|
+
* The substitution does not depend on the privilege on `pg_stat_wal_receiver`: without it the
|
|
226
|
+
* timeline is never visible, and there is nothing to substitute — "no privileges" stays.
|
|
227
227
|
*/
|
|
228
228
|
private withLastTimeline;
|
|
229
|
-
/**
|
|
229
|
+
/** The position after COMMIT on the write connection: every "could not determine" is an exception. */
|
|
230
230
|
private positionAfterCommit;
|
|
231
231
|
private queryWithin;
|
|
232
232
|
/**
|
|
233
|
-
* `work`
|
|
234
|
-
*
|
|
235
|
-
*
|
|
233
|
+
* `work` or `null` if it did not make it by the `deadline` (the monotonic clock) or
|
|
234
|
+
* the router is closed. An already ready value makes it even with an exhausted ceiling;
|
|
235
|
+
* an infinite `deadline` means no timer (`setTimeout(Infinity)` would fire after 1 ms).
|
|
236
236
|
*/
|
|
237
237
|
private within;
|
|
238
238
|
private connectionOf;
|
|
239
239
|
private anyBehind;
|
|
240
|
-
/**
|
|
240
|
+
/** The eligible ones in turn: the load is shared between the replicas that caught up. */
|
|
241
241
|
private pick;
|
|
242
242
|
private exclusionsOf;
|
|
243
243
|
private toReplica;
|
|
244
244
|
private toPrimary;
|
|
245
|
-
/**
|
|
245
|
+
/** The single point of accounting a decision: counters and the hook — exactly once per `route()`. */
|
|
246
246
|
private decide;
|
|
247
247
|
private assertOpen;
|
|
248
248
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replica-router.d.ts","sourceRoot":"","sources":["../src/replica-router.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAKxD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAChF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAGjE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AAC5E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAGxE;;;;;;;;;;;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