001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client; 019 020import static org.apache.hadoop.hbase.util.FutureUtils.get; 021 022import java.io.IOException; 023import java.util.List; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HRegionLocation; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.testclassification.ClientTests; 028import org.apache.hadoop.hbase.testclassification.MediumTests; 029import org.apache.hadoop.hbase.util.Pair; 030import org.junit.AfterClass; 031import org.junit.BeforeClass; 032import org.junit.ClassRule; 033import org.junit.experimental.categories.Category; 034 035import org.apache.hbase.thirdparty.com.google.common.io.Closeables; 036 037@Category({ MediumTests.class, ClientTests.class }) 038public class TestAsyncTableRegionLocator extends AbstractTestRegionLocator { 039 040 @ClassRule 041 public static final HBaseClassTestRule CLASS_RULE = 042 HBaseClassTestRule.forClass(TestAsyncTableRegionLocator.class); 043 044 private static AsyncConnection CONN; 045 046 @BeforeClass 047 public static void setUp() throws Exception { 048 startClusterAndCreateTable(); 049 CONN = ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get(); 050 } 051 052 @AfterClass 053 public static void tearDown() throws Exception { 054 Closeables.close(CONN, true); 055 UTIL.shutdownMiniCluster(); 056 } 057 058 @Override 059 protected byte[][] getStartKeys(TableName tableName) throws IOException { 060 return get(CONN.getRegionLocator(tableName).getStartKeys()).toArray(new byte[0][]); 061 } 062 063 @Override 064 protected byte[][] getEndKeys(TableName tableName) throws IOException { 065 return get(CONN.getRegionLocator(tableName).getEndKeys()).toArray(new byte[0][]); 066 } 067 068 @Override 069 protected Pair<byte[][], byte[][]> getStartEndKeys(TableName tableName) throws IOException { 070 List<Pair<byte[], byte[]>> startEndKeys = 071 get(CONN.getRegionLocator(tableName).getStartEndKeys()); 072 byte[][] startKeys = new byte[startEndKeys.size()][]; 073 byte[][] endKeys = new byte[startEndKeys.size()][]; 074 for (int i = 0, n = startEndKeys.size(); i < n; i++) { 075 Pair<byte[], byte[]> pair = startEndKeys.get(i); 076 startKeys[i] = pair.getFirst(); 077 endKeys[i] = pair.getSecond(); 078 } 079 return Pair.newPair(startKeys, endKeys); 080 } 081 082 @Override 083 protected HRegionLocation getRegionLocation(TableName tableName, byte[] row, int replicaId) 084 throws IOException { 085 return get(CONN.getRegionLocator(tableName).getRegionLocation(row, replicaId)); 086 } 087 088 @Override 089 protected List<HRegionLocation> getRegionLocations(TableName tableName, byte[] row) 090 throws IOException { 091 return get(CONN.getRegionLocator(tableName).getRegionLocations(row)); 092 } 093 094 @Override 095 protected List<HRegionLocation> getAllRegionLocations(TableName tableName) throws IOException { 096 return get(CONN.getRegionLocator(tableName).getAllRegionLocations()); 097 } 098 099 @Override 100 protected void clearCache(TableName tableName) throws IOException { 101 CONN.getRegionLocator(tableName).clearRegionLocationCache(); 102 } 103}