C
- The RPC gateway counterpart for the implementing RPC endpointpublic abstract class RpcEndpoint<C extends RpcGateway> extends Object
RpcService
.
All RPC calls on the same endpoint are called by the same thread (referred to as the endpoint's main thread). Thus, by executing all state changing operations within the main thread, we don't have to reason about concurrent accesses, in the same way in the Actor Model of Erlang or Akka.
The RPC endpoint provides provides runAsync(Runnable)
, callAsync(Callable, Time)
and the getMainThreadExecutor()
to execute code in the RPC endoint's main thread.
Modifier and Type | Field and Description |
---|---|
protected org.slf4j.Logger |
log |
Modifier | Constructor and Description |
---|---|
protected |
RpcEndpoint(RpcService rpcService)
Initializes the RPC endpoint with a random endpoint id.
|
protected |
RpcEndpoint(RpcService rpcService,
String endpointId)
Initializes the RPC endpoint.
|
Modifier and Type | Method and Description |
---|---|
protected <V> Future<V> |
callAsync(Callable<V> callable,
Time timeout)
Execute the callable in the main thread of the underlying RPC service, returning a future for
the result of the callable.
|
String |
getAddress()
Gets the address of the underlying RPC endpoint.
|
String |
getEndpointId()
Returns the rpc endpoint's identifier.
|
protected Executor |
getMainThreadExecutor()
Gets the main thread execution context.
|
RpcService |
getRpcService()
Gets the endpoint's RPC service.
|
C |
getSelf()
Get self-gateway which should be used to run asynchronous RPC calls on this endpoint.
|
Class<C> |
getSelfGatewayType()
Returns the class of the self gateway type.
|
Future<Void> |
getTerminationFuture()
Return a future which is completed when the rpc endpoint has been terminated.
|
protected void |
runAsync(Runnable runnable)
Execute the runnable in the main thread of the underlying RPC endpoint.
|
protected void |
scheduleRunAsync(Runnable runnable,
long delay,
TimeUnit unit)
Execute the runnable in the main thread of the underlying RPC endpoint, with
a delay of the given number of milliseconds.
|
protected void |
scheduleRunAsync(Runnable runnable,
Time delay)
Execute the runnable in the main thread of the underlying RPC endpoint, with
a delay of the given number of milliseconds.
|
void |
shutDown()
Shuts down the underlying RPC endpoint via the RPC service.
|
void |
start()
Starts the rpc endpoint.
|
void |
validateRunsInMainThread()
Validates that the method call happens in the RPC endpoint's main thread.
|
protected RpcEndpoint(RpcService rpcService, String endpointId)
rpcService
- The RPC server that dispatches calls to this RPC endpoint.endpointId
- Unique identifier for this endpointprotected RpcEndpoint(RpcService rpcService)
rpcService
- The RPC server that dispatches calls to this RPC endpoint.public final Class<C> getSelfGatewayType()
public String getEndpointId()
public void start() throws Exception
Exception
- indicating that something went wrong while starting the RPC endpointpublic void shutDown() throws Exception
self gateway
. It will also not accepts executions in main thread
any more (via callAsync(Callable, Time)
and runAsync(Runnable)
).
This method can be overridden to add RPC endpoint specific shut down code. The overridden method should always call the parent shut down method.
Exception
- indicating that the something went wrong while shutting the RPC endpoint downpublic C getSelf()
IMPORTANT: Always issue local method calls via the self-gateway if the current thread is not the main thread of the underlying rpc server, e.g. from within a future callback.
public String getAddress()
protected Executor getMainThreadExecutor()
public RpcService getRpcService()
public Future<Void> getTerminationFuture()
protected void runAsync(Runnable runnable)
runnable
- Runnable to be executed in the main thread of the underlying RPC endpointprotected void scheduleRunAsync(Runnable runnable, Time delay)
runnable
- Runnable to be executeddelay
- The delay after which the runnable will be executedprotected void scheduleRunAsync(Runnable runnable, long delay, TimeUnit unit)
runnable
- Runnable to be executeddelay
- The delay after which the runnable will be executedprotected <V> Future<V> callAsync(Callable<V> callable, Time timeout)
TimeoutException
.V
- Return type of the callablecallable
- Callable to be executed in the main thread of the underlying rpc servertimeout
- Timeout for the callable to be completedpublic void validateRunsInMainThread()
IMPORTANT: This check only happens when assertions are enabled, such as when running tests.
This can be used for additional checks, like
protected void concurrencyCriticalMethod() {
validateRunsInMainThread();
// some critical stuff
}
Copyright © 2014–2018 The Apache Software Foundation. All rights reserved.