View Javadoc

1   /**
2   *
3   * Licensed to the Apache Software Foundation (ASF) under one
4   * or more contributor license agreements.  See the NOTICE file
5   * distributed with this work for additional information
6   * regarding copyright ownership.  The ASF licenses this file
7   * to you under the Apache License, Version 2.0 (the
8   * "License"); you may not use this file except in compliance
9   * with the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19  package org.apache.hadoop.hbase.client;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.NavigableMap;
25  import java.util.Map.Entry;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HRegionInfo;
29  import org.apache.hadoop.hbase.HRegionLocation;
30  import org.apache.hadoop.hbase.RegionLocations;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.classification.InterfaceStability;
35  import org.apache.hadoop.hbase.util.Pair;
36  
37  import com.google.common.annotations.VisibleForTesting;
38  
39  /**
40   * An implementation of {@link RegionLocator}. Used to view region location information for a single
41   * HBase table. Lightweight. Get as needed and just close when done. Instances of this class SHOULD
42   * NOT be constructed directly. Obtain an instance via {@link Connection}. See
43   * {@link ConnectionFactory} class comment for an example of how.
44   *
45   * <p> This class is thread safe
46   */
47  @InterfaceAudience.Private
48  @InterfaceStability.Stable
49  public class HRegionLocator implements RegionLocator {
50  
51    private final TableName tableName;
52    private final ClusterConnection connection;
53  
54    public HRegionLocator(TableName tableName, ClusterConnection connection) {
55      this.connection = connection;
56      this.tableName = tableName;
57    }
58  
59    /**
60     * {@inheritDoc}
61     */
62    @Override
63    public void close() throws IOException {
64      // This method is required by the RegionLocator interface. This implementation does not have any
65      // persistent state, so there is no need to do anything here.
66    }
67  
68    /**
69     * {@inheritDoc}
70     */
71    @Override
72    public HRegionLocation getRegionLocation(final byte [] row)
73    throws IOException {
74      return connection.getRegionLocation(tableName, row, false);
75    }
76  
77    /**
78     * {@inheritDoc}
79     */
80    @Override
81    public HRegionLocation getRegionLocation(final byte [] row, boolean reload)
82    throws IOException {
83      return connection.getRegionLocation(tableName, row, reload);
84    }
85  
86    @Override
87    public List<HRegionLocation> getAllRegionLocations() throws IOException {
88      NavigableMap<HRegionInfo, ServerName> locations =
89          MetaScanner.allTableRegions(this.connection, getName());
90      ArrayList<HRegionLocation> regions = new ArrayList<>(locations.size());
91      for (Entry<HRegionInfo, ServerName> entry : locations.entrySet()) {
92        regions.add(new HRegionLocation(entry.getKey(), entry.getValue()));
93      }
94      return regions;
95    }
96  
97    /**
98     * {@inheritDoc}
99     */
100   @Override
101   public byte[][] getStartKeys() throws IOException {
102     return getStartEndKeys().getFirst();
103   }
104 
105   /**
106    * {@inheritDoc}
107    */
108   @Override
109   public byte[][] getEndKeys() throws IOException {
110     return getStartEndKeys().getSecond();
111   }
112 
113   /**
114    * {@inheritDoc}
115    */
116   @Override
117   public Pair<byte[][], byte[][]> getStartEndKeys() throws IOException {
118     return getStartEndKeys(listRegionLocations());
119   }
120 
121   @VisibleForTesting
122   Pair<byte[][], byte[][]> getStartEndKeys(List<RegionLocations> regions) {
123     final byte[][] startKeyList = new byte[regions.size()][];
124     final byte[][] endKeyList = new byte[regions.size()][];
125 
126     for (int i = 0; i < regions.size(); i++) {
127       HRegionInfo region = regions.get(i).getRegionLocation().getRegionInfo();
128       startKeyList[i] = region.getStartKey();
129       endKeyList[i] = region.getEndKey();
130     }
131 
132     return new Pair<>(startKeyList, endKeyList);
133   }
134 
135   @Override
136   public TableName getName() {
137     return this.tableName;
138   }
139 
140   @VisibleForTesting
141   List<RegionLocations> listRegionLocations() throws IOException {
142     return MetaScanner.listTableRegionLocations(getConfiguration(), this.connection, getName());
143   }
144 
145   public Configuration getConfiguration() {
146     return connection.getConfiguration();
147   }
148 }