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

The following examples show how to use org.apache.hadoop.hbase.metrics.MetricRegistry. 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 File: GlobalMetricRegistriesAdapter.java    From phoenix with 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 #2
Source File: HBaseMetrics2HadoopMetricsAdapter.java    From hbase with 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 File: TestCoprocessorMetrics.java    From hbase with 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 #4
Source File: TestCoprocessorMetrics.java    From hbase with 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 #5
Source File: HadoopShim.java    From pentaho-hadoop-shims with 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 #6
Source File: GlobalMetricRegistriesAdapter.java    From phoenix with 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 #7
Source File: GlobalMetricRegistriesAdapter.java    From phoenix with 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 #8
Source File: ExampleRegionObserverWithMetrics.java    From hbase with 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 #9
Source File: HBaseMetrics2HadoopMetricsAdapter.java    From hbase with 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 #10
Source File: TestCoprocessorMetrics.java    From hbase with 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 #11
Source File: TestCoprocessorMetrics.java    From hbase with 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 #12
Source File: TestCoprocessorMetrics.java    From hbase with 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 #13
Source File: TestCoprocessorMetrics.java    From hbase with 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 #14
Source File: TestCoprocessorMetrics.java    From hbase with 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 #15
Source File: TestCoprocessorMetrics.java    From hbase with 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 #16
Source File: RegionCoprocessorHost.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public MetricRegistry getMetricRegistryForRegionServer() {
  return metricRegistry;
}
 
Example #17
Source File: MetricsCoprocessor.java    From hbase with 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 File: DelegateRegionCoprocessorEnvironment.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public MetricRegistry getMetricRegistryForRegionServer() {
    return delegate.getMetricRegistryForRegionServer();
}
 
Example #19
Source File: MetricsCoprocessor.java    From hbase with Apache License 2.0 4 votes vote down vote up
public static MetricRegistry createRegistryForRSCoprocessor(String clazz) {
  return MetricRegistries.global().create(createRegistryInfoForRSCoprocessor(clazz));
}
 
Example #20
Source File: MetricsCoprocessor.java    From hbase with Apache License 2.0 4 votes vote down vote up
public static MetricRegistry createRegistryForRegionCoprocessor(String clazz) {
  return MetricRegistries.global().create(createRegistryInfoForRegionCoprocessor(clazz));
}
 
Example #21
Source File: GlobalMetricRegistriesAdapter.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private HBaseMetrics2HadoopMetricsAdapter(MetricRegistry registry) {
    this.registry = registry;
    metricTag = QueryServicesOptions.withDefaults().getClientMetricTag();
}
 
Example #22
Source File: MetricsCoprocessor.java    From hbase with Apache License 2.0 4 votes vote down vote up
public static MetricRegistry createRegistryForWALCoprocessor(String clazz) {
  return MetricRegistries.global().create(createRegistryInfoForWALCoprocessor(clazz));
}
 
Example #23
Source File: ExampleMasterObserverWithMetrics.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public void start(CoprocessorEnvironment env) throws IOException {
  // start for the MasterObserver 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 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);
  }
}
 
Example #24
Source File: MetricsCoprocessor.java    From hbase with 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 #25
Source File: MetricsTableQueryMeterImpl.java    From hbase with Apache License 2.0 4 votes vote down vote up
TableMeters(MetricRegistry metricRegistry, TableName tableName) {
  this.tableReadQueryMeter = metricRegistry.meter(qualifyMetricsName(tableName,
    TABLE_READ_QUERY_PER_SECOND));
  this.tableWriteQueryMeter =
    metricRegistry.meter(qualifyMetricsName(tableName, TABLE_WRITE_QUERY_PER_SECOND));
}
 
Example #26
Source File: MetricsTableQueryMeterImpl.java    From hbase with Apache License 2.0 4 votes vote down vote up
public MetricsTableQueryMeterImpl(MetricRegistry metricRegistry) {
  this.metricRegistry = metricRegistry;
}
 
Example #27
Source File: GlobalMetricRegistriesAdapter.java    From hbase with Apache License 2.0 4 votes vote down vote up
MetricsSourceAdapter(MetricRegistry registry) {
  this.registry = registry;
}
 
Example #28
Source File: MetricRegistryFactoryImpl.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public MetricRegistry create(MetricRegistryInfo info) {
  return new MetricRegistryImpl(info);
}
 
Example #29
Source File: MetricRegistriesImpl.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<MetricRegistry> getMetricRegistries() {
  return Collections.unmodifiableCollection(registries.values());
}
 
Example #30
Source File: MetricRegistriesImpl.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<MetricRegistry> get(MetricRegistryInfo info) {
  return Optional.ofNullable(registries.get(info));
}