Package alluxio
Class AbstractClient
- java.lang.Object
-
- alluxio.AbstractClient
-
- All Implemented Interfaces:
Client
,java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
AbstractMasterClient
@ThreadSafe public abstract class AbstractClient extends java.lang.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 toretryRPC(RpcCallable, Logger, String, String, Object...)
that carries actual logic.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static interface
AbstractClient.RpcCallable<V>
-
Field Summary
Fields Modifier and Type Field Description 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 alluxio.grpc.ServiceVersionClientServiceGrpc.ServiceVersionClientServiceBlockingStub
mVersionService
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractClient(ClientContext context)
Creates a new client base with default retry policy supplier.protected
AbstractClient(ClientContext context, java.util.function.Supplier<RetryPolicy> retryPolicySupplier)
Creates a new client base with specified retry policy supplier.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method 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.void
disconnect()
Closes the connection with the Alluxio remote and does the necessary cleanup.java.net.InetSocketAddress
getConfAddress()
By default, return the same underlying address asgetRemoteSockAddress()
.java.lang.String
getRemoteHostName()
protected abstract alluxio.grpc.ServiceType
getRemoteServiceType()
protected long
getRemoteServiceVersion()
java.net.SocketAddress
getRemoteSockAddress()
protected abstract java.lang.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, java.lang.String rpcName, java.lang.String description, java.lang.Object... args)
Tries to execute an RPC defined as aAbstractClient.RpcCallable
.protected <V> V
retryRPC(RetryPolicy retryPolicy, AbstractClient.RpcCallable<V> rpc, org.slf4j.Logger logger, java.lang.String rpcName, java.lang.String description, java.lang.Object... args)
-
-
-
Field Detail
-
mServerAddress
protected GrpcServerAddress mServerAddress
Grpc Address of the remote server. This field is lazily initialized byqueryGrpcServerAddress()
, and could only be null right after instantiation and before use.
-
mChannel
protected GrpcChannel mChannel
Underlying channel to the target service.
-
mVersionService
protected alluxio.grpc.ServiceVersionClientServiceGrpc.ServiceVersionClientServiceBlockingStub mVersionService
-
mConnected
protected boolean mConnected
Is true if this client is currently connected.
-
mClosed
protected volatile boolean mClosed
Is true if this client was closed by the user. No further actions are possible after the client is closed.
-
mServiceVersion
protected long mServiceVersion
Stores the actual remote service version, used to compare with expected local version.
-
mContext
protected ClientContext mContext
Context of the client.
-
-
Constructor Detail
-
AbstractClient
protected AbstractClient(ClientContext context)
Creates a new client base with default retry policy supplier.- Parameters:
context
- information required to connect to Alluxio
-
AbstractClient
protected AbstractClient(ClientContext context, java.util.function.Supplier<RetryPolicy> retryPolicySupplier)
Creates a new client base with specified retry policy supplier.- Parameters:
context
- information required to connect to AlluxioretryPolicySupplier
- factory for retry policies to be used when performing RPCs
-
-
Method Detail
-
getRemoteServiceType
protected abstract alluxio.grpc.ServiceType getRemoteServiceType()
- Returns:
- the expected type of remote service
-
getRemoteServiceVersion
protected long getRemoteServiceVersion() throws AlluxioStatusException
- Returns:
- the actual remote service version
- Throws:
AlluxioStatusException
- if query rpc failed
-
getServiceName
protected abstract java.lang.String getServiceName()
- Returns:
- a string representing the specific service
-
getServiceVersion
protected abstract long getServiceVersion()
- Returns:
- the client service version
-
checkVersion
protected void checkVersion(long clientVersion) throws java.io.IOException
Checks that the service version is compatible with the client.- Parameters:
clientVersion
- the client version- Throws:
java.io.IOException
-
afterConnect
protected void afterConnect() throws java.io.IOException
This method is called after the connection is made to the remote. Implementations should create internal state to finish the connection process.- Throws:
java.io.IOException
-
beforeConnect
protected void beforeConnect() throws java.io.IOException
This method is called before the connection is connected. Implementations should add any additional operations before the connection is connected. loading the cluster defaults- Throws:
java.io.IOException
-
afterDisconnect
protected void afterDisconnect()
This method is called after the connection is disconnected. Implementations should clean up any additional state created for the connection.
-
beforeDisconnect
protected void beforeDisconnect()
This method is called before the connection is disconnected. Implementations should add any additional operations before the connection is disconnected.
-
connectWithRuntimeException
public void connectWithRuntimeException()
Connects with the remote server.- Throws:
AlluxioRuntimeException
- network connection error
-
connect
public void connect() throws AlluxioStatusException
Connects with the remote.- Specified by:
connect
in interfaceClient
- Throws:
AlluxioStatusException
-
disconnect
public void disconnect()
Description copied from interface:Client
Closes the connection with the Alluxio remote and does the necessary cleanup. It should be used if the client has not connected with the remote for a while, for example.- Specified by:
disconnect
in interfaceClient
-
isConnected
public boolean isConnected()
Description copied from interface:Client
Returns the connected status of the client.- Specified by:
isConnected
in interfaceClient
- Returns:
- true if this client is connected to the remote
-
close
public void close()
Closes the connection with the remote permanently. This instance should be not be reused after closing.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
queryGrpcServerAddress
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 fromGrpcServerAddress
to more genericSocketAddress
required byClient.getRemoteSockAddress()
is handled by this class.- Returns:
- the
GrpcServerAddress
of the remote server - Throws:
UnavailableException
- if address can't be determined
-
getRemoteSockAddress
public java.net.SocketAddress getRemoteSockAddress() throws UnavailableException
- Specified by:
getRemoteSockAddress
in interfaceClient
- Returns:
- the
SocketAddress
of the remote - Throws:
UnavailableException
- if the primary address cannot be determined
-
getRemoteHostName
public java.lang.String getRemoteHostName() throws UnavailableException
- Specified by:
getRemoteHostName
in interfaceClient
- Returns:
- the host name of the remote
- Throws:
UnavailableException
- if the primary address cannot be determined
-
getConfAddress
public java.net.InetSocketAddress getConfAddress() throws UnavailableException
By default, return the same underlying address asgetRemoteSockAddress()
. Child classes should override this implementation if they intend to have different address to fetch configuration.- Specified by:
getConfAddress
in interfaceClient
- Returns:
- the remote address of the configuration server
- Throws:
UnavailableException
- if address cannot be determined
-
retryRPC
protected <V> V retryRPC(AbstractClient.RpcCallable<V> rpc, org.slf4j.Logger logger, java.lang.String rpcName, java.lang.String description, java.lang.Object... args) throws AlluxioStatusException
Tries to execute an RPC defined as aAbstractClient.RpcCallable
. Metrics will be recorded based on the provided rpc name. If aUnavailableException
occurs, a reconnection will be tried throughconnect()
and the action will be re-executed.- Type Parameters:
V
- type of return value of the RPC call- Parameters:
rpc
- 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 description- Returns:
- the return value of the RPC call
- Throws:
AlluxioStatusException
- status exception
-
retryRPC
protected <V> V retryRPC(RetryPolicy retryPolicy, AbstractClient.RpcCallable<V> rpc, org.slf4j.Logger logger, java.lang.String rpcName, java.lang.String description, java.lang.Object... args) throws AlluxioStatusException
- Throws:
AlluxioStatusException
-
-