@kylewillett/troupe 0.1.1 → 0.2.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 +16 -1
- package/README.md +129 -3
- package/dist/ask.d.ts +5 -1
- package/dist/ask.js +16 -0
- package/dist/backstage/index.d.ts +2 -1
- package/dist/backstage/index.js +1 -0
- package/dist/backstage/source.d.ts +1 -1
- package/dist/backstage/source.js +65 -2
- package/dist/backstage/types.d.ts +65 -4
- package/dist/backstage/types.js +2 -1
- package/dist/backstage-ui/assets/index-CG896RLZ.css +1 -0
- package/dist/backstage-ui/assets/index-DF_Xnc1u.js +9 -0
- package/dist/backstage-ui/index.html +2 -2
- package/dist/cell.d.ts +16 -14
- package/dist/cell.js +38 -13
- package/dist/checks.d.ts +10 -0
- package/dist/checks.js +34 -2
- package/dist/cluster/index.d.ts +6 -0
- package/dist/cluster/index.js +4 -0
- package/dist/cluster/leases.d.ts +37 -0
- package/dist/cluster/leases.js +132 -0
- package/dist/cluster/membership.d.ts +100 -0
- package/dist/cluster/membership.js +559 -0
- package/dist/cluster/memory.d.ts +31 -0
- package/dist/cluster/memory.js +187 -0
- package/dist/cluster/placement.d.ts +10 -0
- package/dist/cluster/placement.js +30 -0
- package/dist/cluster/postgres/index.d.ts +1 -0
- package/dist/cluster/postgres/index.js +3 -0
- package/dist/cluster/postgres/schema.d.ts +7 -0
- package/dist/cluster/postgres/schema.js +33 -0
- package/dist/cluster/postgres/schema.sql +18 -0
- package/dist/cluster/postgres/store.d.ts +28 -0
- package/dist/cluster/postgres/store.js +124 -0
- package/dist/cluster/region.d.ts +103 -0
- package/dist/cluster/region.js +843 -0
- package/dist/cluster/remoting.d.ts +79 -0
- package/dist/cluster/remoting.js +755 -0
- package/dist/cluster/shard-memory.d.ts +14 -0
- package/dist/cluster/shard-memory.js +92 -0
- package/dist/cluster/shard-store.d.ts +62 -0
- package/dist/cluster/shard-store.js +36 -0
- package/dist/cluster/shard.d.ts +54 -0
- package/dist/cluster/shard.js +278 -0
- package/dist/cluster/sharding.d.ts +109 -0
- package/dist/cluster/sharding.js +278 -0
- package/dist/cluster/table.d.ts +29 -0
- package/dist/cluster/table.js +74 -0
- package/dist/cluster/transport.d.ts +29 -0
- package/dist/cluster/transport.js +4 -0
- package/dist/cluster/wire.d.ts +90 -0
- package/dist/cluster/wire.js +177 -0
- package/dist/hash.d.ts +7 -0
- package/dist/hash.js +25 -0
- package/dist/internal.d.ts +134 -2
- package/dist/internal.js +12 -1
- package/dist/json.d.ts +44 -0
- package/dist/json.js +143 -0
- package/dist/node/cluster/frames.d.ts +31 -0
- package/dist/node/cluster/frames.js +73 -0
- package/dist/node/cluster/index.d.ts +1 -0
- package/dist/node/cluster/index.js +3 -0
- package/dist/node/cluster/tcp.d.ts +101 -0
- package/dist/node/cluster/tcp.js +416 -0
- package/dist/persistence/actor.js +1 -1
- package/dist/persistence/backend.d.ts +1 -10
- package/dist/persistence/backend.js +4 -34
- package/dist/persistence/postgres/index.d.ts +2 -1
- package/dist/persistence/postgres/index.js +1 -1
- package/dist/persistence/postgres/pool.d.ts +44 -0
- package/dist/persistence/postgres/pool.js +65 -0
- package/dist/persistence/postgres/postgres.d.ts +1 -33
- package/dist/persistence/postgres/postgres.js +4 -63
- package/dist/system.d.ts +1 -1
- package/dist/system.js +190 -24
- package/dist/types.d.ts +86 -3
- package/package.json +21 -2
- package/dist/backstage-ui/assets/index-Bg0MCiqs.css +0 -1
- package/dist/backstage-ui/assets/index-CeefBsDh.js +0 -9
- package/dist/persistence/json.d.ts +0 -7
- package/dist/persistence/json.js +0 -75
|
@@ -0,0 +1,843 @@
|
|
|
1
|
+
// An entity type's region, at /system/sharding/<type> on every node that initialises the type
|
|
2
|
+
// (decision 0030). Regions route: a message for an entity goes to its shard here if this node
|
|
3
|
+
// holds the shard's lease, to the holder's region if another node does, and otherwise waits here,
|
|
4
|
+
// in order, while the region finds the shard a home. Regions talk to each other with ordinary
|
|
5
|
+
// remote messages, so the routing is visible as everything else is.
|
|
6
|
+
//
|
|
7
|
+
// Finding a home always starts from the store, which is the only thing that says who holds a
|
|
8
|
+
// lease. If nobody does, the shard's desired owner by rendezvous hashing acquires it: this region,
|
|
9
|
+
// or the one it asks with `start`. A region that gets a message for a shard it doesn't hold sends
|
|
10
|
+
// it back with `notOwner`, and the sender looks again. Every wait and retry is on the scheduler,
|
|
11
|
+
// with jitter from the system's random (decision 0003).
|
|
12
|
+
import { actor } from '../actor.js';
|
|
13
|
+
import { internalsOf } from '../internal.js';
|
|
14
|
+
import { supervise } from '../supervision.js';
|
|
15
|
+
import { desiredOwner, shardOf } from './placement.js';
|
|
16
|
+
import { entityPath, shardDef, ShardInternal, } from './shard.js';
|
|
17
|
+
import { keyOf } from './table.js';
|
|
18
|
+
import { parseRefUri } from './wire.js';
|
|
19
|
+
/** A map that calls `onChange` whenever an entry is added, changed or removed. */
|
|
20
|
+
class WatchedMap extends Map {
|
|
21
|
+
#onChange;
|
|
22
|
+
constructor(onChange) {
|
|
23
|
+
super();
|
|
24
|
+
this.#onChange = onChange;
|
|
25
|
+
}
|
|
26
|
+
set(key, value) {
|
|
27
|
+
const changed = !this.has(key) || this.get(key) !== value;
|
|
28
|
+
super.set(key, value);
|
|
29
|
+
if (changed)
|
|
30
|
+
this.#onChange();
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
delete(key) {
|
|
34
|
+
const deleted = super.delete(key);
|
|
35
|
+
if (deleted)
|
|
36
|
+
this.#onChange();
|
|
37
|
+
return deleted;
|
|
38
|
+
}
|
|
39
|
+
clear() {
|
|
40
|
+
const had = this.size > 0;
|
|
41
|
+
super.clear();
|
|
42
|
+
if (had)
|
|
43
|
+
this.#onChange();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** A message only this node can send its region: none decodes from the wire as one. */
|
|
47
|
+
export class Internal {
|
|
48
|
+
msg;
|
|
49
|
+
constructor(msg) {
|
|
50
|
+
this.msg = msg;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const BACKOFF_MIN_MS = 100;
|
|
54
|
+
const BACKOFF_MAX_MS = 2_000;
|
|
55
|
+
// A message sent back this many times is a dead letter: its shard is moving faster than it.
|
|
56
|
+
const MAX_BOUNCES = 5;
|
|
57
|
+
// How many backoffs a region waits for a hello from every up member before it places a shard
|
|
58
|
+
// without the silent ones: about a second. A member that never initialises the type costs that
|
|
59
|
+
// once per shard, and a hello one round trip, so nodes that start together agree on placement.
|
|
60
|
+
const HELLO_WAIT_ATTEMPTS = 4;
|
|
61
|
+
/** The region actor's definition. Its routing state lives with the incarnation, not in state. */
|
|
62
|
+
export function regionDef(env) {
|
|
63
|
+
let region;
|
|
64
|
+
return actor({
|
|
65
|
+
initial: (ctx) => {
|
|
66
|
+
region = new Region(env, ctx);
|
|
67
|
+
return null;
|
|
68
|
+
},
|
|
69
|
+
receive(state, msg) {
|
|
70
|
+
region?.receive(msg);
|
|
71
|
+
return state;
|
|
72
|
+
},
|
|
73
|
+
onSignal(state, signal, ctx) {
|
|
74
|
+
if (signal.type === 'terminated')
|
|
75
|
+
region?.shardStopped(signal.ref);
|
|
76
|
+
else if (signal.type === 'postStop') {
|
|
77
|
+
region?.stopped(ctx);
|
|
78
|
+
env.onStopped();
|
|
79
|
+
}
|
|
80
|
+
return state;
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
class Region {
|
|
85
|
+
#env;
|
|
86
|
+
#ctx;
|
|
87
|
+
#self;
|
|
88
|
+
#prefix;
|
|
89
|
+
#holders;
|
|
90
|
+
#local = new Map();
|
|
91
|
+
#slots = new Map();
|
|
92
|
+
#peers = new Map();
|
|
93
|
+
#greeted = new Set();
|
|
94
|
+
#refs = new Map();
|
|
95
|
+
// Shards being handed off, from the drain until their messages have gone to the new home.
|
|
96
|
+
#handoffs = new Set();
|
|
97
|
+
#members = new Map();
|
|
98
|
+
#stopReason = 'leaving';
|
|
99
|
+
// The node is leaving: hand every shard off, then stop.
|
|
100
|
+
#leaving = false;
|
|
101
|
+
constructor(env, ctx) {
|
|
102
|
+
this.#env = env;
|
|
103
|
+
this.#ctx = ctx;
|
|
104
|
+
const { address, uid } = env.remoting.self;
|
|
105
|
+
this.#self = { address, key: keyOf(address, uid) };
|
|
106
|
+
this.#prefix = `${env.type}/`;
|
|
107
|
+
this.#holders = new WatchedMap(env.changed);
|
|
108
|
+
env.showHolders(this.#holders);
|
|
109
|
+
// Every region ticks, to greet members it hasn't heard from. Only hosting ones rebalance.
|
|
110
|
+
ctx.timers.startPeriodic('rebalance', new Internal({ t: 'rebalance' }), env.rebalanceMs);
|
|
111
|
+
}
|
|
112
|
+
receive(msg) {
|
|
113
|
+
if (msg instanceof Internal) {
|
|
114
|
+
this.#internal(msg.msg);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const m = msg;
|
|
118
|
+
if (!isRecord(m)) {
|
|
119
|
+
this.#ignore('a message that isn’t an object');
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
switch (m.t) {
|
|
123
|
+
case 'deliver':
|
|
124
|
+
this.#onDeliver(m);
|
|
125
|
+
return;
|
|
126
|
+
case 'hello':
|
|
127
|
+
this.#onHello(m);
|
|
128
|
+
return;
|
|
129
|
+
case 'start':
|
|
130
|
+
if (this.#isShard(m.shard) && isRef(m.back))
|
|
131
|
+
this.#onStart(m.shard, m.back);
|
|
132
|
+
else
|
|
133
|
+
this.#ignore('a malformed start');
|
|
134
|
+
return;
|
|
135
|
+
case 'home':
|
|
136
|
+
if (this.#isShard(m.shard) && (m.holder === null || typeof m.holder === 'string')) {
|
|
137
|
+
this.#onHome(m.shard, m.holder);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
this.#ignore('a malformed home');
|
|
141
|
+
}
|
|
142
|
+
return;
|
|
143
|
+
case 'notOwner':
|
|
144
|
+
this.#onNotOwner(m);
|
|
145
|
+
return;
|
|
146
|
+
default:
|
|
147
|
+
this.#ignore(`a "${String(m.t)}" message`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
#internal(msg) {
|
|
151
|
+
switch (msg.t) {
|
|
152
|
+
case 'members':
|
|
153
|
+
this.#onMembers();
|
|
154
|
+
return;
|
|
155
|
+
case 'retry':
|
|
156
|
+
this.#onRetry(msg.shard);
|
|
157
|
+
return;
|
|
158
|
+
case 'holders':
|
|
159
|
+
this.#onHolders(msg.shard, msg.result);
|
|
160
|
+
return;
|
|
161
|
+
case 'acquired':
|
|
162
|
+
this.#onAcquired(msg.shard, msg.result);
|
|
163
|
+
return;
|
|
164
|
+
case 'lost':
|
|
165
|
+
this.#onLost(msg.shard, msg.token);
|
|
166
|
+
return;
|
|
167
|
+
case 'released':
|
|
168
|
+
this.#onReleased(msg.shard);
|
|
169
|
+
return;
|
|
170
|
+
case 'rebalance':
|
|
171
|
+
this.#onRebalance();
|
|
172
|
+
return;
|
|
173
|
+
case 'claim':
|
|
174
|
+
this.#onClaim(msg.result);
|
|
175
|
+
return;
|
|
176
|
+
case 'stop':
|
|
177
|
+
this.#onStop(msg.reason);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// ---- routing ----
|
|
182
|
+
#onDeliver(m) {
|
|
183
|
+
const { id, m: text, from, bounces, back } = m;
|
|
184
|
+
if (typeof id !== 'string' ||
|
|
185
|
+
typeof text !== 'string' ||
|
|
186
|
+
(from !== undefined && typeof from !== 'string') ||
|
|
187
|
+
(bounces !== undefined && (typeof bounces !== 'number' || !Number.isSafeInteger(bounces)))) {
|
|
188
|
+
this.#ignore('a malformed deliver');
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const envelope = {
|
|
192
|
+
id,
|
|
193
|
+
m: text,
|
|
194
|
+
...(from === undefined ? {} : { from }),
|
|
195
|
+
...(bounces === undefined ? {} : { bounces }),
|
|
196
|
+
};
|
|
197
|
+
const shard = shardOf(id, this.#env.shardCount);
|
|
198
|
+
if (back === undefined) {
|
|
199
|
+
this.#route(shard, envelope, true);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (!isRef(back)) {
|
|
203
|
+
this.#ignore('a deliver whose back isn’t a ref');
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
// From another region, which thinks this node holds the shard.
|
|
207
|
+
if (this.#deliverLocally(shard, envelope))
|
|
208
|
+
return;
|
|
209
|
+
const slot = this.#slots.get(shard);
|
|
210
|
+
if (slot !== undefined && slot.phase !== 'idle') {
|
|
211
|
+
// This region is finding the shard a home already: it may well be here.
|
|
212
|
+
this.#enqueue(shard, envelope, false);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
asRegion(back).tell({ t: 'notOwner', shard, holder: this.#self.key, envelopes: [envelope] });
|
|
216
|
+
}
|
|
217
|
+
/** Sends `envelope` on towards its shard, or holds it here until the shard has a home. */
|
|
218
|
+
#route(shard, envelope, begin) {
|
|
219
|
+
if (this.#deliverLocally(shard, envelope))
|
|
220
|
+
return;
|
|
221
|
+
const holder = this.#holders.get(shard);
|
|
222
|
+
if (holder !== undefined && holder !== this.#self.key && this.#usable(holder)) {
|
|
223
|
+
const region = this.#regionOf(holder);
|
|
224
|
+
if (region !== undefined) {
|
|
225
|
+
region.tell({ t: 'deliver', ...envelope, back: this.#ctx.self });
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
this.#enqueue(shard, envelope, begin);
|
|
230
|
+
}
|
|
231
|
+
/** Hands `envelope` to the local shard, if this node runs it and still holds its lease. */
|
|
232
|
+
#deliverLocally(shard, envelope) {
|
|
233
|
+
const local = this.#local.get(shard);
|
|
234
|
+
if (local === undefined || local.ending !== undefined)
|
|
235
|
+
return false;
|
|
236
|
+
if (this.#env.keeper?.holds(this.#lease(shard)) !== true)
|
|
237
|
+
return false;
|
|
238
|
+
const msg = envelope.from === undefined
|
|
239
|
+
? { t: 'deliver', id: envelope.id, m: envelope.m }
|
|
240
|
+
: { t: 'deliver', id: envelope.id, m: envelope.m, from: envelope.from };
|
|
241
|
+
asShard(local.ref).tell(msg);
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
#enqueue(shard, envelope, begin) {
|
|
245
|
+
const slot = this.#slot(shard);
|
|
246
|
+
if (slot.buffer.length >= this.#env.bufferSize) {
|
|
247
|
+
this.#deadLetter(envelope, 'unreachable');
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
slot.buffer.push(envelope);
|
|
251
|
+
if (begin && slot.phase === 'idle')
|
|
252
|
+
this.#begin(shard, slot);
|
|
253
|
+
}
|
|
254
|
+
#slot(shard) {
|
|
255
|
+
let slot = this.#slots.get(shard);
|
|
256
|
+
if (slot === undefined) {
|
|
257
|
+
slot = { buffer: [], phase: 'idle', attempt: 0, waiters: [], refusedBy: undefined };
|
|
258
|
+
this.#slots.set(shard, slot);
|
|
259
|
+
}
|
|
260
|
+
return slot;
|
|
261
|
+
}
|
|
262
|
+
/** Starts finding the shard a home: first, ask the store who holds it. */
|
|
263
|
+
#begin(shard, slot) {
|
|
264
|
+
if (slot.buffer.length === 0 && slot.waiters.length === 0) {
|
|
265
|
+
this.#dropSlot(shard);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (this.#local.has(shard)) {
|
|
269
|
+
// Its last incarnation here is still stopping.
|
|
270
|
+
this.#backoff(shard, slot);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
slot.phase = 'checking';
|
|
274
|
+
this.#ctx.pipeToSelf(this.#env.store.holders(this.#prefix), (result) => new Internal({ t: 'holders', shard, result }));
|
|
275
|
+
}
|
|
276
|
+
#onHolders(shard, result) {
|
|
277
|
+
if (result.ok)
|
|
278
|
+
this.#learn(result.value);
|
|
279
|
+
const slot = this.#slots.get(shard);
|
|
280
|
+
if (slot?.phase !== 'checking')
|
|
281
|
+
return;
|
|
282
|
+
if (!result.ok) {
|
|
283
|
+
this.#ctx.log.warn(`sharding: could not read the holders of ${this.#env.type}`, {
|
|
284
|
+
error: result.error,
|
|
285
|
+
});
|
|
286
|
+
this.#backoff(shard, slot);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const holder = this.#holders.get(shard);
|
|
290
|
+
if (holder !== undefined &&
|
|
291
|
+
holder !== this.#self.key &&
|
|
292
|
+
holder !== slot.refusedBy &&
|
|
293
|
+
this.#usable(holder)) {
|
|
294
|
+
this.#flush(shard, slot);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
this.#place(shard, slot);
|
|
298
|
+
}
|
|
299
|
+
/** Replaces the cache with the store's holders, keeping the shards this node runs. */
|
|
300
|
+
#learn(holders) {
|
|
301
|
+
const seen = new Set();
|
|
302
|
+
for (const { lease, holder } of holders) {
|
|
303
|
+
if (!lease.startsWith(this.#prefix))
|
|
304
|
+
continue;
|
|
305
|
+
const shard = Number(lease.slice(this.#prefix.length));
|
|
306
|
+
if (!this.#isShard(shard))
|
|
307
|
+
continue;
|
|
308
|
+
seen.add(shard);
|
|
309
|
+
if (!this.#local.has(shard))
|
|
310
|
+
this.#holders.set(shard, holder);
|
|
311
|
+
}
|
|
312
|
+
for (const shard of [...this.#holders.keys()]) {
|
|
313
|
+
if (!seen.has(shard) && !this.#local.has(shard))
|
|
314
|
+
this.#holders.delete(shard);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/** Nobody usable holds the shard: its desired owner should take it. */
|
|
318
|
+
#place(shard, slot) {
|
|
319
|
+
if (slot.attempt < HELLO_WAIT_ATTEMPTS && this.#silentPeers()) {
|
|
320
|
+
this.#backoff(shard, slot);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const owner = desiredOwner(this.#lease(shard), this.#candidates());
|
|
324
|
+
if (owner === undefined && this.#leaving) {
|
|
325
|
+
// Nowhere to go: what waits becomes dead letters as the region stops.
|
|
326
|
+
this.#handoffs.delete(shard);
|
|
327
|
+
slot.phase = 'idle';
|
|
328
|
+
this.#checkLeft();
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
if (owner === undefined) {
|
|
332
|
+
this.#backoff(shard, slot);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (owner === this.#self.address) {
|
|
336
|
+
this.#acquire(shard, slot);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const member = [...this.#members.values()].find((m) => m.address === owner && m.status === 'up');
|
|
340
|
+
const region = member === undefined ? undefined : this.#regionAt(member.address, member.uid);
|
|
341
|
+
if (region === undefined) {
|
|
342
|
+
this.#backoff(shard, slot);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
region.tell({ t: 'start', shard, back: this.#ctx.self });
|
|
346
|
+
// The timer is the ask's timeout: no home by then, and the region looks again.
|
|
347
|
+
this.#backoff(shard, slot);
|
|
348
|
+
slot.phase = 'asking';
|
|
349
|
+
}
|
|
350
|
+
#acquire(shard, slot) {
|
|
351
|
+
const keeper = this.#env.keeper;
|
|
352
|
+
if (keeper === undefined) {
|
|
353
|
+
this.#backoff(shard, slot);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
slot.phase = 'acquiring';
|
|
357
|
+
this.#ctx.pipeToSelf(keeper.acquire(this.#lease(shard)), (result) => new Internal({ t: 'acquired', shard, result }));
|
|
358
|
+
}
|
|
359
|
+
#onAcquired(shard, result) {
|
|
360
|
+
const slot = this.#slot(shard);
|
|
361
|
+
const waiters = slot.waiters;
|
|
362
|
+
slot.waiters = [];
|
|
363
|
+
let holder = null;
|
|
364
|
+
if (!result.ok) {
|
|
365
|
+
this.#ctx.log.warn(`sharding: could not acquire ${this.#lease(shard)}`, {
|
|
366
|
+
error: result.error,
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
else if (result.value.acquired) {
|
|
370
|
+
this.#startShard(shard, result.value.token);
|
|
371
|
+
holder = this.#self.key;
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
holder = result.value.holder;
|
|
375
|
+
this.#holders.set(shard, holder);
|
|
376
|
+
}
|
|
377
|
+
for (const back of waiters)
|
|
378
|
+
asRegion(back).tell({ t: 'home', shard, holder });
|
|
379
|
+
if (holder === null) {
|
|
380
|
+
this.#backoff(shard, slot);
|
|
381
|
+
}
|
|
382
|
+
else if (holder === slot.refusedBy) {
|
|
383
|
+
// The holder that sent these back still has the lease in the store, though it has let the
|
|
384
|
+
// shard go: wait a backoff for it to be released or expire, rather than bounce them
|
|
385
|
+
// straight back. Only once, in case it has taken the shard back since.
|
|
386
|
+
slot.refusedBy = undefined;
|
|
387
|
+
this.#backoff(shard, slot);
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
this.#flush(shard, slot);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
#startShard(shard, token) {
|
|
394
|
+
const env = this.#env;
|
|
395
|
+
env.hooks.publish({
|
|
396
|
+
type: 'shardAcquired',
|
|
397
|
+
entityType: env.type,
|
|
398
|
+
shard,
|
|
399
|
+
node: this.#self.key,
|
|
400
|
+
token,
|
|
401
|
+
});
|
|
402
|
+
const ref = this.#ctx.spawn(shardDef(env, this.#lease(shard)), String(shard), {
|
|
403
|
+
supervision: supervise.resume(),
|
|
404
|
+
});
|
|
405
|
+
this.#ctx.watch(ref);
|
|
406
|
+
this.#local.set(shard, { ref, token, ending: undefined, handingOff: false, released: false });
|
|
407
|
+
this.#holders.set(shard, this.#self.key);
|
|
408
|
+
}
|
|
409
|
+
/** Routes what waited, now the shard has a home. What still can't go backs off and retries. */
|
|
410
|
+
#flush(shard, slot) {
|
|
411
|
+
const waiting = slot.buffer;
|
|
412
|
+
slot.buffer = [];
|
|
413
|
+
slot.phase = 'idle';
|
|
414
|
+
for (const envelope of waiting)
|
|
415
|
+
this.#route(shard, envelope, false);
|
|
416
|
+
if (slot.buffer.length > 0) {
|
|
417
|
+
this.#backoff(shard, slot);
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
// A handoff is over once what waited for it has gone on.
|
|
421
|
+
if (this.#handoffs.delete(shard))
|
|
422
|
+
this.#checkLeft();
|
|
423
|
+
if (slot.waiters.length === 0) {
|
|
424
|
+
this.#ctx.timers.cancel(retryKey(shard));
|
|
425
|
+
this.#dropSlot(shard);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
#dropSlot(shard) {
|
|
429
|
+
this.#slots.delete(shard);
|
|
430
|
+
if (this.#handoffs.delete(shard))
|
|
431
|
+
this.#checkLeft();
|
|
432
|
+
}
|
|
433
|
+
#backoff(shard, slot) {
|
|
434
|
+
slot.attempt++;
|
|
435
|
+
slot.phase = 'waiting';
|
|
436
|
+
const ceiling = Math.min(BACKOFF_MAX_MS, BACKOFF_MIN_MS * 2 ** (slot.attempt - 1));
|
|
437
|
+
const delay = Math.round(ceiling * (0.5 + this.#env.hooks.random() / 2));
|
|
438
|
+
this.#ctx.timers.startSingle(retryKey(shard), new Internal({ t: 'retry', shard }), delay);
|
|
439
|
+
}
|
|
440
|
+
#onRetry(shard) {
|
|
441
|
+
const slot = this.#slots.get(shard);
|
|
442
|
+
if (slot === undefined || (slot.phase !== 'waiting' && slot.phase !== 'asking'))
|
|
443
|
+
return;
|
|
444
|
+
this.#begin(shard, slot);
|
|
445
|
+
}
|
|
446
|
+
#onStart(shard, back) {
|
|
447
|
+
if (this.#local.has(shard)) {
|
|
448
|
+
const holds = this.#local.get(shard)?.ending === undefined;
|
|
449
|
+
asRegion(back).tell({ t: 'home', shard, holder: holds ? this.#self.key : null });
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
if (!this.#env.host || this.#env.keeper === undefined || this.#leaving) {
|
|
453
|
+
asRegion(back).tell({ t: 'home', shard, holder: null });
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
const slot = this.#slot(shard);
|
|
457
|
+
slot.waiters.push(back);
|
|
458
|
+
if (slot.phase === 'acquiring')
|
|
459
|
+
return;
|
|
460
|
+
this.#ctx.timers.cancel(retryKey(shard));
|
|
461
|
+
this.#acquire(shard, slot);
|
|
462
|
+
}
|
|
463
|
+
#onHome(shard, holder) {
|
|
464
|
+
if (holder !== null && holder !== this.#self.key && !this.#local.has(shard)) {
|
|
465
|
+
this.#holders.set(shard, holder);
|
|
466
|
+
}
|
|
467
|
+
const slot = this.#slots.get(shard);
|
|
468
|
+
if (slot?.phase !== 'asking')
|
|
469
|
+
return;
|
|
470
|
+
this.#ctx.timers.cancel(retryKey(shard));
|
|
471
|
+
slot.refusedBy = undefined;
|
|
472
|
+
if (holder === null)
|
|
473
|
+
this.#backoff(shard, slot);
|
|
474
|
+
else
|
|
475
|
+
this.#flush(shard, slot);
|
|
476
|
+
}
|
|
477
|
+
#onNotOwner(m) {
|
|
478
|
+
const { shard, holder, envelopes } = m;
|
|
479
|
+
if (!this.#isShard(shard) || typeof holder !== 'string' || !Array.isArray(envelopes)) {
|
|
480
|
+
this.#ignore('a malformed notOwner');
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
if (this.#holders.get(shard) === holder)
|
|
484
|
+
this.#holders.delete(shard);
|
|
485
|
+
const slot = this.#slot(shard);
|
|
486
|
+
slot.refusedBy = holder;
|
|
487
|
+
for (const item of envelopes) {
|
|
488
|
+
if (!isEnvelope(item))
|
|
489
|
+
continue;
|
|
490
|
+
const bounces = (item.bounces ?? 0) + 1;
|
|
491
|
+
if (bounces > MAX_BOUNCES) {
|
|
492
|
+
this.#ctx.log.warn(`sharding: a message to ${entityPath(this.#env.type, item.id)} came back ${String(bounces)} times`, { id: item.id });
|
|
493
|
+
this.#deadLetter(item, 'unreachable');
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
this.#enqueue(shard, { ...item, bounces }, true);
|
|
497
|
+
}
|
|
498
|
+
if (slot.buffer.length === 0 && slot.phase === 'idle' && slot.waiters.length === 0) {
|
|
499
|
+
this.#dropSlot(shard);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
// ---- shards this node runs ----
|
|
503
|
+
#onLost(shard, token) {
|
|
504
|
+
const local = this.#local.get(shard);
|
|
505
|
+
if (local?.token !== token || local.released)
|
|
506
|
+
return;
|
|
507
|
+
// At the deadline, whatever else is going on, a handoff included: the entities stop now.
|
|
508
|
+
local.ending ??= 'leaseLost';
|
|
509
|
+
local.released = true;
|
|
510
|
+
this.#released(shard, token, 'leaseLost');
|
|
511
|
+
this.#holders.delete(shard);
|
|
512
|
+
this.#ctx.stop(local.ref);
|
|
513
|
+
}
|
|
514
|
+
shardStopped(ref) {
|
|
515
|
+
for (const [shard, local] of this.#local) {
|
|
516
|
+
if (local.ref !== ref)
|
|
517
|
+
continue;
|
|
518
|
+
this.#local.delete(shard);
|
|
519
|
+
if (local.ending === undefined) {
|
|
520
|
+
// Shards resume after a failure, so only a stop the region didn't ask for gets here.
|
|
521
|
+
this.#ctx.log.error(`sharding: shard ${this.#lease(shard)} stopped by itself`);
|
|
522
|
+
this.#release(shard, local, this.#stopReason);
|
|
523
|
+
}
|
|
524
|
+
if (this.#holders.get(shard) === this.#self.key)
|
|
525
|
+
this.#holders.delete(shard);
|
|
526
|
+
if (local.handingOff) {
|
|
527
|
+
// Its entities have all stopped: now the lease can go, and then the messages follow.
|
|
528
|
+
if (local.released)
|
|
529
|
+
this.#onReleased(shard);
|
|
530
|
+
else
|
|
531
|
+
this.#release(shard, local, local.ending ?? 'handoff', this.#ctx, true);
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
const slot = this.#slots.get(shard);
|
|
535
|
+
if (slot?.phase === 'waiting')
|
|
536
|
+
this.#begin(shard, slot);
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
/** The region has stopped, after its shards: their entities are gone, so their leases go. */
|
|
541
|
+
stopped(ctx) {
|
|
542
|
+
for (const [shard, local] of this.#local) {
|
|
543
|
+
if (!local.released)
|
|
544
|
+
this.#release(shard, local, local.ending ?? this.#stopReason, ctx);
|
|
545
|
+
}
|
|
546
|
+
this.#local.clear();
|
|
547
|
+
this.#handoffs.clear();
|
|
548
|
+
for (const slot of this.#slots.values()) {
|
|
549
|
+
for (const envelope of slot.buffer)
|
|
550
|
+
this.#deadLetter(envelope, 'stopped');
|
|
551
|
+
}
|
|
552
|
+
this.#slots.clear();
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Publishes the shard's release and gives its lease up. With `then`, tells the region
|
|
556
|
+
* `released` once the store has answered, either way, so a handoff can go on.
|
|
557
|
+
*/
|
|
558
|
+
#release(shard, local, reason, ctx = this.#ctx, then = false) {
|
|
559
|
+
local.released = true;
|
|
560
|
+
this.#released(shard, local.token, reason);
|
|
561
|
+
const keeper = this.#env.keeper;
|
|
562
|
+
if (keeper === undefined)
|
|
563
|
+
return;
|
|
564
|
+
const lease = this.#lease(shard);
|
|
565
|
+
const releasing = keeper.release(lease).catch((error) => {
|
|
566
|
+
// It expires in the store instead.
|
|
567
|
+
this.#env.hooks.log.warn(`sharding: could not release ${lease}`, { error });
|
|
568
|
+
});
|
|
569
|
+
if (then)
|
|
570
|
+
ctx.pipeToSelf(releasing, () => new Internal({ t: 'released', shard }));
|
|
571
|
+
else
|
|
572
|
+
internalsOf(ctx).track(releasing);
|
|
573
|
+
}
|
|
574
|
+
#released(shard, token, reason) {
|
|
575
|
+
this.#env.hooks.publish({
|
|
576
|
+
type: 'shardReleased',
|
|
577
|
+
entityType: this.#env.type,
|
|
578
|
+
shard,
|
|
579
|
+
node: this.#self.key,
|
|
580
|
+
token,
|
|
581
|
+
reason,
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
// ---- rebalancing and handoff ----
|
|
585
|
+
/**
|
|
586
|
+
* Hands off shards whose desired owner is now another node, a few at a time. With `remember`,
|
|
587
|
+
* also takes the shards nobody holds that this node should own, so their remembered entities
|
|
588
|
+
* start without waiting for a message.
|
|
589
|
+
*/
|
|
590
|
+
#onRebalance() {
|
|
591
|
+
this.#greetSilent();
|
|
592
|
+
if (this.#leaving || this.#env.keeper === undefined)
|
|
593
|
+
return;
|
|
594
|
+
if (this.#env.remember && !this.#silentPeers()) {
|
|
595
|
+
this.#ctx.pipeToSelf(this.#env.store.holders(this.#prefix), (result) => new Internal({ t: 'claim', result }));
|
|
596
|
+
}
|
|
597
|
+
const candidates = this.#candidates();
|
|
598
|
+
for (const [shard, local] of this.#local) {
|
|
599
|
+
if (this.#handoffs.size >= this.#env.maxHandoffs)
|
|
600
|
+
return;
|
|
601
|
+
if (local.ending !== undefined)
|
|
602
|
+
continue;
|
|
603
|
+
const owner = desiredOwner(this.#lease(shard), candidates);
|
|
604
|
+
if (owner !== undefined && owner !== this.#self.address) {
|
|
605
|
+
this.#handOff(shard, local, 'handoff');
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
#onClaim(result) {
|
|
610
|
+
if (!result.ok) {
|
|
611
|
+
this.#ctx.log.warn(`sharding: could not read the holders of ${this.#env.type}`, {
|
|
612
|
+
error: result.error,
|
|
613
|
+
});
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
if (this.#leaving)
|
|
617
|
+
return;
|
|
618
|
+
this.#learn(result.value);
|
|
619
|
+
const held = new Set(result.value.map(({ lease }) => lease));
|
|
620
|
+
const candidates = this.#candidates();
|
|
621
|
+
for (let shard = 0; shard < this.#env.shardCount; shard++) {
|
|
622
|
+
const lease = this.#lease(shard);
|
|
623
|
+
if (held.has(lease) || this.#local.has(shard) || this.#slots.has(shard))
|
|
624
|
+
continue;
|
|
625
|
+
if (desiredOwner(lease, candidates) !== this.#self.address)
|
|
626
|
+
continue;
|
|
627
|
+
this.#acquire(shard, this.#slot(shard));
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Starts moving a shard: its messages wait here from now on, and its entities stop once
|
|
632
|
+
* they've handled what they have, a persist included. The lease goes only after that.
|
|
633
|
+
*/
|
|
634
|
+
#handOff(shard, local, reason) {
|
|
635
|
+
local.ending = reason;
|
|
636
|
+
local.handingOff = true;
|
|
637
|
+
this.#handoffs.add(shard);
|
|
638
|
+
const slot = this.#slot(shard);
|
|
639
|
+
this.#ctx.timers.cancel(retryKey(shard));
|
|
640
|
+
slot.phase = 'handoff';
|
|
641
|
+
asShard(local.ref).tell(new ShardInternal({ t: 'drain' }));
|
|
642
|
+
}
|
|
643
|
+
/** A handed-off shard's lease is gone: its desired owner takes it, and what waited follows. */
|
|
644
|
+
#onReleased(shard) {
|
|
645
|
+
const slot = this.#slots.get(shard);
|
|
646
|
+
if (slot?.phase !== 'handoff')
|
|
647
|
+
return;
|
|
648
|
+
slot.phase = 'idle';
|
|
649
|
+
slot.attempt = 0;
|
|
650
|
+
slot.refusedBy = undefined;
|
|
651
|
+
this.#place(shard, slot);
|
|
652
|
+
}
|
|
653
|
+
#onStop(reason) {
|
|
654
|
+
this.#stopReason = reason;
|
|
655
|
+
if (reason === 'downed') {
|
|
656
|
+
this.#ctx.stop();
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
if (this.#leaving)
|
|
660
|
+
return;
|
|
661
|
+
this.#leaving = true;
|
|
662
|
+
this.#members = new Map(this.#env.members().map((m) => [keyOf(m.address, m.uid), m]));
|
|
663
|
+
this.#ctx.timers.cancel('rebalance');
|
|
664
|
+
for (const [shard, local] of this.#local) {
|
|
665
|
+
if (local.ending === undefined)
|
|
666
|
+
this.#handOff(shard, local, 'leaving');
|
|
667
|
+
}
|
|
668
|
+
this.#checkLeft();
|
|
669
|
+
}
|
|
670
|
+
/** A leaving region stops once every shard it held has gone, with its messages. */
|
|
671
|
+
#checkLeft() {
|
|
672
|
+
if (this.#leaving && this.#local.size === 0 && this.#handoffs.size === 0)
|
|
673
|
+
this.#ctx.stop();
|
|
674
|
+
}
|
|
675
|
+
// ---- members and peers ----
|
|
676
|
+
#onMembers() {
|
|
677
|
+
this.#members = new Map(this.#env.members().map((m) => [keyOf(m.address, m.uid), m]));
|
|
678
|
+
for (const [key, member] of this.#members) {
|
|
679
|
+
if (key === this.#self.key || member.status === 'down' || this.#greeted.has(key))
|
|
680
|
+
continue;
|
|
681
|
+
this.#greeted.add(key);
|
|
682
|
+
this.#regionAt(member.address, member.uid)?.tell(this.#hello(false));
|
|
683
|
+
}
|
|
684
|
+
this.#kick();
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Says hello again to each up, reachable member whose hello this region hasn't had. Remoting is
|
|
688
|
+
* at most once, so a hello lost both ways, as connections come and go around a restart, would
|
|
689
|
+
* otherwise leave that member out of placement for good (found by T8, decision 0031).
|
|
690
|
+
*/
|
|
691
|
+
#greetSilent() {
|
|
692
|
+
for (const [key, member] of this.#members) {
|
|
693
|
+
if (key === this.#self.key || member.status !== 'up' || !member.reachable)
|
|
694
|
+
continue;
|
|
695
|
+
if (this.#peers.get(member.address)?.uid === member.uid)
|
|
696
|
+
continue;
|
|
697
|
+
this.#regionAt(member.address, member.uid)?.tell(this.#hello(false));
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
#hello(reply) {
|
|
701
|
+
const { shardCount, host } = this.#env;
|
|
702
|
+
const hello = { t: 'hello', shardCount, host, region: this.#ctx.self };
|
|
703
|
+
return reply ? { ...hello, reply: true } : hello;
|
|
704
|
+
}
|
|
705
|
+
#onHello(m) {
|
|
706
|
+
const { shardCount, host, region, reply } = m;
|
|
707
|
+
const address = isRef(region) ? parseRefUri(region.path) : undefined;
|
|
708
|
+
if (address === undefined ||
|
|
709
|
+
typeof shardCount !== 'number' ||
|
|
710
|
+
typeof host !== 'boolean' ||
|
|
711
|
+
address.address === this.#self.address) {
|
|
712
|
+
this.#ignore('a malformed hello');
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
const refused = shardCount !== this.#env.shardCount;
|
|
716
|
+
const known = this.#peers.get(address.address);
|
|
717
|
+
this.#peers.set(address.address, { uid: address.uid, host, refused });
|
|
718
|
+
if (refused && !(known?.uid === address.uid && known.refused)) {
|
|
719
|
+
this.#ctx.log.error(`sharding: refused ${address.address} for ${this.#env.type}: its shardCount is ` +
|
|
720
|
+
`${String(shardCount)}, and this node's is ${String(this.#env.shardCount)}`, { address: address.address, shardCount, expected: this.#env.shardCount });
|
|
721
|
+
}
|
|
722
|
+
const key = keyOf(address.address, address.uid);
|
|
723
|
+
if (reply !== true)
|
|
724
|
+
asRegion(region).tell(this.#hello(true));
|
|
725
|
+
this.#greeted.add(key);
|
|
726
|
+
this.#kick();
|
|
727
|
+
}
|
|
728
|
+
/** Members or peers changed: shards that were backing off look again now. */
|
|
729
|
+
#kick() {
|
|
730
|
+
for (const [shard, slot] of this.#slots) {
|
|
731
|
+
if (slot.phase !== 'waiting')
|
|
732
|
+
continue;
|
|
733
|
+
this.#ctx.timers.cancel(retryKey(shard));
|
|
734
|
+
this.#begin(shard, slot);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
/** Whether an up, reachable member hasn't said yet whether it hosts the type. */
|
|
738
|
+
#silentPeers() {
|
|
739
|
+
for (const [key, member] of this.#members) {
|
|
740
|
+
if (key === this.#self.key || member.status !== 'up' || !member.reachable)
|
|
741
|
+
continue;
|
|
742
|
+
if (this.#peers.get(member.address)?.uid !== member.uid)
|
|
743
|
+
return true;
|
|
744
|
+
}
|
|
745
|
+
return false;
|
|
746
|
+
}
|
|
747
|
+
/** The addresses that may own a shard: up, reachable from here, and hosting the type. */
|
|
748
|
+
#candidates() {
|
|
749
|
+
const out = [];
|
|
750
|
+
for (const [key, member] of this.#members) {
|
|
751
|
+
if (member.status !== 'up' || !member.reachable)
|
|
752
|
+
continue;
|
|
753
|
+
if (key === this.#self.key) {
|
|
754
|
+
if (this.#env.host && !this.#leaving)
|
|
755
|
+
out.push(member.address);
|
|
756
|
+
continue;
|
|
757
|
+
}
|
|
758
|
+
const peer = this.#peers.get(member.address);
|
|
759
|
+
if (peer?.uid === member.uid && peer.host && !peer.refused)
|
|
760
|
+
out.push(member.address);
|
|
761
|
+
}
|
|
762
|
+
return out;
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Whether messages can go to the holder: a member this node can reach, that isn't down, and
|
|
766
|
+
* wasn't refused. Messages for a holder this node can't reach wait here and look again, rather
|
|
767
|
+
* than in remoting's buffer until the holder is downed, so a killed holder's shards answer from
|
|
768
|
+
* elsewhere as soon as its lease expires (found by T8, decision 0031).
|
|
769
|
+
*/
|
|
770
|
+
#usable(holder) {
|
|
771
|
+
const member = this.#members.get(holder);
|
|
772
|
+
if (member === undefined || member.status === 'down' || !member.reachable)
|
|
773
|
+
return false;
|
|
774
|
+
const peer = this.#peers.get(member.address);
|
|
775
|
+
return !(peer?.uid === member.uid && peer.refused);
|
|
776
|
+
}
|
|
777
|
+
#regionOf(holder) {
|
|
778
|
+
const member = this.#members.get(holder);
|
|
779
|
+
return member === undefined ? undefined : this.#regionAt(member.address, member.uid);
|
|
780
|
+
}
|
|
781
|
+
#regionAt(address, uid) {
|
|
782
|
+
const key = keyOf(address, uid);
|
|
783
|
+
let ref = this.#refs.get(key);
|
|
784
|
+
if (ref === undefined) {
|
|
785
|
+
const { cluster } = this.#env.remoting.self;
|
|
786
|
+
const uri = `troupe://${cluster}@${address}#${String(uid)}/system/sharding/${this.#env.type}`;
|
|
787
|
+
try {
|
|
788
|
+
ref = this.#env.remoting.ref(uri);
|
|
789
|
+
}
|
|
790
|
+
catch {
|
|
791
|
+
return undefined;
|
|
792
|
+
}
|
|
793
|
+
this.#refs.set(key, ref);
|
|
794
|
+
}
|
|
795
|
+
return asRegion(ref);
|
|
796
|
+
}
|
|
797
|
+
// ---- helpers ----
|
|
798
|
+
#lease(shard) {
|
|
799
|
+
return `${this.#prefix}${String(shard)}`;
|
|
800
|
+
}
|
|
801
|
+
#isShard(value) {
|
|
802
|
+
return (typeof value === 'number' &&
|
|
803
|
+
Number.isSafeInteger(value) &&
|
|
804
|
+
value >= 0 &&
|
|
805
|
+
value < this.#env.shardCount);
|
|
806
|
+
}
|
|
807
|
+
#deadLetter(envelope, reason) {
|
|
808
|
+
const decoded = this.#env.codec.decode(envelope.m);
|
|
809
|
+
this.#env.hooks.deadLetter(entityPath(this.#env.type, envelope.id), decoded.ok ? decoded.value : envelope.m, reason);
|
|
810
|
+
}
|
|
811
|
+
#ignore(what) {
|
|
812
|
+
this.#ctx.log.warn(`sharding: ${this.#env.type} ignored ${what}`);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
/** A shard's ref, with its protocol. */
|
|
816
|
+
function asShard(ref) {
|
|
817
|
+
return ref;
|
|
818
|
+
}
|
|
819
|
+
/** A region's ref, with its protocol: refs from the wire arrive as refs of no protocol. */
|
|
820
|
+
function asRegion(ref) {
|
|
821
|
+
return ref;
|
|
822
|
+
}
|
|
823
|
+
function retryKey(shard) {
|
|
824
|
+
return `retry-${String(shard)}`;
|
|
825
|
+
}
|
|
826
|
+
function isRecord(value) {
|
|
827
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
828
|
+
}
|
|
829
|
+
function isRef(value) {
|
|
830
|
+
return (isRecord(value) &&
|
|
831
|
+
typeof value.path === 'string' &&
|
|
832
|
+
typeof value.tell === 'function' &&
|
|
833
|
+
typeof value.ask === 'function');
|
|
834
|
+
}
|
|
835
|
+
function isEnvelope(value) {
|
|
836
|
+
if (!isRecord(value))
|
|
837
|
+
return false;
|
|
838
|
+
const { id, m, from, bounces } = value;
|
|
839
|
+
return (typeof id === 'string' &&
|
|
840
|
+
typeof m === 'string' &&
|
|
841
|
+
(from === undefined || typeof from === 'string') &&
|
|
842
|
+
(bounces === undefined || (typeof bounces === 'number' && Number.isSafeInteger(bounces))));
|
|
843
|
+
}
|