Skip to content

Commit 82eb6d3

Browse files
committed
HBASE-30089 Rewrite AbstractTestAsyncTableScan and related sub classes
1 parent b0ea092 commit 82eb6d3

File tree

5 files changed

+143
-197
lines changed

5 files changed

+143
-197
lines changed

hbase-server/src/test/java/org/apache/hadoop/hbase/client/AbstractTestAsyncTableScan.java

Lines changed: 74 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
import static org.hamcrest.Matchers.hasProperty;
2828
import static org.hamcrest.Matchers.is;
2929
import static org.hamcrest.Matchers.isA;
30-
import static org.junit.Assert.assertEquals;
31-
import static org.junit.Assert.assertThrows;
32-
import static org.junit.Assert.fail;
30+
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertThrows;
32+
import static org.junit.jupiter.api.Assertions.fail;
3333

34+
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
3435
import io.opentelemetry.sdk.trace.data.SpanData;
3536
import java.io.IOException;
3637
import java.io.UncheckedIOException;
@@ -44,82 +45,66 @@
4445
import java.util.stream.Collectors;
4546
import java.util.stream.IntStream;
4647
import java.util.stream.Stream;
47-
import org.apache.hadoop.conf.Configuration;
48-
import org.apache.hadoop.hbase.ConnectionRule;
4948
import org.apache.hadoop.hbase.HBaseTestingUtil;
5049
import org.apache.hadoop.hbase.MatcherPredicate;
51-
import org.apache.hadoop.hbase.MiniClusterRule;
52-
import org.apache.hadoop.hbase.StartTestingClusterOption;
5350
import org.apache.hadoop.hbase.TableName;
54-
import org.apache.hadoop.hbase.Waiter;
5551
import org.apache.hadoop.hbase.ipc.RemoteWithExtrasException;
5652
import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
5753
import org.apache.hadoop.hbase.trace.HBaseSemanticAttributes;
58-
import org.apache.hadoop.hbase.trace.OpenTelemetryClassRule;
59-
import org.apache.hadoop.hbase.trace.OpenTelemetryTestRule;
6054
import org.apache.hadoop.hbase.trace.TraceUtil;
6155
import org.apache.hadoop.hbase.util.Bytes;
6256
import org.apache.hadoop.hbase.util.JVMClusterUtil;
6357
import org.apache.hadoop.hbase.util.Pair;
6458
import org.hamcrest.Matcher;
65-
import org.junit.ClassRule;
66-
import org.junit.Rule;
67-
import org.junit.Test;
68-
import org.junit.rules.ExternalResource;
69-
import org.junit.rules.RuleChain;
70-
import org.junit.rules.TestName;
71-
import org.junit.rules.TestRule;
59+
import org.junit.jupiter.api.AfterAll;
60+
import org.junit.jupiter.api.BeforeAll;
61+
import org.junit.jupiter.api.BeforeEach;
62+
import org.junit.jupiter.api.TestInfo;
63+
import org.junit.jupiter.api.TestTemplate;
64+
import org.junit.jupiter.api.extension.RegisterExtension;
65+
import org.junit.jupiter.params.provider.Arguments;
7266

