@cratis/chronicle 6.61.0 → 6.63.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/ChronicleClient.ts +86 -3
- package/connection/ChronicleConnection.ts +7 -1
- package/connection/ChronicleServices.ts +2 -0
- package/connection/ConnectionLifecycle.ts +42 -1
- package/dist/ChronicleClient.d.ts +28 -1
- package/dist/ChronicleClient.d.ts.map +1 -1
- package/dist/ChronicleClient.js +71 -3
- package/dist/ChronicleClient.js.map +1 -1
- package/dist/connection/ChronicleConnection.d.ts +1 -0
- package/dist/connection/ChronicleConnection.d.ts.map +1 -1
- package/dist/connection/ChronicleConnection.js +6 -2
- package/dist/connection/ChronicleConnection.js.map +1 -1
- package/dist/connection/ChronicleServices.d.ts +2 -1
- package/dist/connection/ChronicleServices.d.ts.map +1 -1
- package/dist/connection/ConnectionLifecycle.d.ts +24 -0
- package/dist/connection/ConnectionLifecycle.d.ts.map +1 -1
- package/dist/connection/ConnectionLifecycle.js +40 -1
- package/dist/connection/ConnectionLifecycle.js.map +1 -1
- package/dist/eventSequences/EventForEventSourceId.d.ts +21 -1
- package/dist/eventSequences/EventForEventSourceId.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.d.ts +9 -0
- package/dist/eventSequences/EventSequence.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.js +18 -0
- package/dist/eventSequences/EventSequence.js.map +1 -1
- package/dist/eventSequences/IEventSequence.d.ts +30 -0
- package/dist/eventSequences/IEventSequence.d.ts.map +1 -1
- package/dist/imports/EventImport.d.ts +29 -0
- package/dist/imports/EventImport.d.ts.map +1 -0
- package/dist/imports/EventImport.js +248 -0
- package/dist/imports/EventImport.js.map +1 -0
- package/dist/imports/EventImportSettings.d.ts +32 -0
- package/dist/imports/EventImportSettings.d.ts.map +1 -0
- package/dist/imports/EventImportSettings.js +4 -0
- package/dist/imports/EventImportSettings.js.map +1 -0
- package/dist/imports/IEventImport.d.ts +50 -0
- package/dist/imports/IEventImport.d.ts.map +1 -0
- package/dist/imports/IEventImport.js +4 -0
- package/dist/imports/IEventImport.js.map +1 -0
- package/dist/imports/ImportChunkRefused.d.ts +12 -0
- package/dist/imports/ImportChunkRefused.d.ts.map +1 -0
- package/dist/imports/ImportChunkRefused.js +19 -0
- package/dist/imports/ImportChunkRefused.js.map +1 -0
- package/dist/imports/ImportCouldNotBegin.d.ts +10 -0
- package/dist/imports/ImportCouldNotBegin.d.ts.map +1 -0
- package/dist/imports/ImportCouldNotBegin.js +16 -0
- package/dist/imports/ImportCouldNotBegin.js.map +1 -0
- package/dist/imports/ImportOptions.d.ts +26 -0
- package/dist/imports/ImportOptions.d.ts.map +1 -0
- package/dist/imports/ImportOptions.js +4 -0
- package/dist/imports/ImportOptions.js.map +1 -0
- package/dist/imports/ImportProgress.d.ts +20 -0
- package/dist/imports/ImportProgress.d.ts.map +1 -0
- package/dist/imports/ImportProgress.js +4 -0
- package/dist/imports/ImportProgress.js.map +1 -0
- package/dist/imports/ImportRejection.d.ts +10 -0
- package/dist/imports/ImportRejection.d.ts.map +1 -0
- package/dist/imports/ImportRejection.js +4 -0
- package/dist/imports/ImportRejection.js.map +1 -0
- package/dist/imports/ImportResult.d.ts +30 -0
- package/dist/imports/ImportResult.d.ts.map +1 -0
- package/dist/imports/ImportResult.js +4 -0
- package/dist/imports/ImportResult.js.map +1 -0
- package/dist/imports/ImportState.d.ts +18 -0
- package/dist/imports/ImportState.d.ts.map +1 -0
- package/dist/imports/ImportState.js +21 -0
- package/dist/imports/ImportState.js.map +1 -0
- package/dist/imports/ImportTarget.d.ts +16 -0
- package/dist/imports/ImportTarget.d.ts.map +1 -0
- package/dist/imports/ImportTarget.js +4 -0
- package/dist/imports/ImportTarget.js.map +1 -0
- package/dist/imports/ImportWasAbandoned.d.ts +11 -0
- package/dist/imports/ImportWasAbandoned.d.ts.map +1 -0
- package/dist/imports/ImportWasAbandoned.js +16 -0
- package/dist/imports/ImportWasAbandoned.js.map +1 -0
- package/dist/imports/ImportsNotSupported.d.ts +5 -0
- package/dist/imports/ImportsNotSupported.d.ts.map +1 -0
- package/dist/imports/ImportsNotSupported.js +10 -0
- package/dist/imports/ImportsNotSupported.js.map +1 -0
- package/dist/imports/OnInvalidEvent.d.ts +8 -0
- package/dist/imports/OnInvalidEvent.d.ts.map +1 -0
- package/dist/imports/OnInvalidEvent.js +11 -0
- package/dist/imports/OnInvalidEvent.js.map +1 -0
- package/dist/imports/abortable.d.ts +23 -0
- package/dist/imports/abortable.d.ts.map +1 -0
- package/dist/imports/abortable.js +60 -0
- package/dist/imports/abortable.js.map +1 -0
- package/dist/imports/beginEventImport.d.ts +16 -0
- package/dist/imports/beginEventImport.d.ts.map +1 -0
- package/dist/imports/beginEventImport.js +74 -0
- package/dist/imports/beginEventImport.js.map +1 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.d.ts +144 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.d.ts.map +1 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.js +132 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.js.map +1 -0
- package/dist/imports/importEvents.d.ts +13 -0
- package/dist/imports/importEvents.d.ts.map +1 -0
- package/dist/imports/importEvents.js +35 -0
- package/dist/imports/importEvents.js.map +1 -0
- package/dist/imports/index.d.ts +12 -0
- package/dist/imports/index.d.ts.map +1 -0
- package/dist/imports/index.js +9 -0
- package/dist/imports/index.js.map +1 -0
- package/dist/imports/isTransientImportFailure.d.ts +7 -0
- package/dist/imports/isTransientImportFailure.d.ts.map +1 -0
- package/dist/imports/isTransientImportFailure.js +13 -0
- package/dist/imports/isTransientImportFailure.js.map +1 -0
- package/dist/imports/sizeOfImportEvent.d.ts +9 -0
- package/dist/imports/sizeOfImportEvent.d.ts.map +1 -0
- package/dist/imports/sizeOfImportEvent.js +25 -0
- package/dist/imports/sizeOfImportEvent.js.map +1 -0
- package/dist/imports/toImportEvent.d.ts +14 -0
- package/dist/imports/toImportEvent.d.ts.map +1 -0
- package/dist/imports/toImportEvent.js +54 -0
- package/dist/imports/toImportEvent.js.map +1 -0
- package/dist/imports/toImportProgress.d.ts +9 -0
- package/dist/imports/toImportProgress.d.ts.map +1 -0
- package/dist/imports/toImportProgress.js +20 -0
- package/dist/imports/toImportProgress.js.map +1 -0
- package/dist/imports/toImportResult.d.ts +9 -0
- package/dist/imports/toImportResult.d.ts.map +1 -0
- package/dist/imports/toImportResult.js +33 -0
- package/dist/imports/toImportResult.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/observation/ObservationReconnectBackoff.d.ts +64 -0
- package/dist/observation/ObservationReconnectBackoff.d.ts.map +1 -0
- package/dist/observation/ObservationReconnectBackoff.js +75 -0
- package/dist/observation/ObservationReconnectBackoff.js.map +1 -0
- package/dist/observation/ObservationStreamOutcome.d.ts +10 -0
- package/dist/observation/ObservationStreamOutcome.d.ts.map +1 -0
- package/dist/observation/ObservationStreamOutcome.js +4 -0
- package/dist/observation/ObservationStreamOutcome.js.map +1 -0
- package/dist/observation/classifyStreamError.d.ts +25 -0
- package/dist/observation/classifyStreamError.d.ts.map +1 -0
- package/dist/observation/classifyStreamError.js +55 -0
- package/dist/observation/classifyStreamError.js.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.d.ts +13 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.d.ts.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.js +25 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.js.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.d.ts +27 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.d.ts.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.js +36 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.js.map +1 -0
- package/dist/observation/index.d.ts +2 -0
- package/dist/observation/index.d.ts.map +1 -1
- package/dist/observation/index.js +1 -0
- package/dist/observation/index.js.map +1 -1
- package/dist/reactors/Reactors.d.ts +15 -5
- package/dist/reactors/Reactors.d.ts.map +1 -1
- package/dist/reactors/Reactors.js +95 -18
- package/dist/reactors/Reactors.js.map +1 -1
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.d.ts +26 -0
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.d.ts.map +1 -0
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.js +64 -0
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.js.map +1 -0
- package/dist/reducers/Reducers.d.ts +11 -5
- package/dist/reducers/Reducers.d.ts.map +1 -1
- package/dist/reducers/Reducers.js +93 -19
- package/dist/reducers/Reducers.js.map +1 -1
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.d.ts +25 -0
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.d.ts.map +1 -0
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.js +70 -0
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.js.map +1 -0
- package/dist/testing/BatchPartitionOutcome.d.ts +7 -0
- package/dist/testing/BatchPartitionOutcome.d.ts.map +1 -0
- package/dist/testing/BatchPartitionOutcome.js +4 -0
- package/dist/testing/BatchPartitionOutcome.js.map +1 -0
- package/dist/testing/InProcessEventSequence.d.ts +4 -0
- package/dist/testing/InProcessEventSequence.d.ts.map +1 -1
- package/dist/testing/InProcessEventSequence.js +2 -0
- package/dist/testing/InProcessEventSequence.js.map +1 -1
- package/dist/testing/ReactorDeliveryResult.d.ts +2 -0
- package/dist/testing/ReactorDeliveryResult.d.ts.map +1 -1
- package/dist/testing/ReactorScenario.d.ts +46 -10
- package/dist/testing/ReactorScenario.d.ts.map +1 -1
- package/dist/testing/ReactorScenario.js +120 -29
- package/dist/testing/ReactorScenario.js.map +1 -1
- package/dist/testing/deliverToBatchMethods.d.ts +35 -0
- package/dist/testing/deliverToBatchMethods.d.ts.map +1 -0
- package/dist/testing/deliverToBatchMethods.js +55 -0
- package/dist/testing/deliverToBatchMethods.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eventSequences/EventForEventSourceId.ts +25 -1
- package/eventSequences/EventSequence.ts +28 -0
- package/eventSequences/IEventSequence.ts +33 -0
- package/for_ChronicleClient/when_reconnecting/and_an_observation_stream_lost_its_session.spec.ts +212 -0
- package/imports/EventImport.ts +263 -0
- package/imports/EventImportSettings.ts +46 -0
- package/imports/IEventImport.ts +58 -0
- package/imports/ImportChunkRefused.ts +15 -0
- package/imports/ImportCouldNotBegin.ts +14 -0
- package/imports/ImportOptions.ts +36 -0
- package/imports/ImportProgress.ts +29 -0
- package/imports/ImportRejection.ts +14 -0
- package/imports/ImportResult.ts +43 -0
- package/imports/ImportState.ts +26 -0
- package/imports/ImportTarget.ts +23 -0
- package/imports/ImportWasAbandoned.ts +16 -0
- package/imports/ImportsNotSupported.ts +10 -0
- package/imports/OnInvalidEvent.ts +11 -0
- package/imports/abortable.ts +57 -0
- package/imports/beginEventImport.ts +85 -0
- package/imports/for_EventImport/given/a_kernel_import_session.ts +160 -0
- package/imports/for_EventImport/when_a_chunk_fails.spec.ts +65 -0
- package/imports/for_EventImport/when_abandoning.spec.ts +52 -0
- package/imports/for_EventImport/when_cancelling_with_an_abort_signal.spec.ts +59 -0
- package/imports/for_EventImport/when_completing.spec.ts +71 -0
- package/imports/for_EventImport/when_cutting_events_into_chunks.spec.ts +70 -0
- package/imports/for_EventImport/when_keeping_chunks_in_flight.spec.ts +49 -0
- package/imports/for_beginEventImport/when_reattaching.spec.ts +82 -0
- package/imports/for_beginEventImport/when_the_kernel_does_not_serve_imports.spec.ts +55 -0
- package/imports/for_importEvents/when_importing_in_one_call.spec.ts +99 -0
- package/imports/for_toImportEvent/when_converting_an_event.spec.ts +74 -0
- package/imports/importEvents.ts +44 -0
- package/imports/index.ts +14 -0
- package/imports/isTransientImportFailure.ts +15 -0
- package/imports/sizeOfImportEvent.ts +25 -0
- package/imports/toImportEvent.ts +60 -0
- package/imports/toImportProgress.ts +24 -0
- package/imports/toImportResult.ts +36 -0
- package/index.ts +3 -0
- package/observation/ObservationReconnectBackoff.ts +109 -0
- package/observation/ObservationStreamOutcome.ts +12 -0
- package/observation/classifyStreamError.ts +67 -0
- package/observation/for_ObservationReconnectBackoff/given/a_backoff.ts +29 -0
- package/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.ts +52 -0
- package/observation/for_ObservationReconnectBackoff/when_a_stream_delivered_before_it_ended.spec.ts +23 -0
- package/observation/for_ObservationReconnectBackoff/when_a_stream_stayed_open_before_it_ended.spec.ts +25 -0
- package/observation/for_ObservationReconnectBackoff/when_jittering_the_delay.spec.ts +24 -0
- package/observation/for_ObservationReconnectBackoff/when_streams_keep_ending_without_working.spec.ts +24 -0
- package/observation/for_ObservationReconnectBackoff/when_the_base_delay_is_not_positive.spec.ts +18 -0
- package/observation/for_ObservationReconnectBackoff/when_the_settings_are_not_finite.spec.ts +32 -0
- package/observation/for_ObservationReconnectBackoff/when_using_the_defaults.spec.ts +29 -0
- package/observation/for_classifyStreamError/when_classifying_a_stream_error.spec.ts +36 -0
- package/observation/index.ts +2 -0
- package/package.json +7 -2
- package/projections/for_childrenOrderingSupport/when_detecting_support_in_the_pinned_contracts.spec.ts +1 -1
- package/reactors/Reactors.ts +101 -18
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_handler_failed_with_a_lost_session.spec.ts +32 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_replaced_stream_fails_late.spec.ts +28 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_stream_delivered_before_it_ended.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_stream_stayed_open_before_it_ended.spec.ts +23 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_it_is_disposed_while_waiting_to_reopen.spec.ts +27 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_streams_keep_ending_without_working.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_the_client_reconnected_after_streams_kept_ending.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_the_client_reconnected_while_waiting_to_reopen.spec.ts +25 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_the_kernel_lost_the_session.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.ts +69 -0
- package/reducers/Reducers.ts +99 -19
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_reducer_failed_with_a_lost_session.spec.ts +33 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_replaced_stream_fails_late.spec.ts +28 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_stream_delivered_before_it_ended.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_stream_stayed_open_before_it_ended.spec.ts +23 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_it_is_disposed_while_waiting_to_reopen.spec.ts +27 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_streams_keep_ending_without_working.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_the_client_reconnected_after_streams_kept_ending.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_the_client_reconnected_while_waiting_to_reopen.spec.ts +25 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_the_kernel_lost_the_session.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.ts +69 -0
- package/testing/BatchPartitionOutcome.ts +10 -0
- package/testing/InProcessEventSequence.ts +4 -0
- package/testing/ReactorDeliveryResult.ts +2 -0
- package/testing/ReactorScenario.ts +147 -39
- package/testing/deliverToBatchMethods.ts +90 -0
- package/testing/for_ReactorScenario/when_the_reactor_has_a_batch_method.spec.ts +143 -9
package/for_ChronicleClient/when_reconnecting/and_an_observation_stream_lost_its_session.spec.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
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 { afterEach, beforeEach, chai, describe, it, vi } from 'vitest';
|
|
5
|
+
import { ClientError, Status } from 'nice-grpc';
|
|
6
|
+
import { ChronicleClient } from '../../ChronicleClient.js';
|
|
7
|
+
import { ChronicleOptions } from '../../ChronicleOptions.js';
|
|
8
|
+
import { ConnectionLifecycle, type ReconnectRequestedHandler } from '../../connection/ConnectionLifecycle.js';
|
|
9
|
+
|
|
10
|
+
chai.should();
|
|
11
|
+
|
|
12
|
+
const transport = vi.hoisted(() => ({
|
|
13
|
+
resetChannel: vi.fn(),
|
|
14
|
+
connect: vi.fn(),
|
|
15
|
+
disconnect: vi.fn(),
|
|
16
|
+
getVersionInfo: vi.fn(),
|
|
17
|
+
allEventStores: vi.fn()
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
vi.mock('../../connection/ChronicleConnection', () => ({
|
|
21
|
+
ChronicleConnection: class {
|
|
22
|
+
resetChannel = transport.resetChannel;
|
|
23
|
+
connect = transport.connect;
|
|
24
|
+
disconnect = transport.disconnect;
|
|
25
|
+
server = { getVersionInfo: transport.getVersionInfo };
|
|
26
|
+
eventStores = { allEventStores: transport.allEventStores };
|
|
27
|
+
}
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
vi.mock('../../connection/KernelKeepAlive', () => ({
|
|
31
|
+
KernelKeepAlive: class {
|
|
32
|
+
async start() {}
|
|
33
|
+
}
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
const sessionLost = () => new ClientError('/Reactors/Observe', Status.UNKNOWN, 'ClientIsNotConnected: connection is not connected');
|
|
37
|
+
const ignore = () => undefined;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A connected client on fake timers and a jitter of zero, so each session-reconnect wait is exactly half its ceiling:
|
|
41
|
+
* 500 ms after the first session lost soon after connecting, 1 000 ms after the second, and so on.
|
|
42
|
+
*/
|
|
43
|
+
class a_connected_client {
|
|
44
|
+
client!: ChronicleClient;
|
|
45
|
+
lifecycle!: ConnectionLifecycle;
|
|
46
|
+
readonly connectedAt: number[] = [];
|
|
47
|
+
|
|
48
|
+
async establish(): Promise<void> {
|
|
49
|
+
vi.useFakeTimers();
|
|
50
|
+
vi.clearAllMocks();
|
|
51
|
+
const subscribe = ConnectionLifecycle.prototype.onReconnectRequested;
|
|
52
|
+
const self = this;
|
|
53
|
+
vi.spyOn(ConnectionLifecycle.prototype, 'onReconnectRequested').mockImplementation(function (this: ConnectionLifecycle, handler: ReconnectRequestedHandler) {
|
|
54
|
+
self.lifecycle = this;
|
|
55
|
+
return subscribe.call(this, handler);
|
|
56
|
+
});
|
|
57
|
+
transport.resetChannel.mockResolvedValue(undefined);
|
|
58
|
+
transport.connect.mockImplementation(async () => { this.connectedAt.push(Date.now()); });
|
|
59
|
+
transport.getVersionInfo.mockResolvedValue({});
|
|
60
|
+
transport.allEventStores.mockResolvedValue({ IsAuthorized: true, Data: [] });
|
|
61
|
+
this.client = new ChronicleClient(ChronicleOptions.fromConnectionString('chronicle://localhost:35000', { discoveryPatterns: [] }),
|
|
62
|
+
{ now: () => Date.now(), nextJitter: () => 0 });
|
|
63
|
+
await this.client.getEventStores();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Reports, from an observation stream of the given session, that the kernel lost it. */
|
|
67
|
+
loseSession(connectionId: string = this.lifecycle.connectionId): void {
|
|
68
|
+
this.lifecycle.requestReconnect(connectionId, 'reactor-session-lost', sessionLost(), ignore);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Gets the wait before each connect after the first. */
|
|
72
|
+
get waits(): number[] {
|
|
73
|
+
return this.connectedAt.slice(1).map((at, index) => at - this.connectedAt[index]);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
dispose(): void {
|
|
77
|
+
this.client.dispose();
|
|
78
|
+
vi.restoreAllMocks();
|
|
79
|
+
vi.useRealTimers();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
describe('when reconnecting because an observation stream lost its session', () => {
|
|
84
|
+
let context: a_connected_client;
|
|
85
|
+
let connectionIdBefore: string;
|
|
86
|
+
let disconnectedWith: string[];
|
|
87
|
+
|
|
88
|
+
beforeEach(async () => {
|
|
89
|
+
context = new a_connected_client();
|
|
90
|
+
await context.establish();
|
|
91
|
+
connectionIdBefore = context.lifecycle.connectionId;
|
|
92
|
+
disconnectedWith = [];
|
|
93
|
+
context.lifecycle.onDisconnected(async () => { disconnectedWith.push(context.lifecycle.connectionId); });
|
|
94
|
+
|
|
95
|
+
context.loseSession();
|
|
96
|
+
await vi.advanceTimersByTimeAsync(1_000);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
afterEach(() => context.dispose());
|
|
100
|
+
|
|
101
|
+
it('should drop the old session', () => disconnectedWith.should.deep.equal([connectionIdBefore]));
|
|
102
|
+
it('should connect again', () => transport.connect.mock.calls.should.have.lengthOf(2));
|
|
103
|
+
it('should reconnect with a new connection id', () => context.lifecycle.connectionId.should.not.equal(connectionIdBefore));
|
|
104
|
+
it('should be connected again', () => context.lifecycle.isConnected.should.be.true);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('when reconnecting because sessions keep being lost right after connecting', () => {
|
|
108
|
+
let context: a_connected_client;
|
|
109
|
+
|
|
110
|
+
beforeEach(async () => {
|
|
111
|
+
context = new a_connected_client();
|
|
112
|
+
await context.establish();
|
|
113
|
+
context.loseSession();
|
|
114
|
+
await vi.advanceTimersByTimeAsync(5_000);
|
|
115
|
+
context.loseSession();
|
|
116
|
+
await vi.advanceTimersByTimeAsync(5_000);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
afterEach(() => context.dispose());
|
|
120
|
+
|
|
121
|
+
it('should wait longer before each reconnect', () => context.waits.should.deep.equal([500, 5_000 + 1_000 - 500]));
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('when reconnecting because a session that stayed up was lost', () => {
|
|
125
|
+
let context: a_connected_client;
|
|
126
|
+
|
|
127
|
+
beforeEach(async () => {
|
|
128
|
+
context = new a_connected_client();
|
|
129
|
+
await context.establish();
|
|
130
|
+
context.loseSession();
|
|
131
|
+
await vi.advanceTimersByTimeAsync(500);
|
|
132
|
+
await vi.advanceTimersByTimeAsync(31_000);
|
|
133
|
+
context.loseSession();
|
|
134
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
afterEach(() => context.dispose());
|
|
138
|
+
|
|
139
|
+
it('should reconnect without waiting', () => context.waits.should.deep.equal([500, 31_000]));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe('when an observation stream reports a session that was already replaced', () => {
|
|
143
|
+
let context: a_connected_client;
|
|
144
|
+
|
|
145
|
+
beforeEach(async () => {
|
|
146
|
+
context = new a_connected_client();
|
|
147
|
+
await context.establish();
|
|
148
|
+
context.loseSession('a-connection-the-client-no-longer-uses');
|
|
149
|
+
await vi.advanceTimersByTimeAsync(5_000);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
afterEach(() => context.dispose());
|
|
153
|
+
|
|
154
|
+
it('should not reconnect', () => transport.connect.mock.calls.should.have.lengthOf(1));
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe('when many observation streams report the same lost session', () => {
|
|
158
|
+
let context: a_connected_client;
|
|
159
|
+
|
|
160
|
+
beforeEach(async () => {
|
|
161
|
+
context = new a_connected_client();
|
|
162
|
+
await context.establish();
|
|
163
|
+
const connectionId = context.lifecycle.connectionId;
|
|
164
|
+
for (let report = 0; report < 5; report++) context.loseSession(connectionId);
|
|
165
|
+
await vi.advanceTimersByTimeAsync(5_000);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
afterEach(() => context.dispose());
|
|
169
|
+
|
|
170
|
+
it('should reconnect once', () => transport.connect.mock.calls.should.have.lengthOf(2));
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe('when the new session is lost while the reconnect is still registering observers', () => {
|
|
174
|
+
let context: a_connected_client;
|
|
175
|
+
|
|
176
|
+
beforeEach(async () => {
|
|
177
|
+
context = new a_connected_client();
|
|
178
|
+
await context.establish();
|
|
179
|
+
let reported = false;
|
|
180
|
+
context.lifecycle.onConnected(async () => {
|
|
181
|
+
if (reported) return;
|
|
182
|
+
reported = true;
|
|
183
|
+
context.loseSession();
|
|
184
|
+
});
|
|
185
|
+
context.loseSession();
|
|
186
|
+
await vi.advanceTimersByTimeAsync(5_000);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
afterEach(() => context.dispose());
|
|
190
|
+
|
|
191
|
+
it('should reconnect again for the new session', () => transport.connect.mock.calls.should.have.lengthOf(3));
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe('when disposing the client while it waits to reconnect a lost session', () => {
|
|
195
|
+
let context: a_connected_client;
|
|
196
|
+
let timersAfterDispose: number;
|
|
197
|
+
|
|
198
|
+
beforeEach(async () => {
|
|
199
|
+
context = new a_connected_client();
|
|
200
|
+
await context.establish();
|
|
201
|
+
context.loseSession();
|
|
202
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
203
|
+
context.client.dispose();
|
|
204
|
+
timersAfterDispose = vi.getTimerCount();
|
|
205
|
+
await vi.advanceTimersByTimeAsync(5_000);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
afterEach(() => context.dispose());
|
|
209
|
+
|
|
210
|
+
it('should not connect again', () => transport.connect.mock.calls.should.have.lengthOf(1));
|
|
211
|
+
it('should leave no timer behind', () => timersAfterDispose.should.equal(0));
|
|
212
|
+
});
|
|
@@ -0,0 +1,263 @@
|
|
|
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 type { Guid } from '@cratis/fundamentals';
|
|
5
|
+
import type { ImportEvent } from '@cratis/chronicle.contracts';
|
|
6
|
+
import { toContractsGuid } from '../connection/Guid.js';
|
|
7
|
+
import { ensureCommandResponse, ensureQuerySuccess } from '../connection/callResults.js';
|
|
8
|
+
import type { EventForEventSourceId } from '../eventSequences/EventForEventSourceId.js';
|
|
9
|
+
import { delay, readUntilAborted, untilAborted } from './abortable.js';
|
|
10
|
+
import type { EventImportSettings } from './EventImportSettings.js';
|
|
11
|
+
import type { IEventImport } from './IEventImport.js';
|
|
12
|
+
import { ImportChunkRefused } from './ImportChunkRefused.js';
|
|
13
|
+
import type { ImportProgress } from './ImportProgress.js';
|
|
14
|
+
import type { ImportResult } from './ImportResult.js';
|
|
15
|
+
import { ImportState } from './ImportState.js';
|
|
16
|
+
import { ImportWasAbandoned } from './ImportWasAbandoned.js';
|
|
17
|
+
import { isTransientImportFailure } from './isTransientImportFailure.js';
|
|
18
|
+
import { sizeOfImportEvent } from './sizeOfImportEvent.js';
|
|
19
|
+
import { toImportProgress } from './toImportProgress.js';
|
|
20
|
+
import { toImportResult } from './toImportResult.js';
|
|
21
|
+
|
|
22
|
+
const defaultMaxChunksInFlight = 4;
|
|
23
|
+
const defaultMaxChunkAttempts = 5;
|
|
24
|
+
const defaultPollInterval = 500;
|
|
25
|
+
const terminalStates = new Set([ImportState.Completed, ImportState.Failed, ImportState.Abandoned]);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Represents an implementation of {@link IEventImport} that cuts events into chunks within the kernel's limits and
|
|
29
|
+
* keeps a bounded number of them in flight.
|
|
30
|
+
*/
|
|
31
|
+
export class EventImport implements IEventImport {
|
|
32
|
+
readonly importId: Guid;
|
|
33
|
+
readonly eventsAcknowledged: number;
|
|
34
|
+
|
|
35
|
+
readonly #settings: EventImportSettings;
|
|
36
|
+
readonly #maxEventsPerChunk: number;
|
|
37
|
+
readonly #maxBytesPerChunk: number;
|
|
38
|
+
readonly #maxChunksInFlight: number;
|
|
39
|
+
readonly #maxChunkAttempts: number;
|
|
40
|
+
readonly #retryDelay: (attempt: number) => number;
|
|
41
|
+
readonly #abandoning = new AbortController();
|
|
42
|
+
readonly #sends = new Set<Promise<void>>();
|
|
43
|
+
readonly #slotWaiters: (() => void)[] = [];
|
|
44
|
+
#buffer: ImportEvent[] = [];
|
|
45
|
+
#bufferBytes = 0;
|
|
46
|
+
#nextChunkIndex: number;
|
|
47
|
+
#nextEventIndex: number;
|
|
48
|
+
#chunksInFlight = 0;
|
|
49
|
+
#exclusive: Promise<void> = Promise.resolve();
|
|
50
|
+
#failure: { error: unknown } | undefined;
|
|
51
|
+
#abandoned = false;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Creates a new {@link EventImport}.
|
|
55
|
+
* @param settings - The {@link EventImportSettings}.
|
|
56
|
+
*/
|
|
57
|
+
constructor(settings: EventImportSettings) {
|
|
58
|
+
this.#settings = settings;
|
|
59
|
+
this.importId = settings.importId;
|
|
60
|
+
this.eventsAcknowledged = settings.acknowledgedEvents ?? 0;
|
|
61
|
+
this.#nextChunkIndex = settings.acknowledgedChunks ?? 0;
|
|
62
|
+
this.#nextEventIndex = settings.acknowledgedEvents ?? 0;
|
|
63
|
+
const serverEvents = settings.maxChunkEvents > 0 ? settings.maxChunkEvents : Number.POSITIVE_INFINITY;
|
|
64
|
+
this.#maxEventsPerChunk = Math.max(1, Math.min(serverEvents, settings.options.maxEventsPerChunk ?? Number.POSITIVE_INFINITY));
|
|
65
|
+
this.#maxBytesPerChunk = settings.maxChunkBytes > 0 ? settings.maxChunkBytes : Number.POSITIVE_INFINITY;
|
|
66
|
+
this.#maxChunksInFlight = Math.max(1, settings.options.maxChunksInFlight ?? defaultMaxChunksInFlight);
|
|
67
|
+
this.#maxChunkAttempts = Math.max(1, settings.options.maxChunkAttempts ?? defaultMaxChunkAttempts);
|
|
68
|
+
this.#retryDelay = settings.retryDelay ?? (attempt => Math.min(5000, 200 * attempt * attempt));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** @inheritdoc */
|
|
72
|
+
async *progress(signal?: AbortSignal): AsyncIterable<ImportProgress> {
|
|
73
|
+
try {
|
|
74
|
+
for await (const result of this.#settings.imports.observeImport(this.#identity(), { signal })) {
|
|
75
|
+
if (result.Data) yield toImportProgress(result.Data);
|
|
76
|
+
}
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (signal?.aborted) return;
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** @inheritdoc */
|
|
84
|
+
add(events: AsyncIterable<EventForEventSourceId> | Iterable<EventForEventSourceId>, signal?: AbortSignal): Promise<void> {
|
|
85
|
+
return this.#whileNotAbandoned(async waiting => {
|
|
86
|
+
for await (const event of readUntilAborted(events, waiting)) {
|
|
87
|
+
await this.#bufferEvent(event, waiting);
|
|
88
|
+
}
|
|
89
|
+
}, signal);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** @inheritdoc */
|
|
93
|
+
async complete(signal?: AbortSignal): Promise<ImportResult> {
|
|
94
|
+
let result: ImportResult | undefined;
|
|
95
|
+
await this.#whileNotAbandoned(async waiting => {
|
|
96
|
+
await this.#flush(waiting);
|
|
97
|
+
await untilAborted(Promise.all([...this.#sends]), waiting);
|
|
98
|
+
this.#throwIfFailed();
|
|
99
|
+
this.#throwIfAbandoned();
|
|
100
|
+
|
|
101
|
+
const status = ensureCommandResponse('complete import', await this.#settings.imports.completeImport({
|
|
102
|
+
...this.#identity(),
|
|
103
|
+
ExpectedChunks: this.#nextChunkIndex,
|
|
104
|
+
ExpectedEvents: BigInt(this.#nextEventIndex)
|
|
105
|
+
}, { signal: waiting }));
|
|
106
|
+
|
|
107
|
+
result = !status.Error && (this.#settings.options.waitForCommit ?? true)
|
|
108
|
+
? await this.#waitForCommit(waiting)
|
|
109
|
+
: await this.#read(waiting) ?? this.#resultFrom(status.State as number as ImportState, status.Error);
|
|
110
|
+
}, signal);
|
|
111
|
+
return result!;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @inheritdoc */
|
|
115
|
+
async abandon(signal?: AbortSignal): Promise<ImportResult> {
|
|
116
|
+
// No chunk is sent once this is set, and an add in progress is stopped rather than waited for: it may be reading
|
|
117
|
+
// a source that never yields again.
|
|
118
|
+
this.#abandoned = true;
|
|
119
|
+
this.#abandoning.abort(new ImportWasAbandoned(this.importId));
|
|
120
|
+
|
|
121
|
+
// Chunks already handed to the kernel settle first, so none is staged after the import is abandoned.
|
|
122
|
+
await untilAborted(Promise.all([...this.#sends]), signal);
|
|
123
|
+
const status = ensureCommandResponse('abandon import',
|
|
124
|
+
await this.#settings.imports.abandonImport(this.#identity(), { signal }));
|
|
125
|
+
return await this.#read(signal) ?? this.#resultFrom(status.State as number as ImportState, status.Error);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#identity() {
|
|
129
|
+
return { EventStore: this.#settings.eventStore, Namespace: this.#settings.namespace, ImportId: toContractsGuid(this.importId) };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async #whileNotAbandoned(work: (waiting: AbortSignal) => Promise<void>, signal?: AbortSignal): Promise<void> {
|
|
133
|
+
this.#throwIfAbandoned();
|
|
134
|
+
const waiting = signal ? AbortSignal.any([signal, this.#abandoning.signal]) : this.#abandoning.signal;
|
|
135
|
+
// Adds and completion run one at a time, so chunks are cut from the events in the order they were added.
|
|
136
|
+
const run = this.#exclusive.then(() => {
|
|
137
|
+
waiting.throwIfAborted();
|
|
138
|
+
return work(waiting);
|
|
139
|
+
});
|
|
140
|
+
this.#exclusive = run.catch(() => undefined);
|
|
141
|
+
try {
|
|
142
|
+
await run;
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (this.#abandoning.signal.aborted && !signal?.aborted) throw new ImportWasAbandoned(this.importId);
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async #bufferEvent(event: EventForEventSourceId, waiting: AbortSignal): Promise<void> {
|
|
150
|
+
this.#throwIfFailed();
|
|
151
|
+
this.#throwIfAbandoned();
|
|
152
|
+
const converted = this.#settings.convert(event);
|
|
153
|
+
const bytes = sizeOfImportEvent(converted);
|
|
154
|
+
if (this.#buffer.length > 0 &&
|
|
155
|
+
(this.#buffer.length >= this.#maxEventsPerChunk || this.#bufferBytes + bytes > this.#maxBytesPerChunk)) {
|
|
156
|
+
await this.#flush(waiting);
|
|
157
|
+
}
|
|
158
|
+
this.#buffer.push(converted);
|
|
159
|
+
this.#bufferBytes += bytes;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async #flush(waiting: AbortSignal): Promise<void> {
|
|
163
|
+
if (this.#buffer.length === 0) return;
|
|
164
|
+
await this.#acquireSlot(waiting);
|
|
165
|
+
// Checked after the wait, so an abandon sees every chunk sent before it and none is sent after it.
|
|
166
|
+
if (this.#abandoned) {
|
|
167
|
+
this.#releaseSlot();
|
|
168
|
+
throw new ImportWasAbandoned(this.importId);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const events = this.#buffer;
|
|
172
|
+
const chunkIndex = this.#nextChunkIndex++;
|
|
173
|
+
const firstEventIndex = this.#nextEventIndex;
|
|
174
|
+
this.#nextEventIndex += events.length;
|
|
175
|
+
this.#buffer = [];
|
|
176
|
+
this.#bufferBytes = 0;
|
|
177
|
+
|
|
178
|
+
const send: Promise<void> = this.#send(chunkIndex, firstEventIndex, events).finally(() => this.#sends.delete(send));
|
|
179
|
+
this.#sends.add(send);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async #send(chunkIndex: number, firstEventIndex: number, events: ImportEvent[]): Promise<void> {
|
|
183
|
+
try {
|
|
184
|
+
for (let attempt = 1; ; attempt++) {
|
|
185
|
+
try {
|
|
186
|
+
const result = ensureCommandResponse('add import chunk', await this.#settings.imports.addImportChunk({
|
|
187
|
+
...this.#identity(),
|
|
188
|
+
ChunkIndex: chunkIndex,
|
|
189
|
+
FirstEventIndex: BigInt(firstEventIndex),
|
|
190
|
+
Events: events
|
|
191
|
+
}));
|
|
192
|
+
if (result.Error) throw new ImportChunkRefused(chunkIndex, result.Error);
|
|
193
|
+
return;
|
|
194
|
+
} catch (error) {
|
|
195
|
+
if (!isTransientImportFailure(error) || attempt >= this.#maxChunkAttempts) throw error;
|
|
196
|
+
// Sending the chunk again is safe: the kernel stages a chunk under its index once.
|
|
197
|
+
await delay(this.#retryDelay(attempt), this.#abandoning.signal);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
} catch (error) {
|
|
201
|
+
this.#failure ??= { error };
|
|
202
|
+
} finally {
|
|
203
|
+
this.#releaseSlot();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
#acquireSlot(waiting: AbortSignal): Promise<void> {
|
|
208
|
+
if (this.#chunksInFlight < this.#maxChunksInFlight) {
|
|
209
|
+
this.#chunksInFlight++;
|
|
210
|
+
return Promise.resolve();
|
|
211
|
+
}
|
|
212
|
+
return new Promise<void>((resolve, reject) => {
|
|
213
|
+
const granted = () => {
|
|
214
|
+
waiting.removeEventListener('abort', aborted);
|
|
215
|
+
resolve();
|
|
216
|
+
};
|
|
217
|
+
const aborted = () => {
|
|
218
|
+
const index = this.#slotWaiters.indexOf(granted);
|
|
219
|
+
if (index >= 0) this.#slotWaiters.splice(index, 1);
|
|
220
|
+
reject(waiting.reason);
|
|
221
|
+
};
|
|
222
|
+
if (waiting.aborted) return reject(waiting.reason);
|
|
223
|
+
waiting.addEventListener('abort', aborted, { once: true });
|
|
224
|
+
this.#slotWaiters.push(granted);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
#releaseSlot(): void {
|
|
229
|
+
// A waiting chunk takes over the slot, so the number in flight never exceeds the bound.
|
|
230
|
+
const next = this.#slotWaiters.shift();
|
|
231
|
+
if (next) next();
|
|
232
|
+
else this.#chunksInFlight--;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
#throwIfAbandoned(): void {
|
|
236
|
+
if (this.#abandoned) throw new ImportWasAbandoned(this.importId);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
#throwIfFailed(): void {
|
|
240
|
+
if (this.#failure) throw this.#failure.error;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async #waitForCommit(waiting: AbortSignal): Promise<ImportResult> {
|
|
244
|
+
const pollInterval = this.#settings.pollInterval ?? defaultPollInterval;
|
|
245
|
+
while (true) {
|
|
246
|
+
const result = await this.#read(waiting);
|
|
247
|
+
if (result && terminalStates.has(result.state)) return result;
|
|
248
|
+
await delay(pollInterval, waiting);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async #read(signal?: AbortSignal): Promise<ImportResult | undefined> {
|
|
253
|
+
const progress = ensureQuerySuccess('get import', await this.#settings.imports.getImport(this.#identity(), { signal }));
|
|
254
|
+
return progress ? toImportResult(progress) : undefined;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
#resultFrom(state: ImportState, reason: string): ImportResult {
|
|
258
|
+
return {
|
|
259
|
+
importId: this.importId, state, eventsStaged: 0, eventsCommitted: 0, alreadyAppended: 0,
|
|
260
|
+
rejectedCount: 0, rejections: [], failureReason: reason ? reason : undefined, isSuccess: state === ImportState.Completed
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
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 type { Guid } from '@cratis/fundamentals';
|
|
5
|
+
import type { ImportEvent, ImportsClient } from '@cratis/chronicle.contracts';
|
|
6
|
+
import type { EventForEventSourceId } from '../eventSequences/EventForEventSourceId.js';
|
|
7
|
+
import type { ImportOptions } from './ImportOptions.js';
|
|
8
|
+
|
|
9
|
+
/** What an {@link EventImport} session is created with. */
|
|
10
|
+
export interface EventImportSettings {
|
|
11
|
+
/** The Imports service. */
|
|
12
|
+
readonly imports: ImportsClient;
|
|
13
|
+
|
|
14
|
+
/** The name of the event store. */
|
|
15
|
+
readonly eventStore: string;
|
|
16
|
+
|
|
17
|
+
/** The namespace. */
|
|
18
|
+
readonly namespace: string;
|
|
19
|
+
|
|
20
|
+
/** The identifier of the import. */
|
|
21
|
+
readonly importId: Guid;
|
|
22
|
+
|
|
23
|
+
/** The {@link ImportOptions}. */
|
|
24
|
+
readonly options: ImportOptions;
|
|
25
|
+
|
|
26
|
+
/** The most events the kernel accepts in one chunk; zero or less means no limit. */
|
|
27
|
+
readonly maxChunkEvents: number;
|
|
28
|
+
|
|
29
|
+
/** The most bytes of event content the kernel accepts in one chunk; zero or less means no limit. */
|
|
30
|
+
readonly maxChunkBytes: number;
|
|
31
|
+
|
|
32
|
+
/** Converts an event to its wire form. */
|
|
33
|
+
readonly convert: (event: EventForEventSourceId) => ImportEvent;
|
|
34
|
+
|
|
35
|
+
/** How many chunks the kernel already holds without a gap, when reattaching. */
|
|
36
|
+
readonly acknowledgedChunks?: number;
|
|
37
|
+
|
|
38
|
+
/** How many events those chunks carry, when reattaching. */
|
|
39
|
+
readonly acknowledgedEvents?: number;
|
|
40
|
+
|
|
41
|
+
/** The delay, in milliseconds, before an attempt to send a chunk again. */
|
|
42
|
+
readonly retryDelay?: (attempt: number) => number;
|
|
43
|
+
|
|
44
|
+
/** The interval, in milliseconds, between reads of the import while waiting for its commit. */
|
|
45
|
+
readonly pollInterval?: number;
|
|
46
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
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 type { Guid } from '@cratis/fundamentals';
|
|
5
|
+
import type { EventForEventSourceId } from '../eventSequences/EventForEventSourceId.js';
|
|
6
|
+
import type { ImportProgress } from './ImportProgress.js';
|
|
7
|
+
import type { ImportResult } from './ImportResult.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Defines an import session that events are added to in chunks, for producers that are not a single stream.
|
|
11
|
+
* @remarks
|
|
12
|
+
* Events are cut into chunks within the kernel's limits and a bounded number of chunks are sent at the same time.
|
|
13
|
+
* Nothing is committed before {@link IEventImport.complete}. A session that is not completed stays open on the kernel,
|
|
14
|
+
* so it can be reattached by its {@link IEventImport.importId}; the kernel abandons it once its staging retention runs out.
|
|
15
|
+
*/
|
|
16
|
+
export interface IEventImport {
|
|
17
|
+
/** The identifier of the import. */
|
|
18
|
+
readonly importId: Guid;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* How many events the kernel already held, in order and without a gap, when this session was attached. A producer
|
|
22
|
+
* that reattaches continues after this many events; a new session starts at zero.
|
|
23
|
+
*/
|
|
24
|
+
readonly eventsAcknowledged: number;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Observes the progress of the import as the kernel reports it.
|
|
28
|
+
* @param signal - Ends the observation when aborted.
|
|
29
|
+
* @returns An async iterable of {@link ImportProgress}.
|
|
30
|
+
*/
|
|
31
|
+
progress(signal?: AbortSignal): AsyncIterable<ImportProgress>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Adds events to the import, reading them lazily.
|
|
35
|
+
* @param events - The events, in the order they are to be committed.
|
|
36
|
+
* @param signal - Stops adding when aborted.
|
|
37
|
+
* @returns A promise that resolves once the events are handed to the kernel within the in-flight bound.
|
|
38
|
+
*/
|
|
39
|
+
add(events: AsyncIterable<EventForEventSourceId> | Iterable<EventForEventSourceId>, signal?: AbortSignal): Promise<void>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Completes the import: waits for every chunk to be acknowledged, closes staging and starts the commit.
|
|
43
|
+
* @param signal - Stops waiting when aborted.
|
|
44
|
+
* @returns The {@link ImportResult}, once committed unless {@link ImportOptions.waitForCommit} is false.
|
|
45
|
+
*/
|
|
46
|
+
complete(signal?: AbortSignal): Promise<ImportResult>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Abandons the import, discarding everything staged. Nothing is committed.
|
|
50
|
+
* @param signal - Stops waiting for chunks already being sent; when it aborts first, the import is not abandoned on
|
|
51
|
+
* the kernel, which abandons it once its staging retention runs out.
|
|
52
|
+
* @returns The {@link ImportResult}.
|
|
53
|
+
* @remarks
|
|
54
|
+
* No chunk is sent once abandoning begins. An {@link IEventImport.add} in progress is stopped rather than waited for,
|
|
55
|
+
* and ends with {@link ImportWasAbandoned}. Adding to or completing the import afterwards throws {@link ImportWasAbandoned}.
|
|
56
|
+
*/
|
|
57
|
+
abandon(signal?: AbortSignal): Promise<ImportResult>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
/** The exception that is thrown when the kernel refuses a chunk of an import. */
|
|
5
|
+
export class ImportChunkRefused extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new {@link ImportChunkRefused}.
|
|
8
|
+
* @param chunkIndex - The index of the chunk.
|
|
9
|
+
* @param reason - Why the kernel refused it.
|
|
10
|
+
*/
|
|
11
|
+
constructor(readonly chunkIndex: number, readonly reason: string) {
|
|
12
|
+
super(`The kernel refused chunk ${chunkIndex} of the import: ${reason}`);
|
|
13
|
+
this.name = 'ImportChunkRefused';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
/** The exception that is thrown when an import could not begin or be reattached. */
|
|
5
|
+
export class ImportCouldNotBegin extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new {@link ImportCouldNotBegin}.
|
|
8
|
+
* @param reason - Why the import could not begin.
|
|
9
|
+
*/
|
|
10
|
+
constructor(readonly reason: string) {
|
|
11
|
+
super(`The import could not begin: ${reason}`);
|
|
12
|
+
this.name = 'ImportCouldNotBegin';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
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 type { Guid } from '@cratis/fundamentals';
|
|
5
|
+
import type { ImportProgress } from './ImportProgress.js';
|
|
6
|
+
import type { OnInvalidEvent } from './OnInvalidEvent.js';
|
|
7
|
+
|
|
8
|
+
/** Options for importing events into an event sequence. */
|
|
9
|
+
export interface ImportOptions {
|
|
10
|
+
/** What the import does when one of its events is invalid. Defaults to {@link OnInvalidEvent.Reject}: nothing is committed. */
|
|
11
|
+
readonly onInvalid?: OnInvalidEvent;
|
|
12
|
+
|
|
13
|
+
/** Whether completing the import waits until it is committed. Defaults to true. */
|
|
14
|
+
readonly waitForCommit?: boolean;
|
|
15
|
+
|
|
16
|
+
/** How many chunks are sent to the kernel at the same time. Defaults to 4. */
|
|
17
|
+
readonly maxChunksInFlight?: number;
|
|
18
|
+
|
|
19
|
+
/** The most events the client puts in one chunk; the kernel's own limit applies when it is lower. */
|
|
20
|
+
readonly maxEventsPerChunk?: number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The identifier to give the import; a new one is created when not set. Beginning an import with an identifier
|
|
24
|
+
* that already exists returns that import.
|
|
25
|
+
*/
|
|
26
|
+
readonly importId?: string | Guid;
|
|
27
|
+
|
|
28
|
+
/** How many times a chunk is sent after the connection fails. Resending is safe: the kernel stages a chunk once. Defaults to 5. */
|
|
29
|
+
readonly maxChunkAttempts?: number;
|
|
30
|
+
|
|
31
|
+
/** Cancels the import. Chunks the kernel already holds stay staged, so the import can be reattached by its identifier. */
|
|
32
|
+
readonly signal?: AbortSignal;
|
|
33
|
+
|
|
34
|
+
/** Called with the progress of the import as the kernel reports it, until the import call returns. */
|
|
35
|
+
readonly onProgress?: (progress: ImportProgress) => void;
|
|
36
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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 type { Guid } from '@cratis/fundamentals';
|
|
5
|
+
import type { ImportState } from './ImportState.js';
|
|
6
|
+
|
|
7
|
+
/** The progress of an import as the kernel reports it. */
|
|
8
|
+
export interface ImportProgress {
|
|
9
|
+
/** The identifier of the import. */
|
|
10
|
+
readonly importId: Guid;
|
|
11
|
+
|
|
12
|
+
/** The state the import is in. */
|
|
13
|
+
readonly state: ImportState;
|
|
14
|
+
|
|
15
|
+
/** How many events the kernel has received. */
|
|
16
|
+
readonly eventsReceived: number;
|
|
17
|
+
|
|
18
|
+
/** How many events are staged. */
|
|
19
|
+
readonly eventsStaged: number;
|
|
20
|
+
|
|
21
|
+
/** How many events are committed. */
|
|
22
|
+
readonly eventsCommitted: number;
|
|
23
|
+
|
|
24
|
+
/** How many events were already appended under their idempotency key, and so not appended again. */
|
|
25
|
+
readonly alreadyAppended: number;
|
|
26
|
+
|
|
27
|
+
/** How many events were rejected. */
|
|
28
|
+
readonly eventsRejected: number;
|
|
29
|
+
}
|