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