-
Notifications
You must be signed in to change notification settings - Fork 9.1k
HADOOP-18526. Leak of S3AInstrumentation instances via hadoop Metrics references #5144
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
steveloughran
merged 5 commits into
apache:trunk
from
steveloughran:s3/HADOOP-18526-s3a-metrics-leak
Dec 14, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a66532
HADOOP-18526. Leak of S3AInstrumentation instances via hadoop Metrics…
steveloughran cf0b5e1
HADOOP-18526. Leak of S3AInstrumentation instances via hadoop Metrics…
steveloughran f8cdd3d
HADOOP-18526. checkstyle and TRACE of stack in s3afs.initialize()
steveloughran 4897454
HADOOP-18526. s3a instrumentation
steveloughran 627910b
HADOOP-18526. review feedback and test improvement.
steveloughran 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
97 changes: 97 additions & 0 deletions
97
...n-project/hadoop-common/src/main/java/org/apache/hadoop/fs/impl/WeakRefMetricsSource.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,97 @@ | ||
/* | ||
* 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.hadoop.fs.impl; | ||
|
||
import java.lang.ref.WeakReference; | ||
|
||
import org.apache.hadoop.classification.InterfaceAudience; | ||
import org.apache.hadoop.metrics2.MetricsCollector; | ||
import org.apache.hadoop.metrics2.MetricsSource; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* A weak referenced metrics source which avoids hanging on to large objects | ||
* if somehow they don't get fully closed/cleaned up. | ||
* The JVM may clean up all objects which are only weakly referenced whenever | ||
* it does a GC, <i>even if there is no memory pressure</i>. | ||
* To avoid these refs being removed, always keep a strong reference around | ||
* somewhere. | ||
*/ | ||
@InterfaceAudience.Private | ||
public class WeakRefMetricsSource implements MetricsSource { | ||
|
||
/** | ||
* Name to know when unregistering. | ||
*/ | ||
private final String name; | ||
|
||
/** | ||
* Underlying metrics source. | ||
*/ | ||
private final WeakReference<MetricsSource> sourceWeakReference; | ||
|
||
/** | ||
* Constructor. | ||
* @param name Name to know when unregistering. | ||
* @param source metrics source | ||
*/ | ||
public WeakRefMetricsSource(final String name, final MetricsSource source) { | ||
this.name = name; | ||
this.sourceWeakReference = new WeakReference<>(requireNonNull(source)); | ||
} | ||
|
||
/** | ||
* If the weak reference is non null, update the metrics. | ||
* @param collector to contain the resulting metrics snapshot | ||
* @param all if true, return all metrics even if unchanged. | ||
*/ | ||
@Override | ||
public void getMetrics(final MetricsCollector collector, final boolean all) { | ||
MetricsSource metricsSource = sourceWeakReference.get(); | ||
if (metricsSource != null) { | ||
metricsSource.getMetrics(collector, all); | ||
} | ||
} | ||
|
||
/** | ||
* Name to know when unregistering. | ||
* @return the name passed in during construction. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Get the source, will be null if the reference has been GC'd | ||
* @return the source reference | ||
*/ | ||
public MetricsSource getSource() { | ||
return sourceWeakReference.get(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "WeakRefMetricsSource{" + | ||
"name='" + name + '\'' + | ||
", sourceWeakReference is " + | ||
(sourceWeakReference.get() == null ? "unset" : "set") + | ||
'}'; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -138,7 +138,6 @@ | |
import org.apache.hadoop.fs.statistics.DurationTracker; | ||
import org.apache.hadoop.fs.statistics.DurationTrackerFactory; | ||
import org.apache.hadoop.fs.statistics.IOStatistics; | ||
import org.apache.hadoop.fs.statistics.IOStatisticsLogging; | ||
import org.apache.hadoop.fs.statistics.IOStatisticsSource; | ||
import org.apache.hadoop.fs.statistics.IOStatisticsContext; | ||
import org.apache.hadoop.fs.statistics.impl.IOStatisticsStore; | ||
|
@@ -459,6 +458,13 @@ public void initialize(URI name, Configuration originalConf) | |
AuditSpan span = null; | ||
try { | ||
LOG.debug("Initializing S3AFileSystem for {}", bucket); | ||
if (LOG.isTraceEnabled()) { | ||
// log a full trace for deep diagnostics of where an object is created, | ||
// for tracking down memory leak issues. | ||
LOG.trace("Filesystem for {} created; fs.s3a.impl.disable.cache = {}", | ||
name, originalConf.getBoolean("fs.s3a.impl.disable.cache", false), | ||
new RuntimeException(super.toString())); | ||
} | ||
// clone the configuration into one with propagated bucket options | ||
Configuration conf = propagateBucketOptions(originalConf, bucket); | ||
// HADOOP-17894. remove references to s3a stores in JCEKS credentials. | ||
|
@@ -3999,22 +4005,18 @@ public void close() throws IOException { | |
} | ||
isClosed = true; | ||
LOG.debug("Filesystem {} is closed", uri); | ||
if (getConf() != null) { | ||
String iostatisticsLoggingLevel = | ||
getConf().getTrimmed(IOSTATISTICS_LOGGING_LEVEL, | ||
IOSTATISTICS_LOGGING_LEVEL_DEFAULT); | ||
logIOStatisticsAtLevel(LOG, iostatisticsLoggingLevel, getIOStatistics()); | ||
} | ||
try { | ||
super.close(); | ||
} finally { | ||
stopAllServices(); | ||
mukund-thakur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
// Log IOStatistics at debug. | ||
if (LOG.isDebugEnabled()) { | ||
// robust extract and convert to string | ||
LOG.debug("Statistics for {}: {}", uri, | ||
IOStatisticsLogging.ioStatisticsToPrettyString(getIOStatistics())); | ||
// log IO statistics, including of any file deletion during | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it means "including iostatistics of any file deletion..." so IMO it's valid |
||
// superclass close | ||
if (getConf() != null) { | ||
String iostatisticsLoggingLevel = | ||
getConf().getTrimmed(IOSTATISTICS_LOGGING_LEVEL, | ||
IOSTATISTICS_LOGGING_LEVEL_DEFAULT); | ||
logIOStatisticsAtLevel(LOG, iostatisticsLoggingLevel, getIOStatistics()); | ||
} | ||
} | ||
} | ||
|
||
|
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
104 changes: 104 additions & 0 deletions
104
...tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/TestInstrumentationLifecycle.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,104 @@ | ||
/* | ||
* 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.hadoop.fs.s3a; | ||
|
||
import java.net.URI; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
|
||
import org.apache.hadoop.fs.impl.WeakRefMetricsSource; | ||
import org.apache.hadoop.metrics2.MetricsSource; | ||
import org.apache.hadoop.metrics2.MetricsSystem; | ||
import org.apache.hadoop.test.AbstractHadoopTestBase; | ||
|
||
import static org.apache.hadoop.fs.s3a.S3AInstrumentation.getMetricsSystem; | ||
import static org.apache.hadoop.fs.s3a.Statistic.DIRECTORIES_CREATED; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Test the {@link S3AInstrumentation} lifecycle, in particular how | ||
* it binds to hadoop metrics through a {@link WeakRefMetricsSource} | ||
* and that it will deregister itself in {@link S3AInstrumentation#close()}. | ||
*/ | ||
public class TestInstrumentationLifecycle extends AbstractHadoopTestBase { | ||
steveloughran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Test | ||
public void testDoubleClose() throws Throwable { | ||
S3AInstrumentation instrumentation = new S3AInstrumentation(new URI("s3a://example/")); | ||
|
||
// the metric system is created in the constructor | ||
assertThat(S3AInstrumentation.hasMetricSystem()) | ||
.describedAs("S3AInstrumentation.hasMetricSystem()") | ||
.isTrue(); | ||
// ask for a metric | ||
String metricName = DIRECTORIES_CREATED.getSymbol(); | ||
assertThat(instrumentation.lookupMetric(metricName)) | ||
.describedAs("lookupMetric(%s) while open", metricName) | ||
.isNotNull(); | ||
|
||
MetricsSystem activeMetrics = getMetricsSystem(); | ||
final String metricSourceName = instrumentation.getMetricSourceName(); | ||
final MetricsSource source = activeMetrics.getSource(metricSourceName); | ||
// verify the source is registered through a weak ref, and that the | ||
// reference maps to the instance. | ||
Assertions.assertThat(source) | ||
.describedAs("metric source %s", metricSourceName) | ||
.isNotNull() | ||
.isInstanceOf(WeakRefMetricsSource.class) | ||
.extracting(m -> ((WeakRefMetricsSource) m).getSource()) | ||
.isSameAs(instrumentation); | ||
|
||
// this will close the metrics system | ||
instrumentation.close(); | ||
|
||
// iostats is still valid | ||
assertThat(instrumentation.getIOStatistics()) | ||
.describedAs("iostats of %s", instrumentation) | ||
.isNotNull(); | ||
|
||
// no metrics | ||
assertThat(S3AInstrumentation.hasMetricSystem()) | ||
.describedAs("S3AInstrumentation.hasMetricSystem()") | ||
.isFalse(); | ||
|
||
// metric lookup still works, so any invocation of an s3a | ||
// method which still updates a metric also works | ||
assertThat(instrumentation.lookupMetric(metricName)) | ||
.describedAs("lookupMetric(%s) when closed", metricName) | ||
.isNotNull(); | ||
|
||
// which we can implicitly verify by asking for it and | ||
// verifying that we get given a different one back | ||
// from the demand-created instance | ||
MetricsSystem metrics2 = getMetricsSystem(); | ||
assertThat(metrics2) | ||
.describedAs("metric system 2") | ||
.isNotSameAs(activeMetrics); | ||
|
||
// this is going to be a no-op | ||
instrumentation.close(); | ||
|
||
// which we can verify because the metrics system doesn't | ||
// get closed this time | ||
assertThat(getMetricsSystem()) | ||
.describedAs("metric system 3") | ||
.isSameAs(metrics2); | ||
} | ||
} |
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 RTE?
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.
we don't throw it, just trace it. it can be anything. what is your suggestion?
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 not just print it? I mean I don't understand the reason behind wrapping in RuntimeEx.
Also base FileStystem doesn't implement toString() so there won't be anything. Why not use this.tpString()?
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.
it is that stack trace which is the most critical thing as it lets us track down where FS instances are being created and then not closed.