Java Code Examples for org.apache.hadoop.hbase.metrics.MetricRegistry

The following examples show how to use org.apache.hadoop.hbase.metrics.MetricRegistry. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hbase   Source File: TestCoprocessorMetrics.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMasterObserver() throws IOException {
  // Find out the MetricRegistry used by the CP using the global registries
  MetricRegistryInfo info = MetricsCoprocessor.createRegistryInfoForMasterCoprocessor(
      CustomMasterObserver.class.getName());
  Optional<MetricRegistry> registry =  MetricRegistries.global().get(info);
  assertTrue(registry.isPresent());

  Optional<Metric> metric = registry.get().get("CreateTable");
  assertTrue(metric.isPresent());

  try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
       Admin admin = connection.getAdmin()) {

    Timer createTableTimer = (Timer)metric.get();
    long prevCount = createTableTimer.getHistogram().getCount();
    LOG.info("Creating table");
    TableDescriptorBuilder tableDescriptorBuilder =
      TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()));
    ColumnFamilyDescriptor columnFamilyDescriptor =
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("foo")).build();
    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    admin.createTable(tableDescriptorBuilder.build());
    assertEquals(1, createTableTimer.getHistogram().getCount() - prevCount);
  }
}
 
Example 2
Source Project: hbase   Source File: HBaseMetrics2HadoopMetricsAdapter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Iterates over the MetricRegistry and adds them to the {@code builder}.
 *
 * @param builder A record builder
 */
public void snapshotAllMetrics(MetricRegistry metricRegistry, MetricsRecordBuilder builder) {
  Map<String, Metric> metrics = metricRegistry.getMetrics();

  for (Map.Entry<String, Metric> e: metrics.entrySet()) {
    // Always capitalize the name
    String name = StringUtils.capitalize(e.getKey());
    Metric metric = e.getValue();

    if (metric instanceof Gauge) {
      addGauge(name, (Gauge<?>) metric, builder);
    } else if (metric instanceof Counter) {
      addCounter(name, (Counter)metric, builder);
    } else if (metric instanceof Histogram) {
      addHistogram(name, (Histogram)metric, builder);
    } else if (metric instanceof Meter) {
      addMeter(name, (Meter)metric, builder);
    } else if (metric instanceof Timer) {
      addTimer(name, (Timer)metric, builder);
    } else {
      LOG.info("Ignoring unknown Metric class " + metric.getClass().getName());
    }
  }
}
 
Example 3
Source Project: phoenix   Source File: GlobalMetricRegistriesAdapter.java    License: Apache License 2.0 6 votes vote down vote up
private void snapshotAllMetrics(MetricRegistry metricRegistry, MetricsRecordBuilder builder) {
    Map<String, Metric> metrics = metricRegistry.getMetrics();
    Iterator iterator = metrics.entrySet().iterator();

    while(iterator.hasNext()) {
        Entry<String, Metric> e = (Entry)iterator.next();
        String name = StringUtils.capitalize(e.getKey());
        Metric metric = e.getValue();
        if (metric instanceof Gauge) {
            this.addGauge(name, (Gauge)metric, builder);
        } else if (metric instanceof Counter) {
            this.addCounter(name, (Counter)metric, builder);
        } else if (metric instanceof Histogram) {
            this.addHistogram(name, (Histogram)metric, builder);
        } else if (metric instanceof Meter) {
            this.addMeter(name, (Meter)metric, builder);
        } else if (metric instanceof Timer) {
            this.addTimer(name, (Timer)metric, builder);
        } else {
            LOGGER.info("Ignoring unknown Metric class " + metric.getClass().getName());
        }
    }
}
 
Example 4
Source Project: hbase   Source File: TestCoprocessorMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void start(CoprocessorEnvironment env) throws IOException {
  if (env instanceof MasterCoprocessorEnvironment) {
    MetricRegistry registry =
        ((MasterCoprocessorEnvironment) env).getMetricRegistryForMaster();

    createTableTimer  = registry.timer("CreateTable");
  }
}
 
