From d0ceca11cefcaa7d544cdf0073fed6464a94de8e Mon Sep 17 00:00:00 2001 From: Tanner Scadden Date: Wed, 14 Oct 2026 06:23:52 -0400 Subject: [PATCH] Persist cluster sends before local shard lock acquisition (v3) (#9147) Co-authored-by: Claude Opus 5.5 (1M context) Co-authored-by: Effect Bot --- .../cluster-persist-before-shard-lock.md | 5 +++ packages/cluster/src/Sharding.ts | 2 +- packages/cluster/test/Sharding.test.ts | 37 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 .changeset/cluster-persist-before-shard-lock.md diff --git a/.changeset/cluster-persist-before-shard-lock.md b/.changeset/cluster-persist-before-shard-lock.md new file mode 100644 index 0000000000..2bb6475682 --- /dev/null +++ b/.changeset/cluster-persist-before-shard-lock.md @@ -0,0 +1,5 @@ +--- +"@effect/cluster": patch +--- + +Persist outgoing messages for shards assigned to the local runner before their lock is acquired, instead of retrying forever with `EntityNotAssignedToRunner`. diff --git a/packages/cluster/src/Sharding.ts b/packages/cluster/src/Sharding.ts index 683886126a..05fbbd6a5e 100644 --- a/packages/cluster/src/Sharding.ts +++ b/packages/cluster/src/Sharding.ts @@ -963,7 +963,7 @@ const make = Effect.gen(function*() { const state = entityManagers.get(address.entityType) if (!state) { return Effect.flatMap(waitForEntityManager(address.entityType), loop) - } else if (state.status === "closed" || !isEntityOnLocalShards(address)) { + } else if (state.status === "closed") { return Effect.fail(new EntityNotAssignedToRunner({ address })) } diff --git a/packages/cluster/test/Sharding.test.ts b/packages/cluster/test/Sharding.test.ts index f4887baf0a..301f2bf191 100644 --- a/packages/cluster/test/Sharding.test.ts +++ b/packages/cluster/test/Sharding.test.ts @@ -94,6 +94,43 @@ describe.concurrent("Sharding", () => { expect(driver.unprocessed.size).toEqual(0) }).pipe(Effect.provide(TestSharding))) + it.scoped("persists a discard send before acquiring the shard lock", () => + Effect.gen(function*() { + const memory = yield* RunnerStorage.makeMemory + const acquiring = yield* Deferred.make() + const ctx = yield* Layer.build( + TestEntityNoState.pipe( + Layer.provideMerge(Sharding.layer), + Layer.provide(Layer.succeed(RunnerStorage.RunnerStorage, { + ...memory, + acquire: () => Deferred.succeed(acquiring, void 0).pipe(Effect.as([])) + })), + Layer.provide(RunnerHealth.layerNoop), + Layer.provide(TestEntityState.Default), + Layer.provide(Runners.layerNoop), + Layer.provideMerge(MessageStorage.layerMemory), + Layer.provide(ShardingConfig.layer({ shardsPerGroup: 1, sendRetryInterval: 10 })) + ) + ) + yield* Deferred.await(acquiring) + const sharding = Context.get(ctx, Sharding.Sharding) + const entityId = EntityId.make("1") + const shardId = sharding.getShardId(entityId, "default") + assert.isFalse(sharding.hasShardId(shardId)) + + const client = (yield* TestEntity.client.pipe(Effect.provide(ctx)))(entityId) + const send = yield* client.RequestWithKey({ key: "abc" }, { discard: true }).pipe(Effect.fork) + yield* TestClock.adjust(100) + const sendExit = yield* Fiber.poll(send) + assert.isTrue(Option.isSome(sendExit) && Exit.isSuccess(sendExit.value)) + const persisted = yield* Context.get(ctx, MessageStorage.MessageStorage).requestIdForPrimaryKey({ + address: EntityAddress.make({ shardId, entityType: EntityType.EntityType.make(TestEntity.type), entityId }), + tag: "RequestWithKey", + id: "abc" + }) + assert.isTrue(Option.isSome(persisted)) + })) + it.scoped("interrupts", () => Effect.gen(function*() { const driver = yield* MessageStorage.MemoryDriver