Skip to content

Commit 627910b

Browse files
committed
HADOOP-18526. review feedback and test improvement.
Extended S3AInstrumentation and WeakRefMetricsSource to allow for the test to verify that the link from metrics to instrumentation works and is through the weak reference Change-Id: I4fc156dfa60d7c7a868d8b765777c0f445a83257
1 parent 4897454 commit 627910b

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/impl/WeakRefMetricsSource.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public String getName() {
7878
return name;
7979
}
8080

81+
/**
82+
* Get the source, will be null if the reference has been GC'd
83+
* @return the source reference
84+
*/
85+
public MetricsSource getSource() {
86+
return sourceWeakReference.get();
87+
}
88+
8189
@Override
8290
public String toString() {
8391
return "WeakRefMetricsSource{" +

hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AInstrumentation.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,17 @@ public void getMetrics(MetricsCollector collector, boolean all) {
699699
registry.snapshot(collector.addRecord(registry.info().name()), true);
700700
}
701701

702+
/**
703+
* if registered with the metrics, return the
704+
* name of the source.
705+
* @return the name of the metrics, or null if this instance is not bonded.
706+
*/
707+
public String getMetricSourceName() {
708+
return metricsSourceReference != null
709+
? metricsSourceReference.getName()
710+
: null;
711+
}
712+
702713
public void close() {
703714
if (metricsSourceReference != null) {
704715
// get the name

hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/TestInstrumentationLifecycle.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,23 @@
2020

2121
import java.net.URI;
2222

23+
import org.assertj.core.api.Assertions;
2324
import org.junit.Test;
2425

26+
import org.apache.hadoop.fs.impl.WeakRefMetricsSource;
27+
import org.apache.hadoop.metrics2.MetricsSource;
2528
import org.apache.hadoop.metrics2.MetricsSystem;
2629
import org.apache.hadoop.test.AbstractHadoopTestBase;
2730

2831
import static org.apache.hadoop.fs.s3a.S3AInstrumentation.getMetricsSystem;
2932
import static org.apache.hadoop.fs.s3a.Statistic.DIRECTORIES_CREATED;
3033
import static org.assertj.core.api.Assertions.assertThat;
3134

35+
/**
36+
* Test the {@link S3AInstrumentation} lifecycle, in particular how
37+
* it binds to hadoop metrics through a {@link WeakRefMetricsSource}
38+
* and that it will deregister itself in {@link S3AInstrumentation#close()}.
39+
*/
3240
public class TestInstrumentationLifecycle extends AbstractHadoopTestBase {
3341

3442
@Test
@@ -46,6 +54,16 @@ public void testDoubleClose() throws Throwable {
4654
.isNotNull();
4755

4856
MetricsSystem activeMetrics = getMetricsSystem();
57+
final String metricSourceName = instrumentation.getMetricSourceName();
58+
final MetricsSource source = activeMetrics.getSource(metricSourceName);
59+
// verify the source is registered through a weak ref, and that the
60+
// reference maps to the instance.
61+
Assertions.assertThat(source)
62+
.describedAs("metric source %s", metricSourceName)
63+
.isNotNull()
64+
.isInstanceOf(WeakRefMetricsSource.class)
65+
.extracting(m -> ((WeakRefMetricsSource) m).getSource())
66+
.isSameAs(instrumentation);
4967

5068
// this will close the metrics system
5169
instrumentation.close();

0 commit comments

Comments
 (0)