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.ServerName;
33  import org.apache.hadoop.hbase.catalog.CatalogTracker;
34  import org.apache.hadoop.hbase.client.coprocessor.Batch;
35  import org.apache.hadoop.hbase.exceptions.MasterNotRunningException;
36  import org.apache.hadoop.hbase.exceptions.ZooKeeperConnectionException;
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.MasterAdminProtos.MasterAdminService;
40  import org.apache.hadoop.hbase.protobuf.generated.MasterMonitorProtos.MasterMonitorService;
41  
42  /**
43   * A cluster connection.  Knows how to find the master, locate regions out on the cluster,
44   * keeps a cache of locations and then knows how to recalibrate after they move.
45   * {@link HConnectionManager} manages instances of this class.   This is NOT a connection to a
46   * particular server but to all servers in the cluster.  An implementation takes care of individual
47   * connections at a lower level.
48   *
49   * <p>HConnections are used by {@link HTable} mostly but also by
50   * {@link HBaseAdmin}, and {@link CatalogTracker}.  HConnection instances can be shared.  Sharing
51   * is usually what you want because rather than each HConnection instance
52   * having to do its own discovery of regions out on the cluster, instead, all
53   * clients get to share the one cache of locations.  {@link HConnectionManager} does the
54   * sharing for you if you go by it getting connections.  Sharing makes cleanup of
55   * HConnections awkward.  See {@link HConnectionManager} for cleanup discussion.
56   *
57   * @see HConnectionManager
58   */
59  @InterfaceAudience.Public
60  @InterfaceStability.Stable
61  public interface HConnection extends Abortable, Closeable {
62    /**
63     * @return Configuration instance being used by this HConnection instance.
64     */
65    public Configuration getConfiguration();
66  
67    /** @return - true if the master server is running */
68    public boolean isMasterRunning()
69    throws MasterNotRunningException, ZooKeeperConnectionException;
70  
71    /**
72     * A table that isTableEnabled == false and isTableDisabled == false
73     * is possible. This happens when a table has a lot of regions
74     * that must be processed.
75     * @param tableName table name
76     * @return true if the table is enabled, false otherwise
77     * @throws IOException if a remote or network exception occurs
78     */
79    public boolean isTableEnabled(byte[] tableName) throws IOException;
80  
81    /**
82     * @param tableName table name
83     * @return true if the table is disabled, false otherwise
84     * @throws IOException if a remote or network exception occurs
85     */
86    public boolean isTableDisabled(byte[] tableName) throws IOException;
87  
88    /**
89     * @param tableName table name
90     * @return true if all regions of the table are available, false otherwise
91     * @throws IOException if a remote or network exception occurs
92     */
93    public boolean isTableAvailable(byte[] tableName) throws IOException;
94  
95    /**
96     * Use this api to check if the table has been created with the specified number of
97     * splitkeys which was used while creating the given table.
98     * Note : If this api is used after a table's region gets splitted, the api may return
99     * false.
100    * @param tableName
101    *          tableName
102    * @param splitKeys
103    *          splitKeys used while creating table
104    * @throws IOException
105    *           if a remote or network exception occurs
106    */
107   public boolean isTableAvailable(byte[] tableName, byte[][] splitKeys) throws IOException;
108 
109   /**
110    * List all the userspace tables.  In other words, scan the META table.
111    *
112    * If we wanted this to be really fast, we could implement a special
113    * catalog table that just contains table names and their descriptors.
114    * Right now, it only exists as part of the META table's region info.
115    *
116    * @return - returns an array of HTableDescriptors
117    * @throws IOException if a remote or network exception occurs
118    */
119   public HTableDescriptor[] listTables() throws IOException;
120 
121   /**
122    * @param tableName table name
123    * @return table metadata
124    * @throws IOException if a remote or network exception occurs
125    */
126   public HTableDescriptor getHTableDescriptor(byte[] tableName)
127   throws IOException;
128 
129   /**
130    * Find the location of the region of <i>tableName</i> that <i>row</i>
131    * lives in.
132    * @param tableName name of the table <i>row</i> is in
133    * @param row row key you're trying to find the region of
134    * @return HRegionLocation that describes where to find the region in
135    * question
136    * @throws IOException if a remote or network exception occurs
137    */
138   public HRegionLocation locateRegion(final byte [] tableName,
139       final byte [] row)
140   throws IOException;
141 
142   /**
143    * Allows flushing the region cache.
144    */
145   public void clearRegionCache();
146 
147   /**
148    * Allows flushing the region cache of all locations that pertain to
149    * <code>tableName</code>
150    * @param tableName Name of the table whose regions we are to remove from
151    * cache.
152    */
153   public void clearRegionCache(final byte [] tableName);
154 
155   /**
156    * Deletes cached locations for the specific region.
157    * @param location The location object for the region, to be purged from cache.
158    */
159   public void deleteCachedRegionLocation(final HRegionLocation location);
160 
161   /**
162    * Find the location of the region of <i>tableName</i> that <i>row</i>
163    * lives in, ignoring any value that might be in the cache.
164    * @param tableName name of the table <i>row</i> is in
165    * @param row row key you're trying to find the region of
166    * @return HRegionLocation that describes where to find the region in
167    * question
168    * @throws IOException if a remote or network exception occurs
169    */
170   public HRegionLocation relocateRegion(final byte [] tableName,
171       final byte [] row)
172   throws IOException;
173 
174   /**
175    * Gets the location of the region of <i>regionName</i>.
176    * @param regionName name of the region to locate
177    * @return HRegionLocation that describes where to find the region in
178    * question
179    * @throws IOException if a remote or network exception occurs
180    */
181   public HRegionLocation locateRegion(final byte [] regionName)
182   throws IOException;
183 
184   /**
185    * Gets the locations of all regions in the specified table, <i>tableName</i>.
186    * @param tableName table to get regions of
187    * @return list of region locations for all regions of table
188    * @throws IOException
189    */
190   public List<HRegionLocation> locateRegions(final byte[] tableName)
191   throws IOException;
192 
193   /**
194    * Gets the locations of all regions in the specified table, <i>tableName</i>.
195    * @param tableName table to get regions of
196    * @param useCache Should we use the cache to retrieve the region information.
197    * @param offlined True if we are to include offlined regions, false and we'll leave out offlined
198    *          regions from returned list.
199    * @return list of region locations for all regions of table
200    * @throws IOException
201    */
202   public List<HRegionLocation> locateRegions(final byte[] tableName, final boolean useCache,
203       final boolean offlined) throws IOException;
204 
205   /**
206    * Returns a {@link MasterAdminKeepAliveConnection} to the active master
207    */
208   public MasterAdminService.BlockingInterface getMasterAdmin() throws IOException;
209 
210   /**
211    * Returns an {@link MasterMonitorKeepAliveConnection} to the active master
212    */
213   public MasterMonitorService.BlockingInterface getMasterMonitor() throws IOException;
214 
215   /**
216    * Establishes a connection to the region server at the specified address.
217    * @param serverName
218    * @return proxy for HRegionServer
219    * @throws IOException if a remote or network exception occurs
220    */
221   public AdminService.BlockingInterface getAdmin(final ServerName serverName) throws IOException;
222 
223   /**
224    * Establishes a connection to the region server at the specified address, and returns
225    * a region client protocol.
226    *
227    * @param serverName
228    * @return ClientProtocol proxy for RegionServer
229    * @throws IOException if a remote or network exception occurs
230    *
231    */
232   public ClientService.BlockingInterface getClient(final ServerName serverName) throws IOException;
233 
234   /**
235    * Establishes a connection to the region server at the specified address.
236    * @param serverName
237    * @param getMaster do we check if master is alive
238    * @return proxy for HRegionServer
239    * @throws IOException if a remote or network exception occurs
240    * @deprecated You can pass master flag but nothing special is done.
241    */
242   public AdminService.BlockingInterface getAdmin(final ServerName serverName, boolean getMaster)
243       throws IOException;
244 
245   /**
246    * Find region location hosting passed row
247    * @param tableName table name
248    * @param row Row to find.
249    * @param reload If true do not use cache, otherwise bypass.
250    * @return Location of row.
251    * @throws IOException if a remote or network exception occurs
252    */
253   HRegionLocation getRegionLocation(byte [] tableName, byte [] row,
254     boolean reload)
255   throws IOException;
256 
257   /**
258    * Pass in a ServerCallable with your particular bit of logic defined and
259    * this method will manage the process of doing retries with timed waits
260    * and refinds of missing regions.
261    *
262    * @param <T> the type of the return value
263    * @param callable callable to run
264    * @return an object of type T
265    * @throws IOException if a remote or network exception occurs
266    * @throws RuntimeException other unspecified error
267    */
268   @Deprecated
269   public <T> T getRegionServerWithRetries(ServerCallable<T> callable)
270   throws IOException, RuntimeException;
271 
272   /**
273    * Pass in a ServerCallable with your particular bit of logic defined and
274    * this method will pass it to the defined region server.
275    * @param <T> the type of the return value
276    * @param callable callable to run
277    * @return an object of type T
278    * @throws IOException if a remote or network exception occurs
279    * @throws RuntimeException other unspecified error
280    */
281   @Deprecated
282   public <T> T getRegionServerWithoutRetries(ServerCallable<T> callable)
283   throws IOException, RuntimeException;
284 
285   /**
286    * Process a mixed batch of Get, Put and Delete actions. All actions for a
287    * RegionServer are forwarded in one RPC call.
288    *
289    *
290    * @param actions The collection of actions.
291    * @param tableName Name of the hbase table
292    * @param pool thread pool for parallel execution
293    * @param results An empty array, same size as list. If an exception is thrown,
294    * you can test here for partial results, and to determine which actions
295    * processed successfully.
296    * @throws IOException if there are problems talking to META. Per-item
297    * exceptions are stored in the results array.
298    * @deprecated since 0.96 - Use {@link HTableInterface#batch} instead
299    */
300   @Deprecated
301   public void processBatch(List<? extends Row> actions, final byte[] tableName,
302       ExecutorService pool, Object[] results)
303       throws IOException, InterruptedException;
304 
305   /**
306    * Parameterized batch processing, allowing varying return types for different
307    * {@link Row} implementations.
308    * @deprecated since 0.96 - Use {@link HTableInterface#batchCallback} instead
309    */
310   @Deprecated
311   public <R> void processBatchCallback(List<? extends Row> list,
312       byte[] tableName,
313       ExecutorService pool,
314       Object[] results,
315       Batch.Callback<R> callback) throws IOException, InterruptedException;
316 
317   /**
318    * Enable or disable region cache prefetch for the table. It will be
319    * applied for the given table's all HTable instances within this
320    * connection. By default, the cache prefetch is enabled.
321    * @param tableName name of table to configure.
322    * @param enable Set to true to enable region cache prefetch.
323    */
324   public void setRegionCachePrefetch(final byte[] tableName,
325       final boolean enable);
326 
327   /**
328    * Check whether region cache prefetch is enabled or not.
329    * @param tableName name of table to check
330    * @return true if table's region cache prefetch is enabled. Otherwise
331    * it is disabled.
332    */
333   public boolean getRegionCachePrefetch(final byte[] tableName);
334 
335   /**
336    * @return the number of region servers that are currently running
337    * @throws IOException if a remote or network exception occurs
338    * @deprecated This method will be changed from public to package protected.
339    */
340   public int getCurrentNrHRS() throws IOException;
341 
342   /**
343    * @param tableNames List of table names
344    * @return HTD[] table metadata
345    * @throws IOException if a remote or network exception occurs
346    */
347   public HTableDescriptor[] getHTableDescriptors(List<String> tableNames)
348   throws IOException;
349 
350   /**
351    * @return true if this connection is closed
352    */
353   public boolean isClosed();
354 
355 
356   /**
357    * Clear any caches that pertain to server name <code>sn</code>
358    * @param sn A server name
359    */
360   public void clearCaches(final ServerName sn);
361 
362   /**
363    * This function allows HBaseAdmin and potentially others to get a shared MasterMonitor
364    * connection.
365    * @return The shared instance. Never returns null.
366    * @throws MasterNotRunningException
367    */
368   // TODO: Why is this in the public interface when the returned type is shutdown package access?
369   public MasterMonitorKeepAliveConnection getKeepAliveMasterMonitorService()
370   throws MasterNotRunningException;
371 
372   /**
373    * This function allows HBaseAdmin and potentially others to get a shared MasterAdminProtocol
374    * connection.
375    * @return The shared instance. Never returns null.
376    * @throws MasterNotRunningException
377    */
378   // TODO: Why is this in the public interface when the returned type is shutdown package access?
379   public MasterAdminKeepAliveConnection getKeepAliveMasterAdminService() throws MasterNotRunningException;
380 
381   /**
382    * @param serverName
383    * @return true if the server is known as dead, false otherwise.
384    */
385   public boolean isDeadServer(ServerName serverName);
386 }