HADOOP-11932. MetricsSinkAdapter may hang when being stopped. Contributed by Brahma Reddy Battula

(cherry picked from commit f59612edd74d1bef2b60870c24c1f67c56b2b3cb)

(cherry picked from commit 5950c1f6f8ac6f514f8d2e8bfbd1f71747b097de)
This commit is contained in:
Jian He 2015-08-05 16:12:45 -07:00 committed by Vinod Kumar Vavilapalli
parent ca7fe71000
commit d8ddfea491
3 changed files with 66 additions and 3 deletions

View File

@ -69,6 +69,9 @@ Release 2.6.1 - UNRELEASED
HADOOP-8151. Error handling in snappy decompressor throws invalid
exceptions. (Matt Foley via harsh)
HADOOP-11932. MetricsSinkAdapter may hang when being stopped.
(Brahma Reddy Battula via jianhe)
Release 2.6.0 - 2014-11-18
INCOMPATIBLE CHANGES

View File

@ -198,15 +198,15 @@ void start() {
void stop() {
stopping = true;
sinkThread.interrupt();
if (sink instanceof Closeable) {
IOUtils.cleanup(LOG, (Closeable)sink);
}
try {
sinkThread.join();
}
catch (InterruptedException e) {
LOG.warn("Stop interrupted", e);
}
if (sink instanceof Closeable) {
IOUtils.cleanup(LOG, (Closeable)sink);
}
}
String name() {

View File

@ -18,6 +18,8 @@
package org.apache.hadoop.metrics2.impl;
import java.io.Closeable;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
@ -434,6 +436,64 @@ private void checkMetricsRecords(List<MetricsRecord> recs) {
new MetricGaugeInt(MsInfo.NumActiveSinks, 3)));
}
/**
* Class to verify HADOOP-11932. Instead of reading from HTTP, going in loop
* until closed.
*/
private static class TestClosableSink implements MetricsSink, Closeable {
boolean closed = false;
CountDownLatch collectingLatch;
public TestClosableSink(CountDownLatch collectingLatch) {
this.collectingLatch = collectingLatch;
}
@Override
public void init(SubsetConfiguration conf) {
}
@Override
public void close() throws IOException {
closed = true;
}
@Override
public void putMetrics(MetricsRecord record) {
while (!closed) {
collectingLatch.countDown();
}
}
@Override
public void flush() {
}
}
/**
* HADOOP-11932
*/
@Test(timeout = 5000)
public void testHangOnSinkRead() throws Exception {
new ConfigBuilder().add("*.period", 8)
.add("test.sink.test.class", TestSink.class.getName())
.save(TestMetricsConfig.getTestFilename("hadoop-metrics2-test"));
MetricsSystemImpl ms = new MetricsSystemImpl("Test");
ms.start();
try {
CountDownLatch collectingLatch = new CountDownLatch(1);
MetricsSink sink = new TestClosableSink(collectingLatch);
ms.registerSink("closeableSink",
"The sink will be used to test closeability", sink);
// trigger metric collection first time
ms.onTimerEvent();
// Make sure that sink is collecting metrics
assertTrue(collectingLatch.await(1, TimeUnit.SECONDS));
} finally {
ms.stop();
}
}
@Metrics(context="test")
private static class TestSource {
@Metric("C1 desc") MutableCounterLong c1;