@effect/cluster 0.28.4 → 0.29.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/ClusterError/package.json +6 -0
- package/ClusterMetrics/package.json +6 -0
- package/ClusterSchema/package.json +6 -0
- package/DeliverAt/package.json +6 -0
- package/Entity/package.json +6 -0
- package/EntityAddress/package.json +6 -0
- package/EntityId/package.json +6 -0
- package/EntityType/package.json +6 -0
- package/Envelope/package.json +6 -0
- package/HttpCommon/package.json +6 -0
- package/HttpRunner/package.json +6 -0
- package/HttpShardManager/package.json +6 -0
- package/MachineId/package.json +6 -0
- package/MessageStorage/package.json +6 -0
- package/README.md +2 -2
- package/Reply/package.json +6 -0
- package/Runner/package.json +6 -0
- package/RunnerAddress/package.json +6 -0
- package/RunnerHealth/package.json +6 -0
- package/RunnerServer/package.json +6 -0
- package/Runners/package.json +6 -0
- package/ShardStorage/package.json +6 -0
- package/Singleton/package.json +6 -0
- package/SingletonAddress/package.json +6 -0
- package/Snowflake/package.json +6 -0
- package/SocketRunner/package.json +6 -0
- package/SocketShardManager/package.json +6 -0
- package/SqlMessageStorage/package.json +6 -0
- package/SqlShardStorage/package.json +6 -0
- package/SynchronizedClock/package.json +6 -0
- package/dist/cjs/ClusterError.js +180 -0
- package/dist/cjs/ClusterError.js.map +1 -0
- package/dist/cjs/ClusterMetrics.js +63 -0
- package/dist/cjs/ClusterMetrics.js.map +1 -0
- package/dist/cjs/{Pods.js → ClusterSchema.js} +10 -22
- package/dist/cjs/ClusterSchema.js.map +1 -0
- package/dist/cjs/DeliverAt.js +30 -0
- package/dist/cjs/DeliverAt.js.map +1 -0
- package/dist/cjs/Entity.js +187 -0
- package/dist/cjs/Entity.js.map +1 -0
- package/dist/cjs/EntityAddress.js +54 -0
- package/dist/cjs/EntityAddress.js.map +1 -0
- package/dist/cjs/{AtLeastOnce.js → EntityId.js} +6 -7
- package/dist/cjs/EntityId.js.map +1 -0
- package/dist/cjs/{ShardManagerClient.js → EntityType.js} +5 -16
- package/dist/cjs/EntityType.js.map +1 -0
- package/dist/cjs/Envelope.js +168 -0
- package/dist/cjs/Envelope.js.map +1 -0
- package/dist/cjs/HttpCommon.js +49 -0
- package/dist/cjs/HttpCommon.js.map +1 -0
- package/dist/cjs/HttpRunner.js +108 -0
- package/dist/cjs/HttpRunner.js.map +1 -0
- package/dist/cjs/HttpShardManager.js +140 -0
- package/dist/cjs/HttpShardManager.js.map +1 -0
- package/dist/cjs/{AtLeastOnceStorage.js → MachineId.js} +11 -9
- package/dist/cjs/MachineId.js.map +1 -0
- package/dist/cjs/Message.js +99 -18
- package/dist/cjs/Message.js.map +1 -1
- package/dist/cjs/MessageStorage.js +356 -0
- package/dist/cjs/MessageStorage.js.map +1 -0
- package/dist/cjs/Reply.js +200 -0
- package/dist/cjs/Reply.js.map +1 -0
- package/dist/cjs/Runner.js +79 -0
- package/dist/cjs/Runner.js.map +1 -0
- package/dist/cjs/RunnerAddress.js +63 -0
- package/dist/cjs/RunnerAddress.js.map +1 -0
- package/dist/cjs/RunnerHealth.js +68 -0
- package/dist/cjs/RunnerHealth.js.map +1 -0
- package/dist/cjs/RunnerServer.js +125 -0
- package/dist/cjs/RunnerServer.js.map +1 -0
- package/dist/cjs/Runners.js +344 -0
- package/dist/cjs/Runners.js.map +1 -0
- package/dist/cjs/ShardId.js +7 -46
- package/dist/cjs/ShardId.js.map +1 -1
- package/dist/cjs/ShardManager.js +493 -8
- package/dist/cjs/ShardManager.js.map +1 -1
- package/dist/cjs/ShardStorage.js +139 -0
- package/dist/cjs/ShardStorage.js.map +1 -0
- package/dist/cjs/Sharding.js +732 -88
- package/dist/cjs/Sharding.js.map +1 -1
- package/dist/cjs/ShardingConfig.js +85 -18
- package/dist/cjs/ShardingConfig.js.map +1 -1
- package/dist/cjs/ShardingRegistrationEvent.js +26 -32
- package/dist/cjs/ShardingRegistrationEvent.js.map +1 -1
- package/dist/cjs/{ManagerConfig.js → Singleton.js} +11 -20
- package/dist/cjs/Singleton.js.map +1 -0
- package/dist/cjs/SingletonAddress.js +50 -0
- package/dist/cjs/SingletonAddress.js.map +1 -0
- package/dist/cjs/Snowflake.js +133 -0
- package/dist/cjs/Snowflake.js.map +1 -0
- package/dist/cjs/SocketRunner.js +40 -0
- package/dist/cjs/SocketRunner.js.map +1 -0
- package/dist/cjs/SocketShardManager.js +33 -0
- package/dist/cjs/SocketShardManager.js.map +1 -0
- package/dist/cjs/SqlMessageStorage.js +668 -0
- package/dist/cjs/SqlMessageStorage.js.map +1 -0
- package/dist/cjs/SqlShardStorage.js +228 -0
- package/dist/cjs/SqlShardStorage.js.map +1 -0
- package/dist/cjs/SynchronizedClock.js +66 -0
- package/dist/cjs/SynchronizedClock.js.map +1 -0
- package/dist/cjs/index.js +57 -45
- package/dist/cjs/internal/entityManager.js +311 -143
- package/dist/cjs/internal/entityManager.js.map +1 -1
- package/dist/cjs/internal/entityReaper.js +47 -0
- package/dist/cjs/internal/entityReaper.js.map +1 -0
- package/dist/cjs/internal/hash.js +20 -0
- package/dist/cjs/internal/hash.js.map +1 -0
- package/dist/cjs/internal/interruptors.js +9 -0
- package/dist/cjs/internal/interruptors.js.map +1 -0
- package/dist/cjs/internal/resourceMap.js +88 -0
- package/dist/cjs/internal/resourceMap.js.map +1 -0
- package/dist/cjs/internal/resourceRef.js +92 -0
- package/dist/cjs/internal/resourceRef.js.map +1 -0
- package/dist/cjs/internal/shardManager.js +219 -235
- package/dist/cjs/internal/shardManager.js.map +1 -1
- package/dist/dts/ClusterError.d.ts +169 -0
- package/dist/dts/ClusterError.d.ts.map +1 -0
- package/dist/dts/ClusterMetrics.d.ts +50 -0
- package/dist/dts/ClusterMetrics.d.ts.map +1 -0
- package/dist/dts/ClusterSchema.d.ts +13 -0
- package/dist/dts/ClusterSchema.d.ts.map +1 -0
- package/dist/dts/DeliverAt.d.ts +27 -0
- package/dist/dts/DeliverAt.d.ts.map +1 -0
- package/dist/dts/Entity.d.ts +180 -0
- package/dist/dts/Entity.d.ts.map +1 -0
- package/dist/dts/EntityAddress.d.ts +55 -0
- package/dist/dts/EntityAddress.d.ts.map +1 -0
- package/dist/dts/EntityId.d.ts +15 -0
- package/dist/dts/EntityId.d.ts.map +1 -0
- package/dist/dts/EntityType.d.ts +15 -0
- package/dist/dts/EntityType.d.ts.map +1 -0
- package/dist/dts/Envelope.d.ts +252 -0
- package/dist/dts/Envelope.d.ts.map +1 -0
- package/dist/dts/HttpCommon.d.ts +25 -0
- package/dist/dts/HttpCommon.d.ts.map +1 -0
- package/dist/dts/HttpRunner.d.ts +76 -0
- package/dist/dts/HttpRunner.d.ts.map +1 -0
- package/dist/dts/HttpShardManager.d.ts +119 -0
- package/dist/dts/HttpShardManager.d.ts.map +1 -0
- package/dist/dts/MachineId.d.ts +20 -0
- package/dist/dts/MachineId.d.ts.map +1 -0
- package/dist/dts/Message.d.ts +91 -74
- package/dist/dts/Message.d.ts.map +1 -1
- package/dist/dts/MessageStorage.d.ts +336 -0
- package/dist/dts/MessageStorage.d.ts.map +1 -0
- package/dist/dts/Reply.d.ts +171 -0
- package/dist/dts/Reply.d.ts.map +1 -0
- package/dist/dts/Runner.d.ts +81 -0
- package/dist/dts/Runner.d.ts.map +1 -0
- package/dist/dts/RunnerAddress.d.ts +56 -0
- package/dist/dts/RunnerAddress.d.ts.map +1 -0
- package/dist/dts/RunnerHealth.d.ts +54 -0
- package/dist/dts/RunnerHealth.d.ts.map +1 -0
- package/dist/dts/RunnerServer.d.ts +44 -0
- package/dist/dts/RunnerServer.d.ts.map +1 -0
- package/dist/dts/Runners.d.ts +161 -0
- package/dist/dts/Runners.d.ts.map +1 -0
- package/dist/dts/ShardId.d.ts +5 -55
- package/dist/dts/ShardId.d.ts.map +1 -1
- package/dist/dts/ShardManager.d.ts +435 -23
- package/dist/dts/ShardManager.d.ts.map +1 -1
- package/dist/dts/ShardStorage.d.ts +200 -0
- package/dist/dts/ShardStorage.d.ts.map +1 -0
- package/dist/dts/Sharding.d.ts +109 -131
- package/dist/dts/Sharding.d.ts.map +1 -1
- package/dist/dts/ShardingConfig.d.ts +147 -44
- package/dist/dts/ShardingConfig.d.ts.map +1 -1
- package/dist/dts/ShardingRegistrationEvent.d.ts +38 -23
- package/dist/dts/ShardingRegistrationEvent.d.ts.map +1 -1
- package/dist/dts/Singleton.d.ts +13 -0
- package/dist/dts/Singleton.d.ts.map +1 -0
- package/dist/dts/SingletonAddress.d.ts +49 -0
- package/dist/dts/SingletonAddress.d.ts.map +1 -0
- package/dist/dts/Snowflake.d.ts +121 -0
- package/dist/dts/Snowflake.d.ts.map +1 -0
- package/dist/dts/SocketRunner.d.ts +22 -0
- package/dist/dts/SocketRunner.d.ts.map +1 -0
- package/dist/dts/SocketShardManager.d.ts +17 -0
- package/dist/dts/SocketShardManager.d.ts.map +1 -0
- package/dist/dts/SqlMessageStorage.d.ts +43 -0
- package/dist/dts/SqlMessageStorage.d.ts.map +1 -0
- package/dist/dts/SqlShardStorage.d.ts +38 -0
- package/dist/dts/SqlShardStorage.d.ts.map +1 -0
- package/dist/dts/SynchronizedClock.d.ts +19 -0
- package/dist/dts/SynchronizedClock.d.ts.map +1 -0
- package/dist/dts/index.d.ts +48 -24
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/internal/entityReaper.d.ts +2 -0
- package/dist/dts/internal/entityReaper.d.ts.map +1 -0
- package/dist/dts/internal/hash.d.ts +2 -0
- package/dist/dts/internal/hash.d.ts.map +1 -0
- package/dist/dts/internal/interruptors.d.ts +2 -0
- package/dist/dts/internal/interruptors.d.ts.map +1 -0
- package/dist/dts/internal/resourceMap.d.ts +22 -0
- package/dist/dts/internal/resourceMap.d.ts.map +1 -0
- package/dist/dts/internal/resourceRef.d.ts +25 -0
- package/dist/dts/internal/resourceRef.d.ts.map +1 -0
- package/dist/dts/internal/shardManager.d.ts +1 -11
- package/dist/dts/internal/shardManager.d.ts.map +1 -1
- package/dist/esm/ClusterError.js +164 -0
- package/dist/esm/ClusterError.js.map +1 -0
- package/dist/esm/ClusterMetrics.js +54 -0
- package/dist/esm/ClusterMetrics.js.map +1 -0
- package/dist/esm/ClusterSchema.js +13 -0
- package/dist/esm/ClusterSchema.js.map +1 -0
- package/dist/esm/DeliverAt.js +22 -0
- package/dist/esm/DeliverAt.js.map +1 -0
- package/dist/esm/Entity.js +173 -0
- package/dist/esm/Entity.js.map +1 -0
- package/dist/esm/EntityAddress.js +44 -0
- package/dist/esm/EntityAddress.js.map +1 -0
- package/dist/esm/EntityId.js +10 -0
- package/dist/esm/EntityId.js.map +1 -0
- package/dist/esm/EntityType.js +10 -0
- package/dist/esm/EntityType.js.map +1 -0
- package/dist/esm/Envelope.js +154 -0
- package/dist/esm/Envelope.js.map +1 -0
- package/dist/esm/HttpCommon.js +38 -0
- package/dist/esm/HttpCommon.js.map +1 -0
- package/dist/esm/HttpRunner.js +98 -0
- package/dist/esm/HttpRunner.js.map +1 -0
- package/dist/esm/HttpShardManager.js +128 -0
- package/dist/esm/HttpShardManager.js.map +1 -0
- package/dist/esm/MachineId.js +17 -0
- package/dist/esm/MachineId.js.map +1 -0
- package/dist/esm/Message.js +88 -17
- package/dist/esm/Message.js.map +1 -1
- package/dist/esm/MessageStorage.js +345 -0
- package/dist/esm/MessageStorage.js.map +1 -0
- package/dist/esm/Reply.js +184 -0
- package/dist/esm/Reply.js.map +1 -0
- package/dist/esm/Runner.js +68 -0
- package/dist/esm/Runner.js.map +1 -0
- package/dist/esm/RunnerAddress.js +52 -0
- package/dist/esm/RunnerAddress.js.map +1 -0
- package/dist/esm/RunnerHealth.js +58 -0
- package/dist/esm/RunnerHealth.js.map +1 -0
- package/dist/esm/RunnerServer.js +116 -0
- package/dist/esm/RunnerServer.js.map +1 -0
- package/dist/esm/Runners.js +332 -0
- package/dist/esm/Runners.js.map +1 -0
- package/dist/esm/ShardId.js +5 -42
- package/dist/esm/ShardId.js.map +1 -1
- package/dist/esm/ShardManager.js +486 -7
- package/dist/esm/ShardManager.js.map +1 -1
- package/dist/esm/ShardStorage.js +129 -0
- package/dist/esm/ShardStorage.js.map +1 -0
- package/dist/esm/Sharding.js +730 -87
- package/dist/esm/Sharding.js.map +1 -1
- package/dist/esm/ShardingConfig.js +80 -17
- package/dist/esm/ShardingConfig.js.map +1 -1
- package/dist/esm/ShardingRegistrationEvent.js +19 -29
- package/dist/esm/ShardingRegistrationEvent.js.map +1 -1
- package/dist/esm/Singleton.js +15 -0
- package/dist/esm/Singleton.js.map +1 -0
- package/dist/esm/SingletonAddress.js +40 -0
- package/dist/esm/SingletonAddress.js.map +1 -0
- package/dist/esm/Snowflake.js +117 -0
- package/dist/esm/Snowflake.js.map +1 -0
- package/dist/esm/SocketRunner.js +31 -0
- package/dist/esm/SocketRunner.js.map +1 -0
- package/dist/esm/SocketShardManager.js +24 -0
- package/dist/esm/SocketShardManager.js.map +1 -0
- package/dist/esm/SqlMessageStorage.js +658 -0
- package/dist/esm/SqlMessageStorage.js.map +1 -0
- package/dist/esm/SqlShardStorage.js +218 -0
- package/dist/esm/SqlShardStorage.js.map +1 -0
- package/dist/esm/SynchronizedClock.js +57 -0
- package/dist/esm/SynchronizedClock.js.map +1 -0
- package/dist/esm/index.js +48 -24
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/entityManager.js +311 -142
- package/dist/esm/internal/entityManager.js.map +1 -1
- package/dist/esm/internal/entityReaper.js +38 -0
- package/dist/esm/internal/entityReaper.js.map +1 -0
- package/dist/esm/internal/hash.js +12 -0
- package/dist/esm/internal/hash.js.map +1 -0
- package/dist/esm/internal/interruptors.js +3 -0
- package/dist/esm/internal/interruptors.js.map +1 -0
- package/dist/esm/internal/resourceMap.js +79 -0
- package/dist/esm/internal/resourceMap.js.map +1 -0
- package/dist/esm/internal/resourceRef.js +83 -0
- package/dist/esm/internal/resourceRef.js.map +1 -0
- package/dist/esm/internal/shardManager.js +217 -233
- package/dist/esm/internal/shardManager.js.map +1 -1
- package/package.json +212 -154
- package/src/ClusterError.ts +193 -0
- package/src/ClusterMetrics.ts +62 -0
- package/src/ClusterSchema.ts +13 -0
- package/src/DeliverAt.ts +36 -0
- package/src/Entity.ts +438 -0
- package/src/EntityAddress.ts +55 -0
- package/src/EntityId.ts +16 -0
- package/src/EntityType.ts +16 -0
- package/src/Envelope.ts +352 -0
- package/src/HttpCommon.ts +73 -0
- package/src/HttpRunner.ts +196 -0
- package/src/HttpShardManager.ts +273 -0
- package/src/MachineId.ts +27 -0
- package/src/Message.ts +143 -92
- package/src/MessageStorage.ts +697 -0
- package/src/Reply.ts +295 -0
- package/src/Runner.ts +84 -0
- package/src/RunnerAddress.ts +61 -0
- package/src/RunnerHealth.ts +87 -0
- package/src/RunnerServer.ts +156 -0
- package/src/Runners.ts +533 -0
- package/src/ShardId.ts +10 -62
- package/src/ShardManager.ts +780 -29
- package/src/ShardStorage.ts +289 -0
- package/src/Sharding.ts +1060 -183
- package/src/ShardingConfig.ts +186 -45
- package/src/ShardingRegistrationEvent.ts +38 -39
- package/src/Singleton.ts +20 -0
- package/src/SingletonAddress.ts +47 -0
- package/src/Snowflake.ts +194 -0
- package/src/SocketRunner.ts +59 -0
- package/src/SocketShardManager.ts +48 -0
- package/src/SqlMessageStorage.ts +833 -0
- package/src/SqlShardStorage.ts +292 -0
- package/src/SynchronizedClock.ts +82 -0
- package/src/index.ts +54 -24
- package/src/internal/entityManager.ts +464 -361
- package/src/internal/entityReaper.ts +53 -0
- package/src/internal/hash.ts +11 -0
- package/src/internal/interruptors.ts +4 -0
- package/src/internal/resourceMap.ts +89 -0
- package/src/internal/resourceRef.ts +88 -0
- package/src/internal/shardManager.ts +273 -546
- package/AtLeastOnce/package.json +0 -6
- package/AtLeastOnceStorage/package.json +0 -6
- package/Broadcaster/package.json +0 -6
- package/ManagerConfig/package.json +0 -6
- package/MessageState/package.json +0 -6
- package/Messenger/package.json +0 -6
- package/Pod/package.json +0 -6
- package/PodAddress/package.json +0 -6
- package/Pods/package.json +0 -6
- package/PodsHealth/package.json +0 -6
- package/PoisonPill/package.json +0 -6
- package/RecipientAddress/package.json +0 -6
- package/RecipientBehaviour/package.json +0 -6
- package/RecipientBehaviourContext/package.json +0 -6
- package/RecipientType/package.json +0 -6
- package/Serialization/package.json +0 -6
- package/SerializedEnvelope/package.json +0 -6
- package/SerializedMessage/package.json +0 -6
- package/ShardManagerClient/package.json +0 -6
- package/ShardingEvent/package.json +0 -6
- package/ShardingException/package.json +0 -6
- package/Storage/package.json +0 -6
- package/dist/cjs/AtLeastOnce.js.map +0 -1
- package/dist/cjs/AtLeastOnceStorage.js.map +0 -1
- package/dist/cjs/Broadcaster.js +0 -6
- package/dist/cjs/Broadcaster.js.map +0 -1
- package/dist/cjs/ManagerConfig.js.map +0 -1
- package/dist/cjs/MessageState.js +0 -55
- package/dist/cjs/MessageState.js.map +0 -1
- package/dist/cjs/Messenger.js +0 -6
- package/dist/cjs/Messenger.js.map +0 -1
- package/dist/cjs/Pod.js +0 -78
- package/dist/cjs/Pod.js.map +0 -1
- package/dist/cjs/PodAddress.js +0 -77
- package/dist/cjs/PodAddress.js.map +0 -1
- package/dist/cjs/Pods.js.map +0 -1
- package/dist/cjs/PodsHealth.js +0 -41
- package/dist/cjs/PodsHealth.js.map +0 -1
- package/dist/cjs/PoisonPill.js +0 -78
- package/dist/cjs/PoisonPill.js.map +0 -1
- package/dist/cjs/RecipientAddress.js +0 -79
- package/dist/cjs/RecipientAddress.js.map +0 -1
- package/dist/cjs/RecipientBehaviour.js +0 -38
- package/dist/cjs/RecipientBehaviour.js.map +0 -1
- package/dist/cjs/RecipientBehaviourContext.js +0 -64
- package/dist/cjs/RecipientBehaviourContext.js.map +0 -1
- package/dist/cjs/RecipientType.js +0 -123
- package/dist/cjs/RecipientType.js.map +0 -1
- package/dist/cjs/Serialization.js +0 -32
- package/dist/cjs/Serialization.js.map +0 -1
- package/dist/cjs/SerializedEnvelope.js +0 -87
- package/dist/cjs/SerializedEnvelope.js.map +0 -1
- package/dist/cjs/SerializedMessage.js +0 -64
- package/dist/cjs/SerializedMessage.js.map +0 -1
- package/dist/cjs/ShardManagerClient.js.map +0 -1
- package/dist/cjs/ShardingEvent.js +0 -72
- package/dist/cjs/ShardingEvent.js.map +0 -1
- package/dist/cjs/ShardingException.js +0 -107
- package/dist/cjs/ShardingException.js.map +0 -1
- package/dist/cjs/Storage.js +0 -40
- package/dist/cjs/Storage.js.map +0 -1
- package/dist/cjs/internal/atLeastOnce.js +0 -35
- package/dist/cjs/internal/atLeastOnce.js.map +0 -1
- package/dist/cjs/internal/atLeastOnceStorage.js +0 -163
- package/dist/cjs/internal/atLeastOnceStorage.js.map +0 -1
- package/dist/cjs/internal/entityState.js +0 -47
- package/dist/cjs/internal/entityState.js.map +0 -1
- package/dist/cjs/internal/managerConfig.js +0 -46
- package/dist/cjs/internal/managerConfig.js.map +0 -1
- package/dist/cjs/internal/message.js +0 -48
- package/dist/cjs/internal/message.js.map +0 -1
- package/dist/cjs/internal/messageState.js +0 -79
- package/dist/cjs/internal/messageState.js.map +0 -1
- package/dist/cjs/internal/podWithMetadata.js +0 -54
- package/dist/cjs/internal/podWithMetadata.js.map +0 -1
- package/dist/cjs/internal/pods.js +0 -35
- package/dist/cjs/internal/pods.js.map +0 -1
- package/dist/cjs/internal/podsHealth.js +0 -40
- package/dist/cjs/internal/podsHealth.js.map +0 -1
- package/dist/cjs/internal/recipientBehaviour.js +0 -52
- package/dist/cjs/internal/recipientBehaviour.js.map +0 -1
- package/dist/cjs/internal/recipientBehaviourContext.js +0 -36
- package/dist/cjs/internal/recipientBehaviourContext.js.map +0 -1
- package/dist/cjs/internal/serialization.js +0 -48
- package/dist/cjs/internal/serialization.js.map +0 -1
- package/dist/cjs/internal/shardManagerClient.js +0 -48
- package/dist/cjs/internal/shardManagerClient.js.map +0 -1
- package/dist/cjs/internal/shardManagerState.js +0 -44
- package/dist/cjs/internal/shardManagerState.js.map +0 -1
- package/dist/cjs/internal/sharding.js +0 -306
- package/dist/cjs/internal/sharding.js.map +0 -1
- package/dist/cjs/internal/shardingConfig.js +0 -56
- package/dist/cjs/internal/shardingConfig.js.map +0 -1
- package/dist/cjs/internal/storage.js +0 -52
- package/dist/cjs/internal/storage.js.map +0 -1
- package/dist/cjs/internal/utils.js +0 -69
- package/dist/cjs/internal/utils.js.map +0 -1
- package/dist/dts/AtLeastOnce.d.ts +0 -20
- package/dist/dts/AtLeastOnce.d.ts.map +0 -1
- package/dist/dts/AtLeastOnceStorage.d.ts +0 -75
- package/dist/dts/AtLeastOnceStorage.d.ts.map +0 -1
- package/dist/dts/Broadcaster.d.ts +0 -32
- package/dist/dts/Broadcaster.d.ts.map +0 -1
- package/dist/dts/ManagerConfig.d.ts +0 -61
- package/dist/dts/ManagerConfig.d.ts.map +0 -1
- package/dist/dts/MessageState.d.ts +0 -107
- package/dist/dts/MessageState.d.ts.map +0 -1
- package/dist/dts/Messenger.d.ts +0 -32
- package/dist/dts/Messenger.d.ts.map +0 -1
- package/dist/dts/Pod.d.ts +0 -81
- package/dist/dts/Pod.d.ts.map +0 -1
- package/dist/dts/PodAddress.d.ts +0 -80
- package/dist/dts/PodAddress.d.ts.map +0 -1
- package/dist/dts/Pods.d.ts +0 -78
- package/dist/dts/Pods.d.ts.map +0 -1
- package/dist/dts/PodsHealth.d.ts +0 -66
- package/dist/dts/PodsHealth.d.ts.map +0 -1
- package/dist/dts/PoisonPill.d.ts +0 -78
- package/dist/dts/PoisonPill.d.ts.map +0 -1
- package/dist/dts/RecipientAddress.d.ts +0 -57
- package/dist/dts/RecipientAddress.d.ts.map +0 -1
- package/dist/dts/RecipientBehaviour.d.ts +0 -72
- package/dist/dts/RecipientBehaviour.d.ts.map +0 -1
- package/dist/dts/RecipientBehaviourContext.d.ts +0 -83
- package/dist/dts/RecipientBehaviourContext.d.ts.map +0 -1
- package/dist/dts/RecipientType.d.ts +0 -93
- package/dist/dts/RecipientType.d.ts.map +0 -1
- package/dist/dts/Serialization.d.ts +0 -58
- package/dist/dts/Serialization.d.ts.map +0 -1
- package/dist/dts/SerializedEnvelope.d.ts +0 -86
- package/dist/dts/SerializedEnvelope.d.ts.map +0 -1
- package/dist/dts/SerializedMessage.d.ts +0 -66
- package/dist/dts/SerializedMessage.d.ts.map +0 -1
- package/dist/dts/ShardManagerClient.d.ts +0 -50
- package/dist/dts/ShardManagerClient.d.ts.map +0 -1
- package/dist/dts/ShardingEvent.d.ts +0 -90
- package/dist/dts/ShardingEvent.d.ts.map +0 -1
- package/dist/dts/ShardingException.d.ts +0 -125
- package/dist/dts/ShardingException.d.ts.map +0 -1
- package/dist/dts/Storage.d.ts +0 -78
- package/dist/dts/Storage.d.ts.map +0 -1
- package/dist/dts/internal/atLeastOnce.d.ts +0 -2
- package/dist/dts/internal/atLeastOnce.d.ts.map +0 -1
- package/dist/dts/internal/atLeastOnceStorage.d.ts +0 -2
- package/dist/dts/internal/atLeastOnceStorage.d.ts.map +0 -1
- package/dist/dts/internal/entityState.d.ts +0 -21
- package/dist/dts/internal/entityState.d.ts.map +0 -1
- package/dist/dts/internal/managerConfig.d.ts +0 -2
- package/dist/dts/internal/managerConfig.d.ts.map +0 -1
- package/dist/dts/internal/message.d.ts +0 -9
- package/dist/dts/internal/message.d.ts.map +0 -1
- package/dist/dts/internal/messageState.d.ts +0 -2
- package/dist/dts/internal/messageState.d.ts.map +0 -1
- package/dist/dts/internal/podWithMetadata.d.ts +0 -2
- package/dist/dts/internal/podWithMetadata.d.ts.map +0 -1
- package/dist/dts/internal/pods.d.ts +0 -2
- package/dist/dts/internal/pods.d.ts.map +0 -1
- package/dist/dts/internal/podsHealth.d.ts +0 -2
- package/dist/dts/internal/podsHealth.d.ts.map +0 -1
- package/dist/dts/internal/recipientBehaviour.d.ts +0 -2
- package/dist/dts/internal/recipientBehaviour.d.ts.map +0 -1
- package/dist/dts/internal/recipientBehaviourContext.d.ts +0 -2
- package/dist/dts/internal/recipientBehaviourContext.d.ts.map +0 -1
- package/dist/dts/internal/serialization.d.ts +0 -2
- package/dist/dts/internal/serialization.d.ts.map +0 -1
- package/dist/dts/internal/shardManagerClient.d.ts +0 -2
- package/dist/dts/internal/shardManagerClient.d.ts.map +0 -1
- package/dist/dts/internal/shardManagerState.d.ts +0 -26
- package/dist/dts/internal/shardManagerState.d.ts.map +0 -1
- package/dist/dts/internal/sharding.d.ts +0 -2
- package/dist/dts/internal/sharding.d.ts.map +0 -1
- package/dist/dts/internal/shardingConfig.d.ts +0 -2
- package/dist/dts/internal/shardingConfig.d.ts.map +0 -1
- package/dist/dts/internal/storage.d.ts +0 -2
- package/dist/dts/internal/storage.d.ts.map +0 -1
- package/dist/dts/internal/utils.d.ts +0 -2
- package/dist/dts/internal/utils.d.ts.map +0 -1
- package/dist/esm/AtLeastOnce.js +0 -12
- package/dist/esm/AtLeastOnce.js.map +0 -1
- package/dist/esm/AtLeastOnceStorage.js +0 -17
- package/dist/esm/AtLeastOnceStorage.js.map +0 -1
- package/dist/esm/Broadcaster.js +0 -2
- package/dist/esm/Broadcaster.js.map +0 -1
- package/dist/esm/ManagerConfig.js +0 -26
- package/dist/esm/ManagerConfig.js.map +0 -1
- package/dist/esm/MessageState.js +0 -47
- package/dist/esm/MessageState.js.map +0 -1
- package/dist/esm/Messenger.js +0 -2
- package/dist/esm/Messenger.js.map +0 -1
- package/dist/esm/Pod.js +0 -65
- package/dist/esm/Pod.js.map +0 -1
- package/dist/esm/PodAddress.js +0 -64
- package/dist/esm/PodAddress.js.map +0 -1
- package/dist/esm/Pods.js +0 -27
- package/dist/esm/Pods.js.map +0 -1
- package/dist/esm/PodsHealth.js +0 -33
- package/dist/esm/PodsHealth.js.map +0 -1
- package/dist/esm/PoisonPill.js +0 -65
- package/dist/esm/PoisonPill.js.map +0 -1
- package/dist/esm/RecipientAddress.js +0 -67
- package/dist/esm/RecipientAddress.js.map +0 -1
- package/dist/esm/RecipientBehaviour.js +0 -30
- package/dist/esm/RecipientBehaviour.js.map +0 -1
- package/dist/esm/RecipientBehaviourContext.js +0 -56
- package/dist/esm/RecipientBehaviourContext.js.map +0 -1
- package/dist/esm/RecipientType.js +0 -108
- package/dist/esm/RecipientType.js.map +0 -1
- package/dist/esm/Serialization.js +0 -24
- package/dist/esm/Serialization.js.map +0 -1
- package/dist/esm/SerializedEnvelope.js +0 -74
- package/dist/esm/SerializedEnvelope.js.map +0 -1
- package/dist/esm/SerializedMessage.js +0 -51
- package/dist/esm/SerializedMessage.js.map +0 -1
- package/dist/esm/ShardManagerClient.js +0 -22
- package/dist/esm/ShardManagerClient.js.map +0 -1
- package/dist/esm/ShardingEvent.js +0 -62
- package/dist/esm/ShardingEvent.js.map +0 -1
- package/dist/esm/ShardingException.js +0 -91
- package/dist/esm/ShardingException.js.map +0 -1
- package/dist/esm/Storage.js +0 -32
- package/dist/esm/Storage.js.map +0 -1
- package/dist/esm/internal/atLeastOnce.js +0 -26
- package/dist/esm/internal/atLeastOnce.js.map +0 -1
- package/dist/esm/internal/atLeastOnceStorage.js +0 -154
- package/dist/esm/internal/atLeastOnceStorage.js.map +0 -1
- package/dist/esm/internal/entityState.js +0 -35
- package/dist/esm/internal/entityState.js.map +0 -1
- package/dist/esm/internal/managerConfig.js +0 -38
- package/dist/esm/internal/managerConfig.js.map +0 -1
- package/dist/esm/internal/message.js +0 -35
- package/dist/esm/internal/message.js.map +0 -1
- package/dist/esm/internal/messageState.js +0 -66
- package/dist/esm/internal/messageState.js.map +0 -1
- package/dist/esm/internal/podWithMetadata.js +0 -41
- package/dist/esm/internal/podWithMetadata.js.map +0 -1
- package/dist/esm/internal/pods.js +0 -25
- package/dist/esm/internal/pods.js.map +0 -1
- package/dist/esm/internal/podsHealth.js +0 -30
- package/dist/esm/internal/podsHealth.js.map +0 -1
- package/dist/esm/internal/recipientBehaviour.js +0 -42
- package/dist/esm/internal/recipientBehaviour.js.map +0 -1
- package/dist/esm/internal/recipientBehaviourContext.js +0 -26
- package/dist/esm/internal/recipientBehaviourContext.js.map +0 -1
- package/dist/esm/internal/serialization.js +0 -38
- package/dist/esm/internal/serialization.js.map +0 -1
- package/dist/esm/internal/shardManagerClient.js +0 -38
- package/dist/esm/internal/shardManagerClient.js.map +0 -1
- package/dist/esm/internal/shardManagerState.js +0 -36
- package/dist/esm/internal/shardManagerState.js.map +0 -1
- package/dist/esm/internal/sharding.js +0 -288
- package/dist/esm/internal/sharding.js.map +0 -1
- package/dist/esm/internal/shardingConfig.js +0 -47
- package/dist/esm/internal/shardingConfig.js.map +0 -1
- package/dist/esm/internal/storage.js +0 -42
- package/dist/esm/internal/storage.js.map +0 -1
- package/dist/esm/internal/utils.js +0 -56
- package/dist/esm/internal/utils.js.map +0 -1
- package/src/AtLeastOnce.ts +0 -28
- package/src/AtLeastOnceStorage.ts +0 -96
- package/src/Broadcaster.ts +0 -48
- package/src/ManagerConfig.ts +0 -67
- package/src/MessageState.ts +0 -126
- package/src/Messenger.ts +0 -40
- package/src/Pod.ts +0 -95
- package/src/PodAddress.ts +0 -94
- package/src/Pods.ts +0 -100
- package/src/PodsHealth.ts +0 -74
- package/src/PoisonPill.ts +0 -105
- package/src/RecipientAddress.ts +0 -72
- package/src/RecipientBehaviour.ts +0 -108
- package/src/RecipientBehaviourContext.ts +0 -101
- package/src/RecipientType.ts +0 -134
- package/src/Serialization.ts +0 -72
- package/src/SerializedEnvelope.ts +0 -108
- package/src/SerializedMessage.ts +0 -82
- package/src/ShardManagerClient.ts +0 -57
- package/src/ShardingEvent.ts +0 -121
- package/src/ShardingException.ts +0 -151
- package/src/Storage.ts +0 -92
- package/src/internal/atLeastOnce.ts +0 -59
- package/src/internal/atLeastOnceStorage.ts +0 -218
- package/src/internal/entityState.ts +0 -64
- package/src/internal/managerConfig.ts +0 -84
- package/src/internal/message.ts +0 -63
- package/src/internal/messageState.ts +0 -98
- package/src/internal/podWithMetadata.ts +0 -72
- package/src/internal/pods.ts +0 -29
- package/src/internal/podsHealth.ts +0 -39
- package/src/internal/recipientBehaviour.ts +0 -133
- package/src/internal/recipientBehaviourContext.ts +0 -70
- package/src/internal/serialization.ts +0 -63
- package/src/internal/shardManagerClient.ts +0 -49
- package/src/internal/shardManagerState.ts +0 -80
- package/src/internal/sharding.ts +0 -789
- package/src/internal/shardingConfig.ts +0 -97
- package/src/internal/storage.ts +0 -60
- package/src/internal/utils.ts +0 -54
package/dist/cjs/Sharding.js
CHANGED
@@ -3,104 +3,748 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.
|
7
|
-
var
|
6
|
+
exports.make = exports.layer = exports.Sharding = void 0;
|
7
|
+
var RpcClient = _interopRequireWildcard(require("@effect/rpc/RpcClient"));
|
8
|
+
var _RpcMessage = require("@effect/rpc/RpcMessage");
|
9
|
+
var Arr = _interopRequireWildcard(require("effect/Array"));
|
10
|
+
var Cause = _interopRequireWildcard(require("effect/Cause"));
|
11
|
+
var Context = _interopRequireWildcard(require("effect/Context"));
|
12
|
+
var Effect = _interopRequireWildcard(require("effect/Effect"));
|
13
|
+
var Equal = _interopRequireWildcard(require("effect/Equal"));
|
14
|
+
var Fiber = _interopRequireWildcard(require("effect/Fiber"));
|
15
|
+
var FiberHandle = _interopRequireWildcard(require("effect/FiberHandle"));
|
16
|
+
var FiberMap = _interopRequireWildcard(require("effect/FiberMap"));
|
17
|
+
var FiberRef = _interopRequireWildcard(require("effect/FiberRef"));
|
18
|
+
var _Function = require("effect/Function");
|
19
|
+
var HashMap = _interopRequireWildcard(require("effect/HashMap"));
|
20
|
+
var Iterable = _interopRequireWildcard(require("effect/Iterable"));
|
21
|
+
var Layer = _interopRequireWildcard(require("effect/Layer"));
|
22
|
+
var MutableHashMap = _interopRequireWildcard(require("effect/MutableHashMap"));
|
23
|
+
var MutableRef = _interopRequireWildcard(require("effect/MutableRef"));
|
24
|
+
var Option = _interopRequireWildcard(require("effect/Option"));
|
25
|
+
var Predicate = _interopRequireWildcard(require("effect/Predicate"));
|
26
|
+
var PubSub = _interopRequireWildcard(require("effect/PubSub"));
|
27
|
+
var Schedule = _interopRequireWildcard(require("effect/Schedule"));
|
28
|
+
var Scope = _interopRequireWildcard(require("effect/Scope"));
|
29
|
+
var Stream = _interopRequireWildcard(require("effect/Stream"));
|
30
|
+
var _ClusterError = require("./ClusterError.js");
|
31
|
+
var _ClusterSchema = require("./ClusterSchema.js");
|
32
|
+
var _EntityAddress = require("./EntityAddress.js");
|
33
|
+
var _EntityId = require("./EntityId.js");
|
34
|
+
var Envelope = _interopRequireWildcard(require("./Envelope.js"));
|
35
|
+
var EntityManager = _interopRequireWildcard(require("./internal/entityManager.js"));
|
36
|
+
var _entityReaper = require("./internal/entityReaper.js");
|
37
|
+
var _hash = require("./internal/hash.js");
|
38
|
+
var _interruptors = require("./internal/interruptors.js");
|
39
|
+
var _resourceMap = require("./internal/resourceMap.js");
|
40
|
+
var Message = _interopRequireWildcard(require("./Message.js"));
|
41
|
+
var MessageStorage = _interopRequireWildcard(require("./MessageStorage.js"));
|
42
|
+
var Reply = _interopRequireWildcard(require("./Reply.js"));
|
43
|
+
var _Runners = require("./Runners.js");
|
44
|
+
var _ShardId = require("./ShardId.js");
|
45
|
+
var _ShardingConfig = require("./ShardingConfig.js");
|
46
|
+
var _ShardingRegistrationEvent = require("./ShardingRegistrationEvent.js");
|
47
|
+
var _ShardManager = require("./ShardManager.js");
|
48
|
+
var _ShardStorage = require("./ShardStorage.js");
|
49
|
+
var _SingletonAddress = require("./SingletonAddress.js");
|
50
|
+
var Snowflake = _interopRequireWildcard(require("./Snowflake.js"));
|
8
51
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
9
52
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
10
53
|
/**
|
11
54
|
* @since 1.0.0
|
12
|
-
* @category
|
55
|
+
* @category models
|
13
56
|
*/
|
14
|
-
|
57
|
+
class Sharding extends /*#__PURE__*/Context.Tag("@effect/cluster/Sharding")() {}
|
15
58
|
/**
|
16
59
|
* @since 1.0.0
|
17
|
-
* @category
|
60
|
+
* @category constructors
|
18
61
|
*/
|
19
|
-
|
62
|
+
exports.Sharding = Sharding;
|
63
|
+
const make = exports.make = /*#__PURE__*/Effect.gen(function* () {
|
64
|
+
const config = yield* _ShardingConfig.ShardingConfig;
|
65
|
+
const runners = yield* _Runners.Runners;
|
66
|
+
const shardManager = yield* _ShardManager.ShardManagerClient;
|
67
|
+
const snowflakeGen = yield* Snowflake.Generator;
|
68
|
+
const shardingScope = yield* Effect.scope;
|
69
|
+
const isShutdown = MutableRef.make(false);
|
70
|
+
const storage = yield* MessageStorage.MessageStorage;
|
71
|
+
const storageEnabled = storage !== MessageStorage.noop;
|
72
|
+
const shardStorage = yield* _ShardStorage.ShardStorage;
|
73
|
+
const entityManagers = new Map();
|
74
|
+
const shardAssignments = MutableHashMap.empty();
|
75
|
+
const selfShards = new Set();
|
76
|
+
// the active shards are the ones that we have acquired the lock for
|
77
|
+
const acquiredShards = new Set();
|
78
|
+
const activeShardsLatch = yield* Effect.makeLatch(false);
|
79
|
+
const events = yield* PubSub.unbounded();
|
80
|
+
const getRegistrationEvents = Stream.fromPubSub(events);
|
81
|
+
const isLocalRunner = address => Option.isSome(config.runnerAddress) && Equal.equals(address, config.runnerAddress.value);
|
82
|
+
function getShardId(entityId) {
|
83
|
+
return _ShardId.ShardId.make(Math.abs((0, _hash.hashString)(entityId) % config.numberOfShards) + 1);
|
84
|
+
}
|
85
|
+
function isEntityOnLocalShards(address) {
|
86
|
+
return acquiredShards.has(address.shardId);
|
87
|
+
}
|
88
|
+
// --- Shard acquisition ---
|
89
|
+
if (Option.isSome(config.runnerAddress)) {
|
90
|
+
const selfAddress = config.runnerAddress.value;
|
91
|
+
yield* Scope.addFinalizerExit(shardingScope, () => {
|
92
|
+
// the locks expire over time, so if this fails we ignore it
|
93
|
+
return Effect.ignore(shardStorage.releaseAll(selfAddress));
|
94
|
+
});
|
95
|
+
const releasingShards = new Set();
|
96
|
+
yield* Effect.gen(function* () {
|
97
|
+
while (true) {
|
98
|
+
yield* activeShardsLatch.await;
|
99
|
+
// if a shard is no longer assigned to this runner, we release it
|
100
|
+
for (const shardId of acquiredShards) {
|
101
|
+
if (selfShards.has(shardId)) continue;
|
102
|
+
acquiredShards.delete(shardId);
|
103
|
+
releasingShards.add(shardId);
|
104
|
+
}
|
105
|
+
// if a shard has been assigned to this runner, we acquire it
|
106
|
+
const unacquiredShards = new Set();
|
107
|
+
for (const shardId of selfShards) {
|
108
|
+
if (acquiredShards.has(shardId) || releasingShards.has(shardId)) continue;
|
109
|
+
unacquiredShards.add(shardId);
|
110
|
+
}
|
111
|
+
if (releasingShards.size > 0) {
|
112
|
+
yield* Effect.forkIn(syncSingletons, shardingScope);
|
113
|
+
yield* releaseShards;
|
114
|
+
}
|
115
|
+
if (unacquiredShards.size === 0) {
|
116
|
+
yield* activeShardsLatch.close;
|
117
|
+
continue;
|
118
|
+
}
|
119
|
+
const acquired = yield* shardStorage.acquire(selfAddress, unacquiredShards);
|
120
|
+
for (const shardId of acquired) {
|
121
|
+
acquiredShards.add(shardId);
|
122
|
+
}
|
123
|
+
if (acquired.length > 0) {
|
124
|
+
yield* storageReadLatch.open;
|
125
|
+
yield* Effect.forkIn(syncSingletons, shardingScope);
|
126
|
+
}
|
127
|
+
yield* Effect.sleep(1000);
|
128
|
+
}
|
129
|
+
}).pipe(Effect.catchAllCause(cause => Effect.logWarning("Could not acquire/release shards", cause)), Effect.repeat(Schedule.spaced(config.entityMessagePollInterval)), Effect.annotateLogs({
|
130
|
+
package: "@effect/cluster",
|
131
|
+
module: "Sharding",
|
132
|
+
fiber: "Shard acquisition loop",
|
133
|
+
runner: selfAddress
|
134
|
+
}), Effect.interruptible, Effect.forkIn(shardingScope));
|
135
|
+
// refresh the shard locks every minute
|
136
|
+
yield* Effect.suspend(() => shardStorage.refresh(selfAddress, [...acquiredShards, ...releasingShards])).pipe(Effect.flatMap(acquired => {
|
137
|
+
for (const shardId of acquiredShards) {
|
138
|
+
if (!acquired.includes(shardId)) {
|
139
|
+
acquiredShards.delete(shardId);
|
140
|
+
releasingShards.add(shardId);
|
141
|
+
}
|
142
|
+
}
|
143
|
+
return releasingShards.size > 0 ? Effect.andThen(Effect.forkIn(syncSingletons, shardingScope), releaseShards) : Effect.void;
|
144
|
+
}), Effect.retry({
|
145
|
+
times: 5,
|
146
|
+
schedule: Schedule.spaced(250)
|
147
|
+
}), Effect.catchAllCause(cause => Effect.logError("Could not refresh shard locks", cause).pipe(Effect.andThen(clearSelfShards))), Effect.delay("1 minute"), Effect.forever, Effect.interruptible, Effect.forkIn(shardingScope));
|
148
|
+
const releaseShardsLock = Effect.unsafeMakeSemaphore(1).withPermits(1);
|
149
|
+
const releaseShards = releaseShardsLock(Effect.suspend(() => Effect.forEach(releasingShards, shardId => Effect.forEach(entityManagers.values(), state => state.manager.interruptShard(shardId), {
|
150
|
+
concurrency: "unbounded",
|
151
|
+
discard: true
|
152
|
+
}).pipe(Effect.andThen(shardStorage.release(selfAddress, shardId)), Effect.annotateLogs({
|
153
|
+
runner: selfAddress
|
154
|
+
}), Effect.andThen(() => {
|
155
|
+
releasingShards.delete(shardId);
|
156
|
+
})), {
|
157
|
+
concurrency: "unbounded",
|
158
|
+
discard: true
|
159
|
+
})));
|
160
|
+
}
|
161
|
+
const clearSelfShards = Effect.suspend(() => {
|
162
|
+
selfShards.clear();
|
163
|
+
return activeShardsLatch.open;
|
164
|
+
});
|
165
|
+
// --- Singletons ---
|
166
|
+
const singletons = new Map();
|
167
|
+
const singletonFibers = yield* FiberMap.make();
|
168
|
+
const withSingletonLock = Effect.unsafeMakeSemaphore(1).withPermits(1);
|
169
|
+
const registerSingleton = Effect.fnUntraced(function* (name, run) {
|
170
|
+
const address = new _SingletonAddress.SingletonAddress({
|
171
|
+
shardId: getShardId(_EntityId.EntityId.make(name)),
|
172
|
+
name
|
173
|
+
});
|
174
|
+
let map = singletons.get(address.shardId);
|
175
|
+
if (!map) {
|
176
|
+
map = MutableHashMap.empty();
|
177
|
+
singletons.set(address.shardId, map);
|
178
|
+
}
|
179
|
+
if (MutableHashMap.has(map, address)) {
|
180
|
+
return yield* Effect.dieMessage(`Singleton '${name}' is already registered`);
|
181
|
+
}
|
182
|
+
const context = yield* Effect.context();
|
183
|
+
const wrappedRun = run.pipe(Effect.locally(FiberRef.currentLogAnnotations, HashMap.empty()), Effect.andThen(Effect.never), Effect.scoped, Effect.provide(context), Effect.orDie, Effect.interruptible);
|
184
|
+
MutableHashMap.set(map, address, wrappedRun);
|
185
|
+
yield* PubSub.publish(events, (0, _ShardingRegistrationEvent.SingletonRegistered)({
|
186
|
+
address
|
187
|
+
}));
|
188
|
+
// start if we are on the right shard
|
189
|
+
if (acquiredShards.has(address.shardId)) {
|
190
|
+
yield* Effect.logDebug("Starting singleton", address);
|
191
|
+
yield* FiberMap.run(singletonFibers, address, wrappedRun);
|
192
|
+
}
|
193
|
+
}, withSingletonLock);
|
194
|
+
const syncSingletons = withSingletonLock(Effect.gen(function* () {
|
195
|
+
for (const [shardId, map] of singletons) {
|
196
|
+
for (const [address, run] of map) {
|
197
|
+
const running = FiberMap.unsafeHas(singletonFibers, address);
|
198
|
+
const shouldBeRunning = acquiredShards.has(shardId);
|
199
|
+
if (running && !shouldBeRunning) {
|
200
|
+
yield* Effect.logDebug("Stopping singleton", address);
|
201
|
+
_interruptors.internalInterruptors.add(yield* Effect.fiberId);
|
202
|
+
yield* FiberMap.remove(singletonFibers, address);
|
203
|
+
} else if (!running && shouldBeRunning) {
|
204
|
+
yield* Effect.logDebug("Starting singleton", address);
|
205
|
+
yield* FiberMap.run(singletonFibers, address, run);
|
206
|
+
}
|
207
|
+
}
|
208
|
+
}
|
209
|
+
}));
|
210
|
+
// --- Storage inbox ---
|
211
|
+
const storageReadLatch = yield* Effect.makeLatch(true);
|
212
|
+
const openStorageReadLatch = (0, _Function.constant)(storageReadLatch.open);
|
213
|
+
const storageReadLock = Effect.unsafeMakeSemaphore(1);
|
214
|
+
const withStorageReadLock = storageReadLock.withPermits(1);
|
215
|
+
if (storageEnabled && Option.isSome(config.runnerAddress)) {
|
216
|
+
const selfAddress = config.runnerAddress.value;
|
217
|
+
yield* Effect.gen(function* () {
|
218
|
+
yield* Effect.logDebug("Starting");
|
219
|
+
yield* Effect.addFinalizer(() => Effect.logDebug("Shutting down"));
|
220
|
+
// keep track of the last sent request ids to avoid duplicates
|
221
|
+
// we only keep the last 30 sets to avoid memory leaks
|
222
|
+
const sentRequestIds = new Set();
|
223
|
+
const sentRequestIdSets = new Set();
|
224
|
+
while (true) {
|
225
|
+
// wait for the next poll interval, or if we get notified of a change
|
226
|
+
yield* storageReadLatch.await;
|
227
|
+
// if we get notified of a change, ensure we start a read immediately
|
228
|
+
// next iteration
|
229
|
+
storageReadLatch.unsafeClose();
|
230
|
+
// the lock is used to ensure resuming entities have a garantee that no
|
231
|
+
// more items are added to the unprocessed set while the semaphore is
|
232
|
+
// acquired.
|
233
|
+
yield* storageReadLock.take(1);
|
234
|
+
const messages = yield* storage.unprocessedMessages(acquiredShards);
|
235
|
+
const currentSentRequestIds = new Set();
|
236
|
+
sentRequestIdSets.add(currentSentRequestIds);
|
237
|
+
const send = Effect.catchAllCause(Effect.suspend(() => {
|
238
|
+
const message = messages[index];
|
239
|
+
if (message._tag === "IncomingRequest") {
|
240
|
+
if (sentRequestIds.has(message.envelope.requestId)) {
|
241
|
+
return Effect.void;
|
242
|
+
}
|
243
|
+
sentRequestIds.add(message.envelope.requestId);
|
244
|
+
currentSentRequestIds.add(message.envelope.requestId);
|
245
|
+
}
|
246
|
+
const address = message.envelope.address;
|
247
|
+
const state = entityManagers.get(address.entityType);
|
248
|
+
if (!state || !acquiredShards.has(address.shardId)) {
|
249
|
+
return Effect.void;
|
250
|
+
}
|
251
|
+
const isProcessing = state.manager.isProcessingFor(message);
|
252
|
+
// If the message might affect a currently processing request, we
|
253
|
+
// send it to the entity manager to be processed.
|
254
|
+
if (message._tag === "IncomingEnvelope" && isProcessing) {
|
255
|
+
return state.manager.send(message);
|
256
|
+
} else if (isProcessing) {
|
257
|
+
return Effect.void;
|
258
|
+
}
|
259
|
+
// If the entity was resuming in another fiber, we add the message
|
260
|
+
// id to the unprocessed set.
|
261
|
+
const resumptionState = MutableHashMap.get(entityResumptionState, address);
|
262
|
+
if (Option.isSome(resumptionState)) {
|
263
|
+
resumptionState.value.unprocessed.add(message.envelope.requestId);
|
264
|
+
if (message.envelope._tag === "Interrupt") {
|
265
|
+
resumptionState.value.interrupts.set(message.envelope.requestId, message);
|
266
|
+
}
|
267
|
+
return Effect.void;
|
268
|
+
}
|
269
|
+
return state.manager.send(message);
|
270
|
+
}), cause => {
|
271
|
+
const message = messages[index];
|
272
|
+
const error = Cause.failureOption(cause);
|
273
|
+
// if we get a defect, then update storage
|
274
|
+
if (Option.isNone(error)) {
|
275
|
+
return storage.saveReply(Reply.ReplyWithContext.fromDefect({
|
276
|
+
id: snowflakeGen.unsafeNext(),
|
277
|
+
requestId: message.envelope.requestId,
|
278
|
+
defect: Cause.squash(cause)
|
279
|
+
}));
|
280
|
+
}
|
281
|
+
if (error.value._tag === "MailboxFull") {
|
282
|
+
// MailboxFull can only happen for requests, so this cast is safe
|
283
|
+
return resumeEntityFromStorage(message);
|
284
|
+
}
|
285
|
+
return Effect.void;
|
286
|
+
});
|
287
|
+
let index = 0;
|
288
|
+
yield* Effect.whileLoop({
|
289
|
+
while: () => index < messages.length,
|
290
|
+
step: () => index++,
|
291
|
+
body: (0, _Function.constant)(send)
|
292
|
+
});
|
293
|
+
// let the resuming entities check if they are done
|
294
|
+
yield* storageReadLock.release(1);
|
295
|
+
while (sentRequestIdSets.size > 30) {
|
296
|
+
const oldest = Iterable.unsafeHead(sentRequestIdSets);
|
297
|
+
sentRequestIdSets.delete(oldest);
|
298
|
+
for (const id of oldest) {
|
299
|
+
sentRequestIds.delete(id);
|
300
|
+
}
|
301
|
+
}
|
302
|
+
}
|
303
|
+
}).pipe(Effect.scoped, Effect.ensuring(storageReadLock.releaseAll), Effect.catchAllCause(cause => Effect.logWarning("Could not read messages from storage", cause)), Effect.repeat(Schedule.spaced(config.entityMessagePollInterval)), Effect.annotateLogs({
|
304
|
+
package: "@effect/cluster",
|
305
|
+
module: "Sharding",
|
306
|
+
fiber: "Storage read loop",
|
307
|
+
runner: selfAddress
|
308
|
+
}), Effect.interruptible, Effect.forkIn(shardingScope));
|
309
|
+
// open the storage latch every poll interval
|
310
|
+
yield* storageReadLatch.open.pipe(Effect.delay(config.entityMessagePollInterval), Effect.forever, Effect.interruptible, Effect.forkIn(shardingScope));
|
311
|
+
// Resume unprocessed messages for entities that reached a full mailbox.
|
312
|
+
const entityResumptionState = MutableHashMap.empty();
|
313
|
+
const resumeEntityFromStorage = lastReceivedMessage => {
|
314
|
+
const address = lastReceivedMessage.envelope.address;
|
315
|
+
const resumptionState = MutableHashMap.get(entityResumptionState, address);
|
316
|
+
if (Option.isSome(resumptionState)) {
|
317
|
+
resumptionState.value.unprocessed.add(lastReceivedMessage.envelope.requestId);
|
318
|
+
return Effect.void;
|
319
|
+
}
|
320
|
+
MutableHashMap.set(entityResumptionState, address, {
|
321
|
+
unprocessed: new Set([lastReceivedMessage.envelope.requestId]),
|
322
|
+
interrupts: new Map()
|
323
|
+
});
|
324
|
+
return resumeEntityFromStorageImpl(address);
|
325
|
+
};
|
326
|
+
const resumeEntityFromStorageImpl = Effect.fnUntraced(function* (address) {
|
327
|
+
const state = entityManagers.get(address.entityType);
|
328
|
+
if (!state) {
|
329
|
+
MutableHashMap.remove(entityResumptionState, address);
|
330
|
+
return;
|
331
|
+
}
|
332
|
+
const resumptionState = Option.getOrThrow(MutableHashMap.get(entityResumptionState, address));
|
333
|
+
let done = false;
|
334
|
+
while (!done) {
|
335
|
+
// if the shard is no longer assigned to this runner, we stop
|
336
|
+
if (!acquiredShards.has(address.shardId)) {
|
337
|
+
return;
|
338
|
+
}
|
339
|
+
// take a batch of unprocessed messages ids
|
340
|
+
const messageIds = Arr.empty();
|
341
|
+
for (const id of resumptionState.unprocessed) {
|
342
|
+
if (messageIds.length === 1024) break;
|
343
|
+
messageIds.push(id);
|
344
|
+
}
|
345
|
+
const messages = yield* storage.unprocessedMessagesById(messageIds);
|
346
|
+
// this should not happen, but we handle it just in case
|
347
|
+
if (messages.length === 0) {
|
348
|
+
yield* Effect.sleep(config.entityMessagePollInterval);
|
349
|
+
continue;
|
350
|
+
}
|
351
|
+
let index = 0;
|
352
|
+
const sendWithRetry = Effect.catchTags(Effect.suspend(() => {
|
353
|
+
if (!acquiredShards.has(address.shardId)) {
|
354
|
+
return Effect.fail(new _ClusterError.EntityNotManagedByRunner({
|
355
|
+
address
|
356
|
+
}));
|
357
|
+
}
|
358
|
+
const message = messages[index];
|
359
|
+
// check if this is a request that was interrupted
|
360
|
+
const interrupt = message._tag === "IncomingRequest" && resumptionState.interrupts.get(message.envelope.requestId);
|
361
|
+
return interrupt ? Effect.flatMap(state.manager.send(message), () => {
|
362
|
+
resumptionState.interrupts.delete(message.envelope.requestId);
|
363
|
+
return state.manager.send(interrupt);
|
364
|
+
}) : state.manager.send(message);
|
365
|
+
}), {
|
366
|
+
MailboxFull: () => Effect.delay(sendWithRetry, config.sendRetryInterval),
|
367
|
+
AlreadyProcessingMessage: () => Effect.void
|
368
|
+
});
|
369
|
+
yield* Effect.whileLoop({
|
370
|
+
while: () => index < messages.length,
|
371
|
+
body: (0, _Function.constant)(sendWithRetry),
|
372
|
+
step: () => index++
|
373
|
+
});
|
374
|
+
for (const id of messageIds) {
|
375
|
+
resumptionState.unprocessed.delete(id);
|
376
|
+
}
|
377
|
+
if (resumptionState.unprocessed.size > 0) continue;
|
378
|
+
// if we have caught up to the main storage loop, we let it take over
|
379
|
+
yield* withStorageReadLock(Effect.sync(() => {
|
380
|
+
if (resumptionState.unprocessed.size === 0) {
|
381
|
+
MutableHashMap.remove(entityResumptionState, address);
|
382
|
+
done = true;
|
383
|
+
}
|
384
|
+
}));
|
385
|
+
}
|
386
|
+
}, Effect.retry({
|
387
|
+
while: e => e._tag === "PersistenceError",
|
388
|
+
schedule: Schedule.spaced(config.entityMessagePollInterval)
|
389
|
+
}), Effect.catchAllCause(cause => Effect.logError("Could not resume unprocessed messages", cause)), (effect, address) => Effect.annotateLogs(effect, {
|
390
|
+
package: "@effect/cluster",
|
391
|
+
module: "Sharding",
|
392
|
+
fiber: "Resuming unprocessed messages",
|
393
|
+
runner: selfAddress,
|
394
|
+
entity: address
|
395
|
+
}), (effect, address) => Effect.ensuring(effect, Effect.sync(() => MutableHashMap.remove(entityResumptionState, address))), Effect.interruptible, Effect.forkIn(shardingScope));
|
396
|
+
}
|
397
|
+
// --- Sending messages ---
|
398
|
+
const sendLocal = message => Effect.suspend(() => {
|
399
|
+
const address = message.envelope.address;
|
400
|
+
if (!isEntityOnLocalShards(address)) {
|
401
|
+
return Effect.fail(new _ClusterError.EntityNotManagedByRunner({
|
402
|
+
address
|
403
|
+
}));
|
404
|
+
}
|
405
|
+
const state = entityManagers.get(address.entityType);
|
406
|
+
if (!state) {
|
407
|
+
return Effect.fail(new _ClusterError.EntityNotManagedByRunner({
|
408
|
+
address
|
409
|
+
}));
|
410
|
+
}
|
411
|
+
return message._tag === "IncomingRequest" || message._tag === "IncomingEnvelope" ? state.manager.send(message) : runners.sendLocal({
|
412
|
+
message,
|
413
|
+
send: state.manager.sendLocal,
|
414
|
+
simulateRemoteSerialization: config.simulateRemoteSerialization
|
415
|
+
});
|
416
|
+
});
|
417
|
+
const notifyLocal = (message, discard) => Effect.suspend(() => {
|
418
|
+
const address = message.envelope.address;
|
419
|
+
if (!isEntityOnLocalShards(address)) {
|
420
|
+
return Effect.fail(new _ClusterError.EntityNotManagedByRunner({
|
421
|
+
address
|
422
|
+
}));
|
423
|
+
}
|
424
|
+
const notify = storageEnabled ? openStorageReadLatch : () => Effect.dieMessage("Sharding.notifyLocal: storage is disabled");
|
425
|
+
return message._tag === "IncomingRequest" || message._tag === "IncomingEnvelope" ? notify() : runners.notifyLocal({
|
426
|
+
message,
|
427
|
+
notify,
|
428
|
+
discard
|
429
|
+
});
|
430
|
+
});
|
431
|
+
const isTransientError = Predicate.or(_ClusterError.RunnerUnavailable.is, _ClusterError.EntityNotManagedByRunner.is);
|
432
|
+
function sendOutgoing(message, discard, retries) {
|
433
|
+
return Effect.catchIf(Effect.suspend(() => {
|
434
|
+
const address = message.envelope.address;
|
435
|
+
const maybeRunner = MutableHashMap.get(shardAssignments, address.shardId);
|
436
|
+
const isPersisted = storageEnabled && Context.get(message.rpc.annotations, _ClusterSchema.Persisted);
|
437
|
+
const runnerIsLocal = Option.isSome(maybeRunner) && isLocalRunner(maybeRunner.value);
|
438
|
+
if (isPersisted) {
|
439
|
+
return runnerIsLocal ? notifyLocal(message, discard) : runners.notify({
|
440
|
+
address: maybeRunner,
|
441
|
+
message,
|
442
|
+
discard
|
443
|
+
});
|
444
|
+
} else if (Option.isNone(maybeRunner)) {
|
445
|
+
return Effect.fail(new _ClusterError.EntityNotManagedByRunner({
|
446
|
+
address
|
447
|
+
}));
|
448
|
+
}
|
449
|
+
return runnerIsLocal ? sendLocal(message) : runners.send({
|
450
|
+
address: maybeRunner.value,
|
451
|
+
message
|
452
|
+
});
|
453
|
+
}), isTransientError, error => {
|
454
|
+
if (retries === 0) {
|
455
|
+
return Effect.die(error);
|
456
|
+
}
|
457
|
+
return Effect.delay(sendOutgoing(message, discard, retries && retries - 1), config.sendRetryInterval);
|
458
|
+
});
|
459
|
+
}
|
460
|
+
// --- Shard Manager sync ---
|
461
|
+
const shardManagerTimeoutFiber = yield* FiberHandle.make().pipe(Scope.extend(shardingScope));
|
462
|
+
const startShardManagerTimeout = FiberHandle.run(shardManagerTimeoutFiber, Effect.flatMap(Effect.sleep(config.shardManagerUnavailableTimeout), () => {
|
463
|
+
MutableHashMap.clear(shardAssignments);
|
464
|
+
return clearSelfShards;
|
465
|
+
}), {
|
466
|
+
onlyIfMissing: true
|
467
|
+
});
|
468
|
+
const stopShardManagerTimeout = FiberHandle.clear(shardManagerTimeoutFiber);
|
469
|
+
// Every time the link to the shard manager is lost, we re-register the runner
|
470
|
+
// and re-subscribe to sharding events
|
471
|
+
yield* Effect.gen(function* () {
|
472
|
+
yield* Effect.logDebug("Registering with shard manager");
|
473
|
+
if (Option.isSome(config.runnerAddress)) {
|
474
|
+
const machineId = yield* shardManager.register(config.runnerAddress.value);
|
475
|
+
yield* snowflakeGen.setMachineId(machineId);
|
476
|
+
}
|
477
|
+
yield* stopShardManagerTimeout;
|
478
|
+
yield* Effect.logDebug("Subscribing to sharding events");
|
479
|
+
const mailbox = yield* shardManager.shardingEvents;
|
480
|
+
const startedLatch = yield* Effect.makeLatch(false);
|
481
|
+
const eventsFiber = yield* Effect.gen(function* () {
|
482
|
+
while (true) {
|
483
|
+
const [events] = yield* mailbox.takeAll;
|
484
|
+
for (const event of events) {
|
485
|
+
yield* Effect.logDebug("Received sharding event", event);
|
486
|
+
switch (event._tag) {
|
487
|
+
case "StreamStarted":
|
488
|
+
{
|
489
|
+
yield* startedLatch.open;
|
490
|
+
break;
|
491
|
+
}
|
492
|
+
case "ShardsAssigned":
|
493
|
+
{
|
494
|
+
for (const shard of event.shards) {
|
495
|
+
MutableHashMap.set(shardAssignments, shard, event.address);
|
496
|
+
}
|
497
|
+
if (!MutableRef.get(isShutdown) && isLocalRunner(event.address)) {
|
498
|
+
for (const shardId of event.shards) {
|
499
|
+
if (selfShards.has(shardId)) continue;
|
500
|
+
selfShards.add(shardId);
|
501
|
+
}
|
502
|
+
yield* activeShardsLatch.open;
|
503
|
+
}
|
504
|
+
break;
|
505
|
+
}
|
506
|
+
case "ShardsUnassigned":
|
507
|
+
{
|
508
|
+
for (const shard of event.shards) {
|
509
|
+
MutableHashMap.remove(shardAssignments, shard);
|
510
|
+
}
|
511
|
+
if (isLocalRunner(event.address)) {
|
512
|
+
for (const shard of event.shards) {
|
513
|
+
selfShards.delete(shard);
|
514
|
+
}
|
515
|
+
yield* activeShardsLatch.open;
|
516
|
+
}
|
517
|
+
break;
|
518
|
+
}
|
519
|
+
}
|
520
|
+
}
|
521
|
+
}
|
522
|
+
}).pipe(Effect.forkScoped);
|
523
|
+
// Wait for the stream to be established
|
524
|
+
yield* startedLatch.await;
|
525
|
+
// perform a full sync every config.refreshAssignmentsInterval
|
526
|
+
const syncFiber = yield* syncAssignments.pipe(Effect.andThen(Effect.sleep(config.refreshAssignmentsInterval)), Effect.forever, Effect.forkScoped);
|
527
|
+
yield* Fiber.joinAll([eventsFiber, syncFiber]);
|
528
|
+
}).pipe(Effect.scoped, Effect.catchAllCause(cause => Effect.logDebug(cause)), Effect.zipRight(startShardManagerTimeout), Effect.repeat(Schedule.exponential(1000).pipe(Schedule.union(Schedule.spaced(10_000)))), Effect.annotateLogs({
|
529
|
+
package: "@effect/cluster",
|
530
|
+
module: "Sharding",
|
531
|
+
fiber: "ShardManager sync",
|
532
|
+
runner: config.runnerAddress
|
533
|
+
}), Effect.interruptible, Effect.forkIn(shardingScope));
|
534
|
+
const syncAssignments = Effect.gen(function* () {
|
535
|
+
const assignments = yield* shardManager.getAssignments;
|
536
|
+
yield* Effect.logDebug("Received shard assignments", assignments);
|
537
|
+
for (const [shardId, runner] of assignments) {
|
538
|
+
if (Option.isNone(runner)) {
|
539
|
+
MutableHashMap.remove(shardAssignments, shardId);
|
540
|
+
selfShards.delete(shardId);
|
541
|
+
continue;
|
542
|
+
}
|
543
|
+
MutableHashMap.set(shardAssignments, shardId, runner.value);
|
544
|
+
if (!isLocalRunner(runner.value)) {
|
545
|
+
selfShards.delete(shardId);
|
546
|
+
continue;
|
547
|
+
}
|
548
|
+
if (MutableRef.get(isShutdown) || selfShards.has(shardId)) {
|
549
|
+
continue;
|
550
|
+
}
|
551
|
+
selfShards.add(shardId);
|
552
|
+
}
|
553
|
+
yield* activeShardsLatch.open;
|
554
|
+
});
|
555
|
+
const clientRequests = new Map();
|
556
|
+
const clients = yield* _resourceMap.ResourceMap.make(Effect.fnUntraced(function* (entity) {
|
557
|
+
const client = yield* RpcClient.makeNoSerialization(entity.protocol, {
|
558
|
+
supportsAck: true,
|
559
|
+
generateRequestId: () => (0, _RpcMessage.RequestId)(snowflakeGen.unsafeNext()),
|
560
|
+
onFromClient(options) {
|
561
|
+
const address = Context.unsafeGet(options.context, ClientAddressTag);
|
562
|
+
switch (options.message._tag) {
|
563
|
+
case "Request":
|
564
|
+
{
|
565
|
+
const fiber = Option.getOrThrow(Fiber.getCurrentFiber());
|
566
|
+
const id = Snowflake.Snowflake(options.message.id);
|
567
|
+
const rpc = entity.protocol.requests.get(options.message.tag);
|
568
|
+
let respond;
|
569
|
+
if (!options.discard) {
|
570
|
+
const entry = {
|
571
|
+
rpc: rpc,
|
572
|
+
context: fiber.currentContext
|
573
|
+
};
|
574
|
+
clientRequests.set(id, entry);
|
575
|
+
respond = makeClientRespond(entry, client.write);
|
576
|
+
} else {
|
577
|
+
respond = clientRespondDiscard;
|
578
|
+
}
|
579
|
+
return sendOutgoing(new Message.OutgoingRequest({
|
580
|
+
envelope: Envelope.makeRequest({
|
581
|
+
requestId: id,
|
582
|
+
address,
|
583
|
+
tag: options.message.tag,
|
584
|
+
payload: options.message.payload,
|
585
|
+
headers: options.message.headers,
|
586
|
+
traceId: options.message.traceId,
|
587
|
+
spanId: options.message.spanId,
|
588
|
+
sampled: options.message.sampled
|
589
|
+
}),
|
590
|
+
lastReceivedReply: Option.none(),
|
591
|
+
rpc,
|
592
|
+
context: fiber.currentContext,
|
593
|
+
respond
|
594
|
+
}), options.discard);
|
595
|
+
}
|
596
|
+
case "Ack":
|
597
|
+
{
|
598
|
+
const requestId = Snowflake.Snowflake(options.message.requestId);
|
599
|
+
const entry = clientRequests.get(requestId);
|
600
|
+
if (!entry) return Effect.void;
|
601
|
+
return sendOutgoing(new Message.OutgoingEnvelope({
|
602
|
+
envelope: new Envelope.AckChunk({
|
603
|
+
id: snowflakeGen.unsafeNext(),
|
604
|
+
address,
|
605
|
+
requestId,
|
606
|
+
replyId: entry.lastChunkId
|
607
|
+
}),
|
608
|
+
rpc: entry.rpc
|
609
|
+
}), false);
|
610
|
+
}
|
611
|
+
case "Interrupt":
|
612
|
+
{
|
613
|
+
const requestId = Snowflake.Snowflake(options.message.requestId);
|
614
|
+
const entry = clientRequests.get(requestId);
|
615
|
+
if (!entry) return Effect.void;
|
616
|
+
clientRequests.delete(requestId);
|
617
|
+
// for durable messages, we ignore interrupts on shutdown or as a
|
618
|
+
// result of a shard being resassigned
|
619
|
+
const isTransientInterrupt = MutableRef.get(isShutdown) || options.message.interruptors.some(id => _interruptors.internalInterruptors.has(id));
|
620
|
+
if (isTransientInterrupt && storageEnabled && Context.get(entry.rpc.annotations, _ClusterSchema.Persisted)) {
|
621
|
+
return Effect.void;
|
622
|
+
}
|
623
|
+
return Effect.ignore(sendOutgoing(new Message.OutgoingEnvelope({
|
624
|
+
envelope: new Envelope.Interrupt({
|
625
|
+
id: snowflakeGen.unsafeNext(),
|
626
|
+
address,
|
627
|
+
requestId
|
628
|
+
}),
|
629
|
+
rpc: entry.rpc
|
630
|
+
}), false, 3));
|
631
|
+
}
|
632
|
+
}
|
633
|
+
return Effect.void;
|
634
|
+
}
|
635
|
+
});
|
636
|
+
const wrappedClient = {};
|
637
|
+
for (const method of Object.keys(client.client)) {
|
638
|
+
wrappedClient[method] = function (payload, options) {
|
639
|
+
return client.client[method](payload, {
|
640
|
+
...options,
|
641
|
+
context: options?.context ? Context.merge(options.context, this[currentClientAddress]) : this[currentClientAddress]
|
642
|
+
});
|
643
|
+
};
|
644
|
+
}
|
645
|
+
yield* Scope.addFinalizer(yield* Effect.scope, Effect.withFiberRuntime(fiber => {
|
646
|
+
_interruptors.internalInterruptors.add(fiber.id());
|
647
|
+
return Effect.void;
|
648
|
+
}));
|
649
|
+
return entityId => {
|
650
|
+
const id = _EntityId.EntityId.make(entityId);
|
651
|
+
return {
|
652
|
+
...wrappedClient,
|
653
|
+
[currentClientAddress]: ClientAddressTag.context(_EntityAddress.EntityAddress.make({
|
654
|
+
shardId: getShardId(id),
|
655
|
+
entityId: id,
|
656
|
+
entityType: entity.type
|
657
|
+
}))
|
658
|
+
};
|
659
|
+
};
|
660
|
+
}));
|
661
|
+
const makeClient = entity => clients.get(entity);
|
662
|
+
const clientRespondDiscard = _reply => Effect.void;
|
663
|
+
const makeClientRespond = (entry, write) => reply => {
|
664
|
+
switch (reply._tag) {
|
665
|
+
case "Chunk":
|
666
|
+
{
|
667
|
+
entry.lastChunkId = reply.id;
|
668
|
+
return write({
|
669
|
+
_tag: "Chunk",
|
670
|
+
clientId: 0,
|
671
|
+
requestId: (0, _RpcMessage.RequestId)(reply.requestId),
|
672
|
+
values: reply.values
|
673
|
+
});
|
674
|
+
}
|
675
|
+
case "WithExit":
|
676
|
+
{
|
677
|
+
clientRequests.delete(reply.requestId);
|
678
|
+
return write({
|
679
|
+
_tag: "Exit",
|
680
|
+
clientId: 0,
|
681
|
+
requestId: (0, _RpcMessage.RequestId)(reply.requestId),
|
682
|
+
exit: reply.exit
|
683
|
+
});
|
684
|
+
}
|
685
|
+
}
|
686
|
+
};
|
687
|
+
// --- Entities ---
|
688
|
+
const context = yield* Effect.context();
|
689
|
+
const reaper = yield* _entityReaper.EntityReaper;
|
690
|
+
const registerEntity = Effect.fnUntraced(function* (entity, build, options) {
|
691
|
+
if (entityManagers.has(entity.type)) return;
|
692
|
+
const scope = yield* Scope.make();
|
693
|
+
const manager = yield* EntityManager.make(entity, build, {
|
694
|
+
...options,
|
695
|
+
storage,
|
696
|
+
runnerAddress: Option.getOrThrow(config.runnerAddress),
|
697
|
+
sharding
|
698
|
+
}).pipe(Effect.provide(context.pipe(Context.add(_entityReaper.EntityReaper, reaper), Context.add(Scope.Scope, scope), Context.add(Snowflake.Generator, snowflakeGen))));
|
699
|
+
entityManagers.set(entity.type, {
|
700
|
+
entity,
|
701
|
+
scope,
|
702
|
+
manager
|
703
|
+
});
|
704
|
+
yield* Scope.addFinalizer(scope, Effect.sync(() => entityManagers.delete(entity.type)));
|
705
|
+
yield* PubSub.publish(events, (0, _ShardingRegistrationEvent.EntityRegistered)({
|
706
|
+
entity
|
707
|
+
}));
|
708
|
+
});
|
709
|
+
yield* Scope.addFinalizerExit(shardingScope, exit => Effect.forEach(entityManagers.values(), state => Effect.catchAllCause(Scope.close(state.scope, exit), cause => Effect.annotateLogs(Effect.logError("Error closing entity manager", cause), {
|
710
|
+
entity: state.entity.type
|
711
|
+
})), {
|
712
|
+
concurrency: "unbounded",
|
713
|
+
discard: true
|
714
|
+
}));
|
715
|
+
// --- Finalization ---
|
716
|
+
if (Option.isSome(config.runnerAddress)) {
|
717
|
+
const selfAddress = config.runnerAddress.value;
|
718
|
+
// Unregister runner from shard manager when scope is closed
|
719
|
+
yield* Scope.addFinalizer(shardingScope, Effect.gen(function* () {
|
720
|
+
yield* Effect.logDebug("Unregistering runner from shard manager", selfAddress);
|
721
|
+
yield* shardManager.unregister(selfAddress).pipe(Effect.catchAllCause(cause => Effect.logError("Error calling unregister with shard manager", cause)));
|
722
|
+
yield* clearSelfShards;
|
723
|
+
}));
|
724
|
+
}
|
725
|
+
yield* Scope.addFinalizer(shardingScope, Effect.withFiberRuntime(fiber => {
|
726
|
+
MutableRef.set(isShutdown, true);
|
727
|
+
_interruptors.internalInterruptors.add(fiber.id());
|
728
|
+
return Effect.void;
|
729
|
+
}));
|
730
|
+
const sharding = Sharding.of({
|
731
|
+
getRegistrationEvents,
|
732
|
+
getShardId,
|
733
|
+
isShutdown: Effect.sync(() => MutableRef.get(isShutdown)),
|
734
|
+
registerEntity,
|
735
|
+
registerSingleton,
|
736
|
+
makeClient,
|
737
|
+
send: sendLocal,
|
738
|
+
notify: message => notifyLocal(message, false)
|
739
|
+
});
|
740
|
+
return sharding;
|
741
|
+
});
|
20
742
|
/**
|
21
743
|
* @since 1.0.0
|
22
744
|
* @category layers
|
23
745
|
*/
|
24
|
-
const
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
* @since 1.0.0
|
29
|
-
* @category utils
|
30
|
-
*/
|
31
|
-
const register = exports.register = internal.register;
|
32
|
-
/**
|
33
|
-
* Notify the shard manager that shards must be unassigned from this pod.
|
34
|
-
*
|
35
|
-
* @since 1.0.0
|
36
|
-
* @category utils
|
37
|
-
*/
|
38
|
-
const unregister = exports.unregister = internal.unregister;
|
39
|
-
/**
|
40
|
-
* Same as `register`, but will automatically call `unregister` when the `Scope` is terminated.
|
41
|
-
*
|
42
|
-
* @since 1.0.0
|
43
|
-
* @category utils
|
44
|
-
*/
|
45
|
-
const registerScoped = exports.registerScoped = internal.registerScoped;
|
46
|
-
/**
|
47
|
-
* Start a computation that is guaranteed to run only on a single pod.
|
48
|
-
* Each pod should call `registerSingleton` but only a single pod will actually run it at any given time.
|
49
|
-
*
|
50
|
-
* @since 1.0.0
|
51
|
-
* @category utils
|
52
|
-
*/
|
53
|
-
const registerSingleton = exports.registerSingleton = internal.registerSingleton;
|
54
|
-
/**
|
55
|
-
* Register a new entity type, allowing pods to send messages to entities of this type.
|
56
|
-
*
|
57
|
-
* @since 1.0.0
|
58
|
-
* @category utils
|
59
|
-
*/
|
60
|
-
const registerEntity = exports.registerEntity = internal.registerEntity;
|
61
|
-
/**
|
62
|
-
* Register a new topic type, allowing pods to broadcast messages to subscribers.
|
63
|
-
*
|
64
|
-
* @since 1.0.0
|
65
|
-
* @category utils
|
66
|
-
*/
|
67
|
-
const registerTopic = exports.registerTopic = internal.registerTopic;
|
68
|
-
/**
|
69
|
-
* Get an object that allows sending messages to a given entity type.
|
70
|
-
* You can provide a custom send timeout to override the one globally defined.
|
71
|
-
*
|
72
|
-
* @since 1.0.0
|
73
|
-
* @category utils
|
74
|
-
*/
|
75
|
-
const messenger = exports.messenger = internal.messenger;
|
76
|
-
/**
|
77
|
-
* Get an object that allows broadcasting messages to a given topic type.
|
78
|
-
* You can provide a custom send timeout to override the one globally defined.
|
79
|
-
*
|
80
|
-
* @since 1.0.0
|
81
|
-
* @category utils
|
82
|
-
*/
|
83
|
-
const broadcaster = exports.broadcaster = internal.broadcaster;
|
84
|
-
/**
|
85
|
-
* Get the list of pods currently registered to the Shard Manager
|
86
|
-
*
|
87
|
-
* @since 1.0.0
|
88
|
-
* @category utils
|
89
|
-
*/
|
90
|
-
const getPods = exports.getPods = internal.getPods;
|
91
|
-
/**
|
92
|
-
* Sends a raw message to the local entity manager without performing reties.
|
93
|
-
* Those are up to the caller.
|
94
|
-
*
|
95
|
-
* @since 1.0.0
|
96
|
-
* @category utils
|
97
|
-
*/
|
98
|
-
const sendMessageToLocalEntityManagerWithoutRetries = exports.sendMessageToLocalEntityManagerWithoutRetries = internal.sendMessageToLocalEntityManagerWithoutRetries;
|
99
|
-
/**
|
100
|
-
* Gets the list of shardIds assigned to the current Pod
|
101
|
-
*
|
102
|
-
* @since 1.0.0
|
103
|
-
* @category utils
|
104
|
-
*/
|
105
|
-
const getAssignedShardIds = exports.getAssignedShardIds = internal.getAssignedShardIds;
|
746
|
+
const layer = exports.layer = /*#__PURE__*/Layer.scoped(Sharding, make).pipe( /*#__PURE__*/Layer.provide([Snowflake.layerGenerator, _entityReaper.EntityReaper.Default]));
|
747
|
+
// Utilities
|
748
|
+
const ClientAddressTag = /*#__PURE__*/Context.GenericTag("@effect/cluster/Sharding/ClientAddress");
|
749
|
+
const currentClientAddress = /*#__PURE__*/Symbol.for(ClientAddressTag.key);
|
106
750
|
//# sourceMappingURL=Sharding.js.map
|