Example 5
Source Project: hbase   Source File: TestCoprocessorMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void start(CoprocessorEnvironment env) throws IOException {
  if (env instanceof RegionServerCoprocessorEnvironment) {
    MetricRegistry registry =
        ((RegionServerCoprocessorEnvironment) env).getMetricRegistryForRegionServer();

    if (rollWALCounter == null) {
      rollWALCounter = registry.counter("rollWALRequests");
    }
  }
}
 
Example 6
Source Project: hbase   Source File: TestCoprocessorMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void start(CoprocessorEnvironment env) throws IOException {
  if (env instanceof WALCoprocessorEnvironment) {
    MetricRegistry registry =
        ((WALCoprocessorEnvironment) env).getMetricRegistryForRegionServer();

    if (walEditsCount == null) {
      walEditsCount = registry.counter("walEditsCount");
    }
  }
}
 
Example 7
Source Project: hbase   Source File: TestCoprocessorMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void start(CoprocessorEnvironment env) throws IOException {
  if (env instanceof RegionCoprocessorEnvironment) {
    MetricRegistry registry =
        ((RegionCoprocessorEnvironment) env).getMetricRegistryForRegionServer();

    if (preGetCounter == null) {
      preGetCounter = registry.counter("preGetRequests");
    }
  }
}
 
Example 8
Source Project: hbase   Source File: TestCoprocessorMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void start(CoprocessorEnvironment env) throws IOException {
  super.start(env);

  if (env instanceof RegionCoprocessorEnvironment) {
    MetricRegistry registry =
        ((RegionCoprocessorEnvironment) env).getMetricRegistryForRegionServer();

    if (endpointExecution == null) {
      endpointExecution = registry.timer("EndpointExecution");
    }
  }
}
 
Example 9
Source Project: hbase   Source File: TestCoprocessorMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@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()) {
    TableDescriptorBuilder tableDescriptorBuilder =
      TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()));
    ColumnFamilyDescriptor columnFamilyDescriptor =
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("foo")).build();
    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    admin.createTable(tableDescriptorBuilder.build());

    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);
  }
}
 
Example 10
Source Project: hbase   Source File: TestCoprocessorMetrics.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 11
Source Project: hbase   Source File: HBaseMetrics2HadoopMetricsAdapter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Iterates over the MetricRegistry and adds them to the {@code collector}.
 *
 * @param collector A metrics collector
 */
public void snapshotAllMetrics(MetricRegistry metricRegistry,
                               MetricsCollector collector) {
  MetricRegistryInfo info = metricRegistry.getMetricRegistryInfo();
  MetricsRecordBuilder builder = collector.addRecord(Interns.info(info.getMetricsName(),
      info.getMetricsDescription()));
  builder.setContext(info.getMetricsContext());

  snapshotAllMetrics(metricRegistry, builder);
}
 
Example 12
Source Project: hbase   Source File: ExampleRegionObserverWithMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void start(CoprocessorEnvironment env) throws IOException {
  // start for the RegionServerObserver will be called only once in the lifetime of the
  // server. We will construct and register all metrics that we will track across method
  // invocations.

  if (env instanceof RegionCoprocessorEnvironment) {
    // Obtain the MetricRegistry for the RegionServer. Metrics from this registry will be reported
    // at the region server level per-regionserver.
    MetricRegistry registry =
        ((RegionCoprocessorEnvironment) env).getMetricRegistryForRegionServer();
    observer = new ExampleRegionObserver();

    if (preGetCounter == 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.
      // Returned counter instance is shared by all coprocessors of the same class in the same
      // region server.
      preGetCounter = registry.counter("preGetRequests");
    }

    if (costlyOperationTimer == null) {
      // Create a Timer to track execution times for the costly operation.
      costlyOperationTimer = registry.timer("costlyOperation");
    }

    if (flushCounter == null) {
      // Track the number of flushes that have completed
      flushCounter = registry.counter("flushesCompleted");
    }

    if (filesCompactedCounter == null) {
      // Track the number of files that were compacted (many files may be rewritten in a single
      // compaction).
      filesCompactedCounter = registry.counter("filesCompacted");
    }
  }
}
 
