Skip to content
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

Open
wants to merge 1 commit into
base: 2.24.x
Choose a base branch
from

Conversation

ppkarwasz
Copy link
Contributor

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 Loggers 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

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
Comment on lines +184 to +186
// A replacement for Reference.reachabilityFence() from Java 9.
// Prevents `newLogger` to become unreachable in the lines above.
return currentLogger != null ? currentLogger : newLogger;
Copy link
Member

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?

Copy link
Contributor Author

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 to newLogger.
  • 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.

Copy link
Member

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"?

Comment on lines +180 to +182
Logger currentLogger = loggerRefByNameByMessageFactory
.computeIfAbsent(messageFactory, ignored -> new HashMap<>())
.computeIfAbsent(name, ignored -> new WeakReference<>(newLogger))
Copy link
Member

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 WRs 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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants