|
41 | 41 | import java.util.ArrayList; |
42 | 42 | import java.util.List; |
43 | 43 | import java.util.function.Consumer; |
| 44 | +import java.util.stream.Stream; |
44 | 45 | import org.apache.hadoop.conf.Configuration; |
45 | 46 | import org.apache.hadoop.hbase.CompareOperator; |
46 | 47 | import org.apache.hadoop.hbase.HBaseConfiguration; |
|
59 | 60 | import org.apache.hadoop.hbase.client.Put; |
60 | 61 | import org.apache.hadoop.hbase.client.Result; |
61 | 62 | import org.apache.hadoop.hbase.client.ResultScanner; |
| 63 | +import org.apache.hadoop.hbase.client.Row; |
62 | 64 | import org.apache.hadoop.hbase.client.RowMutations; |
63 | 65 | import org.apache.hadoop.hbase.client.Scan; |
64 | 66 | import org.apache.hadoop.hbase.client.Table; |
|
71 | 73 | import org.junit.jupiter.api.Test; |
72 | 74 | import org.junit.jupiter.api.TestInstance; |
73 | 75 | import org.junit.jupiter.api.extension.RegisterExtension; |
| 76 | +import org.junit.jupiter.params.ParameterizedTest; |
| 77 | +import org.junit.jupiter.params.provider.Arguments; |
| 78 | +import org.junit.jupiter.params.provider.MethodSource; |
74 | 79 | import org.testcontainers.containers.GenericContainer; |
75 | 80 | import org.testcontainers.containers.wait.strategy.Wait; |
76 | 81 | import org.testcontainers.containers.wait.strategy.WaitAllStrategy; |
@@ -101,7 +106,6 @@ public abstract class AbstractHbaseTest { |
101 | 106 | private static final String ROW_2 = "row2"; |
102 | 107 | private static final String ROW_3 = "row3"; |
103 | 108 | private static final String ROW_4 = "row4"; |
104 | | - private static final String ROW_5 = "row5"; |
105 | 109 | private static final String SCAN_ROW = "scan-row"; |
106 | 110 |
|
107 | 111 | @RegisterExtension final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); |
@@ -354,47 +358,53 @@ void testScan() throws IOException { |
354 | 358 | testing().waitAndAssertTraces(traceAssertConsumer(TABLE_NAME, SCAN, REGION_SERVER_PORT, true)); |
355 | 359 | } |
356 | 360 |
|
357 | | - @Test |
358 | | - void testBatchGet() throws IOException { |
359 | | - Result[] results; |
| 361 | + @ParameterizedTest |
| 362 | + @MethodSource("batchScenarios") |
| 363 | + void testBatch(BatchScenario scenario) throws Exception { |
360 | 364 | try (Table table = connection.getTable(TABLE_NAME)) { |
361 | | - List<Get> getList = new ArrayList<>(); |
362 | | - getList.add(new Get(Bytes.toBytes(ROW_1))); |
363 | | - getList.add(new Get(Bytes.toBytes(ROW_2))); |
364 | | - getList.add(new Get(Bytes.toBytes(ROW_5))); |
365 | | - results = table.get(getList); |
366 | | - } |
367 | | - assertThat(results).hasSize(3); |
368 | | - { |
369 | | - assertThat(Bytes.toString(results[0].getRow())).isEqualTo(ROW_1); |
370 | | - assertThat(value(results[0], "col1")).isEqualTo("col1_val_1"); |
371 | | - assertThat(value(results[0], "col2")).isEqualTo("col2_val_1"); |
| 365 | + table.batch(scenario.actions, new Object[scenario.actions.size()]); |
372 | 366 | } |
373 | | - { |
374 | | - assertThat(Bytes.toString(results[1].getRow())).isNull(); |
375 | | - assertThat(value(results[1], "col1")).isNull(); |
376 | | - assertThat(value(results[1], "col2")).isNull(); |
377 | | - } |
378 | | - { |
379 | | - assertThat(Bytes.toString(results[2].getRow())).isNull(); |
380 | | - assertThat(value(results[2], "col1")).isNull(); |
381 | | - assertThat(value(results[2], "col2")).isNull(); |
| 367 | + |
| 368 | + if (scenario.actions.isEmpty()) { |
| 369 | + assertThat(testing().spans()).isEmpty(); |
| 370 | + return; |
382 | 371 | } |
383 | | - testing().waitAndAssertTraces(traceAssertConsumer(TABLE_NAME, MULTI, REGION_SERVER_PORT, true)); |
| 372 | + |
| 373 | + testing() |
| 374 | + .waitAndAssertTraces( |
| 375 | + traceAssertConsumer(TABLE_NAME, scenario.operationName, REGION_SERVER_PORT, true)); |
384 | 376 | } |
385 | 377 |
|
386 | | - @Test |
387 | | - void testBatchPut() throws IOException { |
388 | | - try (Table table = connection.getTable(TABLE_NAME)) { |
389 | | - List<Put> putList = new ArrayList<>(); |
390 | | - for (int i = 2; i < 5; i++) { |
391 | | - Put put = new Put(Bytes.toBytes("batch-put-row" + i)); |
392 | | - put.addColumn(COLUMN_FAMILY, Bytes.toBytes("col1"), Bytes.toBytes("col1_val_" + i)); |
393 | | - putList.add(put); |
394 | | - } |
395 | | - table.put(putList); |
396 | | - } |
397 | | - testing().waitAndAssertTraces(traceAssertConsumer(TABLE_NAME, MULTI, REGION_SERVER_PORT, true)); |
| 378 | + private static Stream<Arguments> batchScenarios() { |
| 379 | + return Stream.of( |
| 380 | + // an empty batch produces no span |
| 381 | + Arguments.argumentSet("empty", BatchScenario.builder().build()), |
| 382 | + Arguments.argumentSet( |
| 383 | + "single", BatchScenario.builder().addAction(get(ROW_1)).operationName(MULTI).build()), |
| 384 | + Arguments.argumentSet( |
| 385 | + "twoSameOperation", |
| 386 | + BatchScenario.builder() |
| 387 | + .addAction(get(ROW_1)) |
| 388 | + .addAction(get(ROW_2)) |
| 389 | + .operationName(MULTI) |
| 390 | + .build()), |
| 391 | + Arguments.argumentSet( |
| 392 | + "twoDifferentOperations", |
| 393 | + BatchScenario.builder() |
| 394 | + .addAction(put("batch-matrix-put-row")) |
| 395 | + .addAction(get(ROW_1)) |
| 396 | + .operationName(MULTI) |
| 397 | + .build())); |
| 398 | + } |
| 399 | + |
| 400 | + private static Get get(String rowKey) { |
| 401 | + return new Get(Bytes.toBytes(rowKey)); |
| 402 | + } |
| 403 | + |
| 404 | + private static Put put(String rowKey) { |
| 405 | + Put put = new Put(Bytes.toBytes(rowKey)); |
| 406 | + put.addColumn(COLUMN_FAMILY, Bytes.toBytes("col1"), Bytes.toBytes("col1_val")); |
| 407 | + return put; |
398 | 408 | } |
399 | 409 |
|
400 | 410 | @Test |
@@ -569,4 +579,37 @@ private static String dbCollectionName(TableName table, boolean hasTable) { |
569 | 579 | } |
570 | 580 | return null; |
571 | 581 | } |
| 582 | + |
| 583 | + private static final class BatchScenario { |
| 584 | + final List<Row> actions; |
| 585 | + final String operationName; |
| 586 | + |
| 587 | + BatchScenario(Builder builder) { |
| 588 | + this.actions = builder.actions; |
| 589 | + this.operationName = builder.operationName; |
| 590 | + } |
| 591 | + |
| 592 | + static Builder builder() { |
| 593 | + return new Builder(); |
| 594 | + } |
| 595 | + |
| 596 | + static final class Builder { |
| 597 | + private final List<Row> actions = new ArrayList<>(); |
| 598 | + private String operationName; |
| 599 | + |
| 600 | + Builder operationName(String operationName) { |
| 601 | + this.operationName = operationName; |
| 602 | + return this; |
| 603 | + } |
| 604 | + |
| 605 | + Builder addAction(Row action) { |
| 606 | + this.actions.add(action); |
| 607 | + return this; |
| 608 | + } |
| 609 | + |
| 610 | + BatchScenario build() { |
| 611 | + return new BatchScenario(this); |
| 612 | + } |
| 613 | + } |
| 614 | + } |
572 | 615 | } |
0 commit comments