-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expunge stale entries in InternalLoggerRegistry #3681
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
04facd7
ensured stale loggers are removed from InternalLoggerRegistry and al…
Suvrat1629 f53251c
removed wildcard imports
Suvrat1629 207a7c3
added the comments again
Suvrat1629 9a1d8e3
changed the removeLogger to not iterate
Suvrat1629 05b3f8f
reverted more changes
Suvrat1629 8b964a0
Expunge stale entries in InternalLoggerRegistry
jhl221123 b432d14
Updated InternalLoggerRegistryTest
jhl221123 0cde993
Add changelog entry
jhl221123 3df581e
Update InternalLoggerRegistry and tests
jhl221123 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
153 changes: 153 additions & 0 deletions
153
...src/test/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistryTest.java
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,153 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.logging.log4j.core.util.internal; | ||
|
||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertSame; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.lang.reflect.Field; | ||
import java.util.Map; | ||
import org.apache.logging.log4j.core.Logger; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.apache.logging.log4j.message.MessageFactory; | ||
import org.apache.logging.log4j.message.SimpleMessageFactory; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInfo; | ||
|
||
class InternalLoggerRegistryTest { | ||
private LoggerContext loggerContext; | ||
private InternalLoggerRegistry registry; | ||
private MessageFactory messageFactory; | ||
|
||
@BeforeEach | ||
void setUp(TestInfo testInfo) throws NoSuchFieldException, IllegalAccessException { | ||
loggerContext = new LoggerContext(testInfo.getDisplayName()); | ||
final Field registryField = loggerContext.getClass().getDeclaredField("loggerRegistry"); | ||
registryField.setAccessible(true); | ||
registry = (InternalLoggerRegistry) registryField.get(loggerContext); | ||
messageFactory = SimpleMessageFactory.INSTANCE; | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
if (loggerContext != null) { | ||
loggerContext.stop(); | ||
} | ||
} | ||
|
||
@Test | ||
void testGetLoggerReturnsNullForNonExistentLogger() { | ||
assertNull(registry.getLogger("nonExistent", messageFactory)); | ||
} | ||
|
||
@Test | ||
void testComputeIfAbsentCreatesLogger() { | ||
final Logger logger = registry.computeIfAbsent( | ||
"testLogger", messageFactory, (name, factory) -> new Logger(loggerContext, name, factory) {}); | ||
assertNotNull(logger); | ||
assertEquals("testLogger", logger.getName()); | ||
} | ||
|
||
@Test | ||
void testGetLoggerRetrievesExistingLogger() { | ||
final Logger logger = registry.computeIfAbsent( | ||
"testLogger", messageFactory, (name, factory) -> new Logger(loggerContext, name, factory) {}); | ||
assertSame(logger, registry.getLogger("testLogger", messageFactory)); | ||
} | ||
|
||
@Test | ||
void testHasLoggerReturnsCorrectStatus() { | ||
assertFalse(registry.hasLogger("testLogger", messageFactory)); | ||
registry.computeIfAbsent( | ||
"testLogger", messageFactory, (name, factory) -> new Logger(loggerContext, name, factory) {}); | ||
assertTrue(registry.hasLogger("testLogger", messageFactory)); | ||
} | ||
|
||
@Test | ||
void testExpungeStaleWeakReferenceEntries() { | ||
final String loggerNamePrefix = "testLogger_"; | ||
final int numberOfLoggers = 1000; | ||
|
||
for (int i = 0; i < numberOfLoggers; i++) { | ||
final Logger logger = registry.computeIfAbsent( | ||
loggerNamePrefix + i, | ||
messageFactory, | ||
(name, factory) -> new Logger(loggerContext, name, factory) {}); | ||
logger.info("Using logger {}", logger.getName()); | ||
} | ||
|
||
await().atMost(10, SECONDS).pollInterval(100, MILLISECONDS).untilAsserted(() -> { | ||
System.gc(); | ||
registry.computeIfAbsent( | ||
"triggerExpunge", messageFactory, (name, factory) -> new Logger(loggerContext, name, factory) {}); | ||
|
||
final Map<MessageFactory, Map<String, WeakReference<Logger>>> loggerRefByNameByMessageFactory = | ||
reflectAndGetLoggerMapFromRegistry(); | ||
final Map<String, WeakReference<Logger>> loggerRefByName = | ||
loggerRefByNameByMessageFactory.get(messageFactory); | ||
|
||
int unexpectedCount = 0; | ||
for (int i = 0; i < numberOfLoggers; i++) { | ||
if (loggerRefByName.containsKey(loggerNamePrefix + i)) { | ||
unexpectedCount++; | ||
} | ||
} | ||
assertEquals( | ||
0, unexpectedCount, "Found " + unexpectedCount + " unexpected stale entries for MessageFactory"); | ||
}); | ||
} | ||
|
||
@Test | ||
void testExpungeStaleMessageFactoryEntry() { | ||
final SimpleMessageFactory mockMessageFactory = new SimpleMessageFactory(); | ||
Logger logger = registry.computeIfAbsent( | ||
"testLogger", mockMessageFactory, (name, factory) -> new Logger(loggerContext, name, factory) {}); | ||
logger.info("Using logger {}", logger.getName()); | ||
logger = null; | ||
|
||
await().atMost(10, SECONDS).pollInterval(100, MILLISECONDS).untilAsserted(() -> { | ||
System.gc(); | ||
registry.getLogger("triggerExpunge", mockMessageFactory); | ||
|
||
final Map<MessageFactory, Map<String, WeakReference<Logger>>> loggerRefByNameByMessageFactory = | ||
reflectAndGetLoggerMapFromRegistry(); | ||
assertNull( | ||
loggerRefByNameByMessageFactory.get(mockMessageFactory), | ||
"Stale MessageFactory entry was not removed from the outer map"); | ||
}); | ||
} | ||
|
||
private Map<MessageFactory, Map<String, WeakReference<Logger>>> reflectAndGetLoggerMapFromRegistry() | ||
throws NoSuchFieldException, IllegalAccessException { | ||
final Field loggerMapField = registry.getClass().getDeclaredField("loggerRefByNameByMessageFactory"); | ||
loggerMapField.setAccessible(true); | ||
@SuppressWarnings("unchecked") | ||
final Map<MessageFactory, Map<String, WeakReference<Logger>>> loggerMap = | ||
(Map<MessageFactory, Map<String, WeakReference<Logger>>>) loggerMapField.get(registry); | ||
return loggerMap; | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/changelog/.2.x.x/3430_InternalLoggerRegistry_stale_entry_expunge.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,13 @@ | ||
<?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="3430" link="https://github.com/apache/logging-log4j2/issues/3430"/> | ||
<issue id="3681" link="https://github.com/apache/logging-log4j2/pull/3681"/> | ||
<description format="asciidoc"> | ||
Improved expunging of stale entries in `InternalLoggerRegistry` to prevent potential memory leaks | ||
</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.
Uh oh!
There was an error while loading. Please reload this page.