Skip to content

Commit 4d64766

Browse files
committed
fix(cluster): never miss a shard lock wake-up between checks
Each notification opens the current latch and replaces it, so a waiter that checked its shard before the notification sees an open latch and rechecks.
1 parent ab319b4 commit 4d64766

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

‎packages/effect/src/cluster/Sharding.ts‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,24 @@ const make = Effect.gen(function*() {
303303

304304
// Requests for a shard assigned to this runner wait for its lock, for at most
305305
// `shardLockExpiration`, instead of bouncing while the previous owner drains.
306-
// `release` wakes waiters without opening the latch, so unmet waits park again.
307-
const shardLocksChanged = Latch.makeUnsafe(false)
308-
const awaitShardLock = (shardId: ShardId): Effect.Effect<void> =>
309-
Effect.suspend(() =>
306+
// Each notification opens the current latch and replaces it, so a waiter that
307+
// checked its shard before a notification cannot miss it.
308+
let shardLocksChanged = Latch.makeUnsafe(false)
309+
const notifyShardLockWaiters = Effect.suspend(() => {
310+
const latch = shardLocksChanged
311+
shardLocksChanged = Latch.makeUnsafe(false)
312+
return latch.open
313+
})
314+
const awaitShardLock = (shardId: ShardId): Effect.Effect<void> => {
315+
const loop: Effect.Effect<void> = Effect.suspend(() =>
316+
isAwaitingShardLock(shardId) ? Effect.andThen(shardLocksChanged.await, loop) : Effect.void
317+
)
318+
return Effect.suspend(() =>
310319
isAwaitingShardLock(shardId)
311-
? shardLocksChanged.await.pipe(
312-
Effect.repeat({ while: () => isAwaitingShardLock(shardId) }),
313-
Effect.timeoutOption(config.shardLockExpiration),
314-
Effect.asVoid
315-
)
320+
? Effect.asVoid(Effect.timeoutOption(loop, config.shardLockExpiration))
316321
: Effect.void
317322
)
323+
}
318324

319325
yield* Scope.addFinalizer(
320326
shardingScope,
@@ -505,7 +511,7 @@ const make = Effect.gen(function*() {
505511
MutableHashSet.add(acquiredShards, shardId)
506512
}
507513
if (acquired.length > 0) {
508-
yield* shardLocksChanged.release
514+
yield* notifyShardLockWaiters
509515
yield* storageReadLatch.open
510516
yield* Effect.forkIn(syncSingletons, shardingScope)
511517

@@ -541,7 +547,7 @@ const make = Effect.gen(function*() {
541547
activeShardsLatch.openUnsafe()
542548

543549
return Effect.gen(function*() {
544-
yield* shardLocksChanged.release
550+
yield* notifyShardLockWaiters
545551
yield* Effect.logError("Shard lock storage is unhealthy", cause)
546552
yield* Effect.forkIn(syncSingletons, shardingScope, { startImmediately: true })
547553

@@ -1362,7 +1368,7 @@ const make = Effect.gen(function*() {
13621368
})
13631369
yield* Effect.logDebug("New shard assignments", selfShards)
13641370
activeShardsLatch.openUnsafe()
1365-
yield* shardLocksChanged.release
1371+
yield* notifyShardLockWaiters
13661372

13671373
// update metrics
13681374
if (selfRunner) {
@@ -1821,7 +1827,7 @@ const make = Effect.gen(function*() {
18211827

18221828
if (isShutdown.current) return
18231829
MutableRef.set(isShutdown, true)
1824-
yield* shardLocksChanged.release
1830+
yield* notifyShardLockWaiters
18251831
if (selfRunner) {
18261832
yield* Effect.ignore(runnerStorage.unregister(selfRunner.address))
18271833
}

0 commit comments

Comments
 (0)