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      TableName tableName = getName();
89      NavigableMap<HRegionInfo, ServerName> locations =
90          MetaScanner.allTableRegions(this.connection, tableName);
91      ArrayList<HRegionLocation> regions = new ArrayList<>(locations.size());
92      for (Entry<HRegionInfo, ServerName> entry : locations.entrySet()) {
93        regions.add(new HRegionLocation(entry.getKey(), entry.getValue()));
94      }
95      if (regions.size() > 0) {
96        connection.cacheLocation(tableName, new RegionLocations(regions));
97      }
98      return regions;
99    }
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     return MetaScanner.listTableRegionLocations(getConfiguration(), this.connection, getName());
147   }
148 
149   public Configuration getConfiguration() {
150     return connection.getConfiguration();
151   }
152 }