-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Fix prefix logging #20429
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
Merged
jasontedor
merged 4 commits into
elastic:master
from
jasontedor:logging-prefix-by-wrapping
Sep 13, 2016
Merged
Fix prefix logging #20429
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/main/java/org/elasticsearch/common/logging/PrefixLogger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch 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.elasticsearch.common.logging; | ||
|
|
||
| import org.apache.logging.log4j.Level; | ||
| import org.apache.logging.log4j.Marker; | ||
| import org.apache.logging.log4j.MarkerManager; | ||
| import org.apache.logging.log4j.message.Message; | ||
| import org.apache.logging.log4j.spi.ExtendedLogger; | ||
| import org.apache.logging.log4j.spi.ExtendedLoggerWrapper; | ||
|
|
||
| import java.lang.ref.WeakReference; | ||
| import java.util.WeakHashMap; | ||
|
|
||
| class PrefixLogger extends ExtendedLoggerWrapper { | ||
|
|
||
| // we can not use the built-in Marker tracking (MarkerManager) because the MarkerManager holds | ||
| // a permanent reference to the marker; however, we have transient markers from index-level and | ||
| // shard-level components so this would effectively be a memory leak | ||
| private static final WeakHashMap<String, WeakReference<Marker>> markers = new WeakHashMap<>(); | ||
|
|
||
| private final Marker marker; | ||
|
|
||
| public String prefix() { | ||
| return marker.getName(); | ||
| } | ||
|
|
||
| PrefixLogger(final ExtendedLogger logger, final String name, final String prefix) { | ||
| super(logger, name, null); | ||
|
|
||
| final String actualPrefix = (prefix == null ? "" : prefix).intern(); | ||
| final Marker actualMarker; | ||
| // markers is not thread-safe, so we synchronize access | ||
| synchronized (markers) { | ||
| final WeakReference<Marker> marker = markers.get(actualPrefix); | ||
| final Marker maybeMarker = marker == null ? null : marker.get(); | ||
| if (maybeMarker == null) { | ||
| actualMarker = new MarkerManager.Log4jMarker(actualPrefix); | ||
| markers.put(actualPrefix, new WeakReference<>(actualMarker)); | ||
| } else { | ||
| actualMarker = maybeMarker; | ||
| } | ||
| } | ||
| this.marker = actualMarker; | ||
| } | ||
|
|
||
| @Override | ||
| public void logMessage(final String fqcn, final Level level, final Marker marker, final Message message, final Throwable t) { | ||
| assert marker == null; | ||
| super.logMessage(fqcn, level, this.marker, message, t); | ||
| } | ||
|
|
||
| } | ||
221 changes: 0 additions & 221 deletions
221
core/src/main/java/org/elasticsearch/common/logging/PrefixMessageFactory.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why do we need the whole weak shabang? I get you are trying to reuse marker objects, and I guess the idea is to drop the map once it's not needed any more. If that's correct, I'm a bit queasy about it - I rather see a simpler concurrent map (is protecting with sync(markers) enough for weak references that the GC may collect?)
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.
Using weak references allows these to be cleaned up if they are in fact no longer in use.
A concurrent map will just be a memory leak, we will never clean up prefixes no longer used (think of index and shard prefixes).
The synchronization is not related to the weak references, it's just to protect the map from concurrent modifications (which are not thread safe).
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.
A sad, but valid point. add a comment?
What happens if
marker.get()returns non null at first call and a null at the second? should we capture it and make a hard reference to it?Also to clarify - this method can not guarantee markers will not be created twice with the same prefix, but it limits it considerably, correct?
Last - I'm still not convinced we shouldn't just go with creating a marker on each request and be done with it (it's on more object per logger, which I don't think is a huge deal).
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.
I pushed 40dae7a.
I pushed 40dae7a.
Why do you think that?
For a cluster with 365 indices, 5 shards per index, your proposal will lead to 37292 unique marker objects; with my proposal there are 2194 marker objects. That's why I think this is worth doing.
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.
Does having a hard reference to an object guarantees all weak references to it are kept around? I was worried the GC might decide to remove a weak reference just because, in which case we will create another marker. Again - not saying this is wrong, but looking to learn.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes (although it's usually called a "strong reference").