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 java.io.IOException;
021import java.util.List;
022import org.apache.hadoop.hbase.HRegionLocation;
023import org.apache.hadoop.hbase.RegionLocations;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.testclassification.ClientTests;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.util.Pair;
028import org.junit.jupiter.api.AfterAll;
029import org.junit.jupiter.api.BeforeAll;
030import org.junit.jupiter.api.Tag;
031
032@Tag(MediumTests.TAG)
033@Tag(ClientTests.TAG)
034public class TestRegionLocator extends AbstractTestRegionLocator {
035
036  @BeforeAll
037  public static void setUp() throws Exception {
038    startClusterAndCreateTable();
039  }
040
041  @AfterAll
042  public static void tearDown() throws Exception {
043    UTIL.shutdownMiniCluster();
044  }
045
046  @Override
047  protected byte[][] getStartKeys(TableName tableName) throws IOException {
048    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
049      return locator.getStartKeys();
050    }
051  }
052
053  @Override
054  protected byte[][] getEndKeys(TableName tableName) throws IOException {
055    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
056      return locator.getEndKeys();
057    }
058  }
059
060  @Override
061  protected Pair<byte[][], byte[][]> getStartEndKeys(TableName tableName) throws IOException {
062    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
063      return locator.getStartEndKeys();
064    }
065  }
066
067  @Override
068  protected HRegionLocation getRegionLocation(TableName tableName, byte[] row, int replicaId)
069    throws IOException {
070    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
071      return locator.getRegionLocation(row, replicaId);
072    }
073  }
074
075  @Override
076  protected List<HRegionLocation> getRegionLocations(TableName tableName, byte[] row)
077    throws IOException {
078    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
079      return locator.getRegionLocations(row);
080    }
081  }
082
083  @Override
084  protected List<HRegionLocation> getAllRegionLocations(TableName tableName) throws IOException {
085    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
086      return locator.getAllRegionLocations();
087    }
088  }
089
090  @Override
091  protected List<HRegionLocation> getRegionLocationsPage(TableName tableName, byte[] startKey,
092    int limit) throws IOException {
093    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
094      return locator.getRegionLocationsPage(startKey, limit);
095    }
096  }
097
098  @Override
099  protected RegionLocations getCachedLocation(TableName tableName, byte[] startKey)
100    throws IOException {
101    return ((ConnectionOverAsyncConnection) UTIL.getConnection()).getAsyncConnection().getLocator()
102      .getNonMetaRegionLocator().getCachedLocation(tableName, startKey);
103  }
104
105  @Override
106  protected void clearCache(TableName tableName) throws IOException {
107    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
108      locator.clearRegionLocationCache();
109    }
110  }
111}