@ThreadSafe public abstract class AbstractClient extends Object implements Client
AbstractClient
is the base class for all the grpc clients in alluxio.
It provides framework methods for the grpc workflow like connection, version checking
and automatic retrying for rpc calls.
Concrete Child classes extend this class by filling in the missing component, like where
to find the actual server address queryGrpcServerAddress()
, and rpc
callables to retryRPC(RpcCallable, Logger, String, String, Object...)
that carries actual logic.
Modifier and Type | Class and Description |
---|---|
protected static interface |
AbstractClient.RpcCallable<V>
|
Modifier and Type | Field and Description |
---|---|
protected boolean |
mAlwaysEnableTLS |
protected GrpcChannel |
mChannel
Underlying channel to the target service.
|
protected boolean |
mClosed
Is true if this client was closed by the user.
|
protected boolean |
mConnected
Is true if this client is currently connected.
|
protected ClientContext |
mContext
Context of the client.
|
protected GrpcServerAddress |
mServerAddress
Grpc Address of the remote server.
|
protected long |
mServiceVersion
Stores the actual remote service version, used to compare with expected local version.
|
protected ServiceVersionClientServiceGrpc.ServiceVersionClientServiceBlockingStub |
mVersionService |
Modifier | Constructor and Description |
---|---|
protected |
AbstractClient(ClientContext context)
Creates a new client base with default retry policy supplier.
|
protected |
AbstractClient(ClientContext context,
boolean alwaysEnableTLS) |
protected |
AbstractClient(ClientContext context,
java.util.function.Supplier<RetryPolicy> retryPolicySupplier)
Creates a new client base with specified retry policy supplier.
|
Modifier and Type | Method and Description |
---|---|
protected void |
afterConnect()
This method is called after the connection is made to the remote.
|
protected void |
afterDisconnect()
This method is called after the connection is disconnected.
|
protected void |
beforeConnect()
This method is called before the connection is connected.
|
protected void |
beforeDisconnect()
This method is called before the connection is disconnected.
|
protected void |
checkVersion(long clientVersion)
Checks that the service version is compatible with the client.
|
void |
close()
Closes the connection with the remote permanently.
|
void |
connect()
Connects with the remote.
|
void |
connectWithRuntimeException()
Connects with the remote server.
|
protected GrpcChannel |
createChannel() |
void |
disconnect()
Closes the connection with the Alluxio remote and does the necessary cleanup.
|
InetSocketAddress |
getConfAddress()
By default, return the same underlying address as
getRemoteSockAddress() . |
String |
getRemoteHostName() |
protected abstract ServiceType |
getRemoteServiceType() |
protected long |
getRemoteServiceVersion() |
SocketAddress |
getRemoteSockAddress() |
protected abstract String |
getServiceName() |
protected abstract long |
getServiceVersion() |
boolean |
isClosed() |
boolean |
isConnected()
Returns the connected status of the client.
|
protected abstract GrpcServerAddress |
queryGrpcServerAddress()
AbstractClient works with Grpc Servers. |
protected <V> V |
retryRPC(AbstractClient.RpcCallable<V> rpc,
org.slf4j.Logger logger,
String rpcName,
String description,
Object... args)
Tries to execute an RPC defined as a
AbstractClient.RpcCallable . |
protected <V> V |
retryRPC(RetryPolicy retryPolicy,
AbstractClient.RpcCallable<V> rpc,
org.slf4j.Logger logger,
String rpcName,
String description,
Object... args) |
protected GrpcServerAddress mServerAddress
queryGrpcServerAddress()
,
and could only be null right after instantiation and before use.protected GrpcChannel mChannel
protected ServiceVersionClientServiceGrpc.ServiceVersionClientServiceBlockingStub mVersionService
protected boolean mAlwaysEnableTLS
protected boolean mConnected
protected volatile boolean mClosed
protected long mServiceVersion
protected ClientContext mContext
protected AbstractClient(ClientContext context)
context
- information required to connect to Alluxioprotected AbstractClient(ClientContext context, boolean alwaysEnableTLS)
protected AbstractClient(ClientContext context, java.util.function.Supplier<RetryPolicy> retryPolicySupplier)
context
- information required to connect to AlluxioretryPolicySupplier
- factory for retry policies to be used when performing RPCsprotected abstract ServiceType getRemoteServiceType()
protected long getRemoteServiceVersion() throws AlluxioStatusException
AlluxioStatusException
- if query rpc failedprotected abstract String getServiceName()
protected abstract long getServiceVersion()
protected void checkVersion(long clientVersion) throws IOException
clientVersion
- the client versionIOException
protected void afterConnect() throws IOException
IOException
protected void beforeConnect() throws IOException
IOException
protected void afterDisconnect()
protected void beforeDisconnect()
public void connectWithRuntimeException()
AlluxioRuntimeException
- network connection errorprotected GrpcChannel createChannel() throws AlluxioStatusException
AlluxioStatusException
public void connect() throws AlluxioStatusException
connect
in interface Client
AlluxioStatusException
public void disconnect()
Client
disconnect
in interface Client
public boolean isConnected()
Client
isConnected
in interface Client
public void close()
close
in interface Closeable
close
in interface AutoCloseable
protected abstract GrpcServerAddress queryGrpcServerAddress() throws UnavailableException
AbstractClient
works with Grpc Servers.
Child classes should only override this method to query the address
of the grpc server they talk to. The conversion from GrpcServerAddress
to more generic SocketAddress
required by Client.getRemoteSockAddress()
is handled by this class.GrpcServerAddress
of the remote serverUnavailableException
- if address can't be determinedpublic SocketAddress getRemoteSockAddress() throws UnavailableException
getRemoteSockAddress
in interface Client
SocketAddress
of the remoteUnavailableException
- if the primary address cannot be determinedpublic String getRemoteHostName() throws UnavailableException
getRemoteHostName
in interface Client
UnavailableException
- if the primary address cannot be determinedpublic InetSocketAddress getConfAddress() throws UnavailableException
getRemoteSockAddress()
.
Child classes should override this implementation if they intend to have different
address to fetch configuration.getConfAddress
in interface Client
UnavailableException
- if address cannot be determinedprotected <V> V retryRPC(AbstractClient.RpcCallable<V> rpc, org.slf4j.Logger logger, String rpcName, String description, Object... args) throws AlluxioStatusException
AbstractClient.RpcCallable
. Metrics will be recorded based on
the provided rpc name.
If a UnavailableException
occurs, a reconnection will be tried through
connect()
and the action will be re-executed.V
- type of return value of the RPC callrpc
- the RPC call to be executedlogger
- the logger to use for this callrpcName
- the human-readable name of the RPC calldescription
- the format string of the description, used for loggingargs
- the arguments for the descriptionAlluxioStatusException
- status exceptionprotected <V> V retryRPC(RetryPolicy retryPolicy, AbstractClient.RpcCallable<V> rpc, org.slf4j.Logger logger, String rpcName, String description, Object... args) throws AlluxioStatusException
AlluxioStatusException
Copyright © 2023. All Rights Reserved.