This is an automated email from the ASF dual-hosted git repository.
Apache9 pushed a commit to branch branch-2
in repository https://gitbox.apache.org/repos/asf/hbase.git
The following commit(s) were added to refs/heads/branch-2 by this push:
new b1b8816b159 HBASE-30131 Rewrite TestAsyncMetaRegionLocator (#8206)
(#8216)
b1b8816b159 is described below
commit b1b8816b159c81151ad4630b44020a1146626524
Author: Duo Zhang <[email protected]>
AuthorDate: Mon May 11 09:45:01 2026 +0800
HBASE-30131 Rewrite TestAsyncMetaRegionLocator (#8206) (#8216)
(cherry picked from commit fbcd02d52321efbc10aa5825fb6eb66633017737)
Signed-off-by: Xiao Liu <[email protected]>
---
.../hbase/client/TestAsyncMetaRegionLocator.java | 281 ++++++++-------------
1 file changed, 109 insertions(+), 172 deletions(-)
diff --git
a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncMetaRegionLocator.java
b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncMetaRegionLocator.java
index 1b3a473624b..62035151bae 100644
---
a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncMetaRegionLocator.java
+++
b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncMetaRegionLocator.java
@@ -28,218 +28,155 @@ import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.hasItem;
import io.opentelemetry.api.trace.SpanKind;
+import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.SpanData;
+import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.hbase.ConnectionRule;
-import org.apache.hadoop.hbase.HBaseClassTestRule;
-import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HRegionLocation;
import org.apache.hadoop.hbase.MatcherPredicate;
-import org.apache.hadoop.hbase.MiniClusterRule;
import org.apache.hadoop.hbase.RegionLocations;
-import org.apache.hadoop.hbase.StartMiniClusterOption;
import org.apache.hadoop.hbase.TableName;
-import org.apache.hadoop.hbase.Waiter;
import org.apache.hadoop.hbase.client.RegionReplicaTestHelper.Locator;
import org.apache.hadoop.hbase.client.trace.StringTraceRenderer;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.testclassification.ClientTests;
import org.apache.hadoop.hbase.testclassification.MediumTests;
-import org.apache.hadoop.hbase.trace.OpenTelemetryClassRule;
-import org.apache.hadoop.hbase.trace.OpenTelemetryTestRule;
import org.apache.hadoop.hbase.trace.TraceUtil;
import org.hamcrest.Matcher;
-import org.junit.ClassRule;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-import org.junit.experimental.runners.Enclosed;
-import org.junit.rules.ExternalResource;
-import org.junit.rules.RuleChain;
-import org.junit.rules.TestName;
-import org.junit.rules.TestRule;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.jupiter.params.provider.Arguments;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-@RunWith(Enclosed.class)
+import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
+
+@Tag(MediumTests.TAG)
+@Tag(ClientTests.TAG)
+@HBaseParameterizedTestTemplate(name = "[{index}]: registry = {0}")
public class TestAsyncMetaRegionLocator {
- private static final Logger logger =
LoggerFactory.getLogger(TestAsyncMetaRegionLocator.class);
- private static final class Setup extends ExternalResource {
+ private static final Logger LOG =
LoggerFactory.getLogger(TestAsyncMetaRegionLocator.class);
- private final MiniClusterRule miniClusterRule;
- private final ConnectionRule connectionRule;
+ private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
- private boolean initialized = false;
- private HBaseTestingUtility testUtil;
- private AsyncMetaRegionLocator locator;
- private ConnectionRegistry registry;
+ @RegisterExtension
+ private static final OpenTelemetryExtension OTEL_EXT =
OpenTelemetryExtension.create();
- public Setup(final ConnectionRule connectionRule, final MiniClusterRule
miniClusterRule) {
- this.connectionRule = connectionRule;
- this.miniClusterRule = miniClusterRule;
- }
+ private Class<? extends ConnectionRegistry> registryClass;
- public HBaseTestingUtility getTestingUtility() {
- assertInitialized();
- return testUtil;
- }
+ private ConnectionRegistry registry;
- public AsyncMetaRegionLocator getLocator() {
- assertInitialized();
- return locator;
- }
+ private AsyncMetaRegionLocator locator;
- private void assertInitialized() {
- if (!initialized) {
- throw new IllegalStateException("before method has not been called.");
- }
- }
+ public TestAsyncMetaRegionLocator(Class<? extends ConnectionRegistry>
registryClass) {
+ this.registryClass = registryClass;
+ }
- @Override
- protected void before() throws Throwable {
- final AsyncAdmin admin = connectionRule.getAsyncConnection().getAdmin();
- testUtil = miniClusterRule.getTestingUtility();
- HBaseTestingUtility.setReplicas(admin, TableName.META_TABLE_NAME, 3);
- testUtil.waitUntilNoRegionsInTransition();
- registry = ConnectionRegistryFactory.create(testUtil.getConfiguration(),
User.getCurrent());
- RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(testUtil,
registry);
- admin.balancerSwitch(false).get();
- locator = new AsyncMetaRegionLocator(registry);
- initialized = true;
- }
+ @SuppressWarnings("deprecation")
+ public static Stream<Arguments> parameters() {
+ return Stream.of(Arguments.of(RpcConnectionRegistry.class),
Arguments.of(MasterRegistry.class),
+ Arguments.of(ZKConnectionRegistry.class));
+ }
- @Override
- protected void after() {
- registry.close();
+ @BeforeAll
+ public static void setUpBeforeAll() throws Exception {
+ UTIL.startMiniCluster(3);
+ HBaseTestingUtility.setReplicas(UTIL.getAdmin(),
TableName.META_TABLE_NAME, 3);
+ UTIL.waitUntilNoRegionsInTransition();
+ try (ConnectionRegistry registry =
+ ConnectionRegistryFactory.create(UTIL.getConfiguration(),
User.getCurrent())) {
+ RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(UTIL, registry);
}
+ UTIL.getAdmin().balancerSwitch(false, true);
}
- public static abstract class AbstractBase {
- private final OpenTelemetryClassRule otelClassRule =
OpenTelemetryClassRule.create();
- private final MiniClusterRule miniClusterRule;
- private final Setup setup;
-
- protected Matcher<SpanData> parentSpanMatcher;
- protected List<SpanData> spans;
- protected Matcher<SpanData> registryGetMetaRegionLocationsMatcher;
-
- @Rule
- public final TestRule classRule;
-
- @Rule
- public final OpenTelemetryTestRule otelTestRule = new
OpenTelemetryTestRule(otelClassRule);
-
- @Rule
- public TestName testName = new TestName();
-
- public AbstractBase() {
- miniClusterRule = MiniClusterRule.newBuilder()
-
.setMiniClusterOption(StartMiniClusterOption.builder().numWorkers(3).build())
- .setConfiguration(() -> {
- final Configuration conf = HBaseConfiguration.create();
- conf.setClass(HConstants.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY,
- getConnectionRegistryClass(), ConnectionRegistry.class);
- return conf;
- }).build();
- final ConnectionRule connectionRule =
-
ConnectionRule.createAsyncConnectionRule(miniClusterRule::createAsyncConnection);
- setup = new Setup(connectionRule, miniClusterRule);
- classRule =
RuleChain.outerRule(otelClassRule).around(miniClusterRule).around(connectionRule)
- .around(setup);
- }
+ @AfterAll
+ public static void tearDownAfterAll() throws IOException {
+ UTIL.shutdownMiniCluster();
+ }
- protected abstract Class<? extends ConnectionRegistry>
getConnectionRegistryClass();
-
- @Test
- public void test() throws Exception {
- final AsyncMetaRegionLocator locator = setup.getLocator();
- final HBaseTestingUtility testUtil = setup.getTestingUtility();
-
- TraceUtil.trace(() -> {
- try {
- testLocator(miniClusterRule.getTestingUtility(),
TableName.META_TABLE_NAME,
- new Locator() {
- @Override
- public void updateCachedLocationOnError(HRegionLocation loc,
Throwable error) {
- locator.updateCachedLocationOnError(loc, error);
- }
-
- @Override
- public RegionLocations getRegionLocations(TableName tableName,
int replicaId,
- boolean reload) throws Exception {
- return locator.getRegionLocations(replicaId, reload).get();
- }
- });
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }, testName.getMethodName());
-
- final Configuration conf = testUtil.getConfiguration();
- parentSpanMatcher = allOf(hasName(testName.getMethodName()), hasEnded());
- Waiter.waitFor(conf, TimeUnit.SECONDS.toMillis(5),
- new MatcherPredicate<>(otelClassRule::getSpans,
hasItem(parentSpanMatcher)));
- spans = otelClassRule.getSpans();
- if (logger.isDebugEnabled()) {
- StringTraceRenderer renderer = new StringTraceRenderer(spans);
- renderer.render(logger::debug);
- }
- assertThat(spans, hasItem(parentSpanMatcher));
- final SpanData parentSpan =
spans.stream().filter(parentSpanMatcher::matches).findAny()
- .orElseThrow(AssertionError::new);
-
- registryGetMetaRegionLocationsMatcher =
- allOf(hasName(endsWith("ConnectionRegistry.getMetaRegionLocations")),
- hasParentSpanId(parentSpan), hasKind(SpanKind.INTERNAL), hasEnded());
- assertThat(spans, hasItem(registryGetMetaRegionLocationsMatcher));
- }
+ @BeforeEach
+ public void setUp() throws IOException {
+ Configuration conf = new Configuration(UTIL.getConfiguration());
+ conf.setClass(HConstants.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY,
registryClass,
+ ConnectionRegistry.class);
+ registry = ConnectionRegistryFactory.create(conf, User.getCurrent());
+ locator = new AsyncMetaRegionLocator(registry);
}
- /**
- * Test covers when client is configured with {@link ZKConnectionRegistry}.
- */
- @Category({ MediumTests.class, ClientTests.class })
- public static class TestZKConnectionRegistry extends AbstractBase {
- @ClassRule
- public static final HBaseClassTestRule CLASS_RULE =
- HBaseClassTestRule.forClass(TestZKConnectionRegistry.class);
-
- @Override
- protected Class<? extends ConnectionRegistry> getConnectionRegistryClass()
{
- return ZKConnectionRegistry.class;
- }
+ @AfterEach
+ public void tearDown() throws IOException {
+ Closeables.close(registry, true);
}
- /**
- * Test covers when client is configured with {@link RpcConnectionRegistry}.
- */
- @Category({ MediumTests.class, ClientTests.class })
- public static class TestRpcConnectionRegistry extends AbstractBase {
- @ClassRule
- public static final HBaseClassTestRule CLASS_RULE =
- HBaseClassTestRule.forClass(TestRpcConnectionRegistry.class);
-
- @Override
- protected Class<? extends ConnectionRegistry> getConnectionRegistryClass()
{
- return RpcConnectionRegistry.class;
- }
+ @TestTemplate
+ public void test(TestInfo testInfo) {
+ String methodName = testInfo.getTestMethod().get().getName();
+ TraceUtil.trace(() -> {
+ try {
+ testLocator(UTIL, TableName.META_TABLE_NAME, new Locator() {
+ @Override
+ public void updateCachedLocationOnError(HRegionLocation loc,
Throwable error) {
+ locator.updateCachedLocationOnError(loc, error);
+ }
+
+ @Override
+ public RegionLocations getRegionLocations(TableName tableName, int
replicaId,
+ boolean reload) throws Exception {
+ return locator.getRegionLocations(replicaId, reload).get();
+ }
+ });
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }, methodName);
- @Test
- @Override
- public void test() throws Exception {
- super.test();
- final SpanData registry_getMetaRegionLocationsSpan =
-
spans.stream().filter(registryGetMetaRegionLocationsMatcher::matches).findAny()
+ Matcher<SpanData> parentSpanMatcher = allOf(hasName(methodName),
hasEnded());
+
+ UTIL.waitFor(TimeUnit.SECONDS.toMillis(5),
+ new MatcherPredicate<>(OTEL_EXT::getSpans, hasItem(parentSpanMatcher)));
+ List<SpanData> spans = OTEL_EXT.getSpans();
+ if (LOG.isDebugEnabled()) {
+ StringTraceRenderer renderer = new StringTraceRenderer(spans);
+ renderer.render(LOG::debug);
+ }
+ assertThat(spans, hasItem(parentSpanMatcher));
+ final SpanData parentSpan =
+
spans.stream().filter(parentSpanMatcher::matches).findAny().orElseThrow(AssertionError::new);
+
+ Matcher<SpanData> registryGetMetaRegionLocationsMatcher =
+ allOf(hasName(endsWith(registryClass.getSimpleName() +
".getMetaRegionLocations")),
+ hasParentSpanId(parentSpan), hasKind(SpanKind.INTERNAL), hasEnded());
+ assertThat(spans, hasItem(registryGetMetaRegionLocationsMatcher));
+
+ // RpcConnectionRegistry specific tracing assertions
+ if (registry instanceof RpcConnectionRegistry) {
+ SpanData registryGetMetaRegionLocationsSpan =
+
spans.stream().filter(registryGetMetaRegionLocationsMatcher::matches).findFirst()
.orElseThrow(AssertionError::new);
- final Matcher<SpanData> clientGetMetaRegionLocationsMatcher = allOf(
- hasName(endsWith("ClientMetaService/GetMetaRegionLocations")),
- hasParentSpanId(registry_getMetaRegionLocationsSpan),
hasKind(SpanKind.CLIENT), hasEnded());
+ // we need to fetch cluster id at the first time
+ Matcher<SpanData> fetchClusterIdMatcher = allOf(
+ hasName(endsWith("ConnectionRegistryService/GetConnectionRegistry")),
+ hasParentSpanId(registryGetMetaRegionLocationsSpan),
hasKind(SpanKind.CLIENT), hasEnded());
+ assertThat(spans, hasItem(fetchClusterIdMatcher));
+ SpanData fetchClusterIdSpan =
spans.stream().filter(fetchClusterIdMatcher::matches)
+ .findFirst().orElseThrow(AssertionError::new);
+ Matcher<SpanData> clientGetMetaRegionLocationsMatcher =
+ allOf(hasName(endsWith("ClientMetaService/GetMetaRegionLocations")),
+ hasParentSpanId(fetchClusterIdSpan), hasKind(SpanKind.CLIENT),
hasEnded());
assertThat(spans, hasItem(clientGetMetaRegionLocationsMatcher));
}
}