Example 13
Source Project: phoenix   Source File: GlobalMetricRegistriesAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public void registerMetricRegistry(MetricRegistry registry) {
    if (registry == null) {
        LOGGER.warn("Registry cannot be registered with Hadoop Metrics 2 since it is null.");
        return;
    }

    HBaseMetrics2HadoopMetricsAdapter adapter = new HBaseMetrics2HadoopMetricsAdapter(registry);
    adapter.registerToDefaultMetricsSystem();
}
 
Example 14
Source Project: phoenix   Source File: GlobalMetricRegistriesAdapter.java    License: Apache License 2.0 5 votes vote down vote up
private void snapshotAllMetrics(MetricRegistry metricRegistry, MetricsCollector collector) {
    MetricRegistryInfo hbaseMetricRegistryInfo = metricRegistry.getMetricRegistryInfo();
    MetricsInfo hadoopMetricsInfo = Interns.info(hbaseMetricRegistryInfo.getMetricsName(), hbaseMetricRegistryInfo.getMetricsDescription());
    MetricsRecordBuilder builder = collector.addRecord(hadoopMetricsInfo);
    builder.setContext(hbaseMetricRegistryInfo.getMetricsContext());
    builder.tag(hadoopMetricsInfo, metricTag);
    this.snapshotAllMetrics(metricRegistry, builder);
}
 
Example 15
Source Project: pentaho-hadoop-shims   Source File: HadoopShim.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Class[] getHbaseDependencyClasses() {
  return new Class[] {
    HConstants.class, org.apache.hadoop.hbase.protobuf.generated.ClientProtos.class,
    org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.class, Put.class,
    RpcServer.class, CompatibilityFactory.class, JobUtil.class, TableMapper.class, FastLongHistogram.class,
    Snapshot.class, ZooKeeper.class, Channel.class, Message.class, UnsafeByteOperations.class, Lists.class,
    Tracer.class, MetricRegistry.class, ArrayUtils.class, ObjectMapper.class, Versioned.class,
    JsonView.class, ZKWatcher.class, CacheLoader.class
  };
}
 
Example 16
Source Project: hbase   Source File: MasterCoprocessorHost.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public MetricRegistry getMetricRegistryForMaster() {
  return metricRegistry;
}
 
Example 17
Source Project: hbase   Source File: MetricsCoprocessor.java    License: Apache License 2.0 4 votes vote down vote up
public static MetricRegistry createRegistryForMasterCoprocessor(String clazz) {
  return MetricRegistries.global().create(createRegistryInfoForMasterCoprocessor(clazz));
}
 
Example 18
Source Project: hbase   Source File: MetricsCoprocessor.java    License: Apache License 2.0 4 votes vote down vote up
public static MetricRegistry createRegistryForRSCoprocessor(String clazz) {
  return MetricRegistries.global().create(createRegistryInfoForRSCoprocessor(clazz));
}
 
Example 19
Source Project: hbase   Source File: MetricsCoprocessor.java    License: Apache License 2.0 4 votes vote down vote up
public static MetricRegistry createRegistryForRegionCoprocessor(String clazz) {
  return MetricRegistries.global().create(createRegistryInfoForRegionCoprocessor(clazz));
}
 
Example 20
Source Project: hbase   Source File: MetricsCoprocessor.java    License: Apache License 2.0 4 votes vote down vote up
public static MetricRegistry createRegistryForWALCoprocessor(String clazz) {
  return MetricRegistries.global().create(createRegistryInfoForWALCoprocessor(clazz));
}
 
Example 21
Source Project: hbase   Source File: MetricsCoprocessor.java    License: Apache License 2.0 4 votes vote down vote up
public static void removeRegistry(MetricRegistry registry) {
  if (registry == null) {
    return;
  }
  MetricRegistries.global().remove(registry.getMetricRegistryInfo());
}
 
Example 22
Source Project: hbase   Source File: RegionServerCoprocessorHost.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public MetricRegistry getMetricRegistryForRegionServer() {
  return metricRegistry;
}
 
Example 23
Source Project: hbase   Source File: WALCoprocessorHost.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public MetricRegistry getMetricRegistryForRegionServer() {
  return metricRegistry;
}
 
