LOG4J2-2816: Handle Disruptor event translation exceptions #488
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
LOG4J2-2816
Problem
If
RingBufferLogEventTranslator#translateTo
throws an exception for any reason, Disruptor'sRingBuffer#translateAndPublish
will still publish the sequence in afinally
block (source).In such a case, the "untranslated" and unpopulated event will later be consumed by
RingBufferLogEventHandler
, since its sequence was published. However, the event will be missing all values, includingasyncLogger
. This causes aNullPointerException
to be thrown during event handling inRingBufferLogEvent#execute
:Solution
Log4j needs to handle the case where an exception is thrown by
RingBufferLogEventTranslator#translateTo
.The Disruptor documentation makes it clear that once a slot is claimed in the ring buffer, the sequence must be published. Otherwise the state of the Disruptor can be corrupted (source). That is why
RingBuffer#translateAndPublish
is designed the way it is, and why its usage is recommended over more manual methods.In order to handle such exceptions, then, it seems like the
EventHandler
must be responsible for checking that the event it is handling is sufficiently populated. Such an approach is also suggested by this Disruptor GitHub issue discussion. This PR implements that approach by adding anisPopulated
property toRingBufferLogEvent
.Tests
The new test in
AsyncLoggerEventTranslationExceptionTest
fails onrelease-2.x
and passes on this branch.