org.apache.hadoop.hbase.client
Class CoprocessorHConnection

java.lang.Object
  extended by org.apache.hadoop.hbase.client.CoprocessorHConnection
All Implemented Interfaces:
Closeable, Abortable, HConnection

public class CoprocessorHConnection
extends Object

Connection to an HTable from within a Coprocessor. Can do some nice tricks since we know we are on a regionserver.

This shouldn't be used by usual HBase clients - its merely in this package to maintain visibility considerations for the HConnectionManager.HConnectionImplementation.


Method Summary
 void abort(String msg, Throwable t)
          Abort the server or client.
 void clearCaches(String sn)
          Clear any caches that pertain to server name sn
 void clearRegionCache()
          Allows flushing the region cache.
 void clearRegionCache(byte[] tableName)
          Allows flushing the region cache of all locations that pertain to tableName
 void close()
           
 void deleteCachedRegionLocation(HRegionLocation location)
          Deletes cached locations for the specific region.
protected  void finalize()
          Close the connection for good, regardless of what the current value of refCount is.
 org.apache.hadoop.conf.Configuration getConfiguration()
           
static HConnection getConnectionForEnvironment(CoprocessorEnvironment env)
          Create an unmanaged HConnection based on the environment in which we are running the coprocessor.
protected  ExecutorService getCurrentBatchPool()
           
 int getCurrentNrHRS()
          Scan zookeeper to get the number of region servers
 HRegionInterface getHRegionConnection(HServerAddress hsa)
          Establishes a connection to the region server at the specified address.
 HRegionInterface getHRegionConnection(HServerAddress hsa, boolean master)
          Establishes a connection to the region server at the specified address.
 HRegionInterface getHRegionConnection(String hostname, int port)
          Establishes a connection to the region server at the specified address.
 HRegionInterface getHRegionConnection(String hostname, int port, boolean master)
          Establishes a connection to the region server at the specified address.
 HTableDescriptor getHTableDescriptor(byte[] tableName)
           
 HTableDescriptor[] getHTableDescriptors(List<String> tableNames)
           
 HMasterInterface getMaster()
           
 boolean getRegionCachePrefetch(byte[] tableName)
          Check whether region cache prefetch is enabled or not.
 HRegionLocation getRegionLocation(byte[] name, byte[] row, boolean reload)
          Find region location hosting passed row
<T> T
getRegionServerWithoutRetries(ServerCallable<T> callable)
          Pass in a ServerCallable with your particular bit of logic defined and this method will pass it to the defined region server.
<T> T
getRegionServerWithRetries(ServerCallable<T> callable)
          Pass in a ServerCallable with your particular bit of logic defined and this method will manage the process of doing retries with timed waits and refinds of missing regions.
 HTableInterface getTable(byte[] tableName)
          Retrieve an HTableInterface implementation for access to a table.
 HTableInterface getTable(byte[] tableName, ExecutorService pool)
          Retrieve an HTableInterface implementation for access to a table.
 HTableInterface getTable(String tableName)
          Retrieve an HTableInterface implementation for access to a table.
 HTableInterface getTable(String tableName, ExecutorService pool)
          Retrieve an HTableInterface implementation for access to a table.
 String[] getTableNames()
           
 ZooKeeperWatcher getZooKeeperWatcher()
          Deprecated. 
 boolean isAborted()
          Check if the server or client was aborted.
 boolean isClosed()
           
 boolean isMasterRunning()
           
 boolean isTableAvailable(byte[] tableName)
           
 boolean isTableDisabled(byte[] tableName)
           
 boolean isTableEnabled(byte[] tableName)
          A table that isTableEnabled == false and isTableDisabled == false is possible.
 HTableDescriptor[] listTables()
          List all the userspace tables.
 HRegionLocation locateRegion(byte[] regionName)
          Gets the location of the region of regionName.
 HRegionLocation locateRegion(byte[] tableName, byte[] row)
          Find the location of the region of tableName that row lives in.
 List<HRegionLocation> locateRegions(byte[] tableName)
          Gets the locations of all regions in the specified table, tableName.
 List<HRegionLocation> locateRegions(byte[] tableName, boolean useCache, boolean offlined)
          Gets the locations of all regions in the specified table, tableName.
 void prewarmRegionCache(byte[] tableName, Map<HRegionInfo,HServerAddress> regions)
          Load the region map and warm up the global region cache for the table.
 void processBatch(List<? extends Row> list, byte[] tableName, ExecutorService pool, Object[] results)
          Process a mixed batch of Get, Put and Delete actions.
