Skip to content

Commit

Permalink
Avoid unnecessary creation of prefix loggers
Browse files Browse the repository at this point in the history
Today when acquiring a prefix logger for a logger info stream, we obtain
a new prefix logger per invocation. This can lead to contention on the
markers lock in the constructor of PrefixLogger. Usually this is not a
problem (because the vast majority of callers hold on to the logger they
obtain). Unfortunately, under heavy indexing with multiple threads, the
contention on the lock can be devastating. This commit modifies
LoggerInfoStream to hold on to the loggers it obtains to avoid
contending over the lock there.

Relates #20571
  • Loading branch information
jasontedor committed Sep 20, 2016
1 parent 7041353 commit ba3cabe
Showing 1 changed file with 7 additions and 1 deletion.
Expand Up @@ -23,12 +23,17 @@
import org.apache.lucene.util.InfoStream;
import org.elasticsearch.common.logging.Loggers;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/** An InfoStream (for Lucene's IndexWriter) that redirects
* messages to "lucene.iw.ifd" and "lucene.iw" Logger.trace. */
public final class LoggerInfoStream extends InfoStream {

private final Logger parentLogger;

private final Map<String, Logger> loggers = new ConcurrentHashMap<>();

public LoggerInfoStream(final Logger parentLogger) {
this.parentLogger = parentLogger;
}
Expand All @@ -46,11 +51,12 @@ public boolean isEnabled(String component) {
}

private Logger getLogger(String component) {
return Loggers.getLogger(parentLogger, "." + component);
return loggers.computeIfAbsent(component, c -> Loggers.getLogger(parentLogger, "." + c));
}

@Override
public void close() {

}

}

0 comments on commit ba3cabe

Please sign in to comment.