@reventlessdev/reventless-core 3.0.0-alpha.310 → 3.0.0-alpha.311
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 +20 -0
- package/package.json +5 -5
- package/src/Message.res +9 -2
- package/src/Message.res.mjs +8 -1
- package/src/admin/Platform_Admin.res.mjs +0 -2
- package/src/components/Aggregate/Aggregate_Callback.res +111 -48
- package/src/components/Aggregate/Aggregate_Callback.res.mjs +107 -49
- package/src/components/Builder_Helpers.res +0 -17
- package/src/components/Builder_Helpers.res.mjs +0 -15
- package/src/components/EventLog/EventLog.res +7 -0
- package/src/components/EventLog/EventLog_Builder.res +2 -0
- package/src/components/EventLog/EventLog_Builder.res.mjs +1 -0
- package/src/components/EventLog/EventLog_Operations.res +30 -0
- package/src/components/EventLog/EventLog_Operations.res.mjs +18 -0
- package/src/components/Extension/Extension.res +0 -2
- package/src/components/Extension/Extension_Builder.res +2 -20
- package/src/components/Extension/Extension_Builder.res.mjs +1 -3
- package/src/components/Extension/Extension_Operations.res +0 -19
- package/src/components/Extension/Extension_Operations.res.mjs +1 -7
- package/src/components/ReadModel/ReadModel_Builder.res +12 -2
- package/src/components/ReadModel/ReadModel_Builder.res.mjs +2 -1
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +72 -35
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +45 -31
- package/src/plugin/component/Plugin_Builder.res +1 -26
- package/src/plugin/component/Plugin_Builder.res.mjs +6 -20
- package/src/plugin/component/Plugin_Helpers.res +0 -37
- package/src/plugin/component/Plugin_Helpers.res.mjs +8 -21
- package/tests/aggregate/AggregateCacheTest.res +6 -0
- package/tests/aggregate/AggregateCacheTest.res.mjs +5 -0
- package/tests/aggregate/AggregateConflictTest.res +6 -0
- package/tests/aggregate/AggregateConflictTest.res.mjs +5 -0
- package/tests/aggregate/AggregateFixtures.res +6 -0
- package/tests/aggregate/AggregateFixtures.res.mjs +5 -0
- package/tests/aggregate/AggregateRepeatTest.res +186 -0
- package/tests/aggregate/AggregateRepeatTest.res.mjs +332 -0
- package/tests/aggregate/AggregateSnapshotTest.res +6 -0
- package/tests/aggregate/AggregateSnapshotTest.res.mjs +12 -0
- package/tests/commandgenerator/OwnerActingTest.res +6 -0
- package/tests/commandgenerator/OwnerActingTest.res.mjs +5 -0
- package/tests/extensionpoint/DerivedMessageIdTest.res +217 -0
- package/tests/extensionpoint/DerivedMessageIdTest.res.mjs +361 -0
- package/tests/logger/LogFormatTest.res +1 -2
- package/tests/logger/LogFormatTest.res.mjs +2 -3
- package/tests/message/MessageTest.res +48 -1
- package/tests/message/MessageTest.res.mjs +56 -2
- package/tests/plugin/pluginDefinitionRequiredScalars.txt +0 -1
|
@@ -33,6 +33,12 @@ type replay<'id, 'event> = 'id => promise<array<'event>>
|
|
|
33
33
|
// `fromSeq` starts the replay at that sequence number (inclusive; default 0) —
|
|
34
34
|
// the delta read after seeding from a snapshot at seqNr = fromSeq.
|
|
35
35
|
type replayStream<'id, 'event> = ('id, ~fromSeq: int=?) => Stream.t<'event, string, unit>
|
|
36
|
+
// The same replay as envelopes, meta included — for a reader that needs to know
|
|
37
|
+
// which command caused each event, not only what happened.
|
|
38
|
+
type replayStream'<'id, 'event> = (
|
|
39
|
+
'id,
|
|
40
|
+
~fromSeq: int=?,
|
|
41
|
+
) => Stream.t<Message.event'<'id, 'event>, string, unit>
|
|
36
42
|
type appendStream<'id, 'event> = (
|
|
37
43
|
int,
|
|
38
44
|
'id,
|
|
@@ -52,6 +58,7 @@ module type T = {
|
|
|
52
58
|
append: append<Spec.Id.t, Message.event'<Spec.Id.t, Spec.event>>,
|
|
53
59
|
replay: replay<Spec.Id.t, Spec.event>,
|
|
54
60
|
replayStream: replayStream<Spec.Id.t, Spec.event>,
|
|
61
|
+
replayStream': replayStream'<Spec.Id.t, Spec.event>,
|
|
55
62
|
appendStream: appendStream<Spec.Id.t, Spec.event>,
|
|
56
63
|
latestSnapshot: latestSnapshot<Spec.Id.t>,
|
|
57
64
|
writeSnapshot: writeSnapshot<Spec.Id.t>,
|
|
@@ -9,6 +9,7 @@ module Make = (
|
|
|
9
9
|
append: EventLog.append<Spec.Id.t, Message.event'<Spec.Id.t, Spec.event>>,
|
|
10
10
|
replay: EventLog.replay<Spec.Id.t, Spec.event>,
|
|
11
11
|
replayStream: EventLog.replayStream<Spec.Id.t, Spec.event>,
|
|
12
|
+
replayStream': EventLog.replayStream'<Spec.Id.t, Spec.event>,
|
|
12
13
|
appendStream: EventLog.appendStream<Spec.Id.t, Spec.event>,
|
|
13
14
|
latestSnapshot: EventLog.latestSnapshot<Spec.Id.t>,
|
|
14
15
|
writeSnapshot: EventLog.writeSnapshot<Spec.Id.t>,
|
|
@@ -58,6 +59,7 @@ module Make = (
|
|
|
58
59
|
append: Ops.append,
|
|
59
60
|
replay: Ops.replay,
|
|
60
61
|
replayStream: Ops.replayStream,
|
|
62
|
+
replayStream': Ops.replayStream',
|
|
61
63
|
appendStream: Ops.appendStream,
|
|
62
64
|
latestSnapshot: Ops.latestSnapshot,
|
|
63
65
|
writeSnapshot: Ops.writeSnapshot,
|
|
@@ -10,6 +10,7 @@ module type T = {
|
|
|
10
10
|
let append: EventLog.append<Spec.Id.t, Message.event'<Spec.Id.t, Spec.event>>
|
|
11
11
|
let replay: EventLog.replay<Spec.Id.t, Spec.event>
|
|
12
12
|
let replayStream: EventLog.replayStream<Spec.Id.t, Spec.event>
|
|
13
|
+
let replayStream': EventLog.replayStream'<Spec.Id.t, Spec.event>
|
|
13
14
|
let appendStream: EventLog.appendStream<Spec.Id.t, Spec.event>
|
|
14
15
|
let latestSnapshot: EventLog.latestSnapshot<Spec.Id.t>
|
|
15
16
|
let writeSnapshot: EventLog.writeSnapshot<Spec.Id.t>
|
|
@@ -199,6 +200,35 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
|
|
|
199
200
|
Effect.sync(() => decodeEvent(id->Spec.Id.toString, json))
|
|
200
201
|
)
|
|
201
202
|
|
|
203
|
+
// The stored item is the flat form `storedEventToFlatJson` writes: meta.* at the
|
|
204
|
+
// top level, reassembled the way `flatJsonToStoredEvent` does.
|
|
205
|
+
let decodeMeta = (id, json) =>
|
|
206
|
+
try {
|
|
207
|
+
json
|
|
208
|
+
->JSON.Decode.object
|
|
209
|
+
->Option.getOrThrow
|
|
210
|
+
->Message.composeMeta
|
|
211
|
+
->Reventless.Util_Sury.fromJson(Message.metaSchema)
|
|
212
|
+
} catch {
|
|
213
|
+
| exn =>
|
|
214
|
+
JsError.throwWithMessage(
|
|
215
|
+
`EventLog.replay: Error: id:${id}: Couldn't decode meta of ${json->JSON.stringify}: ${Reventless.Util_Sury.exnMessage(
|
|
216
|
+
exn,
|
|
217
|
+
)}`,
|
|
218
|
+
)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
let replayStream' = (id, ~fromSeq=?) => {
|
|
222
|
+
let idStr = id->Spec.Id.toString
|
|
223
|
+
Ops.storage.replayStream(idStr, ~fromSeq?)->Stream.mapEffect(json =>
|
|
224
|
+
Effect.sync(() => {
|
|
225
|
+
Reventless.Message.id,
|
|
226
|
+
meta: decodeMeta(idStr, json),
|
|
227
|
+
event: decodeEvent(idStr, json),
|
|
228
|
+
})
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
|
|
202
232
|
// Snapshot passthrough — the state stays JSON at this layer; the consumer
|
|
203
233
|
// (Aggregate_Callback) decodes it against its own state schema and hash-gates
|
|
204
234
|
// staleness, so a drifted snapshot degrades to full replay, never to an error.
|
|
@@ -11,6 +11,7 @@ import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
|
|
|
11
11
|
import * as Duration from "effect/Duration";
|
|
12
12
|
import * as Schedule from "effect/Schedule";
|
|
13
13
|
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
14
|
+
import * as Util_Sury$Reventless from "@reventlessdev/reventless-spec/src/util/Util_Sury.res.mjs";
|
|
14
15
|
import * as Message$ReventlessCore from "../../Message.res.mjs";
|
|
15
16
|
import * as Util_Error$ReventlessCore from "../../util/Util_Error.res.mjs";
|
|
16
17
|
import * as EffectLogger$ReventlessCore from "../../util/EffectLogger.res.mjs";
|
|
@@ -187,6 +188,22 @@ function Make(Spec) {
|
|
|
187
188
|
return eventsJson.map(json => decodeEvent(id$1, json));
|
|
188
189
|
};
|
|
189
190
|
let replayStream = (id, fromSeq) => Stream.mapEffect(Ops.storage.replayStream(Spec.Id.toString(id), fromSeq), json => Effect$1.sync(() => decodeEvent(Spec.Id.toString(id), json)));
|
|
191
|
+
let decodeMeta = (id, json) => {
|
|
192
|
+
try {
|
|
193
|
+
return Util_Sury$Reventless.fromJson(Message$ReventlessCore.composeMeta(Stdlib_Option.getOrThrow(Stdlib_JSON.Decode.object(json), undefined)), Message$ReventlessCore.metaSchema);
|
|
194
|
+
} catch (raw_exn) {
|
|
195
|
+
let exn = Primitive_exceptions.internalToException(raw_exn);
|
|
196
|
+
return Stdlib_JsError.throwWithMessage(`EventLog.replay: Error: id:` + id + `: Couldn't decode meta of ` + JSON.stringify(json) + `: ` + Util_Sury$Reventless.exnMessage(exn, undefined));
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
let replayStream$p = (id, fromSeq) => {
|
|
200
|
+
let idStr = Spec.Id.toString(id);
|
|
201
|
+
return Stream.mapEffect(Ops.storage.replayStream(idStr, fromSeq), json => Effect$1.sync(() => ({
|
|
202
|
+
id: id,
|
|
203
|
+
meta: decodeMeta(idStr, json),
|
|
204
|
+
event: decodeEvent(idStr, json)
|
|
205
|
+
})));
|
|
206
|
+
};
|
|
190
207
|
let latestSnapshot = id => Ops.storage.latestSnapshot(Spec.Id.toString(id));
|
|
191
208
|
let writeSnapshot = (id, snap) => Ops.storage.writeSnapshot(Spec.Id.toString(id), snap);
|
|
192
209
|
let appendStream = (startingSeqNr, id, stream) => Ops.storage.appendStream(startingSeqNr, Spec.Id.toString(id), Stream.map(stream, event => {
|
|
@@ -208,6 +225,7 @@ function Make(Spec) {
|
|
|
208
225
|
append: append,
|
|
209
226
|
replay: replay,
|
|
210
227
|
replayStream: replayStream,
|
|
228
|
+
replayStream$p: replayStream$p,
|
|
211
229
|
appendStream: appendStream,
|
|
212
230
|
latestSnapshot: latestSnapshot,
|
|
213
231
|
writeSnapshot: writeSnapshot
|
|
@@ -17,8 +17,6 @@ module type T = {
|
|
|
17
17
|
let make: (
|
|
18
18
|
~publishToPluginExtensionPoint: CommandTopic.publishJsons,
|
|
19
19
|
~publishToAggregates: dict<CommandTopic.publishJsons>,
|
|
20
|
-
~readModelNamesForSourceName: dict<array<string>>,
|
|
21
|
-
~publishToReadModels: dict<EventCollector.enqueueEvent>,
|
|
22
20
|
~queryEngine: Reventless.QueryEngine.operations,
|
|
23
21
|
~opts: option<Pulumi.ComponentResource.options>,
|
|
24
22
|
) => component
|
|
@@ -8,8 +8,6 @@ module Make = (
|
|
|
8
8
|
let construct = (
|
|
9
9
|
~publishToPluginExtensionPoint: CommandTopic.publishJsons,
|
|
10
10
|
~publishToAggregates: dict<CommandTopic.publishJsons>,
|
|
11
|
-
~readModelNamesForSourceName: dict<array<string>>,
|
|
12
|
-
~publishToReadModels: dict<EventCollector.enqueueEvent>,
|
|
13
11
|
~queryEngine,
|
|
14
12
|
self,
|
|
15
13
|
name,
|
|
@@ -20,8 +18,6 @@ module Make = (
|
|
|
20
18
|
{
|
|
21
19
|
let publishToAggregates = publishToAggregates
|
|
22
20
|
let publishToPluginExtensionPoint = publishToPluginExtensionPoint
|
|
23
|
-
let readModelNamesForSourceName = readModelNamesForSourceName
|
|
24
|
-
let publishToReadModels = publishToReadModels
|
|
25
21
|
let queryEngine = queryEngine
|
|
26
22
|
},
|
|
27
23
|
)
|
|
@@ -55,25 +51,11 @@ module Make = (
|
|
|
55
51
|
self->Component.setOutputs(extOutputs)
|
|
56
52
|
}
|
|
57
53
|
|
|
58
|
-
let make = (
|
|
59
|
-
~publishToPluginExtensionPoint,
|
|
60
|
-
~publishToAggregates,
|
|
61
|
-
~readModelNamesForSourceName,
|
|
62
|
-
~publishToReadModels,
|
|
63
|
-
~queryEngine,
|
|
64
|
-
~opts,
|
|
65
|
-
) =>
|
|
54
|
+
let make = (~publishToPluginExtensionPoint, ~publishToAggregates, ~queryEngine, ~opts) =>
|
|
66
55
|
Component.make(
|
|
67
56
|
~componentType=Extension.componentType->ComponentType.toString,
|
|
68
57
|
~name=Spec.name ++ ("." ++ Mappings.name),
|
|
69
|
-
~construct=construct(
|
|
70
|
-
~publishToPluginExtensionPoint,
|
|
71
|
-
~publishToAggregates,
|
|
72
|
-
~readModelNamesForSourceName,
|
|
73
|
-
~publishToReadModels,
|
|
74
|
-
~queryEngine,
|
|
75
|
-
...
|
|
76
|
-
),
|
|
58
|
+
~construct=construct(~publishToPluginExtensionPoint, ~publishToAggregates, ~queryEngine, ...),
|
|
77
59
|
~opts,
|
|
78
60
|
)
|
|
79
61
|
|
|
@@ -10,15 +10,13 @@ import * as Extension_Operations$ReventlessCore from "./Extension_Operations.res
|
|
|
10
10
|
|
|
11
11
|
function Make(Spec) {
|
|
12
12
|
return Mappings => {
|
|
13
|
-
let make = (publishToPluginExtensionPoint, publishToAggregates,
|
|
13
|
+
let make = (publishToPluginExtensionPoint, publishToAggregates, queryEngine, opts) => Component$ReventlessCore.make(ComponentType$ReventlessCore.toString(Extension$ReventlessCore.componentType), Spec.name + ("." + Mappings.name), (extra, extra$1) => {
|
|
14
14
|
let Operations = Extension_Operations$ReventlessCore.Make(Spec)({
|
|
15
15
|
name: Mappings.name,
|
|
16
16
|
mappings: Mappings.mappings
|
|
17
17
|
})({
|
|
18
18
|
publishToAggregates: publishToAggregates,
|
|
19
19
|
publishToPluginExtensionPoint: publishToPluginExtensionPoint,
|
|
20
|
-
readModelNamesForSourceName: readModelNamesForSourceName,
|
|
21
|
-
publishToReadModels: publishToReadModels,
|
|
22
20
|
queryEngine: queryEngine
|
|
23
21
|
});
|
|
24
22
|
let operations_incomingJsonEventsHandler = Operations.incomingJsonEventsHandler;
|
|
@@ -11,8 +11,6 @@ module type Mappings = {
|
|
|
11
11
|
module type Ops = {
|
|
12
12
|
let publishToAggregates: dict<CommandTopic.publishJsons>
|
|
13
13
|
let publishToPluginExtensionPoint: CommandTopic.publishJsons
|
|
14
|
-
let readModelNamesForSourceName: dict<array<string>>
|
|
15
|
-
let publishToReadModels: dict<EventCollector.enqueueEvent>
|
|
16
14
|
let queryEngine: Reventless.QueryEngine.operations
|
|
17
15
|
}
|
|
18
16
|
|
|
@@ -171,23 +169,6 @@ module Make = (
|
|
|
171
169
|
->Util.Promise.toUnit
|
|
172
170
|
}
|
|
173
171
|
await commandActions->apply
|
|
174
|
-
|
|
175
|
-
switch Ops.readModelNamesForSourceName
|
|
176
|
-
->Dict.get(event'.meta.service)
|
|
177
|
-
->Option.map(readModelNames =>
|
|
178
|
-
readModelNames
|
|
179
|
-
->Array.filterMap(readModelName =>
|
|
180
|
-
Ops.publishToReadModels
|
|
181
|
-
->Dict.get(readModelName)
|
|
182
|
-
->Option.map(enqueueEvent => enqueueEvent(0, event'.id, eventJson'->JSON.stringify))
|
|
183
|
-
) // FIXME Error handling
|
|
184
|
-
->Promise.all
|
|
185
|
-
) {
|
|
186
|
-
| Some(p) =>
|
|
187
|
-
let _ = await p
|
|
188
|
-
| None => ()
|
|
189
|
-
}
|
|
190
|
-
|
|
191
172
|
| exception err =>
|
|
192
173
|
let errMsg = err->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("unknown")
|
|
193
174
|
EffectLogger.logError(
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
|
|
3
|
-
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
|
|
4
3
|
import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
|
|
5
4
|
import * as Id$Reventless from "@reventlessdev/reventless-spec/src/types/Id.res.mjs";
|
|
6
5
|
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
@@ -123,12 +122,7 @@ function Make(MappingSpec) {
|
|
|
123
122
|
}
|
|
124
123
|
let commandActions = mapIncomingEvent(event$p, pluginDef, Ops.queryEngine);
|
|
125
124
|
let apply = async commandActions => await Util_Promise$ReventlessCore.toUnit(Promise.all(commandActions.map(applyIncomingCommandAction)));
|
|
126
|
-
await apply(commandActions);
|
|
127
|
-
let p = Stdlib_Option.map(Ops.readModelNamesForSourceName[event$p.meta.service], readModelNames => Promise.all(Stdlib_Array.filterMap(readModelNames, readModelName => Stdlib_Option.map(Ops.publishToReadModels[readModelName], enqueueEvent => enqueueEvent(0, event$p.id, JSON.stringify(eventJson$p))))));
|
|
128
|
-
if (p !== undefined) {
|
|
129
|
-
await p;
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
125
|
+
return await apply(commandActions);
|
|
132
126
|
};
|
|
133
127
|
let outgoingJsonEventsHandler = (eventJson$p, pluginDef) => {
|
|
134
128
|
Effect.runSync(EffectLogger$ReventlessCore.logInfo(comp, undefined, `outgoing delegate event: ` + LogFormat$ReventlessCore.eventSummary(eventJson$p)));
|
|
@@ -93,18 +93,28 @@ module Make = (
|
|
|
93
93
|
// Fail-fast: every Mapping.sourceName must resolve to a topic in allEventTopics,
|
|
94
94
|
// otherwise EventTopic.filter silently drops it and the projection runs on no events.
|
|
95
95
|
// Catches both Aggregate-name typos and DCB-source-name typos (e.g. "FooDcb" instead
|
|
96
|
-
// of "FooDcbEventLog").
|
|
96
|
+
// of "FooDcbEventLog"). A dotted name is an extension point's: another plugin's
|
|
97
|
+
// facts reach a read model only through an Extension and a slice of this plugin.
|
|
97
98
|
sourceNames
|
|
98
99
|
->Belt.Set.String.toArray
|
|
99
100
|
->Array.forEach(sourceName =>
|
|
100
101
|
if !(allEventTopics->Dict.has(sourceName)) {
|
|
101
102
|
let availableNames =
|
|
102
103
|
allEventTopics->Dict.keysToArray->Array.toSorted(String.compare)->Array.join(", ")
|
|
104
|
+
let hint =
|
|
105
|
+
sourceName->String.includes(".")
|
|
106
|
+
? ` "${sourceName}" looks like an extension point name: a read model cannot ` ++
|
|
107
|
+
`hear another plugin's extension point directly. Declare an Extension that ` ++
|
|
108
|
+
`maps its events to a command of one of this plugin's slices or aggregates, ` ++
|
|
109
|
+
`and project that component's events (see the Extension doc, ` ++ `"Targeting a StateChangeSlice instead of an Aggregate").`
|
|
110
|
+
: ""
|
|
103
111
|
JsError.throwWithMessage(
|
|
104
112
|
`ReadModel "${Spec.name}" has a Mapping with sourceName "${sourceName}", ` ++
|
|
105
113
|
`but no EventTopic with that key exists in allEventTopics. ` ++
|
|
106
114
|
`Available source names: [${availableNames}]. ` ++
|
|
107
|
-
`Check Mapping.Make's first arg matches an Aggregate Spec.name or a DCB ` ++
|
|
115
|
+
`Check Mapping.Make's first arg matches an Aggregate Spec.name or a DCB ` ++
|
|
116
|
+
`source name (typically "<pluginName>DcbEventLog").` ++
|
|
117
|
+
hint,
|
|
108
118
|
)
|
|
109
119
|
}
|
|
110
120
|
)
|
|
@@ -62,7 +62,8 @@ function Make(Spec) {
|
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
64
|
let availableNames = Object.keys(allEventTopics).toSorted(Primitive_string.compare).join(", ");
|
|
65
|
-
|
|
65
|
+
let hint = sourceName.includes(".") ? ` "` + sourceName + `" looks like an extension point name: a read model cannot hear another plugin's extension point directly. Declare an Extension that maps its events to a command of one of this plugin's slices or aggregates, and project that component's events (see the Extension doc, "Targeting a StateChangeSlice instead of an Aggregate").` : "";
|
|
66
|
+
Stdlib_JsError.throwWithMessage(`ReadModel "` + Spec.name + `" has a Mapping with sourceName "` + sourceName + `", but no EventTopic with that key exists in allEventTopics. ` + (`Available source names: [` + availableNames + `]. `) + `Check Mapping.Make's first arg matches an Aggregate Spec.name or a DCB source name (typically "<pluginName>DcbEventLog").` + hint);
|
|
66
67
|
});
|
|
67
68
|
let SpecificEventCollector = EventCollector_Builder$ReventlessCore.Make(RuntimeEnvironment)(EventCollectorChannel);
|
|
68
69
|
let eventCollector = Component$ReventlessCore.operations(queryDb).apply(operations => {
|
|
@@ -31,6 +31,20 @@ module Make = (
|
|
|
31
31
|
let decoder = Reventless.DcbDecode.makeDecoder(Spec.consumedEventSchema)
|
|
32
32
|
let queryEventTypes = decoder.eventTypes
|
|
33
33
|
|
|
34
|
+
// For a command with a stable id the read also covers the slice's own produced
|
|
35
|
+
// types: an event among them whose `causationId` is this command's `msgId` says
|
|
36
|
+
// the command was handled already. Reading them through the same query, rather
|
|
37
|
+
// than a tag of their own, keeps every event's tag set — and so the
|
|
38
|
+
// `tag_composite` key a composite read depends on — exactly what the model
|
|
39
|
+
// declares; it also puts the produced types under the append condition, so two
|
|
40
|
+
// concurrent copies cannot both append. The produced types not consumed are
|
|
41
|
+
// dropped by the decoder and never reach `evolve`.
|
|
42
|
+
let producedEventTypes = Reventless.DcbTag.extractAllVariantNames(Spec.eventSchema)
|
|
43
|
+
let queryEventTypesWithOwn = Array.concat(
|
|
44
|
+
queryEventTypes,
|
|
45
|
+
producedEventTypes->Array.filter(t => !(queryEventTypes->Array.includes(t))),
|
|
46
|
+
)
|
|
47
|
+
|
|
34
48
|
let encodeEvent = (
|
|
35
49
|
~parentMeta: Message.meta,
|
|
36
50
|
~tagKeysByEventType: Dict.t<array<string>>,
|
|
@@ -199,8 +213,11 @@ module Make = (
|
|
|
199
213
|
`handling command: ${cmdJson->LogFormat.cmdDetailNoId}`,
|
|
200
214
|
)->Effect.runSync
|
|
201
215
|
|
|
216
|
+
// A command an Extension derived carries a stable id: the read then also asks
|
|
217
|
+
// whether this very command already left events (see `queryEventTypesWithOwn`).
|
|
218
|
+
let asksForRepeat = Message.hasStableMsgId(command'.meta)
|
|
202
219
|
let query = Reventless.DcbTag.buildQueryFromCommand(
|
|
203
|
-
~eventTypes=queryEventTypes,
|
|
220
|
+
~eventTypes=asksForRepeat ? queryEventTypesWithOwn : queryEventTypes,
|
|
204
221
|
~schema=Spec.commandSchema,
|
|
205
222
|
~value=command'.command,
|
|
206
223
|
~tagKeysByEventType,
|
|
@@ -265,7 +282,12 @@ module Make = (
|
|
|
265
282
|
// one shared cache slot, so one slice's folded state could seed another's —
|
|
266
283
|
// a silent correctness bug. `None` simply bypasses the cache (cold read, no
|
|
267
284
|
// write) for that operation.
|
|
268
|
-
|
|
285
|
+
//
|
|
286
|
+
// A command asking for a repeat bypasses the cache too: a cached head sits
|
|
287
|
+
// past the events an earlier delivery left, so a delta read could not see
|
|
288
|
+
// them. Such a command pays a full read of its partition for the guarantee
|
|
289
|
+
// that a second delivery changes nothing.
|
|
290
|
+
let cacheKey = asksForRepeat ? None : query->JSON.stringifyAny
|
|
269
291
|
let cacheGet = () =>
|
|
270
292
|
switch cacheKey {
|
|
271
293
|
| Some(k) => Lru.get(projectionCache, k)
|
|
@@ -323,41 +345,38 @@ module Make = (
|
|
|
323
345
|
}
|
|
324
346
|
|
|
325
347
|
dcbEventLog.readStream(~query, ~after=?afterPos, ~strongConsistency)
|
|
326
|
-
->Stream.map(raw => {
|
|
327
|
-
let data = raw.data->JSON.Decode.object->Option.getOr(Dict.make())
|
|
328
|
-
let decoded = decoder.decode(~eventType=raw.eventType, ~data)
|
|
329
|
-
decoded->Option.map(event => (
|
|
330
|
-
event,
|
|
331
|
-
raw.position,
|
|
332
|
-
raw.eventType,
|
|
333
|
-
readEventId(~partitionTag, raw.tags),
|
|
334
|
-
(ownerFieldsOf(raw), data),
|
|
335
|
-
))
|
|
336
|
-
})
|
|
337
|
-
->Stream.flatMap(opt =>
|
|
338
|
-
switch opt {
|
|
339
|
-
| Some(v) => Stream.fromIterable([v])
|
|
340
|
-
| None => Stream.empty
|
|
341
|
-
}
|
|
342
|
-
)
|
|
343
348
|
// The head accumulator starts at `afterPos` (the seeded head), not `None`,
|
|
344
349
|
// so an empty delta read keeps the cached head — the append must condition
|
|
345
350
|
// on the head that actually exists, not assert "no events exist yet".
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
351
|
+
// Every read event moves the head and is checked as this command's own
|
|
352
|
+
// doing before it is decoded; only the consumed ones reach `evolve`.
|
|
353
|
+
->Stream.runFold((baseState, afterPos, [], baseOwners, false), (
|
|
354
|
+
(dm, _pos, reads, owners, repeated),
|
|
355
|
+
raw,
|
|
356
|
+
) => {
|
|
357
|
+
// Only a command with a stable id asks; a fresh id never matches a stored
|
|
358
|
+
// cause, and asking would only make the answer depend on what the slice
|
|
359
|
+
// happens to read.
|
|
360
|
+
let repeated =
|
|
361
|
+
repeated || (asksForRepeat && raw.meta.causationId == Some(command'.meta.msgId))
|
|
362
|
+
let data = raw.data->JSON.Decode.object->Option.getOr(Dict.make())
|
|
363
|
+
switch decoder.decode(~eventType=raw.eventType, ~data) {
|
|
364
|
+
| None => (dm, Some(raw.position), reads, owners, repeated)
|
|
365
|
+
| Some(event) => (
|
|
366
|
+
Decision.evolve(dm, event),
|
|
367
|
+
Some(raw.position),
|
|
368
|
+
reads->Array.concat([
|
|
369
|
+
switch readEventId(~partitionTag, raw.tags) {
|
|
370
|
+
| Some(id) => `${raw.eventType->LogFormat.bold}(${id})`
|
|
371
|
+
| None => raw.eventType->LogFormat.bold
|
|
372
|
+
},
|
|
373
|
+
]),
|
|
374
|
+
owners->Reventless.OwnerScope.recordOwners(~fields=ownerFieldsOf(raw), data),
|
|
375
|
+
repeated,
|
|
376
|
+
)
|
|
377
|
+
}
|
|
378
|
+
})
|
|
379
|
+
->Effect.tap(((_, _, reads, _, _)) => {
|
|
361
380
|
// On a warm first attempt, record how many events the delta read
|
|
362
381
|
// returned — near-zero on a healthy hit; a large value means the entity
|
|
363
382
|
// churned between commands, so the cache saved little. Gated to the first
|
|
@@ -380,7 +399,7 @@ module Make = (
|
|
|
380
399
|
: ` [${reads->Array.join(", ")}]`}`,
|
|
381
400
|
)
|
|
382
401
|
})
|
|
383
|
-
->Effect.flatMap(((state, headPosition, _, owners)) =>
|
|
402
|
+
->Effect.flatMap(((state, headPosition, _, owners, repeated)) =>
|
|
384
403
|
// Identity, not content: the decision model folds the whole matched
|
|
385
404
|
// history, so serialising it here makes the line's size a function of
|
|
386
405
|
// how long the entity has been in use.
|
|
@@ -391,6 +410,24 @@ module Make = (
|
|
|
391
410
|
)} cmd=${cmdJson->LogFormat.cmdName}`,
|
|
392
411
|
)->Effect.flatMap(_ =>
|
|
393
412
|
switch ownershipRefusal(owners) {
|
|
413
|
+
| _ if repeated =>
|
|
414
|
+
// Before `decide`, and before the ownership check it passed the first
|
|
415
|
+
// time: the partition already holds events this command caused, so it
|
|
416
|
+
// was delivered again. Accepted, nothing new.
|
|
417
|
+
CommandTopic_Helpers.reportAccepted(
|
|
418
|
+
~component=Spec.name,
|
|
419
|
+
cmdJson.meta.msgId,
|
|
420
|
+
switch entityId {
|
|
421
|
+
| Some(eid) => {entityId: eid, eventCount: 0}
|
|
422
|
+
| None => {eventCount: 0}
|
|
423
|
+
},
|
|
424
|
+
)
|
|
425
|
+
EffectLogger.logInfo(
|
|
426
|
+
~comp,
|
|
427
|
+
`repeat dropped: ${cmdJson->LogFormat.cmdName} msgId=${cmdJson.meta.msgId} already caused events in id=${entityId->Option.getOr(
|
|
428
|
+
"-",
|
|
429
|
+
)}`,
|
|
430
|
+
)->Effect.map(_ => Ok("ok"))
|
|
394
431
|
| Some({rejected, isDefect}) =>
|
|
395
432
|
// Refused before `decide`; the read snapshot is still valid.
|
|
396
433
|
cachePut((state, headPosition, owners))
|
|
@@ -9,6 +9,8 @@ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
|
9
9
|
import * as Effect from "effect/Effect";
|
|
10
10
|
import * as Stream$1 from "effect/Stream";
|
|
11
11
|
import * as Owner$Reventless from "@reventlessdev/reventless-spec/src/components/Owner.res.mjs";
|
|
12
|
+
import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
|
|
13
|
+
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
|
|
12
14
|
import * as DcbTag$Reventless from "@reventlessdev/reventless-spec/src/components/DcbTag.res.mjs";
|
|
13
15
|
import * as Lru$ReventlessCore from "../../util/Lru.res.mjs";
|
|
14
16
|
import * as DcbDecode$Reventless from "@reventlessdev/reventless-spec/src/components/DcbDecode.res.mjs";
|
|
@@ -26,6 +28,8 @@ function Make(Spec) {
|
|
|
26
28
|
let comp = `StateChangeSlice(` + Spec.name + `)`;
|
|
27
29
|
let decoder = DcbDecode$Reventless.makeDecoder(Spec.consumedEventSchema);
|
|
28
30
|
let queryEventTypes = decoder.eventTypes;
|
|
31
|
+
let producedEventTypes = DcbTag$Reventless.extractAllVariantNames(Spec.eventSchema);
|
|
32
|
+
let queryEventTypesWithOwn = queryEventTypes.concat(producedEventTypes.filter(t => !queryEventTypes.includes(t)));
|
|
29
33
|
let slicePartitionTag = Stdlib_Lazy.make(() => {
|
|
30
34
|
try {
|
|
31
35
|
return DcbTag$Reventless.deriveSlicePartition({
|
|
@@ -79,7 +83,8 @@ function Make(Spec) {
|
|
|
79
83
|
let handleSingleCommand = (tagKeysByEventType, crossPartitionTagKeys, payloadTagKeys, partitionTag, dcbEventLog, command$p) => {
|
|
80
84
|
let cmdJson = Message$ReventlessCore.commandJsonOfCommand$p(Id$Reventless.$$String.toString, Spec.commandSchema, command$p);
|
|
81
85
|
Effect.runSync(EffectLogger$ReventlessCore.logInfo(comp, cmdJson.commandJson, `handling command: ` + LogFormat$ReventlessCore.cmdDetailNoId(cmdJson)));
|
|
82
|
-
let
|
|
86
|
+
let asksForRepeat = Message$ReventlessCore.hasStableMsgId(command$p.meta);
|
|
87
|
+
let query = DcbTag$Reventless.buildQueryFromCommand(asksForRepeat ? queryEventTypesWithOwn : queryEventTypes, Spec.commandSchema, command$p.command, tagKeysByEventType, crossPartitionTagKeys, payloadTagKeys);
|
|
83
88
|
let queryDetail = query.map(qi => {
|
|
84
89
|
let ts = qi.eventTypes;
|
|
85
90
|
let types = ts !== undefined ? ts.map(LogFormat$ReventlessCore.bold).join("|") : "*";
|
|
@@ -129,7 +134,7 @@ function Make(Spec) {
|
|
|
129
134
|
return CommandTopic_Helpers$ReventlessCore.ownershipRefusal(OwnerScope$Reventless.decideActing(command$p.meta.callerClaim, Stdlib_Option.map(entityId, param => owners)), Spec.name, Message$ReventlessCore.variantNameOfJson(cmdJson.commandJson));
|
|
130
135
|
}
|
|
131
136
|
};
|
|
132
|
-
let cacheKey = JSON.stringify(query);
|
|
137
|
+
let cacheKey = asksForRepeat ? undefined : JSON.stringify(query);
|
|
133
138
|
let cacheGet = () => {
|
|
134
139
|
if (cacheKey !== undefined) {
|
|
135
140
|
return Lru$ReventlessCore.get(projectionCache, cacheKey);
|
|
@@ -174,39 +179,35 @@ function Make(Spec) {
|
|
|
174
179
|
strongConsistency = false;
|
|
175
180
|
break;
|
|
176
181
|
}
|
|
177
|
-
return Effect.flatMap(Effect.tap(Stream$1.runFold(
|
|
178
|
-
let data = Stdlib_Option.getOr(Stdlib_JSON.Decode.object(raw.data), {});
|
|
179
|
-
let decoded = decoder.decode(raw.eventType, data);
|
|
180
|
-
return Stdlib_Option.map(decoded, event => [
|
|
181
|
-
event,
|
|
182
|
-
raw.position,
|
|
183
|
-
raw.eventType,
|
|
184
|
-
readEventId(partitionTag, raw.tags),
|
|
185
|
-
[
|
|
186
|
-
ownerFieldsOf(raw),
|
|
187
|
-
data
|
|
188
|
-
]
|
|
189
|
-
]);
|
|
190
|
-
}), opt => {
|
|
191
|
-
if (opt !== undefined) {
|
|
192
|
-
return Stream$1.fromIterable([opt]);
|
|
193
|
-
} else {
|
|
194
|
-
return Stream$1.empty;
|
|
195
|
-
}
|
|
196
|
-
}), [
|
|
182
|
+
return Effect.flatMap(Effect.tap(Stream$1.runFold(dcbEventLog.readStream(query, afterPos, strongConsistency), [
|
|
197
183
|
match$1[0],
|
|
198
184
|
afterPos,
|
|
199
185
|
[],
|
|
200
|
-
match$1[2]
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
let
|
|
204
|
-
let
|
|
186
|
+
match$1[2],
|
|
187
|
+
false
|
|
188
|
+
], (param, raw) => {
|
|
189
|
+
let owners = param[3];
|
|
190
|
+
let reads = param[2];
|
|
191
|
+
let dm = param[0];
|
|
192
|
+
let repeated = param[4] || asksForRepeat && Primitive_object.equal(raw.meta.causationId, command$p.meta.msgId);
|
|
193
|
+
let data = Stdlib_Option.getOr(Stdlib_JSON.Decode.object(raw.data), {});
|
|
194
|
+
let event = decoder.decode(raw.eventType, data);
|
|
195
|
+
if (event === undefined) {
|
|
196
|
+
return [
|
|
197
|
+
dm,
|
|
198
|
+
raw.position,
|
|
199
|
+
reads,
|
|
200
|
+
owners,
|
|
201
|
+
repeated
|
|
202
|
+
];
|
|
203
|
+
}
|
|
204
|
+
let id = readEventId(partitionTag, raw.tags);
|
|
205
205
|
return [
|
|
206
|
-
Decision.evolve(
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
OwnerScope$Reventless.recordOwners(
|
|
206
|
+
Decision.evolve(dm, Primitive_option.valFromOption(event)),
|
|
207
|
+
raw.position,
|
|
208
|
+
reads.concat([id !== undefined ? LogFormat$ReventlessCore.bold(raw.eventType) + `(` + id + `)` : LogFormat$ReventlessCore.bold(raw.eventType)]),
|
|
209
|
+
OwnerScope$Reventless.recordOwners(owners, ownerFieldsOf(raw), data),
|
|
210
|
+
repeated
|
|
210
211
|
];
|
|
211
212
|
}), param => {
|
|
212
213
|
let reads = param[2];
|
|
@@ -219,11 +220,24 @@ function Make(Spec) {
|
|
|
219
220
|
reads.length === 0 ? "" : ` [` + reads.join(", ") + `]`
|
|
220
221
|
));
|
|
221
222
|
}), param => {
|
|
223
|
+
let repeated = param[4];
|
|
222
224
|
let owners = param[3];
|
|
223
225
|
let headPosition = param[1];
|
|
224
226
|
let state = param[0];
|
|
225
227
|
return Effect.flatMap(EffectLogger$ReventlessCore.logDebug(comp, undefined, `deciding: id=` + Stdlib_Option.getOr(entityId, "-") + ` head=` + Stdlib_Option.getOr(headPosition, "-") + ` cmd=` + LogFormat$ReventlessCore.cmdName(cmdJson)), () => {
|
|
226
228
|
let match = ownershipRefusal(owners);
|
|
229
|
+
if (repeated) {
|
|
230
|
+
CommandTopic_Helpers$ReventlessCore.reportAccepted(Spec.name, cmdJson.meta.msgId, entityId !== undefined ? ({
|
|
231
|
+
entityId: entityId,
|
|
232
|
+
eventCount: 0
|
|
233
|
+
}) : ({
|
|
234
|
+
eventCount: 0
|
|
235
|
+
}));
|
|
236
|
+
return Effect.map(EffectLogger$ReventlessCore.logInfo(comp, undefined, `repeat dropped: ` + LogFormat$ReventlessCore.cmdName(cmdJson) + ` msgId=` + cmdJson.meta.msgId + ` already caused events in id=` + Stdlib_Option.getOr(entityId, "-")), () => ({
|
|
237
|
+
TAG: "Ok",
|
|
238
|
+
_0: "ok"
|
|
239
|
+
}));
|
|
240
|
+
}
|
|
227
241
|
if (match !== undefined) {
|
|
228
242
|
let rejected = match.rejected;
|
|
229
243
|
cachePut([
|