vy commented on code in PR #3474:
URL: https://github.com/apache/logging-log4j2/pull/3474#discussion_r1976659691


##########
log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/util/InternalLoggerRegistryGCTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.test.util;
+
+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.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.util.internal.InternalLoggerRegistry;
+import org.apache.logging.log4j.message.MessageFactory;
+import org.apache.logging.log4j.message.SimpleMessageFactory;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class InternalLoggerRegistryTest {
+    private InternalLoggerRegistry registry;
+    private MessageFactory messageFactory;
+
+    @BeforeEach
+    void setUp() {
+        registry = new InternalLoggerRegistry();
+        messageFactory = new SimpleMessageFactory();
+    }
+
+    @Test
+    void testGetLoggerReturnsNullForNonExistentLogger() {
+        assertNull(registry.getLogger("nonExistent", messageFactory));
+    }
+
+    @Test
+    void testComputeIfAbsentCreatesLogger() {
+        Logger logger =
+                registry.computeIfAbsent("testLogger", messageFactory, (name, 
factory) -> LoggerContext.getContext()
+                        .getLogger(name, factory));
+        assertNotNull(logger);
+        assertEquals("testLogger", logger.getName());
+    }
+
+    @Test
+    void testGetLoggerRetrievesExistingLogger() {
+        Logger logger =
+                registry.computeIfAbsent("testLogger", messageFactory, (name, 
factory) -> LoggerContext.getContext()
+                        .getLogger(name, factory));
+        assertSame(logger, registry.getLogger("testLogger", messageFactory));
+    }
+
+    @Test
+    void testHasLoggerReturnsCorrectStatus() {
+        assertFalse(registry.hasLogger("testLogger", messageFactory));
+        registry.computeIfAbsent("testLogger", messageFactory, (name, factory) 
-> LoggerContext.getContext()
+                .getLogger(name, factory));
+        assertTrue(registry.hasLogger("testLogger", messageFactory));
+    }
+
+    @Test
+    void testExpungeStaleEntriesRemovesGarbageCollectedLoggers() throws 
InterruptedException {
+        Logger logger =
+                registry.computeIfAbsent("testLogger", messageFactory, (name, 
factory) -> LoggerContext.getContext()
+                        .getLogger(name, factory));
+
+        WeakReference<Logger> weakRef = new WeakReference<>(logger);
+        logger = null; // Dereference to allow GC
+
+        // Retry loop to give GC time to collect
+        for (int i = 0; i < 10; i++) {
+            System.gc();
+            Thread.sleep(100);
+            if (weakRef.get() == null) {
+                break;
+            }
+        }
+
+        // Access the registry to potentially trigger cleanup
+        registry.computeIfAbsent("tempLogger", messageFactory, (name, factory) 
-> LoggerContext.getContext()
+                .getLogger(name, factory));
+
+        assertNull(weakRef.get(), "Logger should have been garbage collected");
+        assertNull(
+                registry.getLogger("testLogger", messageFactory), "Stale 
logger should be removed from the registry");

Review Comment:
   Can we instead replace this as follows, please?
   
   1. Use an ephemeral `LoggerContext` (as I described in another comment)
   2. Create 1000 `Logger`s (should be many enough to create GC pressure)
   3. Log using all `Logger`s (so we imitate a real-world setup where `Logger`s 
are used at some point)
   4. Use `Awaitility.waitAtMost(...).until(...)` to verify that after a 
`System.gc()`, `InternalLoggerRegistry::loggerRefByNameByMessageFactory` is 
emptied
   
   You can access to `InternalLoggerRegistry::loggerRefByNameByMessageFactory` 
using reflection. That is, first extract the `LoggerContext::loggerRegistry` 
field, and then the `InternalLoggerRegistry::loggerRefByNameByMessageFactory` 
field.



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java:
##########
@@ -53,13 +57,48 @@ public final class InternalLoggerRegistry {
             new WeakHashMap<>();
 
     private final ReadWriteLock lock = new ReentrantReadWriteLock();
-
     private final Lock readLock = lock.readLock();
-
     private final Lock writeLock = lock.writeLock();
 
+    // ReferenceQueue to track stale WeakReferences
+    private final ReferenceQueue<Logger> staleLoggerRefs = new 
ReferenceQueue<>();
+
     public InternalLoggerRegistry() {}
 
+    /**
+     * Expunges stale logger references from the registry.
+     */
+    private void expungeStaleEntries() {
+        Reference<? extends Logger> loggerRef;
+        while ((loggerRef = staleLoggerRefs.poll()) != null) {
+            removeLogger(loggerRef);
+        }
+    }
+
+    /**
+     * Removes a logger from the registry.
+     */
+    private void removeLogger(Reference<? extends Logger> loggerRef) {
+        Logger logger = loggerRef.get();
+        if (logger == null) return; // Logger already cleared
+
+        MessageFactory messageFactory = logger.getMessageFactory();
+        String name = logger.getName();
+
+        writeLock.lock();
+        try {
+            Map<String, WeakReference<Logger>> loggerRefByName = 
loggerRefByNameByMessageFactory.get(messageFactory);
+            if (loggerRefByName != null) {
+                loggerRefByName.remove(name);
+                if (loggerRefByName.isEmpty()) {
+                    loggerRefByNameByMessageFactory.remove(messageFactory); // 
Cleanup
+                }
+            }

Review Comment:
   Assume I move this logic (and only this!) to a method called 
`unsafeRemoveLogger(Logger)`. Note that this method receives a `Logger`, not a 
`Reference`.



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java:
##########
@@ -53,13 +57,48 @@ public final class InternalLoggerRegistry {
             new WeakHashMap<>();
 
     private final ReadWriteLock lock = new ReentrantReadWriteLock();
-
     private final Lock readLock = lock.readLock();
-
     private final Lock writeLock = lock.writeLock();
 
+    // ReferenceQueue to track stale WeakReferences
+    private final ReferenceQueue<Logger> staleLoggerRefs = new 
ReferenceQueue<>();
+
     public InternalLoggerRegistry() {}
 
+    /**
+     * Expunges stale logger references from the registry.
+     */
+    private void expungeStaleEntries() {
+        Reference<? extends Logger> loggerRef;
+        while ((loggerRef = staleLoggerRefs.poll()) != null) {
+            removeLogger(loggerRef);
+        }

Review Comment:
   Would you mind changing this loop as follows, please?
   ```
   boolean locked = false;
   Reference<? extends Logger> loggerRef;
   while ((loggerRef = staleLoggerRefs.poll()) != null) {
       Logger logger = loggerRef.get();
       if (logger != null) {
           if (!locked) {
               writeLock.lock();
           }
           unsafeRemoveLogger(logger);
       }
   }
   if (locked) {
       writeLock.unlock();
   }
   ```
   See my other comment regarding the `unsafeRemoveLogger(Logger)` method.



##########
log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/util/InternalLoggerRegistryGCTest.java:
##########


Review Comment:
   Can you replace `LoggerContext.getContext()` calls as follows, please?
   ```
   @Test
   void someTest(TestInfo testInfo) {
       try (final LoggerContext loggerContext = new 
LoggerContext(testInfo.getDisplayName)) {
           // Use `loggerContext`
       }
   }
   ```
   That is, don't disrupt the global one, use ephemeral `LC`s instead.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to