@toa.io/core 1.0.0-alpha.31 → 1.0.0-alpha.310

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.
Files changed (274) hide show
  1. package/package.json +28 -12
  2. package/transpiled/abandon.d.ts +9 -0
  3. package/transpiled/abandon.js +37 -0
  4. package/transpiled/abandon.js.map +1 -0
  5. package/transpiled/assignment.d.ts +6 -0
  6. package/transpiled/assignment.js +25 -0
  7. package/transpiled/assignment.js.map +1 -0
  8. package/transpiled/call.d.ts +10 -0
  9. package/transpiled/call.js +165 -0
  10. package/transpiled/call.js.map +1 -0
  11. package/transpiled/cascade.d.ts +7 -0
  12. package/transpiled/cascade.js +28 -0
  13. package/transpiled/cascade.js.map +1 -0
  14. package/transpiled/component.d.ts +15 -0
  15. package/transpiled/component.js +148 -0
  16. package/transpiled/component.js.map +1 -0
  17. package/transpiled/composition.d.ts +6 -0
  18. package/transpiled/composition.js +20 -0
  19. package/transpiled/composition.js.map +1 -0
  20. package/transpiled/connector.d.ts +71 -0
  21. package/transpiled/connector.js +248 -0
  22. package/transpiled/connector.js.map +1 -0
  23. package/transpiled/context.d.ts +25 -0
  24. package/transpiled/context.js +55 -0
  25. package/transpiled/context.js.map +1 -0
  26. package/transpiled/contract/component.d.ts +24 -0
  27. package/transpiled/contract/component.js +48 -0
  28. package/transpiled/contract/component.js.map +1 -0
  29. package/transpiled/contract/contract.d.ts +10 -0
  30. package/transpiled/contract/contract.js +14 -0
  31. package/transpiled/contract/contract.js.map +1 -0
  32. package/transpiled/contract/index.d.ts +3 -0
  33. package/transpiled/contract/index.js +4 -0
  34. package/transpiled/contract/index.js.map +1 -0
  35. package/transpiled/contract/reply.d.ts +7 -0
  36. package/transpiled/contract/reply.js +48 -0
  37. package/transpiled/contract/reply.js.map +1 -0
  38. package/transpiled/contract/request.d.ts +39 -0
  39. package/transpiled/contract/request.js +115 -0
  40. package/transpiled/contract/request.js.map +1 -0
  41. package/transpiled/contract/schemas.d.ts +4 -0
  42. package/transpiled/contract/schemas.js +36 -0
  43. package/transpiled/contract/schemas.js.map +1 -0
  44. package/transpiled/deliveries.d.ts +22 -0
  45. package/transpiled/deliveries.js +31 -0
  46. package/transpiled/deliveries.js.map +1 -0
  47. package/transpiled/effect.d.ts +7 -0
  48. package/transpiled/effect.js +13 -0
  49. package/transpiled/effect.js.map +1 -0
  50. package/transpiled/emission.d.ts +10 -0
  51. package/transpiled/emission.js +16 -0
  52. package/transpiled/emission.js.map +1 -0
  53. package/transpiled/encoded.d.ts +16 -0
  54. package/transpiled/encoded.js +25 -0
  55. package/transpiled/encoded.js.map +1 -0
  56. package/transpiled/entities/changeset.d.ts +10 -0
  57. package/transpiled/entities/changeset.js +27 -0
  58. package/transpiled/entities/changeset.js.map +1 -0
  59. package/transpiled/entities/entity.d.ts +19 -0
  60. package/transpiled/entities/entity.js +137 -0
  61. package/transpiled/entities/entity.js.map +1 -0
  62. package/transpiled/entities/factory.d.ts +27 -0
  63. package/transpiled/entities/factory.js +38 -0
  64. package/transpiled/entities/factory.js.map +1 -0
  65. package/transpiled/entities/index.d.ts +5 -0
  66. package/transpiled/entities/index.js +6 -0
  67. package/transpiled/entities/index.js.map +1 -0
  68. package/transpiled/entities/newid.d.ts +11 -0
  69. package/transpiled/entities/newid.js +41 -0
  70. package/transpiled/entities/newid.js.map +1 -0
  71. package/transpiled/entities/set.d.ts +10 -0
  72. package/transpiled/entities/set.js +19 -0
  73. package/transpiled/entities/set.js.map +1 -0
  74. package/transpiled/event.d.ts +14 -0
  75. package/transpiled/event.js +53 -0
  76. package/transpiled/event.js.map +1 -0
  77. package/transpiled/exceptions.d.ts +104 -0
  78. package/transpiled/exceptions.js +205 -0
  79. package/transpiled/exceptions.js.map +1 -0
  80. package/transpiled/gate.d.ts +40 -0
  81. package/transpiled/gate.js +84 -0
  82. package/transpiled/gate.js.map +1 -0
  83. package/transpiled/guard.d.ts +7 -0
  84. package/transpiled/guard.js +12 -0
  85. package/transpiled/guard.js.map +1 -0
  86. package/transpiled/halting.d.ts +16 -0
  87. package/transpiled/halting.js +25 -0
  88. package/transpiled/halting.js.map +1 -0
  89. package/transpiled/index.d.ts +41 -0
  90. package/transpiled/index.js +34 -0
  91. package/transpiled/index.js.map +1 -0
  92. package/transpiled/instance.d.ts +10 -0
  93. package/transpiled/instance.js +30 -0
  94. package/transpiled/instance.js.map +1 -0
  95. package/transpiled/locator.d.ts +12 -0
  96. package/transpiled/locator.js +32 -0
  97. package/transpiled/locator.js.map +1 -0
  98. package/transpiled/measurements.d.ts +20 -0
  99. package/transpiled/measurements.js +40 -0
  100. package/transpiled/measurements.js.map +1 -0
  101. package/transpiled/observation.d.ts +7 -0
  102. package/transpiled/observation.js +14 -0
  103. package/transpiled/observation.js.map +1 -0
  104. package/transpiled/operation.d.ts +69 -0
  105. package/transpiled/operation.js +174 -0
  106. package/transpiled/operation.js.map +1 -0
  107. package/transpiled/outbox.d.ts +45 -0
  108. package/transpiled/outbox.js +448 -0
  109. package/transpiled/outbox.js.map +1 -0
  110. package/transpiled/outbox.measurements.d.ts +4 -0
  111. package/transpiled/outbox.measurements.js +38 -0
  112. package/transpiled/outbox.measurements.js.map +1 -0
  113. package/transpiled/query/criteria.d.ts +6 -0
  114. package/transpiled/query/criteria.js +74 -0
  115. package/transpiled/query/criteria.js.map +1 -0
  116. package/transpiled/query/options.d.ts +3 -0
  117. package/transpiled/query/options.js +34 -0
  118. package/transpiled/query/options.js.map +1 -0
  119. package/transpiled/query.d.ts +9 -0
  120. package/transpiled/query.js +57 -0
  121. package/transpiled/query.js.map +1 -0
  122. package/transpiled/receiver.d.ts +20 -0
  123. package/transpiled/receiver.js +113 -0
  124. package/transpiled/receiver.js.map +1 -0
  125. package/transpiled/reflection.d.ts +9 -0
  126. package/transpiled/reflection.js +13 -0
  127. package/transpiled/reflection.js.map +1 -0
  128. package/transpiled/remote.d.ts +10 -0
  129. package/transpiled/remote.js +11 -0
  130. package/transpiled/remote.js.map +1 -0
  131. package/transpiled/safety.d.ts +5 -0
  132. package/transpiled/safety.js +26 -0
  133. package/transpiled/safety.js.map +1 -0
  134. package/transpiled/state.d.ts +36 -0
  135. package/transpiled/state.js +131 -0
  136. package/transpiled/state.js.map +1 -0
  137. package/transpiled/trail.d.ts +78 -0
  138. package/transpiled/trail.js +112 -0
  139. package/transpiled/trail.js.map +1 -0
  140. package/transpiled/transition.d.ts +14 -0
  141. package/transpiled/transition.js +51 -0
  142. package/transpiled/transition.js.map +1 -0
  143. package/transpiled/transmission.d.ts +8 -0
  144. package/transpiled/transmission.js +30 -0
  145. package/transpiled/transmission.js.map +1 -0
  146. package/transpiled/types/atomicity.d.ts +58 -0
  147. package/transpiled/types/atomicity.js +2 -0
  148. package/transpiled/types/atomicity.js.map +1 -0
  149. package/transpiled/types/bindings.d.ts +77 -0
  150. package/transpiled/types/bindings.js +2 -0
  151. package/transpiled/types/bindings.js.map +1 -0
  152. package/transpiled/types/bridges.d.ts +41 -0
  153. package/transpiled/types/bridges.js +2 -0
  154. package/transpiled/types/bridges.js.map +1 -0
  155. package/transpiled/types/extensions.d.ts +116 -0
  156. package/transpiled/types/extensions.js +2 -0
  157. package/transpiled/types/extensions.js.map +1 -0
  158. package/transpiled/types/inbox.d.ts +22 -0
  159. package/transpiled/types/inbox.js +2 -0
  160. package/transpiled/types/inbox.js.map +1 -0
  161. package/transpiled/types/index.d.ts +14 -0
  162. package/transpiled/types/index.js +9 -0
  163. package/transpiled/types/index.js.map +1 -0
  164. package/transpiled/types/message.d.ts +16 -0
  165. package/transpiled/types/message.js +2 -0
  166. package/transpiled/types/message.js.map +1 -0
  167. package/transpiled/types/operations.d.ts +15 -0
  168. package/transpiled/types/operations.js +2 -0
  169. package/transpiled/types/operations.js.map +1 -0
  170. package/transpiled/types/outbox.d.ts +54 -0
  171. package/transpiled/types/outbox.js +2 -0
  172. package/transpiled/types/outbox.js.map +1 -0
  173. package/transpiled/types/receiver.d.ts +9 -0
  174. package/transpiled/types/receiver.js +2 -0
  175. package/transpiled/types/receiver.js.map +1 -0
  176. package/transpiled/types/request.d.ts +119 -0
  177. package/transpiled/types/request.js +2 -0
  178. package/transpiled/types/request.js.map +1 -0
  179. package/transpiled/types/state.d.ts +9 -0
  180. package/transpiled/types/state.js +2 -0
  181. package/transpiled/types/state.js.map +1 -0
  182. package/transpiled/types/storages.d.ts +146 -0
  183. package/transpiled/types/storages.js +2 -0
  184. package/transpiled/types/storages.js.map +1 -0
  185. package/transpiled/unmanaged.d.ts +5 -0
  186. package/transpiled/unmanaged.js +7 -0
  187. package/transpiled/unmanaged.js.map +1 -0
  188. package/src/assignment.js +0 -31
  189. package/src/call.js +0 -42
  190. package/src/cascade.js +0 -35
  191. package/src/component.js +0 -41
  192. package/src/composition.js +0 -25
  193. package/src/connector.js +0 -183
  194. package/src/context.js +0 -55
  195. package/src/contract/conditions.js +0 -21
  196. package/src/contract/index.js +0 -7
  197. package/src/contract/reply.js +0 -25
  198. package/src/contract/request.js +0 -67
  199. package/src/contract/schemas/error.yaml +0 -9
  200. package/src/contract/schemas/index.js +0 -7
  201. package/src/contract/schemas/query.yaml +0 -30
  202. package/src/discovery.js +0 -44
  203. package/src/emission.js +0 -23
  204. package/src/entities/changeset.js +0 -38
  205. package/src/entities/entity.js +0 -74
  206. package/src/entities/factory.js +0 -33
  207. package/src/entities/index.js +0 -5
  208. package/src/entities/set.js +0 -15
  209. package/src/event.js +0 -40
  210. package/src/exceptions.js +0 -100
  211. package/src/exposition.js +0 -27
  212. package/src/index.js +0 -47
  213. package/src/locator.js +0 -54
  214. package/src/observation.js +0 -20
  215. package/src/operation.js +0 -69
  216. package/src/query/criteria.js +0 -41
  217. package/src/query/options.js +0 -40
  218. package/src/query.js +0 -41
  219. package/src/receiver.js +0 -61
  220. package/src/reflection.js +0 -28
  221. package/src/remote.js +0 -17
  222. package/src/state.js +0 -99
  223. package/src/transition.js +0 -67
  224. package/src/transmission.js +0 -33
  225. package/test/call.fixtures.js +0 -27
  226. package/test/call.test.js +0 -46
  227. package/test/component.fixtures.js +0 -21
  228. package/test/component.test.js +0 -40
  229. package/test/connector.fixtures.js +0 -40
  230. package/test/connector.test.js +0 -176
  231. package/test/context.fixtures.js +0 -18
  232. package/test/context.test.js +0 -29
  233. package/test/contract/conditions.test.js +0 -26
  234. package/test/contract/contract.fixtures.js +0 -33
  235. package/test/contract/request.test.js +0 -112
  236. package/test/emission.fixtures.js +0 -17
  237. package/test/emission.test.js +0 -35
  238. package/test/entities/entity.fixtures.js +0 -29
  239. package/test/entities/entity.test.js +0 -65
  240. package/test/entities/factory.fixtures.js +0 -18
  241. package/test/entities/factory.test.js +0 -48
  242. package/test/entities/set.fixtures.js +0 -11
  243. package/test/entities/set.test.js +0 -12
  244. package/test/event.fixtures.js +0 -29
  245. package/test/event.test.js +0 -118
  246. package/test/locator.test.js +0 -113
  247. package/test/query.fixtures.js +0 -100
  248. package/test/query.test.js +0 -86
  249. package/test/receiver.fixtures.js +0 -23
  250. package/test/receiver.test.js +0 -95
  251. package/test/reflection.test.js +0 -30
  252. package/test/state.fixtures.js +0 -47
  253. package/test/state.test.js +0 -46
  254. package/test/transmission.fixtures.js +0 -15
  255. package/test/transmission.test.js +0 -46
  256. package/types/bindings.d.ts +0 -40
  257. package/types/bridges.ts +0 -30
  258. package/types/component.d.ts +0 -9
  259. package/types/connector.d.ts +0 -18
  260. package/types/context.d.ts +0 -21
  261. package/types/entity.d.ts +0 -46
  262. package/types/event.d.ts +0 -11
  263. package/types/exception.d.ts +0 -4
  264. package/types/extensions.d.ts +0 -29
  265. package/types/index.ts +0 -14
  266. package/types/locator.d.ts +0 -13
  267. package/types/message.d.ts +0 -3
  268. package/types/operations.d.ts +0 -2
  269. package/types/query.d.ts +0 -15
  270. package/types/receiver.d.ts +0 -12
  271. package/types/reflection.d.ts +0 -14
  272. package/types/request.d.ts +0 -23
  273. package/types/state.d.ts +0 -35
  274. package/types/storages.d.ts +0 -71
