001/**
002*
003* Licensed to the Apache Software Foundation (ASF) under one
004* or more contributor license agreements.  See the NOTICE file
005* distributed with this work for additional information
006* regarding copyright ownership.  The ASF licenses this file
007* to you under the Apache License, Version 2.0 (the
008* "License"); you may not use this file except in compliance
009* with the License.  You may obtain a copy of the License at
010*
011*     http://www.apache.org/licenses/LICENSE-2.0
012*
013* Unless required by applicable law or agreed to in writing, software
014* distributed under the License is distributed on an "AS IS" BASIS,
015* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016* See the License for the specific language governing permissions and
017* limitations under the License.
018*/
019package org.apache.hadoop.hbase.client;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HRegionInfo;
027import org.apache.hadoop.hbase.HRegionLocation;
028import org.apache.hadoop.hbase.MetaTableAccessor;
029import org.apache.hadoop.hbase.RegionLocations;
030import org.apache.hadoop.hbase.ServerName;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.util.Pair;
033import org.apache.yetus.audience.InterfaceAudience;
034import org.apache.yetus.audience.InterfaceStability;
035
036import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
037
038/**
039 * An implementation of {@link RegionLocator}. Used to view region location information for a single
040 * HBase table. Lightweight. Get as needed and just close when done. Instances of this class SHOULD
041 * NOT be constructed directly. Obtain an instance via {@link Connection}. See
042 * {@link ConnectionFactory} class comment for an example of how.
043 *
044 * <p> This class is thread safe
045 */
046@InterfaceAudience.Private
047@InterfaceStability.Stable
048public class HRegionLocator implements RegionLocator {
049
050  private final TableName tableName;
051  private final ClusterConnection connection;
052
053  public HRegionLocator(TableName tableName, ClusterConnection connection) {
054    this.connection = connection;
055    this.tableName = tableName;
056  }
057
058  /**
059   * {@inheritDoc}
060   */
061  @Override
062  public void close() throws IOException {
063    // This method is required by the RegionLocator interface. This implementation does not have any
064    // persistent state, so there is no need to do anything here.
065  }
066
067  /**
068   * {@inheritDoc}
069   */
070  @Override
071  public HRegionLocation getRegionLocation(final byte [] row)
072  throws IOException {
073    return connection.getRegionLocation(tableName, row, false);
074  }
075
076  /**
077   * {@inheritDoc}
078   */
079  @Override
080  public HRegionLocation getRegionLocation(final byte [] row, boolean reload)
081  throws IOException {
082    return connection.getRegionLocation(tableName, row, reload);
083  }
084
085  @Override
086  public List<HRegionLocation> getAllRegionLocations() throws IOException {
087    TableName tableName = getName();
088    List<Pair<RegionInfo, ServerName>> locations =
089        MetaTableAccessor.getTableRegionsAndLocations(this.connection, tableName);
090    ArrayList<HRegionLocation> regions = new ArrayList<>(locations.size());
091    for (Pair<RegionInfo, ServerName> entry : locations) {
092      regions.add(new HRegionLocation(entry.getFirst(), entry.getSecond()));
093
094    }
095    if (regions.size() > 0) {
096      connection.cacheLocation(tableName, new RegionLocations(regions));
097    }
098    return regions;
099  }
100
101  /**
102   * {@inheritDoc}
103   */
104  @Override
105  public byte[][] getStartKeys() throws IOException {
106    return getStartEndKeys().getFirst();
107  }
108
109  /**
110   * {@inheritDoc}
111   */
112  @Override
113  public byte[][] getEndKeys() throws IOException {
114    return getStartEndKeys().getSecond();
115  }
116
117  /**
118   * {@inheritDoc}
119   */
120  @Override
121  public Pair<byte[][], byte[][]> getStartEndKeys() throws IOException {
122    return getStartEndKeys(listRegionLocations());
123  }
124
125  @VisibleForTesting
126  Pair<byte[][], byte[][]> getStartEndKeys(List<RegionLocations> regions) {
127    final byte[][] startKeyList = new byte[regions.size()][];
128    final byte[][] endKeyList = new byte[regions.size()][];
129
130    for (int i = 0; i < regions.size(); i++) {
131      HRegionInfo region = regions.get(i).getRegionLocation().getRegionInfo();
132      startKeyList[i] = region.getStartKey();
133      endKeyList[i] = region.getEndKey();
134    }
135
136    return new Pair<>(startKeyList, endKeyList);
137  }
138
139  @Override
140  public TableName getName() {
141    return this.tableName;
142  }
143
144  @VisibleForTesting
145  List<RegionLocations> listRegionLocations() throws IOException {
146    final List<RegionLocations> regions = new ArrayList<>();
147    MetaTableAccessor.Visitor visitor = new MetaTableAccessor.TableVisitorBase(tableName) {
148      @Override
149      public boolean visitInternal(Result result) throws IOException {
150        RegionLocations locations = MetaTableAccessor.getRegionLocations(result);
151        if (locations == null) return true;
152        regions.add(locations);
153        return true;
154      }
155    };
156    MetaTableAccessor.scanMetaForTableRegions(connection, visitor, tableName);
157    return regions;
158  }
159
160  public Configuration getConfiguration() {
161    return connection.getConfiguration();
162  }
163}