AxonFramework icon indicating copy to clipboard operation
AxonFramework copied to clipboard

[#2021] Dead Letter Queue for Event Processing Groups

Open smcvb opened this issue 4 years ago • 2 comments

This pull request introduces a Dead Letter Queue, or DLQ for short, as mentioned in issue #2021.

Dead Letter Queue

The SequencedDeadLetterQueue interface, in essence, allows maintenance of a "queue of sequences." It holds a queue of sequences to ensure that for event support, events belonging to the same sequence are queued in order. A similar argument can be made in potential future installments of a command DLQ, where command handling order should be maintained.

With this, we ensure that if event X is dead-lettered due to a failure, events X+[1..N] belonging to the same sequence are also enqueued. Doing so, we guard against the scenario that a Query Model instance fails from position X and upward. This approach thus ensures we protect the event order.

With this in mind, we can look at the interface, which provides the following operations:

  • enqueue DeadLetter instances,
  • check for the existence of a "sequence identifier," where a "sequence identifier" typically originates from the SequencingPolicy,
  • evict a DeadLetter from the queue,
  • requeue a DeadLetter in the queue,
  • validate whether the DLQ reaches the maximum capacity,
  • retrieve all DeadLetters for a specific sequence
  • retrieve all DeadLetter sequences
  • process the 'last touched' DeadLetter present in the DLQ through a given task resulting in an EnqueueDecision, which is used to either requeue or evict the processed letter,
  • clear dead letters from the queue.

Next to the above interface, this pull request provides an in-memory implementation of the SequencedDeadLetterQueue. The InMemorySequencedDeadLetterQueue maintains the dead-letters in a ConcurrentSkipListMap of String to ConcurrentLinkedDeque.

The String references the "sequence identifier" directly if it's an instance of String; otherwise we hash the sequence identifier Object. We have omitted the "sequence identifier" as a return value from the DLQ and DeadLetter as we cannot ensure we can return the "sequence identifier" Object when it's serialized.

It keeps track of "sequence identifiers" that are being processed to ensure only a single thread can actively process a given DeadLetter. Thus in doing so it guards concurrent access.

Dead Letters

As specified, the SequencedDeadLetterQueue maintains a "queue of sequences," so you will. These sequences contain a number of DeadLetter instances grouped by "sequence identifier." The DeadLetter interface exposes the following methods:

  • the actual letter, a.k.a. the Message implementation,
  • the cause for dead-lettering, which may be an empty Optional if it is not the first failure in the sequence,
  • the moment it was enqueued,
  • the moment this dead letter was last touched for processing,
  • the diagnostic MetaData contains additional valuable information about the DeadLetter.

Next to the interface, this pull request contains a GenericDeadLetter. This generic instance is capable of holding any Message implementation.

Dead Lettering Processing Group

Next to the Dead Letter Queue API, this pull request introduces a dedicated EventHandlerInvoker (in essence the Processing Group), called the DeadLetteringEventHandlerInvoker. When processing within an Event Handler fails, this invoker will enqueue an EventMessage with the configured SequencedDeadLetterQueue. Secondly, it'll ensure subsequent events adhering to an already existing sequence identifier (as retrieved from the SequencingPolicy) are enqueued as well.

Before actually inserting the letter in the queue, the EnqueuePolicy is validated. This policy returns a so-called EnqueueDecision. It is the EnqueueDecision that describes whether a DeadLetter can be enqueued in the SequencedDeadLetterQueue to begin with.

Lastly, the DeadLetteringEventHandlerInvoker implements the SequencedDeadLetterProcessor interface. This interface exposes numerous "process" operations that delegate towards the process operations of the DLQ. Any of these operations will try to process a single entire sequence of DeadLetters. It will stop if processing of letter processing failed or when there are no more letters left in the chosen sequence.

By having the SequencedDeadLetterProcessor we provide a means for users to trigger the evaluation of dead-letters, without having to collect all message handlers and take transactions into account.

Dead Letter Processing Task

The DeadLetterProcessingTask is an EventMessage specific Function that the DeadLetteringEventHandlerInvoker uses to invoke the SequencedDeadLetterQueue#process operation. This task is given a DeadLetter<EventMessage<?>> to process.

Processing means starting a UnitOfWork for the EventMessage encased in the DeadLetter. The UnitOfWork invokes all EventMessageHandler instances this task was given by the DeadLetteringEventHandlerInvoker.

When the UnitOfWork prepare commit phase starts, we may assume event handling succeeded. Thus this returns the EnqueueDecision to evict the entry from the DLQ.

Upon reaching the rollback phase, event handling failed. As such, the EnqueuePolicy of the DeadLetteringEventHandlerInvoker is invoked once more, with its outcome being the result of processing the dead-letter.

Testing, Configuration, and Dead-Letter processing

This pull request provides two abstract test classes as a form of test suite for SequencedDeadLetterQueue implementations.

As the primary support for the DeadLetterQueue is for event messages, for the time being, the EventProcessingConfigurer provides the means to configure a DeadLetterQueue for a processing group. Additionally, the users can:

  • configure an EnqueuePolicy per processing group,
  • provide fine-grained configuration for the DeadLetteringEventHandlerInvoker, and
  • retrieve SequencedDeadLetterProcessor instances

Users of this feature will be able to use the SequencedDeadLetterProcessor to process DeadLetter instances. We deliberately do not implement an automatic retry process at this stage, as deducing all the edge cases imaginable would be overkill.

Hence, the EventProcessingConfiguration#sequencedDeadLetterProcessor method allows access to, in essence, the DeadLetteringEventHandlerInvoker. Through this the SequencedDeadLetterQueue#process operation is invoked, dealing with a sequence of dead-letters.


Note that we are still planning to:

  • build a JPA version of the DeadLetterQueue (PR #2239),
  • build a JDBC version of the DeadLetterQueue (likely in 4.6.x),
  • provide a form of parameter support within Message Handlers, to differentiate between regular handling and dead-lettered handling,
  • think about supporting dedicated MessageHandlerInterceptors for dead-letters.

This pull request resolves #2021.

smcvb avatar Jun 17 '22 14:06 smcvb