-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Clear the database appender buffer when committing fails #4319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dajiaohuang
wants to merge
1
commit into
apache:2.x
Choose a base branch
from
dajiaohuang:fix/4318-jdbc-buffer-flush-leak
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -18,10 +18,13 @@ | |||
|
|
||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||||
| import static org.mockito.ArgumentMatchers.any; | ||||
| import static org.mockito.ArgumentMatchers.isNull; | ||||
| import static org.mockito.ArgumentMatchers.same; | ||||
| import static org.mockito.BDDMockito.then; | ||||
| import static org.mockito.Mockito.doThrow; | ||||
| import static org.mockito.Mockito.mock; | ||||
| import static org.mockito.Mockito.reset; | ||||
| import static org.mockito.Mockito.spy; | ||||
|
|
@@ -227,6 +230,61 @@ void testBuffering04() throws Exception { | |||
| then(manager).shouldHaveNoMoreInteractions(); | ||||
| } | ||||
|
|
||||
| @Test | ||||
| void testBufferedEventsAreDiscardedWhenCommitFails() throws Exception { | ||||
| setUp("name", 10); | ||||
|
|
||||
| final LogEvent event1 = mock(LogEvent.class); | ||||
| final LogEvent event2 = mock(LogEvent.class); | ||||
|
|
||||
| when(event1.toImmutable()).thenReturn(mock(LogEvent.class)); | ||||
| when(event2.toImmutable()).thenReturn(mock(LogEvent.class)); | ||||
|
|
||||
| manager.startup(); | ||||
| manager.write(event1, null); | ||||
| manager.write(event2, null); | ||||
|
|
||||
| // The first flush fails while committing the transaction, the next one succeeds. | ||||
| doThrow(new DbAppenderLoggingException("Failed to commit the transaction")) | ||||
| .doReturn(true) | ||||
| .when(manager) | ||||
| .commitAndClose(); | ||||
|
|
||||
| assertThrows(DbAppenderLoggingException.class, manager::flush); | ||||
|
|
||||
| manager.flush(); | ||||
|
|
||||
| // Events of a transaction that failed to commit must not be sent again. | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| verify(manager, times(2)).writeInternal(any(LogEvent.class), isNull()); | ||||
| } | ||||
|
|
||||
| @Test | ||||
| void testBufferedEventsAreDiscardedWhenConnectFails() throws Exception { | ||||
| setUp("name", 10); | ||||
|
|
||||
| final LogEvent event1 = mock(LogEvent.class); | ||||
| final LogEvent event2 = mock(LogEvent.class); | ||||
|
|
||||
| when(event1.toImmutable()).thenReturn(mock(LogEvent.class)); | ||||
| when(event2.toImmutable()).thenReturn(mock(LogEvent.class)); | ||||
|
|
||||
| manager.startup(); | ||||
| manager.write(event1, null); | ||||
| manager.write(event2, null); | ||||
|
|
||||
| doThrow(new DbAppenderLoggingException("Failed to connect")) | ||||
| .doNothing() | ||||
| .when(manager) | ||||
| .connectAndStart(); | ||||
|
|
||||
| assertThrows(DbAppenderLoggingException.class, manager::flush); | ||||
|
|
||||
| manager.flush(); | ||||
|
|
||||
| // Events must not be retried after a failed connection attempt. | ||||
| verify(manager, times(0)).writeInternal(any(LogEvent.class), isNull()); | ||||
| } | ||||
|
|
||||
| @Test | ||||
| void testStartupShutdown01() throws Exception { | ||||
| setUp("testName01", 0); | ||||
|
|
||||
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
14 changes: 14 additions & 0 deletions
14
src/changelog/.2.x.x/4318_fix_database_appender_buffer_on_failed_commit.xml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <entry xmlns="https://logging.apache.org/xml/ns" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation=" | ||
| https://logging.apache.org/xml/ns | ||
| https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" | ||
| type="fixed"> | ||
| <issue id="4318" link="https://github.com/apache/logging-log4j2/issues/4318"/> | ||
| <description format="asciidoc"> | ||
| Clear the buffer of a buffered database appender when connecting or committing a transaction fails. | ||
| Previously the events were kept, so the next flush sent them again and the buffer grew without bound | ||
| while the database failure persisted. | ||
| </description> | ||
| </entry> |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.