public abstract class RpcEndpoint extends Object implements RpcGateway
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 endpoint's main thread.
Modifier and Type | Class and Description |
---|---|
protected static class |
RpcEndpoint.MainThreadExecutor
Executor which executes runnables in the main thread context.
|
Modifier and Type | Field and Description |
---|---|
protected org.slf4j.Logger |
log |
protected RpcServer |
rpcServer
Interface to access the underlying rpc server.
|
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> CompletableFuture<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.
|
String |
getHostname()
Gets the hostname of the underlying RPC endpoint.
|
protected RpcEndpoint.MainThreadExecutor |
getMainThreadExecutor()
Gets the main thread execution context.
|
RpcService |
getRpcService()
Gets the endpoint's RPC service.
|
<C extends RpcGateway> |
getSelfGateway(Class<C> selfGatewayType)
Returns a self gateway of the specified type which can be used to issue asynchronous
calls against the RpcEndpoint.
|
CompletableFuture<Void> |
getTerminationFuture()
Return a future which is completed with true when the rpc endpoint has been terminated.
|
abstract CompletableFuture<Void> |
postStop()
User overridable callback.
|
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()
Triggers the shut down of the rpc endpoint.
|
void |
start()
Starts the rpc endpoint.
|
protected void |
stop()
Stops the rpc endpoint.
|
void |
validateRunsInMainThread()
Validates that the method call happens in the RPC endpoint's main thread.
|
protected final org.slf4j.Logger log
protected final RpcServer rpcServer
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 String getEndpointId()
public void start() throws Exception
IMPORTANT: Whenever you override this method, call the parent implementation to enable rpc processing. It is advised to make the parent call last.
Exception
- indicating that something went wrong while starting the RPC endpointprotected final void stop()
public abstract CompletableFuture<Void> postStop()
This method is called when the RpcEndpoint is being shut down. The method is guaranteed to be executed in the main thread context and can be used to clean up internal state.
IMPORTANT: This method should never be called directly by the user.
public final void shutDown()
In order to wait on the completion of the shut down, obtain the termination future
via getTerminationFuture()
} and wait on its completion.
public <C extends RpcGateway> C getSelfGateway(Class<C> selfGatewayType)
IMPORTANT: The self gateway type must be implemented by the RpcEndpoint. Otherwise the method will fail.
C
- type of the self gateway to createselfGatewayType
- class of the self gateway typepublic String getAddress()
getAddress
in interface RpcGateway
public String getHostname()
getHostname
in interface RpcGateway
protected RpcEndpoint.MainThreadExecutor getMainThreadExecutor()
public RpcService getRpcService()
public CompletableFuture<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> CompletableFuture<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–2019 The Apache Software Foundation. All rights reserved.