use of org.apache.thrift.protocol.TMultiplexedProtocol in project alluxio by Alluxio.
the class ThriftClientPool method createNewResource.
/**
* Creates a thrift client instance.
*
* @return the thrift client created
* @throws IOException if it fails to create a thrift client
*/
@Override
protected T createNewResource() throws IOException {
TTransport transport = mTransportProvider.getClientTransport(mParentSubject, mAddress);
TProtocol binaryProtocol = new TBinaryProtocol(transport);
T client = createThriftClient(new TMultiplexedProtocol(binaryProtocol, mServiceName));
TException exception;
RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_MS, MAX_SLEEP_MS, RPC_MAX_NUM_RETRY);
do {
LOG.info("Alluxio client (version {}) is trying to connect with {} {} @ {}", RuntimeConstants.VERSION, mServiceName, mAddress);
try {
if (!transport.isOpen()) {
transport.open();
}
if (transport.isOpen()) {
checkVersion(client);
}
LOG.info("Client registered with {} @ {}", mServiceName, mAddress);
return client;
} catch (TTransportException e) {
if (e.getCause() instanceof java.net.SocketTimeoutException) {
// Do not retry if socket timeout.
String message = "Thrift transport open times out. Please check whether the " + "authentication types match between client and server. Note that NOSASL client " + "is not able to connect to servers with SIMPLE security mode.";
throw new IOException(message, e);
}
LOG.warn("Failed to connect ({}) to {} @ {}: {}", retryPolicy.getRetryCount(), mServiceName, mAddress, e.getMessage());
exception = e;
}
} while (retryPolicy.attemptRetry());
LOG.error("Failed after " + retryPolicy.getRetryCount() + " retries.");
Preconditions.checkNotNull(exception);
throw new IOException(exception);
}
use of org.apache.thrift.protocol.TMultiplexedProtocol in project alluxio by Alluxio.
the class AbstractClient method connect.
/**
* Connects with the remote.
*
* @throws IOException if an I/O error occurs
* @throws ConnectionFailedException if network connection failed
*/
public synchronized void connect() throws IOException, ConnectionFailedException {
if (mConnected) {
return;
}
disconnect();
Preconditions.checkState(!mClosed, "Client is closed, will not try to connect.");
RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_MS, MAX_SLEEP_MS, RPC_MAX_NUM_RETRY);
while (!mClosed) {
mAddress = getAddress();
LOG.info("Alluxio client (version {}) is trying to connect with {} {} @ {}", RuntimeConstants.VERSION, getServiceName(), mMode, mAddress);
TProtocol binaryProtocol = new TBinaryProtocol(mTransportProvider.getClientTransport(mParentSubject, mAddress));
mProtocol = new TMultiplexedProtocol(binaryProtocol, getServiceName());
try {
mProtocol.getTransport().open();
LOG.info("Client registered with {} {} @ {}", getServiceName(), mMode, mAddress);
mConnected = true;
afterConnect();
checkVersion(getClient(), getServiceVersion());
return;
} catch (IOException e) {
if (e.getMessage() != null && FRAME_SIZE_EXCEPTION_PATTERN.matcher(e.getMessage()).find()) {
// See an error like "Frame size (67108864) larger than max length (16777216)!",
// pointing to the helper page.
String message = String.format("Failed to connect to %s %s @ %s: %s. " + "This exception may be caused by incorrect network configuration. " + "Please consult %s for common solutions to address this problem.", getServiceName(), mMode, mAddress, e.getMessage(), RuntimeConstants.ALLUXIO_DEBUG_DOCS_URL);
throw new IOException(message, e);
}
throw e;
} catch (TTransportException e) {
LOG.warn("Failed to connect ({}) to {} {} @ {}: {}", retryPolicy.getRetryCount(), getServiceName(), mMode, mAddress, e.getMessage());
if (e.getCause() instanceof java.net.SocketTimeoutException) {
// Do not retry if socket timeout.
String message = "Thrift transport open times out. Please check whether the " + "authentication types match between client and server. Note that NOSASL client " + "is not able to connect to servers with SIMPLE security mode.";
throw new IOException(message, e);
}
// TODO(peis): Consider closing the connection here as well.
if (!retryPolicy.attemptRetry()) {
break;
}
}
}
// Reaching here indicates that we did not successfully connect.
throw new ConnectionFailedException("Failed to connect to " + getServiceName() + " " + mMode + " @ " + mAddress + " after " + (retryPolicy.getRetryCount()) + " attempts");
}
Aggregations