Skip to content

Commit b231dd6

Browse files
authored
HBASE-25731 - TestConnectionImplementation BadHostname tests fail in branch-1 (#3120)
Signed-off-by Reid Chan <reidchan@apache.org>
1 parent 7851df7 commit b231dd6

File tree

1 file changed

+62
-26
lines changed

1 file changed

+62
-26
lines changed

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

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,29 @@
2020

2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertTrue;
23-
import static org.junit.Assert.fail;
2423

24+
import com.google.protobuf.ServiceException;
2525
import java.io.IOException;
2626
import java.net.UnknownHostException;
2727
import java.util.Arrays;
2828
import java.util.HashSet;
2929
import java.util.List;
3030
import java.util.Set;
31-
3231
import org.apache.hadoop.hbase.HBaseTestingUtility;
3332
import org.apache.hadoop.hbase.HColumnDescriptor;
3433
import org.apache.hadoop.hbase.HConstants;
3534
import org.apache.hadoop.hbase.HRegionLocation;
3635
import org.apache.hadoop.hbase.HTableDescriptor;
36+
import org.apache.hadoop.hbase.NotServingRegionException;
3737
import org.apache.hadoop.hbase.ServerName;
3838
import org.apache.hadoop.hbase.TableName;
39+
import org.apache.hadoop.hbase.ipc.HBaseRpcController;
40+
import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
41+
import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
42+
import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
3943
import org.apache.hadoop.hbase.testclassification.ClientTests;
4044
import org.apache.hadoop.hbase.testclassification.MediumTests;
45+
import org.apache.hadoop.hbase.util.ByteStringer;
4146
import org.apache.hadoop.hbase.util.Bytes;
4247
import org.junit.AfterClass;
4348
import org.junit.BeforeClass;
@@ -52,12 +57,16 @@
5257
public class TestConnectionImplementation {
5358
private static HBaseTestingUtility testUtil;
5459
private static ConnectionManager.HConnectionImplementation conn;
60+
private static HBaseProtos.RegionSpecifier specifier;
5561

5662
@BeforeClass
5763
public static void setupBeforeClass() throws Exception {
5864
testUtil = HBaseTestingUtility.createLocalHTU();
5965
testUtil.startMiniCluster();
6066
conn = (ConnectionManager.HConnectionImplementation) testUtil.getConnection();
67+
specifier = HBaseProtos.RegionSpecifier.
68+
newBuilder().setValue(ByteStringer.wrap(Bytes.toBytes("region")))
69+
.setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.REGION_NAME).build();
6170
}
6271

6372
@AfterClass
@@ -66,42 +75,69 @@ public static void teardownAfterClass() throws Exception {
6675
testUtil.shutdownMiniCluster();
6776
}
6877

69-
@Test(expected = UnknownHostException.class)
78+
@Test
7079
public void testGetAdminBadHostname() throws Exception {
7180
// verify that we can get an instance with the cluster hostname
7281
ServerName master = testUtil.getHBaseCluster().getMaster().getServerName();
73-
try {
74-
conn.getAdmin(master);
75-
} catch (UnknownHostException uhe) {
76-
fail("Obtaining admin to the cluster master should have succeeded");
77-
}
82+
HBaseRpcController controller = conn.getRpcControllerFactory().newController();
83+
84+
AdminProtos.GetRegionInfoRequest request =
85+
AdminProtos.GetRegionInfoRequest.newBuilder().setRegion(specifier).build();
86+
AdminProtos.AdminService.BlockingInterface goodAdmin = conn.getAdmin(master);
87+
verifyAdminCall(goodAdmin, controller, request, false);
7888

7989
// test that we fail to get a client to an unresolvable hostname, which
8090
// means it won't be cached
81-
ServerName badHost =
82-
ServerName.valueOf("unknownhost.invalid:" + HConstants.DEFAULT_MASTER_PORT,
83-
System.currentTimeMillis());
84-
conn.getAdmin(badHost);
85-
fail("Obtaining admin to unresolvable hostname should have failed");
91+
ServerName badHost = ServerName
92+
.valueOf("unknownhost.invalid:" + HConstants.DEFAULT_MASTER_PORT, System.currentTimeMillis());
93+
AdminProtos.AdminService.BlockingInterface badAdmin = conn.getAdmin(badHost);
94+
verifyAdminCall(badAdmin, controller, request, true);
8695
}
8796

88-
@Test(expected = UnknownHostException.class)
89-
public void testGetClientBadHostname() throws Exception {
90-
// verify that we can get an instance with the cluster hostname
91-
ServerName rs = testUtil.getHBaseCluster().getRegionServer(0).getServerName();
97+
private void verifyAdminCall(AdminProtos.AdminService.BlockingInterface admin,
98+
HBaseRpcController rpcController, AdminProtos.GetRegionInfoRequest request,
99+
boolean shouldHaveHostException) {
100+
92101
try {
93-
conn.getClient(rs);
94-
} catch (UnknownHostException uhe) {
95-
fail("Obtaining client to the cluster regionserver should have succeeded");
102+
admin.getRegionInfo(rpcController, request);
103+
} catch (ServiceException se) {
104+
assertEquals(shouldHaveHostException, se.getCause() instanceof UnknownHostException);
105+
} catch (Exception e) {
106+
assertEquals(!shouldHaveHostException, e instanceof NotServingRegionException);
96107
}
108+
}
97109

98-
// test that we fail to get a client to an unresolvable hostname, which
99-
// means it won't be cached
100-
ServerName badHost =
101-
ServerName.valueOf("unknownhost.invalid:" + HConstants.DEFAULT_REGIONSERVER_PORT,
110+
@Test
111+
public void testGetClientBadHostname()
112+
throws Exception {
113+
// verify that we can get an instance with the cluster hostname
114+
ServerName rs = testUtil.getHBaseCluster().getRegionServer(0).getServerName();
115+
HBaseRpcController controller = conn.getRpcControllerFactory().newController();
116+
ClientProtos.Get get = ClientProtos.Get.newBuilder()
117+
.setRow(ByteStringer.wrap(Bytes.toBytes("r"))).build();
118+
ClientProtos.GetRequest request = ClientProtos.GetRequest.newBuilder().setGet(get)
119+
.setRegion(specifier).build();
120+
121+
ClientProtos.ClientService.BlockingInterface goodClient = conn.getClient(rs);
122+
verifyClientCall(goodClient, controller, request, false);
123+
124+
ServerName badHost = ServerName
125+
.valueOf("unknownhost.invalid:" + HConstants.DEFAULT_REGIONSERVER_PORT,
102126
System.currentTimeMillis());
103-
conn.getAdmin(badHost);
104-
fail("Obtaining client to unresolvable hostname should have failed");
127+
ClientProtos.ClientService.BlockingInterface badClient = conn.getClient(badHost);
128+
verifyClientCall(badClient, controller, request, true);
129+
}
130+
131+
private void verifyClientCall(ClientProtos.ClientService.BlockingInterface client,
132+
HBaseRpcController rpcController, ClientProtos.GetRequest request,
133+
boolean shouldHaveHostException) {
134+
try {
135+
client.get(rpcController, request);
136+
} catch (ServiceException se) {
137+
assertEquals(shouldHaveHostException, se.getCause() instanceof UnknownHostException);
138+
} catch (Exception e) {
139+
assertEquals(!shouldHaveHostException, e instanceof NotServingRegionException);
140+
}
105141
}
106142

107143
@Test

0 commit comments

Comments
 (0)