org.apache.hadoop.hbase.ipc
Class RpcServer

java.lang.Object
  extended by org.apache.hadoop.hbase.ipc.RpcServer
All Implemented Interfaces:
RpcServerInterface

@InterfaceAudience.LimitedPrivate(value={"Coprocesssor","Phoenix"})
@InterfaceStability.Evolving
public class RpcServer
extends Object
implements RpcServerInterface

An RPC server that hosts protobuf described Services. An RpcServer instance has a Listener that hosts the socket. Listener has fixed number of Readers in an ExecutorPool, 10 by default. The Listener does an accept and then round robin a Reader is chosen to do the read. The reader is registered on Selector. Read does total read off the channel and the parse from which it makes a Call. The call is wrapped in a CallRunner and passed to the scheduler to be run. Reader goes back to see if more to be done and loops till done.

Scheduler can be variously implemented but default simple scheduler has handlers to which it has given the queues into which calls (i.e. CallRunner instances) are inserted. Handlers run taking from the queue. They run the CallRunner#run method on each item gotten from queue and keep taking while the server is up. CallRunner#run executes the call. When done, asks the included Call to put itself on new queue for RpcServer.Responder to pull from and return result to client.

See Also:
RpcClient

Nested Class Summary
static class RpcServer.BlockingServiceAndInterface
          Datastructure for passing a BlockingService and its associated class of protobuf service interface.
static class RpcServer.CallQueueTooBigException
           
 class RpcServer.Connection
          Reads calls from a connection and queues them for handling.
protected  class RpcServer.Responder
           
 
Field Summary
protected  org.apache.hadoop.security.authorize.ServiceAuthorizationManager authManager
           
protected  AuthenticationTokenSecretManager authTokenSecretMgr
           
protected  org.cliffc.high_scale_lib.Counter callQueueSize
          This is a running count of the size of all outstanding calls by size.
protected  org.apache.hadoop.conf.Configuration conf
           
protected  List<RpcServer.Connection> connectionList
           
protected static ThreadLocal<org.apache.hadoop.hbase.ipc.RpcServer.Call> CurCall
          This is set to Call object before Handler invokes an RPC and ybdie after the call returns.
static byte CURRENT_VERSION
           
protected  HBaseRPCErrorHandler errorHandler
           
protected  InetSocketAddress isa
           
static org.apache.commons.logging.Log LOG
           
protected  int maxIdleTime
           
protected  MetricsHBaseServer metrics
           
protected  int numConnections
           
protected  int port
           
protected  long purgeTimeout
           
protected  RpcServer.Responder responder
           
protected  org.apache.hadoop.security.token.SecretManager<org.apache.hadoop.security.token.TokenIdentifier> secretManager
           
protected  int socketSendBufferSize
           
protected  boolean tcpKeepAlive
           
protected  boolean tcpNoDelay
           
protected  int thresholdIdleConnections
           
 
Constructor Summary
RpcServer(Server serverInstance, String name, List<RpcServer.BlockingServiceAndInterface> services, InetSocketAddress isa, org.apache.hadoop.conf.Configuration conf, RpcScheduler scheduler)
          Constructs a server listening on the named port and address.
 
Method Summary
 void addCallSize(long diff)
          Add/subtract from the current size of all outstanding calls.
 void authorize(org.apache.hadoop.security.UserGroupInformation user, RPCProtos.ConnectionHeader connection, InetAddress addr)
          Authorize the incoming client connection.
static void bind(ServerSocket socket, InetSocketAddress address, int backlog)
          A convenience method to bind to a given address and report better exceptions if the address is not a valid host.
 Pair<com.google.protobuf.Message,CellScanner> call(com.google.protobuf.BlockingService service, com.google.protobuf.Descriptors.MethodDescriptor md, com.google.protobuf.Message param, CellScanner cellScanner, long receiveTime, MonitoredRPCHandler status)
          This is a server side method, which is invoked over RPC.
protected  int channelRead(ReadableByteChannel channel, ByteBuffer buffer)
          This is a wrapper around ReadableByteChannel.read(java.nio.ByteBuffer).
protected  long channelWrite(GatheringByteChannel channel, org.apache.hadoop.hbase.ipc.BufferChain bufferChain)
          This is a wrapper around WritableByteChannel.write(java.nio.ByteBuffer).
protected  void closeConnection(RpcServer.Connection connection)
           
protected  RpcServer.Connection getConnection(SocketChannel channel, long time)
          Subclasses of HBaseServer can override this to provide their own Connection implementations.
static RpcCallContext getCurrentCall()
          Needed for features such as delayed calls.
 HBaseRPCErrorHandler getErrorHandler()
           
 InetSocketAddress getListenerAddress()
          Return the socket (ip+port) on which the RPC server is listening to.
 MetricsHBaseServer getMetrics()
          Returns the metrics instance for reporting RPC call statistics
static InetAddress getRemoteAddress()
           
static InetAddress getRemoteIp()
          Returns the remote side ip address when invoked inside an RPC Returns null incase of an error.
