vpelikh commented on code in PR #4157: URL: https://github.com/apache/logging-log4j2/pull/4157#discussion_r3610872148
########## log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java: ########## @@ -0,0 +1,255 @@ +/* + * 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.Objects.requireNonNull; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.WeakHashMap; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.function.BiFunction; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.message.MessageFactory; +import org.apache.logging.log4j.spi.LoggerRegistry; +import org.apache.logging.log4j.status.StatusLogger; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; + +/** + * A registry of {@link Logger}s namespaced by name and message factory using {@link WeakReference}s. + * <p> + * This class extends {@link LoggerRegistry} to provide garbage-free logger tracking + * with minimal lock contention. Loggers are created outside the write lock to prevent + * deadlocks and improve concurrency. + * </p> + * @since 3.0.0 + */ +@NullMarked +public class InternalLoggerRegistry extends LoggerRegistry<Logger> { + + private final Map<MessageFactory, Map<String, WeakReference<Logger>>> loggerRefByNameByMessageFactory = + new WeakHashMap<>(); + + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + private final Lock readLock = lock.readLock(); + private final Lock writeLock = lock.writeLock(); + + private final ReferenceQueue<Logger> staleLoggerRefs = new ReferenceQueue<>(); + + private final MessageFactory defaultMessageFactory; + + /** + * Constructs a registry whose default message factory (used when a lookup is requested with a + * {@code null} message factory) matches the one a {@link org.apache.logging.log4j.spi.LoggerContext} + * uses to create loggers. The two must resolve {@code null} to the same instance, otherwise + * loggers stored under the context default will not be found by null-factory lookups. + * + * @param defaultMessageFactory the default message factory (non-null) + */ + public InternalLoggerRegistry(final MessageFactory defaultMessageFactory) { + this.defaultMessageFactory = requireNonNull(defaultMessageFactory, "defaultMessageFactory"); + } + + private void expungeStaleEntries() { + final Reference<? extends Logger> loggerRef = staleLoggerRefs.poll(); + if (loggerRef != null) { + writeLock.lock(); + try { + while (staleLoggerRefs.poll() != null) { + // Clear ref queue Review Comment: I’d like to keep the comment – the empty `while` loop that drains the `ReferenceQueue` is not immediately obvious and can be mistaken for a bug. The comment clarifies the intent at no cost. Also, this same comment already exists in the 2.x branch (see [here](https://github.com/apache/logging-log4j2/blob/38889a881f341a2d9a6d6835eb76435673ffc421/log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java#L79)), so keeping it maintains consistency. Let me know what you think. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
