@zakkster/lite-pick 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +71 -0
- package/Pick.d.ts +40 -3
- package/Pick.js +186 -5
- package/README.md +52 -21
- package/llms.txt +27 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,75 @@ All notable changes to `@zakkster/lite-pick` are documented here. The format fol
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project adheres to
|
|
5
5
|
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## [0.3.0] - 2026-09-23
|
|
8
|
+
|
|
9
|
+
M3: P2C (power-of-two-choices), the headline strategy -- and the balance-quality anchor
|
|
10
|
+
(ROADMAP.md M3).
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- `P2cBalancer extends BalancerBase` -- power-of-two-choices. Draws two DISTINCT eligible
|
|
15
|
+
endpoints uniformly at random (rejection sampling over the shared bitmap -- no peer, no
|
|
16
|
+
owned draw-set) and returns the one with the lower in-flight load; ties to the first draw.
|
|
17
|
+
In-flight counts are the caller's `Uint32Array` (read-only to `pick()`). O(1) per pick,
|
|
18
|
+
0 B/op. Fails closed (`PICK_NONE`) when the whole pool is down. Owns only a seeded,
|
|
19
|
+
deterministic PRNG (reproducible benches).
|
|
20
|
+
- The distinct second choice uses a BOUNDED redraw (up to 32 tries), not a single nudge, so
|
|
21
|
+
the two-choices property holds even at tiny pool sizes (~2^-32 collision chance), while
|
|
22
|
+
staying expected-O(1) and 0 B/op.
|
|
23
|
+
- `test/P2C.test.js` -- 10 tests: n=2 always-lower-load, determinism by seed, fail-closed,
|
|
24
|
+
single-node, skips-down, a very-sparse-pool fallback path, a never-returns-a-down-index
|
|
25
|
+
proof under 200k churned picks, and an in-suite balance smoke.
|
|
26
|
+
- **The balance anchor** (`test/balance.mjs`): the balls-into-bins experiment now proves the
|
|
27
|
+
`ln ln n / ln 2` ceiling -- at n=1024, k=32 balls/bin, P2C peak-to-mean gap ~2 vs a random
|
|
28
|
+
single-draw foil's ~21, and P2C's gap stays ~2-3 as n grows to 4096 while random's grows.
|
|
29
|
+
- Gates extended for P2C: torture (retention + 0 B/op `pick()`), PerfGate (`zgcSuite` scenario
|
|
30
|
+
+ a `mustFail` teeth-check), witness (`const` complexity -> flat throughput), benchmark
|
|
31
|
+
matrix (P2C subject + a random-draw foil).
|
|
32
|
+
- `decisions/0005-p2c-draw.md` -- rejection sampling (no peer, RandomSet deferred), the
|
|
33
|
+
bounded-distinct-redraw enrichment, and caller-owned in-flight counters.
|
|
34
|
+
|
|
35
|
+
### Changed
|
|
36
|
+
|
|
37
|
+
- Version 0.2.0 -> 0.3.0 across `package.json`, `Pick.js` `VERSION`, and `llms.txt`.
|
|
38
|
+
- Folded lite-o1 v1.11.0's new members into the substrate map (RESEARCH s6, ROADMAP):
|
|
39
|
+
`Reservoir` -> the Subsetting substrate (post-1.0 #4); `EliasFano` -> a viable ring-with-
|
|
40
|
+
vnodes option for M8 ConsistentHash; `RankSelect` noted as static-only (not for the
|
|
41
|
+
mutating eligibility bitmap).
|
|
42
|
+
|
|
43
|
+
## [0.2.0] - 2026-09-23
|
|
44
|
+
|
|
45
|
+
M2: SmoothWRR, the weighted default (ROADMAP.md M2).
|
|
46
|
+
|
|
47
|
+
### Added
|
|
48
|
+
|
|
49
|
+
- `SmoothWRRBalancer extends BalancerBase` -- nginx-style smooth weighted round-robin
|
|
50
|
+
(`current += weight; pick max; current -= total`). Distributes picks by caller-configured
|
|
51
|
+
integer weights, interleaved SMOOTHLY (weights [5,1,1] -> A,A,B,A,C,A,A), not in the
|
|
52
|
+
bursts of naive weight-expansion WRR. Owns its Float64Array smoothing accumulators; the
|
|
53
|
+
sole writer of the weights via the cold `setWeight(i, w)`. O(cap) per pick, 0 B/op. Fails
|
|
54
|
+
closed (`PICK_NONE`) when the eligible-weight sum is 0 (all down, or all eligible weights 0).
|
|
55
|
+
- `setWeight(i, w)` (cold) -- reconfigure a weight, keeping the eligible-weight total exact.
|
|
56
|
+
`setEligible` is overridden to maintain the total and reset the toggled node's accumulator
|
|
57
|
+
(no stale credit across an eligibility epoch).
|
|
58
|
+
- `test/SmoothWRR.test.js` -- 12 tests: the documented [5,1,1] sequence, exact fairness over
|
|
59
|
+
k cycles, smoothness (max-run strictly below the bursty foil), skips-down / redistribution,
|
|
60
|
+
fail-closed (all down AND all-zero-weight), setWeight/setEligible invariants + accumulator
|
|
61
|
+
reset, uint32 validation, and a never-returns-a-down-index proof under 200k churned picks.
|
|
62
|
+
- Gates extended for SmoothWRR: torture (retention + 0 B/op `pick()`), PerfGate (`zgcSuite`
|
|
63
|
+
scenario at a realistic 256-endpoint pool -- SmoothWRR is O(cap) -- with a `grows` counter
|
|
64
|
+
over all three backing arrays, plus a `mustFail` teeth-check), witness (a per-strategy
|
|
65
|
+
complexity flag: `linear` asserts flat WORK RATE `ops/ms * n`), balance (exact weighted
|
|
66
|
+
fairness + max-run < bursty foil).
|
|
67
|
+
- `benchmark/Matrix.mjs` -- SmoothWRR subject + the naive expand-WRR bursty foil.
|
|
68
|
+
- `decisions/0004-smoothwrr-weight-ownership.md` -- weight ownership (cold setWeight),
|
|
69
|
+
Float64 accumulators, and the epoch-reset-on-eligibility-transition enrichment.
|
|
70
|
+
|
|
71
|
+
### Changed
|
|
72
|
+
|
|
73
|
+
- Version 0.1.0 -> 0.2.0 across `package.json`, `Pick.js` `VERSION`, and `llms.txt`.
|
|
74
|
+
- Corrected the SmoothWRR complexity: O(cap) per pick, not "O(1) amortized" (ROADMAP).
|
|
75
|
+
|
|
7
76
|
## [0.1.0] - 2026-09-23
|
|
8
77
|
|
|
9
78
|
M1: the first strategy, RoundRobin (ROADMAP.md M1). The M0 harness stubs become real,
|
|
@@ -72,5 +141,7 @@ ownership boundary in place before any `pick()` is written.
|
|
|
72
141
|
- Next: **M1 RoundRobin** (0.1.0) -- the first strategy, landing the throughput witness
|
|
73
142
|
and the balance gate.
|
|
74
143
|
|
|
144
|
+
[0.3.0]: https://github.com/PeshoVurtoleta/lite-pick/releases/tag/v0.3.0
|
|
145
|
+
[0.2.0]: https://github.com/PeshoVurtoleta/lite-pick/releases/tag/v0.2.0
|
|
75
146
|
[0.1.0]: https://github.com/PeshoVurtoleta/lite-pick/releases/tag/v0.1.0
|
|
76
147
|
[0.0.1]: https://github.com/PeshoVurtoleta/lite-pick/releases/tag/v0.0.1
|
package/Pick.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @zakkster/lite-pick -- TypeScript declarations.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* strategy classes (
|
|
6
|
-
*
|
|
4
|
+
* M3 (0.3.0): substrate seams + RoundRobin + SmoothWRR + P2C (the headline). The
|
|
5
|
+
* remaining strategy classes (LeastConn/SED/NQ, PeakEWMA, ConsistentHash, BoundedLoad,
|
|
6
|
+
* WeightedRandom) are added one per session.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
/** The single source-of-truth version stamp. */
|
|
@@ -68,3 +68,40 @@ export class RoundRobinBalancer extends BalancerBase {
|
|
|
68
68
|
/** Next eligible index in round-robin order, or `PICK_NONE` when the pool is down. */
|
|
69
69
|
pick(): number;
|
|
70
70
|
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* SmoothWRRBalancer -- nginx-style smooth weighted round-robin (M2). Distributes picks by
|
|
74
|
+
* caller-configured integer weights, interleaved smoothly (weights [5,1,1] -> A,A,B,A,C,A,A).
|
|
75
|
+
* Owns its smoothing accumulators; the sole writer of the weights via `setWeight`. O(cap)
|
|
76
|
+
* per pick, 0 B/op. Fails closed (`PICK_NONE`) when the eligible-weight sum is 0.
|
|
77
|
+
*/
|
|
78
|
+
export class SmoothWRRBalancer extends BalancerBase {
|
|
79
|
+
/**
|
|
80
|
+
* @param capacity endpoint count (fixed).
|
|
81
|
+
* @param eligible shared view: 1 = pickable, 0 = down (length >= capacity).
|
|
82
|
+
* @param weights per-endpoint weights (length >= capacity); mutate only via setWeight.
|
|
83
|
+
*/
|
|
84
|
+
constructor(capacity: number, eligible: Uint8Array, weights: Uint32Array);
|
|
85
|
+
/** Cold path: reconfigure endpoint `i`'s weight, keeping the eligible-weight total exact. */
|
|
86
|
+
setWeight(i: number, w: number): void;
|
|
87
|
+
/** Next endpoint by smooth weighting, or `PICK_NONE` when the eligible-weight sum is 0. */
|
|
88
|
+
pick(): number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* P2cBalancer -- power-of-two-choices (M3), the headline strategy. Draws two distinct
|
|
93
|
+
* eligible endpoints at random and returns the one with the lower in-flight load; the
|
|
94
|
+
* `ln ln n / ln 2` peak-load ceiling. In-flight counts are the caller's Uint32Array
|
|
95
|
+
* (read-only to `pick()`). O(1) per pick, 0 B/op. Fails closed (`PICK_NONE`) when down.
|
|
96
|
+
*/
|
|
97
|
+
export class P2cBalancer extends BalancerBase {
|
|
98
|
+
/**
|
|
99
|
+
* @param capacity endpoint count (fixed).
|
|
100
|
+
* @param eligible shared view: 1 = pickable, 0 = down (length >= capacity).
|
|
101
|
+
* @param inflight per-endpoint in-flight counts (length >= capacity), caller-owned, read-only.
|
|
102
|
+
* @param seed deterministic PRNG seed (default 0x9e3779b9); reproducible benches.
|
|
103
|
+
*/
|
|
104
|
+
constructor(capacity: number, eligible: Uint8Array, inflight: Uint32Array, seed?: number);
|
|
105
|
+
/** Pick by power-of-two-choices (lower in-flight of two random eligibles), or `PICK_NONE`. */
|
|
106
|
+
pick(): number;
|
|
107
|
+
}
|
package/Pick.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @zakkster/lite-pick -- zero-GC load-balancing SELECTION KERNEL.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* M3 (0.3.0): substrate seams + RoundRobin + SmoothWRR + P2C (the headline). This file ships:
|
|
5
5
|
*
|
|
6
6
|
* - VERSION the single source-of-truth version stamp (3-place sync).
|
|
7
7
|
* - PICK_NONE the fail-closed sentinel (-1): "no endpoint", never a dead pick.
|
|
@@ -13,21 +13,24 @@
|
|
|
13
13
|
* It does NOT implement pick() -- strategies subclass it.
|
|
14
14
|
* - RoundRobinBalancer the baseline strategy: a wrapping cursor that forward-scans
|
|
15
15
|
* the eligibility view, skipping down nodes, O(1) amortized, 0 B/op.
|
|
16
|
+
* - SmoothWRRBalancer the weighted default: nginx smooth weighted round-robin over
|
|
17
|
+
* caller-configured integer weights, O(cap)/pick, 0 B/op.
|
|
18
|
+
* - P2cBalancer the headline: power-of-two-choices over caller-owned in-flight counts;
|
|
19
|
+
* the ln ln n balance ceiling, O(1)/pick, 0 B/op.
|
|
16
20
|
*
|
|
17
21
|
* The identity (decisions/0001): lite-pick OWNS NO mutable state it can avoid owning.
|
|
18
22
|
* It reads pre-allocated views (eligibility, inflight, weights, scores) that siblings or
|
|
19
23
|
* the caller write, and returns an integer index. Health, circuit state, and load
|
|
20
24
|
* counters live OUTSIDE the kernel. The steady-state pick path allocates 0 B/op.
|
|
21
25
|
*
|
|
22
|
-
* Roster (one strategy per session -- see ROADMAP.md): RoundRobin [
|
|
23
|
-
*
|
|
24
|
-
* WeightedRandom [planned].
|
|
26
|
+
* Roster (one strategy per session -- see ROADMAP.md): RoundRobin [M1], SmoothWRR [M2],
|
|
27
|
+
* P2C [M3], LeastConn/SED/NQ, PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom [planned].
|
|
25
28
|
*
|
|
26
29
|
* Zero runtime dependencies. node:test only. ESM, single file, tree-shakeable.
|
|
27
30
|
*/
|
|
28
31
|
|
|
29
32
|
/** Version stamp. Synced across package.json and llms.txt (three-place rule). */
|
|
30
|
-
export const VERSION = '0.
|
|
33
|
+
export const VERSION = '0.3.0';
|
|
31
34
|
|
|
32
35
|
/**
|
|
33
36
|
* Fail-closed sentinel returned by pick() when no endpoint is eligible.
|
|
@@ -202,3 +205,181 @@ export class RoundRobinBalancer extends BalancerBase {
|
|
|
202
205
|
return PICK_NONE;
|
|
203
206
|
}
|
|
204
207
|
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* SmoothWRRBalancer -- nginx-style smooth weighted round-robin (M2).
|
|
211
|
+
*
|
|
212
|
+
* Distributes picks by caller-configured integer weights, spreading them SMOOTHLY over
|
|
213
|
+
* time rather than in bursts: weights [5, 1, 1] yield A, A, B, A, C, A, A -- not the
|
|
214
|
+
* A A A A A B C clumping of naive weight-expansion WRR. Each pick adds every eligible
|
|
215
|
+
* node's weight to its accumulator, takes the node with the highest accumulator, and
|
|
216
|
+
* subtracts the total eligible weight from it (the nginx `current += weight; pick max;
|
|
217
|
+
* current -= total` algorithm).
|
|
218
|
+
*
|
|
219
|
+
* Ownership (ADR 0001, ADR 0004): this is the first strategy that owns ALGORITHM state --
|
|
220
|
+
* the per-endpoint smoothing accumulators (`_current`, a Float64Array; Float64 absorbs the
|
|
221
|
+
* sum of uint32 weights without overflow and is 0 B/op on the hot path). Weights live in
|
|
222
|
+
* the caller's Uint32Array, but the balancer is the SOLE writer via the cold `setWeight()`,
|
|
223
|
+
* which keeps `_totalEligibleWeight` exact; mutating the weights array directly desyncs the
|
|
224
|
+
* total (documented UB). An eligibility toggle maintains the total AND resets the toggled
|
|
225
|
+
* node's accumulator (ADR 0004: no stale credit across an eligibility epoch -- anti-flap
|
|
226
|
+
* aligned, ADR 0002).
|
|
227
|
+
*
|
|
228
|
+
* Bound: O(cap) per pick (one scan of the pool -- SmoothWRR is inherently linear in the
|
|
229
|
+
* pool size, negligible at real endpoint counts), zero-alloc. Fails closed (PICK_NONE)
|
|
230
|
+
* when the eligible-weight sum is 0 -- whole pool down, or every eligible node's weight 0.
|
|
231
|
+
*/
|
|
232
|
+
export class SmoothWRRBalancer extends BalancerBase {
|
|
233
|
+
/**
|
|
234
|
+
* @param {number} capacity endpoint count (fixed).
|
|
235
|
+
* @param {Uint8Array} eligible shared view: 1 = pickable, 0 = down (length >= capacity).
|
|
236
|
+
* @param {Uint32Array} weights per-endpoint weights (length >= capacity); the balancer
|
|
237
|
+
* is the sole writer via setWeight() -- direct mutation desyncs the total (UB).
|
|
238
|
+
*/
|
|
239
|
+
constructor(capacity, eligible, weights) {
|
|
240
|
+
super(capacity, eligible);
|
|
241
|
+
if (!(weights instanceof Uint32Array) || weights.length < capacity) {
|
|
242
|
+
throw new RangeError('[lite-pick] weights must be a Uint32Array of length >= capacity');
|
|
243
|
+
}
|
|
244
|
+
this._weights = weights;
|
|
245
|
+
this._current = new Float64Array(capacity);
|
|
246
|
+
let total = 0;
|
|
247
|
+
for (let i = 0; i < capacity; i++) if (eligible[i]) total += weights[i];
|
|
248
|
+
this._totalEligibleWeight = total;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Cold path: mark endpoint i up/down, maintaining the eligible-weight total and
|
|
253
|
+
* RESETTING the toggled node's accumulator (no stale credit across an eligibility
|
|
254
|
+
* epoch, ADR 0004). Zero-alloc.
|
|
255
|
+
* @param {number} i
|
|
256
|
+
* @param {boolean} up
|
|
257
|
+
*/
|
|
258
|
+
setEligible(i, up) {
|
|
259
|
+
const was = this.isEligible(i);
|
|
260
|
+
super.setEligible(i, up); // validates range, flips the bit, updates _live
|
|
261
|
+
const now = this._eligible[i] !== 0;
|
|
262
|
+
if (was !== now) {
|
|
263
|
+
this._totalEligibleWeight += now ? this._weights[i] : -this._weights[i];
|
|
264
|
+
this._current[i] = 0; // reset on every eligibility transition
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Cold path: reconfigure endpoint i's weight, keeping the eligible-weight total exact.
|
|
270
|
+
* @param {number} i
|
|
271
|
+
* @param {number} w new weight (uint32)
|
|
272
|
+
*/
|
|
273
|
+
setWeight(i, w) {
|
|
274
|
+
if (i < 0 || i >= this._cap) throw new RangeError('[lite-pick] index out of range: ' + i);
|
|
275
|
+
const nw = w >>> 0;
|
|
276
|
+
if (nw !== w) throw new RangeError('[lite-pick] weight must be a uint32: ' + w);
|
|
277
|
+
const old = this._weights[i];
|
|
278
|
+
if (nw === old) return;
|
|
279
|
+
this._weights[i] = nw;
|
|
280
|
+
if (this._eligible[i]) this._totalEligibleWeight += nw - old;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Next endpoint by smooth weighting, or PICK_NONE (fail closed). O(cap), zero-alloc.
|
|
285
|
+
* @returns {number}
|
|
286
|
+
*/
|
|
287
|
+
pick() {
|
|
288
|
+
const total = this._totalEligibleWeight;
|
|
289
|
+
if (total <= 0) return PICK_NONE; // whole pool down, or all eligible weights 0
|
|
290
|
+
const cap = this._cap, el = this._eligible, wt = this._weights, cur = this._current;
|
|
291
|
+
let best = -1, bestCur = -Infinity;
|
|
292
|
+
for (let i = 0; i < cap; i++) {
|
|
293
|
+
if (el[i]) {
|
|
294
|
+
const c = cur[i] + wt[i];
|
|
295
|
+
cur[i] = c;
|
|
296
|
+
if (c > bestCur) { bestCur = c; best = i; }
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
cur[best] -= total; // best >= 0 guaranteed while total > 0
|
|
300
|
+
return best;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* P2cBalancer -- power-of-two-choices (M3), the headline strategy.
|
|
306
|
+
*
|
|
307
|
+
* `pick()` draws TWO distinct eligible endpoints uniformly at random and returns the one
|
|
308
|
+
* with the lower in-flight load. One extra probe over pure random buys an exponential drop
|
|
309
|
+
* in peak load: the max load stays within `ln ln n / ln 2 + O(1)` of the mean (Azar-Broder-
|
|
310
|
+
* Karlin-Upfal 1994), versus random's `ln n / ln ln n` gap. That additive `ln ln n` ceiling
|
|
311
|
+
* -- proven in test/balance.mjs against a random foil -- is the library's analytical anchor.
|
|
312
|
+
*
|
|
313
|
+
* Ownership (ADR 0001, ADR 0005): in-flight counts live in the CALLER's Uint32Array, read-
|
|
314
|
+
* only to `pick()` (the caller / the M5 lite-query adapter increments on dispatch, decrements
|
|
315
|
+
* on settle -- lite-pick holds no request state). The eligible draw is REJECTION SAMPLING
|
|
316
|
+
* over the shared bitmap: no peer, no owned draw-set, expected O(1) draws when eligibility is
|
|
317
|
+
* dense (the common case), a bounded retry + a zero-alloc rotated linear-scan fallback for the
|
|
318
|
+
* degenerate sparse case. A true worst-case-O(1) draw via lite-o1 `RandomSet` is a deferred
|
|
319
|
+
* optional-peer optimization (ADR 0005), added only if sparse-eligibility measurement demands.
|
|
320
|
+
*
|
|
321
|
+
* Bound: O(d) = O(1) with d = 2 (two expected-O(1) draws + one compare). Steady-state pick():
|
|
322
|
+
* a few PRNG steps + array reads, no object/closure/array created -- proven 0 B/op by
|
|
323
|
+
* test/torture.mjs and test/perf/PerfGate.test.mjs. Fails closed (PICK_NONE) when the whole
|
|
324
|
+
* pool is down.
|
|
325
|
+
*/
|
|
326
|
+
export class P2cBalancer extends BalancerBase {
|
|
327
|
+
/**
|
|
328
|
+
* @param {number} capacity endpoint count (fixed).
|
|
329
|
+
* @param {Uint8Array} eligible shared view: 1 = pickable, 0 = down (length >= capacity).
|
|
330
|
+
* @param {Uint32Array} inflight per-endpoint in-flight counts (length >= capacity),
|
|
331
|
+
* caller-owned and only READ here.
|
|
332
|
+
* @param {number} [seed=0x9e3779b9] deterministic PRNG seed (reproducible benches).
|
|
333
|
+
*/
|
|
334
|
+
constructor(capacity, eligible, inflight, seed = 0x9e3779b9) {
|
|
335
|
+
super(capacity, eligible);
|
|
336
|
+
if (!(inflight instanceof Uint32Array) || inflight.length < capacity) {
|
|
337
|
+
throw new RangeError('[lite-pick] inflight must be a Uint32Array of length >= capacity');
|
|
338
|
+
}
|
|
339
|
+
this._inflight = inflight;
|
|
340
|
+
this._rng = new Prng(seed);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* A uniformly random ELIGIBLE index, or PICK_NONE if none. Expected O(1) (rejection
|
|
345
|
+
* sampling); a rotated linear scan from a random start is the zero-alloc fallback under
|
|
346
|
+
* degenerate sparsity (unbiased first-eligible-after-a-random-offset). Internal.
|
|
347
|
+
* @returns {number}
|
|
348
|
+
*/
|
|
349
|
+
_draw() {
|
|
350
|
+
if (this._live === 0) return PICK_NONE;
|
|
351
|
+
const cap = this._cap, el = this._eligible;
|
|
352
|
+
for (let tries = 0; tries < 64; tries++) {
|
|
353
|
+
const i = this._rng.nextBelow(cap);
|
|
354
|
+
if (el[i]) return i;
|
|
355
|
+
}
|
|
356
|
+
// Degenerate (very sparse eligibility): scan from a random start, wrapping, and
|
|
357
|
+
// return the first eligible found. Zero-alloc; live > 0 guarantees a hit.
|
|
358
|
+
let i = this._rng.nextBelow(cap);
|
|
359
|
+
for (let k = 0; k < cap; k++) {
|
|
360
|
+
if (el[i]) return i;
|
|
361
|
+
i++;
|
|
362
|
+
if (i >= cap) i = 0;
|
|
363
|
+
}
|
|
364
|
+
return PICK_NONE;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Pick an endpoint by power-of-two-choices, or PICK_NONE (fail closed). O(1).
|
|
369
|
+
* @returns {number}
|
|
370
|
+
*/
|
|
371
|
+
pick() {
|
|
372
|
+
const a = this._draw();
|
|
373
|
+
if (a < 0) return PICK_NONE; // whole pool down: fail closed
|
|
374
|
+
if (this._live === 1) return a; // only one eligible: it is both choices
|
|
375
|
+
// Draw a DISTINCT second choice. A bounded redraw (not a single nudge) keeps the
|
|
376
|
+
// two-choices property intact even at tiny pool sizes, where a single retry collides
|
|
377
|
+
// often: at live>=2 each redraw misses with probability <= 1/2, so 32 tries leaves a
|
|
378
|
+
// ~2^-32 collision chance -- while staying expected-O(1) (about two draws) and 0 B/op.
|
|
379
|
+
let b = this._draw();
|
|
380
|
+
for (let t = 0; b === a && t < 32; t++) b = this._draw();
|
|
381
|
+
if (b < 0 || b === a) return a; // astronomically rare: fall back to the first draw
|
|
382
|
+
// Lower in-flight wins; ties go to the first draw (unbiased over many picks).
|
|
383
|
+
return this._inflight[b] < this._inflight[a] ? b : a;
|
|
384
|
+
}
|
|
385
|
+
}
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @zakkster/lite-pick
|
|
2
2
|
|
|
3
|
-
> Zero-GC load-balancing **selection kernel**: one hot `pick()` that returns an endpoint **index** over a fixed pool and allocates **0 B/op** on the steady-state path. A pure selector, never a proxy -- it consumes health and circuit state, it never owns them. **v0.
|
|
3
|
+
> Zero-GC load-balancing **selection kernel**: one hot `pick()` that returns an endpoint **index** over a fixed pool and allocates **0 B/op** on the steady-state path. A pure selector, never a proxy -- it consumes health and circuit state, it never owns them. **v0.3.0 ships three strategies -- `RoundRobinBalancer`, `SmoothWRRBalancer`, and `P2cBalancer`** (power-of-two-choices, the headline) -- on the substrate seams (`VERSION`, `PICK_NONE`, a deterministic `Prng`, and `BalancerBase`'s shared read-only eligibility view). The rest of the roster -- LeastConn/SED/NQ, PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom -- lands one per session.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@zakkster/lite-pick)
|
|
6
6
|
[](https://github.com/sponsors/PeshoVurtoleta)
|
|
@@ -21,7 +21,7 @@ The npm landscape has old algorithm libraries (`load-balancers`, `loadbalance`,
|
|
|
21
21
|
- **Two pieces of evidence, both shipped.** A **0 B/op** witness on the pick path (no object, closure, string, or array created per pick), and a measured **balance-quality anchor** -- peak-to-average load within the strategy's theoretical ceiling (for P2C, the Azar-Broder-Karlin-Upfal `ln ln n / ln 2` bound) and strictly better than a random foil.
|
|
22
22
|
- **A pure selector, not a proxy.** It **consumes** health and circuit state; it never owns them. Health is a shared read-only bitmap written by [`@zakkster/lite-di-health`](https://www.npmjs.com/package/@zakkster/lite-di-health); circuit state comes from [`@zakkster/lite-statechart`](https://www.npmjs.com/package/@zakkster/lite-statechart); load counters are caller-owned typed arrays. `pick()` only reads.
|
|
23
23
|
|
|
24
|
-
> **Status:
|
|
24
|
+
> **Status: M3 (v0.3.0).** Ships the substrate seams **plus `RoundRobinBalancer`, `SmoothWRRBalancer`, and `P2cBalancer`**. Every strategy is gated: `pick()` proven **0 B/op** (torture + PerfGate), RoundRobin **perfectly fair** with **zero dead picks** vs the naive `i++ % n` foil, SmoothWRR **exactly weighted** and **smooth**, and **P2C proves the `ln ln n` balance ceiling** -- peak-to-mean gap ~2 vs a random foil's ~21 at n=1024, holding flat as the pool grows. See [ROADMAP.md](./ROADMAP.md) for the M3 -> M10 path to 1.0.0, and [decisions/](./decisions) for the ownership boundary (ADR 0001), anti-flapping (ADR 0002), and the RoundRobin (0003), SmoothWRR (0004), and P2C (0005) design forks.
|
|
25
25
|
|
|
26
26
|
```bash
|
|
27
27
|
npm install @zakkster/lite-pick
|
|
@@ -54,6 +54,54 @@ rr.pick() === PICK_NONE; // -> true (-1)
|
|
|
54
54
|
|
|
55
55
|
Every `pick()` above allocates **0 bytes**, owns only an integer cursor, and reads the one shared eligibility view (no second copy to drift). Over a run it hands each *live* endpoint an equal share -- true round-robin over the eligible set, not the raw index space.
|
|
56
56
|
|
|
57
|
+
## SmoothWRR (v0.2.0)
|
|
58
|
+
|
|
59
|
+
The weighted default -- nginx's *smooth* weighted round-robin. Weighted picks are **interleaved evenly** instead of clumped, so a heavy endpoint doesn't get a burst of consecutive requests.
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import { SmoothWRRBalancer } from '@zakkster/lite-pick';
|
|
63
|
+
|
|
64
|
+
const eligible = Uint8Array.from([1, 1, 1]);
|
|
65
|
+
const weights = Uint32Array.from([5, 1, 1]); // A is 5x
|
|
66
|
+
|
|
67
|
+
const wrr = new SmoothWRRBalancer(3, eligible, weights);
|
|
68
|
+
|
|
69
|
+
const seq = Array.from({ length: 7 }, () => wrr.pick());
|
|
70
|
+
// -> [0, 0, 1, 0, 2, 0, 0] smooth: A A B A C A A (not A A A A A B C)
|
|
71
|
+
// over 7 picks: A=5, B=1, C=1 -- exactly the weights
|
|
72
|
+
|
|
73
|
+
// Reweight on the cold path (the balancer stays the sole writer of the weights):
|
|
74
|
+
wrr.setWeight(1, 4); // B is now 4x
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`pick()` is **0 B/op** and **O(cap)** (one scan of the pool -- negligible at real endpoint counts). It owns its smoothing accumulators; weights live in your `Uint32Array` but you mutate them only through `setWeight`, which keeps the internal eligible-weight total exact. Marking a node down/up resets its accumulator, so a recovered node rejoins neutral -- no stale burst or starvation ([ADR 0004](./decisions/0004-smoothwrr-weight-ownership.md)).
|
|
78
|
+
|
|
79
|
+
## P2C -- power-of-two-choices (v0.3.0, the headline)
|
|
80
|
+
|
|
81
|
+
Two random eligible draws, take the one with lower in-flight load. That single extra probe buys an **exponential** drop in peak load -- the max stays within an *additive* `ln ln n / ln 2` of the mean, versus random's `ln n / ln ln n` gap.
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
import { P2cBalancer } from '@zakkster/lite-pick';
|
|
85
|
+
|
|
86
|
+
const eligible = Uint8Array.from([1, 1, 1, 1]);
|
|
87
|
+
const inflight = new Uint32Array(4); // YOU own this; pick() only reads it
|
|
88
|
+
|
|
89
|
+
const p2c = new P2cBalancer(4, eligible, inflight);
|
|
90
|
+
|
|
91
|
+
const i = p2c.pick(); // the lower-loaded of two random eligibles
|
|
92
|
+
inflight[i]++; // you increment on dispatch...
|
|
93
|
+
// ...and inflight[i]-- when the request settles (the M5 lite-query adapter will do this)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The proof (from `test/balance.mjs`, the library's analytical anchor):
|
|
97
|
+
|
|
98
|
+
| pool `n` | P2C peak/avg | random foil peak/avg |
|
|
99
|
+
|---|---|---|
|
|
100
|
+
| 1024 | **1.06** (gap 2) | 1.66 (gap 21) |
|
|
101
|
+
| 4096 | **1.06** (gap 2) | 1.78 (gap 25) |
|
|
102
|
+
|
|
103
|
+
P2C's gap stays a small `ln ln n` constant while the random foil's grows with the pool. `pick()` is **0 B/op** and **O(1)** (two expected-O(1) rejection draws + a compare); in-flight counts are your caller-owned `Uint32Array` ([ADR 0005](./decisions/0005-p2c-draw.md)).
|
|
104
|
+
|
|
57
105
|
## The substrate (under every strategy)
|
|
58
106
|
|
|
59
107
|
```js
|
|
@@ -79,27 +127,10 @@ rng.nextBelow(4); // -> a uint32 in [0, 4)
|
|
|
79
127
|
rng.reset(); // replays the exact stream
|
|
80
128
|
|
|
81
129
|
PICK_NONE; // -> -1 (fail-closed sentinel: no endpoint, never a dead pick)
|
|
82
|
-
VERSION; // -> '0.
|
|
130
|
+
VERSION; // -> '0.3.0'
|
|
83
131
|
```
|
|
84
132
|
|
|
85
|
-
`BalancerBase.pick()` is **abstract** -- it throws, so an unfinished strategy fails loudly rather than returning a dead index
|
|
86
|
-
|
|
87
|
-
```js
|
|
88
|
-
// SHAPE ONLY -- not shipped until M3. Two random eligible draws, return the lower
|
|
89
|
-
// in-flight load. One extra probe over random buys the ln ln n balance ceiling.
|
|
90
|
-
class P2cBalancer extends BalancerBase {
|
|
91
|
-
constructor(capacity, eligible, inflight, seed) {
|
|
92
|
-
super(capacity, eligible);
|
|
93
|
-
this._inflight = inflight; // caller-owned Uint32Array; pick() only reads it
|
|
94
|
-
this._rng = new Prng(seed);
|
|
95
|
-
}
|
|
96
|
-
pick() {
|
|
97
|
-
if (this.live === 0) return PICK_NONE; // fail closed
|
|
98
|
-
const a = this._draw(), b = this._draw(); // two distinct eligible draws
|
|
99
|
-
return this._inflight[b] < this._inflight[a] ? b : a;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
```
|
|
133
|
+
`BalancerBase.pick()` is **abstract** -- it throws, so an unfinished strategy fails loudly rather than returning a dead index. Every shipped strategy (`RoundRobinBalancer`, `SmoothWRRBalancer`, `P2cBalancer`) extends it and reads the same shared eligibility view; you subclass it the same way to add your own.
|
|
103
134
|
|
|
104
135
|
## Design ownership (ratified before any strategy)
|
|
105
136
|
|
package/llms.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @zakkster/lite-pick
|
|
2
2
|
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
License: MIT (c) Zahary Shinikchiev <shinikchiev@yahoo.com>
|
|
5
5
|
Runtime dependencies: none. ESM only. ASCII-only source. sideEffects: false.
|
|
6
6
|
Node: >= 18.
|
|
@@ -14,10 +14,11 @@ reading pre-allocated views that siblings or the caller write, and returning an
|
|
|
14
14
|
The complementary evidence lite-pick ships is a measured balance-quality anchor (peak-to-
|
|
15
15
|
average load vs the strategy's theoretical ceiling) alongside the 0 B/op pick witness.
|
|
16
16
|
|
|
17
|
-
0.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
0.3.0 ships the substrate seams + three strategies: RoundRobin, SmoothWRR (the weighted
|
|
18
|
+
default), and P2C (power-of-two-choices, the headline). It exports `VERSION`, the fail-closed
|
|
19
|
+
sentinel `PICK_NONE` (-1), a deterministic `Prng` (xorshift32), `BalancerBase` (the shared
|
|
20
|
+
read-only eligibility seam + O(1) live count), `RoundRobinBalancer`, `SmoothWRRBalancer`, and
|
|
21
|
+
`P2cBalancer`. The remaining strategies land one per session (see ROADMAP.md): LeastConn/SED/NQ,
|
|
21
22
|
PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom.
|
|
22
23
|
|
|
23
24
|
## Design ownership (decisions/0001, 0002)
|
|
@@ -57,6 +58,27 @@ PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom.
|
|
|
57
58
|
forward-scans the eligibility view, skipping down nodes), or `PICK_NONE` when the pool is
|
|
58
59
|
down. O(1) amortized, O(cap) worst case under sparse eligibility, 0 B/op. Owns only its
|
|
59
60
|
cursor; no eligible-set structure (ADR 0003 chose the stateless bitmap-scan path for M1).
|
|
61
|
+
- `SmoothWRRBalancer extends BalancerBase` -- class. The weighted default (M2).
|
|
62
|
+
- `new SmoothWRRBalancer(capacity, eligible, weights)` -- `weights` is a Uint32Array
|
|
63
|
+
(length >= capacity); the balancer is the sole writer via setWeight (direct mutation
|
|
64
|
+
desyncs the total -- UB).
|
|
65
|
+
- `setWeight(i, w)` -> void. COLD. Reconfigure endpoint i's weight (uint32), keeping the
|
|
66
|
+
eligible-weight total exact.
|
|
67
|
+
- `setEligible(i, up)` -> void. COLD. Overrides the base to maintain the eligible-weight
|
|
68
|
+
total and reset the toggled node's accumulator (no stale credit across an epoch, ADR 0004).
|
|
69
|
+
- `pick()` -> number. Next endpoint by nginx smooth weighted round-robin (`current +=
|
|
70
|
+
weight; pick max; current -= total`), interleaved smoothly (weights [5,1,1] ->
|
|
71
|
+
A,A,B,A,C,A,A), or `PICK_NONE` when the eligible-weight sum is 0. O(cap) per pick, 0 B/op.
|
|
72
|
+
Owns its Float64Array smoothing accumulators (ADR 0004).
|
|
73
|
+
- `P2cBalancer extends BalancerBase` -- class. The headline strategy (M3): power-of-two-choices.
|
|
74
|
+
- `new P2cBalancer(capacity, eligible, inflight, seed?=0x9e3779b9)` -- `inflight` is a
|
|
75
|
+
caller-owned Uint32Array (length >= capacity), read-only to pick(): the caller / the M5
|
|
76
|
+
lite-query adapter increments on dispatch, decrements on settle. `seed` seeds the internal
|
|
77
|
+
deterministic PRNG (reproducible benches).
|
|
78
|
+
- `pick()` -> number. Draws two DISTINCT eligible endpoints uniformly at random (rejection
|
|
79
|
+
sampling over the bitmap, no peer) and returns the one with the lower in-flight load; ties
|
|
80
|
+
to the first draw. The `ln ln n / ln 2` peak-load ceiling (ADR 0005). O(1) per pick, 0 B/op.
|
|
81
|
+
`PICK_NONE` when the whole pool is down.
|
|
60
82
|
|
|
61
83
|
## Gates (every session)
|
|
62
84
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zakkster/lite-pick",
|
|
3
3
|
"author": "Zahary Shinikchiev <shinikchiev@yahoo.com>",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"description": "Zero-dependency, zero-GC load-balancing selection kernel: one hot pick() -> endpoint index over a fixed pool, 0 B/op steady-state. A pure selector (consumes health/circuit state, never a proxy) for the in-process hop, complementary to AWS NLB/ALB. Tree-shakeable ESM roster: RoundRobin, SmoothWRR, P2C, least-conn, PeakEWMA, consistent hashing.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./Pick.js",
|