Search in sources :

Example 1 with MetricsSink

use of org.apache.hadoop.metrics2.MetricsSink in project hadoop by apache.

the class TestMetricsSystemImpl method testInitFirstVerifyStopInvokedImmediately.

@Test
public void testInitFirstVerifyStopInvokedImmediately() throws Exception {
    DefaultMetricsSystem.shutdown();
    new ConfigBuilder().add("*.period", 8).add("test.sink.test.class", TestSink.class.getName()).add("test.*.source.filter.exclude", "s0").add("test.source.s1.metric.filter.exclude", "X*").add("test.sink.sink1.metric.filter.exclude", "Y*").add("test.sink.sink2.metric.filter.exclude", "Y*").save(TestMetricsConfig.getTestFilename("hadoop-metrics2-test"));
    MetricsSystemImpl ms = new MetricsSystemImpl("Test");
    ms.start();
    ms.register("s0", "s0 desc", new TestSource("s0rec"));
    TestSource s1 = ms.register("s1", "s1 desc", new TestSource("s1rec"));
    s1.c1.incr();
    s1.xxx.incr();
    s1.g1.set(2);
    s1.yyy.incr(2);
    s1.s1.add(0);
    MetricsSink sink1 = mock(MetricsSink.class);
    MetricsSink sink2 = mock(MetricsSink.class);
    ms.registerSink("sink1", "sink1 desc", sink1);
    ms.registerSink("sink2", "sink2 desc", sink2);
    // publish the metrics
    ms.publishMetricsNow();
    ms.stop();
    ms.shutdown();
    //When we call stop, at most two sources will be consumed by each sink thread.
    verify(sink1, atMost(2)).putMetrics(r1.capture());
    List<MetricsRecord> mr1 = r1.getAllValues();
    verify(sink2, atMost(2)).putMetrics(r2.capture());
    List<MetricsRecord> mr2 = r2.getAllValues();
    if (mr1.size() != 0 && mr2.size() != 0) {
        checkMetricsRecords(mr1);
        assertEquals("output", mr1, mr2);
    } else if (mr1.size() != 0) {
        checkMetricsRecords(mr1);
    } else if (mr2.size() != 0) {
        checkMetricsRecords(mr2);
    }
}
Also used : MetricsSink(org.apache.hadoop.metrics2.MetricsSink) MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) Test(org.junit.Test)

Example 2 with MetricsSink

use of org.apache.hadoop.metrics2.MetricsSink in project hadoop by apache.

the class TestMetricsSystemImpl method testHangOnSinkRead.

