Search in sources :

Example 21 with TProtocol

use of org.apache.thrift.protocol.TProtocol 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 22 with TProtocol

use of org.apache.thrift.protocol.TProtocol in project carbondata by apache.

the class CarbonUtil method getByteArray.

/**
   * Below method will be used to convert the thrift object to byte array.
   */
public static byte[] getByteArray(TBase t) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    byte[] thriftByteArray = null;
    TProtocol binaryOut = new TCompactProtocol(new TIOStreamTransport(stream));
    try {
        t.write(binaryOut);
        stream.flush();
        thriftByteArray = stream.toByteArray();
    } catch (TException | IOException e) {
        closeStreams(stream);
    } finally {
        closeStreams(stream);
    }
    return thriftByteArray;
}
Also used : TException(org.apache.thrift.TException) TProtocol(org.apache.thrift.protocol.TProtocol) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol)

Example 23 with TProtocol

use of org.apache.thrift.protocol.TProtocol in project jena by apache.

the class BinRDF method inputStreamToStream.

/**
     * Decode the contents of the input stream and send to the {@link StreamRDF}.
     * @param in InputStream
     * @param dest StreamRDF
     */
public static void inputStreamToStream(InputStream in, StreamRDF dest) {
    TProtocol protocol = TRDF.protocol(in);
    protocolToStream(protocol, dest);
}
Also used : TProtocol(org.apache.thrift.protocol.TProtocol)

Example 24 with TProtocol

use of org.apache.thrift.protocol.TProtocol in project jena by apache.

the class BinRDF method streamToFile.

/** 
     * Create an {@link StreamRDF} for output.  A filename ending {@code .gz} will have
     * a gzip compressor added to the output path. A filename of "-" is {@code System.out}.
     * The file is closed when {@link StreamRDF#finish()} is called unless it is {@code System.out}.  
     * Call {@link StreamRDF#start()}...{@link StreamRDF#finish()}.
     * 
     * @param filename The file
     * @param withValues - whether to encode numeric values as values.
     * @return StreamRDF A stream to send to.
     */
public static StreamRDF streamToFile(String filename, boolean withValues) {
    OutputStream out = IO.openOutputFile(filename);
    // Is this internally buffered as well?
    BufferedOutputStream bout = new BufferedOutputStream(out, BUFSIZE_OUT);
    TProtocol protocol = TRDF.protocol(bout);
    return new StreamRDF2Thrift(protocol, withValues);
}
Also used : TProtocol(org.apache.thrift.protocol.TProtocol) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 25 with TProtocol

use of org.apache.thrift.protocol.TProtocol in project jena by apache.

the class BinRDF method fileToStream.

/**
     * Decode the contents of the file and send to the {@link StreamRDF}.
     * A filename ending {@code .gz} will have a gzip decompressor added.
     * A filename of "-" is {@code System.in}.
     * @param filename The file.
     * @param dest Sink
     */
public static void fileToStream(String filename, StreamRDF dest) {
    InputStream in = IO.openFile(filename);
    TProtocol protocol = TRDF.protocol(in);
    protocolToStream(protocol, dest);
}
Also used : TProtocol(org.apache.thrift.protocol.TProtocol) InputStream(java.io.InputStream)

Aggregations

TProtocol (org.apache.thrift.protocol.TProtocol)78 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)29 TTransport (org.apache.thrift.transport.TTransport)28 TSocket (org.apache.thrift.transport.TSocket)23 TException (org.apache.thrift.TException)22 TFramedTransport (org.apache.thrift.transport.TFramedTransport)17 IOException (java.io.IOException)15 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)10 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)10 THttpClient (org.apache.thrift.transport.THttpClient)9 TTransportException (org.apache.thrift.transport.TTransportException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 TJSONProtocol (org.apache.thrift.protocol.TJSONProtocol)7 RDF_Term (org.apache.jena.riot.thrift.wire.RDF_Term)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 TProcessor (org.apache.thrift.TProcessor)5 Hbase (org.apache.hadoop.hbase.thrift.generated.Hbase)4 TTransportFactory (org.apache.thrift.transport.TTransportFactory)4 OutputStream (java.io.OutputStream)3