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.HRegionLocation; 025import org.apache.hadoop.hbase.RegionLocations; 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.jupiter.api.AfterAll; 031import org.junit.jupiter.api.BeforeAll; 032import org.junit.jupiter.api.Tag; 033 034import org.apache.hbase.thirdparty.com.google.common.io.Closeables; 035 036@Tag(MediumTests.TAG) 037@Tag(ClientTests.TAG) 038public class TestAsyncTableRegionLocator extends AbstractTestRegionLocator { 039 040 private static AsyncConnection CONN; 041 042 @BeforeAll 043 public static void setUp() throws Exception { 044 startClusterAndCreateTable(); 045 CONN = ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get(); 046 } 047 048 @AfterAll 049 public static void tearDown() throws Exception { 050 Closeables.close(CONN, true); 051 UTIL.shutdownMiniCluster(); 052 } 053 054 @Override 055 protected byte[][] getStartKeys(TableName tableName) throws IOException { 056 return get(CONN.getRegionLocator(tableName).getStartKeys()).toArray(new byte[0][]); 057 } 058 059 @Override 060 protected byte[][] getEndKeys(TableName tableName) throws IOException { 061 return get(CONN.getRegionLocator(tableName).getEndKeys()).toArray(new byte[0][]); 062 } 063 064 @Override 065 protected Pair<byte[][], byte[][]> getStartEndKeys(TableName tableName) throws IOException { 066 List<Pair<byte[], byte[]>> startEndKeys = 067 get(CONN.getRegionLocator(tableName).getStartEndKeys()); 068 byte[][] startKeys = new byte[startEndKeys.size()][]; 069 byte[][] endKeys = new byte[startEndKeys.size()][]; 070 for (int i = 0, n = startEndKeys.size(); i < n; i++) { 071 Pair<byte[], byte[]> pair = startEndKeys.get(i); 072 startKeys[i] = pair.getFirst(); 073 endKeys[i] = pair.getSecond(); 074 } 075 return Pair.newPair(startKeys, endKeys); 076 } 077 078 @Override 079 protected HRegionLocation getRegionLocation(TableName tableName, byte[] row, int replicaId) 080 throws IOException { 081 return get(CONN.getRegionLocator(tableName).getRegionLocation(row, replicaId)); 082 } 083 084 @Override 085 protected List<HRegionLocation> getRegionLocations(TableName tableName, byte[] row) 086 throws IOException { 087 return get(CONN.getRegionLocator(tableName).getRegionLocations(row)); 088 } 089 090 @Override 091 protected List<HRegionLocation> getAllRegionLocations(TableName tableName) throws IOException { 092 return get(CONN.getRegionLocator(tableName).getAllRegionLocations()); 093 } 094 095 @Override 096 protected List<HRegionLocation> getRegionLocationsPage(TableName tableName, byte[] startKey, 097 int limit) throws IOException { 098 return get(CONN.getRegionLocator(tableName).getRegionLocationsPage(startKey, limit)); 099 } 100 101 @Override 102 protected RegionLocations getCachedLocation(TableName tableName, byte[] startKey) { 103 return ((AsyncConnectionImpl) CONN).getLocator().getNonMetaRegionLocator() 104 .getCachedLocation(tableName, startKey); 105 } 106 107 @Override 108 protected void clearCache(TableName tableName) throws IOException { 109 CONN.getRegionLocator(tableName).clearRegionLocationCache(); 110 } 111}