20
20
21
21
import static org .junit .Assert .assertEquals ;
22
22
import static org .junit .Assert .assertTrue ;
23
- import static org .junit .Assert .fail ;
24
23
24
+ import com .google .protobuf .ServiceException ;
25
25
import java .io .IOException ;
26
26
import java .net .UnknownHostException ;
27
27
import java .util .Arrays ;
28
28
import java .util .HashSet ;
29
29
import java .util .List ;
30
30
import java .util .Set ;
31
-
32
31
import org .apache .hadoop .hbase .HBaseTestingUtility ;
33
32
import org .apache .hadoop .hbase .HColumnDescriptor ;
34
33
import org .apache .hadoop .hbase .HConstants ;
35
34
import org .apache .hadoop .hbase .HRegionLocation ;
36
35
import org .apache .hadoop .hbase .HTableDescriptor ;
36
+ import org .apache .hadoop .hbase .NotServingRegionException ;
37
37
import org .apache .hadoop .hbase .ServerName ;
38
38
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 ;
39
43
import org .apache .hadoop .hbase .testclassification .ClientTests ;
40
44
import org .apache .hadoop .hbase .testclassification .MediumTests ;
45
+ import org .apache .hadoop .hbase .util .ByteStringer ;
41
46
import org .apache .hadoop .hbase .util .Bytes ;
42
47
import org .junit .AfterClass ;
43
48
import org .junit .BeforeClass ;
52
57
public class TestConnectionImplementation {
53
58
private static HBaseTestingUtility testUtil ;
54
59
private static ConnectionManager .HConnectionImplementation conn ;
60
+ private static HBaseProtos .RegionSpecifier specifier ;
55
61
56
62
@ BeforeClass
57
63
public static void setupBeforeClass () throws Exception {
58
64
testUtil = HBaseTestingUtility .createLocalHTU ();
59
65
testUtil .startMiniCluster ();
60
66
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 ();
61
70
}
62
71
63
72
@ AfterClass
@@ -66,42 +75,69 @@ public static void teardownAfterClass() throws Exception {
66
75
testUtil .shutdownMiniCluster ();
67
76
}
68
77
69
- @ Test ( expected = UnknownHostException . class )
78
+ @ Test
70
79
public void testGetAdminBadHostname () throws Exception {
71
80
// verify that we can get an instance with the cluster hostname
72
81
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 );
78
88
79
89
// test that we fail to get a client to an unresolvable hostname, which
80
90
// 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 );
86
95
}
87
96
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
+
92
101
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 );
96
107
}
108
+ }
97
109
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 ,
102
126
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
+ }
105
141
}
106
142
107
143
@ Test
0 commit comments