@zukhruf/election 0.8.18 → 0.8.19
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 +53 -8
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/leader-election.d.ts +18 -6
- package/dist/leader-election.d.ts.map +1 -1
- package/dist/leader-election.js +39 -14
- package/dist/leader-election.js.map +1 -1
- package/dist/sqlite/leader-observer.d.ts +21 -0
- package/dist/sqlite/leader-observer.d.ts.map +1 -0
- package/dist/sqlite/leader-observer.js +40 -0
- package/dist/sqlite/leader-observer.js.map +1 -0
- package/dist/sqlite/leader-pipe.d.ts +25 -0
- package/dist/sqlite/leader-pipe.d.ts.map +1 -0
- package/dist/sqlite/leader-pipe.js +75 -0
- package/dist/sqlite/leader-pipe.js.map +1 -0
- package/dist/sqlite/leader-reader.d.ts +23 -0
- package/dist/sqlite/leader-reader.d.ts.map +1 -0
- package/dist/sqlite/leader-reader.js +104 -0
- package/dist/sqlite/leader-reader.js.map +1 -0
- package/dist/sqlite/leader-record.d.ts +20 -0
- package/dist/sqlite/leader-record.d.ts.map +1 -0
- package/dist/sqlite/leader-record.js +49 -0
- package/dist/sqlite/leader-record.js.map +1 -0
- package/dist/sqlite/sqlite-attempt.d.ts +15 -0
- package/dist/sqlite/sqlite-attempt.d.ts.map +1 -0
- package/dist/sqlite/sqlite-attempt.js +32 -0
- package/dist/sqlite/sqlite-attempt.js.map +1 -0
- package/dist/sqlite/sqlite-election.d.ts +47 -16
- package/dist/sqlite/sqlite-election.d.ts.map +1 -1
- package/dist/sqlite/sqlite-election.js +97 -13
- package/dist/sqlite/sqlite-election.js.map +1 -1
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -49,14 +49,44 @@ a second candidate wins: undefined
|
|
|
49
49
|
next term: 2n
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
- `campaign({ timeout, signal })` resolves with a `Term`, or with `undefined` when another candidate still leads after `timeout` milliseconds. Without a `timeout`, it tries once. When `signal` aborts, it rejects with `signal.reason`, and a claim that it won meanwhile is given up.
|
|
52
|
+
- `campaign({ timeout, signal })` resolves with a `Term`, or with `undefined` when another candidate still leads after `timeout` milliseconds. Without a `timeout`, it tries once. With `timeout: Infinity`, it waits until it wins or `signal` aborts. When `signal` aborts, it rejects with `signal.reason`, and a claim that it won meanwhile is given up.
|
|
53
53
|
- `claimFile` and `epochFile` are files in `directory`. Candidates with the same directory and the same file names are one group. Never delete `claimFile` while candidates run: a new file lets a second leader win.
|
|
54
54
|
- `epochFile` holds the epoch of the last term as decimal digits. When it holds other text, `campaign` rejects with a `SyntaxError` that names the file, gives the claim up, and does not change the file. An example is the empty file that `@zukhruf/mutex` 0.2.0 or earlier can leave after a power loss, because those versions did not sync the file to the disk. Thus the epochs never start again at 1, and each new term has a higher epoch than each earlier term.
|
|
55
55
|
- `lastEpoch()` resolves with the epoch of the last term in `epochFile`, or with `0n` when no term was won yet. It only reads the file. It does not campaign, it does not take the claim, and it does not create a file or the directory. Use it to find out if a value from an earlier term is stale, for example a fencing token that an old leader made with `EpochTokenSource`. A new term can start immediately after the read, so the answer can be old when you get it. When `epochFile` holds text that is not decimal digits, `lastEpoch()` rejects with the same `SyntaxError` as `campaign`. When it cannot read the file for another reason, it rejects with that error. It never answers `0n` for a file that it cannot read.
|
|
56
56
|
- A term that resigns clean writes a third file, `<claimFile>.clean`, beside `claimFile`. It holds the epoch of that term. The next term reads it and removes it.
|
|
57
57
|
- The directory must be on a local file system. On a network file system, `campaign` rejects with `NetworkDirectoryError`.
|
|
58
|
-
-
|
|
59
|
-
- `clock` is the clock that the campaign waits on, and that measures its `timeout
|
|
58
|
+
- A try never blocks the process. Between two tries, a candidate waits on pushes from the leader, not on a period ([A leader that hangs](#a-leader-that-hangs)).
|
|
59
|
+
- `clock` is the clock that the campaign waits on, and that measures its `timeout`, the heartbeats of a leader and the deadline of a reader. It is required. Give `globalThis` in production. A test gives a fake clock, for example `createClock()` of `@sinonjs/fake-timers`, and moves the time of the campaign without a real wait ([`@zukhruf/async`](../async/README.md#run-a-wait-on-a-clock)).
|
|
60
|
+
|
|
61
|
+
## A leader that hangs
|
|
62
|
+
|
|
63
|
+
A hung leader keeps the claim of `SqliteElection`: a leader that `SIGSTOP` stopped, or a leader whose event loop is blocked. The kernel frees the claim only when the process ends. Thus the leader pushes a heartbeat on a pipe, a candidate waits on that pipe, and a candidate ends a leader that is silent for too long. A candidate waits as etcd's `Campaign` and ZooKeeper's leader election recipe wait: on a push, not on a period ([ADR 0007](./docs/adr/0007-a-candidate-waits-on-pushes-from-the-leaders-pipe-not-on-a-period.md)).
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const election = new SqliteElection({
|
|
67
|
+
clock: globalThis,
|
|
68
|
+
directory,
|
|
69
|
+
claimFile: 'jobs.lock',
|
|
70
|
+
epochFile: 'jobs.epoch',
|
|
71
|
+
leaseDuration: 15_000, // the default
|
|
72
|
+
heartbeatInterval: 5_000, // the default: a third of leaseDuration
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
await using term = await election.campaign({ timeout: Infinity, signal });
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
- **The pipe.** The winner of a claim serves a pipe of [`@zukhruf/pipes`](../pipes/README.md) for its term. It sends a heartbeat on each connection when it accepts it, and then every `heartbeatInterval` until the term closes, also when a subclass overrides `watch` ([ADR 0004](./docs/adr/0004-a-leader-renews-its-term-with-a-heartbeat-on-a-pipe-that-it-serves.md)).
|
|
79
|
+
- **The leader record.** The winner writes `<claimFile>.leader` beside `claimFile`, before its term begins. It holds the epoch of the term, the process id of the leader, the address of its pipe, and its `leaseDuration`. The winner first removes the socket file of the pipe that the old record names, which a killed leader leaves ([ADR 0005](./docs/adr/0005-the-leader-record-names-the-process-and-the-pipe-of-a-term.md)).
|
|
80
|
+
- **The wait.** A candidate tries the claim. When another candidate holds it, the candidate reads the record, connects to the pipe, and waits. The close of the pipe tells it that the leader left, and it tries at once. A candidate under a healthy leader makes no try ([ADR 0007](./docs/adr/0007-a-candidate-waits-on-pushes-from-the-leaders-pipe-not-on-a-period.md)).
|
|
81
|
+
- **A hung leader.** A candidate that hears no heartbeat for the `leaseDuration` of the record sends `SIGKILL` to the process of the record, but only while it holds that process's pipe open. The kernel frees the claim and closes the pipe, and the candidate tries. The kernel refuses the kill for a process of another user: then the candidate ends nothing, and it waits for the close until its `timeout`. A reader never ends its own process, in the same way ([ADR 0006](./docs/adr/0006-a-candidate-ends-a-leader-that-is-silent-past-the-lease-duration.md)).
|
|
82
|
+
- **A new leader that does not serve its pipe yet.** A connect that gives `ENOENT` or `ECONNREFUSED` comes while a new leader starts, or under a published leader. The candidate waits a back-off and tries again: 25 ms first, then 1.6 times longer each time, with a jitter of ±20 %, at most 1000 ms.
|
|
83
|
+
- **An observer.** `election.observe()` returns a `LeaderObserver` for a process that does not campaign. It holds a connection to the leader's pipe and ends a silent leader as a candidate does. `observer.left` resolves `'closed'` when the leader left, `'ended'` when the observer ended it, and `'absent'` when no leader served a pipe. It rejects with any other error. Dispose it to close the connection; then `left` does not settle. Its connection and its deadline do not keep the process alive. One observer reads one term ([ADR 0009](./docs/adr/0009-a-process-that-does-not-campaign-observes-the-leader.md)).
|
|
84
|
+
- `leaseDuration` is the longest silence, in milliseconds, that a candidate or an observer accepts from the leader. It defaults to 15000, as the `LeaseDuration` of Kubernetes. A reader uses the value in the record, so the leader decides it.
|
|
85
|
+
- `heartbeatInterval` is the time between two heartbeats, in milliseconds. It defaults to a third of `leaseDuration`, as etcd's client sends a keepalive every TTL / 3. The name is etcd's `heartbeat-interval`.
|
|
86
|
+
- The constructor throws a `RangeError` unless `0 < heartbeatInterval < leaseDuration`.
|
|
87
|
+
- The pipe, its connections and the heartbeat timer of a leader do not keep its process alive. A waiting candidate keeps its process alive until the campaign ends.
|
|
88
|
+
- Run the candidates of one group as one user, in one PID namespace, with one `/tmp`. A candidate that runs as root can end a leader of any user.
|
|
89
|
+
- With `SqliteElection`, a hung leader never runs again after a takeover: a new term starts only after the old process ended. A leader that runs again before its deadline still leads, and its `term.signal` does not abort.
|
|
60
90
|
|
|
61
91
|
## The term
|
|
62
92
|
|
|
@@ -83,28 +113,31 @@ const token = await tokens.next('orders');
|
|
|
83
113
|
|
|
84
114
|
## Write a backend
|
|
85
115
|
|
|
86
|
-
`LeaderElection` is an abstract class. It runs the campaign, and it owns the term. A backend is a subclass that implements
|
|
116
|
+
`LeaderElection` is an abstract class. It runs the campaign, and it owns the term. Its constructor takes only the clock. A backend is a subclass that implements seven steps for its claim ([ADR 0008](./docs/adr/0008-the-backend-owns-the-wait-between-two-tries-and-poll-leaves-the-campaign.md)):
|
|
87
117
|
|
|
88
118
|
| Step | What it does |
|
|
89
119
|
| ----------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
90
120
|
| `open(signal)` | Prepares one attempt, for example a connection. The campaign calls `tryClaim` on it again and again. |
|
|
91
121
|
| `tryClaim(claim, signal)` | Tries the claim once. Resolves with `{ epoch, afterCleanShutdown }` for the term it won, or with `undefined` while another candidate holds the claim. |
|
|
122
|
+
| `waitForChange(claim, signal)` | Resolves when a try of the claim can win: the holder left, was ended, or the backend wants a new try. Settles when `signal` aborts. |
|
|
92
123
|
| `watch(claim, lose)` | Watches a won claim for as long as its term lasts. Calls `lose` when the backend takes the claim away. Returns a `Disposable` that stops the watch. |
|
|
93
124
|
| `recordCleanShutdown(claim, epoch)` | Records that the term of `epoch` resigned clean. The campaign calls it before `release`, while the term still holds the claim. |
|
|
94
125
|
| `release(claim)` | Gives a won claim up when the leader resigns. |
|
|
95
126
|
| `close(claim)` | Frees the resources of an attempt, at each end. |
|
|
96
127
|
|
|
97
|
-
A backend must keep
|
|
128
|
+
A backend must keep six rules:
|
|
98
129
|
|
|
99
130
|
1. `tryClaim` gives the epoch in the same step that wins the claim. Each epoch is higher than each earlier one, and below 2^31.
|
|
100
131
|
2. `tryClaim` gives `afterCleanShutdown: true` only when the term just before this one recorded a clean shutdown. It reads each record one time only. Thus a crash after a clean term never reads as clean.
|
|
101
132
|
3. `watch` calls `lose` before the backend can give the claim to another candidate. For a claim that the backend keeps only for a time, renew it well before that time ends, and count the time on a monotonic clock. Then an old leader stops before a new one starts.
|
|
102
133
|
4. The campaign never calls `recordCleanShutdown` or `release` after a loss, because the claim can be another candidate's then.
|
|
103
134
|
5. The campaign calls `close` after each end: a won term, a lost term, a failed attempt, or an attempt that never won.
|
|
135
|
+
6. `waitForChange` settles when its signal aborts, and it never ends the attempt itself. The campaign aborts that signal at its `timeout`, and then it tries one last time. A backend with no push polls in its own `waitForChange`, for example with `delay` of `@zukhruf/async`.
|
|
104
136
|
|
|
105
|
-
This backend elects among the candidates of one process. Its claim is an entry in a map:
|
|
137
|
+
This backend elects among the candidates of one process. Its claim is an entry in a map, and it has no push, so it polls:
|
|
106
138
|
|
|
107
139
|
```ts
|
|
140
|
+
import { delay } from '@zukhruf/async';
|
|
108
141
|
import { LeaderElection } from '@zukhruf/election';
|
|
109
142
|
|
|
110
143
|
const holders = new Map<string, bigint>();
|
|
@@ -115,7 +148,7 @@ class MapElection extends LeaderElection<string> {
|
|
|
115
148
|
readonly #name: string;
|
|
116
149
|
|
|
117
150
|
constructor(name: string) {
|
|
118
|
-
super(
|
|
151
|
+
super(globalThis);
|
|
119
152
|
this.#name = name;
|
|
120
153
|
}
|
|
121
154
|
|
|
@@ -133,6 +166,11 @@ class MapElection extends LeaderElection<string> {
|
|
|
133
166
|
return { epoch: lastEpoch, afterCleanShutdown };
|
|
134
167
|
}
|
|
135
168
|
|
|
169
|
+
// A resign deletes the entry, but nothing tells the waiting candidates.
|
|
170
|
+
protected waitForChange(_name: string, signal: AbortSignal) {
|
|
171
|
+
return delay(10, { clock: this.clock, signal });
|
|
172
|
+
}
|
|
173
|
+
|
|
136
174
|
// Nothing takes an entry of the map away, so there is nothing to watch.
|
|
137
175
|
protected watch() {
|
|
138
176
|
return { [Symbol.dispose]() {} };
|
|
@@ -184,6 +222,12 @@ The socket lock store of `@zukhruf/mutex` uses `SqliteElection`, with the claim
|
|
|
184
222
|
- [ADR 0001: Leader election is a package, and each backend is a subclass of one campaign](./docs/adr/0001-leader-election-is-a-package-and-each-backend-is-a-subclass.md)
|
|
185
223
|
- [ADR 0002: A term is a lease on leadership, with no fencing](./docs/adr/0002-a-term-is-a-lease-on-leadership-with-no-fencing.md)
|
|
186
224
|
- [ADR 0003: A term that shuts down cleanly tells its successor](./docs/adr/0003-a-term-that-shuts-down-cleanly-tells-its-successor.md)
|
|
225
|
+
- [ADR 0004: A leader renews its term with a heartbeat on a pipe that it serves](./docs/adr/0004-a-leader-renews-its-term-with-a-heartbeat-on-a-pipe-that-it-serves.md) (proposed)
|
|
226
|
+
- [ADR 0005: The leader record names the process and the pipe of a term](./docs/adr/0005-the-leader-record-names-the-process-and-the-pipe-of-a-term.md) (proposed)
|
|
227
|
+
- [ADR 0006: A candidate ends a leader that is silent past the lease duration](./docs/adr/0006-a-candidate-ends-a-leader-that-is-silent-past-the-lease-duration.md) (proposed)
|
|
228
|
+
- [ADR 0007: A candidate waits on pushes from the leader's pipe, not on a period](./docs/adr/0007-a-candidate-waits-on-pushes-from-the-leaders-pipe-not-on-a-period.md) (proposed)
|
|
229
|
+
- [ADR 0008: The backend owns the wait between two tries, and poll leaves the campaign](./docs/adr/0008-the-backend-owns-the-wait-between-two-tries-and-poll-leaves-the-campaign.md) (proposed)
|
|
230
|
+
- [ADR 0009: A process that does not campaign observes the leader](./docs/adr/0009-a-process-that-does-not-campaign-observes-the-leader.md) (proposed)
|
|
187
231
|
- [Code copied into this package](./docs/copied-code.md)
|
|
188
232
|
|
|
189
233
|
## Development
|
|
@@ -201,6 +245,7 @@ npx nx run election:build # compiles src/ to dist/
|
|
|
201
245
|
src/
|
|
202
246
|
leader-election.ts LeaderElection: the campaign, and the steps of a backend
|
|
203
247
|
term.ts Term: the epoch, the lease on leadership, and resign
|
|
204
|
-
sqlite/ SqliteElection: the backend of one host
|
|
248
|
+
sqlite/ SqliteElection: the backend of one host, its leader pipe,
|
|
249
|
+
its leader record, and the readers of the pipe
|
|
205
250
|
testing/ the helpers that start candidate processes in the tests
|
|
206
251
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { type CampaignOptions, LeaderElection } from './leader-election.ts';
|
|
2
2
|
export { type ResignOptions, Term, type TermSteps } from './term.ts';
|
|
3
|
+
export { type LeaderObserver } from './sqlite/leader-observer.ts';
|
|
3
4
|
export { SqliteElection, type SqliteElectionOptions, } from './sqlite/sqlite-election.ts';
|
|
4
5
|
export { NetworkDirectoryError } from '@zukhruf/fs';
|
|
5
6
|
export { LeaseLostError } from '@zukhruf/lease';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAAE,KAAK,aAAa,EAAE,IAAI,EAAE,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AACrE,OAAO,EACL,cAAc,EACd,KAAK,qBAAqB,GAC3B,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAAE,KAAK,aAAa,EAAE,IAAI,EAAE,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AACrE,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EACL,cAAc,EACd,KAAK,qBAAqB,GAC3B,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { LeaderElection } from "./leader-election.js";
|
|
2
2
|
export { Term } from "./term.js";
|
|
3
|
+
export {} from "./sqlite/leader-observer.js";
|
|
3
4
|
export { SqliteElection, } from "./sqlite/sqlite-election.js";
|
|
4
5
|
export { NetworkDirectoryError } from '@zukhruf/fs';
|
|
5
6
|
export { LeaseLostError } from '@zukhruf/lease';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAAsB,IAAI,EAAkB,MAAM,WAAW,CAAC;AACrE,OAAO,EACL,cAAc,GAEf,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAAsB,IAAI,EAAkB,MAAM,WAAW,CAAC;AACrE,OAAO,EAAuB,MAAM,6BAA6B,CAAC;AAClE,OAAO,EACL,cAAc,GAEf,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC"}
|
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
import { type Clock } from '@zukhruf/async';
|
|
2
2
|
import { Term } from './term.ts';
|
|
3
3
|
export interface CampaignOptions {
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Milliseconds to keep trying before conceding to the current leader.
|
|
6
|
+
* Defaults to one attempt. `Infinity` waits until the campaign wins or its
|
|
7
|
+
* signal aborts.
|
|
8
|
+
*/
|
|
5
9
|
timeout?: number | undefined;
|
|
6
10
|
/** Stops the campaign, which then rejects with `signal.reason`. A term won after it aborted is given up. */
|
|
7
11
|
signal?: AbortSignal | undefined;
|
|
8
12
|
}
|
|
9
13
|
/**
|
|
10
14
|
* Elects one leader among candidates. The campaign is the same for each
|
|
11
|
-
* backend: open an attempt, try the claim
|
|
12
|
-
*
|
|
13
|
-
*
|
|
15
|
+
* backend: open an attempt, try the claim, wait for a change that lets a try
|
|
16
|
+
* win, try again until the claim is won or the time is up, watch the won
|
|
17
|
+
* claim, and hand it to a term. Each backend is a subclass that implements
|
|
18
|
+
* the steps for its claim.
|
|
14
19
|
*
|
|
15
|
-
* A backend keeps
|
|
20
|
+
* A backend keeps six rules:
|
|
16
21
|
* - `tryClaim` returns the epoch of the term it won, in the same step, and
|
|
17
22
|
* each epoch is higher than every earlier one and below 2^31.
|
|
18
23
|
* - `tryClaim` reports `afterCleanShutdown` only when the term just before
|
|
@@ -23,12 +28,14 @@ export interface CampaignOptions {
|
|
|
23
28
|
* - `release` is never called after a loss: the claim may be another
|
|
24
29
|
* candidate's by then.
|
|
25
30
|
* - `close` frees the attempt's resources, after each end.
|
|
31
|
+
* - `waitForChange` settles when its signal aborts, and never ends the
|
|
32
|
+
* attempt itself.
|
|
26
33
|
*/
|
|
27
34
|
export declare abstract class LeaderElection<Claim> {
|
|
28
35
|
#private;
|
|
29
36
|
/** The clock of the campaign: `globalThis` in production, a fake clock in a test. */
|
|
30
37
|
protected readonly clock: Clock;
|
|
31
|
-
protected constructor(
|
|
38
|
+
protected constructor(clock: Clock);
|
|
32
39
|
/** Resolves with the new term, or `undefined` when another candidate still leads after `timeout`. */
|
|
33
40
|
campaign({ timeout, signal }?: CampaignOptions): Promise<Term | undefined>;
|
|
34
41
|
/** Prepares one attempt: the resources that `tryClaim` uses again and again. */
|
|
@@ -39,6 +46,11 @@ export declare abstract class LeaderElection<Claim> {
|
|
|
39
46
|
* candidate holds it.
|
|
40
47
|
*/
|
|
41
48
|
protected abstract tryClaim(claim: Claim, signal: AbortSignal | undefined): Promise<Pick<Term, 'epoch' | 'afterCleanShutdown'> | undefined>;
|
|
49
|
+
/**
|
|
50
|
+
* Resolves when a try of the claim can win: the holder left, was ended,
|
|
51
|
+
* or the backend wants a new try. Settles when `signal` aborts.
|
|
52
|
+
*/
|
|
53
|
+
protected abstract waitForChange(claim: Claim, signal: AbortSignal): Promise<void>;
|
|
42
54
|
/** Watches a won claim for as long as its term lasts, and calls `lose` when the backend takes it away. */
|
|
43
55
|
protected abstract watch(claim: Claim, lose: (reason: Error) => void): Disposable;
|
|
44
56
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"leader-election.d.ts","sourceRoot":"","sources":["../src/leader-election.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,
|
|
1
|
+
{"version":3,"file":"leader-election.d.ts","sourceRoot":"","sources":["../src/leader-election.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAA4B,MAAM,gBAAgB,CAAC;AAEtE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,4GAA4G;IAC5G,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC;CAClC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,8BAAsB,cAAc,CAAC,KAAK;;IACxC,qFAAqF;IACrF,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IAEhC,SAAS,aAAa,KAAK,EAAE,KAA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
4
|
* Elects one leader among candidates. The campaign is the same for each
|
|
5
|
-
* backend: open an attempt, try the claim
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* backend: open an attempt, try the claim, wait for a change that lets a try
|
|
6
|
+
* win, try again until the claim is won or the time is up, watch the won
|
|
7
|
+
* claim, and hand it to a term. Each backend is a subclass that implements
|
|
8
|
+
* the steps for its claim.
|
|
8
9
|
*
|
|
9
|
-
* A backend keeps
|
|
10
|
+
* A backend keeps six rules:
|
|
10
11
|
* - `tryClaim` returns the epoch of the term it won, in the same step, and
|
|
11
12
|
* each epoch is higher than every earlier one and below 2^31.
|
|
12
13
|
* - `tryClaim` reports `afterCleanShutdown` only when the term just before
|
|
@@ -17,13 +18,13 @@ import { Term } from "./term.js";
|
|
|
17
18
|
* - `release` is never called after a loss: the claim may be another
|
|
18
19
|
* candidate's by then.
|
|
19
20
|
* - `close` frees the attempt's resources, after each end.
|
|
21
|
+
* - `waitForChange` settles when its signal aborts, and never ends the
|
|
22
|
+
* attempt itself.
|
|
20
23
|
*/
|
|
21
24
|
export class LeaderElection {
|
|
22
|
-
#pollInterval;
|
|
23
25
|
/** The clock of the campaign: `globalThis` in production, a fake clock in a test. */
|
|
24
26
|
clock;
|
|
25
|
-
constructor(
|
|
26
|
-
this.#pollInterval = pollInterval;
|
|
27
|
+
constructor(clock) {
|
|
27
28
|
this.clock = clock;
|
|
28
29
|
}
|
|
29
30
|
/** Resolves with the new term, or `undefined` when another candidate still leads after `timeout`. */
|
|
@@ -41,12 +42,7 @@ export class LeaderElection {
|
|
|
41
42
|
const claim = await this.open(signal);
|
|
42
43
|
let won;
|
|
43
44
|
try {
|
|
44
|
-
won = await
|
|
45
|
-
interval: this.#pollInterval,
|
|
46
|
-
signal,
|
|
47
|
-
clock: this.clock,
|
|
48
|
-
deadline,
|
|
49
|
-
});
|
|
45
|
+
won = await this.#tryUntil(claim, deadline, signal);
|
|
50
46
|
}
|
|
51
47
|
catch (error) {
|
|
52
48
|
await this.close(claim).catch(() => { });
|
|
@@ -58,6 +54,35 @@ export class LeaderElection {
|
|
|
58
54
|
}
|
|
59
55
|
return this.#begin(claim, won, signal);
|
|
60
56
|
}
|
|
57
|
+
/** Tries the claim, and after each try that does not win, waits for a change, until the deadline gives one last try. */
|
|
58
|
+
async #tryUntil(claim, deadline, signal) {
|
|
59
|
+
for (;;) {
|
|
60
|
+
const won = await this.tryClaim(claim, signal);
|
|
61
|
+
if (won !== undefined)
|
|
62
|
+
return won;
|
|
63
|
+
const left = deadline - this.clock.performance.now();
|
|
64
|
+
if (left <= 0)
|
|
65
|
+
return undefined;
|
|
66
|
+
await this.#waitForChange(claim, left, signal);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Waits for the backend's change, up to `left` milliseconds; rejects with `signal.reason` when the caller leaves. */
|
|
70
|
+
async #waitForChange(claim, left, signal) {
|
|
71
|
+
using limit = left === Infinity
|
|
72
|
+
? undefined
|
|
73
|
+
: deadlineAfter(left, { clock: this.clock });
|
|
74
|
+
const stops = [signal, limit?.signal].filter((stop) => stop !== undefined);
|
|
75
|
+
const wait = AbortSignal.any(stops);
|
|
76
|
+
try {
|
|
77
|
+
await this.waitForChange(claim, wait);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
// After an abort, the backend settles as it can; the deadline gives one last try.
|
|
81
|
+
if (!wait.aborted)
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
signal?.throwIfAborted();
|
|
85
|
+
}
|
|
61
86
|
/** Hands a won claim to its term, or gives it up when the caller left meanwhile. */
|
|
62
87
|
async #begin(claim, won, signal) {
|
|
63
88
|
let term;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"leader-election.js","sourceRoot":"","sources":["../src/leader-election.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,IAAI,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"leader-election.js","sourceRoot":"","sources":["../src/leader-election.ts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I,CAAC,OAAO;gBAAE,MAAM,KAAK,CAAC;QACjC,CAAC;QACD,MAAM,EAAE,cAAc,EAAE,CAAC;IAC3B,CAAC;IAED,oFAAoF;IACpF,KAAK,CAAC,MAAM,CACV,KAAY,EACZ,GAA+C,EAC/C,MAA+B;QAE/B,IAAI,IAAU,CAAC;QACf,IAAI,CAAC;YACH,MAAM,EAAE,cAAc,EAAE,CAAC;YACzB,IAAI,GAAG,IAAI,IAAI,CAAC,GAAG,EAAE;gBACnB,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC;gBACxC,mBAAmB,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,mBAAmB,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC;gBACrE,OAAO,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;gBAClC,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC;aAC/B,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;YAC1C,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;YACxC,MAAM,KAAK,CAAC;QACd,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC;IAC7C,CAAC;CACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Clock } from '@zukhruf/async';
|
|
2
|
+
import { type Left } from './leader-reader.ts';
|
|
3
|
+
/**
|
|
4
|
+
* A reader of the leader's pipe that never tries the claim, after etcd's
|
|
5
|
+
* `Election.Observe` (ADR 0009). One observer reads one term. Neither its
|
|
6
|
+
* connection nor its deadline keeps the process alive.
|
|
7
|
+
*/
|
|
8
|
+
export declare class LeaderObserver implements Disposable {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(recordPath: string, clock: Clock);
|
|
11
|
+
/**
|
|
12
|
+
* Resolves `'closed'` when the leader left, `'ended'` when this observer
|
|
13
|
+
* ended a silent leader and its pipe closed, and `'absent'` when no leader
|
|
14
|
+
* served a pipe. Rejects with any other error. Never settles after a
|
|
15
|
+
* disposal.
|
|
16
|
+
*/
|
|
17
|
+
get left(): Promise<Left | 'absent'>;
|
|
18
|
+
/** Closes the connection and clears the deadline. */
|
|
19
|
+
[Symbol.dispose](): void;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=leader-observer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leader-observer.d.ts","sourceRoot":"","sources":["../../src/sqlite/leader-observer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAS,MAAM,gBAAgB,CAAC;AAEnD,OAAO,EAAE,KAAK,IAAI,EAAc,MAAM,oBAAoB,CAAC;AAM3D;;;;GAIG;AACH,qBAAa,cAAe,YAAW,UAAU;;gBAInC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK;IAe5C;;;;;OAKG;IACH,IAAI,IAAI,IAAI,OAAO,CAAC,IAAI,GAAG,QAAQ,CAAC,CAKnC;IAED,qDAAqD;IACrD,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI;CAGzB"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Latch } from '@zukhruf/async';
|
|
2
|
+
import { readLeader } from "./leader-reader.js";
|
|
3
|
+
/**
|
|
4
|
+
* A reader of the leader's pipe that never tries the claim, after etcd's
|
|
5
|
+
* `Election.Observe` (ADR 0009). One observer reads one term. Neither its
|
|
6
|
+
* connection nor its deadline keeps the process alive.
|
|
7
|
+
*/
|
|
8
|
+
export class LeaderObserver {
|
|
9
|
+
#stop = new AbortController();
|
|
10
|
+
#outcome = new Latch();
|
|
11
|
+
constructor(recordPath, clock) {
|
|
12
|
+
readLeader(recordPath, {
|
|
13
|
+
clock,
|
|
14
|
+
ref: false,
|
|
15
|
+
signal: this.#stop.signal,
|
|
16
|
+
}).then((left) => this.#outcome.open({ failed: false, left }), (error) => {
|
|
17
|
+
// After a disposal, `left` tells nothing.
|
|
18
|
+
if (!this.#stop.signal.aborted)
|
|
19
|
+
this.#outcome.open({ failed: true, error });
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Resolves `'closed'` when the leader left, `'ended'` when this observer
|
|
24
|
+
* ended a silent leader and its pipe closed, and `'absent'` when no leader
|
|
25
|
+
* served a pipe. Rejects with any other error. Never settles after a
|
|
26
|
+
* disposal.
|
|
27
|
+
*/
|
|
28
|
+
get left() {
|
|
29
|
+
return this.#outcome.wait().then((outcome) => {
|
|
30
|
+
if (outcome.failed)
|
|
31
|
+
throw outcome.error;
|
|
32
|
+
return outcome.left;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** Closes the connection and clears the deadline. */
|
|
36
|
+
[Symbol.dispose]() {
|
|
37
|
+
this.#stop.abort();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=leader-observer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leader-observer.js","sourceRoot":"","sources":["../../src/sqlite/leader-observer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEnD,OAAO,EAAa,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAM3D;;;;GAIG;AACH,MAAM,OAAO,cAAc;IAChB,KAAK,GAAG,IAAI,eAAe,EAAE,CAAC;IAC9B,QAAQ,GAAG,IAAI,KAAK,EAAW,CAAC;IAEzC,YAAY,UAAkB,EAAE,KAAY;QAC1C,UAAU,CAAC,UAAU,EAAE;YACrB,KAAK;YACL,GAAG,EAAE,KAAK;YACV,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM;SAC1B,CAAC,CAAC,IAAI,CACL,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACrD,CAAC,KAAc,EAAE,EAAE;YACjB,0CAA0C;YAC1C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO;gBAC5B,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QAChD,CAAC,CACF,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;YAC3C,IAAI,OAAO,CAAC,MAAM;gBAAE,MAAM,OAAO,CAAC,KAAK,CAAC;YACxC,OAAO,OAAO,CAAC,IAAI,CAAC;QACtB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,qDAAqD;IACrD,CAAC,MAAM,CAAC,OAAO,CAAC;QACd,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;CACF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Clock } from '@zukhruf/async';
|
|
2
|
+
/**
|
|
3
|
+
* The pipe that a leader serves for its term (ADR 0004). It sends a
|
|
4
|
+
* heartbeat on each connection when it accepts it, and every
|
|
5
|
+
* `heartbeatInterval` until it closes, so the heartbeat lasts as long as the
|
|
6
|
+
* term. Its close is the push that tells each reader that the leader left.
|
|
7
|
+
* It keeps no process alive.
|
|
8
|
+
*/
|
|
9
|
+
export declare class LeaderPipe {
|
|
10
|
+
#private;
|
|
11
|
+
private constructor();
|
|
12
|
+
/**
|
|
13
|
+
* Listens on a pipe with a plain random name in the default folder of the
|
|
14
|
+
* user, so another user cannot connect, and a name is never used again
|
|
15
|
+
* (ADR 0005).
|
|
16
|
+
*/
|
|
17
|
+
static serve(heartbeatInterval: number, clock: Clock): Promise<LeaderPipe>;
|
|
18
|
+
/** The path that the leader record publishes for the readers. */
|
|
19
|
+
get address(): string;
|
|
20
|
+
/** Stops the heartbeat and listening, and ends each connection, because a server keeps open connections open. */
|
|
21
|
+
close(): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
/** Lets the process exit while the timer waits, when the clock's timer can. */
|
|
24
|
+
export declare function unrefed(timer: unknown): unknown;
|
|
25
|
+
//# sourceMappingURL=leader-pipe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leader-pipe.d.ts","sourceRoot":"","sources":["../../src/sqlite/leader-pipe.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAM5C;;;;;;GAMG;AACH,qBAAa,UAAU;;IAKrB,OAAO;IAUP;;;;OAIG;WACU,KAAK,CAChB,iBAAiB,EAAE,MAAM,EACzB,KAAK,EAAE,KAAK,GACX,OAAO,CAAC,UAAU,CAAC;IAoBtB,iEAAiE;IACjE,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,iHAAiH;IAC3G,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAM7B;AAiBD,+EAA+E;AAC/E,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAU/C"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { PipeEndpoint } from '@zukhruf/pipes';
|
|
3
|
+
/** One byte; a reader finds no meaning in its value. */
|
|
4
|
+
const HEARTBEAT = Buffer.of(0);
|
|
5
|
+
/**
|
|
6
|
+
* The pipe that a leader serves for its term (ADR 0004). It sends a
|
|
7
|
+
* heartbeat on each connection when it accepts it, and every
|
|
8
|
+
* `heartbeatInterval` until it closes, so the heartbeat lasts as long as the
|
|
9
|
+
* term. Its close is the push that tells each reader that the leader left.
|
|
10
|
+
* It keeps no process alive.
|
|
11
|
+
*/
|
|
12
|
+
export class LeaderPipe {
|
|
13
|
+
#server;
|
|
14
|
+
#connections;
|
|
15
|
+
#heartbeat;
|
|
16
|
+
constructor(server, connections, heartbeat) {
|
|
17
|
+
this.#server = server;
|
|
18
|
+
this.#connections = connections;
|
|
19
|
+
this.#heartbeat = heartbeat;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Listens on a pipe with a plain random name in the default folder of the
|
|
23
|
+
* user, so another user cannot connect, and a name is never used again
|
|
24
|
+
* (ADR 0005).
|
|
25
|
+
*/
|
|
26
|
+
static async serve(heartbeatInterval, clock) {
|
|
27
|
+
const connections = new Set();
|
|
28
|
+
const endpoint = new PipeEndpoint(randomBytes(16).toHex(), {
|
|
29
|
+
prefix: 'zukhruf-election',
|
|
30
|
+
});
|
|
31
|
+
const server = await endpoint.listen((socket) => {
|
|
32
|
+
socket.unref();
|
|
33
|
+
// A reader that leaves while a heartbeat is on its way is no failure of the leader.
|
|
34
|
+
socket.on('error', () => { });
|
|
35
|
+
connections.add(socket);
|
|
36
|
+
socket.once('close', () => connections.delete(socket));
|
|
37
|
+
socket.write(HEARTBEAT);
|
|
38
|
+
});
|
|
39
|
+
return new LeaderPipe(server.unref(), connections, heartbeat(connections, heartbeatInterval, clock));
|
|
40
|
+
}
|
|
41
|
+
/** The path that the leader record publishes for the readers. */
|
|
42
|
+
get address() {
|
|
43
|
+
return this.#server.endpoint.address.path;
|
|
44
|
+
}
|
|
45
|
+
/** Stops the heartbeat and listening, and ends each connection, because a server keeps open connections open. */
|
|
46
|
+
async close() {
|
|
47
|
+
this.#heartbeat[Symbol.dispose]();
|
|
48
|
+
const closing = this.#server.close();
|
|
49
|
+
for (const socket of this.#connections)
|
|
50
|
+
socket.destroy();
|
|
51
|
+
await closing;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** Sends a heartbeat on each connection every `interval` milliseconds, until disposed. */
|
|
55
|
+
function heartbeat(connections, interval, clock) {
|
|
56
|
+
let timer;
|
|
57
|
+
const beat = () => {
|
|
58
|
+
for (const socket of connections)
|
|
59
|
+
socket.write(HEARTBEAT);
|
|
60
|
+
timer = unrefed(clock.setTimeout(beat, interval));
|
|
61
|
+
};
|
|
62
|
+
timer = unrefed(clock.setTimeout(beat, interval));
|
|
63
|
+
return { [Symbol.dispose]: () => clock.clearTimeout(timer) };
|
|
64
|
+
}
|
|
65
|
+
/** Lets the process exit while the timer waits, when the clock's timer can. */
|
|
66
|
+
export function unrefed(timer) {
|
|
67
|
+
if (typeof timer === 'object' &&
|
|
68
|
+
timer !== null &&
|
|
69
|
+
'unref' in timer &&
|
|
70
|
+
typeof timer.unref === 'function') {
|
|
71
|
+
timer.unref();
|
|
72
|
+
}
|
|
73
|
+
return timer;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=leader-pipe.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leader-pipe.js","sourceRoot":"","sources":["../../src/sqlite/leader-pipe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAI1C,OAAO,EAAE,YAAY,EAAmB,MAAM,gBAAgB,CAAC;AAE/D,wDAAwD;AACxD,MAAM,SAAS,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAE/B;;;;;;GAMG;AACH,MAAM,OAAO,UAAU;IACZ,OAAO,CAAa;IACpB,YAAY,CAAc;IAC1B,UAAU,CAAa;IAEhC,YACE,MAAkB,EAClB,WAAwB,EACxB,SAAqB;QAErB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;QAChC,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IA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AM,OAAO,CAAC;IAChB,CAAC;CACF;AAED,0FAA0F;AAC1F,SAAS,SAAS,CAChB,WAAwB,EACxB,QAAgB,EAChB,KAAY;IAEZ,IAAI,KAAc,CAAC;IACnB,MAAM,IAAI,GAAG,GAAG,EAAE;QAChB,KAAK,MAAM,MAAM,IAAI,WAAW;YAAE,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAC1D,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;IACpD,CAAC,CAAC;IACF,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;IAClD,OAAO,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;AAC/D,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,OAAO,CAAC,KAAc;IACpC,IACE,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAO,IAAI,KAAK;QAChB,OAAO,KAAK,CAAC,KAAK,KAAK,UAAU,EACjC,CAAC;QACD,KAAK,CAAC,KAAK,EAAE,CAAC;IAChB,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Clock } from '@zukhruf/async';
|
|
2
|
+
/** Why a reader's leader left: it closed its pipe, or the reader ended it and then the pipe closed. */
|
|
3
|
+
export type Left = 'closed' | 'ended';
|
|
4
|
+
export interface ReaderOptions {
|
|
5
|
+
clock: Clock;
|
|
6
|
+
/** With `false`, neither the connection nor the deadline keeps the process alive. */
|
|
7
|
+
ref: boolean;
|
|
8
|
+
/** Stops the read: the connection closes, the deadline is cleared, and the read rejects with `signal.reason`. */
|
|
9
|
+
signal: AbortSignal;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Reads the leader record, connects to its pipe once, and resolves when the
|
|
13
|
+
* leader left. Resolves `'absent'` when no leader serves a pipe.
|
|
14
|
+
*
|
|
15
|
+
* While connected, the reader keeps the deadline of the heartbeat: the
|
|
16
|
+
* record's `leaseDuration` after the connect, and again after each heartbeat
|
|
17
|
+
* (ADR 0004). When it comes, the reader sends `SIGKILL` to the process of the
|
|
18
|
+
* record, only while it holds that process's pipe open, so a process id used
|
|
19
|
+
* again is never hit (ADR 0006), and never to its own process. The read rejects when the kill fails with an
|
|
20
|
+
* error other than `ESRCH` or `EPERM`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function readLeader(recordPath: string, options: ReaderOptions): Promise<Left | 'absent'>;
|
|
23
|
+
//# sourceMappingURL=leader-reader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leader-reader.d.ts","sourceRoot":"","sources":["../../src/sqlite/leader-reader.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAO5C,uGAAuG;AACvG,MAAM,MAAM,IAAI,GAAG,QAAQ,GAAG,OAAO,CAAC;AAEtC,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,KAAK,CAAC;IACb,qFAAqF;IACrF,GAAG,EAAE,OAAO,CAAC;IACb,iHAAiH;IACjH,MAAM,EAAE,WAAW,CAAC;CACrB;AAKD;;;;;;;;;;GAUG;AACH,wBAAsB,UAAU,CAC9B,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,IAAI,GAAG,QAAQ,CAAC,CAa1B"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { addAbortListener } from 'node:events';
|
|
2
|
+
import { isErrno } from '@zukhruf/fs';
|
|
3
|
+
import { PipeAddress } from '@zukhruf/pipes';
|
|
4
|
+
import { unrefed } from "./leader-pipe.js";
|
|
5
|
+
import { readLeaderRecord } from "./leader-record.js";
|
|
6
|
+
/** The connect fails because no process serves the pipe of the record, or the pipe is another user's (ADR 0007). */
|
|
7
|
+
const ABSENT = ['ENOENT', 'ECONNREFUSED', 'EACCES'];
|
|
8
|
+
/**
|
|
9
|
+
* Reads the leader record, connects to its pipe once, and resolves when the
|
|
10
|
+
* leader left. Resolves `'absent'` when no leader serves a pipe.
|
|
11
|
+
*
|
|
12
|
+
* While connected, the reader keeps the deadline of the heartbeat: the
|
|
13
|
+
* record's `leaseDuration` after the connect, and again after each heartbeat
|
|
14
|
+
* (ADR 0004). When it comes, the reader sends `SIGKILL` to the process of the
|
|
15
|
+
* record, only while it holds that process's pipe open, so a process id used
|
|
16
|
+
* again is never hit (ADR 0006), and never to its own process. The read rejects when the kill fails with an
|
|
17
|
+
* error other than `ESRCH` or `EPERM`.
|
|
18
|
+
*/
|
|
19
|
+
export async function readLeader(recordPath, options) {
|
|
20
|
+
const record = await readLeaderRecord(recordPath);
|
|
21
|
+
if (record === undefined)
|
|
22
|
+
return 'absent';
|
|
23
|
+
let socket;
|
|
24
|
+
try {
|
|
25
|
+
socket = await new PipeAddress(record.address).connect({
|
|
26
|
+
signal: options.signal,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (ABSENT.some((code) => isErrno(error, code)))
|
|
31
|
+
return 'absent';
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
return untilLeft(socket, record, options);
|
|
35
|
+
}
|
|
36
|
+
function untilLeft(socket, { processId, leaseDuration }, { clock, ref, signal }) {
|
|
37
|
+
if (signal.aborted) {
|
|
38
|
+
socket.destroy();
|
|
39
|
+
return Promise.reject(signal.reason);
|
|
40
|
+
}
|
|
41
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
42
|
+
let timer;
|
|
43
|
+
/** The kill was sent, or the process had ended: only the close is left to wait for. */
|
|
44
|
+
let ended = false;
|
|
45
|
+
/** The kernel refused the kill: the reader sets no new deadline. */
|
|
46
|
+
let refused = false;
|
|
47
|
+
const stop = () => {
|
|
48
|
+
clock.clearTimeout(timer);
|
|
49
|
+
socket.destroy();
|
|
50
|
+
aborting[Symbol.dispose]();
|
|
51
|
+
};
|
|
52
|
+
const expire = () => {
|
|
53
|
+
if (socket.destroyed)
|
|
54
|
+
return;
|
|
55
|
+
// The record names the reader's own process, which runs this code and so
|
|
56
|
+
// is not hung: a kill would end the reader with its leader. Refused, as a
|
|
57
|
+
// kill of another user's process is.
|
|
58
|
+
if (processId === process.pid) {
|
|
59
|
+
refused = true;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
process.kill(processId, 'SIGKILL');
|
|
64
|
+
ended = true;
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
// The process ended already: its close comes, or came.
|
|
68
|
+
if (isErrno(error, 'ESRCH'))
|
|
69
|
+
ended = true;
|
|
70
|
+
// Another user's process: the leader leads until it runs again or its user ends it.
|
|
71
|
+
else if (isErrno(error, 'EPERM'))
|
|
72
|
+
refused = true;
|
|
73
|
+
else {
|
|
74
|
+
stop();
|
|
75
|
+
reject(error);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const arm = () => {
|
|
80
|
+
if (ended || refused)
|
|
81
|
+
return;
|
|
82
|
+
clock.clearTimeout(timer);
|
|
83
|
+
timer = clock.setTimeout(expire, leaseDuration);
|
|
84
|
+
if (!ref)
|
|
85
|
+
unrefed(timer);
|
|
86
|
+
};
|
|
87
|
+
const close = () => {
|
|
88
|
+
stop();
|
|
89
|
+
resolve(ended ? 'ended' : 'closed');
|
|
90
|
+
};
|
|
91
|
+
const aborting = addAbortListener(signal, () => {
|
|
92
|
+
stop();
|
|
93
|
+
reject(signal.reason);
|
|
94
|
+
});
|
|
95
|
+
if (!ref)
|
|
96
|
+
socket.unref();
|
|
97
|
+
socket.on('data', arm);
|
|
98
|
+
socket.once('end', close);
|
|
99
|
+
socket.once('close', close);
|
|
100
|
+
socket.on('error', close);
|
|
101
|
+
arm();
|
|
102
|
+
return promise;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=leader-reader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leader-reader.js","sourceRoot":"","sources":["../../src/sqlite/leader-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAI/C,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAqB,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAazE,oHAAoH;AACpH,MAAM,MAAM,GAAG,CAAC,QAAQ,EAAE,cAAc,EAAE,QAAQ,CAAC,CAAC;AAEpD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,UAAkB,EAClB,OAAsB;IAEtB,MAAM,MAAM,GAAG,MAAM,gBAAgB,CAAC,UAAU,CAAC,CAAC;IAClD,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAC;IAC1C,IAAI,MAAc,CAAC;IACnB,IAAI,CAAC;QACH,MAAM,GAAG,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC;YACrD,MAAM,EAAE,OAAO,CAAC,MAAM;SACvB,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;YAAE,OAAO,QAAQ,CAAC;QACjE,MAAM,KAAK,CAAC;IACd,CAAC;IACD,OAAO,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AAC5C,CAAC;A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AAG,IAAI,CAAC;iBAC5C,CAAC;gBACJ,IAAI,EAAE,CAAC;gBACP,MAAM,CAAC,KAAK,CAAC,CAAC;YAChB,CAAC;QACH,CAAC;IACH,CAAC,CAAC;IACF,MAAM,GAAG,GAAG,GAAG,EAAE;QACf,IAAI,KAAK,IAAI,OAAO;YAAE,OAAO;QAC7B,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QAC1B,KAAK,GAAG,KAAK,CAAC,UAAU,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;QAChD,IAAI,CAAC,GAAG;YAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC,CAAC;IACF,MAAM,KAAK,GAAG,GAAG,EAAE;QACjB,IAAI,EAAE,CAAC;QACP,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;IACtC,CAAC,CAAC;IACF,MAAM,QAAQ,GAAG,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;QAC7C,IAAI,EAAE,CAAC;QACP,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC,CAAC,CAAC;IACH,IAAI,CAAC,GAAG;QAAE,MAAM,CAAC,KAAK,EAAE,CAAC;IACzB,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACvB,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAC1B,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAC5B,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAC1B,GAAG,EAAE,CAAC;IACN,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Clock } from '@zukhruf/async';
|
|
2
|
+
/** What a reader must know about the leader: which pipe to connect to, how long a silence to accept, and which process to end (ADR 0005). */
|
|
3
|
+
export interface LeaderRecord {
|
|
4
|
+
/** The epoch of the term that the record is about. */
|
|
5
|
+
epoch: bigint;
|
|
6
|
+
/** `process.pid` of the leader. */
|
|
7
|
+
processId: number;
|
|
8
|
+
/** The path of the leader's pipe. */
|
|
9
|
+
address: string;
|
|
10
|
+
/** The longest silence, in milliseconds, that a reader accepts from this leader. */
|
|
11
|
+
leaseDuration: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Replaces the record in one step, so a reader never sees a part of it. It is
|
|
15
|
+
* not synced to the disk: after a power loss, no process of its term lives.
|
|
16
|
+
*/
|
|
17
|
+
export declare function writeLeaderRecord(path: string, { epoch, processId, address, leaseDuration }: LeaderRecord, clock: Clock): Promise<void>;
|
|
18
|
+
/** The record of the last leader that wrote one, or `undefined` when no leader did. */
|
|
19
|
+
export declare function readLeaderRecord(path: string): Promise<LeaderRecord | undefined>;
|
|
20
|
+
//# sourceMappingURL=leader-record.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leader-record.d.ts","sourceRoot":"","sources":["../../src/sqlite/leader-record.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAG5C,6IAA6I;AAC7I,MAAM,WAAW,YAAY;IAC3B,sDAAsD;IACtD,KAAK,EAAE,MAAM,CAAC;IACd,mCAAmC;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,qCAAqC;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,oFAAoF;IACpF,aAAa,EAAE,MAAM,CAAC;CACvB;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,MAAM,EACZ,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,aAAa,EAAE,EAAE,YAAY,EAC1D,KAAK,EAAE,KAAK,GACX,OAAO,CAAC,IAAI,CAAC,CAQf;AAED,uFAAuF;AACvF,wBAAsB,gBAAgB,CACpC,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,YAAY,GAAG,SAAS,CAAC,CAiCnC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { atomicWrite, isErrno } from '@zukhruf/fs';
|
|
3
|
+
/**
|
|
4
|
+
* Replaces the record in one step, so a reader never sees a part of it. It is
|
|
5
|
+
* not synced to the disk: after a power loss, no process of its term lives.
|
|
6
|
+
*/
|
|
7
|
+
export async function writeLeaderRecord(path, { epoch, processId, address, leaseDuration }, clock) {
|
|
8
|
+
const text = JSON.stringify({
|
|
9
|
+
epoch: epoch.toString(),
|
|
10
|
+
processId,
|
|
11
|
+
address,
|
|
12
|
+
leaseDuration,
|
|
13
|
+
});
|
|
14
|
+
await atomicWrite(path, text, { clock });
|
|
15
|
+
}
|
|
16
|
+
/** The record of the last leader that wrote one, or `undefined` when no leader did. */
|
|
17
|
+
export async function readLeaderRecord(path) {
|
|
18
|
+
let text;
|
|
19
|
+
try {
|
|
20
|
+
text = await readFile(path, 'utf8');
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
if (isErrno(error, 'ENOENT'))
|
|
24
|
+
return undefined;
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
const record = JSON.parse(text);
|
|
28
|
+
if (typeof record === 'object' &&
|
|
29
|
+
record !== null &&
|
|
30
|
+
'epoch' in record &&
|
|
31
|
+
typeof record.epoch === 'string' &&
|
|
32
|
+
/^\d+$/.test(record.epoch) &&
|
|
33
|
+
'processId' in record &&
|
|
34
|
+
Number.isSafeInteger(record.processId) &&
|
|
35
|
+
'address' in record &&
|
|
36
|
+
typeof record.address === 'string' &&
|
|
37
|
+
'leaseDuration' in record &&
|
|
38
|
+
typeof record.leaseDuration === 'number' &&
|
|
39
|
+
record.leaseDuration > 0) {
|
|
40
|
+
return {
|
|
41
|
+
epoch: BigInt(record.epoch),
|
|
42
|
+
processId: Number(record.processId),
|
|
43
|
+
address: record.address,
|
|
44
|
+
leaseDuration: record.leaseDuration,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
throw new SyntaxError(`The leader record ${path} holds ${JSON.stringify(text)}, not a leader record.`);
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=leader-record.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leader-record.js","sourceRoot":"","sources":["../../src/sqlite/leader-record.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAG5C,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAcnD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,IAAY,EACZ,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,aAAa,EAAgB,EAC1D,KAAY;IAEZ,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC;QAC1B,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE;QACvB,SAAS;QACT,OAAO;QACP,aAAa;KACd,CAAC,CAAC;IACH,MAAM,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;AAC3C,CAAC;AAED,uFAAuF;AACvF,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,IAAY;IAEZ,IAAI,IAAY,CAAC;IACjB,IAAI,CAAC;QACH,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACtC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC;YAAE,OAAO,SAAS,CAAC;QAC/C,MAAM,KAAK,CAAC;IACd,CAAC;IACD,MAAM,MAAM,GAAY,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACzC,IACE,OAAO,MAAM,KAAK,QAAQ;QAC1B,MAAM,KAAK,IAAI;QACf,OAAO,IAAI,MAAM;QACjB,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ;QAChC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;QAC1B,WAAW,IAAI,MAAM;QACrB,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,SAAS,CAAC;QACtC,SAAS,IAAI,MAAM;QACnB,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ;QAClC,eAAe,IAAI,MAAM;QACzB,OAAO,MAAM,CAAC,aAAa,KAAK,QAAQ;QACxC,MAAM,CAAC,aAAa,GAAG,CAAC,EACxB,CAAC;QACD,OAAO;YACL,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;YAC3B,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC;YACnC,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,aAAa,EAAE,MAAM,CAAC,aAAa;SACpC,CAAC;IACJ,CAAC;IACD,MAAM,IAAI,WAAW,CACnB,qBAAqB,IAAI,UAAU,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,wBAAwB,CAChF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { FileLock } from '@zukhruf/fs';
|
|
2
|
+
import type { LeaderPipe } from './leader-pipe.ts';
|
|
3
|
+
/** One attempt of a candidate: the file lock, the pipe that it serves after a win, and the count of its back-off waits. */
|
|
4
|
+
export declare class SqliteAttempt {
|
|
5
|
+
#private;
|
|
6
|
+
readonly lock: FileLock;
|
|
7
|
+
/** The waits of the back-off since the last connect that succeeded. */
|
|
8
|
+
retries: number;
|
|
9
|
+
constructor(lock: FileLock);
|
|
10
|
+
/** The claim is won: the attempt serves `pipe` until it closes. */
|
|
11
|
+
lead(pipe: LeaderPipe): void;
|
|
12
|
+
/** Frees the lock first, so a reader that the close of the pipe wakes finds the claim free (ADR 0007). */
|
|
13
|
+
close(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=sqlite-attempt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-attempt.d.ts","sourceRoot":"","sources":["../../src/sqlite/sqlite-attempt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAuBnD,2HAA2H;AAC3H,qBAAa,aAAa;;IACxB,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,uEAAuE;IACvE,OAAO,SAAK;gBAGA,IAAI,EAAE,QAAQ;IAI1B,mEAAmE;IACnE,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI;IAI5B,0GAA0G;IACpG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAI7B"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class Campaigning {
|
|
2
|
+
async close() { }
|
|
3
|
+
}
|
|
4
|
+
class Leading {
|
|
5
|
+
#pipe;
|
|
6
|
+
constructor(pipe) {
|
|
7
|
+
this.#pipe = pipe;
|
|
8
|
+
}
|
|
9
|
+
close() {
|
|
10
|
+
return this.#pipe.close();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** One attempt of a candidate: the file lock, the pipe that it serves after a win, and the count of its back-off waits. */
|
|
14
|
+
export class SqliteAttempt {
|
|
15
|
+
lock;
|
|
16
|
+
/** The waits of the back-off since the last connect that succeeded. */
|
|
17
|
+
retries = 0;
|
|
18
|
+
#phase = new Campaigning();
|
|
19
|
+
constructor(lock) {
|
|
20
|
+
this.lock = lock;
|
|
21
|
+
}
|
|
22
|
+
/** The claim is won: the attempt serves `pipe` until it closes. */
|
|
23
|
+
lead(pipe) {
|
|
24
|
+
this.#phase = new Leading(pipe);
|
|
25
|
+
}
|
|
26
|
+
/** Frees the lock first, so a reader that the close of the pipe wakes finds the claim free (ADR 0007). */
|
|
27
|
+
async close() {
|
|
28
|
+
this.lock[Symbol.dispose]();
|
|
29
|
+
await this.#phase.close();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=sqlite-attempt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-attempt.js","sourceRoot":"","sources":["../../src/sqlite/sqlite-attempt.ts"],"names":[],"mappings":"AASA,MAAM,WAAW;IACf,KAAK,CAAC,KAAK,KAAmB,CAAC;CAChC;AAED,MAAM,OAAO;IACF,KAAK,CAAa;IAE3B,YAAY,IAAgB;QAC1B,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IAC5B,CAAC;CACF;AAED,2HAA2H;AAC3H,MAAM,OAAO,aAAa;IACf,IAAI,CAAW;IACxB,uEAAuE;IACvE,OAAO,GAAG,CAAC,CAAC;IACZ,MAAM,GAAU,IAAI,WAAW,EAAE,CAAC;IAElC,YAAY,IAAc;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,mEAAmE;IACnE,IAAI,CAAC,IAAgB;QACnB,IAAI,CAAC,MAAM,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC;IAED,0GAA0G;IAC1G,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;QAC5B,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC5B,CAAC;CACF"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { FileLock } from '@zukhruf/fs';
|
|
1
|
+
import { type Clock } from '@zukhruf/async';
|
|
3
2
|
import { LeaderElection } from '../leader-election.ts';
|
|
4
3
|
import type { Term } from '../term.ts';
|
|
4
|
+
import { LeaderObserver } from './leader-observer.ts';
|
|
5
|
+
import { SqliteAttempt } from './sqlite-attempt.ts';
|
|
5
6
|
export interface SqliteElectionOptions {
|
|
6
7
|
/** The local folder where the candidates meet. */
|
|
7
8
|
directory: string;
|
|
@@ -9,25 +10,38 @@ export interface SqliteElectionOptions {
|
|
|
9
10
|
* The file in `directory` whose file lock is the claim.
|
|
10
11
|
* Never delete it while candidates run: a new file would let a second
|
|
11
12
|
* leader win. A term that resigns clean writes `<claimFile>.clean` beside
|
|
12
|
-
* it, and the next term reads and removes it.
|
|
13
|
+
* it, and the next term reads and removes it. Each leader writes its
|
|
14
|
+
* record to `<claimFile>.leader`.
|
|
13
15
|
*/
|
|
14
16
|
claimFile: string;
|
|
15
17
|
/** The file in `directory` that records the epoch of the last term, as decimal text. */
|
|
16
18
|
epochFile: string;
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
/**
|
|
20
|
+
* The longest silence, in milliseconds, that a candidate or an observer
|
|
21
|
+
* accepts from this election's leader before it ends that leader. The
|
|
22
|
+
* leader record holds it, so a reader uses the leader's value. Defaults to
|
|
23
|
+
* 15000, as Kubernetes' `LeaseDuration`.
|
|
24
|
+
*/
|
|
25
|
+
leaseDuration?: number | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Milliseconds between two heartbeats of a leader. Defaults to a third of
|
|
28
|
+
* `leaseDuration`, as etcd's client sends a keepalive every TTL / 3.
|
|
29
|
+
*/
|
|
30
|
+
heartbeatInterval?: number | undefined;
|
|
31
|
+
/** The clock that the campaign waits on, and that times the heartbeats and their deadline: `globalThis` in production, a fake clock in a test. */
|
|
20
32
|
clock: Clock;
|
|
21
33
|
}
|
|
22
34
|
/**
|
|
23
35
|
* Elects one leader among the processes of one host that share `directory`.
|
|
24
36
|
* The claim is the file lock of `claimFile`, held for the whole term. The
|
|
25
|
-
* kernel keeps that lock until the leader's process dies, so a
|
|
26
|
-
*
|
|
37
|
+
* kernel keeps that lock until the leader's process dies, so a dead leader
|
|
38
|
+
* frees it at once. A leader serves a pipe and pushes a heartbeat on it; a
|
|
39
|
+
* candidate waits on that pipe, and ends a leader that stays silent past its
|
|
40
|
+
* lease duration (ADR 0004 to 0007).
|
|
27
41
|
*/
|
|
28
|
-
export declare class SqliteElection extends LeaderElection<
|
|
42
|
+
export declare class SqliteElection extends LeaderElection<SqliteAttempt> {
|
|
29
43
|
#private;
|
|
30
|
-
constructor({ directory, claimFile, epochFile,
|
|
44
|
+
constructor({ directory, claimFile, epochFile, leaseDuration, heartbeatInterval, clock, }: SqliteElectionOptions);
|
|
31
45
|
/**
|
|
32
46
|
* The epoch of the last term on disk, or 0 when no term was won yet. It
|
|
33
47
|
* only reads the epoch file: it does not campaign, take the claim, or
|
|
@@ -35,17 +49,34 @@ export declare class SqliteElection extends LeaderElection<FileLock> {
|
|
|
35
49
|
* can already be stale.
|
|
36
50
|
*/
|
|
37
51
|
lastEpoch(): Promise<bigint>;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Observes the leader of this term without campaigning, after etcd's
|
|
54
|
+
* `Election.Observe`. The observer ends a leader that stays silent past
|
|
55
|
+
* its lease duration, as a waiting candidate does.
|
|
56
|
+
*/
|
|
57
|
+
observe(): LeaderObserver;
|
|
58
|
+
protected open(): Promise<SqliteAttempt>;
|
|
59
|
+
protected tryClaim(attempt: SqliteAttempt): Promise<Pick<Term, 'epoch' | 'afterCleanShutdown'> | undefined>;
|
|
60
|
+
/**
|
|
61
|
+
* Waits on the leader's pipe: its close, or the deadline of its heartbeat,
|
|
62
|
+
* which ends the leader first. With no pipe to connect to, a new leader is
|
|
63
|
+
* starting or a published one leads, so it waits the back-off (ADR 0007).
|
|
64
|
+
*/
|
|
65
|
+
protected waitForChange(attempt: SqliteAttempt, signal: AbortSignal): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* The kernel holds the claim for as long as the process lives, so nothing
|
|
68
|
+
* takes it away. The heartbeat is no part of the watch: the term's pipe
|
|
69
|
+
* sends it from the win until the term closes (ADR 0004), also under a
|
|
70
|
+
* subclass that overrides `watch`.
|
|
71
|
+
*/
|
|
41
72
|
protected watch(): Disposable;
|
|
42
73
|
/**
|
|
43
74
|
* A lost note costs the next term only its recovery, so the note needs no
|
|
44
75
|
* sync to the disk: an atomic replace is enough, and a reader never sees a
|
|
45
76
|
* part of it.
|
|
46
77
|
*/
|
|
47
|
-
protected recordCleanShutdown(
|
|
48
|
-
protected release(
|
|
49
|
-
protected close(
|
|
78
|
+
protected recordCleanShutdown(_attempt: SqliteAttempt, epoch: bigint): Promise<void>;
|
|
79
|
+
protected release(attempt: SqliteAttempt): Promise<void>;
|
|
80
|
+
protected close(attempt: SqliteAttempt): Promise<void>;
|
|
50
81
|
}
|
|
51
82
|
//# sourceMappingURL=sqlite-election.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite-election.d.ts","sourceRoot":"","sources":["../../src/sqlite/sqlite-election.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"sqlite-election.d.ts","sourceRoot":"","sources":["../../src/sqlite/sqlite-election.ts"],"names":[],"mappings":"AAGA,OAAO,EAAW,KAAK,KAAK,EAAS,MAAM,gBAAgB,CAAC;AAS5D,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAItD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,WAAW,qBAAqB;IACpC,kDAAkD;IAClD,SAAS,EAAE,MAAM,CAAC;IAClB;;;;;;OAMG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB,wFAAwF;IACxF,SAAS,EAAE,MAAM,CAAC;IAClB;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,kJAAkJ;IAClJ,KAAK,EAAE,KAAK,CAAC;CACd;AAaD;;;;;;;GAOG;AACH,qBAAa,cAAe,SAAQ,cAAc,CAAC,aAAa,CAAC;;gBASnD,EACV,SAAS,EACT,SAAS,EACT,SAAS,EACT,aAAsB,EACtB,iBAAqC,EACrC,KAAK,GACN,EAAE,qBAAqB;IAgBxB;;;;;OAKG;IACH,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC;IAI5B;;;;OAIG;IACH,OAAO,IAAI,cAAc;cAIT,IAAI,IAAI,OAAO,CAAC,aAAa,CAAC;cAM9B,QAAQ,CACtB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,GAAG,oBAAoB,CAAC,GAAG,SAAS,CAAC;IA6ClE;;;;OAIG;cACa,aAAa,CAC3B,OAAO,EAAE,aAAa,EACtB,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,IAAI,CAAC;IAgBhB;;;;;OAKG;IACH,SAAS,CAAC,KAAK,IAAI,UAAU;IAI7B;;;;OAIG;cACa,mBAAmB,CACjC,QAAQ,EAAE,aAAa,EACvB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC;cAMA,OAAO,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAI9D,SAAS,CAAC,KAAK,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAGvD"}
|
|
@@ -1,24 +1,51 @@
|
|
|
1
1
|
import { mkdir, readFile, rm } from 'node:fs/promises';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
|
+
import { Backoff, delay } from '@zukhruf/async';
|
|
3
4
|
import { FileLock, assertLocalDirectory, atomicWrite, durableWrite, isErrno, } from '@zukhruf/fs';
|
|
4
5
|
import { LeaderElection } from "../leader-election.js";
|
|
6
|
+
import { LeaderObserver } from "./leader-observer.js";
|
|
7
|
+
import { LeaderPipe } from "./leader-pipe.js";
|
|
8
|
+
import { readLeader } from "./leader-reader.js";
|
|
9
|
+
import { readLeaderRecord, writeLeaderRecord } from "./leader-record.js";
|
|
10
|
+
import { SqliteAttempt } from "./sqlite-attempt.js";
|
|
11
|
+
/**
|
|
12
|
+
* The wait after a connect that found no pipe: etcd's first retry of 25 ms,
|
|
13
|
+
* gRPC's multiplier of 1.6 and jitter of ±20 %, at most 1000 ms (ADR 0007).
|
|
14
|
+
*/
|
|
15
|
+
const backoff = new Backoff({
|
|
16
|
+
initial: 25,
|
|
17
|
+
multiplier: 1.6,
|
|
18
|
+
max: 1000,
|
|
19
|
+
jitter: { kind: 'ratio', ratio: 0.2 },
|
|
20
|
+
});
|
|
5
21
|
/**
|
|
6
22
|
* Elects one leader among the processes of one host that share `directory`.
|
|
7
23
|
* The claim is the file lock of `claimFile`, held for the whole term. The
|
|
8
|
-
* kernel keeps that lock until the leader's process dies, so a
|
|
9
|
-
*
|
|
24
|
+
* kernel keeps that lock until the leader's process dies, so a dead leader
|
|
25
|
+
* frees it at once. A leader serves a pipe and pushes a heartbeat on it; a
|
|
26
|
+
* candidate waits on that pipe, and ends a leader that stays silent past its
|
|
27
|
+
* lease duration (ADR 0004 to 0007).
|
|
10
28
|
*/
|
|
11
29
|
export class SqliteElection extends LeaderElection {
|
|
12
30
|
#directory;
|
|
13
31
|
#claimPath;
|
|
14
32
|
#epochPath;
|
|
15
33
|
#cleanShutdownPath;
|
|
16
|
-
|
|
17
|
-
|
|
34
|
+
#recordPath;
|
|
35
|
+
#leaseDuration;
|
|
36
|
+
#heartbeatInterval;
|
|
37
|
+
constructor({ directory, claimFile, epochFile, leaseDuration = 15_000, heartbeatInterval = leaseDuration / 3, clock, }) {
|
|
38
|
+
super(clock);
|
|
39
|
+
if (!(heartbeatInterval > 0 && heartbeatInterval < leaseDuration)) {
|
|
40
|
+
throw new RangeError(`heartbeatInterval (${heartbeatInterval}) must be above 0 and below leaseDuration (${leaseDuration}).`);
|
|
41
|
+
}
|
|
18
42
|
this.#directory = directory;
|
|
19
43
|
this.#claimPath = join(directory, claimFile);
|
|
20
44
|
this.#epochPath = join(directory, epochFile);
|
|
21
45
|
this.#cleanShutdownPath = `${this.#claimPath}.clean`;
|
|
46
|
+
this.#recordPath = `${this.#claimPath}.leader`;
|
|
47
|
+
this.#leaseDuration = leaseDuration;
|
|
48
|
+
this.#heartbeatInterval = heartbeatInterval;
|
|
22
49
|
}
|
|
23
50
|
/**
|
|
24
51
|
* The epoch of the last term on disk, or 0 when no term was won yet. It
|
|
@@ -29,13 +56,21 @@ export class SqliteElection extends LeaderElection {
|
|
|
29
56
|
lastEpoch() {
|
|
30
57
|
return readEpoch(this.#epochPath);
|
|
31
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Observes the leader of this term without campaigning, after etcd's
|
|
61
|
+
* `Election.Observe`. The observer ends a leader that stays silent past
|
|
62
|
+
* its lease duration, as a waiting candidate does.
|
|
63
|
+
*/
|
|
64
|
+
observe() {
|
|
65
|
+
return new LeaderObserver(this.#recordPath, this.clock);
|
|
66
|
+
}
|
|
32
67
|
async open() {
|
|
33
68
|
await assertLocalDirectory(this.#directory);
|
|
34
69
|
await mkdir(this.#directory, { recursive: true });
|
|
35
|
-
return FileLock.open(this.#claimPath);
|
|
70
|
+
return new SqliteAttempt(FileLock.open(this.#claimPath));
|
|
36
71
|
}
|
|
37
|
-
async tryClaim(
|
|
38
|
-
if (!
|
|
72
|
+
async tryClaim(attempt) {
|
|
73
|
+
if (!attempt.lock.tryLock())
|
|
39
74
|
return undefined;
|
|
40
75
|
// Safe without further locking: only the holder of the claim gets here.
|
|
41
76
|
const previous = await readEpoch(this.#epochPath);
|
|
@@ -49,9 +84,58 @@ export class SqliteElection extends LeaderElection {
|
|
|
49
84
|
});
|
|
50
85
|
// Read once: if the epochs ever start again, an old note could name one of them.
|
|
51
86
|
await rm(this.#cleanShutdownPath, { force: true });
|
|
87
|
+
await this.#removePreviousPipe();
|
|
88
|
+
// The record names a pipe that listens, or listened (ADR 0005).
|
|
89
|
+
const pipe = await LeaderPipe.serve(this.#heartbeatInterval, this.clock);
|
|
90
|
+
attempt.lead(pipe);
|
|
91
|
+
await writeLeaderRecord(this.#recordPath, {
|
|
92
|
+
epoch,
|
|
93
|
+
processId: process.pid,
|
|
94
|
+
address: pipe.address,
|
|
95
|
+
leaseDuration: this.#leaseDuration,
|
|
96
|
+
}, this.clock);
|
|
52
97
|
return { epoch, afterCleanShutdown };
|
|
53
98
|
}
|
|
54
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* Removes the socket file of the previous term's pipe, which a killed
|
|
101
|
+
* leader leaves behind, as @zukhruf/sqlite-notify's sender removes the file
|
|
102
|
+
* of a dead listener. Only the holder of the claim gets here, so no process
|
|
103
|
+
* serves that pipe any more. On Windows a pipe goes with its process, and
|
|
104
|
+
* there is no file to remove (ADR 0005).
|
|
105
|
+
*/
|
|
106
|
+
async #removePreviousPipe() {
|
|
107
|
+
if (process.platform === 'win32')
|
|
108
|
+
return;
|
|
109
|
+
const previous = await readLeaderRecord(this.#recordPath);
|
|
110
|
+
if (previous !== undefined)
|
|
111
|
+
await rm(previous.address, { force: true });
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Waits on the leader's pipe: its close, or the deadline of its heartbeat,
|
|
115
|
+
* which ends the leader first. With no pipe to connect to, a new leader is
|
|
116
|
+
* starting or a published one leads, so it waits the back-off (ADR 0007).
|
|
117
|
+
*/
|
|
118
|
+
async waitForChange(attempt, signal) {
|
|
119
|
+
const left = await readLeader(this.#recordPath, {
|
|
120
|
+
clock: this.clock,
|
|
121
|
+
ref: true,
|
|
122
|
+
signal,
|
|
123
|
+
});
|
|
124
|
+
if (left === 'absent') {
|
|
125
|
+
await delay(backoff.duration(attempt.retries++), {
|
|
126
|
+
clock: this.clock,
|
|
127
|
+
signal,
|
|
128
|
+
});
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
attempt.retries = 0;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The kernel holds the claim for as long as the process lives, so nothing
|
|
135
|
+
* takes it away. The heartbeat is no part of the watch: the term's pipe
|
|
136
|
+
* sends it from the win until the term closes (ADR 0004), also under a
|
|
137
|
+
* subclass that overrides `watch`.
|
|
138
|
+
*/
|
|
55
139
|
watch() {
|
|
56
140
|
return { [Symbol.dispose]() { } };
|
|
57
141
|
}
|
|
@@ -60,16 +144,16 @@ export class SqliteElection extends LeaderElection {
|
|
|
60
144
|
* sync to the disk: an atomic replace is enough, and a reader never sees a
|
|
61
145
|
* part of it.
|
|
62
146
|
*/
|
|
63
|
-
async recordCleanShutdown(
|
|
147
|
+
async recordCleanShutdown(_attempt, epoch) {
|
|
64
148
|
await atomicWrite(this.#cleanShutdownPath, epoch.toString(), {
|
|
65
149
|
clock: this.clock,
|
|
66
150
|
});
|
|
67
151
|
}
|
|
68
|
-
async release(
|
|
69
|
-
|
|
152
|
+
async release(attempt) {
|
|
153
|
+
attempt.lock.unlock();
|
|
70
154
|
}
|
|
71
|
-
|
|
72
|
-
|
|
155
|
+
close(attempt) {
|
|
156
|
+
return attempt.close();
|
|
73
157
|
}
|
|
74
158
|
}
|
|
75
159
|
/** The epoch that the note names, as its text, or `undefined` when there is no note. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite-election.js","sourceRoot":"","sources":["../../src/sqlite/sqlite-election.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"sqlite-election.js","sourceRoot":"","sources":["../../src/sqlite/sqlite-election.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE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AU,SAAS,CAAC,IAAY;IACnC,IAAI,IAAY,CAAC;IACjB,IAAI,CAAC;QACH,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACtC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC;YAAE,OAAO,EAAE,CAAC;QACxC,MAAM,KAAK,CAAC;IACd,CAAC;IACD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;QACrB,MAAM,IAAI,WAAW,CACnB,kBAAkB,IAAI,UAAU,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,uBAAuB,CAC5E,CAAC;IACJ,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC;AACtB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/election",
|
|
3
|
-
"version": "0.8.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.8.19",
|
|
4
|
+
"description": "Elects a leader whose term ends when it resigns or dies, with a higher epoch each term; backends subclass one campaign. SqliteElection, on one host: the leader pushes heartbeats on a pipe; candidates and observers wait on it and end a frozen leader.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -40,12 +40,13 @@
|
|
|
40
40
|
"access": "public"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@zukhruf/async": "^0.8.
|
|
44
|
-
"@zukhruf/fs": "^0.8.
|
|
45
|
-
"@zukhruf/lease": "^0.8.
|
|
43
|
+
"@zukhruf/async": "^0.8.19",
|
|
44
|
+
"@zukhruf/fs": "^0.8.19",
|
|
45
|
+
"@zukhruf/lease": "^0.8.19",
|
|
46
|
+
"@zukhruf/pipes": "^0.8.19"
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
48
49
|
"@sinonjs/fake-timers": "15.4.0",
|
|
49
|
-
"@zukhruf/testing": "^0.8.
|
|
50
|
+
"@zukhruf/testing": "^0.8.19"
|
|
50
51
|
}
|
|
51
52
|
}
|