From 43c453887209bd16c5cf614c7dff91e4e795eb5f Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 19 Sep 2025 22:40:58 +1200 Subject: [PATCH] clean up runners when unavailable (#540) --- packages/effect/src/unstable/cluster/Runners.ts | 11 +++++++++-- .../effect/src/unstable/cluster/Sharding.ts | 17 +++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/effect/src/unstable/cluster/Runners.ts b/packages/effect/src/unstable/cluster/Runners.ts index 5dc344ee7b..22f556e8bc 100644 --- a/packages/effect/src/unstable/cluster/Runners.ts +++ b/packages/effect/src/unstable/cluster/Runners.ts @@ -108,6 +108,11 @@ export class Runners extends ServiceMap.Key Effect.Effect + + /** + * Mark a Runner as unavailable. + */ + readonly onRunnerUnavailable: (address: RunnerAddress) => Effect.Effect }>()("effect/cluster/Runners") {} /** @@ -395,7 +400,8 @@ export const makeNoop: Effect.Effect< > = make({ send: ({ message }) => Effect.fail(new EntityNotManagedByRunner({ address: message.envelope.address })), notify: () => Effect.void, - ping: () => Effect.void + ping: () => Effect.void, + onRunnerUnavailable: () => Effect.void }) /** @@ -602,7 +608,8 @@ export const makeRpc: Effect.Effect< return Effect.void }) ) - } + }, + onRunnerUnavailable: (address) => RcMap.invalidate(clients, address) }) }) diff --git a/packages/effect/src/unstable/cluster/Sharding.ts b/packages/effect/src/unstable/cluster/Sharding.ts index e35e6a509f..db9a6a0219 100644 --- a/packages/effect/src/unstable/cluster/Sharding.ts +++ b/packages/effect/src/unstable/cluster/Sharding.ts @@ -12,7 +12,7 @@ import * as Option from "../../data/Option.ts" import * as Effect from "../../Effect.ts" import * as Fiber from "../../Fiber.ts" import * as FiberMap from "../../FiberMap.ts" -import { constant } from "../../Function.ts" +import { constant, flow } from "../../Function.ts" import * as Equal from "../../interfaces/Equal.ts" import * as Layer from "../../Layer.ts" import * as MutableRef from "../../MutableRef.ts" @@ -180,11 +180,15 @@ interface EntityManagerState { const make = Effect.gen(function*() { const config = yield* ShardingConfig - const runners = yield* Runners + const runnersService = yield* Runners const runnerHealth = yield* RunnerHealth.RunnerHealth const snowflakeGen = yield* Snowflake.Generator const shardingScope = yield* Effect.scope const isShutdown = MutableRef.make(false) + const runFork = flow( + Effect.runForkWith(ServiceMap.omit(Scope.Scope)(yield* Effect.services())), + Fiber.runIn(shardingScope) + ) const storage = yield* MessageStorage.MessageStorage const storageEnabled = storage !== MessageStorage.noop @@ -716,7 +720,7 @@ const make = Effect.gen(function*() { return message._tag === "IncomingRequest" || message._tag === "IncomingEnvelope" ? state.manager.send(message) : - runners.sendLocal({ + runnersService.sendLocal({ message, send: state.manager.sendLocal, simulateRemoteSerialization: config.simulateRemoteSerialization @@ -753,7 +757,7 @@ const make = Effect.gen(function*() { return notify() } - return runners.notifyLocal({ message, notify, discard }) as any + return runnersService.notifyLocal({ message, notify, discard }) as any } ) @@ -777,13 +781,13 @@ const make = Effect.gen(function*() { if (isPersisted) { return runnerIsLocal ? notifyLocal(message, discard) - : runners.notify({ address: maybeRunner, message, discard }) + : runnersService.notify({ address: maybeRunner, message, discard }) } else if (maybeRunner === undefined) { return Effect.fail(new EntityNotAssignedToRunner({ address })) } return runnerIsLocal ? sendLocal(message) - : runners.send({ address: maybeRunner, message }) + : runnersService.send({ address: maybeRunner, message }) }), (error) => error._tag === "EntityNotAssignedToRunner" || error._tag === "RunnerUnavailable" ? error : Filter.fail(error), @@ -865,6 +869,7 @@ const make = Effect.gen(function*() { changed = true MutableHashMap.remove(allRunners, runner) MutableHashSet.remove(healthyRunners, runner) + runFork(runnersService.onRunnerUnavailable(runner.address)) for (let i = 0; i < runner.groups.length; i++) { HashRing.remove(hashRings.get(runner.groups[i])!, runner.address) }