Search in sources :

Example 1 with MetricRegistry

use of org.apache.hadoop.hbase.metrics.MetricRegistry in project hbase by apache.

the class ExampleMasterObserverWithMetrics method start.

@Override
public void start(CoprocessorEnvironment env) throws IOException {
    if (env instanceof MasterCoprocessorEnvironment) {
        // Obtain the MetricRegistry for the Master. Metrics from this registry will be reported
        // at the master level per-server.
        MetricRegistry registry = ((MasterCoprocessorEnvironment) env).getMetricRegistryForMaster();
        if (createTableTimer == null) {
            // Create a new Counter, or get the already registered counter.
            // It is much better to only call this once and save the Counter as a class field instead
            // of creating the counter every time a coprocessor method is invoked. This will negate
            // any performance bottleneck coming from map lookups tracking metrics in the registry.
            createTableTimer = registry.timer("CreateTable");
        // on stop(), we can remove these registered metrics via calling registry.remove(). But
        // it is not needed for coprocessors at the master level. If coprocessor is stopped,
        // the server is stopping anyway, so there will not be any resource leaks.
        }
        if (disableTableCounter == null) {
            disableTableCounter = registry.counter("DisableTable");
        }
        // Register a custom gauge. The Gauge object will be registered in the metrics registry and
        // periodically the getValue() is invoked to obtain the snapshot.
        registry.register("totalMemory", new Gauge<Long>() {

            @Override
            public Long getValue() {
                return getTotalMemory();
            }
        });
        // Register a custom gauge using Java-8 lambdas (Supplier converted into Gauge)
        registry.register("maxMemory", this::getMaxMemory);
    }
}
Also used : MetricRegistry(org.apache.hadoop.hbase.metrics.MetricRegistry) MasterCoprocessorEnvironment(org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment)

Example 2 with MetricRegistry

use of org.apache.hadoop.hbase.metrics.MetricRegistry in project hbase by apache.

the class TestCoprocessorMetrics method testRegionObserverAfterRegionClosed.

@Test
public void testRegionObserverAfterRegionClosed() throws IOException {
    final TableName tableName = TableName.valueOf(name.getMethodName());
    try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
        Admin admin = connection.getAdmin()) {
        admin.createTable(new HTableDescriptor(tableName).addFamily(new HColumnDescriptor(foo)).addCoprocessor(CustomRegionObserver.class.getName()), // create with 2 regions
        new byte[][] { foo });
        try (Table table = connection.getTable(tableName)) {
            table.get(new Get(foo));
            // 2 gets
            table.get(new Get(foo));
        }
        assertPreGetRequestsCounter(CustomRegionObserver.class);
        // close one of the regions
        try (RegionLocator locator = connection.getRegionLocator(tableName)) {
            HRegionLocation loc = locator.getRegionLocation(foo);
            admin.closeRegion(loc.getServerName(), loc.getRegionInfo());
            HRegionServer server = UTIL.getMiniHBaseCluster().getRegionServer(loc.getServerName());
            UTIL.waitFor(30000, () -> server.getOnlineRegion(loc.getRegionInfo().getRegionName()) == null);
            assertNull(server.getOnlineRegion(loc.getRegionInfo().getRegionName()));
        }
        // with only 1 region remaining, we should still be able to find the Counter
        assertPreGetRequestsCounter(CustomRegionObserver.class);
        // close the table
        admin.disableTable(tableName);
        MetricRegistryInfo info = MetricsCoprocessor.createRegistryInfoForRegionCoprocessor(CustomRegionObserver.class.getName());
        // ensure that MetricRegistry is deleted
        Optional<MetricRegistry> registry = MetricRegistries.global().get(info);
        assertFalse(registry.isPresent());
    }
}
Also used : RegionLocator(org.apache.hadoop.hbase.client.RegionLocator) Table(org.apache.hadoop.hbase.client.Table) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) MetricRegistry(org.apache.hadoop.hbase.metrics.MetricRegistry) Connection(org.apache.hadoop.hbase.client.Connection) Admin(org.apache.hadoop.hbase.client.Admin) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) HRegionServer(org.apache.hadoop.hbase.regionserver.HRegionServer) TableName(org.apache.hadoop.hbase.TableName) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) Get(org.apache.hadoop.hbase.client.Get) MetricRegistryInfo(org.apache.hadoop.hbase.metrics.MetricRegistryInfo) Test(org.junit.Test)

Example 3 with MetricRegistry

use of org.apache.hadoop.hbase.metrics.MetricRegistry in project hbase by apache.

the class TestCoprocessorMetrics method assertPreGetRequestsCounter.

/**
   * Helper for below tests
   */
