Example usage for org.apache.thrift.transport TTransportException getType

List of usage examples for org.apache.thrift.transport TTransportException getType

Introduction

In this page you can find the example usage for org.apache.thrift.transport TTransportException getType.

Prototype

public int getType() 

Source Link

Usage

From source file:alluxio.network.thrift.BootstrapServerTransport.java

License:Apache License

@Override
public void open() throws TTransportException {
    LOG.debug("opening server transport");
    if (!mBaseTransport.isOpen()) {
        mBaseTransport.open();/*from ww w . j  av a2 s .co  m*/
    }
    byte[] messageHeader = new byte[BOOTSTRAP_HEADER.length];
    int bytes;
    try {
        bytes = mBaseTransport.peek(messageHeader, 0, BOOTSTRAP_HEADER.length);
    } catch (TTransportException e) {
        if (e.getType() == TTransportException.END_OF_FILE) {
            LOG.debug("No data in the stream {}", mBaseTransport);
            mBaseTransport.close();
            throw new TTransportException("No data in the stream.");
        }
        throw e;
    }

    if (bytes == BOOTSTRAP_HEADER.length && Arrays.equals(messageHeader, BOOTSTRAP_HEADER)) {
        mBaseTransport.consumeBuffer(BOOTSTRAP_HEADER.length);
        mTransport = mBaseTransport;
    } else {
        mTransport = mTransportFactory.getTransport(mBaseTransport);
    }
    if (!mTransport.isOpen()) {
        mTransport.open();
    }
}

From source file:com.cloudera.recordservice.core.ThriftUtils.java

License:Apache License

/**
 * Connects to a thrift service running at hostname/port, returning a TTransport
 * object to that service. If kerberosPrincipal is not null, the connection will
 * be kerberized. If delegationToken is not null, we will authenticate using
 * delegation tokens.//w  w  w  .  j  a  va  2s .  c o m
 */
static TTransport createTransport(String service, String hostname, int port, String kerberosPrincipal,
        DelegationToken token, int timeoutMs) throws IOException {
    if (kerberosPrincipal != null && token != null) {
        throw new IllegalArgumentException("Cannot specify both kerberos principal and delegation token.");
    }

    TTransport transport = new TSocket(hostname, port, timeoutMs);

    if (kerberosPrincipal != null) {
        // Replace _HOST to hostname in kerberosPrincipal
        kerberosPrincipal = kerberosPrincipal.replace("_HOST", hostname);
        LOG.info(String.format("Connecting to %s at %s:%d with kerberos principal: %s, with timeout: %sms",
                service, hostname, port, kerberosPrincipal, timeoutMs));

        // Kerberized, wrap the transport in a sasl transport.
        String[] names = kerberosPrincipal.split("[/@]");
        if (names.length != 3) {
            throw new IllegalArgumentException("Kerberos principal should have 3 parts: " + kerberosPrincipal);
        }
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
        Map<String, String> saslProps = new HashMap<String, String>();
        saslProps.put(Sasl.SERVER_AUTH, "true");
        transport = new TSaslClientTransport(KERBEROS_MECHANISM, null, names[0], names[1], saslProps, null,
                transport);
    } else if (token != null) {
        LOG.info(String.format("Connecting to %s at %s:%d using delegation token, with timeout: %sms", service,
                hostname, port, timeoutMs));

        // Delegation token, wrap the transport in a sasl transport.
        CallbackHandler callback = new DigestHandler(token);
        transport = new TSaslClientTransport(TOKEN_MECHANISM, null, "impala", "default",
                new HashMap<String, String>(), callback, transport);
    } else {
        LOG.info(String.format("Connecting to %s at %s:%d, with timeout: %sms", service, hostname, port,
                timeoutMs));
    }

    try {
        transport.open();
    } catch (TTransportException e) {
        String msg = String.format("Could not connect to %s: %s:%d", service, hostname, port);
        LOG.warn(String.format("%s: error: %s", msg, e));
        if (e.getType() == TTransportException.END_OF_FILE && (kerberosPrincipal != null || token != null)) {
            // If connecting from a secure connection to a non-secure server, the
            // connection will fail because the client is expecting the server
            // to participate in the handshake which it is not.
            // This is a heuristic (it might because of other reasons) but
            // likely helpful.
            msg += " Attempting to connect with a secure connection. "
                    + "Ensure the server has security enabled.";
        }
        throw new IOException(msg, e);
    }

    LOG.info(String.format("Connected to %s at %s:%d", service, hostname, port));
    return transport;
}

From source file:com.facebook.hive.metastore.client.TestRetryingHiveMetastore.java

License:Apache License

@Test
public void testNonExistent() throws Exception {
    final int port = NetUtils.findUnusedPort();

    final HiveMetastoreClientConfig metastoreConfig = new HiveMetastoreClientConfig().setPort(port)
            .setMaxRetries(5).setRetrySleep(new Duration(1, TimeUnit.SECONDS))
            .setRetryTimeout(new Duration(30, TimeUnit.SECONDS));

    try (final ThriftClientManager clientManager = new ThriftClientManager()) {
        final ThriftClientConfig clientConfig = new ThriftClientConfig();
        final HiveMetastoreFactory factory = new SimpleHiveMetastoreFactory(clientManager, clientConfig,
                metastoreConfig);//from   w ww .  j  a v  a 2 s .c om

        try (final HiveMetastore metastore = factory.getDefaultClient()) {
            assertFalse(metastore.isConnected());
            metastore.getTable("hello", "world");
            fail();
        } catch (TTransportException te) {
            assertEquals(TTransportException.UNKNOWN, te.getType());
        }
    }
}

