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  package org.apache.hadoop.hbase.client;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.MediumTests;
25  import org.apache.hadoop.hbase.ServerName;
26  import org.apache.hadoop.hbase.regionserver.HRegionServer;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.AfterClass;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  import org.mockito.Mockito;
33  
34  @Category(MediumTests.class)
35  public class TestCoprocessorHConnection {
36  
37    private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
38  
39    @BeforeClass
40    public static void setupCluster() throws Exception {
41      UTIL.startMiniCluster();
42    }
43  
44    @AfterClass
45    public static void shutdownCluster() throws Exception {
46      UTIL.shutdownMiniCluster();
47    }
48  
49    /**
50     * Ensure that if the HRegion we are looking up isn't on this server (and not in the cache), that
51     * we still correctly look it up.
52     * @throws Exception on failure
53     */
54    @Test
55    public void testNonServerLocalLookup() throws Exception {
56      Configuration conf = UTIL.getConfiguration();
57      // make a fake server that should never be called
58      HRegionServer server = Mockito.mock(HRegionServer.class);
59      ServerName name = new ServerName("not.a.server.hostname", 12345, -1L);
60      Mockito.when(server.getServerName()).thenReturn(name);
61      CoprocessorHConnection connection = new CoprocessorHConnection(conf, server);
62  
63      // make sure we get the mock server when doing a direct lookup
64      assertEquals("Didn't get the mock server from the connection", server,
65        connection.getHRegionConnection(name.getHostname(), name.getPort()));
66  
67      // create a table that exists
68      byte[] tableName = Bytes.toBytes("testNonServerLocalLookup");
69      byte[] family = Bytes.toBytes("family");
70      UTIL.createTable(tableName, family);
71  
72      // if we can write to the table correctly, then our connection is doing the right thing
73      HTable table = new HTable(tableName, connection);
74      Put p = new Put(Bytes.toBytes("row"));
75      p.add(family, null, null);
76      table.put(p);
77      table.flushCommits();
78  
79  
80      // cleanup
81      table.close();
82      connection.close();
83    }
84  
85    @Test
86    public void testLocalServerLookup() throws Exception {
87      Configuration conf = UTIL.getConfiguration();
88      // get a real rs
89      HRegionServer server =
90          UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().get(0).getRegionServer();
91      // fake the connection to look like we are living on that server
92      CoprocessorHConnection connection = new CoprocessorHConnection(conf, server);
93  
94      // create a table that exists
95      byte[] tableName = Bytes.toBytes("testLocalServerLookup");
96      byte[] family = Bytes.toBytes("family");
97      UTIL.createTable(tableName, family);
98  
99      // if we can write to the table correctly, then our connection is doing the right thing
100     HTable table = new HTable(tableName, connection);
101     Put p = new Put(Bytes.toBytes("row"));
102     p.add(family, null, null);
103     table.put(p);
104     table.flushCommits();
105 
106     //make sure we get the actual server when doing a direct lookup
107     ServerName name = server.getServerName();
108     assertEquals("Didn't get the expected server from the connection", server,
109       connection.getHRegionConnection(name.getHostname(), name.getPort()));
110 
111     // cleanup
112     table.close();
113     connection.close();
114   }
115 }