<R> void
processBatchCallback(List<? extends Row> list, byte[] tableName, ExecutorService pool, Object[] results, Batch.Callback<R> callback)
          Parameterized batch processing, allowing varying return types for different Row implementations.
<T extends CoprocessorProtocol,R>
void
processExecs(Class<T> protocol, List<byte[]> rows, byte[] tableName, ExecutorService pool, Batch.Call<T,R> callable, Batch.Callback<R> callback)
          Executes the given Batch.Call callable for each row in the given list and invokes Batch.Callback.update(byte[], byte[], Object) for each result returned.
 HRegionLocation relocateRegion(byte[] tableName, byte[] row)
          Find the location of the region of tableName that row lives in, ignoring any value that might be in the cache.
 void setRegionCachePrefetch(byte[] tableName, boolean enable)
          Enable or disable region cache prefetch for the table.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getConnectionForEnvironment

public static HConnection getConnectionForEnvironment(CoprocessorEnvironment env)
                                               throws IOException
Create an unmanaged HConnection based on the environment in which we are running the coprocessor. The HConnection must be externally cleaned up (we bypass the usual HTable cleanup mechanisms since we own everything).

Parameters:
env - environment hosting the HConnection
Returns:
an unmanaged HConnection.
Throws:
IOException - if we cannot create the basic connection

getTable

public HTableInterface getTable(String tableName)
                         throws IOException
Description copied from interface: HConnection
Retrieve an HTableInterface implementation for access to a table. The returned HTableInterface is not thread safe, a new instance should be created for each using thread. This is a lightweight operation, pooling or caching of the returned HTableInterface is neither required nor desired. Note that the HConnection needs to be unmanaged (created with HConnectionManager.createConnection(Configuration)).

Specified by:
getTable in interface HConnection
Returns:
an HTable to use for interactions with this table
Throws:
IOException

getTable

public HTableInterface getTable(byte[] tableName)
                         throws IOException
Description copied from interface: HConnection
Retrieve an HTableInterface implementation for access to a table. The returned HTableInterface is not thread safe, a new instance should be created for each using thread. This is a lightweight operation, pooling or caching of the returned HTableInterface is neither required nor desired. Note that the HConnection needs to be unmanaged (created with HConnectionManager.createConnection(Configuration)).

Specified by:
getTable in interface HConnection
Returns:
an HTable to use for interactions with this table
Throws:
IOException

getTable

public HTableInterface getTable(String tableName,
                                ExecutorService pool)
                         throws IOException
Description copied from interface: HConnection
Retrieve an HTableInterface implementation for access to a table. The returned HTableInterface is not thread safe, a new instance should be created for each using thread. This is a lightweight operation, pooling or caching of the returned HTableInterface is neither required nor desired. Note that the HConnection needs to be unmanaged (created with HConnectionManager.createConnection(Configuration)).

Specified by:
getTable in interface HConnection
pool - The thread pool to use for batch operations, null to use a default pool.
Returns:
an HTable to use for interactions with this table
Throws:
IOException

getTable

public HTableInterface getTable(byte[] tableName,
                                ExecutorService pool)
                         throws IOException
Description copied from interface: HConnection
Retrieve an HTableInterface implementation for access to a table. The returned HTableInterface is not thread safe, a new instance should be created for each using thread. This is a lightweight operation, pooling or caching of the returned HTableInterface is neither required nor desired. Note that the HConnection needs to be unmanaged (created with HConnectionManager.createConnection(Configuration)).

Specified by:
getTable in interface HConnection
pool - The thread pool to use for batch operations, null to use a default pool.
Returns:
an HTable to use for interactions with this table
Throws:
IOException

getCurrentBatchPool

protected ExecutorService getCurrentBatchPool()

getConfiguration

public org.apache.hadoop.conf.Configuration getConfiguration()
Specified by:
getConfiguration in interface HConnection
Returns:
Configuration instance being used by this HConnection instance.

getMaster

public HMasterInterface getMaster()
                           throws MasterNotRunningException,
                                  ZooKeeperConnectionException
Specified by:
getMaster in interface HConnection
Returns:
proxy connection to master server for this instance
Throws:
MasterNotRunningException - if the master is not running
ZooKeeperConnectionException - if unable to connect to zookeeper

isMasterRunning

public boolean isMasterRunning()
                        throws MasterNotRunningException,
                               ZooKeeperConnectionException
Specified by:
isMasterRunning in interface HConnection
Returns:
- true if the master server is running
Throws:
MasterNotRunningException
ZooKeeperConnectionException

getRegionLocation

public HRegionLocation getRegionLocation(byte[] name,
                                         byte[] row,
                                         boolean reload)
                                  throws IOException
