@zukhruf/election 0.8.154 → 0.8.156

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 CHANGED
@@ -52,13 +52,14 @@ next term: 2n
52
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
  - `election.name` is the path of `claimFile`: `join(directory, claimFile)`. Two elections with the same name are one election. A warning names the election by its name ([ADR 0010](./docs/adr/0010-an-election-has-a-name-and-the-name-of-a-sqlite-election-is-the-path-of-its-claim-file.md)).
55
+ - Each epoch of a `SqliteElection` is below 2^31, so `EpochTokenSource` of `@zukhruf/fencing` takes it. When `epochFile` holds 2^31 - 1, the next epoch would be 2^31: then `campaign` rejects with a `RangeError` that names the file, gives the claim up, and does not change the file ([ADR 0011](./docs/adr/0011-a-leader-selector-runs-a-task-each-time-it-wins-a-term-of-a-leader-election.md)).
55
56
  - `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.
56
57
  - `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.
57
58
  - 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.
58
59
  - The directory must be on a local file system. On a network file system, `campaign` rejects with `NetworkDirectoryError`.
59
60
  - 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)).
60
61
  - `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)).
61
- - To run a job each time this process leads, and to campaign again after each term, use `LeaderSelector` of `@zukhruf/mutex`. It resigns the term when the job ends, and its dispose waits for the job ([Run a job in only one process](../mutex/docs/recipes/run-a-job-in-only-one-process.md)).
62
+ - To run a job each time this process leads, and to campaign again after each term, use a `LeaderSelector`. It resigns the term when the job ends, and its dispose waits for the job ([Run a task each time this process leads](#run-a-task-each-time-this-process-leads)).
62
63
 
63
64
  ## A leader that hangs
64
65
 
@@ -94,7 +95,7 @@ await using term = await election.campaign({ timeout: Infinity, signal });
94
95
 
95
96
  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).
96
97
 
97
- - `term.epoch` is the number of the term. Each epoch is below 2^31.
98
+ - `term.epoch` is the number of the term. It is higher than the epoch of each earlier term of the election. The epoch of a `SqliteElection` is below 2^31.
98
99
  - `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, and the term still ends: a failure to stop watching the claim or to free it has no caller to go to, so it is a `TermWarning` (see [Errors](#errors)). When both fail, the warning's `cause` is a `SuppressedError`: its `suppressed` is the failure to stop watching, and its `error` is the failure to free.
99
100
  - `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)).
100
101
  - `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.
@@ -102,7 +103,7 @@ A term is a lease on leadership. `Term` implements the `Lease` interface of [`@z
102
103
 
103
104
  A leader that acts for the group must stop when `term.signal` aborts, because another candidate can lead then. `SqliteElection` never loses a living term, so its signal never aborts. A backend that can take the claim away can lose one.
104
105
 
105
- A term has no fencing token. The signal warns the leader, but it does not protect a resource: a leader can write after another candidate won. To protect a resource, make fencing tokens from the epoch with `EpochTokenSource` of [`@zukhruf/fencing`](../fencing/README.md). Then a resource can refuse the writes of an older leader:
106
+ A term has no fencing token. The signal warns the leader, but it does not protect a resource: a leader can write after another candidate won. To protect a resource, make fencing tokens from the epoch with `EpochTokenSource` of [`@zukhruf/fencing`](../fencing/README.md). It takes an epoch below 2^31, as each epoch of `SqliteElection` is, and throws a `RangeError` for a higher one. Then a resource can refuse the writes of an older leader:
106
107
 
107
108
  ```ts
108
109
  import { EpochTokenSource } from '@zukhruf/fencing';
@@ -113,6 +114,92 @@ const token = await tokens.next('orders');
113
114
 
114
115
  [ADR 0002](./docs/adr/0002-a-term-is-a-lease-on-leadership-with-no-fencing.md) tells why.
115
116
 
117
+ ## Run a task each time this process leads
118
+
119
+ Some work must run in only one process of a group, for the whole life of each process: a writer of a warehouse, a cleanup, a poll of a queue. `LeaderSelector.start(election, listener, options)` campaigns with no time limit, runs `listener.takeLeadership(term, signal)` while the term lasts, and resigns the term when the task ends. With `autoRequeue`, it campaigns again, until its dispose. It is Apache Curator's `LeaderSelector`, over a `LeaderElection` as client-go's `LeaderElector` is over a `resourcelock.Interface` ([ADR 0011](./docs/adr/0011-a-leader-selector-runs-a-task-each-time-it-wins-a-term-of-a-leader-election.md)).
120
+
121
+ ```ts title="leader.ts"
122
+ import { mkdtemp, rm } from 'node:fs/promises';
123
+ import { tmpdir } from 'node:os';
124
+ import { join } from 'node:path';
125
+ import { setTimeout as delay } from 'node:timers/promises';
126
+
127
+ import { Backoff } from '@zukhruf/async';
128
+ import {
129
+ LeaderSelector,
130
+ type LeaderSelectorListener,
131
+ SqliteElection,
132
+ } from '@zukhruf/election';
133
+
134
+ const directory = await mkdtemp(join(tmpdir(), 'writer-'));
135
+ const election = () =>
136
+ new SqliteElection({
137
+ clock: globalThis,
138
+ directory,
139
+ claimFile: 'writer.lock',
140
+ epochFile: 'writer.epoch',
141
+ });
142
+ const autoRequeue = {
143
+ backoff: new Backoff({
144
+ initial: 1_000,
145
+ multiplier: 2,
146
+ max: 60_000,
147
+ jitter: { kind: 'full' },
148
+ }),
149
+ clock: globalThis,
150
+ };
151
+ const writer = (name: string): LeaderSelectorListener => ({
152
+ takeLeadership: async (term, signal) => {
153
+ console.log(`${name} leads in term ${term.epoch}`);
154
+ // Write until the selector stops the task: a dispose, an interrupt or a loss.
155
+ while (!signal.aborted) await delay(100);
156
+ console.log(`${name} resigns`);
157
+ },
158
+ });
159
+
160
+ const first = LeaderSelector.start(election(), writer('first'), {
161
+ autoRequeue,
162
+ });
163
+ await delay(150);
164
+ const second = LeaderSelector.start(election(), writer('second'), {
165
+ autoRequeue,
166
+ });
167
+ await delay(150);
168
+ await first[Symbol.asyncDispose](); // The standby leads at once.
169
+ await delay(150);
170
+ await second[Symbol.asyncDispose]();
171
+ await rm(directory, { recursive: true, force: true });
172
+ ```
173
+
174
+ Output:
175
+
176
+ ```
177
+ first leads in term 1
178
+ first resigns
179
+ second leads in term 2
180
+ second resigns
181
+ ```
182
+
183
+ The signal of the task aborts at the dispose, at `interruptLeadership()`, and when the term is lost. Return, or reject with `signal.reason`, to give the leadership up. After each round:
184
+
185
+ | The round ends because | With `autoRequeue` | Without `autoRequeue` |
186
+ | ---------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- |
187
+ | The dispose aborted the selector, in the campaign or in the task | The selector ends, after it resigned the term. `ended` resolves. | The same. |
188
+ | The term was lost, or `interruptLeadership()` aborted the task | The next round starts at once, with no wait. | The selector waits for `requeue()`. |
189
+ | The task resolved | The next round starts at once. | The selector waits for `requeue()`. |
190
+ | The task rejected | The selector ends, after it resigned the term. `ended` and the dispose reject with the failure. | The same. |
191
+ | The resign failed after the task resolved | The selector ends with that failure. `ended` and the dispose reject with it. | The same. |
192
+ | The campaign failed | A `LeaderSelectorWarning` (see [Errors](#errors)), then a wait of `backoff.duration(retries)` on `clock`, then the next campaign. `retries` counts the failed campaigns since the last term. | The selector ends. `ended` and the dispose reject with the failure. |
193
+
194
+ - **Each backend.** `start` takes a `LeaderElection`, so the selector leads over each backend: `SqliteElection` among the processes of one host, or a backend of your own ([Write a backend](#write-a-backend)). The task gets the `Term`, with its epoch.
195
+ - **A task that ends after a loss has a loss.** When the term is lost, a task that resolves or rejects after it ends the round as a loss, not as a failure.
196
+ - **The dispose waits.** It aborts the selector, and waits until the task ended and the term was resigned, so a standby takes over before this process exits. A second dispose waits for the first. Keep the selector in the `AsyncDisposableStack` of the program. It runs its rounds in a `BackgroundService` of [`@zukhruf/async`](../async/README.md#own-work-that-runs-until-you-stop-it-with-a-background-service).
197
+ - **A failure ends the selector.** The selector does not run a failed task again. The owner decides what comes next, for example `process.exit(1)`, so that the platform restarts the process.
198
+ - **`requeue()`** queues one more round of a selector without `autoRequeue`. It returns `false` while a round is queued or runs, and throws after the dispose and after a failure ended the selector. `hasLeadership` is true while `takeLeadership` runs.
199
+ - **With `autoRequeue`, the selector campaigns again after each failed campaign**, also after a fault that stays, as client-go tries again each `RetryPeriod` and Curator's `autoRequeue` "should ignore connection loss or session expired and just keep trying". A `NetworkDirectoryError` of `SqliteElection` stays, so check the directory with `assertLocalDirectory` of [`@zukhruf/fs`](../fs/README.md) before the start.
200
+
201
+ See the recipe [Run a job in only one process](./docs/recipes/run-a-job-in-only-one-process.md).
202
+
116
203
  ## Write a backend
117
204
 
118
205
  `LeaderElection` is an abstract class. It runs the campaign, and it owns the term. Its constructor takes the name of the election and the clock. The name is the identity of the election in its backend: two elections with the same name are one election. Derive it from the claim, so that it cannot disagree with the claim ([ADR 0010](./docs/adr/0010-an-election-has-a-name-and-the-name-of-a-sqlite-election-is-the-path-of-its-claim-file.md)). 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)):
@@ -129,7 +216,7 @@ const token = await tokens.next('orders');
129
216
 
130
217
  A backend must keep six rules:
131
218
 
132
- 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.
219
+ 1. `tryClaim` gives the epoch in the same step that wins the claim. Each epoch is higher than each earlier one. A backend whose epoch feeds `EpochTokenSource` keeps it below 2^31, as `SqliteElection` does ([ADR 0011](./docs/adr/0011-a-leader-selector-runs-a-task-each-time-it-wins-a-term-of-a-leader-election.md)).
133
220
  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.
134
221
  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.
135
222
  4. The campaign never calls `recordCleanShutdown` or `release` after a loss, because the claim can be another candidate's then.
@@ -211,18 +298,21 @@ The socket lock store of `@zukhruf/mutex` uses `SqliteElection`, with the claim
211
298
 
212
299
  ## Errors
213
300
 
214
- | Error | When |
215
- | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
216
- | `NetworkDirectoryError` | The directory of a `SqliteElection` is on a network file system. It is the class of `@zukhruf/fs`. |
217
- | `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. |
218
- | `signal.reason` | A campaign rejects with it when its signal aborts. |
219
- | `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. |
301
+ | Error | When |
302
+ | ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
303
+ | `NetworkDirectoryError` | The directory of a `SqliteElection` is on a network file system. It is the class of `@zukhruf/fs`. |
304
+ | `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. |
305
+ | `signal.reason` | A campaign rejects with it when its signal aborts. |
306
+ | `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. |
307
+ | `RangeError` | A campaign of a `SqliteElection` rejects with it when the next epoch would be 2^31 or more. The message names `epochFile`. |
308
+ | The failure of a leader's task | `ended` and the dispose of a `LeaderSelector` reject with the error of a task that failed, of a resign that failed, and, without `autoRequeue`, of a failed campaign. `requeue()` throws an `Error` after the dispose, and after a failure ended the selector. |
220
309
 
221
- A failure that no caller waits for is a process warning, in the shape of [root ADR 0010](../../docs/adr/0010-a-package-warning-names-its-type-code-and-subject-and-keeps-the-failure-as-its-cause.md). A `'warning'` listener reads its type as `name`, its `code`, and its subject in `detail`, one `<term>: <value>` line each. Its `cause` is the failure, and its message ends with the failure's message. `--disable-warning=<code or type>` stops the print. `TermWarning` is a class that the package exports, so a listener can test `warning instanceof` the class ([root ADR 0011](../../docs/adr/0011-a-package-warning-is-an-instance-of-its-own-class-which-the-package-exports.md)). With two installed copies of the package, `instanceof` fails across the copies, so compare `code`.
310
+ A failure that no caller waits for is a process warning, in the shape of [root ADR 0010](../../docs/adr/0010-a-package-warning-names-its-type-code-and-subject-and-keeps-the-failure-as-its-cause.md). A `'warning'` listener reads its type as `name`, its `code`, and its subject in `detail`, one `<term>: <value>` line each. Its `cause` is the failure, and its message ends with the failure's message. `--disable-warning=<code or type>` stops the print. `TermWarning` and `LeaderSelectorWarning` are classes that the package exports, so a listener can test `warning instanceof` the class ([root ADR 0011](../../docs/adr/0011-a-package-warning-is-an-instance-of-its-own-class-which-the-package-exports.md)). With two installed copies of the package, `instanceof` fails across the copies, so compare `code`.
222
311
 
223
- | Warning | Code | Detail | When |
224
- | ------------- | ----------------------------------- | --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
225
- | `TermWarning` | `ZUKHRUF_ELECTION_TERM_FREE_FAILED` | `election: <name>` and `epoch: <epoch>` | A lost term fails to stop watching its claim or to free it. The term still ends. When both fail, the `cause` is a `SuppressedError`: its `suppressed` is the failure to stop watching, and its `error` is the failure to free. |
312
+ | Warning | Code | Detail | When |
313
+ | ----------------------- | -------------------------------------------------- | --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
314
+ | `TermWarning` | `ZUKHRUF_ELECTION_TERM_FREE_FAILED` | `election: <name>` and `epoch: <epoch>` | A lost term fails to stop watching its claim or to free it. The term still ends. When both fail, the `cause` is a `SuppressedError`: its `suppressed` is the failure to stop watching, and its `error` is the failure to free. |
315
+ | `LeaderSelectorWarning` | `ZUKHRUF_ELECTION_LEADER_SELECTOR_CAMPAIGN_FAILED` | `election: <name>` | A campaign of a `LeaderSelector` with `autoRequeue` fails. The selector waits `backoff.duration(retries)`, and campaigns again. |
226
316
 
227
317
  ## API
228
318
 
@@ -286,6 +376,33 @@ class Term implements Lease, AsyncDisposable {
286
376
  [Symbol.asyncDispose](): Promise<void>; // resign()
287
377
  }
288
378
 
379
+ interface LeaderSelectorListener {
380
+ takeLeadership(term: Term, signal: AbortSignal): Promise<void>; // return, or reject with signal.reason
381
+ }
382
+
383
+ interface LeaderSelectorOptions {
384
+ autoRequeue?: { backoff: Backoff; clock: Clock } | undefined; // without it: lead once, then wait for requeue()
385
+ }
386
+
387
+ class LeaderSelector implements AsyncDisposable {
388
+ static start(
389
+ election: LeaderElection<unknown>,
390
+ listener: LeaderSelectorListener,
391
+ options?: LeaderSelectorOptions,
392
+ ): LeaderSelector;
393
+ get hasLeadership(): boolean; // true while takeLeadership runs
394
+ get ended(): Promise<void>; // rejects with the failure that ended the selector
395
+ requeue(): boolean; // false while a round is queued or runs
396
+ interruptLeadership(): void;
397
+ [Symbol.asyncDispose](): Promise<void>; // waits for the task and the resign
398
+ }
399
+
400
+ class LeaderSelectorWarning extends Error {
401
+ readonly code: 'ZUKHRUF_ELECTION_LEADER_SELECTOR_CAMPAIGN_FAILED';
402
+ readonly detail: string; // election: <name>
403
+ constructor(failure: unknown, subject: { election: string });
404
+ }
405
+
289
406
  class TermWarning extends Error {
290
407
  readonly code: 'ZUKHRUF_ELECTION_TERM_FREE_FAILED';
291
408
  readonly detail: string; // election: <name>, then epoch: <epoch>
@@ -322,7 +439,7 @@ export { LeaseLostError } from '@zukhruf/lease';
322
439
  ## Documentation
323
440
 
324
441
  - [How SqliteElection elects a leader](./docs/concepts/leader-election.md)
325
- - [Recipe: Run a job in only one process](../mutex/docs/recipes/run-a-job-in-only-one-process.md), with `LeaderSelector` of `@zukhruf/mutex`
442
+ - [Recipe: Run a job in only one process](./docs/recipes/run-a-job-in-only-one-process.md), with `LeaderSelector`
326
443
  - [Own one item at a time, and skip an item that another process owns](./docs/recipes/skip-work-that-another-process-owns.md)
327
444
  - [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)
328
445
  - [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)
@@ -334,6 +451,7 @@ export { LeaseLostError } from '@zukhruf/lease';
334
451
  - [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)
335
452
  - [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)
336
453
  - [ADR 0010: An election has a name, and the name of a SQLite election is the path of its claim file](./docs/adr/0010-an-election-has-a-name-and-the-name-of-a-sqlite-election-is-the-path-of-its-claim-file.md)
454
+ - [ADR 0011: A leader selector runs a task each time it wins a term of a leader election, over a `LeaderElection` only](./docs/adr/0011-a-leader-selector-runs-a-task-each-time-it-wins-a-term-of-a-leader-election.md)
337
455
  - [Code copied into this package](./docs/copied-code.md)
338
456
 
339
457
  ## Development
@@ -351,6 +469,7 @@ npx nx run election:build # compiles src/ to dist/
351
469
  src/
352
470
  leader-election.ts LeaderElection: the campaign, and the steps of a backend
353
471
  term.ts Term: the epoch, the lease on leadership, and resign
472
+ leader-selector.ts LeaderSelector: a task each time this process wins a term
354
473
  sqlite/ SqliteElection: the backend of one host, its leader pipe,
355
474
  its leader record, and the readers of the pipe
356
475
  ```
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { type CampaignOptions, LeaderElection } from './leader-election.ts';
2
+ export { LeaderSelector, type LeaderSelectorListener, type LeaderSelectorOptions, } from './leader-selector.ts';
3
+ export { LeaderSelectorWarning } from './leader-selector-warning.ts';
2
4
  export { type ResignOptions, Term, type TermSteps } from './term.ts';
3
5
  export { TermWarning } from './term-warning.ts';
4
6
  export { type LeaderObserver } from './sqlite/leader-observer.ts';
@@ -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,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,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"}
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,EACL,cAAc,EACd,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,GAC3B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,KAAK,aAAa,EAAE,IAAI,EAAE,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,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,4 +1,6 @@
1
1
  export { LeaderElection } from './leader-election.js';
2
+ export { LeaderSelector, } from './leader-selector.js';
3
+ export { LeaderSelectorWarning } from './leader-selector-warning.js';
2
4
  export { Term } from './term.js';
3
5
  export { TermWarning } from './term-warning.js';
4
6
  export {} from './sqlite/leader-observer.js';
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,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,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
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EACL,cAAc,GAGf,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAsB,IAAI,EAAkB,MAAM,WAAW,CAAC;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,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"}
@@ -19,7 +19,7 @@ export interface CampaignOptions {
19
19
  *
20
20
  * A backend keeps six rules:
21
21
  * - `tryClaim` returns the epoch of the term it won, in the same step, and
22
- * each epoch is higher than every earlier one and below 2^31.
22
+ * each epoch is higher than every earlier one.
23
23
  * - `tryClaim` reports `afterCleanShutdown` only when the term just before
24
24
  * it recorded a clean shutdown with `recordCleanShutdown`, and it reports
25
25
  * each record once.
@@ -9,7 +9,7 @@ import { Term } from './term.js';
9
9
  *
10
10
  * A backend keeps six rules:
11
11
  * - `tryClaim` returns the epoch of the term it won, in the same step, and
12
- * each epoch is higher than every earlier one and below 2^31.
12
+ * each epoch is higher than every earlier one.
13
13
  * - `tryClaim` reports `afterCleanShutdown` only when the term just before
14
14
  * it recorded a clean shutdown with `recordCleanShutdown`, and it reports
15
15
  * each record once.
@@ -0,0 +1,9 @@
1
+ /** A campaign of a `LeaderSelector` with `autoRequeue` failed. The selector waits the duration of its `backoff`, and campaigns again. */
2
+ export declare class LeaderSelectorWarning extends Error {
3
+ readonly code = "ZUKHRUF_ELECTION_LEADER_SELECTOR_CAMPAIGN_FAILED";
4
+ readonly detail: string;
5
+ constructor(failure: unknown, { election }: {
6
+ election: string;
7
+ });
8
+ }
9
+ //# sourceMappingURL=leader-selector-warning.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"leader-selector-warning.d.ts","sourceRoot":"","sources":["../src/leader-selector-warning.ts"],"names":[],"mappings":"AAAA,yIAAyI;AACzI,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C,QAAQ,CAAC,IAAI,sDAAsD;IACnE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,YAAY,OAAO,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAE,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,EAO/D;CACF"}
@@ -0,0 +1,11 @@
1
+ /** A campaign of a `LeaderSelector` with `autoRequeue` failed. The selector waits the duration of its `backoff`, and campaigns again. */
2
+ export class LeaderSelectorWarning extends Error {
3
+ code = 'ZUKHRUF_ELECTION_LEADER_SELECTOR_CAMPAIGN_FAILED';
4
+ detail;
5
+ constructor(failure, { election }) {
6
+ super(`The campaign failed, and the selector campaigns again: ${Error.isError(failure) ? failure.message : String(failure)}`, { cause: failure });
7
+ this.name = 'LeaderSelectorWarning';
8
+ this.detail = `election: ${election}`;
9
+ }
10
+ }
11
+ //# sourceMappingURL=leader-selector-warning.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"leader-selector-warning.js","sourceRoot":"","sources":["../src/leader-selector-warning.ts"],"names":[],"mappings":"AAAA,yIAAyI;AACzI,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IACrC,IAAI,GAAG,kDAAkD,CAAC;IAC1D,MAAM,CAAS;IAExB,YAAY,OAAgB,EAAE,EAAE,QAAQ,EAAwB;QAC9D,KAAK,CACH,0DAA0D,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,EACtH,EAAE,KAAK,EAAE,OAAO,EAAE,CACnB,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,aAAa,QAAQ,EAAE,CAAC;IACxC,CAAC;CACF"}
@@ -0,0 +1,38 @@
1
+ import { type Backoff, type Clock } from '@zukhruf/async';
2
+ import type { LeaderElection } from './leader-election.ts';
3
+ import type { Term } from './term.ts';
4
+ export interface LeaderSelectorListener {
5
+ /** Return, or reject with `signal.reason`, to give the leadership up. */
6
+ takeLeadership(term: Term, signal: AbortSignal): Promise<void>;
7
+ }
8
+ export interface LeaderSelectorOptions {
9
+ /** Without it, the selector leads once and then waits for `requeue()`. */
10
+ autoRequeue?: {
11
+ backoff: Backoff;
12
+ clock: Clock;
13
+ } | undefined;
14
+ }
15
+ /**
16
+ * Runs a task each time this process wins a term of an election, and
17
+ * resigns the term when the task ends. With `autoRequeue`, it campaigns
18
+ * again, until its dispose. Apache Curator's `LeaderSelector`, over a
19
+ * `LeaderElection` as client-go's `LeaderElector` is over a
20
+ * `resourcelock.Interface` (ADR 0011).
21
+ */
22
+ export declare class LeaderSelector implements AsyncDisposable {
23
+ #private;
24
+ private constructor();
25
+ /** Campaigns at once, and runs `listener.takeLeadership` with each term that it wins. */
26
+ static start(election: LeaderElection<unknown>, listener: LeaderSelectorListener, { autoRequeue }?: LeaderSelectorOptions): LeaderSelector;
27
+ /** True while `takeLeadership` runs. */
28
+ get hasLeadership(): boolean;
29
+ /** Resolves at a clean end, and rejects with the failure that ended the selector. */
30
+ get ended(): Promise<void>;
31
+ /** Queues one more round. False when a round is queued or runs. Throws after the dispose, and after a failure ended the selector. */
32
+ requeue(): boolean;
33
+ /** Aborts the signal of the running task. Does nothing while the selector does not lead. */
34
+ interruptLeadership(): void;
35
+ /** Aborts the selector, and waits until the task ended and the term was resigned. */
36
+ [Symbol.asyncDispose](): Promise<void>;
37
+ }
38
+ //# sourceMappingURL=leader-selector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"leader-selector.d.ts","sourceRoot":"","sources":["../src/leader-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,OAAO,EACZ,KAAK,KAAK,EAIX,MAAM,gBAAgB,CAAC;AAExB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,WAAW,sBAAsB;IACrC,yEAAyE;IACzE,cAAc,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAChE;AAED,MAAM,WAAW,qBAAqB;IACpC,0EAA0E;IAC1E,WAAW,CAAC,EAAE;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,KAAK,CAAA;KAAE,GAAG,SAAS,CAAC;CAC9D;AAED;;;;;;GAMG;AACH,qBAAa,cAAe,YAAW,eAAe;;IAQpD,OAAO,eASN;IAED,yFAAyF;IACzF,MAAM,CAAC,KAAK,CACV,QAAQ,EAAE,cAAc,CAAC,OAAO,CAAC,EACjC,QAAQ,EAAE,sBAAsB,EAChC,EAAE,WAAW,EAAE,GAAE,qBAA0B,GAC1C,cAAc,CAQhB;IAED,wCAAwC;IACxC,IAAI,aAAa,IAAI,OAAO,CAE3B;IAED,qFAAqF;IACrF,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAEzB;IAED,qIAAqI;IACrI,OAAO,IAAI,OAAO,CASjB;IAED,4FAA4F;IAC5F,mBAAmB,IAAI,IAAI,CAE1B;IAED,qFAAqF;IACrF,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAGrC;CAsDF"}
@@ -0,0 +1,172 @@
1
+ import { BackgroundService, Latch, delay, untilAborted, } from '@zukhruf/async';
2
+ import { LeaderSelectorWarning } from './leader-selector-warning.js';
3
+ /**
4
+ * Runs a task each time this process wins a term of an election, and
5
+ * resigns the term when the task ends. With `autoRequeue`, it campaigns
6
+ * again, until its dispose. Apache Curator's `LeaderSelector`, over a
7
+ * `LeaderElection` as client-go's `LeaderElector` is over a
8
+ * `resourcelock.Interface` (ADR 0011).
9
+ */
10
+ export class LeaderSelector {
11
+ #election;
12
+ #listener;
13
+ #requeue;
14
+ #disposed = new AbortController();
15
+ #state = QUEUED;
16
+ #rounds;
17
+ constructor(election, listener, requeue) {
18
+ this.#election = election;
19
+ this.#listener = listener;
20
+ this.#requeue = requeue;
21
+ this.#rounds = BackgroundService.start((stop) => this.#run(stop));
22
+ }
23
+ /** Campaigns at once, and runs `listener.takeLeadership` with each term that it wins. */
24
+ static start(election, listener, { autoRequeue } = {}) {
25
+ return new LeaderSelector(election, listener, autoRequeue === undefined
26
+ ? new RequeueOnCall()
27
+ : new AutoRequeue(autoRequeue, election.name));
28
+ }
29
+ /** True while `takeLeadership` runs. */
30
+ get hasLeadership() {
31
+ return this.#state instanceof Leading;
32
+ }
33
+ /** Resolves at a clean end, and rejects with the failure that ended the selector. */
34
+ get ended() {
35
+ return this.#rounds.ended;
36
+ }
37
+ /** Queues one more round. False when a round is queued or runs. Throws after the dispose, and after a failure ended the selector. */
38
+ requeue() {
39
+ if (this.#disposed.signal.aborted)
40
+ throw new Error('This leader selector is disposed.');
41
+ if (this.#state instanceof Ended)
42
+ throw new Error('This leader selector ended, so it leads no more.');
43
+ if (!(this.#state instanceof Idle))
44
+ return false;
45
+ this.#state.requeued.open();
46
+ this.#state = QUEUED;
47
+ return true;
48
+ }
49
+ /** Aborts the signal of the running task. Does nothing while the selector does not lead. */
50
+ interruptLeadership() {
51
+ if (this.#state instanceof Leading)
52
+ this.#state.interrupt.abort();
53
+ }
54
+ /** Aborts the selector, and waits until the task ended and the term was resigned. */
55
+ [Symbol.asyncDispose]() {
56
+ this.#disposed.abort();
57
+ return this.#rounds[Symbol.asyncDispose]();
58
+ }
59
+ async #run(stop) {
60
+ try {
61
+ await this.#loop(stop);
62
+ }
63
+ finally {
64
+ this.#state = ENDED;
65
+ }
66
+ }
67
+ async #loop(stop) {
68
+ while (!stop.aborted) {
69
+ const state = this.#state;
70
+ if (state instanceof Idle)
71
+ await untilAborted(state.requeued.wait(), stop);
72
+ let term;
73
+ try {
74
+ // A campaign with no time limit resolves only with a term.
75
+ term = (await this.#election.campaign({
76
+ timeout: Infinity,
77
+ signal: stop,
78
+ }));
79
+ }
80
+ catch (failure) {
81
+ if (stop.aborted && failure === stop.reason)
82
+ throw failure;
83
+ await this.#requeue.afterFailedCampaign(failure, stop);
84
+ continue;
85
+ }
86
+ await this.#lead(term, stop);
87
+ this.#state = this.#requeue.afterTerm();
88
+ }
89
+ }
90
+ /**
91
+ * Runs the task while the term lasts, and resigns the term after it. The
92
+ * signal of the task aborts at the dispose, at `interruptLeadership()` and
93
+ * at a loss. Only a failure of the task, or of the resign, rejects.
94
+ */
95
+ async #lead(won, stop) {
96
+ await using term = won;
97
+ const interrupt = new AbortController();
98
+ const signal = AbortSignal.any([stop, interrupt.signal, term.signal]);
99
+ this.#state = new Leading(interrupt);
100
+ try {
101
+ await this.#listener.takeLeadership(term, signal);
102
+ }
103
+ catch (error) {
104
+ // The loss ended the task, whatever the task did after it.
105
+ if (term.signal.aborted)
106
+ return;
107
+ // The task gave the leadership up as it was asked to.
108
+ if (signal.aborted && error === signal.reason)
109
+ return;
110
+ throw error;
111
+ }
112
+ finally {
113
+ this.#state = QUEUED;
114
+ }
115
+ }
116
+ }
117
+ /** Without `autoRequeue`: the selector waits for `requeue()` after each term, and a failed campaign ends it. */
118
+ class RequeueOnCall {
119
+ afterTerm() {
120
+ return new Idle();
121
+ }
122
+ async afterFailedCampaign(failure) {
123
+ throw failure;
124
+ }
125
+ }
126
+ /**
127
+ * With `autoRequeue`: the selector campaigns again after each term, and
128
+ * after a failed campaign it warns and waits the `backoff` duration of the
129
+ * failed campaigns since the last term, as client-go waits its `RetryPeriod`.
130
+ */
131
+ class AutoRequeue {
132
+ #backoff;
133
+ #clock;
134
+ #election;
135
+ #failedCampaigns = 0;
136
+ constructor({ backoff, clock }, election) {
137
+ this.#backoff = backoff;
138
+ this.#clock = clock;
139
+ this.#election = election;
140
+ }
141
+ afterTerm() {
142
+ this.#failedCampaigns = 0;
143
+ return QUEUED;
144
+ }
145
+ async afterFailedCampaign(failure, stop) {
146
+ process.emitWarning(new LeaderSelectorWarning(failure, { election: this.#election }));
147
+ await delay(this.#backoff.duration(this.#failedCampaigns++), {
148
+ clock: this.#clock,
149
+ signal: stop,
150
+ });
151
+ }
152
+ }
153
+ /** A round is queued or runs, and its task does not run: it campaigns, resigns, or backs off. */
154
+ class Queued {
155
+ }
156
+ const QUEUED = new Queued();
157
+ /** The task runs, and `interrupt` aborts its signal. */
158
+ class Leading {
159
+ interrupt;
160
+ constructor(interrupt) {
161
+ this.interrupt = interrupt;
162
+ }
163
+ }
164
+ /** A round ended without autoRequeue: the selector waits for `requeue()`, which opens `requeued`. */
165
+ class Idle {
166
+ requeued = new Latch();
167
+ }
168
+ /** The rounds ended, by the dispose or by a failure: the selector leads no more. */
169
+ class Ended {
170
+ }
171
+ const ENDED = new Ended();
172
+ //# sourceMappingURL=leader-selector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"leader-selector.js","sourceRoot":"","sources":["../src/leader-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAGjB,KAAK,EACL,KAAK,EACL,YAAY,GACb,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAarE;;;;;;GAMG;AACH,MAAM,OAAO,cAAc;IAChB,SAAS,CAA0B;IACnC,SAAS,CAAyB;IAClC,QAAQ,CAAU;IAClB,SAAS,GAAG,IAAI,eAAe,EAAE,CAAC;IAC3C,MAAM,GAAoC,MAAM,CAAC;IACxC,OAAO,CAAoB;IAEpC,YACE,QAAiC,EACjC,QAAgC,EAChC,OAAgB;QAEhB,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACpE,CAAC;IAED,yFAAyF;IACzF,MAAM,CAAC,KAAK,CACV,QAAiC,EACjC,QAAgC,EAChC,EAAE,WAAW,EAAE,GAA0B,EAAE;QAE3C,OAAO,IAAI,cAAc,CACvB,QAAQ,EACR,QAAQ,EACR,WAAW,KAAK,SAAS;YACvB,CAAC,CAAC,IAAI,aAAa,EAAE;YACrB,CAAC,CAAC,IAAI,WAAW,CAAC,WAAW,EAAE,QAAQ,CAAC,IAAI,CAAC,CAChD,CAAC;IACJ,CAAC;IAED,wCAAwC;IACxC,IAAI,aAAa;QACf,OAAO,IAAI,CAAC,MAAM,YAAY,OAAO,CAAC;IACxC,CAAC;IAED,qFAAqF;IACrF,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,OAAO,CAAC,K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SAAS,CAAC;IACnC;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,kJAAkJ;IAClJ,KAAK,EAAE,KAAK,CAAC;CACd;AAoBD;;;;;;;;GAQG;AACH,qBAAa,cAAe,SAAQ,cAAc,CAAC,aAAa,CAAC;;IAS/D,YAAY,EACV,SAAS,EACT,SAAS,EACT,SAAS,EACT,aAAsB,EACtB,iBAAqC,EACrC,KAAK,GACN,EAAE,qBAAqB,EAevB;IAED;;;;;OAKG;IACH,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,CAE3B;IAED;;;;OAIG;IACH,OAAO,IAAI,cAAc,CAExB;IAED,UAAgB,IAAI,IAAI,OAAO,CAAC,aAAa,CAAC,CAI7C;IAED,UAAgB,QAAQ,CACtB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,GAAG,oBAAoB,CAAC,GAAG,SAAS,CAAC,CAkCjE;IAeD;;;;OAIG;IACH,UAAgB,aAAa,CAC3B,OAAO,EAAE,aAAa,EACtB,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,IAAI,CAAC,CAcf;IAED;;;;;OAKG;IACH,SAAS,CAAC,KAAK,IAAI,UAAU,CAE5B;IAED;;;;OAIG;IACH,UAAgB,mBAAmB,CACjC,QAAQ,EAAE,aAAa,EACvB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC,CAIf;IAED,UAAgB,OAAO,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAE7D;IAED,SAAS,CAAC,KAAK,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAErD;CACF"}
@@ -18,6 +18,12 @@ const backoff = new Backoff({
18
18
  max: 1000,
19
19
  jitter: { kind: 'ratio', ratio: 0.2 },
20
20
  });
21
+ /**
22
+ * The bound of the epoch of a term: `EpochTokenSource` of `@zukhruf/fencing`
23
+ * puts it above 32 bits of sequence in a signed 64-bit token, so it takes an
24
+ * epoch below 2^31 (ADR 0011).
25
+ */
26
+ const EPOCH_LIMIT = 1n << 31n;
21
27
  /**
22
28
  * Elects one leader among the processes of one host that share `directory`.
23
29
  * The claim is the file lock of `claimFile`, held for the whole term. The
@@ -81,6 +87,8 @@ export class SqliteElection extends LeaderElection {
81
87
  const afterCleanShutdown = (await readCleanShutdown(this.#cleanShutdownPath)) ===
82
88
  previous.toString();
83
89
  const epoch = previous + 1n;
90
+ if (epoch >= EPOCH_LIMIT)
91
+ throw new RangeError(`The epoch file ${this.#epochPath} holds ${previous}, so the next epoch would be ${epoch}, and an epoch must be below 2^31.`);
84
92
  await durableWrite(this.#epochPath, epoch.toString(), {
85
93
  clock: this.clock,
86
94
  });
@@ -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;AAEjC,OAAO,EAAE,OAAO,EAAc,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EACL,QAAQ,EACR,oBAAoB,EACpB,WAAW,EACX,YAAY,EACZ,OAAO,GACR,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AA+BpD;;;GAGG;AACH,MAAM,OAAO,GAAG,IAAI,OA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C,MAAM,EAAE,CAAC;IACxB,CAAC;IAES,KAAK,CAAC,OAAsB;QACpC,OAAO,OAAO,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;CACF;AAED,wFAAwF;AACxF,KAAK,UAAU,iBAAiB,CAAC,IAAY;IAC3C,IAAI,CAAC;QACH,OAAO,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;AACH,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,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,6 +1,6 @@
1
1
  {
2
2
  "name": "@zukhruf/election",
3
- "version": "0.8.154",
3
+ "version": "0.8.156",
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,14 +40,14 @@
40
40
  "access": "public"
41
41
  },
42
42
  "dependencies": {
43
- "@zukhruf/async": "^0.8.154",
44
- "@zukhruf/fs": "^0.8.154",
45
- "@zukhruf/lease": "^0.8.154",
46
- "@zukhruf/pipes": "^0.8.154"
43
+ "@zukhruf/async": "^0.8.156",
44
+ "@zukhruf/fs": "^0.8.156",
45
+ "@zukhruf/lease": "^0.8.156",
46
+ "@zukhruf/pipes": "^0.8.156"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@sinonjs/fake-timers": "15.5.0",
50
- "@zukhruf/parameterized-test": "^0.8.154",
51
- "@zukhruf/testing": "^0.8.154"
50
+ "@zukhruf/parameterized-test": "^0.8.156",
51
+ "@zukhruf/testing": "^0.8.156"
52
52
  }
53
53
  }