@@ -0,0 +1,448 @@
1
+ import { console } from 'openspan';
2
+ import { Connector } from './connector.js';
3
+ import { newid } from './entities/newid.js';
4
+ import * as trail from './trail.js';
5
+ import * as measure from './outbox.measurements.js';
6
+ import { environment } from '@toa.io/generic';
7
+ /**
8
+ * Owns the intent to publish. A row is built before the write so that the storage can commit
9
+ * it in the same transaction as the entity; publication then happens off the operation's path,
10
+ * and anything that fails to publish is recovered from the row.
11
+ *
12
+ * The mechanism is a safety net: in a healthy system the row is written, published within
13
+ * milliseconds by the same process, and marked published on that process's next tick.
14
+ *
15
+ * A storage that cannot commit a row atomically has no outbox, and this degrades to the
16
+ * inline emission it replaces.
17
+ */
18
+ export class Outbox extends Connector {
19
+ #destinations;
20
+ #storage;
21
+ #atom;
22
+ #gap;
23
+ #interval;
24
+ #batch;
25
+ #defer;
26
+ /*
27
+ * Everything a destination is answerable for is held per destination, which is what keeps
28
+ * one from delaying another: a dead one fills its own in-flight cap and the cycle stops
29
+ * handing it rows, while the rest go on being published and settled.
30
+ */
31
+ /** ids this process has published, by destination, held until a cycle marks them */
32
+ #published = new Map();
33
+ /** in-flight publications, by destination, awaited (with a bound) on close */
34
+ #inflight = new Map();
35
+ /** rows this replica is publishing right now, so a cycle does not pick them up again */
36
+ #publishing = new Map();
37
+ #timer;
38
+ #off;
39
+ #pumping = false;
40
+ /** cycles in a row this replica has been assigned no lane */
41
+ #unassigned = 0;
42
+ #closing = false;
43
+ // eslint-disable-next-line max-params
44
+ constructor(destinations, storage, atom, options = {}) {
45
+ super();
46
+ this.#destinations = destinations;
47
+ this.#storage = storage;
48
+ this.#atom = atom;
49
+ for (const destination of destinations) {
50
+ this.#published.set(destination.name, new Set());
51
+ this.#inflight.set(destination.name, new Set());
52
+ this.#publishing.set(destination.name, new Set());
53
+ }
54
+ this.#interval = number('TOA_OUTBOX_INTERVAL', options.interval, INTERVAL);
55
+ this.#batch = number('TOA_OUTBOX_BATCH', options.batch, BATCH);
56
+ this.#gap = options.gap ?? this.#interval * K;
57
+ this.#defer = environment.get('TOA_OUTBOX_DEFER') === '1';
58
+ // one at a time: an array would link the group it makes rather than its members
59
+ for (const destination of destinations)
60
+ this.depends(destination);
61
+ this.depends(atom);
62
+ if (storage !== undefined)
63
+ this.depends(storage);
64
+ }
65
+ /** whether the storage can commit a row atomically with the entity */
66
+ get durable() {
67
+ return this.#storage?.outbox !== undefined;
68
+ }
69
+ /**
70
+ * An assignment's images are the write's own, so it hands over an event with neither, and
71
+ * the storage fills them in.
72
+ */
73
+ row(event) {
74
+ const row = {
75
+ id: newid(),
76
+ lane: this.#lane(),
77
+ published: false,
78
+ pending: Date.now() + this.#gap,
79
+ outstanding: this.#destinations.map((destination) => destination.name),
80
+ event: event
81
+ };
82
+ /*
83
+ * This runs on the operation's own path, which is the only moment the chain is in scope:
84
+ * the pump that publishes the row may be another replica, an hour later. A row that had
85
+ * no chain stores no field.
86
+ */
87
+ const hops = trail.current()?.hops;
88
+ if (hops !== undefined)
89
+ row.trail = hops;
90
+ return row;
91
+ }
92
+ /**
93
+ * Hands a committed row over. Awaited by the caller only on the legacy path — with an
94
+ * outbox this returns at once and the broker leaves the operation's path.
95
+ */
96
+ publish(row) {
97
+ // without a durable outbox this is the inline path, and the caller awaits every destination
98
+ if (!this.durable)
99
+ return this.#inline(row);
100
+ /*
101
+ * A publication started while the pump is closing would outlive the emitters it needs,
102
+ * and `comq` waits on a connection that is going rather than failing. The row is already
103
+ * durable, so leaving it is exactly what it is for.
104
+ */
105
+ if (this.#closing || this.#defer)
106
+ return;
107
+ this.#publish(row);
108
+ }
109
+ /**
110
+ * The inline path, where there is no row to come back to: every destination is attempted and
111
+ * the operation waits for all of them, so a failure reaches whoever wrote.
112
+ */
113
+ async #inline(row) {
114
+ const sending = this.#destinations
115
+ .filter((destination) => row.outstanding.includes(destination.name))
116
+ .map(async (destination) => destination.emit(row));
117
+ await Promise.all(sending);
118
+ }
119
+ async open() {
120
+ if (!this.durable)
121
+ return;
122
+ if (this.#defer)
123
+ console.warn('Outbox immediate publication is deferred; events are published by the pump only');
124
+ this.#timer = setInterval(() => {
125
+ this.#tick();
126
+ }, this.#interval);
127
+ this.#timer.unref();
128
+ /*
129
+ * A lane changing hands is exactly when rows stranded in it become this replica's to
130
+ * publish, and the cycle would not notice for up to an interval. Being told costs a cycle
131
+ * that finds nothing in the usual case, where the claim arrives once and never changes.
132
+ */
133
+ this.#off = this.#atom.onassigned(() => {
134
+ this.#tick();
135
+ });
136
+ }
137
+ async close() {
138
+ this.#closing = true;
139
+ this.#off?.();
140
+ if (this.#timer !== undefined)
141
+ clearInterval(this.#timer);
142
+ await this.#drain();
143
+ await this.#mark();
144
+ }
145
+ /**
146
+ * Publishes one row and swallows the failure: the row stays unpublished and comes back on a
147
+ * later cycle, which is the whole point of having written it.
148
+ *
149
+ * There is no timeout here on purpose. A publication is a confirmed write to a durable
150
+ * exchange, and `comq` waits for the broker to come back rather than failing — abandoning
151
+ * it would not stop it, it would only mean the row is published twice once it lands. What
152
+ * bounds this instead is the in-flight cap and the drain on close.
153
+ *
154
+ */
155
+ /** @returns what it started, so a cycle can wait for it before marking */
156
+ #publish(row) {
157
+ const started = [];
158
+ for (const destination of this.#destinations)
159
+ if (this.#due(row, destination.name))
160
+ started.push(this.#send(row, destination));
161
+ return started;
162
+ }
163
+ /**
164
+ * Whether this row is still this process's to send to that destination. A destination the
165
+ * row names and this process does not have is not its to send, and is left where it is.
166
+ */
167
+ #due(row, destination) {
168
+ if (!row.outstanding.includes(destination))
169
+ return false;
170
+ const published = this.#published.get(destination);
171
+ if (published === undefined)
172
+ return false;
173
+ return !published.has(row.id) && !this.#publishing.get(destination).has(row.id);
174
+ }
175
+ /**
176
+ * Publishes one row to one destination and swallows the failure: the row stays outstanding
177
+ * for it and comes back on a later cycle, which is the whole point of having written it.
178
+ * Nothing here reaches the other destinations of the same row.
179
+ *
180
+ * There is no timeout on purpose. A publication is a confirmed write to a durable exchange,
181
+ * and `comq` waits for the broker to come back rather than failing — abandoning it would not
182
+ * stop it, it would only mean the row is published twice once it lands. What bounds this
183
+ * instead is the in-flight cap, which is per destination so that a dead one cannot stop the
184
+ * cycle from feeding the others, and the drain on close.
185
+ */
186
+ async #send(row, destination) {
187
+ const name = destination.name;
188
+ const published = this.#published.get(name);
189
+ const publishing = this.#publishing.get(name);
190
+ const inflight = this.#inflight.get(name);
191
+ if (inflight.size >= INFLIGHT || published.size >= PUBLISHED)
192
+ return;
193
+ publishing.add(row.id);
194
+ const sending = destination.emit(row);
195
+ inflight.add(sending);
196
+ try {
197
+ await sending;
198
+ published.add(row.id);
199
+ }
200
+ catch (error) {
201
+ // the message, not the error: an `Error` has no enumerable own properties, so what
202
+ // reached the log was `{}` and a publication that keeps failing said nothing about why
203
+ measure.failed(name);
204
+ console.warn('Outbox publication failed', {
205
+ row: row.id,
206
+ destination: name,
207
+ message: error?.message
208
+ });
209
+ }
210
+ finally {
211
+ inflight.delete(sending);
212
+ publishing.delete(row.id);
213
+ }
214
+ }
215
+ /**
216
+ * `comq` retries a publish for as long as the broker is down rather than rejecting, so an
217
+ * unbounded drain outlives any grace period.
218
+ *
219
+ */
220
+ async #drain() {
221
+ const inflight = [...this.#inflight.values()].flatMap((set) => [...set]);
222
+ if (inflight.length === 0)
223
+ return;
224
+ await Promise.race([Promise.allSettled(inflight), delay(DRAIN)]);
225
+ }
226
+ /**
227
+ * Reads what is due, publishes it, and marks everything this process has sent — what it just
228
+ * published and what the immediate path published since the last cycle. One cycle at a time.
229
+ *
230
+ */
231
+ #tick() {
232
+ if (this.#pumping)
233
+ return;
234
+ this.#pumping = true;
235
+ void this.#pump().finally(() => (this.#pumping = false));
236
+ }
237
+ async #pump() {
238
+ this.#assignment();
239
+ let page;
240
+ let after;
241
+ // what this cycle saw, so that a destination it read nothing for is set to zero rather
242
+ // than left holding whatever the cycle before it found
243
+ const counts = new Map();
244
+ const oldest = new Map();
245
+ do {
246
+ page = await this.#read(after);
247
+ if (page.length === 0)
248
+ break;
249
+ // so a page is never read twice
250
+ after = page[page.length - 1]?.id;
251
+ /*
252
+ * A row is unpublished in the database until a cycle marks it, so a page includes what
253
+ * this replica is sending right now and what a failed marking left behind. Only this
254
+ * process knows either.
255
+ */
256
+ const rows = page.filter((row) => this.#destinations.some((destination) => this.#due(row, destination.name)));
257
+ this.#outstanding(rows, counts, oldest);
258
+ if (rows.length > 0) {
259
+ console.debug('Outbox recovering unpublished rows', { count: rows.length });
260
+ // every row is given its chance, at every destination it is still outstanding for;
261
+ // what a broker refused stays outstanding there and comes back on a later cycle
262
+ await this.#awaited(rows.flatMap((row) => this.#publish(row)));
263
+ }
264
+ // a full page is a page that may have been cut short
265
+ } while (page.length === this.#batch);
266
+ measure.cycled(this.#destinations.map((destination) => destination.name), counts, oldest);
267
+ await this.#mark();
268
+ }
269
+ /** What a page leaves outstanding, by destination: how many, and the oldest of them. */
270
+ #outstanding(rows, counts, oldest) {
271
+ for (const row of rows)
272
+ for (const { name } of this.#destinations) {
273
+ if (!this.#due(row, name))
274
+ continue;
275
+ counts.set(name, (counts.get(name) ?? 0) + 1);
276
+ oldest.set(name, Math.min(oldest.get(name) ?? row.pending, row.pending));
277
+ }
278
+ }
279
+ /**
280
+ * Says when this replica has owned nothing for long enough that nothing is going to assign
281
+ * it anything.
282
+ *
283
+ * Owning no lane is ordinary for a moment — a replica that has just started has not been
284
+ * told yet — and is why nothing here refuses at boot: one cycle of it cannot be told from an
285
+ * atomicity that is absent. Ten in a row can, and what it means is that this outbox recovers
286
+ * nothing: a publication that failed is never retried, and that change is lost with the
287
+ * process that failed to make it.
288
+ *
289
+ * Said every ten cycles rather than once, so that it is still there to be found in a log
290
+ * read long after the deployment that broke it, and once when it is over.
291
+ */
292
+ #assignment() {
293
+ const lanes = this.#atom.slots(LANES);
294
+ if (lanes !== null && lanes.length > 0) {
295
+ if (this.#unassigned >= UNASSIGNED)
296
+ console.info('Outbox lanes assigned, recovery resumed');
297
+ this.#unassigned = 0;
298
+ measure.starving(0);
299
+ return;
300
+ }
301
+ this.#unassigned++;
302
+ measure.starving(this.#unassigned);
303
+ if (this.#unassigned % UNASSIGNED === 0)
304
+ console.warn('Outbox owns no lane and recovers nothing', {
305
+ cycles: this.#unassigned,
306
+ interval: this.#interval
307
+ });
308
+ }
309
+ /**
310
+ * One page of what is due. In a healthy system the first one is empty, every cycle — a row is
311
+ * due only if the process that wrote it failed to publish or died before marking it.
312
+ *
313
+ * Reading is suspended, not stopped, while this replica does not know which lanes are its
314
+ * own: the cycle keeps running and keeps marking, and reading resumes as soon as an
315
+ * assignment arrives. Reading without an assignment would be a different guarantee, where
316
+ * every replica publishes every stranded row.
317
+ *
318
+ * @param after the last id of the page before, so a page is never read twice
319
+ */
320
+ async #read(after) {
321
+ const lanes = this.#atom.slots(LANES);
322
+ if (lanes === null || lanes.length === 0)
323
+ return [];
324
+ return this.#storage.outbox.pending(lanes, Date.now(), this.#batch, after).catch((error) => {
325
+ console.warn('Outbox read failed', { error });
326
+ return [];
327
+ });
328
+ }
329
+ /**
330
+ * Waits for what this cycle started, so that it marks what it sent rather than leaving it to
331
+ * the next one — but not past its own interval. A publication is never abandoned: it stays in
332
+ * flight, `#publishing` keeps a later cycle from sending it again, and the drain on close
333
+ * still waits for it. What is bounded is only how long a cycle waits before marking the rest,
334
+ * so that one destination with nothing to say cannot hold up the settling of another.
335
+ */
336
+ async #awaited(started) {
337
+ if (started.length === 0)
338
+ return;
339
+ await Promise.race([Promise.allSettled(started), delay(this.#interval)]);
340
+ }
341
+ /**
342
+ * One batched write for many rows, which is why the ids are held in memory rather than
343
+ * marked one by one. Ids that fail to be marked are kept and retried; a row that is never
344
+ * marked is simply published again, which is within the contract.
345
+ *
346
+ * Rows are grouped by the destinations that completed for them, so the ordinary case — every
347
+ * destination landing in the same window — is one group and one write, as it was when there
348
+ * was only ever one. A second write happens where the destinations diverged, which is the
349
+ * failure this exists for.
350
+ */
351
+ async #mark() {
352
+ for (const [destinations, ids] of this.#groups())
353
+ await this.#settle(destinations, ids);
354
+ }
355
+ /** @private */
356
+ async #settle(destinations, ids) {
357
+ try {
358
+ await this.#storage.outbox.settle(ids, destinations);
359
+ }
360
+ catch (error) {
361
+ console.warn('Outbox marking failed', { count: ids.length, error });
362
+ return;
363
+ }
364
+ for (const destination of destinations) {
365
+ const published = this.#published.get(destination);
366
+ for (const id of ids)
367
+ published.delete(id);
368
+ }
369
+ }
370
+ /**
371
+ * What has been published since the last cycle, as rows sharing the destinations that
372
+ * completed for them.
373
+ */
374
+ #groups() {
375
+ /** the destinations that completed for an id, in the order the destinations are held */
376
+ const completed = new Map();
377
+ for (const [destination, ids] of this.#published)
378
+ for (const id of ids) {
379
+ const names = completed.get(id);
380
+ if (names === undefined)
381
+ completed.set(id, [destination]);
382
+ else
383
+ names.push(destination);
384
+ }
385
+ const groups = new Map();
386
+ for (const [id, destinations] of completed) {
387
+ const key = destinations.join(SEPARATOR);
388
+ const group = groups.get(key);
389
+ if (group === undefined)
390
+ groups.set(key, [destinations, [id]]);
391
+ else
392
+ group[1].push(id);
393
+ }
394
+ return [...groups.values()];
395
+ }
396
+ /**
397
+ * A lane this replica currently owns, so that in steady state it settles its own rows
398
+ * before it ever reads them. Any lane at all when it owns none: the row still has to be
399
+ * written, and whoever ends up owning that lane will pump it.
400
+ *
401
+ */
402
+ #lane() {
403
+ const owned = this.#atom.slots(LANES);
404
+ return owned === null || owned.length === 0
405
+ ? Math.floor(Math.random() * LANES)
406
+ : owned[Math.floor(Math.random() * owned.length)];
407
+ }
408
+ }
409
+ function number(variable, declared, fallback) {
410
+ if (declared !== undefined)
411
+ return declared;
412
+ const value = Number(environment.get(variable));
413
+ return Number.isNaN(value) || value <= 0 ? fallback : value;
414
+ }
415
+ async function delay(ms) {
416
+ return new Promise((resolve) => {
417
+ setTimeout(resolve, ms).unref();
418
+ });
419
+ }
420
+ /**
421
+ * Constant, never configuration: rows carry their lane, so lowering this would leave rows in
422
+ * lanes nobody reads any more. It is also the ceiling on replicas of one component, and a
423
+ * power of two so that the common replica counts divide evenly.
424
+ */
425
+ export const LANES = 128;
426
+ /** one cycle reads, publishes and marks; in steady state it finds nothing to read */
427
+ /**
428
+ * Cycles owning nothing before that is said out loud, and between one saying and the next.
429
+ * Long enough that a replica which has just started, or one whose assignment is being redrawn,
430
+ * says nothing at all.
431
+ */
432
+ const UNASSIGNED = 10;
433
+ const INTERVAL = 5000;
434
+ /**
435
+ * `gap = interval * K`. Not a steady-state necessity — a replica writes into a lane it owns
436
+ * and marks what it published — but a guard for when a lane changes hands between the write
437
+ * and the settle. Two cycles of separation, plus one of margin.
438
+ */
439
+ const K = 3;
440
+ /** how many rows one read brings back; the pump reads on while a page comes back full */
441
+ const BATCH = 200;
442
+ const DRAIN = 10_000;
443
+ /** publications in flight at once, per destination */
444
+ const INFLIGHT = 1000;
445
+ const PUBLISHED = 10_000;
446
+ /** what a group of destinations is keyed by; no name can hold it */
447
+ const SEPARATOR = '\u0000';
448
+ //# sourceMappingURL=outbox.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"outbox.js","sourceRoot":"","sources":["../source/outbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC3C,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,OAAO,MAAM,0BAA0B,CAAA;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAY7C;;;;;;;;;;GAUG;AACH,MAAM,OAAO,MAAO,SAAQ,SAAS;IAC1B,aAAa,CAAe;IAC5B,QAAQ,CAAqB;IAC7B,KAAK,CAAM;IAEX,IAAI,CAAQ;IACZ,SAAS,CAAQ;IACjB,MAAM,CAAQ;IACd,MAAM,CAAS;IAExB;;;;OAIG;IAEH,oFAAoF;IAC3E,UAAU,GAAG,IAAI,GAAG,EAAuB,CAAA;IAEpD,8EAA8E;IACrE,SAAS,GAAG,IAAI,GAAG,EAA8B,CAAA;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E,CAAA;IACjC,CAAC,CAAC,CAAA;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAA;AAExB,qFAAqF;AACrF;;;;GAIG;AACH,MAAM,UAAU,GAAG,EAAE,CAAA;AAErB,MAAM,QAAQ,GAAG,IAAI,CAAA;AAErB;;;;GAIG;AACH,MAAM,CAAC,GAAG,CAAC,CAAA;AAEX,yFAAyF;AACzF,MAAM,KAAK,GAAG,GAAG,CAAA;AAEjB,MAAM,KAAK,GAAG,MAAM,CAAA;AAEpB,sDAAsD;AACtD,MAAM,QAAQ,GAAG,IAAI,CAAA;AACrB,MAAM,SAAS,GAAG,MAAM,CAAA;AAExB,oEAAoE;AACpE,MAAM,SAAS,GAAG,QAAQ,CAAA"}
@@ -0,0 +1,4 @@
1
+ export declare function failed(destination: string): void;
2
+ export declare function starving(cycles: number): void;
3
+ /** What one cycle saw, set together so that a destination it read nothing for reads as zero. */
4
+ export declare function cycled(destinations: string[], counts: Map<string, number>, oldest: Map<string, number>): void;
@@ -0,0 +1,38 @@
1
+ import { registry } from 'openspan';
2
+ /**
3
+ * What the outbox measures, and the zone with the most to gain from being measured: the pump
4
+ * swallows a publication failure into a log line, so events stop leaving and nothing says so.
5
+ */
6
+ const meters = registry();
7
+ const DESTINATION = { destination: null };
8
+ /** Rows this replica has read as still outstanding, at the end of a cycle. */
9
+ const pending = meters.gauge('toa.outbox.pending', DESTINATION);
10
+ /**
11
+ * How long the oldest outstanding row has waited. The number to alert on, because it is one
12
+ * threshold a person can choose — *events are more than N seconds behind* — where a count of rows
13
+ * has none: a handful four hours old is a stuck destination, a thousand a second old is a busy one.
14
+ */
15
+ const age = meters.gauge('toa.outbox.age', DESTINATION);
16
+ const failures = meters.counter('toa.outbox.failures', DESTINATION);
17
+ /**
18
+ * Cycles in a row this replica was given no lane. Past ten it means this outbox recovers nothing:
19
+ * a publication that failed is never retried, and that change is lost with the process that failed
20
+ * to make it.
21
+ */
22
+ const unassigned = meters.gauge('toa.outbox.unassigned');
23
+ export function failed(destination) {
24
+ failures.add(1, { destination });
25
+ }
26
+ export function starving(cycles) {
27
+ unassigned.set(cycles);
28
+ }
29
+ /** What one cycle saw, set together so that a destination it read nothing for reads as zero. */
30
+ export function cycled(destinations, counts, oldest) {
31
+ const now = Date.now();
32
+ for (const destination of destinations) {
33
+ pending.set(counts.get(destination) ?? 0, { destination });
34
+ const since = oldest.get(destination);
35
+ age.set(since === undefined ? 0 : Math.max(0, (now - since) / 1000), { destination });
36
+ }
37
+ }
38
+ //# sourceMappingURL=outbox.measurements.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"outbox.measurements.js","sourceRoot":"","sources":["../source/outbox.measurements.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAEnC;;;GAGG;AACH,MAAM,MAAM,GAAG,QAAQ,EAAE,CAAA;AAEzB,MAAM,WAAW,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,CAAA;AAEzC,8EAA8E;AAC9E,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,oBAAoB,EAAE,WAAW,CAAC,CAAA;AAE/D;;;;GAIG;AACH,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,gBAAgB,EAAE,WAAW,CAAC,CAAA;AAEvD,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,qBAAqB,EAAE,WAAW,CAAC,CAAA;AAEnE;;;;GAIG;AACH,MAAM,UAAU,GAAG,MAAM,CAAC,KAAK,CAAC,uBAAuB,CAAC,CAAA;AAExD,MAAM,UAAU,MAAM,CAAC,WAAmB;IACxC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,WAAW,EAAE,CAAC,CAAA;AAClC,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,MAAc;IACrC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACxB,CAAC;AAED,gGAAgG;AAChG,MAAM,UAAU,MAAM,CACpB,YAAsB,EACtB,MAA2B,EAC3B,MAA2B;IAE3B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;IAEtB,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE,CAAC;QACvC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,EAAE,WAAW,EAAE,CAAC,CAAA;QAE1D,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;QAErC,GAAG,CAAC,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,IAAI,CAAC,EAAE,EAAE,WAAW,EAAE,CAAC,CAAA;IACvF,CAAC;AACH,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { Node } from '../types/storages.ts';
2
+ /** What a component declares about the properties a criteria may select on. */
3
+ export type Properties = Record<string, {
4
+ type: string;
5
+ }>;
6
+ export declare function criteria(expression: string, properties?: Properties): Node;
@@ -0,0 +1,74 @@
1
+ import { parse } from '@rsql/parser';
2
+ import { QuerySyntaxException } from '../exceptions.js';
3
+ export function criteria(expression, properties) {
4
+ let ast;
5
+ try {
6
+ ast = parse(expression);
7
+ }
8
+ catch (e) {
9
+ throw new QuerySyntaxException(e.message);
10
+ }
11
+ if (properties !== undefined)
12
+ read(ast, properties);
13
+ return ast;
14
+ }
15
+ function read(node, properties) {
16
+ if (node.type === 'COMPARISON' &&
17
+ node.left?.type === 'SELECTOR' &&
18
+ node.right?.type === 'VALUE') {
19
+ const selector = node.left.selector;
20
+ const property = properties[selector];
21
+ if (property === undefined) {
22
+ throw new QuerySyntaxException(`Criteria selector '${selector}' is not defined`);
23
+ }
24
+ const cast = CAST[property.type];
25
+ // `=in=` and `=out=` carry a list, and casting that as one value gives whatever
26
+ // a comma-separated string reads as
27
+ if (cast !== undefined)
28
+ node.right.value = Array.isArray(node.right.value)
29
+ ? node.right.value.map((value) => cast(value, selector))
30
+ : cast(node.right.value, selector);
31
+ }
32
+ else {
33
+ if (node.left !== undefined)
34
+ read(node.left, properties);
35
+ if (node.right !== undefined)
36
+ read(node.right, properties);
37
+ }
38
+ }
39
+ /**
40
+ * A criteria arrives as text, so a value is read as what the property it selects on holds. What
41
+ * cannot be read as that is refused rather than passed on: `parseInt` answers `NaN` for a word
42
+ * and `12` for `12kg`, and a storage asked to match either finds nothing and says nothing —
43
+ * a mistyped filter reads as an empty result.
44
+ *
45
+ * A string is left as it came, because every text is one: what a criteria may compare a string
46
+ * to is not the type's to say.
47
+ */
48
+ const CAST = {
49
+ number: (value, selector) => finite(value, selector, 'a number'),
50
+ integer: (value, selector) => {
51
+ const number = finite(value, selector, 'an integer');
52
+ if (!Number.isInteger(number))
53
+ refuse(selector, 'an integer', value);
54
+ return number;
55
+ },
56
+ boolean: (value, selector) => {
57
+ if (value === 'true')
58
+ return true;
59
+ if (value === 'false')
60
+ return false;
61
+ return refuse(selector, 'a boolean', value);
62
+ }
63
+ };
64
+ function finite(value, selector, expected) {
65
+ // `Number` reads a blank string as zero, and a criteria that says nothing says nothing
66
+ const number = value.trim() === '' ? Number.NaN : Number(value);
67
+ if (!Number.isFinite(number))
68
+ refuse(selector, expected, value);
69
+ return number;
70
+ }
71
+ function refuse(selector, expected, value) {
72
+ throw new QuerySyntaxException(`Criteria selector '${selector}' takes ${expected}, and '${value}' is not one`);
73
+ }
74
+ //# sourceMappingURL=criteria.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"criteria.js","sourceRoot":"","sources":["../../source/query/criteria.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AACpC,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAMvD,MAAM,UAAU,QAAQ,CAAC,UAAkB,EAAE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AC,QAAQ,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAA;IAE/D,OAAO,MAAM,CAAA;AACf,CAAC;AAED,SAAS,MAAM,CAAC,QAAgB,EAAE,QAAgB,EAAE,KAAa;IAC/D,MAAM,IAAI,oBAAoB,CAC5B,sBAAsB,QAAQ,WAAW,QAAQ,UAAU,KAAK,cAAc,CAC/E,CAAA;AACH,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Options } from '../types/storages.ts';
2
+ import type { Properties } from './criteria.ts';
3
+ export declare function options(given: Record<string, any>, properties: Properties): Options;
@@ -0,0 +1,34 @@
1
+ import { QuerySyntaxException } from '../exceptions.js';
2
+ export function options(given, properties) {
3
+ if (given.sort !== undefined)
4
+ given.sort = sort(given.sort, properties);
5
+ if (given.projection !== undefined)
6
+ given.projection = projection(given.projection, properties);
7
+ return given;
8
+ }
9
+ function sort(sort, properties) {
10
+ const result = [];
11
+ for (const sorting of sort) {
12
+ const [property, direction] = sorting.split(':');
13
+ if (properties[property] === undefined)
14
+ throw new QuerySyntaxException(`Sort property '${property}' is not defined`);
15
+ result.push([property, direction ?? 'asc']);
16
+ }
17
+ return result;
18
+ }
19
+ /**
20
+ * A copy, because a route declares one projection and sends it with every request it serves,
21
+ * and the system properties are what every read answers besides.
22
+ */
23
+ function projection(declared, properties) {
24
+ for (const property of declared)
25
+ if (properties[property] === undefined)
26
+ throw new QuerySyntaxException(`Projection property '${property}' is not defined`);
27
+ const projection = declared.slice();
28
+ for (const property of SYSTEM)
29
+ if (!projection.includes(property))
30
+ projection.push(property);
31
+ return projection;
32
+ }
33
+ const SYSTEM = ['VERSION', 'CREATED', 'UPDATED', 'DELETED', 'REGION'];
34
+ //# sourceMappingURL=options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.js","sourceRoot":"","sources":["../../source/query/options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAIvD,MAAM,UAAU,OAAO,CAAC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ACnB,CAAC;AAED,MAAM,MAAM,GAAG,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAA"}
@@ -0,0 +1,9 @@
1
+ import type { Properties } from './query/criteria.ts';
2
+ import type { Query as Parsed } from './types/storages.ts';
3
+ import type { Query as Requested } from './types/request.ts';
4
+ /** Translates the query a request carries into the one a storage is given. */
5
+ export declare class Query {
6
+ #private;
7
+ constructor(properties: Properties);
8
+ parse(query: Requested): Parsed;
9
+ }