Description copied from interface: HConnection
Find region location hosting passed row

Specified by:
getRegionLocation in interface HConnection
Parameters:
name - table name
row - Row to find.
reload - If true do not use cache, otherwise bypass.
Returns:
Location of row.
Throws:
IOException - if a remote or network exception occurs

isTableEnabled

public boolean isTableEnabled(byte[] tableName)
                       throws IOException
Description copied from interface: HConnection
A table that isTableEnabled == false and isTableDisabled == false is possible. This happens when a table has a lot of regions that must be processed.

Specified by:
isTableEnabled in interface HConnection
Parameters:
tableName - table name
Returns:
true if the table is enabled, false otherwise
Throws:
IOException - if a remote or network exception occurs

isTableDisabled

public boolean isTableDisabled(byte[] tableName)
                        throws IOException
Specified by:
isTableDisabled in interface HConnection
Parameters:
tableName - table name
Returns:
true if the table is disabled, false otherwise
Throws:
IOException - if a remote or network exception occurs

isTableAvailable

public boolean isTableAvailable(byte[] tableName)
                         throws IOException
Specified by:
isTableAvailable in interface HConnection
Parameters:
tableName - table name
Returns:
true if all regions of the table are available, false otherwise
Throws:
IOException - if a remote or network exception occurs

locateRegion

public HRegionLocation locateRegion(byte[] regionName)
                             throws IOException
Description copied from interface: HConnection
Gets the location of the region of regionName.

Specified by:
locateRegion in interface HConnection
Parameters:
regionName - name of the region to locate
Returns:
HRegionLocation that describes where to find the region in question
Throws:
IOException - if a remote or network exception occurs

locateRegions

public List<HRegionLocation> locateRegions(byte[] tableName)
                                    throws IOException
Description copied from interface: HConnection
Gets the locations of all regions in the specified table, tableName.

Specified by:
locateRegions in interface HConnection
Parameters:
tableName - table to get regions of
Returns:
list of region locations for all regions of table
Throws:
IOException

locateRegions

public List<HRegionLocation> locateRegions(byte[] tableName,
                                           boolean useCache,
                                           boolean offlined)
                                    throws IOException
Description copied from interface: HConnection
Gets the locations of all regions in the specified table, tableName.

Specified by:
locateRegions in interface HConnection
Parameters:
tableName - table to get regions of
useCache - Should we use the cache to retrieve the region information.
offlined - True if we are to include offlined regions, false and we'll leave out offlined regions from returned list.
Returns:
list of region locations for all regions of table
Throws:
IOException

locateRegion

public HRegionLocation locateRegion(byte[] tableName,
                                    byte[] row)
                             throws IOException
Description copied from interface: HConnection
Find the location of the region of tableName that row lives in.

Specified by:
locateRegion in interface HConnection
Parameters:
tableName - name of the table row is in
row - row key you're trying to find the region of
Returns:
HRegionLocation that describes where to find the region in question
Throws:
IOException - if a remote or network exception occurs

relocateRegion

public HRegionLocation relocateRegion(byte[] tableName,
                                      byte[] row)
                               throws IOException
Description copied from interface: HConnection
Find the location of the region of tableName that row lives in, ignoring any value that might be in the cache.

Specified by:
relocateRegion in interface HConnection
Parameters:
tableName - name of the table row is in
row - row key you're trying to find the region of
Returns:
HRegionLocation that describes where to find the region in question
Throws:
IOException - if a remote or network exception occurs

deleteCachedRegionLocation

public void deleteCachedRegionLocation(HRegionLocation location)
Description copied from interface: HConnection
Deletes cached locations for the specific region.

Specified by:
deleteCachedRegionLocation in interface HConnection
Parameters:
location - The location object for the region, to be purged from cache.

clearCaches

public void clearCaches(String sn)
Description copied from interface: HConnection
Clear any caches that pertain to server name sn

Specified by:
clearCaches in interface HConnection
Parameters:
sn - A server name as hostname:port

clearRegionCache

public void clearRegionCache()
Description copied from interface: HConnection
Allows flushing the region cache.

Specified by:
clearRegionCache in interface HConnection

clearRegionCache

public void clearRegionCache(byte[] tableName)
Description copied from interface: HConnection
Allows flushing the region cache of all locations that pertain to tableName

Specified by:
clearRegionCache in interface HConnection
Parameters:
tableName - Name of the table whose regions we are to remove from cache.

getHRegionConnection

public HRegionInterface getHRegionConnection(HServerAddress hsa)
                                      throws IOException