From source file:com.facebook.presto.hive.metastore.thrift.Transport.java

License:Apache License

private static TTransportException rewriteException(TTransportException e, String host) {
    return new TTransportException(e.getType(), String.format("%s: %s", host, e.getMessage()), e.getCause());
}

From source file:com.flaptor.indextank.storage.RecordIterator.java

License:Apache License

@Override
protected LogRecord computeNext() {
    if (end >= 0 && transport.getBytesRead() >= end) {
        if (transport != null) {
            totalRead = transport.getBytesRead();
            transport.close();//from w  ww  .  j  av  a  2  s . co  m
        }
        return endOfData();
    }
    LogRecord record = new LogRecord();
    try {
        ((TBinaryProtocol) protocol).setReadLength(10000000);
        record.read(protocol);
        if (transport != null) {
            safelyRead = transport.getBytesRead();
        }
    } catch (TTransportException e) {
        switch (e.getType()) {
        case TTransportException.END_OF_FILE:
            if (transport != null) {
                totalRead = transport.getBytesRead();
                transport.close();
            }
            return endOfData();
        case TTransportException.UNKNOWN:
            if (e.getMessage().startsWith("Cannot read. Remote side has closed")) {
                if (transport != null) {
                    totalRead = transport.getBytesRead();
                    transport.close();
                }
                return endOfData();
            }
        default:
            transport.close();
            throw new RuntimeException("Failed while iterating: " + description, e);
        }
    } catch (TException e) {
        transport.close();
        throw new RuntimeException("Failed while iterating: " + description, e);
    }
    return record;
}

From source file:com.linecorp.armeria.common.thrift.text.TTextProtocol.java

License:Apache License

/**
 * Read in the root node if it has not yet been read.
 *///from  w w w  .  ja v a2s.  c  o m
private void readRoot() throws IOException {
    if (root != null) {
        return;
    }
    ByteArrayOutputStream content = new ByteArrayOutputStream();
    byte[] buffer = new byte[READ_BUFFER_SIZE];
    try {
        while (trans_.read(buffer, 0, READ_BUFFER_SIZE) > 0) {
            content.write(buffer);
        }
    } catch (TTransportException e) {
        if (TTransportException.END_OF_FILE != e.getType()) {
            throw new IOException(e);
        }
    }
    root = OBJECT_MAPPER.readTree(content.toByteArray());
}

From source file:com.netflix.metacat.connector.hive.client.thrift.HiveMetastoreClientFactory.java

License:Apache License

private static TTransportException rewriteException(final TTransportException e, final String host) {
    return new TTransportException(e.getType(), String.format("%s: %s", host, e.getMessage()), e.getCause());
}

From source file:com.twitter.common.thrift.text.TTextProtocol.java

License:Apache License

/**
 * Set up the stream parser to read from the trans_ TTransport
 * buffer./*from w w w  .  j a v  a 2  s  . c  o m*/
 */
private JsonStreamParser createParser() throws IOException {
    return new JsonStreamParser(new String(ByteStreams.toByteArray(new InputStream() {
        private int index;
        private int max;
        private final byte[] buffer = new byte[READ_BUFFER_SIZE];

        @Override
        public int read() throws IOException {
            if (max == -1) {
                return -1;
            }
            if (max > 0 && index < max) {
                return buffer[index++];
            }
            try {
                max = trans_.read(buffer, 0, READ_BUFFER_SIZE);
                index = 0;
            } catch (TTransportException e) {
                if (TTransportException.END_OF_FILE != e.getType()) {
                    throw new IOException(e);
                }
                max = -1;
            }
            return read();
        }
    }), Charsets.UTF_8));
}

From source file:de.l3s.streamcorpus.terrier.ThriftFileCollectionRecordReader.java

License:Apache License

@Override
/**//from  ww w  .  java  2  s  . c  o m
 * parse the next key value, update position and return true
 */
public boolean nextKeyValue() throws IOException, InterruptedException {

    while (true) {

        // The file is corrupted, skip it
        if (cis == null) {
            if (!getNextFile()) {
                return false;
            } else
                continue;
        }

        key.set(paths.get(collectionIndex));

        // assume the underlying file is opened, read and when the EOF
        // is met, move to the next file
        if (cis.available() > 0) {
            try {
                value.read(tp);
                position = cis.getPos();
                return true;
            } catch (TTransportException e) {
                int type = e.getType();
                if (type == TTransportException.END_OF_FILE) {
                    if (!getNextFile()) {
                        return false;
                    } else
                        continue;
                }
            } catch (TException e) {
                e.printStackTrace();
                throw new IOException(e);
            }
        } else {
            if (!getNextFile()) {
                return false;
            } else
                continue;
        }
    }
}

From source file:edu.gslis.streamcorpus.ThriftRecordReader.java

License:Apache License

@Override
public boolean nextKeyValue() throws IOException {
    key.set(path.getName() + "-" + pos);

    if (in.available() > 0) {

        try {// w ww  .  j ava 2s  .  c om
            value.read(tp);
            pos = end - in.available() - startOffset;

        } catch (TTransportException tte) {
            // END_OF_FILE is used to indicate EOF and is not an exception.
            if (tte.getType() != TTransportException.END_OF_FILE)
                tte.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            // throw new IOException(e);
        }
    } else
        return false;

    return true;
}