Clear the database appender buffer when committing fails - #4319
Open
dajiaohuang wants to merge 1 commit into
Open
dajiaohuang wants to merge 1 commit into
dajiaohuang wants to merge 1 commit into
Conversation
`AbstractDatabaseManager#flush` cleared the buffer in a `finally` block that also contained `commitAndClose`. If committing or closing the transaction threw, the `clear()` was skipped, so the events stayed in the buffer and were sent to the database a second time by the next flush. Since `flush()` is also called from `shutdown()`, and the buffer only grows while the failure persists, this leaked log events and produced duplicate rows. Move `buffer.clear()` into an inner `finally` block so that events are discarded regardless of the outcome of `commitAndClose`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #4318.
AbstractDatabaseManager#flush()handed the buffered events towriteInternal()and then rancommitAndClose()andbuffer.clear()inthe same
finallyblock:When
commitAndClose()throws (a database failure during the commit/closephase), the
clear()was skipped. The events therefore stayed in thebuffer and were written a second time by the following flush, and, because
the buffer keeps accumulating new events while the failure persists, it
grew without bound.
flush()is also called fromshutdown(), so afailing commit at shutdown could also mask the original exception with a
secondary one from re-sending the same events.
The fix moves
buffer.clear()into an innerfinallyblock, so the eventsare discarded no matter how
commitAndClose()returns or throws. Theevents were already passed to the database layer, so keeping them cannot
recover the transaction.
testBufferedEventsAreDiscardedWhenCommitFailsmakescommitAndClose()fail once and then succeed, and asserts that
writeInternal()is invokedexactly twice in total (once per event), i.e. that the failed transaction
is not re-sent.
Note on scope:
connectAndStart()is deliberately left outside thetry,so events are still retried when the connection cannot be established.
Only the commit/close phase changes behaviour here.
Checklist
2.xbranch if you are targeting Log4j 2; usemainotherwise./mvnw verifysucceeds (the build instructions)src/changelog/.2.x.xdirectory