Description copied from interface: HConnection
Establishes a connection to the region server at the specified address.

Specified by:
getHRegionConnection in interface HConnection
Parameters:
hsa - - the server to connect to
Returns:
proxy for HRegionServer
Throws:
IOException - if a remote or network exception occurs

getHRegionConnection

public HRegionInterface getHRegionConnection(String hostname,
                                             int port)
                                      throws IOException
Description copied from interface: HConnection
Establishes a connection to the region server at the specified address.

Specified by:
getHRegionConnection in interface HConnection
Parameters:
hostname - RegionServer hostname
port - RegionServer port
Returns:
proxy for HRegionServer
Throws:
IOException - if a remote or network exception occurs

getHRegionConnection

public HRegionInterface getHRegionConnection(HServerAddress hsa,
                                             boolean master)
                                      throws IOException
Description copied from interface: HConnection
Establishes a connection to the region server at the specified address.

Specified by:
getHRegionConnection in interface HConnection
Parameters:
hsa - - the server to connect to
master - - do we check if master is alive
Returns:
proxy for HRegionServer
Throws:
IOException - if a remote or network exception occurs

getHRegionConnection

public HRegionInterface getHRegionConnection(String hostname,
                                             int port,
                                             boolean master)
                                      throws IOException
Description copied from interface: HConnection
Establishes a connection to the region server at the specified address.

Specified by:
getHRegionConnection in interface HConnection
Parameters:
hostname - RegionServer hostname
port - RegionServer port
master - - do we check if master is alive
Returns:
proxy for HRegionServer
Throws:
IOException - if a remote or network exception occurs

getZooKeeperWatcher

@Deprecated
public ZooKeeperWatcher getZooKeeperWatcher()
                                     throws ZooKeeperConnectionException
Deprecated. 

Get the ZooKeeper instance for this TableServers instance. If ZK has not been initialized yet, this will connect to ZK.

Specified by:
getZooKeeperWatcher in interface HConnection
Returns:
ZooKeeperWatcher handle being used by the connection.
Throws:
ZooKeeperConnectionException - if there's a problem connecting to zk

getRegionServerWithRetries

public <T> T getRegionServerWithRetries(ServerCallable<T> callable)
                             throws IOException,
                                    RuntimeException
Description copied from interface: HConnection
Pass in a ServerCallable with your particular bit of logic defined and this method will manage the process of doing retries with timed waits and refinds of missing regions.

Specified by:
getRegionServerWithRetries in interface HConnection
Type Parameters:
T - the type of the return value
Parameters:
callable - callable to run
Returns:
an object of type T
Throws:
IOException - if a remote or network exception occurs
RuntimeException - other unspecified error

getRegionServerWithoutRetries

public <T> T getRegionServerWithoutRetries(ServerCallable<T> callable)
                                throws IOException,
                                       RuntimeException
Description copied from interface: HConnection
Pass in a ServerCallable with your particular bit of logic defined and this method will pass it to the defined region server.

Specified by:
getRegionServerWithoutRetries in interface HConnection
Type Parameters:
T - the type of the return value
Parameters:
callable - callable to run
Returns:
an object of type T
Throws:
IOException - if a remote or network exception occurs
RuntimeException - other unspecified error

processBatch

public void processBatch(List<? extends Row> list,
                         byte[] tableName,
                         ExecutorService pool,
                         Object[] results)
                  throws IOException,
                         InterruptedException
Description copied from interface: HConnection
Process a mixed batch of Get, Put and Delete actions. All actions for a RegionServer are forwarded in one RPC call.

Specified by:
processBatch in interface HConnection
Parameters:
list - The collection of actions.
tableName - Name of the hbase table
pool - thread pool for parallel execution
results - An empty array, same size as list. If an exception is thrown, you can test here for partial results, and to determine which actions processed successfully.
Throws:
IOException - if there are problems talking to META. Per-item exceptions are stored in the results array.
InterruptedException

processExecs

public <T extends CoprocessorProtocol,R> void processExecs(Class<T> protocol,
                                                           List<byte[]> rows,
                                                           byte[] tableName,
                                                           ExecutorService pool,
                                                           Batch.Call<T,R> callable,
                                                           Batch.Callback<R> callback)
                  throws IOException,
                         Throwable
Executes the given Batch.Call callable for each row in the given list and invokes Batch.Callback.update(byte[], byte[], Object) for each result returned.

