-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Minimize lock usage in InternalLoggerRegistry
#3418
base: 2.24.x
Are you sure you want to change the base?
Conversation
It has been reported that holding a lock on `InternalLoggerRegistry` during the creation of a logger can cause performance loss and deadlocks. The logger constructor can trigger property lookups and other pluggable operations, we don't entirely control. The fix to #3252 only covered one of these cases. This change moves the instantiation of new `Logger`s outside the write lock. While in some cases, this will cause multiple instantiations of loggers with the same parameters, all such loggers are functionally equivalent. On the other hand, the change allows the creation of different loggers in parallel. Closes #3399
...j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java
Show resolved
Hide resolved
// A replacement for Reference.reachabilityFence() from Java 9. | ||
// Prevents `newLogger` to become unreachable in the lines above. | ||
return currentLogger != null ? currentLogger : newLogger; |
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.
Are you sure this is necessary? If WR
returns a non-null value, and you hold a reference to that, I don't think WR
can be emptied in the meantime. Am I mistaken?
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.
You might be right, but I am not eager to risk another 2.24.1
😄
My reasoning was:
- We get a
WR
tonewLogger
. - At that point in code
newLogger
has already been referenced for the last time. The() -> new WeakReference<>(newLogger)
has already been used and can be reclaimed. - If a GC occurs just before the
WR.get()
, the reference might be emptied.
Since I reference newLogger
on line 186, currentLogger
will never be null
.
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.
At that point in code
newLogger
has already been referenced for the last time. The() -> new WeakReference<>(newLogger)
has already been used and can be reclaimed.
No, it cannot; because there is still a strong reference to the instance.
You might be right, but I am not eager to risk another
2.24.1
😄
Could you at least document that this is a "speculative measure"?
...j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java
Show resolved
Hide resolved
...j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java
Show resolved
Hide resolved
Logger currentLogger = loggerRefByNameByMessageFactory | ||
.computeIfAbsent(messageFactory, ignored -> new HashMap<>()) | ||
.computeIfAbsent(name, ignored -> new WeakReference<>(newLogger)) |
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.
This logic is wrong. It can be that there is a WR
registered for the (messageFactory, name)
key, yet the WR
s reference is reclaimed. I think you need to revert this block back to my LoggerRegistry::computeIfAbsent
implementation in 2062676:
Map<String, WeakReference<Logger>> loggerRefByName = loggerRefByNameByMessageFactory.get(messageFactory);
// noinspection Java8MapApi (avoid the allocation of lambda passed to `Map::computeIfAbsent`)
if (loggerRefByName == null) {
loggerRefByNameByMessageFactory.put(messageFactory, loggerRefByName = new HashMap<>());
}
final WeakReference<Logger> loggerRef = loggerRefByName.get(name);
if (loggerRef == null || (logger = loggerRef.get()) == null) {
loggerRefByName.put(name, new WeakReference<>(logger = newLogger));
}
return logger;
It has been reported that holding a lock on
InternalLoggerRegistry
during the creation of a logger can cause performance loss and deadlocks. The logger constructor can trigger property lookups and other pluggable operations, we don't entirely control. The fix to #3252 only covered one of these cases.This change moves the instantiation of new
Logger
s outside the write lock. While in some cases, this will cause multiple instantiations of loggers with the same parameters, all such loggers are functionally equivalent. On the other hand, the change allows the creation of different loggers in parallel.Closes #3399