|
22 | 22 | import static org.junit.Assert.assertNull; |
23 | 23 | import static org.junit.Assert.assertTrue; |
24 | 24 | import static org.junit.Assert.fail; |
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 27 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 28 | +import static org.mockito.ArgumentMatchers.eq; |
25 | 29 | import static org.mockito.Mockito.mock; |
26 | 30 | import static org.mockito.Mockito.when; |
27 | 31 |
|
|
42 | 46 | import org.apache.hadoop.hbase.HConstants; |
43 | 47 | import org.apache.hadoop.hbase.HRegionLocation; |
44 | 48 | import org.apache.hadoop.hbase.HTableDescriptor; |
| 49 | +import org.apache.hadoop.hbase.RegionLocations; |
45 | 50 | import org.apache.hadoop.hbase.ReplicationPeerNotFoundException; |
| 51 | +import org.apache.hadoop.hbase.TableDescriptors; |
46 | 52 | import org.apache.hadoop.hbase.TableName; |
47 | 53 | import org.apache.hadoop.hbase.Waiter; |
48 | 54 | import org.apache.hadoop.hbase.client.Admin; |
@@ -557,6 +563,59 @@ public void testMetaCacheMissTriggersRefresh() throws Exception { |
557 | 563 | } |
558 | 564 | } |
559 | 565 |
|
| 566 | + @Test |
| 567 | + public void testNullTableDescriptorDoesNotCauseNPE() throws Exception { |
| 568 | + TableName tableName = TableName.valueOf(name.getMethodName()); |
| 569 | + int regionReplication = 2; |
| 570 | + HTableDescriptor htd = HTU.createTableDescriptor(tableName); |
| 571 | + htd.setRegionReplication(regionReplication); |
| 572 | + createOrEnableTableWithRetries(htd, true); |
| 573 | + |
| 574 | + Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration()); |
| 575 | + Table table = connection.getTable(tableName); |
| 576 | + |
| 577 | + try { |
| 578 | + HTU.loadNumericRows(table, HBaseTestingUtility.fam1, 0, 100); |
| 579 | + |
| 580 | + RegionLocator rl = connection.getRegionLocator(tableName); |
| 581 | + HRegionLocation hrl = rl.getRegionLocation(HConstants.EMPTY_BYTE_ARRAY); |
| 582 | + byte[] encodedRegionName = hrl.getRegionInfo().getEncodedNameAsBytes(); |
| 583 | + rl.close(); |
| 584 | + |
| 585 | + AtomicLong skippedEdits = new AtomicLong(); |
| 586 | + RegionReplicaReplicationEndpoint.RegionReplicaOutputSink sink = |
| 587 | + mock(RegionReplicaReplicationEndpoint.RegionReplicaOutputSink.class); |
| 588 | + when(sink.getSkippedEditsCounter()).thenReturn(skippedEdits); |
| 589 | + |
| 590 | + TableDescriptors mockTableDescriptors = mock(TableDescriptors.class); |
| 591 | + when(mockTableDescriptors.get(tableName)).thenReturn(null); |
| 592 | + |
| 593 | + RegionLocations singleLocation = new RegionLocations(hrl); |
| 594 | + ClusterConnection mockConnection = mock(ClusterConnection.class); |
| 595 | + when(mockConnection.locateRegion(eq(tableName), any(byte[].class), anyBoolean(), anyBoolean(), |
| 596 | + anyInt())).thenReturn(singleLocation); |
| 597 | + when(mockConnection.getConfiguration()).thenReturn(HTU.getConfiguration()); |
| 598 | + |
| 599 | + RegionReplicaReplicationEndpoint.RegionReplicaSinkWriter sinkWriter = |
| 600 | + new RegionReplicaReplicationEndpoint.RegionReplicaSinkWriter(sink, mockConnection, |
| 601 | + Executors.newSingleThreadExecutor(), Integer.MAX_VALUE, mockTableDescriptors); |
| 602 | + |
| 603 | + Cell cell = CellBuilderFactory.create(CellBuilderType.DEEP_COPY) |
| 604 | + .setRow(Bytes.toBytes("testRow")).setFamily(HBaseTestingUtility.fam1) |
| 605 | + .setValue(Bytes.toBytes("testValue")).setType(Type.Put).build(); |
| 606 | + |
| 607 | + Entry entry = |
| 608 | + new Entry(new WALKeyImpl(encodedRegionName, tableName, 1), new WALEdit().add(cell)); |
| 609 | + |
| 610 | + sinkWriter.append(tableName, encodedRegionName, Bytes.toBytes("testRow"), |
| 611 | + Lists.newArrayList(entry)); |
| 612 | + |
| 613 | + } finally { |
| 614 | + table.close(); |
| 615 | + connection.close(); |
| 616 | + } |
| 617 | + } |
| 618 | + |
560 | 619 | @Test |
561 | 620 | public void testMetaCacheSkippedForSingleReplicaTable() throws Exception { |
562 | 621 | TableName tableName = TableName.valueOf(name.getMethodName()); |
|
0 commit comments