static User getRequestUser()
          Returns the user credentials associated with the current RPC request or null if no credentials were provided.
static String getRequestUserName()
          Returns the username for any user associated with the current RPC request or null if no user is set.
 RpcScheduler getScheduler()
           
 org.apache.hadoop.security.token.SecretManager<? extends org.apache.hadoop.security.token.TokenIdentifier> getSecretManager()
           
static boolean isInRpcCallContext()
           
 boolean isStarted()
           
 void join()
          Wait for the server to be stopped.
 void openServer()
          Open a previously started server.
 void refreshAuthManager(org.apache.hadoop.security.authorize.PolicyProvider pp)
          Refresh authentication manager policy.
 void setErrorHandler(HBaseRPCErrorHandler handler)
          Set the handler for calling out of RPC for error conditions.
 void setSecretManager(org.apache.hadoop.security.token.SecretManager<? extends org.apache.hadoop.security.token.TokenIdentifier> secretManager)
           
 void setSocketSendBufSize(int size)
          Sets the socket buffer size used for responding to RPCs.
 void start()
          Starts the service.
 void startThreads()
          Starts the service threads but does not allow requests to be responded yet.
 void stop()
          Stops the service.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

public static final org.apache.commons.logging.Log LOG

CURRENT_VERSION

public static final byte CURRENT_VERSION
See Also:
Constant Field Values

secretManager

protected org.apache.hadoop.security.token.SecretManager<org.apache.hadoop.security.token.TokenIdentifier> secretManager

authManager

protected org.apache.hadoop.security.authorize.ServiceAuthorizationManager authManager

CurCall

protected static final ThreadLocal<org.apache.hadoop.hbase.ipc.RpcServer.Call> CurCall
This is set to Call object before Handler invokes an RPC and ybdie after the call returns.


isa

protected final InetSocketAddress isa

port

protected int port

maxIdleTime

protected int maxIdleTime

thresholdIdleConnections

protected int thresholdIdleConnections

metrics

protected MetricsHBaseServer metrics

conf

protected final org.apache.hadoop.conf.Configuration conf

socketSendBufferSize

protected int socketSendBufferSize

tcpNoDelay

protected final boolean tcpNoDelay

tcpKeepAlive

protected final boolean tcpKeepAlive

purgeTimeout

protected final long purgeTimeout

callQueueSize

protected final org.cliffc.high_scale_lib.Counter callQueueSize
This is a running count of the size of all outstanding calls by size.


connectionList

protected final List<RpcServer.Connection> connectionList

responder

protected RpcServer.Responder responder

authTokenSecretMgr

protected AuthenticationTokenSecretManager authTokenSecretMgr

numConnections

protected int numConnections

errorHandler

protected HBaseRPCErrorHandler errorHandler
Constructor Detail

RpcServer

public RpcServer(Server serverInstance,
                 String name,
                 List<RpcServer.BlockingServiceAndInterface> services,
                 InetSocketAddress isa,
                 org.apache.hadoop.conf.Configuration conf,
                 RpcScheduler scheduler)
          throws IOException
Constructs a server listening on the named port and address.

Parameters:
serverInstance - hosting instance of Server. We will do authentications if an instance else pass null for no authentication check.
name - Used keying this rpc servers' metrics and for naming the Listener thread.
services - A list of services.
isa - Where to listen
conf -
Throws:
IOException
Method Detail

getConnection

protected RpcServer.Connection getConnection(SocketChannel channel,
                                             long time)
Subclasses of HBaseServer can override this to provide their own Connection implementations.


closeConnection

protected void closeConnection(RpcServer.Connection connection)

setSocketSendBufSize

public void setSocketSendBufSize(int size)
Sets the socket buffer size used for responding to RPCs.

Specified by:
setSocketSendBufSize in interface RpcServerInterface
Parameters:
size - send size

start

public void start()
Starts the service. Must be called before any calls will be handled.

Specified by:
start in interface RpcServerInterface

openServer

public void openServer()
Open a previously started server.

Specified by:
openServer in interface RpcServerInterface

isStarted

public boolean isStarted()
Specified by:
isStarted in interface RpcServerInterface

startThreads

public void startThreads()
Starts the service threads but does not allow requests to be responded yet. Client will get ServerNotRunningYetException instead.

Specified by:
startThreads in interface RpcServerInterface

refreshAuthManager

public void refreshAuthManager(org.apache.hadoop.security.authorize.PolicyProvider pp)
Description copied from interface: RpcServerInterface
Refresh authentication manager policy.

Specified by:
refreshAuthManager in interface RpcServerInterface

getSecretManager

public org.apache.hadoop.security.token.SecretManager<? extends org.apache.hadoop.security.token.TokenIdentifier> getSecretManager()

setSecretManager

public void setSecretManager(org.apache.hadoop.security.token.SecretManager<? extends org.apache.hadoop.security.token.TokenIdentifier> secretManager)

call

