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.Closeable;
22  import java.io.IOException;
23  import java.util.List;
24  import java.util.concurrent.ExecutorService;
25  
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.classification.InterfaceStability;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.Abortable;
30  import org.apache.hadoop.hbase.HRegionLocation;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.MasterNotRunningException;
33  import org.apache.hadoop.hbase.ServerName;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
36  import org.apache.hadoop.hbase.client.coprocessor.Batch;
37  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.AdminService;
38  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
39  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.MasterService;
40  
41  /**
42   * A cluster connection.  Knows how to find the master, locate regions out on the cluster,
43   * keeps a cache of locations and then knows how to re-calibrate after they move.  You need one
44   * of these to talk to your HBase cluster. {@link HConnectionManager} manages instances of this
45   * class.  See it for how to get one of these.
46   *
47   * <p>This is NOT a connection to a particular server but to ALL servers in the cluster.  Individual
48   * connections are managed at a lower level.
49   *
50   * <p>HConnections are used by {@link HTable} mostly but also by
51   * {@link HBaseAdmin}, and {@link org.apache.hadoop.hbase.zookeeper.MetaTableLocator}.
52   * HConnection instances can be shared.  Sharing
53   * is usually what you want because rather than each HConnection instance
54   * having to do its own discovery of regions out on the cluster, instead, all
55   * clients get to share the one cache of locations.  {@link HConnectionManager} does the
56   * sharing for you if you go by it getting connections.  Sharing makes cleanup of
57   * HConnections awkward.  See {@link HConnectionManager} for cleanup discussion.
58   *
59   * @see HConnectionManager
60   */
61  @InterfaceAudience.Public
62  @InterfaceStability.Stable
63  public interface HConnection extends Abortable, Closeable {
64    /**
65     * Key for configuration in Configuration whose value is the class we implement making a
66     * new HConnection instance.
67     */
68    public static final String HBASE_CLIENT_CONNECTION_IMPL = "hbase.client.connection.impl";
69  
70    /**
71     * @return Configuration instance being used by this HConnection instance.
72     */
73    Configuration getConfiguration();
74  
75    /**
76     * Retrieve an HTableInterface implementation for access to a table.
77     * The returned HTableInterface is not thread safe, a new instance should
78     * be created for each using thread.
79     * This is a lightweight operation, pooling or caching of the returned HTableInterface
80     * is neither required nor desired.
81     * Note that the HConnection needs to be unmanaged
82     * (created with {@link HConnectionManager#createConnection(Configuration)}).
83     * @param tableName
84     * @return an HTable to use for interactions with this table
85     */
86    public HTableInterface getTable(String tableName) throws IOException;
87  
88    /**
89     * Retrieve an HTableInterface implementation for access to a table.
90     * The returned HTableInterface is not thread safe, a new instance should
91     * be created for each using thread.
92     * This is a lightweight operation, pooling or caching of the returned HTableInterface
93     * is neither required nor desired.
94     * Note that the HConnection needs to be unmanaged
95     * (created with {@link HConnectionManager#createConnection(Configuration)}).
96     * @param tableName
97     * @return an HTable to use for interactions with this table
98     */
99    public HTableInterface getTable(byte[] tableName) throws IOException;
100 
101   /**
102    * Retrieve an HTableInterface implementation for access to a table.
103    * The returned HTableInterface is not thread safe, a new instance should
104    * be created for each using thread.
105    * This is a lightweight operation, pooling or caching of the returned HTableInterface
106    * is neither required nor desired.
107    * Note that the HConnection needs to be unmanaged
108    * (created with {@link HConnectionManager#createConnection(Configuration)}).
109    * @param tableName
110    * @return an HTable to use for interactions with this table
111    */
112   public HTableInterface getTable(TableName tableName) throws IOException;
113 
114   /**
115    * Retrieve an HTableInterface implementation for access to a table.
116    * The returned HTableInterface is not thread safe, a new instance should
117    * be created for each using thread.
118    * This is a lightweight operation, pooling or caching of the returned HTableInterface
119    * is neither required nor desired.
120    * Note that the HConnection needs to be unmanaged
121    * (created with {@link HConnectionManager#createConnection(Configuration)}).
122    * @param tableName
123    * @param pool The thread pool to use for batch operations, null to use a default pool.
124    * @return an HTable to use for interactions with this table
125    */
126   public HTableInterface getTable(String tableName, ExecutorService pool)  throws IOException;
127 
128   /**
129    * Retrieve an HTableInterface implementation for access to a table.
130    * The returned HTableInterface is not thread safe, a new instance should
131    * be created for each using thread.
132    * This is a lightweight operation, pooling or caching of the returned HTableInterface
133    * is neither required nor desired.
134    * Note that the HConnection needs to be unmanaged
135    * (created with {@link HConnectionManager#createConnection(Configuration)}).
136    * @param tableName
137    * @param pool The thread pool to use for batch operations, null to use a default pool.
138    * @return an HTable to use for interactions with this table
139    */
140   public HTableInterface getTable(byte[] tableName, ExecutorService pool)  throws IOException;
141 
142   /**
143    * Retrieve an HTableInterface implementation for access to a table.
144    * The returned HTableInterface is not thread safe, a new instance should
145    * be created for each using thread.
146    * This is a lightweight operation, pooling or caching of the returned HTableInterface
147    * is neither required nor desired.
148    * Note that the HConnection needs to be unmanaged
149    * (created with {@link HConnectionManager#createConnection(Configuration)}).
150    * @param tableName
151    * @param pool The thread pool to use for batch operations, null to use a default pool.
152    * @return an HTable to use for interactions with this table
153    */
154   public HTableInterface getTable(TableName tableName, ExecutorService pool)  throws IOException;
155 
156   /**
157    * Retrieve an Admin implementation to administer an HBase cluster.
158    * The returned Admin is not guaranteed to be thread-safe.  A new instance should be created for
159    * each using thread.  This is a lightweight operation.  Pooling or caching of the returned
160    * Admin is not recommended.  Note that HConnection needs to be unmanaged
161    * (created with {@link HConnectionManager#createConnection(Configuration)}).
162    *
163    * @return an Admin instance for cluster administration
164    */
165   Admin getAdmin() throws IOException;
166 
167   /** @return - true if the master server is running
168    * @deprecated internal method, do not use thru HConnection */
169   @Deprecated
170   boolean isMasterRunning()
171   throws MasterNotRunningException, ZooKeeperConnectionException;
172 
173   /**
174    * A table that isTableEnabled == false and isTableDisabled == false
175    * is possible. This happens when a table has a lot of regions
176    * that must be processed.
177    * @param tableName table name
178    * @return true if the table is enabled, false otherwise
179    * @throws IOException if a remote or network exception occurs
180    */
181   boolean isTableEnabled(TableName tableName) throws IOException;
182 
183   @Deprecated
184   boolean isTableEnabled(byte[] tableName) throws IOException;
185 
186   /**
187    * @param tableName table name
188    * @return true if the table is disabled, false otherwise
189    * @throws IOException if a remote or network exception occurs
190    */
191   boolean isTableDisabled(TableName tableName) throws IOException;
192 
193   @Deprecated
194   boolean isTableDisabled(byte[] tableName) throws IOException;
195 
196   /**
197    * @param tableName table name
198    * @return true if all regions of the table are available, false otherwise
199    * @throws IOException if a remote or network exception occurs
200    */
201   boolean isTableAvailable(TableName tableName) throws IOException;
202 
203   @Deprecated
204   boolean isTableAvailable(byte[] tableName) throws IOException;
205 
206   /**
207    * Use this api to check if the table has been created with the specified number of
208    * splitkeys which was used while creating the given table.
209    * Note : If this api is used after a table's region gets splitted, the api may return
210    * false.
211    * @param tableName
212    *          tableName
213    * @param splitKeys
214    *          splitKeys used while creating table
215    * @throws IOException
216    *           if a remote or network exception occurs
217    * @deprecated internal method, do not use thru HConnection */
218   @Deprecated
219   boolean isTableAvailable(TableName tableName, byte[][] splitKeys) throws
220       IOException;
221 
222   @Deprecated
223   boolean isTableAvailable(byte[] tableName, byte[][] splitKeys) throws
224       IOException;
225 
226   /**
227    * List all the userspace tables.  In other words, scan the hbase:meta table.
228    *
229    * If we wanted this to be really fast, we could implement a special
230    * catalog table that just contains table names and their descriptors.
231    * Right now, it only exists as part of the hbase:meta table's region info.
232    *
233    * @return - returns an array of HTableDescriptors
234    * @throws IOException if a remote or network exception occurs
235    */
236   HTableDescriptor[] listTables() throws IOException;
237 
238   // This is a bit ugly - We call this getTableNames in 0.94 and the
239   // successor function, returning TableName, listTableNames in later versions
240   // because Java polymorphism doesn't consider return value types
241 
242   @Deprecated
243   String[] getTableNames() throws IOException;
244 
245   TableName[] listTableNames() throws IOException;
246 
247   /**
248    * @param tableName table name
249    * @return table metadata
250    * @throws IOException if a remote or network exception occurs
251    */
252   HTableDescriptor getHTableDescriptor(TableName tableName)
253   throws IOException;
254 
255   @Deprecated
256   HTableDescriptor getHTableDescriptor(byte[] tableName)
257   throws IOException;
258 
259   /**
260    * Find the location of the region of <i>tableName</i> that <i>row</i>
261    * lives in.
262    * @param tableName name of the table <i>row</i> is in
263    * @param row row key you're trying to find the region of
264    * @return HRegionLocation that describes where to find the region in
265    * question
266    * @throws IOException if a remote or network exception occurs
267    * @deprecated internal method, do not use thru HConnection
268    */
269   @Deprecated
270   public HRegionLocation locateRegion(final TableName tableName,
271       final byte [] row) throws IOException;
272 
273   @Deprecated
274   public HRegionLocation locateRegion(final byte[] tableName,
275       final byte [] row) throws IOException;
276 
277   /**
278    * Allows flushing the region cache.
279    * @deprecated internal method, do not use thru HConnection */
280   @Deprecated
281   void clearRegionCache();
282 
283   /**
284    * Allows flushing the region cache of all locations that pertain to
285    * <code>tableName</code>
286    * @param tableName Name of the table whose regions we are to remove from
287    * cache.
288    * @deprecated internal method, do not use thru HConnection */
289   @Deprecated
290   void clearRegionCache(final TableName tableName);
291 
292   @Deprecated
293   void clearRegionCache(final byte[] tableName);
294 
295   /**
296    * Deletes cached locations for the specific region.
297    * @param location The location object for the region, to be purged from cache.
298    * @deprecated internal method, do not use thru HConnection */
299   @Deprecated
300   void deleteCachedRegionLocation(final HRegionLocation location);
301 
302   /**
303    * Find the location of the region of <i>tableName</i> that <i>row</i>
304    * lives in, ignoring any value that might be in the cache.
305    * @param tableName name of the table <i>row</i> is in
306    * @param row row key you're trying to find the region of
307    * @return HRegionLocation that describes where to find the region in
308    * question
309    * @throws IOException if a remote or network exception occurs
310    * @deprecated internal method, do not use thru HConnection */
311   @Deprecated
312   HRegionLocation relocateRegion(final TableName tableName,
313       final byte [] row) throws IOException;
314 
315   @Deprecated
316   HRegionLocation relocateRegion(final byte[] tableName,
317       final byte [] row) throws IOException;
318 
319   @Deprecated
320   void updateCachedLocations(TableName tableName, byte[] rowkey,
321                                     Object exception, HRegionLocation source);
322 
323   /**
324    * Update the location cache. This is used internally by HBase, in most cases it should not be
325    *  used by the client application.
326    * @param tableName the table name
327    * @param regionName the regionName
328    * @param rowkey the row
329    * @param exception the exception if any. Can be null.
330    * @param source the previous location
331    * @deprecated internal method, do not use thru HConnection
332    */
333   @Deprecated
334   void updateCachedLocations(TableName tableName, byte[] regionName, byte[] rowkey,
335                                     Object exception, ServerName source);
336 
337   @Deprecated
338   void updateCachedLocations(byte[] tableName, byte[] rowkey,
339                                     Object exception, HRegionLocation source);
340 
341   /**
342    * Gets the location of the region of <i>regionName</i>.
343    * @param regionName name of the region to locate
344    * @return HRegionLocation that describes where to find the region in
345    * question
346    * @throws IOException if a remote or network exception occurs
347    * @deprecated internal method, do not use thru HConnection */
348   @Deprecated
349   HRegionLocation locateRegion(final byte[] regionName)
350   throws IOException;
351 
352   /**
353    * Gets the locations of all regions in the specified table, <i>tableName</i>.
354    * @param tableName table to get regions of
355    * @return list of region locations for all regions of table
356    * @throws IOException
357    * @deprecated internal method, do not use thru HConnection */
358   @Deprecated
359   List<HRegionLocation> locateRegions(final TableName tableName) throws IOException;
360 
361   @Deprecated
362   List<HRegionLocation> locateRegions(final byte[] tableName) throws IOException;
363 
364   /**
365    * Gets the locations of all regions in the specified table, <i>tableName</i>.
366    * @param tableName table to get regions of
367    * @param useCache Should we use the cache to retrieve the region information.
368    * @param offlined True if we are to include offlined regions, false and we'll leave out offlined
369    *          regions from returned list.
370    * @return list of region locations for all regions of table
371    * @throws IOException
372    * @deprecated internal method, do not use thru HConnection
373    */
374   @Deprecated
375   public List<HRegionLocation> locateRegions(final TableName tableName,
376       final boolean useCache,
377       final boolean offlined) throws IOException;
378 
379   @Deprecated
380   public List<HRegionLocation> locateRegions(final byte[] tableName,
381       final boolean useCache,
382       final boolean offlined) throws IOException;
383 
384   /**
385    * Returns a {@link MasterKeepAliveConnection} to the active master
386    * @deprecated internal method, do not use thru HConnection */
387   @Deprecated
388   MasterService.BlockingInterface getMaster() throws IOException;
389 
390 
391   /**
392    * Establishes a connection to the region server at the specified address.
393    * @param serverName
394    * @return proxy for HRegionServer
395    * @throws IOException if a remote or network exception occurs
396    * @deprecated internal method, do not use thru HConnection */
397   @Deprecated
398   AdminService.BlockingInterface getAdmin(final ServerName serverName) throws IOException;
399 
400   /**
401    * Establishes a connection to the region server at the specified address, and returns
402    * a region client protocol.
403    *
404    * @param serverName
405    * @return ClientProtocol proxy for RegionServer
406    * @throws IOException if a remote or network exception occurs
407    * @deprecated internal method, do not use thru HConnection */
408   @Deprecated
409   ClientService.BlockingInterface getClient(final ServerName serverName) throws IOException;
410 
411   /**
412    * Establishes a connection to the region server at the specified address.
413    * @param serverName
414    * @param getMaster do we check if master is alive
415    * @return proxy for HRegionServer
416    * @throws IOException if a remote or network exception occurs
417    * @deprecated You can pass master flag but nothing special is done.
418    */
419   @Deprecated
420   AdminService.BlockingInterface getAdmin(final ServerName serverName, boolean getMaster)
421       throws IOException;
422 
423   /**
424    * Find region location hosting passed row
425    * @param tableName table name
426    * @param row Row to find.
427    * @param reload If true do not use cache, otherwise bypass.
428    * @return Location of row.
429    * @throws IOException if a remote or network exception occurs
430    * @deprecated internal method, do not use thru HConnection */
431   @Deprecated
432   HRegionLocation getRegionLocation(TableName tableName, byte [] row,
433     boolean reload)
434   throws IOException;
435 
436   @Deprecated
437   HRegionLocation getRegionLocation(byte[] tableName, byte [] row,
438     boolean reload)
439   throws IOException;
440 
441   /**
442    * Process a mixed batch of Get, Put and Delete actions. All actions for a
443    * RegionServer are forwarded in one RPC call.
444    *
445    *
446    * @param actions The collection of actions.
447    * @param tableName Name of the hbase table
448    * @param pool thread pool for parallel execution
449    * @param results An empty array, same size as list. If an exception is thrown,
450    * you can test here for partial results, and to determine which actions
451    * processed successfully.
452    * @throws IOException if there are problems talking to META. Per-item
453    * exceptions are stored in the results array.
454    * @deprecated since 0.96 - Use {@link HTableInterface#batch} instead
455    */
456   @Deprecated
457   void processBatch(List<? extends Row> actions, final TableName tableName,
458       ExecutorService pool, Object[] results) throws IOException, InterruptedException;
459 
460   @Deprecated
461   void processBatch(List<? extends Row> actions, final byte[] tableName,
462       ExecutorService pool, Object[] results) throws IOException, InterruptedException;
463 
464   /**
465    * Parameterized batch processing, allowing varying return types for different
466    * {@link Row} implementations.
467    * @deprecated since 0.96 - Use {@link HTableInterface#batchCallback} instead
468    */
469   @Deprecated
470   public <R> void processBatchCallback(List<? extends Row> list,
471       final TableName tableName,
472       ExecutorService pool,
473       Object[] results,
474       Batch.Callback<R> callback) throws IOException, InterruptedException;
475 
476   @Deprecated
477   public <R> void processBatchCallback(List<? extends Row> list,
478       final byte[] tableName,
479       ExecutorService pool,
480       Object[] results,
481       Batch.Callback<R> callback) throws IOException, InterruptedException;
482 
483   /**
484    * @deprecated does nothing since since 0.99
485    **/
486   @Deprecated
487   public void setRegionCachePrefetch(final TableName tableName,
488       final boolean enable);
489 
490   /**
491    * @deprecated does nothing since 0.99
492    **/
493   @Deprecated
494   public void setRegionCachePrefetch(final byte[] tableName,
495       final boolean enable);
496 
497   /**
498    * @deprecated always return false since 0.99
499    **/
500   @Deprecated
501   boolean getRegionCachePrefetch(final TableName tableName);
502 
503   /**
504    * @deprecated always return false since 0.99
505    **/
506   @Deprecated
507   boolean getRegionCachePrefetch(final byte[] tableName);
508 
509   /**
510    * @return the number of region servers that are currently running
511    * @throws IOException if a remote or network exception occurs
512    * @deprecated This method will be changed from public to package protected.
513    */
514   @Deprecated
515   int getCurrentNrHRS() throws IOException;
516 
517   /**
518    * @param tableNames List of table names
519    * @return HTD[] table metadata
520    * @throws IOException if a remote or network exception occurs
521    */
522   HTableDescriptor[] getHTableDescriptorsByTableName(List<TableName> tableNames) throws IOException;
523 
524   @Deprecated
525   HTableDescriptor[] getHTableDescriptors(List<String> tableNames) throws
526       IOException;
527 
528   /**
529    * @return true if this connection is closed
530    */
531   boolean isClosed();
532 
533 
534   /**
535    * Clear any caches that pertain to server name <code>sn</code>.
536    * @param sn A server name
537    * @deprecated internal method, do not use thru HConnection */
538   @Deprecated
539   void clearCaches(final ServerName sn);
540 
541   /**
542    * This function allows HBaseAdmin and potentially others to get a shared MasterService
543    * connection.
544    * @return The shared instance. Never returns null.
545    * @throws MasterNotRunningException
546    * @deprecated Since 0.96.0
547    */
548   // TODO: Why is this in the public interface when the returned type is shutdown package access?
549   @Deprecated
550   MasterKeepAliveConnection getKeepAliveMasterService()
551   throws MasterNotRunningException;
552 
553   /**
554    * @param serverName
555    * @return true if the server is known as dead, false otherwise.
556    * @deprecated internal method, do not use thru HConnection */
557   @Deprecated
558   boolean isDeadServer(ServerName serverName);
559 
560   /**
561    * @return Nonce generator for this HConnection; may be null if disabled in configuration.
562    * @deprecated internal method, do not use thru HConnection */
563   @Deprecated
564   public NonceGenerator getNonceGenerator();
565 }