@typeonce/effect-machine 0.47.0 → 0.48.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/AGENTS.md +374 -22
- package/CLAUDE.md +374 -22
- package/README.md +56 -7
- package/ai-docs/src/10_modeling/01_create-a-machine.ts +3 -3
- package/ai-docs/src/10_modeling/index.md +64 -17
- package/ai-docs/src/20_root-api/index.md +2 -2
- package/ai-docs/src/35_cluster/index.md +213 -0
- package/ai-docs/src/37_new-host/index.md +90 -0
- package/dist/LocalMachine.d.ts +417 -0
- package/dist/LocalMachine.d.ts.map +1 -0
- package/dist/LocalMachine.js +193 -0
- package/dist/LocalMachine.js.map +1 -0
- package/dist/Machine.d.ts +1122 -814
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +195 -186
- package/dist/Machine.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/machine/atom.d.ts +8 -10
- package/dist/internal/machine/atom.d.ts.map +1 -1
- package/dist/internal/machine/atom.js +7 -6
- package/dist/internal/machine/atom.js.map +1 -1
- package/dist/internal/machine/capability.d.ts +101 -0
- package/dist/internal/machine/capability.d.ts.map +1 -0
- package/dist/internal/machine/capability.js +70 -0
- package/dist/internal/machine/capability.js.map +1 -0
- package/dist/internal/machine/{invocationDefinition.d.ts → capturedInvocation.d.ts} +11 -11
- package/dist/internal/machine/capturedInvocation.d.ts.map +1 -0
- package/dist/internal/machine/{invocationDefinition.js → capturedInvocation.js} +2 -2
- package/dist/internal/machine/capturedInvocation.js.map +1 -0
- package/dist/internal/machine/child.d.ts +14 -0
- package/dist/internal/machine/child.d.ts.map +1 -0
- package/dist/internal/machine/child.js +19 -0
- package/dist/internal/machine/child.js.map +1 -0
- package/dist/internal/machine/childRegistry.d.ts +0 -2
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/childRegistry.js +2 -2
- package/dist/internal/machine/childRegistry.js.map +1 -1
- package/dist/internal/machine/cluster.d.ts +19 -4
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +263 -110
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterChild.d.ts +66 -0
- package/dist/internal/machine/clusterChild.d.ts.map +1 -0
- package/dist/internal/machine/clusterChild.js +105 -0
- package/dist/internal/machine/clusterChild.js.map +1 -0
- package/dist/internal/machine/clusterFailure.d.ts +14 -0
- package/dist/internal/machine/clusterFailure.d.ts.map +1 -0
- package/dist/internal/machine/clusterFailure.js +44 -0
- package/dist/internal/machine/clusterFailure.js.map +1 -0
- package/dist/internal/machine/clusterInstance.d.ts +194 -0
- package/dist/internal/machine/clusterInstance.d.ts.map +1 -0
- package/dist/internal/machine/clusterInstance.js +1076 -0
- package/dist/internal/machine/clusterInstance.js.map +1 -0
- package/dist/internal/machine/clusterKeepAlive.d.ts +47 -0
- package/dist/internal/machine/clusterKeepAlive.d.ts.map +1 -0
- package/dist/internal/machine/clusterKeepAlive.js +72 -0
- package/dist/internal/machine/clusterKeepAlive.js.map +1 -0
- package/dist/internal/machine/clusterProgress.d.ts +73 -0
- package/dist/internal/machine/clusterProgress.d.ts.map +1 -0
- package/dist/internal/machine/clusterProgress.js +116 -0
- package/dist/internal/machine/clusterProgress.js.map +1 -0
- package/dist/internal/machine/clusterProtocol.d.ts +37 -4
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -1
- package/dist/internal/machine/clusterProtocol.js +18 -4
- package/dist/internal/machine/clusterProtocol.js.map +1 -1
- package/dist/internal/machine/clusterProxy.d.ts +46 -0
- package/dist/internal/machine/clusterProxy.d.ts.map +1 -0
- package/dist/internal/machine/clusterProxy.js +150 -0
- package/dist/internal/machine/clusterProxy.js.map +1 -0
- package/dist/internal/machine/clusterRead.d.ts +61 -0
- package/dist/internal/machine/clusterRead.d.ts.map +1 -0
- package/dist/internal/machine/clusterRead.js +108 -0
- package/dist/internal/machine/clusterRead.js.map +1 -0
- package/dist/internal/machine/clusterSqlStorage.d.ts +16 -0
- package/dist/internal/machine/clusterSqlStorage.d.ts.map +1 -0
- package/dist/internal/machine/clusterSqlStorage.js +138 -0
- package/dist/internal/machine/clusterSqlStorage.js.map +1 -0
- package/dist/internal/machine/clusterStorageCheck.d.ts +19 -0
- package/dist/internal/machine/clusterStorageCheck.d.ts.map +1 -0
- package/dist/internal/machine/clusterStorageCheck.js +186 -0
- package/dist/internal/machine/clusterStorageCheck.js.map +1 -0
- package/dist/internal/machine/clusterTarget.d.ts +74 -0
- package/dist/internal/machine/clusterTarget.d.ts.map +1 -0
- package/dist/internal/machine/clusterTarget.js +81 -0
- package/dist/internal/machine/clusterTarget.js.map +1 -0
- package/dist/internal/machine/clusterTimer.d.ts +21 -0
- package/dist/internal/machine/clusterTimer.d.ts.map +1 -0
- package/dist/internal/machine/clusterTimer.js +50 -0
- package/dist/internal/machine/clusterTimer.js.map +1 -0
- package/dist/internal/machine/clusterWake.d.ts +61 -0
- package/dist/internal/machine/clusterWake.d.ts.map +1 -0
- package/dist/internal/machine/clusterWake.js +81 -0
- package/dist/internal/machine/clusterWake.js.map +1 -0
- package/dist/internal/machine/clusterWork.d.ts +130 -0
- package/dist/internal/machine/clusterWork.d.ts.map +1 -0
- package/dist/internal/machine/clusterWork.js +191 -0
- package/dist/internal/machine/clusterWork.js.map +1 -0
- package/dist/internal/machine/command.d.ts +4 -4
- package/dist/internal/machine/command.d.ts.map +1 -1
- package/dist/internal/machine/commandRuntime.d.ts +8 -6
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/commandRuntime.js +1 -5
- package/dist/internal/machine/commandRuntime.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts +0 -13
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +0 -151
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/errors.d.ts +11 -2
- package/dist/internal/machine/errors.d.ts.map +1 -1
- package/dist/internal/machine/errors.js +11 -2
- package/dist/internal/machine/errors.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts +45 -3
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +328 -191
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +6 -6
- package/dist/internal/machine/implementation.d.ts.map +1 -1
- package/dist/internal/machine/inspectionRuntime.d.ts +1 -1
- package/dist/internal/machine/inspectionRuntime.d.ts.map +1 -1
- package/dist/internal/machine/invocationChanges.d.ts +60 -0
- package/dist/internal/machine/invocationChanges.d.ts.map +1 -0
- package/dist/internal/machine/invocationChanges.js +54 -0
- package/dist/internal/machine/invocationChanges.js.map +1 -0
- package/dist/internal/machine/{activities.d.ts → invocationDefinitions.d.ts} +8 -8
- package/dist/internal/machine/invocationDefinitions.d.ts.map +1 -0
- package/dist/internal/machine/{activities.js → invocationDefinitions.js} +5 -5
- package/dist/internal/machine/invocationDefinitions.js.map +1 -0
- package/dist/internal/machine/invocationEvent.d.ts +16 -7
- package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
- package/dist/internal/machine/invocationEvent.js +26 -12
- package/dist/internal/machine/invocationEvent.js.map +1 -1
- package/dist/internal/machine/invocationOutcome.d.ts +89 -0
- package/dist/internal/machine/invocationOutcome.d.ts.map +1 -0
- package/dist/internal/machine/invocationOutcome.js +76 -0
- package/dist/internal/machine/invocationOutcome.js.map +1 -0
- package/dist/internal/machine/invocationResolution.d.ts +137 -0
- package/dist/internal/machine/invocationResolution.d.ts.map +1 -0
- package/dist/internal/machine/invocationResolution.js +150 -0
- package/dist/internal/machine/invocationResolution.js.map +1 -0
- package/dist/internal/machine/logicScope.d.ts +24 -0
- package/dist/internal/machine/logicScope.d.ts.map +1 -0
- package/dist/internal/machine/logicScope.js +33 -0
- package/dist/internal/machine/logicScope.js.map +1 -0
- package/dist/internal/machine/machine.d.ts +17 -23
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +24 -40
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/observation.d.ts +3 -0
- package/dist/internal/machine/observation.d.ts.map +1 -1
- package/dist/internal/machine/observation.js +56 -2
- package/dist/internal/machine/observation.js.map +1 -1
- package/dist/internal/machine/plannedSnapshot.d.ts.map +1 -1
- package/dist/internal/machine/plannedSnapshot.js +11 -20
- package/dist/internal/machine/plannedSnapshot.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +36 -23
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +15 -8
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +17 -9
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +43 -503
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/protocol.d.ts +1 -1
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.js +48 -10
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +20 -16
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +93 -88
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -1
- package/dist/internal/machine/runtimeCompiled.js +359 -120
- package/dist/internal/machine/runtimeCompiled.js.map +1 -1
- package/dist/internal/machine/runtimeProtocol.d.ts +151 -82
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
- package/dist/internal/machine/runtimeProtocol.js +236 -219
- package/dist/internal/machine/runtimeProtocol.js.map +1 -1
- package/dist/internal/machine/stateDefinition.d.ts +0 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +0 -1
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/step.d.ts +486 -0
- package/dist/internal/machine/step.d.ts.map +1 -0
- package/dist/internal/machine/step.js +957 -0
- package/dist/internal/machine/step.js.map +1 -0
- package/dist/internal/machine/stepHost.d.ts +33 -0
- package/dist/internal/machine/stepHost.d.ts.map +1 -0
- package/dist/internal/machine/stepHost.js +501 -0
- package/dist/internal/machine/stepHost.js.map +1 -0
- package/dist/internal/machine/symbols.d.ts +2 -11
- package/dist/internal/machine/symbols.d.ts.map +1 -1
- package/dist/internal/machine/symbols.js +2 -2
- package/dist/internal/machine/symbols.js.map +1 -1
- package/dist/internal/machine/transition.d.ts +7 -7
- package/dist/internal/machine/transition.d.ts.map +1 -1
- package/dist/internal/testing/machine/check.d.ts.map +1 -1
- package/dist/internal/testing/machine/check.js +217 -25
- package/dist/internal/testing/machine/check.js.map +1 -1
- package/dist/internal/testing/machine/exploration.d.ts +19 -2
- package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
- package/dist/internal/testing/machine/exploration.js +14 -3
- package/dist/internal/testing/machine/exploration.js.map +1 -1
- package/dist/internal/testing/machine/format.js.map +1 -1
- package/dist/internal/testing/machine/probe.d.ts.map +1 -1
- package/dist/internal/testing/machine/probe.js +1 -1
- package/dist/internal/testing/machine/probe.js.map +1 -1
- package/dist/internal/testing/machine/runtime.d.ts +2 -2
- package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
- package/dist/internal/testing/machine/runtime.js +10 -3
- package/dist/internal/testing/machine/runtime.js.map +1 -1
- package/dist/internal/testing/machine/trace.d.ts +38 -3
- package/dist/internal/testing/machine/trace.d.ts.map +1 -1
- package/dist/internal/testing/machine/trace.js +180 -33
- package/dist/internal/testing/machine/trace.js.map +1 -1
- package/dist/internal/testing/machine/transitionCoverage.d.ts +1 -1
- package/dist/internal/testing/machine/transitionCoverage.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +9 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/internal/testing/machine/work.d.ts +47 -0
- package/dist/internal/testing/machine/work.d.ts.map +1 -0
- package/dist/internal/testing/machine/work.js +74 -0
- package/dist/internal/testing/machine/work.js.map +1 -0
- package/dist/testing/MachineTest.d.ts +253 -37
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +81 -9
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +1071 -41
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +441 -12
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachineProxy.d.ts +257 -0
- package/dist/unstable/cluster/ClusterMachineProxy.d.ts.map +1 -0
- package/dist/unstable/cluster/ClusterMachineProxy.js +73 -0
- package/dist/unstable/cluster/ClusterMachineProxy.js.map +1 -0
- package/dist/unstable/cluster/SqlMachineStorage.d.ts +125 -0
- package/dist/unstable/cluster/SqlMachineStorage.d.ts.map +1 -0
- package/dist/unstable/cluster/SqlMachineStorage.js +62 -0
- package/dist/unstable/cluster/SqlMachineStorage.js.map +1 -0
- package/dist/unstable/cluster/index.d.ts +13 -0
- package/dist/unstable/cluster/index.d.ts.map +1 -1
- package/dist/unstable/cluster/index.js +13 -0
- package/dist/unstable/cluster/index.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +21 -26
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/package.json +5 -3
- package/src/LocalMachine.ts +611 -0
- package/src/Machine.ts +1383 -1336
- package/src/index.ts +1 -0
- package/src/internal/machine/atom.ts +20 -49
- package/src/internal/machine/capability.ts +123 -0
- package/src/internal/machine/{invocationDefinition.ts → capturedInvocation.ts} +13 -13
- package/src/internal/machine/child.ts +25 -0
- package/src/internal/machine/childRegistry.ts +1 -4
- package/src/internal/machine/cluster.ts +420 -205
- package/src/internal/machine/clusterChild.ts +117 -0
- package/src/internal/machine/clusterFailure.ts +53 -0
- package/src/internal/machine/clusterInstance.ts +1779 -0
- package/src/internal/machine/clusterKeepAlive.ts +93 -0
- package/src/internal/machine/clusterProgress.ts +159 -0
- package/src/internal/machine/clusterProtocol.ts +46 -4
- package/src/internal/machine/clusterProxy.ts +233 -0
- package/src/internal/machine/clusterRead.ts +207 -0
- package/src/internal/machine/clusterSqlStorage.ts +181 -0
- package/src/internal/machine/clusterStorageCheck.ts +274 -0
- package/src/internal/machine/clusterTarget.ts +154 -0
- package/src/internal/machine/clusterTimer.ts +58 -0
- package/src/internal/machine/clusterWake.ts +100 -0
- package/src/internal/machine/clusterWork.ts +269 -0
- package/src/internal/machine/command.ts +4 -4
- package/src/internal/machine/commandRuntime.ts +12 -24
- package/src/internal/machine/configuration.ts +0 -181
- package/src/internal/machine/errors.ts +11 -2
- package/src/internal/machine/executionPlan.ts +493 -263
- package/src/internal/machine/implementation.ts +6 -6
- package/src/internal/machine/inspectionRuntime.ts +1 -1
- package/src/internal/machine/invocationChanges.ts +89 -0
- package/src/internal/machine/{activities.ts → invocationDefinitions.ts} +10 -10
- package/src/internal/machine/invocationEvent.ts +35 -12
- package/src/internal/machine/invocationOutcome.ts +145 -0
- package/src/internal/machine/invocationResolution.ts +307 -0
- package/src/internal/machine/logicScope.ts +47 -0
- package/src/internal/machine/machine.ts +35 -100
- package/src/internal/machine/observation.ts +67 -2
- package/src/internal/machine/plannedSnapshot.ts +17 -20
- package/src/internal/machine/planner.ts +75 -99
- package/src/internal/machine/process.ts +78 -722
- package/src/internal/machine/protocol.ts +70 -26
- package/src/internal/machine/runtime.ts +198 -195
- package/src/internal/machine/runtimeCompiled.ts +496 -171
- package/src/internal/machine/runtimeProtocol.ts +416 -305
- package/src/internal/machine/stateDefinition.ts +0 -5
- package/src/internal/machine/step.ts +1518 -0
- package/src/internal/machine/stepHost.ts +634 -0
- package/src/internal/machine/symbols.ts +3 -14
- package/src/internal/machine/transition.ts +11 -11
- package/src/internal/testing/machine/check.ts +240 -36
- package/src/internal/testing/machine/exploration.ts +42 -3
- package/src/internal/testing/machine/format.ts +1 -1
- package/src/internal/testing/machine/probe.ts +2 -1
- package/src/internal/testing/machine/runtime.ts +15 -5
- package/src/internal/testing/machine/trace.ts +261 -41
- package/src/internal/testing/machine/transitionCoverage.ts +1 -1
- package/src/internal/testing/machine/verification.ts +11 -5
- package/src/internal/testing/machine/work.ts +101 -0
- package/src/testing/MachineTest.ts +268 -44
- package/src/unstable/cluster/ClusterMachine.ts +1183 -59
- package/src/unstable/cluster/ClusterMachineProxy.ts +372 -0
- package/src/unstable/cluster/SqlMachineStorage.ts +136 -0
- package/src/unstable/cluster/index.ts +15 -0
- package/src/unstable/reactivity/AtomMachine.ts +27 -88
- package/dist/internal/machine/activities.d.ts.map +0 -1
- package/dist/internal/machine/activities.js.map +0 -1
- package/dist/internal/machine/invocation.d.ts +0 -33
- package/dist/internal/machine/invocation.d.ts.map +0 -1
- package/dist/internal/machine/invocation.js +0 -220
- package/dist/internal/machine/invocation.js.map +0 -1
- package/dist/internal/machine/invocationDefinition.d.ts.map +0 -1
- package/dist/internal/machine/invocationDefinition.js.map +0 -1
- package/dist/internal/machine/requirements.d.ts +0 -4
- package/dist/internal/machine/requirements.d.ts.map +0 -1
- package/dist/internal/machine/requirements.js +0 -2
- package/dist/internal/machine/requirements.js.map +0 -1
- package/dist/internal/machine/runtimeGeneric.d.ts +0 -3
- package/dist/internal/machine/runtimeGeneric.d.ts.map +0 -1
- package/dist/internal/machine/runtimeGeneric.js +0 -465
- package/dist/internal/machine/runtimeGeneric.js.map +0 -1
- package/src/internal/machine/invocation.ts +0 -376
- package/src/internal/machine/requirements.ts +0 -11
- package/src/internal/machine/runtimeGeneric.ts +0 -797
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads the stored state of a Cluster machine from outside its entity.
|
|
3
|
+
*
|
|
4
|
+
* A read goes to `Storage`, which is the truth, and never to the live instance,
|
|
5
|
+
* so it does not depend on which runner owns the entity, does not wake it, and
|
|
6
|
+
* does not keep it alive. Changes are observed by reading again, so a reader
|
|
7
|
+
* sees committed checkpoints and may skip the ones committed between two reads.
|
|
8
|
+
*/
|
|
9
|
+
import type { ClusterError, EntityAddress } from "effect/cluster"
|
|
10
|
+
import * as Duration from "effect/Duration"
|
|
11
|
+
import * as Effect from "effect/Effect"
|
|
12
|
+
import * as Option from "effect/Option"
|
|
13
|
+
import * as Schedule from "effect/Schedule"
|
|
14
|
+
import * as Stream from "effect/Stream"
|
|
15
|
+
import type * as Machine from "../../Machine.js"
|
|
16
|
+
import type { Checkpoint, Snapshot } from "../../unstable/cluster/ClusterMachine.js"
|
|
17
|
+
import { readStatus } from "./clusterInstance.js"
|
|
18
|
+
import { MachineFailedError, ReadError, Storage } from "./clusterProtocol.js"
|
|
19
|
+
import * as internalMachine from "./machine.js"
|
|
20
|
+
import * as Planner from "./planner.js"
|
|
21
|
+
|
|
22
|
+
type PersistenceError = ClusterError.PersistenceError
|
|
23
|
+
|
|
24
|
+
type Services<M extends Machine.Machine.Any> =
|
|
25
|
+
| Storage
|
|
26
|
+
| Machine.Machine.SnapshotDecodingServices<Machine.Machine.States<M>>
|
|
27
|
+
|
|
28
|
+
export interface Reader<M extends Machine.Machine.Any> {
|
|
29
|
+
readonly snapshot: Effect.Effect<Option.Option<Snapshot<M>>, ReadError | PersistenceError, Services<M>>
|
|
30
|
+
readonly changes: Stream.Stream<Snapshot<M>, ReadError | PersistenceError, Services<M>>
|
|
31
|
+
readonly join: Effect.Effect<
|
|
32
|
+
Machine.Machine.Output<M>,
|
|
33
|
+
MachineFailedError | ReadError | PersistenceError,
|
|
34
|
+
Services<M>
|
|
35
|
+
>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Validated wire representation of a committed checkpoint, ready for RPC transport. */
|
|
39
|
+
export type WireSnapshot =
|
|
40
|
+
| { readonly status: "active"; readonly revision: number; readonly snapshot: unknown }
|
|
41
|
+
| { readonly status: "done"; readonly revision: number; readonly snapshot: unknown }
|
|
42
|
+
| { readonly status: "failed"; readonly revision: number; readonly snapshot: unknown; readonly failure: string }
|
|
43
|
+
| { readonly status: "stopped"; readonly revision: number; readonly snapshot: unknown }
|
|
44
|
+
|
|
45
|
+
export interface EncodedReader<M extends Machine.Machine.Any> {
|
|
46
|
+
readonly snapshot: Effect.Effect<Option.Option<WireSnapshot>, ReadError | PersistenceError, Services<M>>
|
|
47
|
+
readonly changes: Stream.Stream<WireSnapshot, ReadError | PersistenceError, Services<M>>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface Options {
|
|
51
|
+
readonly machine: Machine.Machine.Any
|
|
52
|
+
readonly definitionId: string
|
|
53
|
+
readonly version: string
|
|
54
|
+
readonly address: EntityAddress.EntityAddress
|
|
55
|
+
readonly interval: Duration.Input
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const invalid = (message: string) => new ReadError({ reason: "InvalidCheckpoint", message })
|
|
59
|
+
|
|
60
|
+
const toWire = (snapshot: Snapshot<Machine.Machine.Any>, encoded: unknown): WireSnapshot => {
|
|
61
|
+
switch (snapshot.status) {
|
|
62
|
+
case "active":
|
|
63
|
+
return { status: "active", revision: snapshot.revision, snapshot: encoded }
|
|
64
|
+
case "done":
|
|
65
|
+
return { status: "done", revision: snapshot.revision, snapshot: encoded }
|
|
66
|
+
case "failed":
|
|
67
|
+
return { status: "failed", revision: snapshot.revision, snapshot: encoded, failure: snapshot.failure }
|
|
68
|
+
case "stopped":
|
|
69
|
+
return { status: "stopped", revision: snapshot.revision, snapshot: encoded }
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Decodes a validated wire snapshot back into a typed `ClusterMachine.Snapshot`. */
|
|
74
|
+
export const decodeWire = <M extends Machine.Machine.Any>(
|
|
75
|
+
machine: M,
|
|
76
|
+
wire: WireSnapshot
|
|
77
|
+
): Effect.Effect<
|
|
78
|
+
Snapshot<M>,
|
|
79
|
+
ReadError,
|
|
80
|
+
Machine.Machine.SnapshotDecodingServices<Machine.Machine.States<M>>
|
|
81
|
+
> =>
|
|
82
|
+
Effect.gen(function*() {
|
|
83
|
+
const state = yield* internalMachine.decodeSnapshot(machine, wire.snapshot).pipe(
|
|
84
|
+
Effect.mapError((error) => invalid(String(error.cause)))
|
|
85
|
+
)
|
|
86
|
+
if (wire.status === "failed") {
|
|
87
|
+
return { status: "failed", revision: wire.revision, state, failure: wire.failure }
|
|
88
|
+
}
|
|
89
|
+
if (wire.status === "stopped") {
|
|
90
|
+
return { status: "stopped", revision: wire.revision, state }
|
|
91
|
+
}
|
|
92
|
+
if (wire.status === "active") {
|
|
93
|
+
return { status: "active", revision: wire.revision, state }
|
|
94
|
+
}
|
|
95
|
+
if (!internalMachine.isFinal(machine, state)) {
|
|
96
|
+
return yield* invalid("Expected a final state for a done snapshot")
|
|
97
|
+
}
|
|
98
|
+
const output = yield* Planner.getFinalOutputEffect(machine, state, Planner.InitialEvent).pipe(
|
|
99
|
+
Effect.mapError((error) => invalid(String(error.cause)))
|
|
100
|
+
)
|
|
101
|
+
return { status: "done", revision: wire.revision, state, output }
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
/** Waits for the terminal snapshot of a change stream and returns its output or failure. */
|
|
105
|
+
export const joinChanges = <M extends Machine.Machine.Any, E, R>(
|
|
106
|
+
changes: Stream.Stream<Snapshot<M>, E, R>
|
|
107
|
+
): Effect.Effect<Machine.Machine.Output<M>, MachineFailedError | E, R> =>
|
|
108
|
+
changes.pipe(
|
|
109
|
+
Stream.filter((read) => read.status !== "active"),
|
|
110
|
+
Stream.runHead,
|
|
111
|
+
Effect.flatMap((read) =>
|
|
112
|
+
Option.isNone(read)
|
|
113
|
+
? Effect.die(new Error("A reader of a Cluster machine ended without a terminal checkpoint"))
|
|
114
|
+
: read.value.status === "failed"
|
|
115
|
+
? Effect.fail(new MachineFailedError({ failure: read.value.failure }))
|
|
116
|
+
: read.value.status === "stopped"
|
|
117
|
+
? Effect.fail(new MachineFailedError({ failure: "The child machine was stopped by its parent" }))
|
|
118
|
+
: Effect.succeed(read.value.output)
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
// Validates a checkpoint as an entity does when it loads one, so that a reader
|
|
123
|
+
// refuses what the entity would refuse.
|
|
124
|
+
const decode = Effect.fnUntraced(function*(
|
|
125
|
+
options: Options,
|
|
126
|
+
checkpoint: Checkpoint
|
|
127
|
+
): Effect.fn.Return<Snapshot<Machine.Machine.Any>, ReadError, any> {
|
|
128
|
+
const { definitionId, machine, version } = options
|
|
129
|
+
if (checkpoint.definitionId !== definitionId) {
|
|
130
|
+
return yield* new ReadError({
|
|
131
|
+
reason: "DefinitionIdMismatch",
|
|
132
|
+
message: `Expected machine definition id ${definitionId}, received ${checkpoint.definitionId}`
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
if (checkpoint.version !== version) {
|
|
136
|
+
return yield* new ReadError({
|
|
137
|
+
reason: "VersionMismatch",
|
|
138
|
+
message: `Expected version ${version}, received ${checkpoint.version}`
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
const { revision } = checkpoint
|
|
142
|
+
if (!Number.isSafeInteger(revision) || revision < 1) {
|
|
143
|
+
return yield* invalid(`Expected a positive integer revision, received ${revision}`)
|
|
144
|
+
}
|
|
145
|
+
const status = readStatus(checkpoint.status)
|
|
146
|
+
if (status === undefined) return yield* invalid("Expected an active or failed status")
|
|
147
|
+
const state = yield* internalMachine.decodeSnapshot(machine, checkpoint.snapshot).pipe(
|
|
148
|
+
Effect.mapError((error) => invalid(String(error.cause)))
|
|
149
|
+
)
|
|
150
|
+
if (status._tag === "Failed") return { status: "failed", revision, state, failure: status.failure }
|
|
151
|
+
if (status._tag === "Stopped") return { status: "stopped", revision, state }
|
|
152
|
+
if (!internalMachine.isFinal(machine, state)) return { status: "active", revision, state }
|
|
153
|
+
const output = yield* Planner.getFinalOutputEffect(machine, state, Planner.InitialEvent).pipe(
|
|
154
|
+
Effect.mapError((error) => invalid(String(error.cause)))
|
|
155
|
+
)
|
|
156
|
+
return { status: "done", revision, state, output }
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
const pollChanges = <A extends { readonly revision: number; readonly status: string }, E, R>(
|
|
160
|
+
snapshot: Effect.Effect<Option.Option<A>, E, R>,
|
|
161
|
+
interval: Duration.Input
|
|
162
|
+
): Stream.Stream<A, E, R> =>
|
|
163
|
+
Stream.fromEffectSchedule(
|
|
164
|
+
snapshot,
|
|
165
|
+
Schedule.spaced(Duration.fromInputUnsafe(interval))
|
|
166
|
+
).pipe(
|
|
167
|
+
Stream.filter(Option.isSome),
|
|
168
|
+
Stream.map((read) => read.value),
|
|
169
|
+
Stream.changesWith((left, right) => left.revision === right.revision),
|
|
170
|
+
Stream.takeUntil((read) => read.status !== "active")
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
export const makeEncoded = <M extends Machine.Machine.Any>(options: Options): EncodedReader<M> => {
|
|
174
|
+
const snapshot = Effect.gen(function*() {
|
|
175
|
+
const storage = yield* Storage
|
|
176
|
+
return yield* storage.load(options.address)
|
|
177
|
+
}).pipe(
|
|
178
|
+
Effect.flatMap(Option.match({
|
|
179
|
+
onNone: () => Effect.succeedNone,
|
|
180
|
+
onSome: (checkpoint) =>
|
|
181
|
+
Effect.map(decode(options, checkpoint), (decoded) => Option.some(toWire(decoded, checkpoint.snapshot)))
|
|
182
|
+
}))
|
|
183
|
+
) as EncodedReader<M>["snapshot"]
|
|
184
|
+
|
|
185
|
+
return { snapshot, changes: pollChanges(snapshot, options.interval) }
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export const make = <M extends Machine.Machine.Any>(options: Options): Reader<M> => {
|
|
189
|
+
const snapshot = Effect.gen(function*() {
|
|
190
|
+
const storage = yield* Storage
|
|
191
|
+
return yield* storage.load(options.address)
|
|
192
|
+
}).pipe(
|
|
193
|
+
Effect.flatMap(Option.match({
|
|
194
|
+
onNone: () => Effect.succeedNone,
|
|
195
|
+
onSome: (checkpoint) => Effect.asSome(decode(options, checkpoint))
|
|
196
|
+
}))
|
|
197
|
+
) as Reader<M>["snapshot"]
|
|
198
|
+
|
|
199
|
+
// Reads again after each interval and emits every revision it has not seen,
|
|
200
|
+
// ending after a terminal one. Reading again is also how a reader follows an
|
|
201
|
+
// entity that is passivated or moves: there is no subscription to lose.
|
|
202
|
+
const changes: Reader<M>["changes"] = pollChanges(snapshot, options.interval)
|
|
203
|
+
|
|
204
|
+
const join: Reader<M>["join"] = joinChanges(changes)
|
|
205
|
+
|
|
206
|
+
return { snapshot, changes, join }
|
|
207
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/** Durable checkpoint storage for Cluster machines in a SQL database. */
|
|
2
|
+
import { ClusterError, Snowflake } from "effect/cluster"
|
|
3
|
+
import type { EntityAddress } from "effect/cluster"
|
|
4
|
+
import * as Effect from "effect/Effect"
|
|
5
|
+
import * as Layer from "effect/Layer"
|
|
6
|
+
import * as Option from "effect/Option"
|
|
7
|
+
import * as Predicate from "effect/Predicate"
|
|
8
|
+
import * as Migrator from "effect/sql/Migrator"
|
|
9
|
+
import * as SqlClient from "effect/sql/SqlClient"
|
|
10
|
+
import type { Checkpoint } from "../../unstable/cluster/ClusterMachine.js"
|
|
11
|
+
import { CommitResult, Storage } from "./clusterProtocol.js"
|
|
12
|
+
|
|
13
|
+
type EntityAddress = EntityAddress.EntityAddress
|
|
14
|
+
|
|
15
|
+
export interface Options {
|
|
16
|
+
readonly prefix?: string | undefined
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const defaultPrefix = "cluster_machine"
|
|
20
|
+
|
|
21
|
+
const tables = (options: Options | undefined) => {
|
|
22
|
+
const prefix = options?.prefix ?? defaultPrefix
|
|
23
|
+
return {
|
|
24
|
+
checkpoints: `${prefix}_checkpoints`,
|
|
25
|
+
requests: `${prefix}_requests`,
|
|
26
|
+
migrations: `${prefix}_migrations`
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const unsupportedDialect = (dialect: string) =>
|
|
31
|
+
Effect.die(new Error(`SqlMachineStorage supports PostgreSQL and SQLite, not ${dialect}`))
|
|
32
|
+
|
|
33
|
+
export const migrations = (options?: Options): Migrator.Loader => {
|
|
34
|
+
const names = tables(options)
|
|
35
|
+
return Migrator.fromRecord({
|
|
36
|
+
"0001_create_tables": Effect.gen(function*() {
|
|
37
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms()
|
|
38
|
+
if (sql.onDialectOrElse({ orElse: () => true, pg: () => false, sqlite: () => false })) {
|
|
39
|
+
return yield* unsupportedDialect(
|
|
40
|
+
sql.onDialectOrElse({ orElse: () => "this dialect", mysql: () => "MySQL", mssql: () => "SQL Server" })
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
// The text of a request id keeps its 64 bits on every driver.
|
|
44
|
+
yield* sql`
|
|
45
|
+
CREATE TABLE IF NOT EXISTS ${sql(names.checkpoints)} (
|
|
46
|
+
entity_type VARCHAR(150) NOT NULL,
|
|
47
|
+
entity_id VARCHAR(255) NOT NULL,
|
|
48
|
+
revision BIGINT NOT NULL,
|
|
49
|
+
checkpoint TEXT NOT NULL,
|
|
50
|
+
PRIMARY KEY (entity_type, entity_id)
|
|
51
|
+
)
|
|
52
|
+
`
|
|
53
|
+
yield* sql`
|
|
54
|
+
CREATE TABLE IF NOT EXISTS ${sql(names.requests)} (
|
|
55
|
+
entity_type VARCHAR(150) NOT NULL,
|
|
56
|
+
entity_id VARCHAR(255) NOT NULL,
|
|
57
|
+
request_id VARCHAR(20) NOT NULL,
|
|
58
|
+
PRIMARY KEY (entity_type, entity_id, request_id)
|
|
59
|
+
)
|
|
60
|
+
`
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const runMigrations = (options?: Options): Effect.Effect<void, never, SqlClient.SqlClient> =>
|
|
66
|
+
Migrator.make({})({
|
|
67
|
+
loader: migrations(options),
|
|
68
|
+
table: tables(options).migrations
|
|
69
|
+
}).pipe(Effect.asVoid, Effect.orDie)
|
|
70
|
+
|
|
71
|
+
export const layerMigrations = (options?: Options): Layer.Layer<never, never, SqlClient.SqlClient> =>
|
|
72
|
+
Layer.effectDiscard(runMigrations(options))
|
|
73
|
+
|
|
74
|
+
// A checkpoint is stored as JSON. Its request id is a 64-bit integer, which
|
|
75
|
+
// JSON numbers do not hold, so it is stored as text.
|
|
76
|
+
const encode = (checkpoint: Checkpoint): string =>
|
|
77
|
+
JSON.stringify(
|
|
78
|
+
checkpoint.origin._tag === "Request"
|
|
79
|
+
? { ...checkpoint, origin: { _tag: "Request", requestId: String(checkpoint.origin.requestId) } }
|
|
80
|
+
: checkpoint
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
const decode = (text: string): Checkpoint => {
|
|
84
|
+
const parsed = JSON.parse(text)
|
|
85
|
+
const origin = parsed?.origin
|
|
86
|
+
return Predicate.hasProperty(origin, "requestId") && Predicate.isString(origin.requestId)
|
|
87
|
+
? { ...parsed, origin: { ...origin, requestId: Snowflake.Snowflake(origin.requestId) } }
|
|
88
|
+
: parsed
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const persistence = <A, E, R>(effect: Effect.Effect<A, E, R>) => ClusterError.PersistenceError.refail(effect)
|
|
92
|
+
|
|
93
|
+
export const makeStorage = (options?: Options): Effect.Effect<Storage["Service"], never, SqlClient.SqlClient> =>
|
|
94
|
+
Effect.gen(function*() {
|
|
95
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms()
|
|
96
|
+
if (sql.onDialectOrElse({ orElse: () => true, pg: () => false, sqlite: () => false })) {
|
|
97
|
+
return yield* unsupportedDialect(
|
|
98
|
+
sql.onDialectOrElse({ orElse: () => "this dialect", mysql: () => "MySQL", mssql: () => "SQL Server" })
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
const names = tables(options)
|
|
102
|
+
const checkpoints = sql(names.checkpoints)
|
|
103
|
+
const requests = sql(names.requests)
|
|
104
|
+
|
|
105
|
+
const selectCheckpoint = (address: EntityAddress) =>
|
|
106
|
+
sql<{ readonly checkpoint: string }>`
|
|
107
|
+
SELECT checkpoint FROM ${checkpoints}
|
|
108
|
+
WHERE entity_type = ${address.entityType} AND entity_id = ${address.entityId}
|
|
109
|
+
`.pipe(Effect.map((rows) => Option.map(Option.fromNullishOr(rows[0]), (row) => decode(row.checkpoint))))
|
|
110
|
+
|
|
111
|
+
const processed = (address: EntityAddress, requestId: Snowflake.Snowflake) =>
|
|
112
|
+
sql`
|
|
113
|
+
SELECT 1 AS found FROM ${requests}
|
|
114
|
+
WHERE entity_type = ${address.entityType} AND entity_id = ${address.entityId}
|
|
115
|
+
AND request_id = ${String(requestId)}
|
|
116
|
+
`.pipe(Effect.map((rows) => rows.length > 0))
|
|
117
|
+
|
|
118
|
+
// Writes the checkpoint only over the revision before it, and reports
|
|
119
|
+
// whether it did. Concurrent writers of one entity meet on its row.
|
|
120
|
+
const write = (address: EntityAddress, checkpoint: Checkpoint) => {
|
|
121
|
+
const text = encode(checkpoint)
|
|
122
|
+
return (checkpoint.revision === 1
|
|
123
|
+
? sql`
|
|
124
|
+
INSERT INTO ${checkpoints} (entity_type, entity_id, revision, checkpoint)
|
|
125
|
+
VALUES (${address.entityType}, ${address.entityId}, ${checkpoint.revision}, ${text})
|
|
126
|
+
ON CONFLICT DO NOTHING
|
|
127
|
+
RETURNING revision
|
|
128
|
+
`
|
|
129
|
+
: sql`
|
|
130
|
+
UPDATE ${checkpoints} SET revision = ${checkpoint.revision}, checkpoint = ${text}
|
|
131
|
+
WHERE entity_type = ${address.entityType} AND entity_id = ${address.entityId}
|
|
132
|
+
AND revision = ${checkpoint.revision - 1}
|
|
133
|
+
RETURNING revision
|
|
134
|
+
`).pipe(Effect.map((rows) => rows.length > 0))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const commit = Effect.fnUntraced(function*(address: EntityAddress, checkpoint: Checkpoint) {
|
|
138
|
+
const { origin } = checkpoint
|
|
139
|
+
const request = origin._tag === "Request" ? origin.requestId : undefined
|
|
140
|
+
if (request !== undefined && (yield* processed(address, request))) return CommitResult.Duplicate()
|
|
141
|
+
if (!(yield* write(address, checkpoint))) {
|
|
142
|
+
// A writer that got there first may have committed this very request,
|
|
143
|
+
// which makes this commit a duplicate rather than a conflict.
|
|
144
|
+
return request !== undefined && (yield* processed(address, request))
|
|
145
|
+
? CommitResult.Duplicate()
|
|
146
|
+
: CommitResult.RevisionConflict()
|
|
147
|
+
}
|
|
148
|
+
if (request !== undefined) {
|
|
149
|
+
yield* sql`
|
|
150
|
+
INSERT INTO ${requests} (entity_type, entity_id, request_id)
|
|
151
|
+
VALUES (${address.entityType}, ${address.entityId}, ${String(request)})
|
|
152
|
+
`
|
|
153
|
+
}
|
|
154
|
+
return CommitResult.Committed()
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
return Storage.of({
|
|
158
|
+
load: (address) => persistence(selectCheckpoint(address)),
|
|
159
|
+
// One transaction gives both answers from one view of the store.
|
|
160
|
+
loadForRequest: (address, requestId) =>
|
|
161
|
+
Effect.all({
|
|
162
|
+
checkpoint: selectCheckpoint(address),
|
|
163
|
+
processed: processed(address, requestId)
|
|
164
|
+
}).pipe(sql.withTransaction, persistence),
|
|
165
|
+
// Inside the transaction of `MessageStorage` over the same client, this
|
|
166
|
+
// joins it, so the checkpoint commits or rolls back with the step.
|
|
167
|
+
commit: (address, checkpoint) =>
|
|
168
|
+
Effect.suspend(() => commit(address, checkpoint)).pipe(sql.withTransaction, persistence)
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
export const make = (options?: Options): Effect.Effect<Storage["Service"], never, SqlClient.SqlClient> =>
|
|
173
|
+
Effect.andThen(runMigrations(options), makeStorage(options))
|
|
174
|
+
|
|
175
|
+
export const layerStorage = (options?: Options): Layer.Layer<Storage, never, SqlClient.SqlClient> =>
|
|
176
|
+
Layer.effect(Storage, makeStorage(options))
|
|
177
|
+
|
|
178
|
+
export const layerWith = (options: Options): Layer.Layer<Storage, never, SqlClient.SqlClient> =>
|
|
179
|
+
Layer.effect(Storage, make(options))
|
|
180
|
+
|
|
181
|
+
export const layer: Layer.Layer<Storage, never, SqlClient.SqlClient> = layerWith({})
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/** A check that a `Storage` keeps the contract that Cluster machines rely on. */
|
|
2
|
+
import { EntityAddress, EntityId, EntityType, ShardId, Snowflake } from "effect/cluster"
|
|
3
|
+
import type { ClusterError } from "effect/cluster"
|
|
4
|
+
import * as Effect from "effect/Effect"
|
|
5
|
+
import * as Equal from "effect/Equal"
|
|
6
|
+
import * as Option from "effect/Option"
|
|
7
|
+
import * as Schema from "effect/Schema"
|
|
8
|
+
import type { Checkpoint } from "../../unstable/cluster/ClusterMachine.js"
|
|
9
|
+
import type { CommitResult, Storage } from "./clusterProtocol.js"
|
|
10
|
+
|
|
11
|
+
export class StorageCheckFailure extends Schema.TaggedError<StorageCheckFailure>(
|
|
12
|
+
"effect/cluster/ClusterMachine/StorageCheckFailure"
|
|
13
|
+
)("StorageCheckFailure", {
|
|
14
|
+
scenario: Schema.String,
|
|
15
|
+
message: Schema.String
|
|
16
|
+
}) {}
|
|
17
|
+
|
|
18
|
+
export interface CheckOptions {
|
|
19
|
+
readonly withTransaction?: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type Service = Storage["Service"]
|
|
23
|
+
type Failure = StorageCheckFailure | ClusterError.PersistenceError
|
|
24
|
+
|
|
25
|
+
const run = Math.random().toString(36).slice(2, 10)
|
|
26
|
+
let entities = 0
|
|
27
|
+
|
|
28
|
+
const freshAddress = (entityType = "StorageCheck") =>
|
|
29
|
+
EntityAddress.make({
|
|
30
|
+
shardId: ShardId.make("default", 1),
|
|
31
|
+
entityType: EntityType.make(entityType),
|
|
32
|
+
entityId: EntityId.make(`check-${run}-${++entities}`)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const request = (id: number | string): Checkpoint.Origin => ({ _tag: "Request", requestId: Snowflake.Snowflake(id) })
|
|
36
|
+
const timers: Checkpoint.Origin = { _tag: "Timers" }
|
|
37
|
+
const invocation: Checkpoint.Origin = { _tag: "Invocation", path: "Working", id: "load" }
|
|
38
|
+
|
|
39
|
+
const checkpoint = (revision: number, origin: Checkpoint.Origin, status?: Checkpoint.Status): Checkpoint => ({
|
|
40
|
+
definitionId: "storage-check",
|
|
41
|
+
version: "1",
|
|
42
|
+
revision,
|
|
43
|
+
origin,
|
|
44
|
+
snapshot: { path: "", value: { revision }, children: [] } as unknown as Checkpoint["snapshot"],
|
|
45
|
+
status: status ?? { _tag: "Active", invocations: {} }
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const same = (left: unknown, right: unknown): boolean =>
|
|
49
|
+
typeof left === "bigint" || typeof right === "bigint"
|
|
50
|
+
? left === right
|
|
51
|
+
: Array.isArray(left) && Array.isArray(right)
|
|
52
|
+
? left.length === right.length && left.every((value, index) => same(value, right[index]))
|
|
53
|
+
: typeof left === "object" && left !== null && typeof right === "object" && right !== null
|
|
54
|
+
? Object.keys(left).length === Object.keys(right).length &&
|
|
55
|
+
Object.entries(left).every(([key, value]) => same(value, (right as Record<string, unknown>)[key]))
|
|
56
|
+
: Equal.equals(left, right)
|
|
57
|
+
|
|
58
|
+
const scenario = (name: string, body: (storage: Service, options: CheckOptions) => Effect.Effect<void, Failure>) => ({
|
|
59
|
+
name,
|
|
60
|
+
body
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
const failWith = (name: string) => (message: string) => new StorageCheckFailure({ scenario: name, message })
|
|
64
|
+
|
|
65
|
+
const expectResult = (
|
|
66
|
+
name: string,
|
|
67
|
+
what: string,
|
|
68
|
+
actual: CommitResult,
|
|
69
|
+
expected: CommitResult["_tag"]
|
|
70
|
+
): Effect.Effect<void, StorageCheckFailure> =>
|
|
71
|
+
actual._tag === expected
|
|
72
|
+
? Effect.void
|
|
73
|
+
: Effect.fail(failWith(name)(`Expected ${what} to be ${expected}, received ${actual._tag}`))
|
|
74
|
+
|
|
75
|
+
const expectStored = Effect.fnUntraced(function*(
|
|
76
|
+
name: string,
|
|
77
|
+
storage: Service,
|
|
78
|
+
address: EntityAddress.EntityAddress,
|
|
79
|
+
expected: Checkpoint | undefined
|
|
80
|
+
) {
|
|
81
|
+
const loaded = yield* storage.load(address)
|
|
82
|
+
if (expected === undefined) {
|
|
83
|
+
if (Option.isSome(loaded)) return yield* failWith(name)("Expected no checkpoint, received one")
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
if (Option.isNone(loaded)) return yield* failWith(name)(`Expected revision ${expected.revision}, received none`)
|
|
87
|
+
if (!same(loaded.value, expected)) {
|
|
88
|
+
return yield* failWith(name)(
|
|
89
|
+
`Expected the checkpoint of revision ${expected.revision} unchanged, received revision ${loaded.value.revision}`
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
const scenarios = [
|
|
95
|
+
scenario("loads nothing for an entity without a checkpoint", (storage) =>
|
|
96
|
+
Effect.gen(function*() {
|
|
97
|
+
const name = "loads nothing for an entity without a checkpoint"
|
|
98
|
+
const address = freshAddress()
|
|
99
|
+
yield* expectStored(name, storage, address, undefined)
|
|
100
|
+
const loaded = yield* storage.loadForRequest(address, Snowflake.Snowflake(1))
|
|
101
|
+
if (Option.isSome(loaded.checkpoint) || loaded.processed) {
|
|
102
|
+
return yield* failWith(name)("Expected no checkpoint and no processed request")
|
|
103
|
+
}
|
|
104
|
+
})),
|
|
105
|
+
scenario("stores every field of a checkpoint unchanged", (storage) =>
|
|
106
|
+
Effect.gen(function*() {
|
|
107
|
+
const name = "stores every field of a checkpoint unchanged"
|
|
108
|
+
const address = freshAddress()
|
|
109
|
+
const first = checkpoint(1, request("9007199254740993"), {
|
|
110
|
+
_tag: "Active",
|
|
111
|
+
invocations: { Working: { load: { _tag: "Waiting", dueAt: 1_764_000_000_000 }, poll: { _tag: "Settled" } } }
|
|
112
|
+
})
|
|
113
|
+
yield* expectResult(name, "the first commit", yield* storage.commit(address, first), "Committed")
|
|
114
|
+
yield* expectStored(name, storage, address, first)
|
|
115
|
+
const failed = checkpoint(2, invocation, { _tag: "Failed", failure: "The machine failed" })
|
|
116
|
+
yield* expectResult(name, "the second commit", yield* storage.commit(address, failed), "Committed")
|
|
117
|
+
yield* expectStored(name, storage, address, failed)
|
|
118
|
+
})),
|
|
119
|
+
scenario("commits only the revision after the stored one", (storage) =>
|
|
120
|
+
Effect.gen(function*() {
|
|
121
|
+
const name = "commits only the revision after the stored one"
|
|
122
|
+
const address = freshAddress()
|
|
123
|
+
yield* expectResult(
|
|
124
|
+
name,
|
|
125
|
+
"revision 2 of no checkpoint",
|
|
126
|
+
yield* storage.commit(address, checkpoint(2, timers)),
|
|
127
|
+
"RevisionConflict"
|
|
128
|
+
)
|
|
129
|
+
const first = checkpoint(1, request(1))
|
|
130
|
+
yield* expectResult(name, "revision 1", yield* storage.commit(address, first), "Committed")
|
|
131
|
+
yield* expectResult(
|
|
132
|
+
name,
|
|
133
|
+
"revision 1 again",
|
|
134
|
+
yield* storage.commit(address, checkpoint(1, timers)),
|
|
135
|
+
"RevisionConflict"
|
|
136
|
+
)
|
|
137
|
+
yield* expectResult(name, "revision 3", yield* storage.commit(address, checkpoint(3, timers)), "RevisionConflict")
|
|
138
|
+
yield* expectStored(name, storage, address, first)
|
|
139
|
+
const second = checkpoint(2, timers)
|
|
140
|
+
yield* expectResult(name, "revision 2", yield* storage.commit(address, second), "Committed")
|
|
141
|
+
yield* expectStored(name, storage, address, second)
|
|
142
|
+
})),
|
|
143
|
+
scenario("treats a committed request as a duplicate before its revision", (storage) =>
|
|
144
|
+
Effect.gen(function*() {
|
|
145
|
+
const name = "treats a committed request as a duplicate before its revision"
|
|
146
|
+
const address = freshAddress()
|
|
147
|
+
const first = checkpoint(1, request(1))
|
|
148
|
+
yield* expectResult(name, "the request", yield* storage.commit(address, first), "Committed")
|
|
149
|
+
yield* expectResult(
|
|
150
|
+
name,
|
|
151
|
+
"the request at the next revision",
|
|
152
|
+
yield* storage.commit(address, checkpoint(2, request(1))),
|
|
153
|
+
"Duplicate"
|
|
154
|
+
)
|
|
155
|
+
yield* expectResult(
|
|
156
|
+
name,
|
|
157
|
+
"the request at a stale revision",
|
|
158
|
+
yield* storage.commit(address, checkpoint(1, request(1))),
|
|
159
|
+
"Duplicate"
|
|
160
|
+
)
|
|
161
|
+
yield* expectResult(
|
|
162
|
+
name,
|
|
163
|
+
"the request at a future revision",
|
|
164
|
+
yield* storage.commit(address, checkpoint(5, request(1))),
|
|
165
|
+
"Duplicate"
|
|
166
|
+
)
|
|
167
|
+
yield* expectStored(name, storage, address, first)
|
|
168
|
+
const loaded = yield* storage.loadForRequest(address, Snowflake.Snowflake(1))
|
|
169
|
+
if (!loaded.processed || !Option.isSome(loaded.checkpoint) || !same(loaded.checkpoint.value, first)) {
|
|
170
|
+
return yield* failWith(name)("Expected the request processed, with the checkpoint it committed")
|
|
171
|
+
}
|
|
172
|
+
const other = yield* storage.loadForRequest(address, Snowflake.Snowflake(2))
|
|
173
|
+
if (other.processed) return yield* failWith(name)("Expected another request not processed")
|
|
174
|
+
})),
|
|
175
|
+
scenario("records no request for a rejected commit", (storage) =>
|
|
176
|
+
Effect.gen(function*() {
|
|
177
|
+
const name = "records no request for a rejected commit"
|
|
178
|
+
const address = freshAddress()
|
|
179
|
+
yield* expectResult(name, "revision 1", yield* storage.commit(address, checkpoint(1, timers)), "Committed")
|
|
180
|
+
yield* expectResult(
|
|
181
|
+
name,
|
|
182
|
+
"a request at a stale revision",
|
|
183
|
+
yield* storage.commit(address, checkpoint(1, request(7))),
|
|
184
|
+
"RevisionConflict"
|
|
185
|
+
)
|
|
186
|
+
const loaded = yield* storage.loadForRequest(address, Snowflake.Snowflake(7))
|
|
187
|
+
if (loaded.processed) return yield* failWith(name)("Expected a conflicting request not recorded")
|
|
188
|
+
yield* expectResult(
|
|
189
|
+
name,
|
|
190
|
+
"the request at the next revision",
|
|
191
|
+
yield* storage.commit(address, checkpoint(2, request(7))),
|
|
192
|
+
"Committed"
|
|
193
|
+
)
|
|
194
|
+
})),
|
|
195
|
+
scenario("never treats a commit without a request as a duplicate", (storage) =>
|
|
196
|
+
Effect.gen(function*() {
|
|
197
|
+
const name = "never treats a commit without a request as a duplicate"
|
|
198
|
+
const address = freshAddress()
|
|
199
|
+
yield* expectResult(name, "an invocation", yield* storage.commit(address, checkpoint(1, invocation)), "Committed")
|
|
200
|
+
yield* expectResult(
|
|
201
|
+
name,
|
|
202
|
+
"the same invocation",
|
|
203
|
+
yield* storage.commit(address, checkpoint(2, invocation)),
|
|
204
|
+
"Committed"
|
|
205
|
+
)
|
|
206
|
+
yield* expectResult(name, "timers", yield* storage.commit(address, checkpoint(3, timers)), "Committed")
|
|
207
|
+
yield* expectResult(name, "timers again", yield* storage.commit(address, checkpoint(4, timers)), "Committed")
|
|
208
|
+
})),
|
|
209
|
+
scenario("keeps entities of other ids and types apart", (storage) =>
|
|
210
|
+
Effect.gen(function*() {
|
|
211
|
+
const name = "keeps entities of other ids and types apart"
|
|
212
|
+
const address = freshAddress()
|
|
213
|
+
const sameId = EntityAddress.make({
|
|
214
|
+
shardId: address.shardId,
|
|
215
|
+
entityType: EntityType.make("StorageCheckOther"),
|
|
216
|
+
entityId: address.entityId
|
|
217
|
+
})
|
|
218
|
+
const first = checkpoint(1, request(1))
|
|
219
|
+
yield* expectResult(name, "the first entity", yield* storage.commit(address, first), "Committed")
|
|
220
|
+
yield* expectStored(name, storage, sameId, undefined)
|
|
221
|
+
yield* expectResult(
|
|
222
|
+
name,
|
|
223
|
+
"the same id of another type",
|
|
224
|
+
yield* storage.commit(sameId, checkpoint(1, request(1))),
|
|
225
|
+
"Committed"
|
|
226
|
+
)
|
|
227
|
+
const other = freshAddress()
|
|
228
|
+
yield* expectResult(name, "another id", yield* storage.commit(other, checkpoint(1, request(1))), "Committed")
|
|
229
|
+
yield* expectStored(name, storage, address, first)
|
|
230
|
+
})),
|
|
231
|
+
scenario("lets one of two concurrent commits of a revision win", (storage) =>
|
|
232
|
+
Effect.gen(function*() {
|
|
233
|
+
const name = "lets one of two concurrent commits of a revision win"
|
|
234
|
+
const address = freshAddress()
|
|
235
|
+
const [left, right] = yield* Effect.all([
|
|
236
|
+
storage.commit(address, checkpoint(1, request(1))),
|
|
237
|
+
storage.commit(address, checkpoint(1, request(2)))
|
|
238
|
+
], { concurrency: 2 })
|
|
239
|
+
const tags = [left._tag, right._tag].sort().join(" and ")
|
|
240
|
+
if (tags !== "Committed and RevisionConflict") {
|
|
241
|
+
return yield* failWith(name)(`Expected Committed and RevisionConflict, received ${tags}`)
|
|
242
|
+
}
|
|
243
|
+
const [again, duplicate] = yield* Effect.all([
|
|
244
|
+
storage.commit(address, checkpoint(2, request(3))),
|
|
245
|
+
storage.commit(address, checkpoint(2, request(3)))
|
|
246
|
+
], { concurrency: 2 })
|
|
247
|
+
const requestTags = [again._tag, duplicate._tag].sort().join(" and ")
|
|
248
|
+
if (requestTags !== "Committed and Duplicate") {
|
|
249
|
+
return yield* failWith(name)(`Expected one request Committed and Duplicate, received ${requestTags}`)
|
|
250
|
+
}
|
|
251
|
+
})),
|
|
252
|
+
scenario("rolls a commit back with the transaction around it", (storage, options) =>
|
|
253
|
+
Effect.gen(function*() {
|
|
254
|
+
const name = "rolls a commit back with the transaction around it"
|
|
255
|
+
const withTransaction = options.withTransaction
|
|
256
|
+
if (withTransaction === undefined) return
|
|
257
|
+
const address = freshAddress()
|
|
258
|
+
const first = checkpoint(1, request(1))
|
|
259
|
+
yield* expectResult(name, "revision 1", yield* storage.commit(address, first), "Committed")
|
|
260
|
+
const rolledBack = new Error("roll back")
|
|
261
|
+
const exit = yield* Effect.exit(withTransaction(
|
|
262
|
+
Effect.flatMap(storage.commit(address, checkpoint(2, request(2))), () => Effect.die(rolledBack))
|
|
263
|
+
))
|
|
264
|
+
if (exit._tag === "Success") return yield* failWith(name)("Expected the transaction to fail")
|
|
265
|
+
yield* expectStored(name, storage, address, first)
|
|
266
|
+
const loaded = yield* storage.loadForRequest(address, Snowflake.Snowflake(2))
|
|
267
|
+
if (loaded.processed) return yield* failWith(name)("Expected the request of a rolled back commit not recorded")
|
|
268
|
+
}))
|
|
269
|
+
]
|
|
270
|
+
|
|
271
|
+
export const check = (storage: Service, options: CheckOptions = {}): Effect.Effect<void, Failure> =>
|
|
272
|
+
Effect.forEach(scenarios, ({ body }) => body(storage, options), { discard: true })
|
|
273
|
+
|
|
274
|
+
export const scenarioNames: ReadonlyArray<string> = scenarios.map(({ name }) => name)
|