@zukhruf/election 0.8.43 → 0.8.44
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 +4 -3
- package/dist/leader-election.d.ts +6 -1
- package/dist/leader-election.d.ts.map +1 -1
- package/dist/leader-election.js +12 -4
- package/dist/leader-election.js.map +1 -1
- package/dist/term.d.ts +4 -2
- package/dist/term.d.ts.map +1 -1
- package/dist/term.js +13 -5
- package/dist/term.js.map +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -93,7 +93,7 @@ await using term = await election.campaign({ timeout: Infinity, signal });
|
|
|
93
93
|
A term is a lease on leadership. `Term` implements the `Lease` interface of [`@zukhruf/lease`](../lease/README.md), so a function that takes a `Lease` also takes a term. See the glossary of [leases](../lease/CONTEXT.md).
|
|
94
94
|
|
|
95
95
|
- `term.epoch` is the number of the term. Each epoch is below 2^31.
|
|
96
|
-
- `term.resign()` ends the term and gives the claim up. A second call waits for the first, and its options do nothing. `await using` resigns at the end of its scope.
|
|
96
|
+
- `term.resign()` ends the term and gives the claim up. A second call waits for the first, and its options do nothing. `await using` resigns at the end of its scope. When the release fails, the claim is still freed, and `resign` rejects with the release's error. When the release or the record of `clean` fails and freeing the claim fails too, `resign` rejects with a `SuppressedError`: its `suppressed` is that step's error, and its `error` is the failure to free. After a loss, `resign` never rejects: a failure to free the claim has no caller to go to, so it is a process warning (`process.emitWarning`).
|
|
97
97
|
- `term.resign({ clean: true })` records a clean shutdown for the next term, and then ends the term. Use it only when the leader leaves nothing for the next term to recover, for example no work in progress. A lost term records nothing, also with `clean`. When the record fails, the claim is freed, and `resign` rejects with that error. The socket lock store of `@zukhruf/mutex` resigns clean when no process holds a key ([its ADR 0018](../mutex/docs/adr/0018-the-next-leader-skips-the-grace-window-after-a-clean-shutdown.md)). `@zukhruf/single-flight` no longer records a clean shutdown, because its coordinator has no grace window to skip ([its ADR 0010](../single-flight/docs/adr/0010-a-leader-holds-a-kernel-lock-on-its-key-until-its-outcome-is-handed-over.md)).
|
|
98
98
|
- `term.afterCleanShutdown` is `true` only when the term just before this one resigned clean. Then the new leader knows that it has nothing to recover. It is `false` after a crash, after a lost term, after a resign without `clean`, and for the first term.
|
|
99
99
|
- `term.signal` aborts with `LeaseLostError` when the term is lost while its leader still runs. The `subject` of the error is `'leadership'`, and its `cause` is the reason of the backend. The signal does not abort when the leader resigns.
|
|
@@ -131,7 +131,7 @@ A backend must keep six rules:
|
|
|
131
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.
|
|
132
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.
|
|
133
133
|
4. The campaign never calls `recordCleanShutdown` or `release` after a loss, because the claim can be another candidate's then.
|
|
134
|
-
5. The campaign calls `close` after each end: a won term, a lost term, a failed attempt, or an attempt that never won.
|
|
134
|
+
5. The campaign calls `close` after each end: a won term, a lost term, a failed attempt, or an attempt that never won. When a step fails and `release` or `close` fails too, the campaign rejects with a `SuppressedError`: its `suppressed` is the step's error, and its `error` is the failure to give the claim up or free it.
|
|
135
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`.
|
|
136
136
|
|
|
137
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:
|
|
@@ -214,6 +214,7 @@ The socket lock store of `@zukhruf/mutex` uses `SqliteElection`, with the claim
|
|
|
214
214
|
| `NetworkDirectoryError` | The directory of a `SqliteElection` is on a network file system. It is the class of `@zukhruf/fs`. |
|
|
215
215
|
| `LeaseLostError` | The reason of `term.signal` when the backend took the claim away. Its `subject` is `'leadership'`, and its `cause` is the backend's reason. It is the class of `@zukhruf/lease`, and this package exports it too. |
|
|
216
216
|
| `signal.reason` | A campaign rejects with it when its signal aborts. |
|
|
217
|
+
| `SuppressedError` | A campaign or a resign rejects with it when a step failed and giving the claim up or freeing it failed too. Its `suppressed` is the step's error, and its `error` is the failure to free. |
|
|
217
218
|
|
|
218
219
|
## API
|
|
219
220
|
|
|
@@ -302,6 +303,7 @@ export { LeaseLostError } from '@zukhruf/lease';
|
|
|
302
303
|
|
|
303
304
|
- [How SqliteElection elects a leader](./docs/concepts/leader-election.md)
|
|
304
305
|
- [Recipe: Run a job in only one process](./docs/recipes/singleton-job-with-leader-election.md)
|
|
306
|
+
- [Own one item at a time, and skip an item that another process owns](./docs/recipes/skip-work-that-another-process-owns.md)
|
|
305
307
|
- [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)
|
|
306
308
|
- [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)
|
|
307
309
|
- [ADR 0003: A term that shuts down cleanly tells its successor](./docs/adr/0003-a-term-that-shuts-down-cleanly-tells-its-successor.md)
|
|
@@ -330,5 +332,4 @@ src/
|
|
|
330
332
|
term.ts Term: the epoch, the lease on leadership, and resign
|
|
331
333
|
sqlite/ SqliteElection: the backend of one host, its leader pipe,
|
|
332
334
|
its leader record, and the readers of the pipe
|
|
333
|
-
testing/ the helpers that start candidate processes in the tests
|
|
334
335
|
```
|
|
@@ -36,7 +36,12 @@ export declare abstract class LeaderElection<Claim> {
|
|
|
36
36
|
/** The clock of the campaign: `globalThis` in production, a fake clock in a test. */
|
|
37
37
|
protected readonly clock: Clock;
|
|
38
38
|
protected constructor(clock: Clock);
|
|
39
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* Resolves with the new term, or `undefined` when another candidate still
|
|
41
|
+
* leads after `timeout`. When a step fails and giving the claim up or
|
|
42
|
+
* freeing the attempt fails too, it rejects with a `SuppressedError`: its
|
|
43
|
+
* `suppressed` is the step's error, and its `error` is the failure to free.
|
|
44
|
+
*/
|
|
40
45
|
campaign({ timeout, signal }?: CampaignOptions): Promise<Term | undefined>;
|
|
41
46
|
/** Prepares one attempt: the resources that `tryClaim` uses again and again. */
|
|
42
47
|
protected abstract open(signal: AbortSignal | undefined): Promise<Claim>;
|
|
@@ -1 +1 @@
|
|
|
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,KAAK;IAIlC
|
|
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,SA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
/**
|
|
30
|
+
/**
|
|
31
|
+
* Resolves with the new term, or `undefined` when another candidate still
|
|
32
|
+
* leads after `timeout`. When a step fails and giving the claim up or
|
|
33
|
+
* freeing the attempt fails too, it rejects with a `SuppressedError`: its
|
|
34
|
+
* `suppressed` is the step's error, and its `error` is the failure to free.
|
|
35
|
+
*/
|
|
31
36
|
async campaign({ timeout = 0, signal } = {}) {
|
|
32
37
|
signal?.throwIfAborted();
|
|
33
38
|
const deadline = this.clock.performance.now() + timeout;
|
|
@@ -45,7 +50,8 @@ export class LeaderElection {
|
|
|
45
50
|
won = await this.#tryUntil(claim, deadline, signal);
|
|
46
51
|
}
|
|
47
52
|
catch (error) {
|
|
48
|
-
await
|
|
53
|
+
await using closing = new AsyncDisposableStack();
|
|
54
|
+
closing.defer(() => this.close(claim));
|
|
49
55
|
throw error;
|
|
50
56
|
}
|
|
51
57
|
if (won === undefined) {
|
|
@@ -96,8 +102,10 @@ export class LeaderElection {
|
|
|
96
102
|
});
|
|
97
103
|
}
|
|
98
104
|
catch (error) {
|
|
99
|
-
|
|
100
|
-
await
|
|
105
|
+
// Disposed last in, first out: the release, then the close.
|
|
106
|
+
await using giveUp = new AsyncDisposableStack();
|
|
107
|
+
giveUp.defer(() => this.close(claim));
|
|
108
|
+
giveUp.defer(() => this.release(claim));
|
|
101
109
|
throw error;
|
|
102
110
|
}
|
|
103
111
|
return term.signal.aborted ? 'lost' : term;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"leader-election.js","sourceRoot":"","sources":["../src/leader-election.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,OAAO,IAAI,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAEtE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAajC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAgB,cAAc;IAClC,qFAAqF;IAClE,KAAK,CAAQ;IAEhC,YAAsB,KAAY;QAChC,IAAI,CAAC,KAAK,GAA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IAC7C,CAAC;CACF"}
|
package/dist/term.d.ts
CHANGED
|
@@ -43,8 +43,10 @@ export declare class Term implements Lease, AsyncDisposable {
|
|
|
43
43
|
/**
|
|
44
44
|
* Ends the term and gives the claim up. With `clean`, it first records a
|
|
45
45
|
* clean shutdown for the next term. A second call waits for the first, and
|
|
46
|
-
* its options do nothing.
|
|
47
|
-
*
|
|
46
|
+
* its options do nothing. When a step fails and freeing the claim fails
|
|
47
|
+
* too, it rejects with a `SuppressedError`: its `suppressed` is the step's
|
|
48
|
+
* error, and its `error` is the failure to free. After a loss, it only waits
|
|
49
|
+
* until the claim's resources are free, records nothing, and never rejects.
|
|
48
50
|
*/
|
|
49
51
|
resign(options?: ResignOptions): Promise<void>;
|
|
50
52
|
[Symbol.asyncDispose](): Promise<void>;
|
package/dist/term.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"term.d.ts","sourceRoot":"","sources":["../src/term.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAmB,MAAM,gBAAgB,CAAC;AAE7D,4FAA4F;AAC5F,MAAM,WAAW,SAAS;IACxB,+EAA+E;IAC/E,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,KAAK,KAAK,IAAI,GAAG,UAAU,CAAC;IACjD;;;OAGG;IACH,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACrC,oEAAoE;IACpE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB,uDAAuD;IACvD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,MAAM,WAAW,aAAa;IAC5B;;;;OAIG;IACH,KAAK,EAAE,OAAO,CAAC;CAChB;AAED;;;;;GAKG;AACH,qBAAa,IAAK,YAAW,KAAK,EAAE,eAAe;;IACjD,qGAAqG;IACrG,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,kBAAk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AI,CAAC;CAmDvC"}
|
package/dist/term.js
CHANGED
|
@@ -34,8 +34,10 @@ export class Term {
|
|
|
34
34
|
/**
|
|
35
35
|
* Ends the term and gives the claim up. With `clean`, it first records a
|
|
36
36
|
* clean shutdown for the next term. A second call waits for the first, and
|
|
37
|
-
* its options do nothing.
|
|
38
|
-
*
|
|
37
|
+
* its options do nothing. When a step fails and freeing the claim fails
|
|
38
|
+
* too, it rejects with a `SuppressedError`: its `suppressed` is the step's
|
|
39
|
+
* error, and its `error` is the failure to free. After a loss, it only waits
|
|
40
|
+
* until the claim's resources are free, records nothing, and never rejects.
|
|
39
41
|
*/
|
|
40
42
|
async resign(options) {
|
|
41
43
|
if (!this.#ending.signal.aborted) {
|
|
@@ -77,7 +79,8 @@ export class Term {
|
|
|
77
79
|
await this.#steps.release();
|
|
78
80
|
}
|
|
79
81
|
catch (error) {
|
|
80
|
-
await
|
|
82
|
+
await using closing = new AsyncDisposableStack();
|
|
83
|
+
closing.defer(() => this.#steps.close());
|
|
81
84
|
throw error;
|
|
82
85
|
}
|
|
83
86
|
await this.#steps.close();
|
|
@@ -85,14 +88,19 @@ export class Term {
|
|
|
85
88
|
/**
|
|
86
89
|
* A lost claim is no longer this term's to give up: a release could undo
|
|
87
90
|
* the next leader's claim. Only its resources are freed, and a loss has no
|
|
88
|
-
* caller to report a failure to
|
|
91
|
+
* caller to report a failure to, so a failure to free them is a process
|
|
92
|
+
* warning.
|
|
89
93
|
*/
|
|
90
94
|
async #freeLost() {
|
|
91
95
|
try {
|
|
92
96
|
this.#watching.dispose();
|
|
93
97
|
}
|
|
94
98
|
finally {
|
|
95
|
-
await this.#steps.close().catch(() => {
|
|
99
|
+
await this.#steps.close().catch((error) => {
|
|
100
|
+
process.emitWarning(Error.isError(error)
|
|
101
|
+
? error
|
|
102
|
+
: new Error(String(error), { cause: error }));
|
|
103
|
+
});
|
|
96
104
|
}
|
|
97
105
|
}
|
|
98
106
|
}
|
package/dist/term.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"term.js","sourceRoot":"","sources":["../src/term.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAc,eAAe,EAAE,MAAM,gBAAgB,CAAC;AA0B7D;;;;;GAKG;AACH,MAAM,OAAO,IAAI;IACf,qGAAqG;IAC5F,KAAK,CAAS;IACvB;;;;OAIG;IACM,kBAAkB,CAAU;IAC5B,MAAM,CAAY;IAClB,MAAM,GAAG,IAAI,eAAe,CAAC,YAAY,CAAC,CAAC;IACpD,gEAAgE;IACvD,OAAO,GAAG,IAAI,eAAe,EAAE,CAAC;IAChC,MAAM,GAAG,IAAI,MAAM,EAAQ,CAAC;IAC5B,SAAS,GAAG,IAAI,eAAe,EAAE,CAAC;IAE3C,YACE,EAAE,KAAK,EAAE,kBAAkB,EAA8C,EACzE,KAAgB;QAEhB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;QAC7C,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAClE,CAAC;IAED,mFAAmF;IACnF,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IAC5B,CAAC;IAED
|
|
1
|
+
{"version":3,"file":"term.js","sourceRoot":"","sources":["../src/term.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAc,eAAe,EAAE,MAAM,gBAAgB,CAAC;AA0B7D;;;;;GAKG;AACH,MAAM,OAAO,IAAI;IACf,qGAAqG;IAC5F,KAAK,CAAS;IACvB;;;;OAIG;IACM,kBAAkB,C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,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;gBACjD,OAAO,CAAC,WAAW,CACjB,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;oBAClB,CAAC,CAAC,KAAK;oBACP,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAC/C,CAAC;YACJ,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/election",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.44",
|
|
4
4
|
"description": "Elects a leader among candidates. Each term has a higher epoch and ends when the leader resigns or dies. Backends subclass one campaign. `SqliteElection` elects among processes of one host; candidates and observers end a leader whose pipe heartbeats stop.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -40,13 +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.
|
|
46
|
-
"@zukhruf/pipes": "^0.8.
|
|
43
|
+
"@zukhruf/async": "^0.8.44",
|
|
44
|
+
"@zukhruf/fs": "^0.8.44",
|
|
45
|
+
"@zukhruf/lease": "^0.8.44",
|
|
46
|
+
"@zukhruf/pipes": "^0.8.44"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@sinonjs/fake-timers": "15.4.0",
|
|
50
|
-
"@zukhruf/testing": "^0.8.
|
|
50
|
+
"@zukhruf/testing": "^0.8.44"
|
|
51
51
|
}
|
|
52
52
|
}
|