private void assertPreGetRequestsCounter(Class<?> coprocClass) {
    // Find out the MetricRegistry used by the CP using the global registries
    MetricRegistryInfo info = MetricsCoprocessor.createRegistryInfoForRegionCoprocessor(coprocClass.getName());
    Optional<MetricRegistry> registry = MetricRegistries.global().get(info);
    assertTrue(registry.isPresent());
    Optional<Metric> metric = registry.get().get("preGetRequests");
    assertTrue(metric.isPresent());
    Counter preGetRequests = (Counter) metric.get();
    assertEquals(2, preGetRequests.getCount());
}
Also used : Counter(org.apache.hadoop.hbase.metrics.Counter) MetricRegistry(org.apache.hadoop.hbase.metrics.MetricRegistry) Metric(org.apache.hadoop.hbase.metrics.Metric) MetricRegistryInfo(org.apache.hadoop.hbase.metrics.MetricRegistryInfo)

Example 4 with MetricRegistry

use of org.apache.hadoop.hbase.metrics.MetricRegistry in project hbase by apache.

the class TestCoprocessorMetrics method testRegionServerObserver.

@Test
public void testRegionServerObserver() throws IOException {
    try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
        Admin admin = connection.getAdmin()) {
        LOG.info("Rolling WALs");
        admin.rollWALWriter(UTIL.getMiniHBaseCluster().getServerHoldingMeta());
    }
    // Find out the MetricRegistry used by the CP using the global registries
    MetricRegistryInfo info = MetricsCoprocessor.createRegistryInfoForRSCoprocessor(CustomRegionServerObserver.class.getName());
    Optional<MetricRegistry> registry = MetricRegistries.global().get(info);
    assertTrue(registry.isPresent());
    Optional<Metric> metric = registry.get().get("rollWALRequests");
    assertTrue(metric.isPresent());
    Counter rollWalRequests = (Counter) metric.get();
    assertEquals(1, rollWalRequests.getCount());
}
Also used : Counter(org.apache.hadoop.hbase.metrics.Counter) MetricRegistry(org.apache.hadoop.hbase.metrics.MetricRegistry) Connection(org.apache.hadoop.hbase.client.Connection) Metric(org.apache.hadoop.hbase.metrics.Metric) Admin(org.apache.hadoop.hbase.client.Admin) MetricRegistryInfo(org.apache.hadoop.hbase.metrics.MetricRegistryInfo) Test(org.junit.Test)

Example 5 with MetricRegistry

use of org.apache.hadoop.hbase.metrics.MetricRegistry in project hbase by apache.

the class TestCoprocessorMetrics method testWALObserver.

@Test
public void testWALObserver() throws IOException {
    // Find out the MetricRegistry used by the CP using the global registries
    MetricRegistryInfo info = MetricsCoprocessor.createRegistryInfoForWALCoprocessor(CustomWALObserver.class.getName());
    Optional<MetricRegistry> registry = MetricRegistries.global().get(info);
    assertTrue(registry.isPresent());
    Optional<Metric> metric = registry.get().get("walEditsCount");
    assertTrue(metric.isPresent());
    try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
        Admin admin = connection.getAdmin()) {
        admin.createTable(new HTableDescriptor(TableName.valueOf(name.getMethodName())).addFamily(new HColumnDescriptor("foo")));
        Counter rollWalRequests = (Counter) metric.get();
        long prevCount = rollWalRequests.getCount();
        assertTrue(prevCount > 0);
        try (Table table = connection.getTable(TableName.valueOf(name.getMethodName()))) {
            table.put(new Put(foo).addColumn(foo, foo, foo));
        }
        assertEquals(1, rollWalRequests.getCount() - prevCount);
    }
}
Also used : Table(org.apache.hadoop.hbase.client.Table) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) MetricRegistry(org.apache.hadoop.hbase.metrics.MetricRegistry) Connection(org.apache.hadoop.hbase.client.Connection) Admin(org.apache.hadoop.hbase.client.Admin) Put(org.apache.hadoop.hbase.client.Put) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Counter(org.apache.hadoop.hbase.metrics.Counter) Metric(org.apache.hadoop.hbase.metrics.Metric) MetricRegistryInfo(org.apache.hadoop.hbase.metrics.MetricRegistryInfo) Test(org.junit.Test)

Aggregations

MetricRegistry (org.apache.hadoop.hbase.metrics.MetricRegistry)8 MetricRegistryInfo (org.apache.hadoop.hbase.metrics.MetricRegistryInfo)7 Admin (org.apache.hadoop.hbase.client.Admin)5 Connection (org.apache.hadoop.hbase.client.Connection)5 Metric (org.apache.hadoop.hbase.metrics.Metric)5 Test (org.junit.Test)5 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)4 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)4 Table (org.apache.hadoop.hbase.client.Table)3 Counter (org.apache.hadoop.hbase.metrics.Counter)3 TableName (org.apache.hadoop.hbase.TableName)2 Put (org.apache.hadoop.hbase.client.Put)2 Timer (org.apache.hadoop.hbase.metrics.Timer)2 Entry (java.util.Map.Entry)1 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)1 Get (org.apache.hadoop.hbase.client.Get)1 Mutation (org.apache.hadoop.hbase.client.Mutation)1 RegionLocator (org.apache.hadoop.hbase.client.RegionLocator)1 MasterCoprocessorEnvironment (org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment)1 CoprocessorRpcChannel (org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel)1