Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ All notable changes to this project are documented in this file.
`new_async` still take a plain `Duration` (always bounded).

### Changed
- **`not_responding` and `responding` carry a `reason`.** `heartbeat` means the
FFI thread stalled and the context needs a recreate. `queueFull` means
sustained event-queue overflow, which recovers on its own. Each reason latches
and emits its own pair, so a `responding` ends only its own reason. The CBOR
payload gains a `reason` field; a decoder that rejects unknown fields must
update. Poll mode already had separate `not_responding` kinds
(`MsgNotRespondingHeartbeat`, `MsgNotRespondingQueueFull`), but its one
`MsgResponding` kind does not say which reason ends.
- **An event-queue overflow drops and counts; it no longer wedges the context.**
A full queue used to set a sticky flag that refused every later request until
the context was recycled. Now the event is dropped and counted per reason
Expand Down
25 changes: 16 additions & 9 deletions ffi/event_thread.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
## NotResponding/Responding on FFI-heartbeat stall/recovery.

type
NotRespondingReason* {.pure.} = enum
heartbeat ## FFI thread stalled; recreate the context
queueFull ## sustained event-queue overflow; recovers on its own

NotRespondingEvent* = object
reason*: NotRespondingReason

RespondingEvent* = object
reason*: NotRespondingReason

const
NotRespondingEventName* = "not_responding"
Expand Down Expand Up @@ -46,12 +53,12 @@ proc emitLivenessEvent[T, P](ctx: ptr FFIContext[T], name: string, payload: P) =
cast[pointer](emptyListenerPayload)
ctx.dispatchToListeners(name, dataPtr, event.len)

proc onNotResponding*(ctx: ptr FFIContext) =
emitLivenessEvent(ctx, NotRespondingEventName, NotRespondingEvent())
proc onNotResponding*(ctx: ptr FFIContext, reason: NotRespondingReason) =
Comment thread
gmelodie marked this conversation as resolved.
Resolved
emitLivenessEvent(ctx, NotRespondingEventName, NotRespondingEvent(reason: reason))

proc onResponding*(ctx: ptr FFIContext) =
## Fired once when the heartbeat resumes after a NotRespondingEvent.
emitLivenessEvent(ctx, RespondingEventName, RespondingEvent())
proc onResponding*(ctx: ptr FFIContext, reason: NotRespondingReason) =
## Fired once when `reason` ends. Another reason may still be active.
emitLivenessEvent(ctx, RespondingEventName, RespondingEvent(reason: reason))

type
ReverseWorkerBlockedEvent* = object
Expand Down Expand Up @@ -134,12 +141,12 @@ proc check[T](hb: var HeartbeatMonitor, ctx: ptr FFIContext[T]) =
let cur = ctx.ffiHeartbeat.load()
if cur != hb.lastValue:
if hb.notifiedStale:
onResponding(ctx)
onResponding(ctx, NotRespondingReason.heartbeat)
hb.lastValue = cur
hb.lastChange = Moment.now()
hb.notifiedStale = false
elif not hb.notifiedStale and Moment.now() - hb.lastChange > FFIHeartbeatStaleThreshold:
onNotResponding(ctx)
onNotResponding(ctx, NotRespondingReason.heartbeat)
hb.notifiedStale = true

proc eventRun[T](ctx: ptr FFIContext[T]) {.async.} =
Expand Down Expand Up @@ -167,9 +174,9 @@ proc eventRun[T](ctx: ptr FFIContext[T]) {.async.} =
let drops = ctx[].eventQueue.dropCounts().total()
case overflow.step(drops, getMonoTime().ticks div 1_000_000)
of OverflowChange.Sustained:
onNotResponding(ctx)
onNotResponding(ctx, NotRespondingReason.queueFull)
of OverflowChange.Cleared:
onResponding(ctx)
onResponding(ctx, NotRespondingReason.queueFull)
of OverflowChange.NoChange:
discard
# A recycle parks the loop in the teardown on purpose; a stall there is
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/test_event_dispatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,14 @@ suite "liveness events":
ctx[].eventRegistry, NotRespondingEventName, testCallback, addr evt
)

onNotResponding(ctx)
onNotResponding(ctx, NotRespondingReason.queueFull)

waitCallback(evt)
check evt.retCode == RET_OK
let decoded = cborDecode(payload(evt), EventEnvelope[NotRespondingEvent])
check decoded.isOk()
check decoded.value.eventType == NotRespondingEventName
check decoded.value.payload.reason == NotRespondingReason.queueFull

test "onResponding delivers EventEnvelope[RespondingEvent] to subscribers":
setupCallbackData(evt)
Expand All @@ -191,18 +192,19 @@ suite "liveness events":
ctx[].eventRegistry, RespondingEventName, testCallback, addr evt
)

onResponding(ctx)
onResponding(ctx, NotRespondingReason.heartbeat)

waitCallback(evt)
check evt.retCode == RET_OK
let decoded = cborDecode(payload(evt), EventEnvelope[RespondingEvent])
check decoded.isOk()
check decoded.value.eventType == RespondingEventName
check decoded.value.payload.reason == NotRespondingReason.heartbeat

test "liveness events with no subscriber are a no-op":
withPool(ctx):
onNotResponding(ctx)
onResponding(ctx)
onNotResponding(ctx, NotRespondingReason.heartbeat)
onResponding(ctx, NotRespondingReason.heartbeat)

suite "event thread drains queued events":
test "enqueued event is delivered to subscriber within a tick":
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/test_event_thread.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ when not defined(gcRefc):
gBlockingEnabled.store(false)

check waitCallbackTimeout(notif, 1500)
let decoded = cborDecode(payload(notif), EventEnvelope[NotRespondingEvent])
check decoded.isOk()
check decoded.value.payload.reason == NotRespondingReason.heartbeat

type BackpressureState = object
enteredLock: Lock
Expand Down Expand Up @@ -285,8 +288,14 @@ suite "queue overflow":
.isOk()

check waitCallbackTimeout(notif, 4 * EventOverflowSustainMs)
let decoded = cborDecode(payload(notif), EventEnvelope[NotRespondingEvent])
check decoded.isOk()
check decoded.value.payload.reason == NotRespondingReason.queueFull
waitCallback(rsp)
check rsp.retCode == RET_OK
check ctx.eventDrops().ringFull > 0

check waitCallbackTimeout(back, 6 * EventOverflowSustainMs)
let ended = cborDecode(payload(back), EventEnvelope[RespondingEvent])
check ended.isOk()
check ended.value.payload.reason == NotRespondingReason.queueFull
Loading