View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Tests that we fail fast when hostname resolution is not working and do not cache
49   * unresolved InetSocketAddresses.
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      // verify that we can get an instance with the cluster hostname
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      // test that we fail to get a client to an unresolvable hostname, which
80      // 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");
86    }
87  
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();
92      try {
93        conn.getClient(rs);
94      } catch (UnknownHostException uhe) {
95        fail("Obtaining client to the cluster regionserver should have succeeded");
96      }
97  
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,
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     // Create a table with region replicas
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       // Get locations of the regions of the table
124       List<HRegionLocation> locations = con.locateRegions(tableName, false, false);
125 
126       // The size of the returned locations should be 3
127       assertEquals(regionReplication, locations.size());
128 
129       // The replicaIds of the returned locations should be 0, 1 and 2
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 }