73-
public abstract class AbstractTestAsyncTableScan {
67+
import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
7468

75-
protected static final OpenTelemetryClassRule OTEL_CLASS_RULE = OpenTelemetryClassRule.create();
69+
public abstract class AbstractTestAsyncTableScan {
7670

77-
private static Configuration createConfiguration() {
78-
Configuration conf = new Configuration();
79-
// Disable directory sharing to prevent race conditions when tests run in parallel.
80-
// Each test instance gets its own isolated directories to avoid one test's tearDown()
81-
// deleting directories another parallel test is still using.
82-
conf.setBoolean("hbase.test.disable-directory-sharing", true);
83-
return conf;
84-
}
71+
@RegisterExtension
72+
protected static final OpenTelemetryExtension OTEL_EXT = OpenTelemetryExtension.create();
8573

86-
protected static final MiniClusterRule MINI_CLUSTER_RULE =
87-
MiniClusterRule.newBuilder().setConfiguration(createConfiguration())
88-
.setMiniClusterOption(StartTestingClusterOption.builder().numWorkers(3).build()).build();
74+
protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
8975

90-
protected static final ConnectionRule CONN_RULE =
91-
ConnectionRule.createAsyncConnectionRule(MINI_CLUSTER_RULE::createAsyncConnection);
76+
protected static AsyncConnection CONN;
9277

93-
private static final class Setup extends ExternalResource {
94-
@Override
95-
protected void before() throws Throwable {
96-
final HBaseTestingUtil testingUtil = MINI_CLUSTER_RULE.getTestingUtility();
97-
final AsyncConnection conn = CONN_RULE.getAsyncConnection();
78+
protected String methodName;
9879

99-
byte[][] splitKeys = new byte[8][];
100-
for (int i = 111; i < 999; i += 111) {
101-
splitKeys[i / 111 - 1] = Bytes.toBytes(String.format("%03d", i));
102-
}
103-
testingUtil.createTable(TABLE_NAME, FAMILY, splitKeys);
104-
testingUtil.waitTableAvailable(TABLE_NAME);
105-
conn.getTable(TABLE_NAME)
106-
.putAll(IntStream.range(0, COUNT)
107-
.mapToObj(i -> new Put(Bytes.toBytes(String.format("%03d", i)))
108-
.addColumn(FAMILY, CQ1, Bytes.toBytes(i)).addColumn(FAMILY, CQ2, Bytes.toBytes(i * i)))
109-
.collect(Collectors.toList()))
110-
.get();
80+
@BeforeAll
81+
public static void setUpBeforeClass() throws Exception {
82+
UTIL.startMiniCluster(3);
83+
byte[][] splitKeys = new byte[8][];
84+
for (int i = 111; i < 999; i += 111) {
85+
splitKeys[i / 111 - 1] = Bytes.toBytes(String.format("%03d", i));
11186
}
87+
UTIL.createTable(TABLE_NAME, FAMILY, splitKeys);
88+
UTIL.waitTableAvailable(TABLE_NAME);
89+
try (Table table = UTIL.getConnection().getTable(TABLE_NAME)) {
90+
table.put(IntStream.range(0, COUNT)
91+
.mapToObj(i -> new Put(Bytes.toBytes(String.format("%03d", i)))
92+
.addColumn(FAMILY, CQ1, Bytes.toBytes(i)).addColumn(FAMILY, CQ2, Bytes.toBytes(i * i)))
93+
.collect(Collectors.toList()));
94+
}
95+
CONN = ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get();
11296
}
11397

114-
@ClassRule
115-
public static final TestRule classRule = RuleChain.outerRule(OTEL_CLASS_RULE)
116-
.around(MINI_CLUSTER_RULE).around(CONN_RULE).around(new Setup());
117-
118-
@Rule
119-
public final OpenTelemetryTestRule otelTestRule = new OpenTelemetryTestRule(OTEL_CLASS_RULE);
98+
@AfterAll
99+
public static void tearDownAfterClass() throws Exception {
100+
Closeables.close(CONN, true);
101+
UTIL.shutdownMiniCluster();
102+
}
120103

121-
@Rule
122-
public final TestName testName = new TestName();
104+
@BeforeEach
105+
public void setUp(TestInfo testInfo) {
106+
methodName = testInfo.getTestMethod().get().getName();
107+
}
123108

124109
protected static TableName TABLE_NAME = TableName.valueOf("async");
125110

@@ -149,11 +134,11 @@ private static Scan createBatchSmallResultSizeScan() {
149134
}
150135

151136
private static AsyncTable<?> getRawTable() {
152-
return CONN_RULE.getAsyncConnection().getTable(TABLE_NAME);
137+
return CONN.getTable(TABLE_NAME);
153138
}
154139

155140
private static AsyncTable<?> getTable() {
156-
return CONN_RULE.getAsyncConnection().getTable(TABLE_NAME, ForkJoinPool.commonPool());
141+
return CONN.getTable(TABLE_NAME, ForkJoinPool.commonPool());
157142
}
158143

159144
private static List<Pair<String, Supplier<Scan>>> getScanCreator() {
@@ -164,23 +149,20 @@ private static List<Pair<String, Supplier<Scan>>> getScanCreator() {
164149
AbstractTestAsyncTableScan::createBatchSmallResultSizeScan));
165150
}
166151

167-
protected static List<Object[]> getScanCreatorParams() {
168-
return getScanCreator().stream().map(p -> new Object[] { p.getFirst(), p.getSecond() })
169-
.collect(Collectors.toList());
152+
protected static Stream<Arguments> getScanCreatorParams() {
153+
return getScanCreator().stream().map(p -> Arguments.of(p.getFirst(), p.getSecond()));
170154
}
171155

172156
private static List<Pair<String, Supplier<AsyncTable<?>>>> getTableCreator() {
173157
return Arrays.asList(Pair.newPair("raw", AbstractTestAsyncTableScan::getRawTable),
174158
Pair.newPair("normal", AbstractTestAsyncTableScan::getTable));
175159
}
176160

177-
protected static List<Object[]> getTableAndScanCreatorParams() {
161+
protected static Stream<Arguments> getTableAndScanCreatorParams() {
178162
List<Pair<String, Supplier<AsyncTable<?>>>> tableCreator = getTableCreator();
179163
List<Pair<String, Supplier<Scan>>> scanCreator = getScanCreator();
180-
return tableCreator.stream()
181-
.flatMap(tp -> scanCreator.stream()
182-
.map(sp -> new Object[] { tp.getFirst(), tp.getSecond(), sp.getFirst(), sp.getSecond() }))
183-
.collect(Collectors.toList());
164+
return tableCreator.stream().flatMap(tp -> scanCreator.stream()
165+
.map(sp -> Arguments.of(tp.getFirst(), tp.getSecond(), sp.getFirst(), sp.getSecond())));
184166
}
185167

186168
protected abstract Scan createScan();
@@ -211,25 +193,22 @@ protected final List<Result> convertFromBatchResult(List<Result> results) {
211193
}
212194

213195
protected static void waitForSpan(final Matcher<SpanData> parentSpanMatcher) {
214-
final Configuration conf = MINI_CLUSTER_RULE.getTestingUtility().getConfiguration();
215-
Waiter.waitFor(conf, TimeUnit.SECONDS.toMillis(5), new MatcherPredicate<>(
216-
"Span for test failed to complete.", OTEL_CLASS_RULE::getSpans, hasItem(parentSpanMatcher)));
196+
UTIL.waitFor(TimeUnit.SECONDS.toMillis(5), new MatcherPredicate<>(
197+
"Span for test failed to complete.", OTEL_EXT::getSpans, hasItem(parentSpanMatcher)));
217198
}
218199

219200
protected static Stream<SpanData> spanStream() {
220-
return OTEL_CLASS_RULE.getSpans().stream().filter(Objects::nonNull);
201+
return OTEL_EXT.getSpans().stream().filter(Objects::nonNull);
221202
}
222203

223-
@Test
204+
@TestTemplate
224205
public void testScanAll() throws Exception {
225206
List<Result> results = doScan(createScan(), -1);
226207
// make sure all scanners are closed at RS side
227-
MINI_CLUSTER_RULE.getTestingUtility().getHBaseCluster().getRegionServerThreads().stream()
208+
UTIL.getHBaseCluster().getRegionServerThreads().stream()
228209
.map(JVMClusterUtil.RegionServerThread::getRegionServer).forEach(
229-
rs -> assertEquals(
230-
"The scanner count of " + rs.getServerName() + " is "
231-
+ rs.getRSRpcServices().getScannersCount(),
232-
0, rs.getRSRpcServices().getScannersCount()));
210+
rs -> assertEquals(0, rs.getRSRpcServices().getScannersCount(), "The scanner count of "
211+
+ rs.getServerName() + " is " + rs.getRSRpcServices().getScannersCount()));
233212
assertEquals(COUNT, results.size());
234213
IntStream.range(0, COUNT).forEach(i -> {
235214
Result result = results.get(i);
@@ -244,43 +223,41 @@ private void assertResultEquals(Result result, int i) {
244223
assertEquals(i * i, Bytes.toInt(result.getValue(FAMILY, CQ2)));
245224
}
246225

247-
@Test
226+
@TestTemplate
248227
public void testReversedScanAll() throws Exception {
249228
List<Result> results =
250-
TraceUtil.trace(() -> doScan(createScan().setReversed(true), -1), testName.getMethodName());
229+
TraceUtil.trace(() -> doScan(createScan().setReversed(true), -1), methodName);
251230
assertEquals(COUNT, results.size());
252231
IntStream.range(0, COUNT).forEach(i -> assertResultEquals(results.get(i), COUNT - i - 1));
253232
assertTraceContinuity();
254233
}
255234

256-
@Test
235+
@TestTemplate
257236
public void testScanNoStopKey() throws Exception {
258237
int start = 345;
259238
List<Result> results = TraceUtil.trace(
260239
() -> doScan(createScan().withStartRow(Bytes.toBytes(String.format("%03d", start))), -1),
261-
testName.getMethodName());
240+
methodName);
262241
assertEquals(COUNT - start, results.size());
263242
IntStream.range(0, COUNT - start).forEach(i -> assertResultEquals(results.get(i), start + i));
264243
assertTraceContinuity();
265244
}
266245

267-
@Test
246+
@TestTemplate
268247
public void testReverseScanNoStopKey() throws Exception {
269248
int start = 765;
270249
final Scan scan =
271250
createScan().withStartRow(Bytes.toBytes(String.format("%03d", start))).setReversed(true);
272-
List<Result> results = TraceUtil.trace(() -> doScan(scan, -1), testName.getMethodName());
251+
List<Result> results = TraceUtil.trace(() -> doScan(scan, -1), methodName);
273252
assertEquals(start + 1, results.size());
274253
IntStream.range(0, start + 1).forEach(i -> assertResultEquals(results.get(i), start - i));
275254
assertTraceContinuity();
276255
}
277256

278-
@Test
257+
@TestTemplate
279258
public void testScanWrongColumnFamily() {
280-
final Exception e = assertThrows(Exception.class,
281-
() -> TraceUtil.trace(
282-
() -> doScan(createScan().addFamily(Bytes.toBytes("WrongColumnFamily")), -1),
283-
testName.getMethodName()));
259+
final Exception e = assertThrows(Exception.class, () -> TraceUtil.trace(
260+
() -> doScan(createScan().addFamily(Bytes.toBytes("WrongColumnFamily")), -1), methodName));
284261
// hamcrest generic enforcement for `anyOf` is a pain; skip it
285262
// but -- don't we always unwrap ExecutionExceptions -- bug?
286263
if (e instanceof NoSuchColumnFamilyException) {
@@ -349,7 +326,7 @@ private void testReversedScan(int start, boolean startInclusive, int stop, boole
349326
IntStream.range(0, count).forEach(i -> assertResultEquals(results.get(i), actualStart - i));
350327
}
351328

352-
@Test
329+
@TestTemplate
353330
public void testScanWithStartKeyAndStopKey() throws Exception {
354331
testScan(1, true, 998, false, -1); // from first region to last region
355332
testScan(123, true, 345, true, -1);
@@ -358,7 +335,7 @@ public void testScanWithStartKeyAndStopKey() throws Exception {
358335
testScan(456, false, 678, false, -1);
359336
}
360337

361-
@Test
338+
@TestTemplate
362339
public void testReversedScanWithStartKeyAndStopKey() throws Exception {
363340
testReversedScan(998, true, 1, false, -1); // from last region to first region
364341
testReversedScan(543, true, 321, true, -1);
@@ -367,23 +344,23 @@ public void testReversedScanWithStartKeyAndStopKey() throws Exception {
367344
testReversedScan(876, false, 654, false, -1);
368345
}
369346

370-
@Test
347+
@TestTemplate
371348
public void testScanAtRegionBoundary() throws Exception {
372349
testScan(222, true, 333, true, -1);
373350
testScan(333, true, 444, false, -1);
374351
testScan(444, false, 555, true, -1);
375352
testScan(555, false, 666, false, -1);
376353
}
377354

378-
@Test
355+
@TestTemplate
379356
public void testReversedScanAtRegionBoundary() throws Exception {
380357
testReversedScan(333, true, 222, true, -1);
381358
testReversedScan(444, true, 333, false, -1);
382359
testReversedScan(555, false, 444, true, -1);
383360
testReversedScan(666, false, 555, false, -1);
384361
}
385362

386-
@Test
363+
@TestTemplate
387364
public void testScanWithLimit() throws Exception {
388365
testScan(1, true, 998, false, 900); // from first region to last region
389366
testScan(123, true, 234, true, 100);
@@ -392,7 +369,7 @@ public void testScanWithLimit() throws Exception {
392369
testScan(456, false, 678, false, 100);
393370
}
394371

395-
@Test
372+
@TestTemplate
396373
public void testScanWithLimitGreaterThanActualCount() throws Exception {
397374
testScan(1, true, 998, false, 1000); // from first region to last region
398375
testScan(123, true, 345, true, 200);
@@ -401,7 +378,7 @@ public void testScanWithLimitGreaterThanActualCount() throws Exception {
401378
testScan(456, false, 678, false, 200);
402379
}
403380

404-
@Test
381+
@TestTemplate
405382
public void testReversedScanWithLimit() throws Exception {
406383
testReversedScan(998, true, 1, false, 900); // from last region to first region
407384
testReversedScan(543, true, 321, true, 100);
@@ -410,7 +387,7 @@ public void testReversedScanWithLimit() throws Exception {
410387
testReversedScan(876, false, 654, false, 100);
411388
}
412389

413-
@Test
390+
@TestTemplate
414391
public void testReversedScanWithLimitGreaterThanActualCount() throws Exception {
415392
testReversedScan(998, true, 1, false, 1000); // from last region to first region
416393
testReversedScan(543, true, 321, true, 200);
@@ -419,7 +396,7 @@ public void testReversedScanWithLimitGreaterThanActualCount() throws Exception {
419396
testReversedScan(876, false, 654, false, 200);
420397
}
421398

422-
@Test
399+
@TestTemplate
423400
public void testScanEndingEarly() throws Exception {
424401
testScan(1, true, 998, false, 0, 900); // from first region to last region
425402
testScan(123, true, 234, true, 0, 100);

0 commit comments

Comments
 (0)