Search in sources :

Example 1 with MetricsSourceBuilder

use of org.apache.hadoop.metrics2.lib.MetricsSourceBuilder in project hadoop by apache.

the class MetricsSystemImpl method register.

@Override
public synchronized <T> T register(String name, String desc, T source) {
    MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(source);
    final MetricsSource s = sb.build();
    MetricsInfo si = sb.info();
    String name2 = name == null ? si.name() : name;
    final String finalDesc = desc == null ? si.description() : desc;
    final // be friendly to non-metrics tests
    String finalName = DefaultMetricsSystem.sourceName(name2, !monitoring);
    allSources.put(finalName, s);
    LOG.debug(finalName + ", " + finalDesc);
    if (monitoring) {
        registerSource(finalName, finalDesc, s);
    }
    // We want to re-register the source to pick up new config when the
    // metrics system restarts.
    register(finalName, new AbstractCallback() {

        @Override
        public void postStart() {
            registerSource(finalName, finalDesc, s);
        }
    });
    return source;
}
Also used : MetricsInfo(org.apache.hadoop.metrics2.MetricsInfo) MetricsSource(org.apache.hadoop.metrics2.MetricsSource) MetricsSourceBuilder(org.apache.hadoop.metrics2.lib.MetricsSourceBuilder)

Example 2 with MetricsSourceBuilder

use of org.apache.hadoop.metrics2.lib.MetricsSourceBuilder in project hadoop by apache.

the class TestMetricsSourceAdapter method testGetMetricsAndJmx.

@Test
public void testGetMetricsAndJmx() throws Exception {
    // create test source with a single metric counter of value 0
    TestSource source = new TestSource("test");
    MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(source);
    final MetricsSource s = sb.build();
    List<MetricsTag> injectedTags = new ArrayList<MetricsTag>();
    MetricsSourceAdapter sa = new MetricsSourceAdapter("test", "test", "test desc", s, injectedTags, null, null, 1, false);
    // all metrics are initially assumed to have changed
    MetricsCollectorImpl builder = new MetricsCollectorImpl();
    Iterable<MetricsRecordImpl> metricsRecords = sa.getMetrics(builder, true);
    // Validate getMetrics and JMX initial values
    MetricsRecordImpl metricsRecord = metricsRecords.iterator().next();
    assertEquals(0L, metricsRecord.metrics().iterator().next().value().longValue());
    // skip JMX cache TTL
    Thread.sleep(100);
    assertEquals(0L, (Number) sa.getAttribute("C1"));
    // change metric value
    source.incrementCnt();
    // validate getMetrics and JMX
    builder = new MetricsCollectorImpl();
    metricsRecords = sa.getMetrics(builder, true);
    metricsRecord = metricsRecords.iterator().next();
    assertTrue(metricsRecord.metrics().iterator().hasNext());
    // skip JMX cache TTL
    Thread.sleep(100);
    assertEquals(1L, (Number) sa.getAttribute("C1"));
}
Also used : MetricsSource(org.apache.hadoop.metrics2.MetricsSource) MetricsSourceBuilder(org.apache.hadoop.metrics2.lib.MetricsSourceBuilder) ArrayList(java.util.ArrayList) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) Test(org.junit.Test)

Example 3 with MetricsSourceBuilder

use of org.apache.hadoop.metrics2.lib.MetricsSourceBuilder in project hadoop by apache.

the class CleanerMetrics method create.

static CleanerMetrics create() {
    MetricsSystem ms = DefaultMetricsSystem.instance();
    CleanerMetrics metricObject = new CleanerMetrics();
    MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(metricObject);
    final MetricsSource s = sb.build();
    ms.register("cleaner", "The cleaner service of truly shared cache", s);
    metricObject.metricSource = s;
    return metricObject;
}
Also used : MetricsSource(org.apache.hadoop.metrics2.MetricsSource) MetricsSourceBuilder(org.apache.hadoop.metrics2.lib.MetricsSourceBuilder) MetricsSystem(org.apache.hadoop.metrics2.MetricsSystem) DefaultMetricsSystem(org.apache.hadoop.metrics2.lib.DefaultMetricsSystem)

Example 4 with MetricsSourceBuilder

use of org.apache.hadoop.metrics2.lib.MetricsSourceBuilder in project hadoop by apache.

the class TestMetricsSourceAdapter method testPurgeOldMetrics.

