View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.client;
21  
22  import java.io.Closeable;
23  import java.io.IOException;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.ExecutorService;
27  
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.Abortable;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.HRegionLocation;
32  import org.apache.hadoop.hbase.HServerAddress;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.MasterNotRunningException;
35  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
36  import org.apache.hadoop.hbase.catalog.CatalogTracker;
37  import org.apache.hadoop.hbase.client.coprocessor.Batch;
38  import org.apache.hadoop.hbase.ipc.CoprocessorProtocol;
39  import org.apache.hadoop.hbase.ipc.HMasterInterface;
40  import org.apache.hadoop.hbase.ipc.HRegionInterface;
41  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
42  
43  /**
44   * Cluster connection.  Hosts a connection to the ZooKeeper ensemble and
45   * thereafter into the HBase cluster.  Knows how to locate regions out on the cluster,
46   * keeps a cache of locations and then knows how to recalibrate after they move.
47   * {@link HConnectionManager} manages instances of this class.
48   *
49   * <p>HConnections are used by {@link HTable} mostly but also by
50   * {@link HBaseAdmin}, {@link CatalogTracker},
51   * and {@link ZooKeeperWatcher}.  HConnection instances can be shared.  Sharing
52   * is usually what you want because rather than each HConnection instance
53   * having to do its own discovery of regions out on the cluster, instead, all
54   * clients get to share the one cache of locations.  Sharing makes cleanup of
55   * HConnections awkward.  See {@link HConnectionManager} for cleanup
56   * discussion.
57   *
58   * @see HConnectionManager
59   */
60  public interface HConnection extends Abortable, Closeable {
61    /**
62     * @return Configuration instance being used by this HConnection instance.
63     */
64    public Configuration getConfiguration();
65  
66    /**
67     * Retrieve ZooKeeperWatcher used by this connection.
68     * @return ZooKeeperWatcher handle being used by the connection.
69     * @throws IOException if a remote or network exception occurs
70     * @deprecated Removed because it was a mistake exposing zookeeper in this
71     * interface (ZooKeeper is an implementation detail).
72     * Deprecated in HBase 0.94
73     */
74    @Deprecated
75    public ZooKeeperWatcher getZooKeeperWatcher() throws IOException;
76  
77    /**
78     * @return proxy connection to master server for this instance
79     * @throws MasterNotRunningException if the master is not running
80     * @throws ZooKeeperConnectionException if unable to connect to zookeeper
81     * @deprecated Removed because it was a mistake exposing master in this
82     * interface (master is an implementation detail). Master functions are
83     * available from HConnection or HBaseAdmin, without having to use
84     * directly the master.
85     * Deprecated in HBase 0.94
86     */
87    @Deprecated
88    public HMasterInterface getMaster()
89    throws MasterNotRunningException, ZooKeeperConnectionException;
90  
91    /** @return - true if the master server is running */
92    public boolean isMasterRunning()
93    throws MasterNotRunningException, ZooKeeperConnectionException;
94  
95    /**
96     * A table that isTableEnabled == false and isTableDisabled == false
97     * is possible. This happens when a table has a lot of regions
98     * that must be processed.
99     * @param tableName table name
100    * @return true if the table is enabled, false otherwise
101    * @throws IOException if a remote or network exception occurs
102    */
103   public boolean isTableEnabled(byte[] tableName) throws IOException;
104 
105   /**
106    * @param tableName table name
107    * @return true if the table is disabled, false otherwise
108    * @throws IOException if a remote or network exception occurs
109    */
110   public boolean isTableDisabled(byte[] tableName) throws IOException;
111 
112   /**
113    * @param tableName table name
114    * @return true if all regions of the table are available, false otherwise
115    * @throws IOException if a remote or network exception occurs
116    */
117   public boolean isTableAvailable(byte[] tableName) throws IOException;
118 
119   /**
120    * List all the userspace tables.  In other words, scan the META table.
121    *
122    * If we wanted this to be really fast, we could implement a special
123    * catalog table that just contains table names and their descriptors.
124    * Right now, it only exists as part of the META table's region info.
125    *
126    * @return - returns an array of HTableDescriptors
127    * @throws IOException if a remote or network exception occurs
128    */
129   public HTableDescriptor[] listTables() throws IOException;
130 
131   /**
132    * @param tableName table name
133    * @return table metadata
134    * @throws IOException if a remote or network exception occurs
135    */
136   public HTableDescriptor getHTableDescriptor(byte[] tableName)
137   throws IOException;
138 
139   /**
140    * Find the location of the region of <i>tableName</i> that <i>row</i>
141    * lives in.
142    * @param tableName name of the table <i>row</i> is in
143    * @param row row key you're trying to find the region of
144    * @return HRegionLocation that describes where to find the region in
145    * question
146    * @throws IOException if a remote or network exception occurs
147    */
148   public HRegionLocation locateRegion(final byte [] tableName,
149       final byte [] row)
150   throws IOException;
151 
152   /**
153    * Allows flushing the region cache.
154    */
155   public void clearRegionCache();
156 
157   /**
158    * Allows flushing the region cache of all locations that pertain to
159    * <code>tableName</code>
160    * @param tableName Name of the table whose regions we are to remove from
161    * cache.
162    */
163   public void clearRegionCache(final byte [] tableName);
164 
165   /**
166    * Find the location of the region of <i>tableName</i> that <i>row</i>
167    * lives in, ignoring any value that might be in the cache.
168    * @param tableName name of the table <i>row</i> is in
169    * @param row row key you're trying to find the region of
170    * @return HRegionLocation that describes where to find the region in
171    * question
172    * @throws IOException if a remote or network exception occurs
173    */
174   public HRegionLocation relocateRegion(final byte [] tableName,
175       final byte [] row)
176   throws IOException;
177 
178   /**
179    * Gets the location of the region of <i>regionName</i>.
180    * @param regionName name of the region to locate
181    * @return HRegionLocation that describes where to find the region in
182    * question
183    * @throws IOException if a remote or network exception occurs
184    */
185   public HRegionLocation locateRegion(final byte [] regionName)
186   throws IOException;
187 
188   /**
189    * Gets the locations of all regions in the specified table, <i>tableName</i>.
190    * @param tableName table to get regions of
191    * @return list of region locations for all regions of table
192    * @throws IOException
193    */
194   public List<HRegionLocation> locateRegions(final byte[] tableName)
195       throws IOException;
196 
197   /**
198    * Gets the locations of all regions in the specified table, <i>tableName</i>.
199    * @param tableName table to get regions of
200    * @param useCache Should we use the cache to retrieve the region information.
201    * @param offlined True if we are to include offlined regions, false and we'll leave out offlined
202    *          regions from returned list.
203    * @return list of region locations for all regions of table
204    * @throws IOException
205    */
206   public List<HRegionLocation> locateRegions(final byte[] tableName, final boolean useCache,
207       final boolean offlined) throws IOException;
208 
209   /**
210    * Establishes a connection to the region server at the specified address.
211    * @param regionServer - the server to connect to
212    * @return proxy for HRegionServer
213    * @throws IOException if a remote or network exception occurs
214    * @deprecated Use {@link #getHRegionConnection(String, int)}
215    */
216   public HRegionInterface getHRegionConnection(HServerAddress regionServer)
217   throws IOException;
218 
219   /**
220    * Establishes a connection to the region server at the specified address.
221    * @param hostname RegionServer hostname
222    * @param port RegionServer port
223    * @return proxy for HRegionServer
224    * @throws IOException if a remote or network exception occurs
225    *
226    */
227   public HRegionInterface getHRegionConnection(final String hostname, final int port)
228   throws IOException;
229 
230   /**
231    * Establishes a connection to the region server at the specified address.
232    * @param regionServer - the server to connect to
233    * @param getMaster - do we check if master is alive
234    * @return proxy for HRegionServer
235    * @throws IOException if a remote or network exception occurs
236    * @deprecated Use {@link #getHRegionConnection(HServerAddress, boolean)}
237    */
238   public HRegionInterface getHRegionConnection(HServerAddress regionServer,
239      boolean getMaster)
240   throws IOException;
241 
242   /**
243    * Establishes a connection to the region server at the specified address.
244    * @param hostname RegionServer hostname
245    * @param port RegionServer port
246    * @param getMaster - do we check if master is alive
247    * @return proxy for HRegionServer
248    * @throws IOException if a remote or network exception occurs
249    */
250   public HRegionInterface getHRegionConnection(final String hostname,
251      final int port, boolean getMaster)
252   throws IOException;
253 
254   /**
255    * Find region location hosting passed row
256    * @param tableName table name
257    * @param row Row to find.
258    * @param reload If true do not use cache, otherwise bypass.
259    * @return Location of row.
260    * @throws IOException if a remote or network exception occurs
261    */
262   HRegionLocation getRegionLocation(byte [] tableName, byte [] row,
263     boolean reload)
264   throws IOException;
265 
266   /**
267    * Pass in a ServerCallable with your particular bit of logic defined and
268    * this method will manage the process of doing retries with timed waits
269    * and refinds of missing regions.
270    *
271    * @param <T> the type of the return value
272    * @param callable callable to run
273    * @return an object of type T
274    * @throws IOException if a remote or network exception occurs
275    * @throws RuntimeException other unspecified error
276    * @deprecated Use {@link HConnectionManager#withoutRetries(ServerCallable)}
277    */
278   public <T> T getRegionServerWithRetries(ServerCallable<T> callable)
279   throws IOException, RuntimeException;
280 
281   /**
282    * Pass in a ServerCallable with your particular bit of logic defined and
283    * this method will pass it to the defined region server.
284    * @param <T> the type of the return value
285    * @param callable callable to run
286    * @return an object of type T
287    * @throws IOException if a remote or network exception occurs
288    * @throws RuntimeException other unspecified error
289    * @deprecated Use {@link HConnectionManager#withoutRetries(ServerCallable)}
290    */
291   public <T> T getRegionServerWithoutRetries(ServerCallable<T> callable)
292   throws IOException, RuntimeException;
293 
294   /**
295    * Process a mixed batch of Get, Put and Delete actions. All actions for a
296    * RegionServer are forwarded in one RPC call.
297    *
298    *
299    * @param actions The collection of actions.
300    * @param tableName Name of the hbase table
301    * @param pool thread pool for parallel execution
302    * @param results An empty array, same size as list. If an exception is thrown,
303    * you can test here for partial results, and to determine which actions
304    * processed successfully.
305    * @throws IOException if there are problems talking to META. Per-item
306    * exceptions are stored in the results array.
307    */
308   public void processBatch(List<? extends Row> actions, final byte[] tableName,
309       ExecutorService pool, Object[] results)
310       throws IOException, InterruptedException;
311 
312   /**
313    * Parameterized batch processing, allowing varying return types for different
314    * {@link Row} implementations.
315    */
316   public <R> void processBatchCallback(List<? extends Row> list,
317       byte[] tableName,
318       ExecutorService pool,
319       Object[] results,
320       Batch.Callback<R> callback) throws IOException, InterruptedException;
321 
322 
323   /**
324    * Executes the given
325    * {@link org.apache.hadoop.hbase.client.coprocessor.Batch.Call}
326    * callable for each row in the given list and invokes
327    * {@link org.apache.hadoop.hbase.client.coprocessor.Batch.Callback#update(byte[], byte[], Object)}
328    * for each result returned.
329    *
330    * @param protocol the protocol interface being called
331    * @param rows a list of row keys for which the callable should be invoked
332    * @param tableName table name for the coprocessor invoked
333    * @param pool ExecutorService used to submit the calls per row
334    * @param call instance on which to invoke
335    * {@link org.apache.hadoop.hbase.client.coprocessor.Batch.Call#call(Object)}
336    * for each row
337    * @param callback instance on which to invoke
338    * {@link org.apache.hadoop.hbase.client.coprocessor.Batch.Callback#update(byte[], byte[], Object)}
339    * for each result
340    * @param <T> the protocol interface type
341    * @param <R> the callable's return type
342    * @throws IOException
343    */
344   public <T extends CoprocessorProtocol,R> void processExecs(
345       final Class<T> protocol,
346       List<byte[]> rows,
347       final byte[] tableName,
348       ExecutorService pool,
349       final Batch.Call<T,R> call,
350       final Batch.Callback<R> callback) throws IOException, Throwable;
351 
352   /**
353    * Enable or disable region cache prefetch for the table. It will be
354    * applied for the given table's all HTable instances within this
355    * connection. By default, the cache prefetch is enabled.
356    * @param tableName name of table to configure.
357    * @param enable Set to true to enable region cache prefetch.
358    */
359   public void setRegionCachePrefetch(final byte[] tableName,
360       final boolean enable);
361 
362   /**
363    * Check whether region cache prefetch is enabled or not.
364    * @param tableName name of table to check
365    * @return true if table's region cache prefetch is enabled. Otherwise
366    * it is disabled.
367    */
368   public boolean getRegionCachePrefetch(final byte[] tableName);
369 
370   /**
371    * Load the region map and warm up the global region cache for the table.
372    * @param tableName name of the table to perform region cache prewarm.
373    * @param regions a region map.
374    */
375   public void prewarmRegionCache(final byte[] tableName,
376       final Map<HRegionInfo, HServerAddress> regions);
377 
378   /**
379    * Scan zookeeper to get the number of region servers
380    * @return the number of region servers that are currently running
381    * @throws IOException if a remote or network exception occurs
382    * @deprecated This method will be changed from public to package protected.
383    */
384   public int getCurrentNrHRS() throws IOException;
385 
386   /**
387    * @param tableNames List of table names
388    * @return HTD[] table metadata
389    * @throws IOException if a remote or network exception occurs
390    */
391   public HTableDescriptor[] getHTableDescriptors(List<String> tableNames)
392   throws IOException;
393 
394   /**
395    * @return true if this connection is closed
396    */
397   public boolean isClosed();
398 
399   /**
400    * Clear any caches that pertain to server name <code>sn</code>
401    * @param sn A server name as hostname:port
402    */
403   public void clearCaches(final String sn);
404 }