/**
   * 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();
    }
}
Also used : MetricsSink(org.apache.hadoop.metrics2.MetricsSink) Test(org.junit.Test)

Example 3 with MetricsSink

use of org.apache.hadoop.metrics2.MetricsSink in project hadoop by apache.

the class MetricsSystemImpl method registerSink.

synchronized void registerSink(String name, String desc, MetricsSink sink) {
    checkNotNull(config, "config");
    MetricsConfig conf = sinkConfigs.get(name);
    MetricsSinkAdapter sa = conf != null ? newSink(name, desc, sink, conf) : newSink(name, desc, sink, config.subset(SINK_KEY));
    sinks.put(name, sa);
    sa.start();
    LOG.info("Registered sink " + name);
}
Also used : MetricsConfig(org.apache.hadoop.metrics2.impl.MetricsConfig)

Example 4 with MetricsSink

use of org.apache.hadoop.metrics2.MetricsSink in project hadoop by apache.

the class TestMetricsSystemImpl method testInitFirstVerifyCallBacks.

@Test
public void testInitFirstVerifyCallBacks() throws Exception {
    DefaultMetricsSystem.shutdown();
    new ConfigBuilder().add("*.period", 8).add("test.sink.test.class", TestSink.class.getName()).add("test.*.source.filter.exclude", "s0").add("test.source.s1.metric.filter.exclude", "X*").add("test.sink.sink1.metric.filter.exclude", "Y*").add("test.sink.sink2.metric.filter.exclude", "Y*").save(TestMetricsConfig.getTestFilename("hadoop-metrics2-test"));
    MetricsSystemImpl ms = new MetricsSystemImpl("Test");
    ms.start();
    ms.register("s0", "s0 desc", new TestSource("s0rec"));
    TestSource s1 = ms.register("s1", "s1 desc", new TestSource("s1rec"));
    s1.c1.incr();
    s1.xxx.incr();
    s1.g1.set(2);
    s1.yyy.incr(2);
    s1.s1.add(0);
    MetricsSink sink1 = mock(MetricsSink.class);
    MetricsSink sink2 = mock(MetricsSink.class);
    ms.registerSink("sink1", "sink1 desc", sink1);
    ms.registerSink("sink2", "sink2 desc", sink2);
    // publish the metrics
    ms.publishMetricsNow();
    try {
        verify(sink1, timeout(200).times(2)).putMetrics(r1.capture());
        verify(sink2, timeout(200).times(2)).putMetrics(r2.capture());
    } finally {
        ms.stop();
        ms.shutdown();
    }
    //When we call stop, at most two sources will be consumed by each sink thread.
    List<MetricsRecord> mr1 = r1.getAllValues();
    List<MetricsRecord> mr2 = r2.getAllValues();
    checkMetricsRecords(mr1);
    assertEquals("output", mr1, mr2);
}
Also used : MetricsSink(org.apache.hadoop.metrics2.MetricsSink) MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) Test(org.junit.Test)

Example 5 with MetricsSink

use of org.apache.hadoop.metrics2.MetricsSink in project hadoop by apache.

the class TestMetricsSystemImpl method testQSize.

@Test
public void testQSize() throws Exception {
    new ConfigBuilder().add("*.period", 8).add("*.queue.capacity", 2).add("test.sink.test.class", TestSink.class.getName()).save(TestMetricsConfig.getTestFilename("hadoop-metrics2-test"));
    MetricsSystemImpl ms = new MetricsSystemImpl("Test");
    final CountDownLatch proceedSignal = new CountDownLatch(1);
    final CountDownLatch reachedPutMetricSignal = new CountDownLatch(1);
    ms.start();
    try {
        MetricsSink slowSink = mock(MetricsSink.class);
        MetricsSink dataSink = mock(MetricsSink.class);
        ms.registerSink("slowSink", "The sink that will wait on putMetric", slowSink);
        ms.registerSink("dataSink", "The sink I'll use to get info about slowSink", dataSink);
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                reachedPutMetricSignal.countDown();
                proceedSignal.await();
                return null;
            }
        }).when(slowSink).putMetrics(any(MetricsRecord.class));
        // trigger metric collection first time
        ms.onTimerEvent();
        assertTrue(reachedPutMetricSignal.await(1, TimeUnit.SECONDS));
        // Now that the slow sink is still processing the first metric,
        // its queue length should be 1 for the second collection.
        ms.onTimerEvent();
        verify(dataSink, timeout(500).times(2)).putMetrics(r1.capture());
        List<MetricsRecord> mr = r1.getAllValues();
        Number qSize = Iterables.find(mr.get(1).metrics(), new Predicate<AbstractMetric>() {

            @Override
            public boolean apply(@Nullable AbstractMetric input) {
                assert input != null;
                return input.name().equals("Sink_slowSinkQsize");
            }
        }).value();
        assertEquals(1, qSize);
    } finally {
        proceedSignal.countDown();
        ms.stop();
    }
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) Predicate(com.google.common.base.Predicate) Answer(org.mockito.stubbing.Answer) MetricsSink(org.apache.hadoop.metrics2.MetricsSink) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Aggregations

MetricsSink (org.apache.hadoop.metrics2.MetricsSink)4 Test (org.junit.Test)4 MetricsRecord (org.apache.hadoop.metrics2.MetricsRecord)3 Predicate (com.google.common.base.Predicate)1 Nullable (javax.annotation.Nullable)1 AbstractMetric (org.apache.hadoop.metrics2.AbstractMetric)1 MetricsConfig (org.apache.hadoop.metrics2.impl.MetricsConfig)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1