@Test
public void testPurgeOldMetrics() throws Exception {
    // create test source with a single metric counter of value 1
    PurgableSource source = new PurgableSource();
    MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(source);
    final MetricsSource s = sb.build();
    List<MetricsTag> injectedTags = new ArrayList<MetricsTag>();
    MetricsSourceAdapter sa = new MetricsSourceAdapter("tst", "tst", "testdesc", s, injectedTags, null, null, 1, false);
    MBeanInfo info = sa.getMBeanInfo();
    boolean sawIt = false;
    for (MBeanAttributeInfo mBeanAttributeInfo : info.getAttributes()) {
        sawIt |= mBeanAttributeInfo.getName().equals(source.lastKeyName);
    }
    ;
    assertTrue("The last generated metric is not exported to jmx", sawIt);
    // skip JMX cache TTL
    Thread.sleep(1000);
    info = sa.getMBeanInfo();
    sawIt = false;
    for (MBeanAttributeInfo mBeanAttributeInfo : info.getAttributes()) {
        sawIt |= mBeanAttributeInfo.getName().equals(source.lastKeyName);
    }
    ;
    assertTrue("The last generated metric is not exported to jmx", sawIt);
}
Also used : MetricsSource(org.apache.hadoop.metrics2.MetricsSource) MBeanInfo(javax.management.MBeanInfo) MetricsSourceBuilder(org.apache.hadoop.metrics2.lib.MetricsSourceBuilder) ArrayList(java.util.ArrayList) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) Test(org.junit.Test)

Example 5 with MetricsSourceBuilder

use of org.apache.hadoop.metrics2.lib.MetricsSourceBuilder in project hadoop by apache.

the class TestMetricsSourceAdapter method testMetricCacheUpdateRace.

/**
   * Test a race condition when updating the JMX cache (HADOOP-12482):
   * 1. Thread A reads the JMX metric every 2 JMX cache TTL. It marks the JMX
   *    cache to be updated by marking lastRecs to null. After this it adds a
   *    new key to the metrics. The next call to read should pick up this new
   *    key.
   * 2. Thread B triggers JMX metric update every 1 JMX cache TTL. It assigns
   *    lastRecs to a new object (not null any more).
   * 3. Thread A tries to read JMX metric again, sees lastRecs is not null and
   *    does not update JMX cache. As a result the read does not pickup the new
   *    metric.
   * @throws Exception
   */
@Test
public void testMetricCacheUpdateRace() throws Exception {
    // Create test source with a single metric counter of value 1.
    TestMetricsSource source = new TestMetricsSource();
    MetricsSourceBuilder sourceBuilder = MetricsAnnotations.newSourceBuilder(source);
    // ms
    final long JMX_CACHE_TTL = 250;
    List<MetricsTag> injectedTags = new ArrayList<>();
    MetricsSourceAdapter sourceAdapter = new MetricsSourceAdapter("test", "test", "test JMX cache update race condition", sourceBuilder.build(), injectedTags, null, null, JMX_CACHE_TTL, false);
    ScheduledExecutorService updaterExecutor = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder().build());
    ScheduledExecutorService readerExecutor = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder().build());
    final AtomicBoolean hasError = new AtomicBoolean(false);
    // Wake up every 1 JMX cache TTL to set lastRecs before updateJmxCache() is
    // called.
    SourceUpdater srcUpdater = new SourceUpdater(sourceAdapter, hasError);
    ScheduledFuture<?> updaterFuture = updaterExecutor.scheduleAtFixedRate(srcUpdater, sourceAdapter.getJmxCacheTTL(), sourceAdapter.getJmxCacheTTL(), TimeUnit.MILLISECONDS);
    srcUpdater.setFuture(updaterFuture);
    // Wake up every 2 JMX cache TTL so updateJmxCache() will try to update
    // JMX cache.
    SourceReader srcReader = new SourceReader(source, sourceAdapter, hasError);
    ScheduledFuture<?> readerFuture = readerExecutor.scheduleAtFixedRate(srcReader, // set JMX info cache at the beginning
    0, 2 * sourceAdapter.getJmxCacheTTL(), TimeUnit.MILLISECONDS);
    srcReader.setFuture(readerFuture);
    // Let the threads do their work.
    Thread.sleep(RACE_TEST_RUNTIME);
    assertFalse("Hit error", hasError.get());
    // cleanup
    updaterExecutor.shutdownNow();
    readerExecutor.shutdownNow();
    updaterExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS);
    readerExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MetricsSourceBuilder(org.apache.hadoop.metrics2.lib.MetricsSourceBuilder) ArrayList(java.util.ArrayList) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Test(org.junit.Test)

Aggregations

MetricsSourceBuilder (org.apache.hadoop.metrics2.lib.MetricsSourceBuilder)5 MetricsSource (org.apache.hadoop.metrics2.MetricsSource)4 ArrayList (java.util.ArrayList)3 MetricsTag (org.apache.hadoop.metrics2.MetricsTag)3 Test (org.junit.Test)3 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)1 MBeanInfo (javax.management.MBeanInfo)1 MetricsInfo (org.apache.hadoop.metrics2.MetricsInfo)1 MetricsSystem (org.apache.hadoop.metrics2.MetricsSystem)1 DefaultMetricsSystem (org.apache.hadoop.metrics2.lib.DefaultMetricsSystem)1