下面列出了怎么用org.apache.hadoop.hbase.metrics.MetricRegistry的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
}
/**
* 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());
}
}
}
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());
}
}
}
@Override
public void start(CoprocessorEnvironment env) throws IOException {
if (env instanceof MasterCoprocessorEnvironment) {
MetricRegistry registry =
((MasterCoprocessorEnvironment) env).getMetricRegistryForMaster();
createTableTimer = registry.timer("CreateTable");
}
}
@Override
public void start(CoprocessorEnvironment env) throws IOException {
if (env instanceof RegionServerCoprocessorEnvironment) {
MetricRegistry registry =
((RegionServerCoprocessorEnvironment) env).getMetricRegistryForRegionServer();
if (rollWALCounter == null) {
rollWALCounter = registry.counter("rollWALRequests");
}
}
}
@Override
public void start(CoprocessorEnvironment env) throws IOException {
if (env instanceof WALCoprocessorEnvironment) {
MetricRegistry registry =
((WALCoprocessorEnvironment) env).getMetricRegistryForRegionServer();
if (walEditsCount == null) {
walEditsCount = registry.counter("walEditsCount");
}
}
}
@Override
public void start(CoprocessorEnvironment env) throws IOException {
if (env instanceof RegionCoprocessorEnvironment) {
MetricRegistry registry =
((RegionCoprocessorEnvironment) env).getMetricRegistryForRegionServer();
if (preGetCounter == null) {
preGetCounter = registry.counter("preGetRequests");
}
}
}
@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");
}
}
}
@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);
}
}
/**
* 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());
}
/**
* 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);
}
@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");
}
}
}
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();
}
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);
}
@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
};
}
@Override
public MetricRegistry getMetricRegistryForMaster() {
return metricRegistry;
}
public static MetricRegistry createRegistryForMasterCoprocessor(String clazz) {
return MetricRegistries.global().create(createRegistryInfoForMasterCoprocessor(clazz));
}
public static MetricRegistry createRegistryForRSCoprocessor(String clazz) {
return MetricRegistries.global().create(createRegistryInfoForRSCoprocessor(clazz));
}
public static MetricRegistry createRegistryForRegionCoprocessor(String clazz) {
return MetricRegistries.global().create(createRegistryInfoForRegionCoprocessor(clazz));
}
public static MetricRegistry createRegistryForWALCoprocessor(String clazz) {
return MetricRegistries.global().create(createRegistryInfoForWALCoprocessor(clazz));
}
public static void removeRegistry(MetricRegistry registry) {
if (registry == null) {
return;
}
MetricRegistries.global().remove(registry.getMetricRegistryInfo());
}
@Override
public MetricRegistry getMetricRegistryForRegionServer() {
return metricRegistry;
}
@Override
public MetricRegistry getMetricRegistryForRegionServer() {
return metricRegistry;
}
@Override
public MetricRegistry getMetricRegistryForRegionServer() {
return metricRegistry;
}
@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());
}
}
@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());
}
@Override
public MetricRegistry create(MetricRegistryInfo info) {
return registries.put(info, () -> factory.create(info));
}
@Override
public Optional<MetricRegistry> get(MetricRegistryInfo info) {
return Optional.ofNullable(registries.get(info));
}
@Override
public Collection<MetricRegistry> getMetricRegistries() {
return Collections.unmodifiableCollection(registries.values());
}
@Override
public MetricRegistry create(MetricRegistryInfo info) {
return new MetricRegistryImpl(info);
}