public Pair<com.google.protobuf.Message,CellScanner> call(com.google.protobuf.BlockingService service,
                                                          com.google.protobuf.Descriptors.MethodDescriptor md,
                                                          com.google.protobuf.Message param,
                                                          CellScanner cellScanner,
                                                          long receiveTime,
                                                          MonitoredRPCHandler status)
                                                   throws IOException
This is a server side method, which is invoked over RPC. On success the return response has protobuf response payload. On failure, the exception name and the stack trace are returned in the protobuf response.

Specified by:
call in interface RpcServerInterface
Throws:
IOException

stop

public void stop()
Stops the service. No new calls will be handled after this is called.

Specified by:
stop in interface RpcServerInterface

join

public void join()
          throws InterruptedException
Wait for the server to be stopped. Does not wait for all subthreads to finish. See stop().

Specified by:
join in interface RpcServerInterface
Throws:
InterruptedException - e

getListenerAddress

public InetSocketAddress getListenerAddress()
Return the socket (ip+port) on which the RPC server is listening to.

Specified by:
getListenerAddress in interface RpcServerInterface
Returns:
the socket (ip+port) on which the RPC server is listening to.

setErrorHandler

public void setErrorHandler(HBaseRPCErrorHandler handler)
Set the handler for calling out of RPC for error conditions.

Specified by:
setErrorHandler in interface RpcServerInterface
Parameters:
handler - the handler implementation

getErrorHandler

public HBaseRPCErrorHandler getErrorHandler()
Specified by:
getErrorHandler in interface RpcServerInterface

getMetrics

public MetricsHBaseServer getMetrics()
Returns the metrics instance for reporting RPC call statistics

Specified by:
getMetrics in interface RpcServerInterface

addCallSize

public void addCallSize(long diff)
Description copied from interface: RpcServerInterface
Add/subtract from the current size of all outstanding calls. Called on setup of a call to add call total size and then again at end of a call to remove the call size.

Specified by:
addCallSize in interface RpcServerInterface
Parameters:
diff - Change (plus or minus)

authorize

public void authorize(org.apache.hadoop.security.UserGroupInformation user,
                      RPCProtos.ConnectionHeader connection,
                      InetAddress addr)
               throws org.apache.hadoop.security.authorize.AuthorizationException
Authorize the incoming client connection.

Parameters:
user - client user
connection - incoming connection
addr - InetAddress of incoming connection
Throws:
org.apache.hadoop.security.authorize.AuthorizationException - when the client isn't authorized to talk the protocol

channelWrite

protected long channelWrite(GatheringByteChannel channel,
                            org.apache.hadoop.hbase.ipc.BufferChain bufferChain)
                     throws IOException
This is a wrapper around WritableByteChannel.write(java.nio.ByteBuffer). If the amount of data is large, it writes to channel in smaller chunks. This is to avoid jdk from creating many direct buffers as the size of buffer increases. This also minimizes extra copies in NIO layer as a result of multiple write operations required to write a large buffer.

Parameters:
channel - writable byte channel to write to
bufferChain - Chain of buffers to write
Returns:
number of bytes written
Throws:
IOException - e
See Also:
WritableByteChannel.write(java.nio.ByteBuffer)

channelRead

protected int channelRead(ReadableByteChannel channel,
                          ByteBuffer buffer)
                   throws IOException
This is a wrapper around ReadableByteChannel.read(java.nio.ByteBuffer). If the amount of data is large, it writes to channel in smaller chunks. This is to avoid jdk from creating many direct buffers as the size of ByteBuffer increases. There should not be any performance degredation.

Parameters:
channel - writable byte channel to write on
buffer - buffer to write
Returns:
number of bytes written
Throws:
IOException - e
See Also:
ReadableByteChannel.read(java.nio.ByteBuffer)

getCurrentCall

public static RpcCallContext getCurrentCall()
Needed for features such as delayed calls. We need to be able to store the current call so that we can complete it later or ask questions of what is supported by the current ongoing call.

Returns:
An RpcCallConext backed by the currently ongoing call (gotten from a thread local)

isInRpcCallContext

public static boolean isInRpcCallContext()

getRequestUser

public static User getRequestUser()
Returns the user credentials associated with the current RPC request or null if no credentials were provided.

Returns:
A User

getRequestUserName

public static String getRequestUserName()
Returns the username for any user associated with the current RPC request or null if no user is set.


getRemoteAddress

public static InetAddress getRemoteAddress()
Returns:
Address of remote client if a request is ongoing, else null

getRemoteIp

public static InetAddress getRemoteIp()
Returns the remote side ip address when invoked inside an RPC Returns null incase of an error.

Returns:
InetAddress

bind

public static void bind(ServerSocket socket,
                        InetSocketAddress address,
                        int backlog)
                 throws IOException
A convenience method to bind to a given address and report better exceptions if the address is not a valid host.

Parameters:
socket - the socket to bind
address - the address to bind to
backlog - the number of connections allowed in the queue
Throws:
BindException - if the address can't be bound
UnknownHostException - if the address isn't a valid host name
IOException - other random errors from bind

getScheduler

public RpcScheduler getScheduler()


Copyright © 2007–2015 The Apache Software Foundation. All rights reserved.