1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import java.io.IOException;
26 import java.net.UnknownHostException;
27 import java.util.Arrays;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HColumnDescriptor;
34 import org.apache.hadoop.hbase.HConstants;
35 import org.apache.hadoop.hbase.HRegionLocation;
36 import org.apache.hadoop.hbase.HTableDescriptor;
37 import org.apache.hadoop.hbase.ServerName;
38 import org.apache.hadoop.hbase.TableName;
39 import org.apache.hadoop.hbase.testclassification.ClientTests;
40 import org.apache.hadoop.hbase.testclassification.MediumTests;
41 import org.apache.hadoop.hbase.util.Bytes;
42 import org.junit.AfterClass;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46
47
48
49
50
51 @Category({MediumTests.class, ClientTests.class})
52 public class TestConnectionImplementation {
53 private static HBaseTestingUtility testUtil;
54 private static ConnectionManager.HConnectionImplementation conn;
55
56 @BeforeClass
57 public static void setupBeforeClass() throws Exception {
58 testUtil = HBaseTestingUtility.createLocalHTU();
59 testUtil.startMiniCluster();
60 conn = (ConnectionManager.HConnectionImplementation) testUtil.getConnection();
61 }
62
63 @AfterClass
64 public static void teardownAfterClass() throws Exception {
65 conn.close();
66 testUtil.shutdownMiniCluster();
67 }
68
69 @Test(expected = UnknownHostException.class)
70 public void testGetAdminBadHostname() throws Exception {
71
72 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 }
78
79
80
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");
86 }
87
88 @Test(expected = UnknownHostException.class)
89 public void testGetClientBadHostname() throws Exception {
90
91 ServerName rs = testUtil.getHBaseCluster().getRegionServer(0).getServerName();
92 try {
93 conn.getClient(rs);
94 } catch (UnknownHostException uhe) {
95 fail("Obtaining client to the cluster regionserver should have succeeded");
96 }
97
98
99
100 ServerName badHost =
101 ServerName.valueOf("unknownhost.invalid:" + HConstants.DEFAULT_REGIONSERVER_PORT,
102 System.currentTimeMillis());
103 conn.getAdmin(badHost);
104 fail("Obtaining client to unresolvable hostname should have failed");
105 }
106
107 @Test
108 public void testLocateRegionsWithRegionReplicas() throws IOException {
109 int regionReplication = 3;
110 byte[] family = Bytes.toBytes("cf");
111 TableName tableName = TableName.valueOf("testLocateRegionsWithRegionReplicas");
112
113
114 HTableDescriptor desc = new HTableDescriptor(tableName);
115 desc.addFamily(new HColumnDescriptor(family));
116 desc.setRegionReplication(regionReplication);
117 testUtil.getConnection().getAdmin().createTable(desc);
118
119 try (ConnectionManager.HConnectionImplementation con =
120 (ConnectionManager.HConnectionImplementation) ConnectionFactory.
121 createConnection(testUtil.getConfiguration())) {
122
123
124 List<HRegionLocation> locations = con.locateRegions(tableName, false, false);
125
126
127 assertEquals(regionReplication, locations.size());
128
129
130 Set<Integer> expectedReplicaIds = new HashSet<>(Arrays.asList(0, 1, 2));
131 for (HRegionLocation location : locations) {
132 assertTrue(expectedReplicaIds.remove(location.getRegionInfo().getReplicaId()));
133 }
134 } finally {
135 testUtil.deleteTable(tableName);
136 }
137 }
138 }