This is an automated email from the ASF dual-hosted git repository. yasithdev pushed a commit to branch core-fail-unhandled-deliveries in repository https://gitbox.apache.org/repos/asf/airavata-custos.git
commit 0e1dbe5ea5bd9f0a29d8d89a3a08e69a5fabbde0 Author: Yasith Jayawardana <[email protected]> AuthorDate: Thu Nov 5 01:55:52 2026 -0500 Fail unhandled deliveries at once --- docs/architecture.md | 2 +- pkg/events/bus_test.go | 16 ++++++++++++++++ pkg/events/worker.go | 11 +++++++---- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 08846df16..563c1499f 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -75,7 +75,7 @@ Failures stop at the layer that detected them: an invalid token returns 401 befo `pkg/events.Bus` is backed by three core tables: `event_subscriptions`, `events` and `event_deliveries`. There is no external broker. A subscriber is a connector, named by its `Type` constant. Subscriptions are saved, so an event published while a connector is not loaded gets a delivery row and waits for it. Turning a connector off with `enabled: false` removes its subscription. -One worker goroutine polls due deliveries every 10 seconds and calls the handler with a 2 minute timeout. On failure it retries with backoff, 30 seconds at first and up to 1 hour, 10 attempts in all. A handler can wrap `events.ErrPermanent` to fail at once. Handlers must be safe to run more than once for the same event. Every attempt writes an `audit_events` row under the event's trace. Deliveries are listed and a failed one retried under `/events/deliveries`; `/events/subscriptions` sho [...] +One worker goroutine polls due deliveries every 10 seconds and calls the handler with a 2 minute timeout. On failure it retries with backoff, 30 seconds at first and up to 1 hour, 10 attempts in all. A handler can wrap `events.ErrPermanent` to fail at once. Handlers must be safe to run more than once for the same event. A delivery whose topic the loaded connector no longer handles fails at once. Every attempt writes an `audit_events` row under the event's trace. Deliveries are listed and [...] ## Identity and authentication diff --git a/pkg/events/bus_test.go b/pkg/events/bus_test.go index 86e309781..2e9dbafef 100644 --- a/pkg/events/bus_test.go +++ b/pkg/events/bus_test.go @@ -266,6 +266,22 @@ func TestPermanentErrorFailsWithoutRetry(t *testing.T) { } } +// Make sure a pending delivery for a topic the loaded subscriber dropped fails at once instead of waiting forever. +func TestDroppedTopicDeliveryFails(t *testing.T) { + database := setupTestDB(t) + noop := func(context.Context, models.ComputeClusterUser) error { return nil } + earlier := newBus(t, database) + earlier.SubscribeComputeClusterUserDeleted(subscriberTest, noop) + publish(t, tracingtest.Context(), earlier, ComputeClusterUserDeleteEvent, clusterUser()) + + bus := newBus(t, database) + bus.SubscribeComputeClusterUserCreated(subscriberTest, noop) + bus.deliverDue(context.Background()) + if d := onlyDelivery(t, bus, models.EventDeliveryFailed); d.Attempts != 1 { + t.Fatalf("expected 1 attempt, got %d", d.Attempts) + } +} + // Make sure an event published before its subscriber loads still gets a // delivery from the saved subscription, waits untouched, and is delivered // once the subscriber registers. diff --git a/pkg/events/worker.go b/pkg/events/worker.go index b376c4942..56fb9a800 100644 --- a/pkg/events/worker.go +++ b/pkg/events/worker.go @@ -22,6 +22,7 @@ import ( "database/sql" "encoding/json" "errors" + "fmt" "log/slog" "time" @@ -38,6 +39,8 @@ var ( // ErrPermanent marks an error a retry cannot fix, so the delivery fails at once. ErrPermanent = errors.New("permanent delivery failure") + errNoHandler = fmt.Errorf("%w: no handler for topic", ErrPermanent) + ErrDeliveryNotFound = errors.New("event delivery not found") ErrDeliveryNotFailed = errors.New("event delivery has not failed") ) @@ -160,9 +163,6 @@ func (b *Bus) loadedSubscribers() []string { // deliver calls the handler for one row and records the outcome. func (b *Bus) deliver(ctx context.Context, row models.PendingDelivery) { handler, ok := b.handlerFor(row.Subscriber, row.Event.EventType) - if !ok { - return - } // Build the handler's context. It carries the span and source of the // request that published the event, so audit rows join that trace. It is @@ -179,7 +179,10 @@ func (b *Bus) deliver(ctx context.Context, row models.PendingDelivery) { defer span.End() event := Event{Type: EventType(row.Event.EventType), Payload: row.Event.Payload} - err := callHandler(handlerCtx, handler, event, row.Event.Payload) + err := errNoHandler + if ok { + err = callHandler(handlerCtx, handler, event, row.Event.Payload) + } attempts := row.Attempts + 1 now := time.Now().UTC()
