@cratis/chronicle 6.56.0 → 6.58.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/EventStore.ts +2 -2
- package/WellKnownTelemetryNames.ts +2 -0
- package/auditing/CausationType.ts +5 -0
- package/dist/EventStore.js +2 -2
- package/dist/EventStore.js.map +1 -1
- package/dist/WellKnownTelemetryNames.d.ts +2 -0
- package/dist/WellKnownTelemetryNames.d.ts.map +1 -1
- package/dist/WellKnownTelemetryNames.js +2 -0
- package/dist/WellKnownTelemetryNames.js.map +1 -1
- package/dist/auditing/CausationType.d.ts +4 -0
- package/dist/auditing/CausationType.d.ts.map +1 -1
- package/dist/auditing/CausationType.js +4 -0
- package/dist/auditing/CausationType.js.map +1 -1
- package/dist/eventSequences/CompensateOptions.d.ts +7 -0
- package/dist/eventSequences/CompensateOptions.d.ts.map +1 -0
- package/dist/eventSequences/CompensateOptions.js +4 -0
- package/dist/eventSequences/CompensateOptions.js.map +1 -0
- package/dist/eventSequences/EventSequence.d.ts +3 -0
- package/dist/eventSequences/EventSequence.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.js +45 -0
- package/dist/eventSequences/EventSequence.js.map +1 -1
- package/dist/eventSequences/IEventSequence.d.ts +13 -0
- package/dist/eventSequences/IEventSequence.d.ts.map +1 -1
- package/dist/eventSequences/index.d.ts +1 -0
- package/dist/eventSequences/index.d.ts.map +1 -1
- package/dist/eventSequences/index.js.map +1 -1
- package/dist/events/EventContext.d.ts +5 -0
- package/dist/events/EventContext.d.ts.map +1 -1
- package/dist/events/compensationForDecorator.d.ts +32 -0
- package/dist/events/compensationForDecorator.d.ts.map +1 -0
- package/dist/events/compensationForDecorator.js +44 -0
- package/dist/events/compensationForDecorator.js.map +1 -0
- package/dist/events/eventTypeDecorator.d.ts +7 -0
- package/dist/events/eventTypeDecorator.d.ts.map +1 -1
- package/dist/events/eventTypeDecorator.js +13 -4
- package/dist/events/eventTypeDecorator.js.map +1 -1
- package/dist/events/index.d.ts +2 -1
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +2 -1
- package/dist/events/index.js.map +1 -1
- package/dist/events/toClientEventContext.d.ts.map +1 -1
- package/dist/events/toClientEventContext.js +20 -0
- package/dist/events/toClientEventContext.js.map +1 -1
- package/dist/observation/batches/BatchContext.d.ts +43 -0
- package/dist/observation/batches/BatchContext.d.ts.map +1 -0
- package/dist/observation/batches/BatchContext.js +60 -0
- package/dist/observation/batches/BatchContext.js.map +1 -0
- package/dist/observation/batches/BatchContextObserver.d.ts +14 -0
- package/dist/observation/batches/BatchContextObserver.d.ts.map +1 -0
- package/dist/observation/batches/BatchContextObserver.js +4 -0
- package/dist/observation/batches/BatchContextObserver.js.map +1 -0
- package/dist/observation/batches/BatchContextValues.d.ts +13 -0
- package/dist/observation/batches/BatchContextValues.d.ts.map +1 -0
- package/dist/observation/batches/BatchContextValues.js +4 -0
- package/dist/observation/batches/BatchContextValues.js.map +1 -0
- package/dist/observation/batches/BatchMethod.d.ts +9 -0
- package/dist/observation/batches/BatchMethod.d.ts.map +1 -0
- package/dist/observation/batches/BatchMethod.js +4 -0
- package/dist/observation/batches/BatchMethod.js.map +1 -0
- package/dist/observation/batches/BatchMethodReturnedAValue.d.ts +12 -0
- package/dist/observation/batches/BatchMethodReturnedAValue.d.ts.map +1 -0
- package/dist/observation/batches/BatchMethodReturnedAValue.js +20 -0
- package/dist/observation/batches/BatchMethodReturnedAValue.js.map +1 -0
- package/dist/observation/batches/BatchMethods.d.ts +31 -0
- package/dist/observation/batches/BatchMethods.d.ts.map +1 -0
- package/dist/observation/batches/BatchMethods.js +81 -0
- package/dist/observation/batches/BatchMethods.js.map +1 -0
- package/dist/observation/batches/BatchOutcome.d.ts +15 -0
- package/dist/observation/batches/BatchOutcome.d.ts.map +1 -0
- package/dist/observation/batches/BatchOutcome.js +4 -0
- package/dist/observation/batches/BatchOutcome.js.map +1 -0
- package/dist/observation/batches/BatchOutcomeKind.d.ts +8 -0
- package/dist/observation/batches/BatchOutcomeKind.d.ts.map +1 -0
- package/dist/observation/batches/BatchOutcomeKind.js +11 -0
- package/dist/observation/batches/BatchOutcomeKind.js.map +1 -0
- package/dist/observation/batches/DeferralNotSupportedByKernel.d.ts +13 -0
- package/dist/observation/batches/DeferralNotSupportedByKernel.d.ts.map +1 -0
- package/dist/observation/batches/DeferralNotSupportedByKernel.js +19 -0
- package/dist/observation/batches/DeferralNotSupportedByKernel.js.map +1 -0
- package/dist/observation/batches/DeliveredPartition.d.ts +11 -0
- package/dist/observation/batches/DeliveredPartition.d.ts.map +1 -0
- package/dist/observation/batches/DeliveredPartition.js +4 -0
- package/dist/observation/batches/DeliveredPartition.js.map +1 -0
- package/dist/observation/batches/EventAndContext.d.ts +28 -0
- package/dist/observation/batches/EventAndContext.d.ts.map +1 -0
- package/dist/observation/batches/EventAndContext.js +35 -0
- package/dist/observation/batches/EventAndContext.js.map +1 -0
- package/dist/observation/batches/EventBatch.d.ts +52 -0
- package/dist/observation/batches/EventBatch.d.ts.map +1 -0
- package/dist/observation/batches/EventBatch.js +92 -0
- package/dist/observation/batches/EventBatch.js.map +1 -0
- package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.d.ts +14 -0
- package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.d.ts.map +1 -0
- package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.js +23 -0
- package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.js.map +1 -0
- package/dist/observation/batches/InvalidBatchMethod.d.ts +13 -0
- package/dist/observation/batches/InvalidBatchMethod.d.ts.map +1 -0
- package/dist/observation/batches/InvalidBatchMethod.js +20 -0
- package/dist/observation/batches/InvalidBatchMethod.js.map +1 -0
- package/dist/observation/batches/ObservationOrigin.d.ts +21 -0
- package/dist/observation/batches/ObservationOrigin.d.ts.map +1 -0
- package/dist/observation/batches/ObservationOrigin.js +32 -0
- package/dist/observation/batches/ObservationOrigin.js.map +1 -0
- package/dist/observation/batches/ObservedEventType.d.ts +11 -0
- package/dist/observation/batches/ObservedEventType.d.ts.map +1 -0
- package/dist/observation/batches/ObservedEventType.js +4 -0
- package/dist/observation/batches/ObservedEventType.js.map +1 -0
- package/dist/observation/batches/PageMessages.d.ts +22 -0
- package/dist/observation/batches/PageMessages.d.ts.map +1 -0
- package/dist/observation/batches/PageMessages.js +32 -0
- package/dist/observation/batches/PageMessages.js.map +1 -0
- package/dist/observation/batches/PartitionFailedInBatch.d.ts +10 -0
- package/dist/observation/batches/PartitionFailedInBatch.d.ts.map +1 -0
- package/dist/observation/batches/PartitionFailedInBatch.js +16 -0
- package/dist/observation/batches/PartitionFailedInBatch.js.map +1 -0
- package/dist/observation/batches/PartitionNotInBatch.d.ts +10 -0
- package/dist/observation/batches/PartitionNotInBatch.d.ts.map +1 -0
- package/dist/observation/batches/PartitionNotInBatch.js +16 -0
- package/dist/observation/batches/PartitionNotInBatch.js.map +1 -0
- package/dist/observation/batches/PartitionOutcome.d.ts +19 -0
- package/dist/observation/batches/PartitionOutcome.d.ts.map +1 -0
- package/dist/observation/batches/PartitionOutcome.js +4 -0
- package/dist/observation/batches/PartitionOutcome.js.map +1 -0
- package/dist/observation/batches/PartitionProblem.d.ts +17 -0
- package/dist/observation/batches/PartitionProblem.d.ts.map +1 -0
- package/dist/observation/batches/PartitionProblem.js +4 -0
- package/dist/observation/batches/PartitionProblem.js.map +1 -0
- package/dist/observation/batches/PartitionSettlement.d.ts +29 -0
- package/dist/observation/batches/PartitionSettlement.d.ts.map +1 -0
- package/dist/observation/batches/PartitionSettlement.js +64 -0
- package/dist/observation/batches/PartitionSettlement.js.map +1 -0
- package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.d.ts +12 -0
- package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.d.ts.map +1 -0
- package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.js +20 -0
- package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.js.map +1 -0
- package/dist/observation/batches/batchSize.d.ts +42 -0
- package/dist/observation/batches/batchSize.d.ts.map +1 -0
- package/dist/observation/batches/batchSize.js +55 -0
- package/dist/observation/batches/batchSize.js.map +1 -0
- package/dist/observation/batches/createBatchContext.d.ts +13 -0
- package/dist/observation/batches/createBatchContext.d.ts.map +1 -0
- package/dist/observation/batches/createBatchContext.js +27 -0
- package/dist/observation/batches/createBatchContext.js.map +1 -0
- package/dist/observation/batches/observes.d.ts +39 -0
- package/dist/observation/batches/observes.d.ts.map +1 -0
- package/dist/observation/batches/observes.js +63 -0
- package/dist/observation/batches/observes.js.map +1 -0
- package/dist/observation/batches/runConcurrently.d.ts +9 -0
- package/dist/observation/batches/runConcurrently.d.ts.map +1 -0
- package/dist/observation/batches/runConcurrently.js +22 -0
- package/dist/observation/batches/runConcurrently.js.map +1 -0
- package/dist/observation/createHandlerInstanceValidator.d.ts +1 -1
- package/dist/observation/createHandlerInstanceValidator.d.ts.map +1 -1
- package/dist/observation/createHandlerInstanceValidator.js +5 -7
- package/dist/observation/createHandlerInstanceValidator.js.map +1 -1
- package/dist/observation/getInstanceMethods.d.ts +3 -1
- package/dist/observation/getInstanceMethods.d.ts.map +1 -1
- package/dist/observation/getInstanceMethods.js +13 -4
- package/dist/observation/getInstanceMethods.js.map +1 -1
- package/dist/observation/index.d.ts +14 -0
- package/dist/observation/index.d.ts.map +1 -1
- package/dist/observation/index.js +13 -0
- package/dist/observation/index.js.map +1 -1
- package/dist/projections/eventContextPropertyExpression.js +1 -1
- package/dist/projections/eventContextPropertyExpression.js.map +1 -1
- package/dist/reactors/ReactorDispatcher.d.ts +15 -0
- package/dist/reactors/ReactorDispatcher.d.ts.map +1 -1
- package/dist/reactors/ReactorDispatcher.js +25 -2
- package/dist/reactors/ReactorDispatcher.js.map +1 -1
- package/dist/reactors/ReactorObservationTarget.d.ts +45 -0
- package/dist/reactors/ReactorObservationTarget.d.ts.map +1 -0
- package/dist/reactors/ReactorObservationTarget.js +4 -0
- package/dist/reactors/ReactorObservationTarget.js.map +1 -0
- package/dist/reactors/ReactorPageDispatcher.d.ts +33 -0
- package/dist/reactors/ReactorPageDispatcher.d.ts.map +1 -0
- package/dist/reactors/ReactorPageDispatcher.js +293 -0
- package/dist/reactors/ReactorPageDispatcher.js.map +1 -0
- package/dist/reactors/Reactors.d.ts.map +1 -1
- package/dist/reactors/Reactors.js +21 -149
- package/dist/reactors/Reactors.js.map +1 -1
- package/dist/reactors/for_Reactors/given/observe_reactor_messages.d.ts +14 -0
- package/dist/reactors/for_Reactors/given/observe_reactor_messages.d.ts.map +1 -0
- package/dist/reactors/for_Reactors/given/observe_reactor_messages.js +61 -0
- package/dist/reactors/for_Reactors/given/observe_reactor_messages.js.map +1 -0
- package/dist/reactors/index.d.ts +2 -0
- package/dist/reactors/index.d.ts.map +1 -1
- package/dist/reactors/index.js +1 -0
- package/dist/reactors/index.js.map +1 -1
- package/dist/reducers/ReducerEventDispatcher.d.ts +26 -1
- package/dist/reducers/ReducerEventDispatcher.d.ts.map +1 -1
- package/dist/reducers/ReducerEventDispatcher.js +35 -3
- package/dist/reducers/ReducerEventDispatcher.js.map +1 -1
- package/dist/reducers/Reducers.d.ts +2 -0
- package/dist/reducers/Reducers.d.ts.map +1 -1
- package/dist/reducers/Reducers.js +94 -22
- package/dist/reducers/Reducers.js.map +1 -1
- package/dist/reducers/ReductionOutcome.d.ts +27 -3
- package/dist/reducers/ReductionOutcome.d.ts.map +1 -1
- package/dist/reducers/ReductionOutcome.js +51 -7
- package/dist/reducers/ReductionOutcome.js.map +1 -1
- package/dist/reducers/for_Reducers/given/observe_reducer_messages.d.ts +19 -0
- package/dist/reducers/for_Reducers/given/observe_reducer_messages.d.ts.map +1 -0
- package/dist/reducers/for_Reducers/given/observe_reducer_messages.js +61 -0
- package/dist/reducers/for_Reducers/given/observe_reducer_messages.js.map +1 -0
- package/dist/reducers/index.d.ts +2 -0
- package/dist/reducers/index.d.ts.map +1 -1
- package/dist/reducers/index.js +1 -0
- package/dist/reducers/index.js.map +1 -1
- package/dist/schemas/JsonSchema.d.ts +2 -0
- package/dist/schemas/JsonSchema.d.ts.map +1 -1
- package/dist/testing/InProcessEventSequence.d.ts +1 -0
- package/dist/testing/InProcessEventSequence.d.ts.map +1 -1
- package/dist/testing/InProcessEventSequence.js +1 -0
- package/dist/testing/InProcessEventSequence.js.map +1 -1
- package/dist/testing/ReactorScenario.d.ts.map +1 -1
- package/dist/testing/ReactorScenario.js +4 -1
- package/dist/testing/ReactorScenario.js.map +1 -1
- package/dist/testing/ReducerReadModelProcessor.d.ts +1 -0
- package/dist/testing/ReducerReadModelProcessor.d.ts.map +1 -1
- package/dist/testing/ReducerReadModelProcessor.js +45 -0
- package/dist/testing/ReducerReadModelProcessor.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eventSequences/CompensateOptions.ts +10 -0
- package/eventSequences/EventSequence.compensate.spec.ts +72 -0
- package/eventSequences/EventSequence.ts +51 -0
- package/eventSequences/IEventSequence.ts +14 -0
- package/eventSequences/index.ts +1 -0
- package/events/EventContext.ts +6 -0
- package/events/compensationFor.spec.ts +73 -0
- package/events/compensationForDecorator.ts +48 -0
- package/events/eventTypeDecorator.ts +13 -4
- package/events/index.ts +2 -1
- package/events/toClientEventContext.ts +21 -0
- package/observation/batches/BatchContext.ts +75 -0
- package/observation/batches/BatchContextObserver.ts +16 -0
- package/observation/batches/BatchContextValues.ts +16 -0
- package/observation/batches/BatchMethod.ts +13 -0
- package/observation/batches/BatchMethodReturnedAValue.ts +16 -0
- package/observation/batches/BatchMethods.ts +90 -0
- package/observation/batches/BatchOutcome.ts +22 -0
- package/observation/batches/BatchOutcomeKind.ts +11 -0
- package/observation/batches/DeferralNotSupportedByKernel.ts +17 -0
- package/observation/batches/DeliveredPartition.ts +16 -0
- package/observation/batches/EventAndContext.ts +37 -0
- package/observation/batches/EventBatch.ts +105 -0
- package/observation/batches/EventTypeObservedByMoreThanOneMethod.ts +17 -0
- package/observation/batches/InvalidBatchMethod.ts +16 -0
- package/observation/batches/ObservationOrigin.ts +37 -0
- package/observation/batches/ObservedEventType.ts +16 -0
- package/observation/batches/PageMessages.ts +37 -0
- package/observation/batches/PartitionFailedInBatch.ts +14 -0
- package/observation/batches/PartitionNotInBatch.ts +14 -0
- package/observation/batches/PartitionOutcome.ts +22 -0
- package/observation/batches/PartitionProblem.ts +25 -0
- package/observation/batches/PartitionSettlement.ts +70 -0
- package/observation/batches/ReducerMixesBatchAndPerEventMethods.ts +16 -0
- package/observation/batches/batchSize.ts +79 -0
- package/observation/batches/createBatchContext.ts +32 -0
- package/observation/batches/for_BatchMethods/when_discovering_batch_methods.spec.ts +155 -0
- package/observation/batches/for_EventBatch/when_reporting_outcomes.spec.ts +66 -0
- package/observation/batches/for_PartitionSettlement/when_settling_a_partition.spec.ts +31 -0
- package/observation/batches/for_batchSize/when_declaring_bounds.spec.ts +23 -0
- package/observation/batches/for_observes/when_decorating_methods.legacy.spec.ts +28 -0
- package/observation/batches/for_observes/when_decorating_methods.spec.ts +33 -0
- package/observation/batches/for_runConcurrently/when_running_items.spec.ts +22 -0
- package/observation/batches/observes.ts +68 -0
- package/observation/batches/runConcurrently.ts +23 -0
- package/observation/createHandlerInstanceValidator.ts +5 -7
- package/observation/getInstanceMethods.ts +13 -4
- package/observation/index.ts +14 -0
- package/package.json +2 -2
- package/projections/eventContextPropertyExpression.ts +2 -2
- package/reactors/ReactorDispatcher.ts +27 -2
- package/reactors/ReactorObservationTarget.ts +48 -0
- package/reactors/ReactorPageDispatcher.ts +321 -0
- package/reactors/Reactors.ts +22 -164
- package/reactors/for_Reactors/given/observe_reactor_messages.ts +72 -0
- package/reactors/for_Reactors/when_delivering_pages/as_a_legacy_single_partition.spec.ts +51 -0
- package/reactors/for_Reactors/when_delivering_pages/returning_a_value_from_a_batch_method.spec.ts +34 -0
- package/reactors/for_Reactors/when_delivering_pages/with_batch_and_per_event_methods.spec.ts +81 -0
- package/reactors/for_Reactors/when_delivering_pages/with_batch_outcomes.spec.ts +86 -0
- package/reactors/for_Reactors/when_delivering_pages/with_per_event_handlers.spec.ts +52 -0
- package/reactors/index.ts +2 -0
- package/reducers/ReducerEventDispatcher.ts +44 -3
- package/reducers/Reducers.ts +109 -25
- package/reducers/ReductionOutcome.ts +55 -8
- package/reducers/for_Reducers/given/observe_reducer_messages.ts +77 -0
- package/reducers/for_Reducers/when_delivering_pages/to_a_batch_reducer.spec.ts +111 -0
- package/reducers/for_Reducers/when_delivering_pages/to_a_per_event_reducer.spec.ts +53 -0
- package/reducers/index.ts +2 -0
- package/schemas/JsonSchema.ts +2 -0
- package/telemetry/for_ChronicleTelemetry/given/a_client.fixture.ts +1 -0
- package/telemetry/for_ChronicleTelemetry/given/a_span_name_registry.fixture.ts +2 -0
- package/telemetry/for_ChronicleTelemetry/when_naming_spans/with_convention_names.spec.ts +2 -1
- package/testing/InProcessEventSequence.ts +1 -0
- package/testing/ReactorScenario.ts +5 -1
- package/testing/ReducerReadModelProcessor.ts +44 -0
- package/testing/for_ReactorScenario/when_the_reactor_has_a_batch_method.spec.ts +26 -0
- package/testing/for_ReadModelScenario/when_reducing_with_a_batch_method/with_events_for_two_event_sources.spec.ts +43 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { ObservationState, type ReactorResult } from '@cratis/chronicle.contracts';
|
|
6
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
7
|
+
import { BatchContext, EventBatch, observes } from '../../../observation/index.js';
|
|
8
|
+
import { reactor } from '../../reactor.js';
|
|
9
|
+
import { appended, observeReactorMessages, single } from '../given/observe_reactor_messages.js';
|
|
10
|
+
|
|
11
|
+
chai.should();
|
|
12
|
+
|
|
13
|
+
@eventType('legacy-order-placed')
|
|
14
|
+
class OrderPlaced {}
|
|
15
|
+
|
|
16
|
+
describe('when a kernel without page delivery sends a single partition to a batch reactor', () => {
|
|
17
|
+
let canDefer: boolean[];
|
|
18
|
+
let results: ReactorResult[];
|
|
19
|
+
|
|
20
|
+
beforeEach(async () => {
|
|
21
|
+
canDefer = [];
|
|
22
|
+
|
|
23
|
+
@reactor('legacy-orders')
|
|
24
|
+
class Orders {
|
|
25
|
+
@observes(OrderPlaced)
|
|
26
|
+
place(batch: EventBatch, context: BatchContext): void {
|
|
27
|
+
canDefer.push(context.canDefer);
|
|
28
|
+
if (batch.events.some(entry => entry.context.sequenceNumber === 3n)) batch.defer('order-1', 1000);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
({ results } = await observeReactorMessages(Orders, [OrderPlaced], [
|
|
33
|
+
single('order-1', [appended('legacy-order-placed', 'order-1', 1n), appended('legacy-order-placed', 'order-1', 2n)], 11n),
|
|
34
|
+
single('order-1', [appended('legacy-order-placed', 'order-1', 3n)], 12n)
|
|
35
|
+
]));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should tell the batch method that it cannot defer', () => canDefer.should.deep.equal([false, false]));
|
|
39
|
+
it('should reply in the legacy fields without per-partition results', () => {
|
|
40
|
+
results[0].Partition.should.equal('order-1');
|
|
41
|
+
results[0].State.should.equal(ObservationState.Success);
|
|
42
|
+
results[0].LastSuccessfulObservation.should.equal(2n);
|
|
43
|
+
results[0].Partitions.should.be.empty;
|
|
44
|
+
results[0].DeliveryId.should.equal(11n);
|
|
45
|
+
});
|
|
46
|
+
it('should fail a deferral the kernel cannot take, and say why', () => {
|
|
47
|
+
results[1].State.should.equal(ObservationState.Failed);
|
|
48
|
+
results[1].LastSuccessfulObservation.should.equal(4294967295n);
|
|
49
|
+
results[1].ExceptionMessages[0].should.contain('DeferralNotSupportedByKernel');
|
|
50
|
+
});
|
|
51
|
+
});
|
package/reactors/for_Reactors/when_delivering_pages/returning_a_value_from_a_batch_method.spec.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { ObservationState, type ReactorResult } from '@cratis/chronicle.contracts';
|
|
6
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
7
|
+
import { observes } from '../../../observation/index.js';
|
|
8
|
+
import { reactor } from '../../reactor.js';
|
|
9
|
+
import { appended, observeReactorMessages, page } from '../given/observe_reactor_messages.js';
|
|
10
|
+
|
|
11
|
+
chai.should();
|
|
12
|
+
|
|
13
|
+
@eventType('returning-invoice-sent')
|
|
14
|
+
class InvoiceSent {}
|
|
15
|
+
|
|
16
|
+
describe('when a reactor batch method returns a value', () => {
|
|
17
|
+
let results: ReactorResult[];
|
|
18
|
+
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
@reactor('returning-invoices')
|
|
21
|
+
class Invoices {
|
|
22
|
+
@observes(InvoiceSent)
|
|
23
|
+
send() {
|
|
24
|
+
return new InvoiceSent();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
({ results } = await observeReactorMessages(Invoices, [InvoiceSent], [page(1n, { 'invoice-1': [appended('returning-invoice-sent', 'invoice-1', 1n)] })]));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should fail the partition', () => results[0].Partitions[0].State.should.equal(ObservationState.Failed));
|
|
32
|
+
it('should say that batch methods do not return events', () =>
|
|
33
|
+
results[0].Partitions[0].ExceptionMessages[0].should.contain('BatchMethodReturnedAValue'));
|
|
34
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { ObservationOrigin as ContractObservationOrigin, ObservationState, type RegisterReactor, type ReactorResult } from '@cratis/chronicle.contracts';
|
|
6
|
+
import type { EventContext } from '../../../events/EventContext.js';
|
|
7
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
8
|
+
import { BatchContext, EventBatch, ObservationOrigin, observes } from '../../../observation/index.js';
|
|
9
|
+
import { reactor } from '../../reactor.js';
|
|
10
|
+
import { appended, observeReactorMessages, page } from '../given/observe_reactor_messages.js';
|
|
11
|
+
|
|
12
|
+
chai.should();
|
|
13
|
+
|
|
14
|
+
@eventType('batch-user-registered')
|
|
15
|
+
class UserRegistered {
|
|
16
|
+
constructor(readonly name: string = '') {}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@eventType('batch-user-renamed')
|
|
20
|
+
class UserRenamed {
|
|
21
|
+
constructor(readonly newName: string = '') {}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@eventType('batch-user-email-changed')
|
|
25
|
+
class UserEmailChanged {}
|
|
26
|
+
|
|
27
|
+
describe('when delivering a page to a reactor with a batch method and a per-event method', () => {
|
|
28
|
+
let batches: { names: string[]; partitions: readonly string[] }[];
|
|
29
|
+
let contexts: BatchContext[];
|
|
30
|
+
let perEvent: string[];
|
|
31
|
+
let registration: RegisterReactor;
|
|
32
|
+
let results: ReactorResult[];
|
|
33
|
+
|
|
34
|
+
beforeEach(async () => {
|
|
35
|
+
batches = [];
|
|
36
|
+
contexts = [];
|
|
37
|
+
perEvent = [];
|
|
38
|
+
|
|
39
|
+
@reactor('batch-users')
|
|
40
|
+
class Users {
|
|
41
|
+
@observes(UserRegistered)
|
|
42
|
+
@observes(UserRenamed)
|
|
43
|
+
index(batch: EventBatch, context: BatchContext): void {
|
|
44
|
+
contexts.push(context);
|
|
45
|
+
batches.push({
|
|
46
|
+
names: batch.events.map(entry => entry.is(UserRegistered) ? entry.event.name : entry.is(UserRenamed) ? entry.event.newName : '?'),
|
|
47
|
+
partitions: batch.partitions
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
userEmailChanged(_event: UserEmailChanged, context: EventContext) {
|
|
52
|
+
perEvent.push(`${context.eventSourceId}:${context.sequenceNumber}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
({ registration, results } = await observeReactorMessages(Users, [UserRegistered, UserRenamed, UserEmailChanged], [page(7n, {
|
|
57
|
+
alice: [appended('batch-user-registered', 'alice', 1n, { name: 'Alice' }), appended('batch-user-email-changed', 'alice', 3n),
|
|
58
|
+
appended('batch-user-renamed', 'alice', 4n, { newName: 'Alicia' })],
|
|
59
|
+
bob: [appended('batch-user-registered', 'bob', 2n, { name: 'Bob' })]
|
|
60
|
+
}, ContractObservationOrigin.CatchingUp)]));
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should register the event types of both methods', () =>
|
|
64
|
+
registration.Reactor!.EventTypes.map(eventType => eventType.EventType!.Id).sort()
|
|
65
|
+
.should.deep.equal(['batch-user-email-changed', 'batch-user-registered', 'batch-user-renamed']));
|
|
66
|
+
it('should call the batch method once with its event types across partitions in sequence order', () =>
|
|
67
|
+
batches.should.deep.equal([{ names: ['Alice', 'Bob', 'Alicia'], partitions: ['alice', 'bob'] }]));
|
|
68
|
+
it('should call the per-event method only for its own event type', () => perEvent.should.deep.equal(['alice:3']));
|
|
69
|
+
it('should describe the page in the batch context', () => {
|
|
70
|
+
contexts[0].origin.should.equal(ObservationOrigin.CatchUp);
|
|
71
|
+
contexts[0].observerId.should.equal('batch-users');
|
|
72
|
+
contexts[0].partitions.should.deep.equal(['alice', 'bob']);
|
|
73
|
+
contexts[0].eventCount.should.equal(4);
|
|
74
|
+
contexts[0].firstSequenceNumber!.should.equal(1n);
|
|
75
|
+
contexts[0].lastSequenceNumber!.should.equal(4n);
|
|
76
|
+
contexts[0].canDefer.should.be.true;
|
|
77
|
+
});
|
|
78
|
+
it('should settle every partition at its last event', () =>
|
|
79
|
+
results[0].Partitions.map(result => [result.Partition, result.State, result.LastSuccessfulObservation])
|
|
80
|
+
.should.deep.equal([['alice', ObservationState.Success, 4n], ['bob', ObservationState.Success, 2n]]));
|
|
81
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { ObservationState, type ReactorResult } from '@cratis/chronicle.contracts';
|
|
6
|
+
import type { EventContext } from '../../../events/EventContext.js';
|
|
7
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
8
|
+
import { EventBatch, observes } from '../../../observation/index.js';
|
|
9
|
+
import { reactor } from '../../reactor.js';
|
|
10
|
+
import { appended, observeReactorMessages, page } from '../given/observe_reactor_messages.js';
|
|
11
|
+
|
|
12
|
+
chai.should();
|
|
13
|
+
|
|
14
|
+
@eventType('outcome-document-indexed')
|
|
15
|
+
class DocumentIndexed {}
|
|
16
|
+
|
|
17
|
+
@eventType('outcome-document-archived')
|
|
18
|
+
class DocumentArchived {}
|
|
19
|
+
|
|
20
|
+
@eventType('outcome-document-shared')
|
|
21
|
+
class DocumentShared {}
|
|
22
|
+
|
|
23
|
+
describe('when batch methods report outcomes for partitions of a page', () => {
|
|
24
|
+
let results: ReactorResult[];
|
|
25
|
+
let failingMethodCalls: number;
|
|
26
|
+
|
|
27
|
+
beforeEach(async () => {
|
|
28
|
+
failingMethodCalls = 0;
|
|
29
|
+
|
|
30
|
+
@reactor('outcome-documents')
|
|
31
|
+
class Documents {
|
|
32
|
+
@observes(DocumentIndexed)
|
|
33
|
+
index(batch: EventBatch): void {
|
|
34
|
+
const second = batch.events.find(entry => entry.context.sequenceNumber === 5n)!;
|
|
35
|
+
batch.fail(second.context, new Error('index rejected the document'));
|
|
36
|
+
batch.defer('throttled', 1500);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@observes(DocumentArchived)
|
|
40
|
+
archive(): void {
|
|
41
|
+
failingMethodCalls++;
|
|
42
|
+
throw new Error('archive unavailable');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
documentShared(_event: DocumentShared, context: EventContext) {
|
|
46
|
+
if (context.eventSourceId === 'failed-late') throw new Error('share failed');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
({ results } = await observeReactorMessages(Documents, [DocumentIndexed, DocumentArchived, DocumentShared], [page(1n, {
|
|
51
|
+
healthy: [appended('outcome-document-indexed', 'healthy', 1n)],
|
|
52
|
+
'failed-at-event': [appended('outcome-document-indexed', 'failed-at-event', 2n), appended('outcome-document-indexed', 'failed-at-event', 5n)],
|
|
53
|
+
throttled: [appended('outcome-document-indexed', 'throttled', 3n), appended('outcome-document-indexed', 'throttled', 6n)],
|
|
54
|
+
archived: [appended('outcome-document-indexed', 'archived', 4n), appended('outcome-document-archived', 'archived', 7n)],
|
|
55
|
+
'failed-late': [appended('outcome-document-archived', 'failed-late', 8n), appended('outcome-document-shared', 'failed-late', 9n)]
|
|
56
|
+
})]));
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const resultFor = (partition: string) => results[0].Partitions.find(result => result.Partition === partition)!;
|
|
60
|
+
|
|
61
|
+
it('should call each batch method once for the page', () => failingMethodCalls.should.equal(1));
|
|
62
|
+
it('should succeed a partition nothing was reported for', () => {
|
|
63
|
+
resultFor('healthy').State.should.equal(ObservationState.Success);
|
|
64
|
+
resultFor('healthy').LastSuccessfulObservation.should.equal(1n);
|
|
65
|
+
});
|
|
66
|
+
it('should fail a partition from the event it was failed at, keeping the earlier events', () => {
|
|
67
|
+
resultFor('failed-at-event').State.should.equal(ObservationState.Failed);
|
|
68
|
+
resultFor('failed-at-event').LastSuccessfulObservation.should.equal(2n);
|
|
69
|
+
resultFor('failed-at-event').ExceptionMessages.should.deep.equal(['Error: index rejected the document']);
|
|
70
|
+
});
|
|
71
|
+
it('should defer a partition from its first event in the batch, with the delay', () => {
|
|
72
|
+
resultFor('throttled').State.should.equal(ObservationState.Deferred);
|
|
73
|
+
resultFor('throttled').LastSuccessfulObservation.should.equal(4294967295n);
|
|
74
|
+
resultFor('throttled').RetryAfterMilliseconds.should.equal(1500n);
|
|
75
|
+
});
|
|
76
|
+
it('should fail every partition of a throwing batch method at its first event in that batch', () => {
|
|
77
|
+
resultFor('archived').State.should.equal(ObservationState.Failed);
|
|
78
|
+
resultFor('archived').LastSuccessfulObservation.should.equal(4n);
|
|
79
|
+
resultFor('archived').ExceptionMessages.should.deep.equal(['Error: archive unavailable']);
|
|
80
|
+
});
|
|
81
|
+
it('should settle a partition at the earliest problem any method reported', () => {
|
|
82
|
+
resultFor('failed-late').State.should.equal(ObservationState.Failed);
|
|
83
|
+
resultFor('failed-late').LastSuccessfulObservation.should.equal(4294967295n);
|
|
84
|
+
resultFor('failed-late').ExceptionMessages.should.deep.equal(['Error: archive unavailable']);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { ObservationState, type RegisterReactor, type ReactorResult } from '@cratis/chronicle.contracts';
|
|
6
|
+
import type { EventContext } from '../../../events/EventContext.js';
|
|
7
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
8
|
+
import { reactor } from '../../reactor.js';
|
|
9
|
+
import { appended, observeReactorMessages, page } from '../given/observe_reactor_messages.js';
|
|
10
|
+
|
|
11
|
+
chai.should();
|
|
12
|
+
|
|
13
|
+
@eventType('paged-item-added')
|
|
14
|
+
class ItemAdded {}
|
|
15
|
+
|
|
16
|
+
describe('when delivering a page to a reactor with per-event handlers', () => {
|
|
17
|
+
let calls: string[];
|
|
18
|
+
let registration: RegisterReactor;
|
|
19
|
+
let results: ReactorResult[];
|
|
20
|
+
|
|
21
|
+
beforeEach(async () => {
|
|
22
|
+
calls = [];
|
|
23
|
+
|
|
24
|
+
@reactor('paged-per-event')
|
|
25
|
+
class Items {
|
|
26
|
+
itemAdded(_event: ItemAdded, context: EventContext) {
|
|
27
|
+
calls.push(`${context.eventSourceId}:${context.sequenceNumber}`);
|
|
28
|
+
if (context.sequenceNumber === 3n) throw new Error('second item of b failed');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
({ registration, results } = await observeReactorMessages(Items, [ItemAdded], [page(42n, {
|
|
33
|
+
a: [appended('paged-item-added', 'a', 1n), appended('paged-item-added', 'a', 4n)],
|
|
34
|
+
b: [appended('paged-item-added', 'b', 2n), appended('paged-item-added', 'b', 3n), appended('paged-item-added', 'b', 5n)]
|
|
35
|
+
})]));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should opt in to page delivery at registration', () => registration.PageDelivery!.Enabled.should.be.true);
|
|
39
|
+
it('should call the handler for each event of each partition in order, and stop a partition at its failure', () =>
|
|
40
|
+
calls.should.deep.equal(['a:1', 'a:4', 'b:2', 'b:3']));
|
|
41
|
+
it('should echo the delivery identifier', () => results[0].DeliveryId.should.equal(42n));
|
|
42
|
+
it('should reply with one result per partition', () => results[0].Partitions.map(result => result.Partition).should.deep.equal(['a', 'b']));
|
|
43
|
+
it('should settle the healthy partition at its last event', () => {
|
|
44
|
+
results[0].Partitions[0].State.should.equal(ObservationState.Success);
|
|
45
|
+
results[0].Partitions[0].LastSuccessfulObservation.should.equal(4n);
|
|
46
|
+
});
|
|
47
|
+
it('should settle the failed partition at the event before its failure', () => {
|
|
48
|
+
results[0].Partitions[1].State.should.equal(ObservationState.Failed);
|
|
49
|
+
results[0].Partitions[1].LastSuccessfulObservation.should.equal(2n);
|
|
50
|
+
results[0].Partitions[1].ExceptionMessages.should.deep.equal(['Error: second item of b failed']);
|
|
51
|
+
});
|
|
52
|
+
});
|
package/reactors/index.ts
CHANGED
|
@@ -13,3 +13,5 @@ export type { IReactors } from './IReactors.js';
|
|
|
13
13
|
export { Reactors } from './Reactors.js';
|
|
14
14
|
export type { ReactorResultHandler } from './ReactorResultHandler.js';
|
|
15
15
|
export type { ReactorServices } from './ReactorServices.js';
|
|
16
|
+
export { observes, batchSize, EventBatch, EventAndContext, BatchContext, ObservationOrigin } from '../observation/index.js';
|
|
17
|
+
export type { BatchSizeOptions } from '../observation/index.js';
|
|
@@ -4,18 +4,47 @@
|
|
|
4
4
|
import type { Constructor } from '@cratis/fundamentals';
|
|
5
5
|
import { createHandlerInstanceValidator } from '../observation/createHandlerInstanceValidator.js';
|
|
6
6
|
import { getEventHandlers } from '../observation/getEventHandlers.js';
|
|
7
|
+
import { getInstanceMethods } from '../observation/getInstanceMethods.js';
|
|
8
|
+
import type { BatchContext } from '../observation/batches/BatchContext.js';
|
|
9
|
+
import type { BatchMethod } from '../observation/batches/BatchMethod.js';
|
|
10
|
+
import { getBatchMethods, withoutBatchMethods } from '../observation/batches/BatchMethods.js';
|
|
11
|
+
import type { EventBatch } from '../observation/batches/EventBatch.js';
|
|
12
|
+
import { ReducerMixesBatchAndPerEventMethods } from '../observation/batches/ReducerMixesBatchAndPerEventMethods.js';
|
|
7
13
|
import type { EventContext } from '../events/EventContext.js';
|
|
8
14
|
import type { ReducerEventHandler } from './ReducerEventHandler.js';
|
|
9
15
|
|
|
10
16
|
/** Selects and invokes the same reducer handlers for kernel deliveries and in-process scenarios. */
|
|
11
17
|
export class ReducerEventDispatcher {
|
|
18
|
+
/** The per-event handlers; empty when the reducer has a batch method. */
|
|
12
19
|
readonly handlers: ReducerEventHandler[];
|
|
20
|
+
|
|
21
|
+
/** The batch method, when the reducer has one instead of per-event handlers. */
|
|
22
|
+
readonly batchMethod: BatchMethod | undefined;
|
|
23
|
+
|
|
13
24
|
readonly validateInstance: (instance: object) => void;
|
|
14
25
|
|
|
15
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Discovers event methods from the artifact event types. A reducer has either per-event methods or one batch method.
|
|
28
|
+
* @param reducerType - The reducer class.
|
|
29
|
+
* @param eventTypes - The event types registered with the event store.
|
|
30
|
+
* @throws {@link ReducerMixesBatchAndPerEventMethods} when the reducer mixes batch and per-event methods, or has two batch methods.
|
|
31
|
+
*/
|
|
16
32
|
constructor(reducerType: Constructor, eventTypes: readonly Constructor[]) {
|
|
17
|
-
|
|
18
|
-
|
|
33
|
+
const methods = getInstanceMethods(reducerType);
|
|
34
|
+
const batchMethods = getBatchMethods(reducerType, eventTypes, methods);
|
|
35
|
+
this.handlers = getEventHandlers(reducerType, eventTypes, withoutBatchMethods(methods, batchMethods));
|
|
36
|
+
if (batchMethods.length > 1 || (batchMethods.length === 1 && this.handlers.length > 0)) {
|
|
37
|
+
throw new ReducerMixesBatchAndPerEventMethods((reducerType as Function).name,
|
|
38
|
+
[...batchMethods.map(method => method.methodName), ...this.handlers.map(handler => handler.methodName)]);
|
|
39
|
+
}
|
|
40
|
+
this.batchMethod = batchMethods[0];
|
|
41
|
+
this.validateInstance = createHandlerInstanceValidator(reducerType,
|
|
42
|
+
[...this.handlers.map(handler => handler.methodName), ...batchMethods.map(method => method.methodName)], methods);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The event types the reducer registers: the batch method's observed types, or the per-event handlers' types. */
|
|
46
|
+
get eventTypes(): readonly { readonly id: string; readonly generation: number }[] {
|
|
47
|
+
return this.batchMethod ? [...this.batchMethod.eventTypes.values()] : this.handlers;
|
|
19
48
|
}
|
|
20
49
|
|
|
21
50
|
/** Resolves a registered event type, or returns undefined for an unsubscribed event. */
|
|
@@ -33,4 +62,16 @@ export class ReducerEventDispatcher {
|
|
|
33
62
|
): Promise<unknown> {
|
|
34
63
|
return await instance[handler.methodName](event, currentState, context) as unknown;
|
|
35
64
|
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Calls the batch method with the events of one event source, its current state and the page's context.
|
|
68
|
+
* @param instance - The reducer instance.
|
|
69
|
+
* @param batch - The {@link EventBatch} of the event source.
|
|
70
|
+
* @param currentState - The stored state of the event source, or undefined.
|
|
71
|
+
* @param context - The {@link BatchContext} of the page.
|
|
72
|
+
* @returns The next state; undefined or null removes the read model.
|
|
73
|
+
*/
|
|
74
|
+
async invokeBatch(instance: Record<string, Function>, batch: EventBatch, currentState: unknown, context: BatchContext): Promise<unknown> {
|
|
75
|
+
return await instance[this.batchMethod!.methodName](batch, currentState, context) as unknown;
|
|
76
|
+
}
|
|
36
77
|
}
|
package/reducers/Reducers.ts
CHANGED
|
@@ -5,7 +5,7 @@ import 'reflect-metadata';
|
|
|
5
5
|
import { createLogger } from '../logging/createLogger.js';
|
|
6
6
|
import { runInBackgroundContext } from '../telemetry/runInBackgroundContext.js';
|
|
7
7
|
import { Constructor } from '@cratis/fundamentals';
|
|
8
|
-
import { AppendedEvent, EventGenerationDelivery, ReadModelObserverType, ReduceOperationMessage, ReducerMessage, ReplayState } from '@cratis/chronicle.contracts';
|
|
8
|
+
import { AppendedEvent, EventGenerationDelivery, ObservationState, ReadModelObserverType, ReduceOperationMessage, ReducerMessage, ReplayState } from '@cratis/chronicle.contracts';
|
|
9
9
|
import { IClientArtifactsProvider } from '../artifacts/index.js';
|
|
10
10
|
import { ChronicleConnection } from '../connection/index.js';
|
|
11
11
|
import { ConnectionLifecycle } from '../connection/ConnectionLifecycle.js';
|
|
@@ -35,6 +35,16 @@ import { ArtifactKind } from '../artifacts/ArtifactKind.js';
|
|
|
35
35
|
import { ArtifactDelivery } from '../artifacts/ArtifactDelivery.js';
|
|
36
36
|
import { refuseClientHostedObservers } from '../artifacts/refuseClientHostedObservers.js';
|
|
37
37
|
import { withActivatedArtifact, runActivated } from '../artifacts/withActivatedArtifact.js';
|
|
38
|
+
import type { BatchContext } from '../observation/batches/BatchContext.js';
|
|
39
|
+
import { BatchOutcomeKind } from '../observation/batches/BatchOutcomeKind.js';
|
|
40
|
+
import { getMaxConcurrency, toPageDelivery } from '../observation/batches/batchSize.js';
|
|
41
|
+
import { createBatchContext } from '../observation/batches/createBatchContext.js';
|
|
42
|
+
import type { DeliveredPartition } from '../observation/batches/DeliveredPartition.js';
|
|
43
|
+
import { EventAndContext } from '../observation/batches/EventAndContext.js';
|
|
44
|
+
import { EventBatch } from '../observation/batches/EventBatch.js';
|
|
45
|
+
import { isPage, partitionsOfOperation } from '../observation/batches/PageMessages.js';
|
|
46
|
+
import { PartitionFailedInBatch } from '../observation/batches/PartitionFailedInBatch.js';
|
|
47
|
+
import { runConcurrently } from '../observation/batches/runConcurrently.js';
|
|
38
48
|
|
|
39
49
|
/** Expression used to partition reducer observations by event source ID. */
|
|
40
50
|
const EVENT_SOURCE_ID_KEY = '$eventSourceId';
|
|
@@ -328,15 +338,14 @@ export class Reducers implements IReducers {
|
|
|
328
338
|
const session: ReducerObservationSession = { ...observation, signal: controller.signal, reducerInstance };
|
|
329
339
|
|
|
330
340
|
for await (const operation of this._connection.reducers.observe(queue, { signal: controller.signal })) {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
queue.send(outcome.toMessage(operation.Partition));
|
|
341
|
+
const partitions = partitionsOfOperation(operation);
|
|
342
|
+
const notification = this.beginReduction(session, operation, partitions);
|
|
343
|
+
await this.notifyReplayTransition(session, operation, notification);
|
|
344
|
+
const context = createBatchContext({ observerId: id, eventStore: this._eventStoreName, namespace: this._namespace,
|
|
345
|
+
eventSequenceId: session.eventSequenceId, signal: controller.signal }, operation, partitions);
|
|
346
|
+
const outcomes = await runConcurrently(partitions, getMaxConcurrency(reducerType),
|
|
347
|
+
partition => this.reducePartition(session, partition, notification, context));
|
|
348
|
+
queue.send(toResultMessage(operation, partitions, outcomes));
|
|
340
349
|
}
|
|
341
350
|
} catch (err) {
|
|
342
351
|
if (!this._queues.has(id)) {
|
|
@@ -366,7 +375,7 @@ export class Reducers implements IReducers {
|
|
|
366
375
|
Reducer: {
|
|
367
376
|
ReducerId: id,
|
|
368
377
|
EventSequenceId: eventSequenceId,
|
|
369
|
-
EventTypes: dispatcher.
|
|
378
|
+
EventTypes: dispatcher.eventTypes.map(et => ({
|
|
370
379
|
EventType: { Id: et.id, Generation: et.generation, Tombstone: false },
|
|
371
380
|
Key: EVENT_SOURCE_ID_KEY
|
|
372
381
|
})),
|
|
@@ -381,24 +390,37 @@ export class Reducers implements IReducers {
|
|
|
381
390
|
EventStreamType: getEventStreamTypeFor(reducerType)
|
|
382
391
|
}
|
|
383
392
|
},
|
|
384
|
-
PageDelivery:
|
|
393
|
+
PageDelivery: toPageDelivery(reducerType)
|
|
385
394
|
},
|
|
386
395
|
Value1: undefined
|
|
387
396
|
}
|
|
388
397
|
};
|
|
389
398
|
}
|
|
390
399
|
|
|
391
|
-
private beginReduction(session: ReducerObservationSession, operation: ReduceOperationMessage
|
|
400
|
+
private beginReduction(session: ReducerObservationSession, operation: ReduceOperationMessage,
|
|
401
|
+
partitions: readonly DeliveredPartition[]): ReductionOutcome {
|
|
392
402
|
this._logger.debug('Received reduce operation', {
|
|
393
403
|
reducerId: session.id,
|
|
394
|
-
|
|
395
|
-
|
|
404
|
+
partitions: partitions.length,
|
|
405
|
+
count: partitions.reduce((count, partition) => count + partition.events.length, 0),
|
|
396
406
|
replayState: operation.ReplayState
|
|
397
407
|
});
|
|
398
408
|
|
|
399
409
|
return new ReductionOutcome();
|
|
400
410
|
}
|
|
401
411
|
|
|
412
|
+
private async reducePartition(session: ReducerObservationSession, partition: DeliveredPartition,
|
|
413
|
+
notification: ReductionOutcome, context: BatchContext): Promise<ReductionOutcome> {
|
|
414
|
+
const outcome = new ReductionOutcome();
|
|
415
|
+
if (notification.failed) {
|
|
416
|
+
outcome.copyFrom(notification);
|
|
417
|
+
return outcome;
|
|
418
|
+
}
|
|
419
|
+
outcome.currentState = partition.initialState ? JSON.parse(partition.initialState) as unknown : undefined;
|
|
420
|
+
await this.reduceEvents(session, partition, outcome, context);
|
|
421
|
+
return outcome;
|
|
422
|
+
}
|
|
423
|
+
|
|
402
424
|
private async notifyReplayTransition(
|
|
403
425
|
session: ReducerObservationSession,
|
|
404
426
|
operation: ReduceOperationMessage,
|
|
@@ -442,16 +464,18 @@ export class Reducers implements IReducers {
|
|
|
442
464
|
|
|
443
465
|
private async reduceEvents(
|
|
444
466
|
session: ReducerObservationSession,
|
|
445
|
-
|
|
446
|
-
outcome: ReductionOutcome
|
|
467
|
+
partition: DeliveredPartition,
|
|
468
|
+
outcome: ReductionOutcome,
|
|
469
|
+
context: BatchContext
|
|
447
470
|
): Promise<void> {
|
|
448
471
|
const { id, reducerType, eventSequenceId, dispatcher, signal, reducerInstance } = session;
|
|
449
472
|
const firstInvocableEvent = this._artifactActivator && !outcome.failed
|
|
450
|
-
?
|
|
451
|
-
|
|
473
|
+
? partition.events.find(event => !!event.Context?.EventType?.Id && (dispatcher.batchMethod
|
|
474
|
+
? dispatcher.batchMethod.eventTypes.has(event.Context.EventType.Id)
|
|
475
|
+
: !!dispatcher.handlerFor(event.Context.EventType.Id)))
|
|
452
476
|
: undefined;
|
|
453
477
|
if (!firstInvocableEvent || !this._artifactActivator) {
|
|
454
|
-
await this.processEvents(session,
|
|
478
|
+
await this.processEvents(session, partition, outcome, { instance: reducerInstance ?? {} }, context);
|
|
455
479
|
return;
|
|
456
480
|
}
|
|
457
481
|
|
|
@@ -459,12 +483,12 @@ export class Reducers implements IReducers {
|
|
|
459
483
|
const eventStore = this.requireEventStore();
|
|
460
484
|
await withActivatedArtifact(reducerType as new () => Record<string, Function>, {
|
|
461
485
|
kind: ArtifactKind.Reducer, artifactId: id, eventStore,
|
|
462
|
-
readModels: eventStore.readModels, eventSequenceId, partition:
|
|
486
|
+
readModels: eventStore.readModels, eventSequenceId, partition: partition.partition,
|
|
463
487
|
signal, delivery: ArtifactDelivery.Events,
|
|
464
488
|
eventContext: toClientEventContext(firstInvocableEvent.Context!)
|
|
465
489
|
}, this._artifactActivator, artifact => {
|
|
466
490
|
dispatcher.validateInstance(artifact.instance);
|
|
467
|
-
return this.processEvents(session,
|
|
491
|
+
return this.processEvents(session, partition, outcome, artifact, context);
|
|
468
492
|
}, this._connection.logger);
|
|
469
493
|
} catch (err) {
|
|
470
494
|
this._logger.error('Error activating reducer', { reducerId: id, error: err });
|
|
@@ -474,11 +498,17 @@ export class Reducers implements IReducers {
|
|
|
474
498
|
|
|
475
499
|
private async processEvents(
|
|
476
500
|
session: ReducerObservationSession,
|
|
477
|
-
|
|
501
|
+
partition: DeliveredPartition,
|
|
478
502
|
outcome: ReductionOutcome,
|
|
479
|
-
artifact: ActivatedArtifact<Record<string, Function
|
|
503
|
+
artifact: ActivatedArtifact<Record<string, Function>>,
|
|
504
|
+
context: BatchContext
|
|
480
505
|
): Promise<void> {
|
|
481
|
-
|
|
506
|
+
if (outcome.failed) return;
|
|
507
|
+
if (session.dispatcher.batchMethod) {
|
|
508
|
+
await this.processBatch(session, partition, outcome, artifact, context);
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
for (const event of partition.events) {
|
|
482
512
|
try {
|
|
483
513
|
// Skipped events stay synchronous; only an actual handler invocation is awaited.
|
|
484
514
|
const pending = this.processEvent(session, event, outcome, artifact);
|
|
@@ -491,6 +521,37 @@ export class Reducers implements IReducers {
|
|
|
491
521
|
}
|
|
492
522
|
}
|
|
493
523
|
|
|
524
|
+
private async processBatch(
|
|
525
|
+
{ id, dispatcher }: ReducerObservationSession,
|
|
526
|
+
partition: DeliveredPartition,
|
|
527
|
+
outcome: ReductionOutcome,
|
|
528
|
+
artifact: ActivatedArtifact<Record<string, Function>>,
|
|
529
|
+
context: BatchContext
|
|
530
|
+
): Promise<void> {
|
|
531
|
+
const method = dispatcher.batchMethod!;
|
|
532
|
+
const events = partition.events.filter(event => method.eventTypes.has(event.Context?.EventType?.Id ?? ''));
|
|
533
|
+
if (events.length === 0) return;
|
|
534
|
+
try {
|
|
535
|
+
const batch = new EventBatch(events.map(event => new EventAndContext(JSON.parse(event.Content) as Record<string, unknown>,
|
|
536
|
+
toClientEventContext(event.Context!), method.eventTypes.get(event.Context!.EventType!.Id)!.type)), context.canDefer);
|
|
537
|
+
this._logger.info('Invoking reducer batch method', { reducerId: id, method: method.methodName, count: events.length });
|
|
538
|
+
const next = await runActivated(artifact, () => dispatcher.invokeBatch(artifact.instance, batch, outcome.currentState, context),
|
|
539
|
+
{ delivery: ArtifactDelivery.Events, eventContext: batch.events[0].context, methodName: method.methodName });
|
|
540
|
+
const [reported] = batch.outcomes;
|
|
541
|
+
if (reported?.kind === BatchOutcomeKind.Deferred) {
|
|
542
|
+
outcome.recordDeferral(reported.retryAfterMilliseconds);
|
|
543
|
+
} else if (reported) {
|
|
544
|
+
outcome.recordFailure(reported.error ?? new PartitionFailedInBatch(partition.partition));
|
|
545
|
+
} else {
|
|
546
|
+
outcome.currentState = next;
|
|
547
|
+
outcome.lastSuccessfullyObservedEvent = events.at(-1)!.Context!.SequenceNumber;
|
|
548
|
+
}
|
|
549
|
+
} catch (err) {
|
|
550
|
+
this._logger.error('Error handling events in reducer batch method', { reducerId: id, error: err });
|
|
551
|
+
outcome.recordFailure(err);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
494
555
|
private processEvent(
|
|
495
556
|
{ id, dispatcher }: ReducerObservationSession,
|
|
496
557
|
event: AppendedEvent,
|
|
@@ -540,3 +601,26 @@ export class Reducers implements IReducers {
|
|
|
540
601
|
this._queues.clear();
|
|
541
602
|
}
|
|
542
603
|
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* Creates the result of a reduce operation: one result per event source for a page, or the legacy single-partition fields.
|
|
607
|
+
* @param operation - The delivered operation.
|
|
608
|
+
* @param partitions - The partitions of the operation.
|
|
609
|
+
* @param outcomes - The outcome of each partition, in the same order.
|
|
610
|
+
* @returns The {@link ReducerMessage}.
|
|
611
|
+
*/
|
|
612
|
+
function toResultMessage(operation: ReduceOperationMessage, partitions: readonly DeliveredPartition[],
|
|
613
|
+
outcomes: readonly ReductionOutcome[]): ReducerMessage {
|
|
614
|
+
if (!isPage(operation)) return outcomes[0].toMessage(operation.Partition, operation.DeliveryId ?? 0n);
|
|
615
|
+
return {
|
|
616
|
+
Content: {
|
|
617
|
+
Value0: undefined,
|
|
618
|
+
Value1: {
|
|
619
|
+
Partition: '', State: ObservationState.OBSERVATION_STATE_None, LastSuccessfulObservation: 0n,
|
|
620
|
+
ExceptionMessages: [], ExceptionStackTrace: '', ReadModelState: '',
|
|
621
|
+
DeliveryId: operation.DeliveryId ?? 0n, RetryAfterMilliseconds: 0n,
|
|
622
|
+
Partitions: outcomes.map((outcome, index) => outcome.toPartitionResult(partitions[index].partition))
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
}
|