Skip to content

HBASE-29322 The TableRequests metric is blocked counted. #6995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -126,6 +127,18 @@ private void doRun() {
continue;
}

// To determine whether a new MetricRegistry is created in registries,
// Need to remove the old MetricRegistry in registeredSources for the new MetricRegistry
// https://issues.apache.org/jira/browse/HBASE-29322
MetricsSourceAdapter registeredSource = registeredSources.get(info);
if (registeredSource != null && !Objects.equals(registeredSource.registry, registry)) {
registeredSources.remove(info);
DefaultMetricsSystem.instance().unregisterSource(info.getMetricsJmxContext());
helper.removeSourceName(info.getMetricsJmxContext());
helper.removeObjectName(info.getMetricsJmxContext());
LOG.info("Unregistered old adapter for the MetricRegistry: " + info.getMetricsJmxContext());
}

if (!registeredSources.containsKey(info)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Registering adapter for the MetricRegistry: " + info.getMetricsJmxContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,9 @@ public void updateTableWriteQueryMeter() {
public MetricRegistryInfo getMetricRegistryInfo() {
return registryInfo;
}

// Visible for testing
public MetricRegistry getMetricRegistry() {
return registry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.util.Optional;
Expand All @@ -35,6 +36,9 @@
import org.apache.hadoop.hbase.regionserver.metrics.MetricsTableRequests;
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.metrics2.MetricsCollector;
import org.apache.hadoop.metrics2.MetricsSource;
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand Down Expand Up @@ -129,4 +133,64 @@ public void testTableQueryMeterSwitch() {
assertTrue(read.isPresent());
assertEquals(((DropwizardMeter) read.get()).getCount(), 500);
}

/**
* When a new MetricRegistry object is created, it needs to be re-registered (HBASE-29322)
*/
@Test
public void testMetricRegistryReregistered() {
TableName tn = TableName.valueOf("test_table");
// Create a new MetricRegistry for the first time
MetricsTableRequests requests = new MetricsTableRequests(tn, new Configuration());
MetricRegistryInfo info = requests.getMetricRegistryInfo();
MetricRegistry oldRegistry = requests.getMetricRegistry();
// Register the MetricRegistry with the DefaultMetricsSystem
MetricsSourceAdapter adapter = new MetricsSourceAdapter(oldRegistry);
DefaultMetricsSystem.instance().register(info.getMetricsJmxContext(),
info.getMetricsDescription(), adapter);

Optional<MetricRegistry> registry1 = MetricRegistries.global().get(info);
assertTrue(registry1.isPresent());
assertEquals(oldRegistry, registry1.get());
// remove old MetricRegistry
requests.removeRegistry();

// Recreate a new MetricRegistry
requests = new MetricsTableRequests(tn, new Configuration());
MetricRegistry newRegistry = requests.getMetricRegistry();
Optional<MetricRegistry> registry2 = MetricRegistries.global().get(info);
assertTrue(registry2.isPresent());
assertEquals(newRegistry, registry2.get());

// The old MetricRegistry is still registered in the DefaultMetricsSystem
MetricsSourceAdapter metricsSource =
(MetricsSourceAdapter) DefaultMetricsSystem.instance().getSource(info.getMetricsJmxContext());
assertEquals(oldRegistry, metricsSource.registry);
assertNotEquals(newRegistry, metricsSource.registry);

// Re-register the new MetricRegistry in DefaultMetricsSystem
DefaultMetricsSystem.instance().unregisterSource(info.getMetricsJmxContext());
MetricsSourceAdapter newAdapter = new MetricsSourceAdapter(newRegistry);
DefaultMetricsSystem.instance().register(info.getMetricsJmxContext(),
info.getMetricsDescription(), newAdapter);

// The old MetricRegistry is replaced
MetricsSourceAdapter newMetricsSource =
(MetricsSourceAdapter) DefaultMetricsSystem.instance().getSource(info.getMetricsJmxContext());
assertNotEquals(oldRegistry, newMetricsSource.registry);
assertEquals(newRegistry, newMetricsSource.registry);
}

private static class MetricsSourceAdapter implements MetricsSource {
private final MetricRegistry registry;

MetricsSourceAdapter(MetricRegistry registry) {
this.registry = registry;
}

@Override
public void getMetrics(MetricsCollector collector, boolean all) {

}
}
}