Search in sources :

Example 1 with TMultiplexedProtocol

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);
}
Also used : TException(org.apache.thrift.TException) ExponentialBackoffRetry(alluxio.retry.ExponentialBackoffRetry) TTransportException(org.apache.thrift.transport.TTransportException) IOException(java.io.IOException) TMultiplexedProtocol(org.apache.thrift.protocol.TMultiplexedProtocol) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TTransport(org.apache.thrift.transport.TTransport) RetryPolicy(alluxio.retry.RetryPolicy)

Example 2 with TMultiplexedProtocol

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");
}
Also used : TMultiplexedProtocol(org.apache.thrift.protocol.TMultiplexedProtocol) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) ExponentialBackoffRetry(alluxio.retry.ExponentialBackoffRetry) TTransportException(org.apache.thrift.transport.TTransportException) IOException(java.io.IOException) ThriftIOException(alluxio.thrift.ThriftIOException) RetryPolicy(alluxio.retry.RetryPolicy) ConnectionFailedException(alluxio.exception.ConnectionFailedException)

Aggregations

ExponentialBackoffRetry (alluxio.retry.ExponentialBackoffRetry)2 RetryPolicy (alluxio.retry.RetryPolicy)2 IOException (java.io.IOException)2 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)2 TMultiplexedProtocol (org.apache.thrift.protocol.TMultiplexedProtocol)2 TProtocol (org.apache.thrift.protocol.TProtocol)2 TTransportException (org.apache.thrift.transport.TTransportException)2 ConnectionFailedException (alluxio.exception.ConnectionFailedException)1 ThriftIOException (alluxio.thrift.ThriftIOException)1 TException (org.apache.thrift.TException)1 TTransport (org.apache.thrift.transport.TTransport)1