Specified by:
processExecs in interface HConnection
Type Parameters:
T - the protocol interface type
R - the callable's return type
Parameters:
protocol - the protocol interface being called
rows - a list of row keys for which the callable should be invoked
tableName - table name for the coprocessor invoked
pool - ExecutorService used to submit the calls per row
callable - instance on which to invoke Batch.Call.call(Object) for each row
callback - instance on which to invoke Batch.Callback.update(byte[], byte[], Object) for each result
Throws:
IOException
Throwable

processBatchCallback

public <R> void processBatchCallback(List<? extends Row> list,
                                     byte[] tableName,
                                     ExecutorService pool,
                                     Object[] results,
                                     Batch.Callback<R> callback)
                          throws IOException,
                                 InterruptedException
Parameterized batch processing, allowing varying return types for different Row implementations.

Specified by:
processBatchCallback in interface HConnection
Throws:
IOException
InterruptedException

setRegionCachePrefetch

public void setRegionCachePrefetch(byte[] tableName,
                                   boolean enable)
Description copied from interface: HConnection
Enable or disable region cache prefetch for the table. It will be applied for the given table's all HTable instances within this connection. By default, the cache prefetch is enabled.

Specified by:
setRegionCachePrefetch in interface HConnection
Parameters:
tableName - name of table to configure.
enable - Set to true to enable region cache prefetch.

getRegionCachePrefetch

public boolean getRegionCachePrefetch(byte[] tableName)
Description copied from interface: HConnection
Check whether region cache prefetch is enabled or not.

Specified by:
getRegionCachePrefetch in interface HConnection
Parameters:
tableName - name of table to check
Returns:
true if table's region cache prefetch is enabled. Otherwise it is disabled.

prewarmRegionCache

public void prewarmRegionCache(byte[] tableName,
                               Map<HRegionInfo,HServerAddress> regions)
Description copied from interface: HConnection
Load the region map and warm up the global region cache for the table.

Specified by:
prewarmRegionCache in interface HConnection
Parameters:
tableName - name of the table to perform region cache prewarm.
regions - a region map.

abort

public void abort(String msg,
                  Throwable t)
Description copied from interface: Abortable
Abort the server or client.

Specified by:
abort in interface Abortable
Parameters:
msg - Why we're aborting.
t - Throwable that caused abort. Can be null.

isClosed

public boolean isClosed()
Specified by:
isClosed in interface HConnection
Returns:
true if this connection is closed

isAborted

public boolean isAborted()
Description copied from interface: Abortable
Check if the server or client was aborted.

Specified by:
isAborted in interface Abortable
Returns:
true if the server or client was aborted, false otherwise

getCurrentNrHRS

public int getCurrentNrHRS()
                    throws IOException
Description copied from interface: HConnection
Scan zookeeper to get the number of region servers

Specified by:
getCurrentNrHRS in interface HConnection
Returns:
the number of region servers that are currently running
Throws:
IOException - if a remote or network exception occurs

close

public void close()
Specified by:
close in interface Closeable

finalize

protected void finalize()
                 throws Throwable
Close the connection for good, regardless of what the current value of refCount is. Ideally, refCount should be zero at this point, which would be the case if all of its consumers close the connection. However, on the off chance that someone is unable to close the connection, perhaps because it bailed out prematurely, the method below will ensure that this HConnection instance is cleaned up. Caveat: The JVM may take an unknown amount of time to call finalize on an unreachable object, so our hope is that every consumer cleans up after itself, like any good citizen.

Overrides:
finalize in class Object
Throws:
Throwable

listTables

public HTableDescriptor[] listTables()
                              throws IOException
Description copied from interface: HConnection
List all the userspace tables. In other words, scan the META table. If we wanted this to be really fast, we could implement a special catalog table that just contains table names and their descriptors. Right now, it only exists as part of the META table's region info.

Specified by:
listTables in interface HConnection
Returns:
- returns an array of HTableDescriptors
Throws:
IOException - if a remote or network exception occurs

getHTableDescriptors

public HTableDescriptor[] getHTableDescriptors(List<String> tableNames)
                                        throws IOException
Specified by:
getHTableDescriptors in interface HConnection
Parameters:
tableNames - List of table names
Returns:
HTD[] table metadata
Throws:
IOException - if a remote or network exception occurs

getTableNames

public String[] getTableNames()
                       throws IOException
Specified by:
getTableNames in interface HConnection
Returns:
string[] table names
Throws:
IOException - if a remote or network exception occurs

getHTableDescriptor

public HTableDescriptor getHTableDescriptor(byte[] tableName)
                                     throws IOException
Specified by:
getHTableDescriptor in interface HConnection
Parameters:
tableName - table name
Returns:
table metadata
Throws:
IOException - if a remote or network exception occurs


Copyright © 2015 The Apache Software Foundation. All Rights Reserved.