@reventlessdev/reventless-core 3.0.0-alpha.341 → 3.0.0-alpha.342
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 -0
- package/package.json +7 -7
- package/src/Projection.res +32 -4
- package/src/Projection.res.mjs +51 -4
- package/src/ViewChange.res +1 -9
- package/src/ViewChange.res.mjs +2 -13
- package/src/admin/Platform_ComponentDefinitionsApi.res +29 -1
- package/src/admin/Platform_ComponentDefinitionsApi.res.mjs +47 -1
- package/src/admin/Platform_PluginStructuresApi.res +4 -3
- package/src/admin/Platform_PluginStructuresApi.res.mjs +5 -1
- package/src/components/Api/SuryToJsonSchema.res +7 -0
- package/src/components/Api/SuryToJsonSchema.res.mjs +18 -0
- package/src/components/Dcb/Dcb_Builder.res +59 -0
- package/src/components/Dcb/Dcb_Builder.res.mjs +48 -3
- package/src/components/DcbEventLog/DcbEventLog.res +2 -0
- package/src/components/DcbEventLog/DcbEventLog_Adapter.res +7 -0
- package/src/components/DcbEventLog/DcbEventLog_Builder.res +12 -3
- package/src/components/DcbEventLog/DcbEventLog_Builder.res.mjs +5 -3
- package/src/components/DcbEventLog/DcbEventLog_Operations.res +3 -0
- package/src/components/DcbEventLog/DcbEventLog_Operations.res.mjs +3 -1
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res.mjs +2 -2
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +158 -107
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +178 -154
- package/src/components/StateChangeSlice/StateChangeSlice.res +3 -0
- package/src/components/StateChangeSlice/StateChangeSlice_Builder.res +6 -0
- package/src/components/StateChangeSlice/StateChangeSlice_Builder.res.mjs +5 -4
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +369 -279
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +210 -166
- package/src/plugin/component/Plugin_Structure.res +45 -0
- package/src/plugin/component/Plugin_Structure.res.mjs +42 -1
- package/src/util/Logger.res +5 -5
- package/src/util/Logger.res.mjs +3 -3
- package/tests/admin/AdminApiSchemaDriftTest.res +23 -0
- package/tests/admin/AdminApiSchemaDriftTest.res.mjs +21 -1
- package/tests/admin/Platform_ComponentDefinitionsApiTest.res +1 -0
- package/tests/admin/Platform_ComponentDefinitionsApiTest.res.mjs +1 -0
- package/tests/admin/Platform_PluginStructuresApiTest.res +1 -0
- package/tests/admin/Platform_PluginStructuresApiTest.res.mjs +1 -0
- package/tests/api/QuantitySemanticTest.res +68 -0
- package/tests/api/QuantitySemanticTest.res.mjs +68 -0
- package/tests/commandgenerator/OwnerActingTest.res.mjs +1 -1
- package/tests/commandtopic/CommandOutcomeHookTest.res.mjs +1 -1
- package/tests/dcb/DcbFenceCountReadTest.res +213 -0
- package/tests/dcb/DcbFenceCountReadTest.res.mjs +358 -0
- package/tests/dcb/DcbPiiSliceTest.res.mjs +1 -1
- package/tests/dcb/DcbReadsNothingTest.res.mjs +1 -1
- package/tests/dcb/DcbScopeInferenceTest.res +143 -2
- package/tests/dcb/DcbScopeInferenceTest.res.mjs +192 -2
- package/tests/dcb/DcbStateChangeSliceTest.res.mjs +22 -22
- package/tests/dcb/DcbTagTest.res +68 -0
- package/tests/dcb/DcbTagTest.res.mjs +86 -10
- package/tests/dcb/DcbValidationTest.res.mjs +1 -1
- package/tests/logger/LoggerLazyTest.res.mjs +3 -3
- package/tests/plugin/PluginStructureAccessTest.res +1 -0
- package/tests/plugin/PluginStructureAccessTest.res.mjs +1 -1
- package/tests/plugin/PluginStructureTest.res +61 -0
- package/tests/plugin/PluginStructureTest.res.mjs +78 -32
- package/tests/plugin/StateChange/PsRateSession.res +37 -0
- package/tests/plugin/StateChange/PsRateSession.res.mjs +80 -0
- package/tests/plugin/pluginDefinitionRequiredScalars.txt +10 -0
- package/tests/projection/ViewChangeTest.res +32 -0
- package/tests/projection/ViewChangeTest.res.mjs +57 -0
|
@@ -4,6 +4,8 @@ module type T = {
|
|
|
4
4
|
let handleCommands: (
|
|
5
5
|
~tagKeysByEventType: Dict.t<array<string>>=?,
|
|
6
6
|
~crossPartitionTagKeys: array<string>=?,
|
|
7
|
+
/** The boundary's pairs; the slice reads those it is the reader of from the index. */
|
|
8
|
+
~crossPartitionPairs: array<Reventless.DcbScopeInference.pair>=?,
|
|
7
9
|
/** This slice's partition within its boundary; inferred from the slice alone when absent. */
|
|
8
10
|
~partitionTag: Reventless.DcbTag.derivedPartitionTag=?,
|
|
9
11
|
DcbEventLog.operations,
|
|
@@ -233,10 +235,11 @@ module Make = (
|
|
|
233
235
|
// (see the plan's Step 4).
|
|
234
236
|
let projectionCacheCapacity = 100
|
|
235
237
|
// The owners folded so far ride along with the state: a delta read never
|
|
236
|
-
// sees the event that recorded them.
|
|
238
|
+
// sees the event that recorded them. So do the events read per cross-partition
|
|
239
|
+
// fence, which a delta read adds to.
|
|
237
240
|
let projectionCache: Lru.t<
|
|
238
241
|
string,
|
|
239
|
-
(Decision.state, option<Reventless.DcbTag.sequencePosition>, array<string>),
|
|
242
|
+
(Decision.state, option<Reventless.DcbTag.sequencePosition>, array<string>, dict<int>),
|
|
240
243
|
> = Lru.make(~capacity=projectionCacheCapacity, ~ttlMs=Lru.decodedStateTtlMs())
|
|
241
244
|
|
|
242
245
|
let resetCache = () => projectionCache->Lru.clear
|
|
@@ -253,6 +256,7 @@ module Make = (
|
|
|
253
256
|
~tagKeysByEventType,
|
|
254
257
|
~crossPartitionTagKeys,
|
|
255
258
|
~payloadTagKeys,
|
|
259
|
+
~pairEventTypes,
|
|
256
260
|
~partitionTag,
|
|
257
261
|
dcbEventLog: DcbEventLog.operations,
|
|
258
262
|
command': Message.command'<Reventless.Id.String.t, Spec.command>,
|
|
@@ -282,6 +286,7 @@ module Make = (
|
|
|
282
286
|
~tagKeysByEventType,
|
|
283
287
|
~crossPartitionTagKeys,
|
|
284
288
|
~payloadTagKeys,
|
|
289
|
+
~pairEventTypes,
|
|
285
290
|
)
|
|
286
291
|
|
|
287
292
|
// Log the DCB query parameters — the OR clauses (event types + tags) the
|
|
@@ -391,10 +396,63 @@ module Make = (
|
|
|
391
396
|
~slice=Spec.name,
|
|
392
397
|
)
|
|
393
398
|
|
|
399
|
+
// The cross-partition clauses read through an eventually consistent index.
|
|
400
|
+
// Their fences are read first, strongly, and the read must reach their counts.
|
|
401
|
+
let fenceClauses = Reventless.DcbFenceCount.clauses(query, ~crossPartitionTagKeys)
|
|
402
|
+
|
|
394
403
|
let rec attempt = (~retries) => {
|
|
395
|
-
let (baseState, afterPos, baseOwners) = switch seed.contents {
|
|
396
|
-
| Some((state, head, owners)) => (state, head, owners)
|
|
397
|
-
| None => (Decision.initialState, None, [])
|
|
404
|
+
let (baseState, afterPos, baseOwners, baseSeen) = switch seed.contents {
|
|
405
|
+
| Some((state, head, owners, seen)) => (state, head, owners, seen)
|
|
406
|
+
| None => (Decision.initialState, None, [], Dict.make())
|
|
407
|
+
}
|
|
408
|
+
let seen = baseSeen->Dict.copy
|
|
409
|
+
let fencesRead = switch (fenceClauses, dcbEventLog.readFences) {
|
|
410
|
+
| ([], _) | (_, None) => Effect.succeed([])
|
|
411
|
+
| (clauses, Some(readFences)) =>
|
|
412
|
+
Effect.tryPromise(
|
|
413
|
+
~catch=e => `reading the fences failed: ${String.make(e)}`,
|
|
414
|
+
() => readFences(clauses->Array.map(c => c.tag)),
|
|
415
|
+
)
|
|
416
|
+
}
|
|
417
|
+
// The index has not yet shown every event its fence counted. A missed event
|
|
418
|
+
// may sit below the head, so the next attempt reads in full, after a wait.
|
|
419
|
+
let indexBehind = (missing: array<Reventless.DcbFenceCount.shortfall>) => {
|
|
420
|
+
let detail =
|
|
421
|
+
missing
|
|
422
|
+
->Array.map(m =>
|
|
423
|
+
`${m.tag.key}=${m.tag.value} ${m.eventType}: read ${m.read->Int.toString} of ${m.counted->Int.toString}`
|
|
424
|
+
)
|
|
425
|
+
->Array.join(", ")
|
|
426
|
+
if retries > 0 {
|
|
427
|
+
seed := None
|
|
428
|
+
EffectLogger.logWarn(
|
|
429
|
+
~comp,
|
|
430
|
+
~ids,
|
|
431
|
+
`index behind its fence (retrying ${(maxRetries - retries + 1)
|
|
432
|
+
->Int.toString}/${maxRetries->Int.toString}): ${detail}`,
|
|
433
|
+
)
|
|
434
|
+
->Effect.flatMap(_ => Effect.sleep(Duration.millis(100 * (maxRetries - retries + 1))))
|
|
435
|
+
->Effect.flatMap(_ => attempt(~retries=retries - 1))
|
|
436
|
+
} else {
|
|
437
|
+
cacheInvalidate()
|
|
438
|
+
CommandTopic_Helpers.reportRejected(
|
|
439
|
+
~component=Spec.name,
|
|
440
|
+
~correlationId=cmdJson.meta.correlationId,
|
|
441
|
+
~claimed=cmdJson.meta.callerClaim->Option.isSome,
|
|
442
|
+
~causationId=?cmdJson.meta.causationId,
|
|
443
|
+
~cause=InfrastructureFailure,
|
|
444
|
+
cmdJson.meta.msgId,
|
|
445
|
+
{
|
|
446
|
+
errorCode: Reventless.CommandOutcome.code(Conflict),
|
|
447
|
+
errorDetail: `index behind its fence: ${detail}`,
|
|
448
|
+
},
|
|
449
|
+
)
|
|
450
|
+
EffectLogger.logError(
|
|
451
|
+
~comp,
|
|
452
|
+
~ids,
|
|
453
|
+
`index behind its fence, retries exhausted: ${detail}`,
|
|
454
|
+
)->Effect.map(_ => Error(detail))
|
|
455
|
+
}
|
|
398
456
|
}
|
|
399
457
|
let cacheHit = seed.contents->Option.isSome
|
|
400
458
|
|
|
@@ -416,305 +474,333 @@ module Make = (
|
|
|
416
474
|
// A slice that consumes nothing (`consumedEvent = unit`) reads nothing: the store is
|
|
417
475
|
// not asked at all.
|
|
418
476
|
|
|
419
|
-
(
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
->Stream.mapEffect(openRaw)
|
|
425
|
-
// The head accumulator starts at `afterPos` (the seeded head), not `None`,
|
|
426
|
-
// so an empty delta read keeps the cached head — the append must condition
|
|
427
|
-
// on the head that actually exists, not assert "no events exist yet".
|
|
428
|
-
// Every read event moves the head and is checked as this command's own
|
|
429
|
-
// doing before it is decoded; only the consumed ones reach `evolve`.
|
|
430
|
-
->Stream.runFold((baseState, afterPos, [], baseOwners, false), (
|
|
431
|
-
(dm, _pos, reads, owners, repeated),
|
|
432
|
-
raw,
|
|
433
|
-
) => {
|
|
434
|
-
// Only a command with a stable id asks; a fresh id never matches a stored
|
|
435
|
-
// cause, and asking would only make the answer depend on what the slice
|
|
436
|
-
// happens to read.
|
|
437
|
-
let repeated =
|
|
438
|
-
repeated || (asksForRepeat && raw.meta.causationId == Some(command'.meta.msgId))
|
|
439
|
-
let data = raw.data->JSON.Decode.object->Option.getOr(Dict.make())
|
|
440
|
-
switch decoder.decode(~eventType=raw.eventType, ~data) {
|
|
441
|
-
| None => (dm, Some(raw.position), reads, owners, repeated)
|
|
442
|
-
| Some(event) => (
|
|
443
|
-
Decision.evolve(dm, event),
|
|
444
|
-
Some(raw.position),
|
|
445
|
-
reads->Array.concat([
|
|
446
|
-
switch readEventId(~partitionTag, raw.tags) {
|
|
447
|
-
| Some(id) => `${raw.eventType->LogFormat.bold}(${id})`
|
|
448
|
-
| None => raw.eventType->LogFormat.bold
|
|
449
|
-
},
|
|
450
|
-
]),
|
|
451
|
-
owners->Reventless.OwnerScope.recordOwners(~fields=ownerFieldsOf(raw), data),
|
|
452
|
-
repeated,
|
|
453
|
-
)
|
|
454
|
-
}
|
|
455
|
-
})
|
|
456
|
-
->Effect.tap(((_, _, reads, _, _)) => {
|
|
457
|
-
// On a warm first attempt, record how many events the delta read
|
|
458
|
-
// returned — near-zero on a healthy hit; a large value means the entity
|
|
459
|
-
// churned between commands, so the cache saved little. Gated to the first
|
|
460
|
-
// attempt (retries at maxRetries) so each command yields at most one
|
|
461
|
-
// sample, aligned 1:1 with a CacheHit. Conflict-driven retry reads are
|
|
462
|
-
// covered by AppendRetry instead.
|
|
463
|
-
if cacheHit && retries == maxRetries {
|
|
464
|
-
Metrics.emitCount(
|
|
465
|
-
~metric="DcbDecisionModelDeltaEventCount",
|
|
466
|
-
~slice=Spec.name,
|
|
467
|
-
~value=reads->Array.length,
|
|
468
|
-
)
|
|
469
|
-
}
|
|
470
|
-
EffectLogger.logInfo(
|
|
471
|
-
~comp,
|
|
472
|
-
~ids,
|
|
473
|
-
`read${cacheHit ? " (cached, delta)" : ""}: ${reads
|
|
474
|
-
->Array.length
|
|
475
|
-
->Int.toString} event(s)${reads->Array.length == 0
|
|
476
|
-
? ""
|
|
477
|
-
: ` [${reads->Array.join(", ")}]`}`,
|
|
477
|
+
fencesRead->Effect.flatMap(fences =>
|
|
478
|
+
(
|
|
479
|
+
Reventless.DcbTag.readsNothing(query)
|
|
480
|
+
? Stream.empty
|
|
481
|
+
: dcbEventLog.readStream(~query, ~after=?afterPos, ~strongConsistency)
|
|
478
482
|
)
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
//
|
|
482
|
-
//
|
|
483
|
-
//
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
483
|
+
->Stream.mapEffect(openRaw)
|
|
484
|
+
// The head accumulator starts at `afterPos` (the seeded head), not `None`,
|
|
485
|
+
// so an empty delta read keeps the cached head — the append must condition
|
|
486
|
+
// on the head that actually exists, not assert "no events exist yet".
|
|
487
|
+
// Every read event moves the head and is checked as this command's own
|
|
488
|
+
// doing before it is decoded; only the consumed ones reach `evolve`.
|
|
489
|
+
->Stream.runFold((baseState, afterPos, [], baseOwners, false), (
|
|
490
|
+
(dm, _pos, reads, owners, repeated),
|
|
491
|
+
raw,
|
|
492
|
+
) => {
|
|
493
|
+
seen->Reventless.DcbFenceCount.countRead(
|
|
494
|
+
fenceClauses,
|
|
495
|
+
~eventType=raw.eventType,
|
|
496
|
+
~tags=raw.tags,
|
|
497
|
+
)
|
|
498
|
+
// Only a command with a stable id asks; a fresh id never matches a stored
|
|
499
|
+
// cause, and asking would only make the answer depend on what the slice
|
|
500
|
+
// happens to read.
|
|
501
|
+
let repeated =
|
|
502
|
+
repeated || (asksForRepeat && raw.meta.causationId == Some(command'.meta.msgId))
|
|
503
|
+
let data = raw.data->JSON.Decode.object->Option.getOr(Dict.make())
|
|
504
|
+
switch decoder.decode(~eventType=raw.eventType, ~data) {
|
|
505
|
+
| None => (dm, Some(raw.position), reads, owners, repeated)
|
|
506
|
+
| Some(event) => (
|
|
507
|
+
Decision.evolve(dm, event),
|
|
508
|
+
Some(raw.position),
|
|
509
|
+
reads->Array.concat([
|
|
510
|
+
switch readEventId(~partitionTag, raw.tags) {
|
|
511
|
+
| Some(id) => `${raw.eventType->LogFormat.bold}(${id})`
|
|
512
|
+
| None => raw.eventType->LogFormat.bold
|
|
513
|
+
},
|
|
514
|
+
]),
|
|
515
|
+
owners->Reventless.OwnerScope.recordOwners(~fields=ownerFieldsOf(raw), data),
|
|
516
|
+
repeated,
|
|
505
517
|
)
|
|
506
|
-
|
|
518
|
+
}
|
|
519
|
+
})
|
|
520
|
+
->Effect.tap(((_, _, reads, _, _)) => {
|
|
521
|
+
// On a warm first attempt, record how many events the delta read
|
|
522
|
+
// returned — near-zero on a healthy hit; a large value means the entity
|
|
523
|
+
// churned between commands, so the cache saved little. Gated to the first
|
|
524
|
+
// attempt (retries at maxRetries) so each command yields at most one
|
|
525
|
+
// sample, aligned 1:1 with a CacheHit. Conflict-driven retry reads are
|
|
526
|
+
// covered by AppendRetry instead.
|
|
527
|
+
if cacheHit && retries == maxRetries {
|
|
528
|
+
Metrics.emitCount(
|
|
529
|
+
~metric="DcbDecisionModelDeltaEventCount",
|
|
530
|
+
~slice=Spec.name,
|
|
531
|
+
~value=reads->Array.length,
|
|
532
|
+
)
|
|
533
|
+
}
|
|
534
|
+
EffectLogger.logInfo(
|
|
535
|
+
~comp,
|
|
536
|
+
~ids,
|
|
537
|
+
`read${cacheHit ? " (cached, delta)" : ""}: ${reads
|
|
538
|
+
->Array.length
|
|
539
|
+
->Int.toString} event(s)${reads->Array.length == 0
|
|
540
|
+
? ""
|
|
541
|
+
: ` [${reads->Array.join(", ")}]`}`,
|
|
542
|
+
)
|
|
543
|
+
})
|
|
544
|
+
->Effect.flatMap(((state, headPosition, _, owners, repeated)) =>
|
|
545
|
+
switch Reventless.DcbFenceCount.shortfalls(fences, fenceClauses, seen) {
|
|
546
|
+
| [] =>
|
|
547
|
+
// Identity, not content: the decision model folds the whole matched
|
|
548
|
+
// history, so serialising it here makes the line's size a function of
|
|
549
|
+
// how long the entity has been in use.
|
|
550
|
+
EffectLogger.logDebug(
|
|
507
551
|
~comp,
|
|
508
552
|
~ids,
|
|
509
|
-
`
|
|
553
|
+
`deciding: id=${entityId->Option.getOr("-")} head=${headPosition->Option.getOr(
|
|
510
554
|
"-",
|
|
511
|
-
)}`,
|
|
512
|
-
)->Effect.
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
// snapshot is
|
|
559
|
-
cachePut((state, headPosition, owners))
|
|
560
|
-
CommandTopic_Helpers.
|
|
555
|
+
)} cmd=${cmdJson->LogFormat.cmdName}`,
|
|
556
|
+
)->Effect.flatMap(
|
|
557
|
+
_ =>
|
|
558
|
+
switch ownershipRefusal(owners) {
|
|
559
|
+
| _ if repeated =>
|
|
560
|
+
// Before `decide`, and before the ownership check it passed the first
|
|
561
|
+
// time: the partition already holds events this command caused, so it
|
|
562
|
+
// was delivered again. Accepted, nothing new.
|
|
563
|
+
CommandTopic_Helpers.reportAccepted(
|
|
564
|
+
~component=Spec.name,
|
|
565
|
+
~correlationId=cmdJson.meta.correlationId,
|
|
566
|
+
~causationId=?cmdJson.meta.causationId,
|
|
567
|
+
cmdJson.meta.msgId,
|
|
568
|
+
switch entityId {
|
|
569
|
+
| Some(eid) => {entityId: eid, eventCount: 0}
|
|
570
|
+
| None => {eventCount: 0}
|
|
571
|
+
},
|
|
572
|
+
)
|
|
573
|
+
EffectLogger.logInfo(
|
|
574
|
+
~comp,
|
|
575
|
+
~ids,
|
|
576
|
+
`repeat dropped: ${cmdJson->LogFormat.cmdName} msgId=${cmdJson.meta.msgId} already caused events in id=${entityId->Option.getOr(
|
|
577
|
+
"-",
|
|
578
|
+
)}`,
|
|
579
|
+
)->Effect.map(_ => Ok("ok"))
|
|
580
|
+
| Some({rejected, isDefect}) =>
|
|
581
|
+
// Refused before `decide`; the read snapshot is still valid.
|
|
582
|
+
cachePut((state, headPosition, owners, seen))
|
|
583
|
+
CommandTopic_Helpers.reportRejected(
|
|
584
|
+
~component=Spec.name,
|
|
585
|
+
~correlationId=cmdJson.meta.correlationId,
|
|
586
|
+
~claimed=cmdJson.meta.callerClaim->Option.isSome,
|
|
587
|
+
~causationId=?cmdJson.meta.causationId,
|
|
588
|
+
~cause=AccessRefusal,
|
|
589
|
+
cmdJson.meta.msgId,
|
|
590
|
+
rejected,
|
|
591
|
+
)
|
|
592
|
+
let line = `refused: ${rejected.errorDetail} id=${entityId->Option.getOr("-")}`
|
|
593
|
+
|
|
594
|
+
(
|
|
595
|
+
isDefect
|
|
596
|
+
? EffectLogger.logError(~comp, ~ids, line)
|
|
597
|
+
: EffectLogger.logWarn(~comp, ~ids, line)
|
|
598
|
+
)->Effect.map(_ => Ok("rejected"))
|
|
599
|
+
| None =>
|
|
600
|
+
switch Decision.decide(state, command'.command) {
|
|
601
|
+
| Ok(newEvents) if newEvents->Array.length == 0 =>
|
|
602
|
+
// No append, but the read snapshot is valid — cache it for the next command.
|
|
603
|
+
cachePut((state, headPosition, owners, seen))
|
|
604
|
+
CommandTopic_Helpers.reportAccepted(
|
|
561
605
|
~component=Spec.name,
|
|
562
606
|
~correlationId=cmdJson.meta.correlationId,
|
|
563
|
-
~claimed=cmdJson.meta.callerClaim->Option.isSome,
|
|
564
607
|
~causationId=?cmdJson.meta.causationId,
|
|
565
|
-
~cause=InfrastructureFailure,
|
|
566
608
|
cmdJson.meta.msgId,
|
|
567
|
-
{
|
|
568
|
-
|
|
569
|
-
|
|
609
|
+
switch entityId {
|
|
610
|
+
| Some(eid) => {entityId: eid, eventCount: 0}
|
|
611
|
+
| None => {eventCount: 0}
|
|
570
612
|
},
|
|
571
613
|
)
|
|
572
|
-
EffectLogger.
|
|
573
|
-
_ =>
|
|
614
|
+
EffectLogger.logInfo(~comp, ~ids, "no events produced")->Effect.map(
|
|
615
|
+
_ => Ok("ok"),
|
|
574
616
|
)
|
|
575
|
-
| Ok(
|
|
576
|
-
let
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
rawEvents
|
|
580
|
-
->Array.map(
|
|
581
|
-
e =>
|
|
582
|
-
switch e.data {
|
|
583
|
-
| Object(dict) =>
|
|
584
|
-
Message.combineMessage(e.eventType, dict)->LogFormat.cmdLabelOfJson
|
|
585
|
-
| _ => LogFormat.bold(e.eventType)
|
|
586
|
-
},
|
|
617
|
+
| Ok(newEvents) =>
|
|
618
|
+
let encoded =
|
|
619
|
+
newEvents->Array.map(
|
|
620
|
+
e => encodeEvent(~parentMeta=command'.meta, ~tagKeysByEventType, e),
|
|
587
621
|
)
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
~ids,
|
|
597
|
-
~detail=eventJsons,
|
|
598
|
-
`produced ${eventCount} event(s): [${eventDetails}]`,
|
|
599
|
-
)
|
|
600
|
-
->Effect.flatMap(
|
|
601
|
-
_ =>
|
|
602
|
-
Effect.promise(
|
|
603
|
-
() =>
|
|
604
|
-
// What it did not read cannot have changed under it: no condition.
|
|
605
|
-
Reventless.DcbTag.readsNothing(query)
|
|
606
|
-
? dcbEventLog.append(rawEvents)
|
|
607
|
-
: dcbEventLog.append(rawEvents, ~condition),
|
|
608
|
-
),
|
|
609
|
-
)
|
|
610
|
-
->Effect.flatMap(
|
|
611
|
-
appendResult =>
|
|
612
|
-
switch appendResult {
|
|
613
|
-
| Ok(_position) =>
|
|
614
|
-
// Cache the decided-on state at the read head (NOT including the
|
|
615
|
-
// events we just appended, and NOT the returned position): the
|
|
616
|
-
// next command's delta read picks our events up from `headPosition`.
|
|
617
|
-
cachePut((state, headPosition, owners))
|
|
618
|
-
CommandTopic_Helpers.reportAccepted(
|
|
622
|
+
Effect.promise(() => sealRawEvents(encoded))->Effect.flatMap(
|
|
623
|
+
sealResult =>
|
|
624
|
+
switch sealResult {
|
|
625
|
+
| Error(detail) =>
|
|
626
|
+
// Refused before the append: nothing is stored, and the read
|
|
627
|
+
// snapshot is still valid.
|
|
628
|
+
cachePut((state, headPosition, owners, seen))
|
|
629
|
+
CommandTopic_Helpers.reportRejected(
|
|
619
630
|
~component=Spec.name,
|
|
620
631
|
~correlationId=cmdJson.meta.correlationId,
|
|
632
|
+
~claimed=cmdJson.meta.callerClaim->Option.isSome,
|
|
621
633
|
~causationId=?cmdJson.meta.causationId,
|
|
634
|
+
~cause=InfrastructureFailure,
|
|
622
635
|
cmdJson.meta.msgId,
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
636
|
+
{
|
|
637
|
+
errorCode: Reventless.CommandOutcome.code(AppendFailed),
|
|
638
|
+
errorDetail: detail,
|
|
626
639
|
},
|
|
627
640
|
)
|
|
628
|
-
EffectLogger.
|
|
641
|
+
EffectLogger.logError(
|
|
629
642
|
~comp,
|
|
630
643
|
~ids,
|
|
631
|
-
`append: ${
|
|
632
|
-
)->Effect.map(_ =>
|
|
633
|
-
|
|
|
634
|
-
|
|
635
|
-
//
|
|
636
|
-
let
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
// the delta the conflicting writer added, not full history.
|
|
649
|
-
seed := Some((state, headPosition, owners))
|
|
650
|
-
// Per-slice CloudWatch counter: rising AppendRetry signals a
|
|
651
|
-
// slice feeling contention (the metric to watch the eventual
|
|
652
|
-
// default with — see dcb-high-contention-handling.md).
|
|
653
|
-
Metrics.emitCount(~metric="AppendRetry", ~slice=Spec.name)
|
|
654
|
-
EffectLogger.logWarn(
|
|
655
|
-
~comp,
|
|
656
|
-
~ids,
|
|
657
|
-
`append failed (retrying ${(maxRetries - retries + 1)
|
|
658
|
-
->Int.toString}/${maxRetries->Int.toString}): ${errDetail}`,
|
|
659
|
-
)->Effect.flatMap(_ => attempt(~retries=retries - 1))
|
|
660
|
-
} else {
|
|
661
|
-
// Retries exhausted — drop any cached snapshot so the next
|
|
662
|
-
// command for this entity takes the cold full-read path.
|
|
663
|
-
cacheInvalidate()
|
|
664
|
-
|
|
665
|
-
// A surfaced Conflict means the 3-retry loop could not absorb
|
|
666
|
-
// the contention — the operator signal for "this slice is hot"
|
|
667
|
-
// (consider sharding / async — Issue 10, §4 of the analysis).
|
|
668
|
-
if errorCode == Reventless.CommandOutcome.code(Conflict) {
|
|
669
|
-
Metrics.emitCount(~metric="AppendConflict", ~slice=Spec.name)
|
|
670
|
-
}
|
|
671
|
-
// Both arms here are storage, not the model: a surfaced Conflict is
|
|
672
|
-
// contention the retry loop couldn't absorb, and AppendFailed is the
|
|
673
|
-
// store failing. `decide` had already said Ok.
|
|
674
|
-
CommandTopic_Helpers.reportRejected(
|
|
675
|
-
~component=Spec.name,
|
|
676
|
-
~correlationId=cmdJson.meta.correlationId,
|
|
677
|
-
~claimed=cmdJson.meta.callerClaim->Option.isSome,
|
|
678
|
-
~causationId=?cmdJson.meta.causationId,
|
|
679
|
-
~cause=InfrastructureFailure,
|
|
680
|
-
cmdJson.meta.msgId,
|
|
681
|
-
{errorCode, errorDetail: errDetail},
|
|
644
|
+
`append refused: ${detail}`,
|
|
645
|
+
)->Effect.map(_ => Error(detail))
|
|
646
|
+
| Ok(rawEvents) =>
|
|
647
|
+
let eventCount = rawEvents->Array.length->Int.toString
|
|
648
|
+
// Through the redacting formatter, like every other log line.
|
|
649
|
+
let eventDetails =
|
|
650
|
+
rawEvents
|
|
651
|
+
->Array.map(
|
|
652
|
+
e =>
|
|
653
|
+
switch e.data {
|
|
654
|
+
| Object(dict) =>
|
|
655
|
+
Message.combineMessage(
|
|
656
|
+
e.eventType,
|
|
657
|
+
dict,
|
|
658
|
+
)->LogFormat.cmdLabelOfJson
|
|
659
|
+
| _ => LogFormat.bold(e.eventType)
|
|
660
|
+
},
|
|
682
661
|
)
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
662
|
+
->Array.join(", ")
|
|
663
|
+
let eventJsons = rawEvents->Array.map(e => e.data)->JSON.Encode.array
|
|
664
|
+
let fenceCounts = Reventless.DcbFenceCount.readCounts(
|
|
665
|
+
fences,
|
|
666
|
+
fenceClauses,
|
|
667
|
+
seen,
|
|
668
|
+
)
|
|
669
|
+
let condition: Reventless.DcbTag.appendCondition = {
|
|
670
|
+
query,
|
|
671
|
+
after: ?headPosition,
|
|
672
|
+
fenceCounts: ?(fenceCounts == [] ? None : Some(fenceCounts)),
|
|
688
673
|
}
|
|
674
|
+
EffectLogger.logInfo(
|
|
675
|
+
~comp,
|
|
676
|
+
~ids,
|
|
677
|
+
~detail=eventJsons,
|
|
678
|
+
`produced ${eventCount} event(s): [${eventDetails}]`,
|
|
679
|
+
)
|
|
680
|
+
->Effect.flatMap(
|
|
681
|
+
_ =>
|
|
682
|
+
Effect.promise(
|
|
683
|
+
() =>
|
|
684
|
+
// What it did not read cannot have changed under it: no condition.
|
|
685
|
+
Reventless.DcbTag.readsNothing(query)
|
|
686
|
+
? dcbEventLog.append(rawEvents)
|
|
687
|
+
: dcbEventLog.append(rawEvents, ~condition),
|
|
688
|
+
),
|
|
689
|
+
)
|
|
690
|
+
->Effect.flatMap(
|
|
691
|
+
appendResult =>
|
|
692
|
+
switch appendResult {
|
|
693
|
+
| Ok(_position) =>
|
|
694
|
+
// Cache the decided-on state at the read head (NOT including the
|
|
695
|
+
// events we just appended, and NOT the returned position): the
|
|
696
|
+
// next command's delta read picks our events up from `headPosition`.
|
|
697
|
+
cachePut((state, headPosition, owners, seen))
|
|
698
|
+
CommandTopic_Helpers.reportAccepted(
|
|
699
|
+
~component=Spec.name,
|
|
700
|
+
~correlationId=cmdJson.meta.correlationId,
|
|
701
|
+
~causationId=?cmdJson.meta.causationId,
|
|
702
|
+
cmdJson.meta.msgId,
|
|
703
|
+
switch entityId {
|
|
704
|
+
| Some(eid) => {
|
|
705
|
+
entityId: eid,
|
|
706
|
+
eventCount: rawEvents->Array.length,
|
|
707
|
+
}
|
|
708
|
+
| None => {eventCount: rawEvents->Array.length}
|
|
709
|
+
},
|
|
710
|
+
)
|
|
711
|
+
EffectLogger.logInfo(
|
|
712
|
+
~comp,
|
|
713
|
+
~ids,
|
|
714
|
+
`append: ${eventCount} event(s)`,
|
|
715
|
+
)->Effect.map(_ => Ok("ok"))
|
|
716
|
+
| Error(err) =>
|
|
717
|
+
// Typed classification (was substring-matching the error string,
|
|
718
|
+
// which disagreed with the backends' casing).
|
|
719
|
+
let (errorCode, errDetail) = switch err {
|
|
720
|
+
| ReventlessInfra.DcbEventLog.Conflict => (
|
|
721
|
+
Reventless.CommandOutcome.code(Conflict),
|
|
722
|
+
"conflict: condition check failed",
|
|
723
|
+
)
|
|
724
|
+
| StorageFailure(msg) => (
|
|
725
|
+
Reventless.CommandOutcome.code(AppendFailed),
|
|
726
|
+
msg,
|
|
727
|
+
)
|
|
728
|
+
}
|
|
729
|
+
if retries > 0 {
|
|
730
|
+
// Re-seed from the just-read snapshot so the retry reads only
|
|
731
|
+
// the delta the conflicting writer added, not full history.
|
|
732
|
+
seed := Some((state, headPosition, owners, seen))
|
|
733
|
+
// Per-slice CloudWatch counter: rising AppendRetry signals a
|
|
734
|
+
// slice feeling contention (the metric to watch the eventual
|
|
735
|
+
// default with — see dcb-high-contention-handling.md).
|
|
736
|
+
Metrics.emitCount(~metric="AppendRetry", ~slice=Spec.name)
|
|
737
|
+
EffectLogger.logWarn(
|
|
738
|
+
~comp,
|
|
739
|
+
~ids,
|
|
740
|
+
`append failed (retrying ${(maxRetries - retries + 1)
|
|
741
|
+
->Int.toString}/${maxRetries->Int.toString}): ${errDetail}`,
|
|
742
|
+
)->Effect.flatMap(_ => attempt(~retries=retries - 1))
|
|
743
|
+
} else {
|
|
744
|
+
// Retries exhausted — drop any cached snapshot so the next
|
|
745
|
+
// command for this entity takes the cold full-read path.
|
|
746
|
+
cacheInvalidate()
|
|
747
|
+
|
|
748
|
+
// A surfaced Conflict means the 3-retry loop could not absorb
|
|
749
|
+
// the contention — the operator signal for "this slice is hot"
|
|
750
|
+
// (consider sharding / async — Issue 10, §4 of the analysis).
|
|
751
|
+
if errorCode == Reventless.CommandOutcome.code(Conflict) {
|
|
752
|
+
Metrics.emitCount(~metric="AppendConflict", ~slice=Spec.name)
|
|
753
|
+
}
|
|
754
|
+
// Both arms here are storage, not the model: a surfaced Conflict is
|
|
755
|
+
// contention the retry loop couldn't absorb, and AppendFailed is the
|
|
756
|
+
// store failing. `decide` had already said Ok.
|
|
757
|
+
CommandTopic_Helpers.reportRejected(
|
|
758
|
+
~component=Spec.name,
|
|
759
|
+
~correlationId=cmdJson.meta.correlationId,
|
|
760
|
+
~claimed=cmdJson.meta.callerClaim->Option.isSome,
|
|
761
|
+
~causationId=?cmdJson.meta.causationId,
|
|
762
|
+
~cause=InfrastructureFailure,
|
|
763
|
+
cmdJson.meta.msgId,
|
|
764
|
+
{errorCode, errorDetail: errDetail},
|
|
765
|
+
)
|
|
766
|
+
EffectLogger.logError(
|
|
767
|
+
~comp,
|
|
768
|
+
~ids,
|
|
769
|
+
`append failed, retries exhausted: ${errDetail}`,
|
|
770
|
+
)->Effect.map(_ => Error(errDetail))
|
|
771
|
+
}
|
|
772
|
+
},
|
|
773
|
+
)
|
|
689
774
|
},
|
|
690
775
|
)
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
776
|
+
| Error(error) =>
|
|
777
|
+
// Business-rule rejection — no append, but the read snapshot is valid; cache it.
|
|
778
|
+
cachePut((state, headPosition, owners, seen))
|
|
779
|
+
let errorJson = error->Reventless.Util_Sury.toJson(Spec.errorSchema)
|
|
780
|
+
let errorCode = errorJson->Message.variantNameOfJson
|
|
781
|
+
let (_, payloadDict) = errorJson->Message.splitMessage
|
|
782
|
+
let errorDetail =
|
|
783
|
+
payloadDict->Dict.toArray->Array.length == 0
|
|
784
|
+
? ""
|
|
785
|
+
: payloadDict->JSON.Encode.object->JSON.stringify
|
|
786
|
+
CommandTopic_Helpers.reportRejected(
|
|
787
|
+
~component=Spec.name,
|
|
788
|
+
~correlationId=cmdJson.meta.correlationId,
|
|
789
|
+
~claimed=cmdJson.meta.callerClaim->Option.isSome,
|
|
790
|
+
~causationId=?cmdJson.meta.causationId,
|
|
791
|
+
~cause=DomainRejection,
|
|
792
|
+
cmdJson.meta.msgId,
|
|
793
|
+
{errorCode, errorDetail},
|
|
794
|
+
)
|
|
795
|
+
EffectLogger.logError(
|
|
796
|
+
~comp,
|
|
797
|
+
~ids,
|
|
798
|
+
`decide rejected: ${errorCode} ${errorDetail}`,
|
|
799
|
+
)->Effect.map(_ => Ok("rejected"))
|
|
800
|
+
}
|
|
801
|
+
},
|
|
802
|
+
)
|
|
803
|
+
| missing => indexBehind(missing)
|
|
718
804
|
}
|
|
719
805
|
)
|
|
720
806
|
)
|
|
@@ -728,6 +814,7 @@ module Make = (
|
|
|
728
814
|
let handleCommands = (
|
|
729
815
|
~tagKeysByEventType=Dict.make(),
|
|
730
816
|
~crossPartitionTagKeys=[],
|
|
817
|
+
~crossPartitionPairs=[],
|
|
731
818
|
~partitionTag=?,
|
|
732
819
|
dcbEventLog,
|
|
733
820
|
stream,
|
|
@@ -738,12 +825,15 @@ module Make = (
|
|
|
738
825
|
}
|
|
739
826
|
let payloadTagKeys =
|
|
740
827
|
scopeShape->Reventless.DcbTag.commandPayloadTagKeys(~partitionTag, ~crossPartitionTagKeys)
|
|
828
|
+
let pairEventTypes =
|
|
829
|
+
crossPartitionPairs->Reventless.DcbScopeInference.pairEventTypes(~reader=Spec.name)
|
|
741
830
|
stream
|
|
742
831
|
->Stream.mapEffect(({ReventlessInfra.CommandTopic.reference: reference, command}) =>
|
|
743
832
|
handleSingleCommand(
|
|
744
833
|
~tagKeysByEventType,
|
|
745
834
|
~crossPartitionTagKeys,
|
|
746
835
|
~payloadTagKeys,
|
|
836
|
+
~pairEventTypes,
|
|
747
837
|
~partitionTag,
|
|
748
838
|
dcbEventLog,
|
|
749
839
|
command,
|