Example 24
Source Project: hbase   Source File: RegionCoprocessorHost.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public MetricRegistry getMetricRegistryForRegionServer() {
  return metricRegistry;
}
 
Example 25
Source Project: hbase   Source File: TestCoprocessorMetrics.java    License: Apache License 2.0 4 votes vote down vote up
@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 TableDescriptorBuilder.ModifyableTableDescriptor(tableName)
        .setColumnFamily(
          new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(foo))
        // add the coprocessor for the region
        .setCoprocessor(CustomRegionObserver.class.getName()), new byte[][]{foo});
    // create with 2 regions
    try (Table table = connection.getTable(tableName)) {
      table.get(new Get(foo));
      table.get(new Get(foo)); // 2 gets
    }

    assertPreGetRequestsCounter(CustomRegionObserver.class);

    // close one of the regions
    try (RegionLocator locator = connection.getRegionLocator(tableName)) {
      HRegionLocation loc = locator.getRegionLocation(foo);
      admin.unassign(loc.getRegion().getEncodedNameAsBytes(), true);

      HRegionServer server = UTIL.getMiniHBaseCluster().getRegionServer(loc.getServerName());
      UTIL.waitFor(30000,
        () -> server.getOnlineRegion(loc.getRegion().getRegionName()) == null);
      assertNull(server.getOnlineRegion(loc.getRegion().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());
  }
}
 
Example 26
Source Project: hbase   Source File: TestCoprocessorMetrics.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testRegionObserverEndpoint() throws IOException, ServiceException {
  final TableName tableName = TableName.valueOf(name.getMethodName());
  try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
       Admin admin = connection.getAdmin()) {
    admin.createTable(
      new TableDescriptorBuilder.ModifyableTableDescriptor(tableName)
        .setColumnFamily(
          new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(foo))
        // add the coprocessor for the region
        .setCoprocessor(CustomRegionEndpoint.class.getName()));

    try (Table table = connection.getTable(tableName)) {
      List<Mutation> mutations = Lists.newArrayList(new Put(foo), new Put(bar));
      MutateRowsRequest.Builder mrmBuilder = MutateRowsRequest.newBuilder();

      for (Mutation mutation : mutations) {
        mrmBuilder.addMutationRequest(ProtobufUtil.toMutation(
            ClientProtos.MutationProto.MutationType.PUT, mutation));
      }

      CoprocessorRpcChannel channel = table.coprocessorService(bar);
      MultiRowMutationService.BlockingInterface service =
          MultiRowMutationService.newBlockingStub(channel);
      MutateRowsRequest mrm = mrmBuilder.build();
      service.mutateRows(null, mrm);
    }
  }

  // Find out the MetricRegistry used by the CP using the global registries
  MetricRegistryInfo info = MetricsCoprocessor.createRegistryInfoForRegionCoprocessor(
      CustomRegionEndpoint.class.getName());

  Optional<MetricRegistry> registry =  MetricRegistries.global().get(info);
  assertTrue(registry.isPresent());

  Optional<Metric> metric = registry.get().get("EndpointExecution");
  assertTrue(metric.isPresent());

  Timer endpointExecutions = (Timer)metric.get();
  assertEquals(1, endpointExecutions.getHistogram().getCount());
}
 
Example 27
Source Project: hbase   Source File: MetricRegistriesImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public MetricRegistry create(MetricRegistryInfo info) {
  return registries.put(info, () -> factory.create(info));
}
 
Example 28
Source Project: hbase   Source File: MetricRegistriesImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Optional<MetricRegistry> get(MetricRegistryInfo info) {
  return Optional.ofNullable(registries.get(info));
}
 
Example 29
Source Project: hbase   Source File: MetricRegistriesImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Collection<MetricRegistry> getMetricRegistries() {
  return Collections.unmodifiableCollection(registries.values());
}
 
Example 30
Source Project: hbase   Source File: MetricRegistryFactoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public MetricRegistry create(MetricRegistryInfo info) {
  return new MetricRegistryImpl(info);
}