@reventlessdev/reventless-postgres 3.0.0-alpha.20 → 3.0.0-alpha.201
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 +1451 -0
- package/README.md +1 -1
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/ReventlessPostgres.cmt +0 -0
- package/lib/bs/compiler-info.json +3 -3
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres.ast +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres.res +1 -1
- package/lib/bs/src/DcbEventLogStorage_Postgres.res.mjs +6 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res +116 -44
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +64 -7
- package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogChangeFeed.ast +0 -0
- package/lib/bs/src/EventLogChangeFeed.res +15 -6
- package/lib/bs/src/EventLogChangeFeed.res.mjs +3 -3
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogStorage_Postgres.ast +0 -0
- package/lib/bs/src/EventLogStorage_Postgres.res +1 -1
- package/lib/bs/src/EventLogStorage_Postgres.res.mjs +3 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops.res +74 -14
- package/lib/bs/src/EventLogStorage_Postgres_Ops.res.mjs +34 -4
- package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgChangeFeed.ast +0 -0
- package/lib/bs/src/PgChangeFeed.res +21 -9
- package/lib/bs/src/PgChangeFeed.res.mjs +6 -6
- package/lib/bs/src/PgDriver-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgDriver-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgDriver-ReventlessPostgres.cmti +0 -0
- package/lib/bs/src/PgDriver.ast +0 -0
- package/lib/bs/src/PgDriver.iast +0 -0
- package/lib/bs/src/PgDriver.res +1 -2
- package/lib/bs/src/PgSchema-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgSchema-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgSchema.ast +0 -0
- package/lib/bs/src/PgSchema.res +8 -0
- package/lib/bs/src/PgSchema.res.mjs +4 -1
- package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres.ast +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres.res +7 -2
- package/lib/bs/src/QueryDbStorage_Postgres.res.mjs +1 -1
- package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops.ast +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops.res +14 -14
- package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryEnginePostgres.ast +0 -0
- package/lib/bs/src/QueryEnginePostgres.res +141 -42
- package/lib/bs/src/QueryEnginePostgres.res.mjs +53 -30
- package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
- package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
- package/lib/bs/tests/PostgresIntegrationTest.ast +0 -0
- package/lib/bs/tests/PostgresIntegrationTest.res +157 -104
- package/lib/bs/tests/PostgresIntegrationTest.res.mjs +1 -1
- package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest.ast +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest.res +29 -6
- package/lib/bs/tests/QueryEnginePostgresListPageTest.res.mjs +4 -4
- package/lib/ocaml/.compiler.log +2 -2
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres.ast +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres.res +1 -1
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.res +116 -44
- package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogChangeFeed.ast +0 -0
- package/lib/ocaml/EventLogChangeFeed.res +15 -6
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogStorage_Postgres.ast +0 -0
- package/lib/ocaml/EventLogStorage_Postgres.res +1 -1
- package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops.res +74 -14
- package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgChangeFeed.ast +0 -0
- package/lib/ocaml/PgChangeFeed.res +21 -9
- package/lib/ocaml/PgDriver-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgDriver-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgDriver-ReventlessPostgres.cmti +0 -0
- package/lib/ocaml/PgDriver.ast +0 -0
- package/lib/ocaml/PgDriver.iast +0 -0
- package/lib/ocaml/PgDriver.res +1 -2
- package/lib/ocaml/PgSchema-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgSchema-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgSchema.ast +0 -0
- package/lib/ocaml/PgSchema.res +8 -0
- package/lib/ocaml/PostgresIntegrationTest.res +157 -104
- package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres.ast +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres.res +7 -2
- package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres_Ops.res +14 -14
- package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryEnginePostgres.ast +0 -0
- package/lib/ocaml/QueryEnginePostgres.res +141 -42
- package/lib/ocaml/QueryEnginePostgresListPageTest.res +29 -6
- package/lib/ocaml/ReventlessPostgres.cmt +0 -0
- package/package.json +13 -13
- package/src/DcbEventLogStorage_Postgres.res +1 -1
- package/src/DcbEventLogStorage_Postgres.res.mjs +6 -0
- package/src/DcbEventLogStorage_Postgres_Ops.res +116 -44
- package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +64 -7
- package/src/EventLogChangeFeed.res +15 -6
- package/src/EventLogChangeFeed.res.mjs +3 -3
- package/src/EventLogStorage_Postgres.res +1 -1
- package/src/EventLogStorage_Postgres.res.mjs +3 -0
- package/src/EventLogStorage_Postgres_Ops.res +74 -14
- package/src/EventLogStorage_Postgres_Ops.res.mjs +34 -4
- package/src/PgChangeFeed.res +21 -9
- package/src/PgChangeFeed.res.mjs +6 -6
- package/src/PgDriver.res +1 -2
- package/src/PgSchema.res +8 -0
- package/src/PgSchema.res.mjs +4 -1
- package/src/QueryDbStorage_Postgres.res +7 -2
- package/src/QueryDbStorage_Postgres.res.mjs +1 -1
- package/src/QueryDbStorage_Postgres_Ops.res +14 -14
- package/src/QueryEnginePostgres.res +141 -42
- package/src/QueryEnginePostgres.res.mjs +53 -30
- package/tests/PostgresIntegrationTest.res +157 -104
- package/tests/QueryEnginePostgresListPageTest.res +29 -6
- package/tests/PostgresIntegrationTest.res.mjs +0 -377
- package/tests/QueryEnginePostgresListPageTest.res.mjs +0 -461
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// would leak into the Lambda import graph and fail resolution on real Lambda. The
|
|
7
7
|
// thin `DcbEventLogStorage_Postgres` wrapper `include`s this and adds the
|
|
8
8
|
// deploy-time `Pulumi.Output`-shaped adapter for local/deploy consumers.
|
|
9
|
-
// See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
|
|
9
|
+
// See docs/plans/done/deployed-lambda-esm-self-containment.md (Rung-3 finding).
|
|
10
10
|
|
|
11
11
|
open ReventlessCore
|
|
12
12
|
open Reventless
|
|
@@ -67,7 +67,7 @@ let rowToEvent = (row: dict<JSON.t>): DcbEventLog_Adapter.rawSequencedEvent => {
|
|
|
67
67
|
| _ => ""
|
|
68
68
|
}
|
|
69
69
|
let meta = switch row->Dict.get("meta") {
|
|
70
|
-
| Some(metaJson) => metaJson->
|
|
70
|
+
| Some(metaJson) => metaJson->Reventless.Util_Sury.fromJson(Reventless.Message.metaSchema)
|
|
71
71
|
| None => JsError.throwWithMessage("missing meta column in dcb_event row")
|
|
72
72
|
}
|
|
73
73
|
{
|
|
@@ -118,10 +118,12 @@ let buildReadWhere = (
|
|
|
118
118
|
let clauseSqls = query->Array.map(clause => {
|
|
119
119
|
let clauseParts = []
|
|
120
120
|
switch clause.eventTypes {
|
|
121
|
-
|
|
121
|
+
// A clause with no event type matches nothing (`DcbTag.clauseMatchesNothing`).
|
|
122
|
+
| Some([]) => clauseParts->Array.push("FALSE")
|
|
123
|
+
| Some(types) =>
|
|
122
124
|
let ph = types->Array.map(t => b->param(JSON.Encode.string(t)))->Array.join(",")
|
|
123
125
|
clauseParts->Array.push(`event_type IN (${ph})`)
|
|
124
|
-
|
|
|
126
|
+
| None => ()
|
|
125
127
|
}
|
|
126
128
|
switch clause.tags {
|
|
127
129
|
| Some(tags) if tags->Array.length > 0 =>
|
|
@@ -143,7 +145,7 @@ let buildReadWhere = (
|
|
|
143
145
|
// Strip the zero-padding a cursor carries for string-sortability, leaving a
|
|
144
146
|
// plain decimal both `::xid8` and `::bigint` parse unambiguously ("0" if empty).
|
|
145
147
|
let stripZeros = (s: string): string => {
|
|
146
|
-
let t = s->String.replaceRegExp(
|
|
148
|
+
let t = s->String.replaceRegExp(/^0+/, "")
|
|
147
149
|
t == "" ? "0" : t
|
|
148
150
|
}
|
|
149
151
|
|
|
@@ -175,6 +177,75 @@ let countAll = async (pool: PgDriver.pool): int =>
|
|
|
175
177
|
| None => 0
|
|
176
178
|
}
|
|
177
179
|
|
|
180
|
+
/** Where a backwards read of one log starts: strictly below a position, or at
|
|
181
|
+
and below it. */
|
|
182
|
+
type bound =
|
|
183
|
+
| Below(float)
|
|
184
|
+
| AtMost(float)
|
|
185
|
+
|
|
186
|
+
/** One log's events newest first by `position`, narrowed to one correlation id
|
|
187
|
+
when given. Rows are `(position, event)`; the event's own `position` field is
|
|
188
|
+
the read cursor, as `read` returns it. */
|
|
189
|
+
let readBackwards = async (
|
|
190
|
+
pool: PgDriver.pool,
|
|
191
|
+
~name: string,
|
|
192
|
+
~bound: option<bound>,
|
|
193
|
+
~limit: int,
|
|
194
|
+
~correlationId: option<string>,
|
|
195
|
+
): array<(float, DcbEventLog_Adapter.rawSequencedEvent)> => {
|
|
196
|
+
let b = mkBuilder()
|
|
197
|
+
let parts = [`log_name = ${b->param(JSON.Encode.string(name))}`]
|
|
198
|
+
switch bound {
|
|
199
|
+
| Some(Below(p)) => parts->Array.push(`position < ${b->param(JSON.Encode.float(p))}::bigint`)
|
|
200
|
+
| Some(AtMost(p)) => parts->Array.push(`position <= ${b->param(JSON.Encode.float(p))}::bigint`)
|
|
201
|
+
| None => ()
|
|
202
|
+
}
|
|
203
|
+
switch correlationId {
|
|
204
|
+
| Some(id) => parts->Array.push(`(meta->>'correlationId') = ${b->param(JSON.Encode.string(id))}`)
|
|
205
|
+
| None => ()
|
|
206
|
+
}
|
|
207
|
+
let limitP = b->param(JSON.Encode.int(limit))
|
|
208
|
+
|
|
209
|
+
(
|
|
210
|
+
await pool->PgDriver.query(
|
|
211
|
+
`SELECT ${selectColumns}, position::text AS pos_raw FROM dcb_event WHERE ${parts->Array.join(
|
|
212
|
+
" AND ",
|
|
213
|
+
)} ORDER BY position DESC LIMIT ${limitP}::int`,
|
|
214
|
+
b.params,
|
|
215
|
+
)
|
|
216
|
+
)->Array.map(row => (
|
|
217
|
+
switch row->Dict.get("pos_raw") {
|
|
218
|
+
| Some(JSON.String(s)) => s->Float.fromString->Option.getOr(0.)
|
|
219
|
+
| _ => 0.
|
|
220
|
+
},
|
|
221
|
+
rowToEvent(row),
|
|
222
|
+
))
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** The position of each event one append wrote, by message id. The cursor an
|
|
226
|
+
append returns names its transaction, and every event of the append shares
|
|
227
|
+
it. */
|
|
228
|
+
let positionsOfAppend = async (pool: PgDriver.pool, ~name: string, ~cursor: string): array<(
|
|
229
|
+
string,
|
|
230
|
+
float,
|
|
231
|
+
)> =>
|
|
232
|
+
switch decodeCursor(cursor) {
|
|
233
|
+
| None => []
|
|
234
|
+
| Some((tx, _)) =>
|
|
235
|
+
(
|
|
236
|
+
await pool->PgDriver.query(
|
|
237
|
+
"SELECT position::text AS pos, meta->>'msgId' AS msg_id FROM dcb_event WHERE log_name = $1 AND transaction_id = $2::xid8",
|
|
238
|
+
[JSON.Encode.string(name), JSON.Encode.string(tx)],
|
|
239
|
+
)
|
|
240
|
+
)->Array.filterMap(row =>
|
|
241
|
+
switch (row->Dict.get("msg_id"), row->Dict.get("pos")) {
|
|
242
|
+
| (Some(JSON.String(msgId)), Some(JSON.String(pos))) =>
|
|
243
|
+
pos->Float.fromString->Option.map(p => (msgId, p))
|
|
244
|
+
| _ => None
|
|
245
|
+
}
|
|
246
|
+
)
|
|
247
|
+
}
|
|
248
|
+
|
|
178
249
|
// Builds the DCB runtime op set bound to `pool`. Returns `(name, ops)`; the
|
|
179
250
|
// deploy-time `DcbEventLog_Adapter` wrapper lives in `DcbEventLogStorage_Postgres`.
|
|
180
251
|
let makeOps = (
|
|
@@ -222,49 +293,45 @@ let makeOps = (
|
|
|
222
293
|
}
|
|
223
294
|
}
|
|
224
295
|
|
|
225
|
-
let append = async (
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
296
|
+
let append = async (newEvents: array<DcbEventLog_Adapter.rawStoredEvent>, ~condition=?): result<
|
|
297
|
+
DcbTag.sequencePosition,
|
|
298
|
+
ReventlessInfra.DcbEventLog.appendError,
|
|
299
|
+
> => {
|
|
229
300
|
let eventsJson = JSON.Array(
|
|
230
|
-
newEvents->Array.map(ev =>
|
|
231
|
-
|
|
301
|
+
newEvents->Array.map(ev => JSON.Object(
|
|
302
|
+
Dict.fromArray([
|
|
303
|
+
("event_type", JSON.Encode.string(ev.eventType)),
|
|
304
|
+
(
|
|
305
|
+
"tags",
|
|
306
|
+
JSON.Array(ev.tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value))),
|
|
307
|
+
),
|
|
308
|
+
("data", ev.data),
|
|
309
|
+
("meta", ev.meta->Reventless.Util_Sury.toJson(Reventless.Message.metaSchema)),
|
|
310
|
+
]),
|
|
311
|
+
)),
|
|
312
|
+
)
|
|
313
|
+
let conditionParam = switch condition {
|
|
314
|
+
| Some(cond: DcbTag.appendCondition) =>
|
|
315
|
+
let queryJson = JSON.Array(
|
|
316
|
+
cond.query->Array.map(clause => JSON.Object(
|
|
232
317
|
Dict.fromArray([
|
|
233
|
-
(
|
|
318
|
+
(
|
|
319
|
+
"eventTypes",
|
|
320
|
+
switch clause.eventTypes {
|
|
321
|
+
| Some(types) => JSON.Array(types->Array.map(JSON.Encode.string))
|
|
322
|
+
| None => JSON.Encode.null
|
|
323
|
+
},
|
|
324
|
+
),
|
|
234
325
|
(
|
|
235
326
|
"tags",
|
|
236
|
-
|
|
327
|
+
switch clause.tags {
|
|
328
|
+
| Some(tags) =>
|
|
329
|
+
JSON.Array(tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value)))
|
|
330
|
+
| None => JSON.Encode.null
|
|
331
|
+
},
|
|
237
332
|
),
|
|
238
|
-
("data", ev.data),
|
|
239
|
-
("meta", ev.meta->S.reverseConvertToJsonOrThrow(Reventless.Message.metaSchema)),
|
|
240
333
|
]),
|
|
241
|
-
)
|
|
242
|
-
),
|
|
243
|
-
)
|
|
244
|
-
let conditionParam = switch condition {
|
|
245
|
-
| Some(cond: DcbTag.appendCondition) =>
|
|
246
|
-
let queryJson = JSON.Array(
|
|
247
|
-
cond.query->Array.map(clause =>
|
|
248
|
-
JSON.Object(
|
|
249
|
-
Dict.fromArray([
|
|
250
|
-
(
|
|
251
|
-
"eventTypes",
|
|
252
|
-
switch clause.eventTypes {
|
|
253
|
-
| Some(types) => JSON.Array(types->Array.map(JSON.Encode.string))
|
|
254
|
-
| None => JSON.Encode.null
|
|
255
|
-
},
|
|
256
|
-
),
|
|
257
|
-
(
|
|
258
|
-
"tags",
|
|
259
|
-
switch clause.tags {
|
|
260
|
-
| Some(tags) =>
|
|
261
|
-
JSON.Array(tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value)))
|
|
262
|
-
| None => JSON.Encode.null
|
|
263
|
-
},
|
|
264
|
-
),
|
|
265
|
-
]),
|
|
266
|
-
)
|
|
267
|
-
),
|
|
334
|
+
)),
|
|
268
335
|
)
|
|
269
336
|
let fields = [("query", queryJson)]
|
|
270
337
|
switch cond.after {
|
|
@@ -306,7 +373,10 @@ let makeOps = (
|
|
|
306
373
|
? Error(ReventlessInfra.DcbEventLog.Conflict)
|
|
307
374
|
: Error(
|
|
308
375
|
ReventlessInfra.DcbEventLog.StorageFailure(
|
|
309
|
-
exn
|
|
376
|
+
exn
|
|
377
|
+
->JsExn.fromException
|
|
378
|
+
->Option.flatMap(JsExn.message)
|
|
379
|
+
->Option.getOr("storage error"),
|
|
310
380
|
),
|
|
311
381
|
)
|
|
312
382
|
}
|
|
@@ -324,7 +394,9 @@ let makeOps = (
|
|
|
324
394
|
Effect.promise(() => {
|
|
325
395
|
let b = mkBuilder()
|
|
326
396
|
let where = buildReadWhere(b, ~name, ~query, ~after=cursor, ~applyFence=true)
|
|
327
|
-
let sql = `SELECT ${selectColumns}, transaction_id::text AS tx_raw, position::text AS pos_raw FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC LIMIT ${Int.toString(
|
|
397
|
+
let sql = `SELECT ${selectColumns}, transaction_id::text AS tx_raw, position::text AS pos_raw FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC LIMIT ${Int.toString(
|
|
398
|
+
pageSize,
|
|
399
|
+
)}`
|
|
328
400
|
pool->PgDriver.query(sql, b.params)
|
|
329
401
|
})->Effect.map(rows => {
|
|
330
402
|
let events = rows->Array.map(rowToEvent)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -15,8 +15,11 @@
|
|
|
15
15
|
// later commit) is never skipped by a checkpointing reader. `global_seq` alone,
|
|
16
16
|
// being an IDENTITY assigned at INSERT, could NOT provide that guarantee.
|
|
17
17
|
|
|
18
|
-
// Reuse the shared `<a>:<b>` cursor codec from the DCB storage — identical
|
|
19
|
-
module
|
|
18
|
+
// Reuse the shared `<a>:<b>` cursor codec from the DCB storage — identical
|
|
19
|
+
// format. Point at the runtime-pure `_Ops` module (no `@pulumi/pulumi`): this
|
|
20
|
+
// feed is drained by the deployed change-feed relay Lambda, whose cold-start
|
|
21
|
+
// graph must stay Pulumi-free.
|
|
22
|
+
module Dcb = DcbEventLogStorage_Postgres_Ops
|
|
20
23
|
|
|
21
24
|
// One classic event as the feed surfaces it. `payload` is the stored event JSON
|
|
22
25
|
// verbatim; a bus/topic relay transforms it into its target shape (e.g. the AWS
|
|
@@ -87,11 +90,12 @@ let readBatch = async (
|
|
|
87
90
|
params->Array.push(JSON.Encode.string(tx))
|
|
88
91
|
params->Array.push(JSON.Encode.string(pos))
|
|
89
92
|
where :=
|
|
90
|
-
where.contents ++
|
|
91
|
-
" AND (transaction_id > $2::xid8 OR (transaction_id = $2::xid8 AND global_seq > $3::bigint))"
|
|
93
|
+
where.contents ++ " AND (transaction_id > $2::xid8 OR (transaction_id = $2::xid8 AND global_seq > $3::bigint))"
|
|
92
94
|
| None => ()
|
|
93
95
|
}
|
|
94
|
-
let sql = `SELECT ${selectColumns} FROM event_log WHERE ${where.contents} ORDER BY transaction_id ASC, global_seq ASC LIMIT ${Int.toString(
|
|
96
|
+
let sql = `SELECT ${selectColumns} FROM event_log WHERE ${where.contents} ORDER BY transaction_id ASC, global_seq ASC LIMIT ${Int.toString(
|
|
97
|
+
limit,
|
|
98
|
+
)}`
|
|
95
99
|
let rows = await pool->PgDriver.query(sql, params)
|
|
96
100
|
let events = rows->Array.map(rowToEvent)
|
|
97
101
|
let cursor = switch events->Array.get(events->Array.length - 1) {
|
|
@@ -163,7 +167,12 @@ let drain = async (
|
|
|
163
167
|
let cursor = ref(await loadCheckpoint(pool, ~subscriber))
|
|
164
168
|
let continue = ref(true)
|
|
165
169
|
while continue.contents {
|
|
166
|
-
let {events, cursor: newCursor} = await readBatch(
|
|
170
|
+
let {events, cursor: newCursor} = await readBatch(
|
|
171
|
+
pool,
|
|
172
|
+
~logName,
|
|
173
|
+
~after=?cursor.contents,
|
|
174
|
+
~limit,
|
|
175
|
+
)
|
|
167
176
|
if events->Array.length == 0 {
|
|
168
177
|
continue := false
|
|
169
178
|
} else {
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// real Lambda). This module `include`s that pure module and adds only the
|
|
8
8
|
// deploy-time `EventLog_Adapter` shape (`operations` wrapped in `Pulumi.Output`),
|
|
9
9
|
// keeping the historic `makeStorage` 3-tuple `(name, ops, adapter)` API unchanged.
|
|
10
|
-
// See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
|
|
10
|
+
// See docs/plans/done/deployed-lambda-esm-self-containment.md (Rung-3 finding).
|
|
11
11
|
|
|
12
12
|
open ReventlessCore
|
|
13
13
|
include EventLogStorage_Postgres_Ops
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// in no Lambda — not the layer, not `/var/runtime`). The thin
|
|
8
8
|
// `EventLogStorage_Postgres` wrapper adds the deploy-time `Pulumi.Output`-shaped
|
|
9
9
|
// adapter on top for local/deploy consumers; the runtime imports `makeOps` here.
|
|
10
|
-
// See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
|
|
10
|
+
// See docs/plans/done/deployed-lambda-esm-self-containment.md (Rung-3 finding).
|
|
11
11
|
|
|
12
12
|
open ReventlessCore
|
|
13
13
|
|
|
@@ -42,14 +42,62 @@ let isUniqueViolation = (exn: exn): bool => {
|
|
|
42
42
|
msg->String.includes("23505")
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
/** Where a backwards read of one log starts: strictly below a position, or at
|
|
46
|
+
and below it. */
|
|
47
|
+
type bound =
|
|
48
|
+
| Below(float)
|
|
49
|
+
| AtMost(float)
|
|
50
|
+
|
|
51
|
+
/** One log's stored events newest first, across its entities, by `global_seq`,
|
|
52
|
+
narrowed to one correlation id when given. Rows are `(global_seq, aggregate
|
|
53
|
+
id, stored event)`. */
|
|
54
|
+
let readBackwards = async (
|
|
55
|
+
pool: PgDriver.pool,
|
|
56
|
+
~name: string,
|
|
57
|
+
~bound: option<bound>,
|
|
58
|
+
~limit: int,
|
|
59
|
+
~correlationId: option<string>,
|
|
60
|
+
): array<(float, string, JSON.t)> => {
|
|
61
|
+
let params = [JSON.Encode.string(name)]
|
|
62
|
+
let param = v => {
|
|
63
|
+
params->Array.push(v)
|
|
64
|
+
"$" ++ params->Array.length->Int.toString
|
|
65
|
+
}
|
|
66
|
+
let parts = ["log_name = $1"]
|
|
67
|
+
switch bound {
|
|
68
|
+
| Some(Below(p)) => parts->Array.push(`global_seq < ${param(JSON.Encode.float(p))}::bigint`)
|
|
69
|
+
| Some(AtMost(p)) => parts->Array.push(`global_seq <= ${param(JSON.Encode.float(p))}::bigint`)
|
|
70
|
+
| None => ()
|
|
71
|
+
}
|
|
72
|
+
switch correlationId {
|
|
73
|
+
| Some(id) => parts->Array.push(`(payload->>'correlationId') = ${param(JSON.Encode.string(id))}`)
|
|
74
|
+
| None => ()
|
|
75
|
+
}
|
|
76
|
+
let limitP = param(JSON.Encode.int(limit))
|
|
77
|
+
|
|
78
|
+
(
|
|
79
|
+
await pool->PgDriver.query(
|
|
80
|
+
`SELECT global_seq::text AS pos, aggregate_id, payload FROM event_log WHERE ${parts->Array.join(
|
|
81
|
+
" AND ",
|
|
82
|
+
)} ORDER BY global_seq DESC LIMIT ${limitP}::int`,
|
|
83
|
+
params,
|
|
84
|
+
)
|
|
85
|
+
)->Array.map(row => (
|
|
86
|
+
row->Dict.get("pos")->Option.mapOr(0, coerceInt)->Int.toFloat,
|
|
87
|
+
switch row->Dict.get("aggregate_id") {
|
|
88
|
+
| Some(JSON.String(id)) => id
|
|
89
|
+
| _ => ""
|
|
90
|
+
},
|
|
91
|
+
decodePayload(row),
|
|
92
|
+
))
|
|
93
|
+
}
|
|
94
|
+
|
|
45
95
|
// Builds the classic runtime op set bound to `pool`. Returns `(name, ops)`; the
|
|
46
96
|
// deploy-time `EventLog_Adapter` wrapper lives in `EventLogStorage_Postgres`.
|
|
47
|
-
let makeOps = (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
~onAppended=noTracking,
|
|
52
|
-
): (string, EventLog_Adapter.operations) => {
|
|
97
|
+
let makeOps = (~pool: PgDriver.pool, ~name: string, ~opts as _, ~onAppended=noTracking): (
|
|
98
|
+
string,
|
|
99
|
+
EventLog_Adapter.operations,
|
|
100
|
+
) => {
|
|
53
101
|
let appendTracked = async (~track, seqNr: int, id: string, jsons: array<JSON.t>) => {
|
|
54
102
|
// Empty append is a legitimate no-op (at-least-once retries, idempotent
|
|
55
103
|
// commands), NOT a conflict — return before the guarded insert whose 0-row
|
|
@@ -95,7 +143,10 @@ let makeOps = (
|
|
|
95
143
|
? Error(EventLog.Conflict)
|
|
96
144
|
: Error(
|
|
97
145
|
EventLog.StorageFailure(
|
|
98
|
-
exn
|
|
146
|
+
exn
|
|
147
|
+
->JsExn.fromException
|
|
148
|
+
->Option.flatMap(JsExn.message)
|
|
149
|
+
->Option.getOr("storage error"),
|
|
99
150
|
),
|
|
100
151
|
)
|
|
101
152
|
}
|
|
@@ -106,12 +157,14 @@ let makeOps = (
|
|
|
106
157
|
appendTracked(~track=true, seqNr, id, jsons)
|
|
107
158
|
|
|
108
159
|
let replayArray = async (id: string, ~fromSeq=0): array<JSON.t> =>
|
|
109
|
-
(
|
|
110
|
-
|
|
160
|
+
(
|
|
161
|
+
await pool->PgDriver.query(
|
|
162
|
+
"SELECT payload FROM event_log
|
|
111
163
|
WHERE log_name = $1 AND aggregate_id = $2 AND seq_nr >= $3::bigint
|
|
112
164
|
ORDER BY seq_nr ASC",
|
|
113
|
-
|
|
114
|
-
|
|
165
|
+
[JSON.Encode.string(name), JSON.Encode.string(id), JSON.Encode.int(fromSeq)],
|
|
166
|
+
)
|
|
167
|
+
)->Array.map(decodePayload)
|
|
115
168
|
|
|
116
169
|
let replay: EventLog.replay<string, JSON.t> = id => replayArray(id)
|
|
117
170
|
|
|
@@ -152,14 +205,21 @@ let makeOps = (
|
|
|
152
205
|
Ok()
|
|
153
206
|
} catch {
|
|
154
207
|
| exn =>
|
|
155
|
-
Error(
|
|
208
|
+
Error(
|
|
209
|
+
exn
|
|
210
|
+
->JsExn.fromException
|
|
211
|
+
->Option.flatMap(JsExn.message)
|
|
212
|
+
->Option.getOr("snapshot write error"),
|
|
213
|
+
)
|
|
156
214
|
}
|
|
157
215
|
|
|
158
216
|
let appendStream: EventLog.appendStream<string, JSON.t> = (startingSeqNr, id, stream) =>
|
|
159
217
|
stream
|
|
160
218
|
->Stream.runCollect
|
|
161
219
|
->Effect.flatMap(jsons =>
|
|
162
|
-
Effect.promise(() =>
|
|
220
|
+
Effect.promise(() =>
|
|
221
|
+
appendTracked(~track=false, startingSeqNr, id, jsons)
|
|
222
|
+
)->Effect.flatMap(result =>
|
|
163
223
|
switch result {
|
|
164
224
|
| Ok() => Effect.succeed()
|
|
165
225
|
| Error(EventLog.Conflict) => Effect.fail("conflict")
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -18,7 +18,12 @@
|
|
|
18
18
|
open ReventlessCore
|
|
19
19
|
open Reventless
|
|
20
20
|
|
|
21
|
-
module
|
|
21
|
+
// Runtime-pure ops module (no `@pulumi/pulumi`) — this feed is drained by the
|
|
22
|
+
// deployed change-feed relay Lambda, so its cold-start graph must stay
|
|
23
|
+
// Pulumi-free. `_Ops` carries every helper used below (mkBuilder,
|
|
24
|
+
// buildReadWhere, decodeCursor, selectColumns, rowToEvent); the deploy-time
|
|
25
|
+
// DcbEventLogStorage_Postgres only `include`s it and adds the Pulumi adapter.
|
|
26
|
+
module Dcb = DcbEventLogStorage_Postgres_Ops
|
|
22
27
|
|
|
23
28
|
// One page of the feed. `events` are in (transaction_id, position) order;
|
|
24
29
|
// `cursor` is the checkpoint to persist and to pass as the next `after` (None
|
|
@@ -44,7 +49,9 @@ let readBatch = async (
|
|
|
44
49
|
~after=after->Option.flatMap(Dcb.decodeCursor),
|
|
45
50
|
~applyFence=true,
|
|
46
51
|
)
|
|
47
|
-
let sql = `SELECT ${Dcb.selectColumns} FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC LIMIT ${Int.toString(
|
|
52
|
+
let sql = `SELECT ${Dcb.selectColumns} FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC LIMIT ${Int.toString(
|
|
53
|
+
limit,
|
|
54
|
+
)}`
|
|
48
55
|
let rows = await pool->PgDriver.query(sql, b.params)
|
|
49
56
|
let events = rows->Array.map(Dcb.rowToEvent)
|
|
50
57
|
let cursor = switch events->Array.get(events->Array.length - 1) {
|
|
@@ -58,10 +65,9 @@ let readBatch = async (
|
|
|
58
65
|
|
|
59
66
|
// The persisted checkpoint for `subscriber`, encoded as a cursor, or None if the
|
|
60
67
|
// subscriber has never committed (→ replay from the beginning).
|
|
61
|
-
let loadCheckpoint = async (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
): option<DcbTag.sequencePosition> =>
|
|
68
|
+
let loadCheckpoint = async (pool: PgDriver.pool, ~subscriber: string): option<
|
|
69
|
+
DcbTag.sequencePosition,
|
|
70
|
+
> =>
|
|
65
71
|
switch await pool->PgDriver.queryOne(
|
|
66
72
|
"SELECT lpad(last_tx::text, 20, '0') || ':' || lpad(last_position::text, 20, '0') AS cursor
|
|
67
73
|
FROM dcb_subscription WHERE subscriber = $1 AND last_position > 0",
|
|
@@ -100,8 +106,9 @@ let saveCheckpoint = async (
|
|
|
100
106
|
// dedicated client (caller owns it — release via PgDriver.unlisten). Pair with a
|
|
101
107
|
// low-frequency fallback tick in the consumer loop so a missed NOTIFY only adds
|
|
102
108
|
// latency, never a permanent stall.
|
|
103
|
-
let listen = (pool: PgDriver.pool, ~logName: string, ~onWake: unit => unit): promise<
|
|
104
|
-
|
|
109
|
+
let listen = (pool: PgDriver.pool, ~logName: string, ~onWake: unit => unit): promise<
|
|
110
|
+
PgDriver.client,
|
|
111
|
+
> => pool->PgDriver.listen(~channel="dcb_" ++ logName, ~onNotify=_ => onWake())
|
|
105
112
|
|
|
106
113
|
let unlisten = (client: PgDriver.client, ~logName: string): promise<unit> =>
|
|
107
114
|
client->PgDriver.unlisten(~channel="dcb_" ++ logName)
|
|
@@ -124,7 +131,12 @@ let drain = async (
|
|
|
124
131
|
let cursor = ref(await loadCheckpoint(pool, ~subscriber))
|
|
125
132
|
let continue = ref(true)
|
|
126
133
|
while continue.contents {
|
|
127
|
-
let {events, cursor: newCursor} = await readBatch(
|
|
134
|
+
let {events, cursor: newCursor} = await readBatch(
|
|
135
|
+
pool,
|
|
136
|
+
~logName,
|
|
137
|
+
~after=?cursor.contents,
|
|
138
|
+
~limit,
|
|
139
|
+
)
|
|
128
140
|
if events->Array.length == 0 {
|
|
129
141
|
continue := false
|
|
130
142
|
} else {
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/lib/ocaml/PgDriver.ast
CHANGED
|
Binary file
|
package/lib/ocaml/PgDriver.iast
CHANGED
|
Binary file
|
package/lib/ocaml/PgDriver.res
CHANGED
|
@@ -47,8 +47,7 @@ type queryResult = {rows: array<dict<JSON.t>>}
|
|
|
47
47
|
|
|
48
48
|
let query = async (pool, sql, params) => (await _query(pool, sql, params)).rows
|
|
49
49
|
|
|
50
|
-
let queryOne = async (pool, sql, params) =>
|
|
51
|
-
(await _query(pool, sql, params)).rows->Array.get(0)
|
|
50
|
+
let queryOne = async (pool, sql, params) => (await _query(pool, sql, params)).rows->Array.get(0)
|
|
52
51
|
|
|
53
52
|
let exec = async (pool, sql) => {
|
|
54
53
|
let _ = await _queryText(pool, sql)
|
|
Binary file
|
|
Binary file
|
package/lib/ocaml/PgSchema.ast
CHANGED
|
Binary file
|
package/lib/ocaml/PgSchema.res
CHANGED
|
@@ -44,6 +44,10 @@ let tableStatements = [
|
|
|
44
44
|
// every future insert with no change to EventLogStorage_Postgres.append.
|
|
45
45
|
"ALTER TABLE event_log ADD COLUMN IF NOT EXISTS transaction_id xid8 NOT NULL DEFAULT pg_current_xact_id()",
|
|
46
46
|
"CREATE INDEX IF NOT EXISTS event_log_tx_gseq ON event_log (log_name, transaction_id, global_seq)",
|
|
47
|
+
// Reading one log newest first across its entities, and finding every event of
|
|
48
|
+
// one flow. The stored event is flat, so the correlation id is a top-level key.
|
|
49
|
+
"CREATE INDEX IF NOT EXISTS event_log_log_gseq ON event_log (log_name, global_seq)",
|
|
50
|
+
"CREATE INDEX IF NOT EXISTS event_log_correlation ON event_log (log_name, (payload->>'correlationId'), global_seq)",
|
|
47
51
|
"CREATE TABLE IF NOT EXISTS event_log_subscription (
|
|
48
52
|
subscriber text PRIMARY KEY,
|
|
49
53
|
last_tx xid8 NOT NULL DEFAULT '0'::xid8,
|
|
@@ -64,6 +68,7 @@ let tableStatements = [
|
|
|
64
68
|
"CREATE INDEX IF NOT EXISTS dcb_event_tags_gin ON dcb_event USING gin (tags)",
|
|
65
69
|
"CREATE INDEX IF NOT EXISTS dcb_event_type_pos ON dcb_event (log_name, event_type, position)",
|
|
66
70
|
"CREATE INDEX IF NOT EXISTS dcb_event_tx_pos ON dcb_event (log_name, transaction_id, position)",
|
|
71
|
+
"CREATE INDEX IF NOT EXISTS dcb_event_correlation ON dcb_event (log_name, (meta->>'correlationId'), position)",
|
|
67
72
|
// --- Row-lock companion (B4) ---
|
|
68
73
|
"CREATE TABLE IF NOT EXISTS dcb_scope (
|
|
69
74
|
log_name text NOT NULL,
|
|
@@ -132,6 +137,9 @@ BEGIN
|
|
|
132
137
|
INTO v_types
|
|
133
138
|
FROM jsonb_array_elements_text(qi->'eventTypes') t;
|
|
134
139
|
v_parts := array_append(v_parts, 'event_type IN (' || v_types || ')');
|
|
140
|
+
ELSIF jsonb_typeof(qi->'eventTypes') = 'array' THEN
|
|
141
|
+
-- A clause with no event type matches nothing.
|
|
142
|
+
v_parts := array_append(v_parts, 'FALSE');
|
|
135
143
|
END IF;
|
|
136
144
|
|
|
137
145
|
IF jsonb_typeof(qi->'tags') = 'array'
|