Search in sources :

Example 21 with TCompactProtocol

use of org.apache.thrift.protocol.TCompactProtocol in project accumulo by apache.

the class ThriftMessageUtil method deserialize.

/**
 * Deserializes a message into the provided {@code instance} from {@code serialized}
 *
 * @param serialized
 *          The serialized representation of the object
 * @param instance
 *          An instance of the object to reconstitute
 * @return The reconstituted instance provided
 * @throws IOException
 *           When deserialization fails
 */
public <T extends TBase<?, ?>> T deserialize(byte[] serialized, int offset, int length, T instance) throws IOException {
    requireNonNull(instance);
    TCompactProtocol proto = new TCompactProtocol(new TMemoryInputTransport(serialized, offset, length));
    try {
        instance.read(proto);
    } catch (TException e) {
        throw new IOException(e);
    }
    return instance;
}
Also used : TException(org.apache.thrift.TException) IOException(java.io.IOException) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TMemoryInputTransport(org.apache.thrift.transport.TMemoryInputTransport)

Example 22 with TCompactProtocol

use of org.apache.thrift.protocol.TCompactProtocol in project flink by apache.

the class HiveMetaStoreClient method open.

private void open() throws MetaException {
    isConnected = false;
    TTransportException tte = null;
    boolean useSSL = MetastoreConf.getBoolVar(conf, ConfVars.USE_SSL);
    boolean useSasl = MetastoreConf.getBoolVar(conf, ConfVars.USE_THRIFT_SASL);
    boolean useFramedTransport = MetastoreConf.getBoolVar(conf, ConfVars.USE_THRIFT_FRAMED_TRANSPORT);
    boolean useCompactProtocol = MetastoreConf.getBoolVar(conf, ConfVars.USE_THRIFT_COMPACT_PROTOCOL);
    int clientSocketTimeout = (int) MetastoreConf.getTimeVar(conf, ConfVars.CLIENT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
    for (int attempt = 0; !isConnected && attempt < retries; ++attempt) {
        for (URI store : metastoreUris) {
            LOG.info("Trying to connect to metastore with URI " + store);
            try {
                if (useSSL) {
                    try {
                        String trustStorePath = MetastoreConf.getVar(conf, ConfVars.SSL_TRUSTSTORE_PATH).trim();
                        if (trustStorePath.isEmpty()) {
                            throw new IllegalArgumentException(ConfVars.SSL_TRUSTSTORE_PATH.toString() + " Not configured for SSL connection");
                        }
                        String trustStorePassword = MetastoreConf.getPassword(conf, MetastoreConf.ConfVars.SSL_TRUSTSTORE_PASSWORD);
                        // Create an SSL socket and connect
                        transport = SecurityUtils.getSSLSocket(store.getHost(), store.getPort(), clientSocketTimeout, trustStorePath, trustStorePassword);
                        LOG.info("Opened an SSL connection to metastore, current connections: " + connCount.incrementAndGet());
                    } catch (IOException e) {
                        throw new IllegalArgumentException(e);
                    } catch (TTransportException e) {
                        tte = e;
                        throw new MetaException(e.toString());
                    }
                } else {
                    transport = new TSocket(store.getHost(), store.getPort(), clientSocketTimeout);
                }
                if (useSasl) {
                    // Wrap thrift connection with SASL for secure connection.
                    try {
                        HadoopThriftAuthBridge.Client authBridge = HadoopThriftAuthBridge.getBridge().createClient();
                        // check if we should use delegation tokens to authenticate
                        // the call below gets hold of the tokens if they are set up by hadoop
                        // this should happen on the map/reduce tasks if the client added the
                        // tokens into hadoop's credential store in the front end during job
                        // submission.
                        String tokenSig = MetastoreConf.getVar(conf, ConfVars.TOKEN_SIGNATURE);
                        // tokenSig could be null
                        tokenStrForm = SecurityUtils.getTokenStrForm(tokenSig);
                        if (tokenStrForm != null) {
                            LOG.info("HMSC::open(): Found delegation token. Creating DIGEST-based thrift connection.");
                            // authenticate using delegation tokens via the "DIGEST" mechanism
                            transport = authBridge.createClientTransport(null, store.getHost(), "DIGEST", tokenStrForm, transport, MetaStoreUtils.getMetaStoreSaslProperties(conf, useSSL));
                        } else {
                            LOG.info("HMSC::open(): Could not find delegation token. Creating KERBEROS-based thrift connection.");
                            String principalConfig = MetastoreConf.getVar(conf, ConfVars.KERBEROS_PRINCIPAL);
                            transport = authBridge.createClientTransport(principalConfig, store.getHost(), "KERBEROS", null, transport, MetaStoreUtils.getMetaStoreSaslProperties(conf, useSSL));
                        }
                    } catch (IOException ioe) {
                        LOG.error("Couldn't create client transport", ioe);
                        throw new MetaException(ioe.toString());
                    }
                } else {
                    if (useFramedTransport) {
                        transport = new TFramedTransport(transport);
                    }
                }
                final TProtocol protocol;
                if (useCompactProtocol) {
                    protocol = new TCompactProtocol(transport);
                } else {
                    protocol = new TBinaryProtocol(transport);
                }
                client = new ThriftHiveMetastore.Client(protocol);
                try {
                    if (!transport.isOpen()) {
                        transport.open();
                        LOG.info("Opened a connection to metastore, current connections: " + connCount.incrementAndGet());
                    }
                    isConnected = true;
                } catch (TTransportException e) {
                    tte = e;
                    if (LOG.isDebugEnabled()) {
                        LOG.warn("Failed to connect to the MetaStore Server...", e);
                    } else {
                        // Don't print full exception trace if DEBUG is not on.
                        LOG.warn("Failed to connect to the MetaStore Server...");
                    }
                }
                if (isConnected && !useSasl && MetastoreConf.getBoolVar(conf, ConfVars.EXECUTE_SET_UGI)) {
                    // Call set_ugi, only in unsecure mode.
                    try {
                        UserGroupInformation ugi = SecurityUtils.getUGI();
                        client.set_ugi(ugi.getUserName(), Arrays.asList(ugi.getGroupNames()));
                    } catch (LoginException e) {
                        LOG.warn("Failed to do login. set_ugi() is not successful, " + "Continuing without it.", e);
                    } catch (IOException e) {
                        LOG.warn("Failed to find ugi of client set_ugi() is not successful, " + "Continuing without it.", e);
                    } catch (TException e) {
                        LOG.warn("set_ugi() not successful, Likely cause: new client talking to old server. " + "Continuing without it.", e);
                    }
                }
            } catch (MetaException e) {
                LOG.error("Unable to connect to metastore with URI " + store + " in attempt " + attempt, e);
            }
            if (isConnected) {
                break;
            }
        }
        // Wait before launching the next round of connection retries.
        if (!isConnected && retryDelaySeconds > 0) {
            try {
                LOG.info("Waiting " + retryDelaySeconds + " seconds before next connection attempt.");
                Thread.sleep(retryDelaySeconds * 1000);
            } catch (InterruptedException ignore) {
            }
        }
    }
    if (!isConnected) {
        throw new MetaException("Could not connect to meta store using any of the URIs provided." + " Most recent failure: " + StringUtils.stringifyException(tte));
    }
    snapshotActiveConf();
    LOG.info("Connected to metastore.");
}
Also used : TException(org.apache.thrift.TException) TTransportException(org.apache.thrift.transport.TTransportException) IOException(java.io.IOException) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) URI(java.net.URI) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) HadoopThriftAuthBridge(org.apache.hadoop.hive.metastore.security.HadoopThriftAuthBridge) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) LoginException(javax.security.auth.login.LoginException) TSocket(org.apache.thrift.transport.TSocket) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 23 with TCompactProtocol

use of org.apache.thrift.protocol.TCompactProtocol in project hbase by apache.

the class TestThrift2ServerCmdLine method talkToThriftServer.

@Override
protected void talkToThriftServer(int port) throws Exception {
    TSocket sock = new TSocket(InetAddress.getLoopbackAddress().getHostName(), port);
    TTransport transport = sock;
    if (specifyFramed || implType.isAlwaysFramed()) {
        transport = new TFramedTransport(transport);
    }
    sock.open();
    try {
        TProtocol tProtocol;
        if (specifyCompact) {
            tProtocol = new TCompactProtocol(transport);
        } else {
            tProtocol = new TBinaryProtocol(transport);
        }
        THBaseService.Client client = new THBaseService.Client(tProtocol);
        TTableName tTableName = new TTableName();
        tTableName.setNs(Bytes.toBytes(""));
        tTableName.setQualifier(Bytes.toBytes(TABLENAME));
        if (!tableCreated) {
            Assert.assertTrue(!client.tableExists(tTableName));
            TTableDescriptor tTableDescriptor = new TTableDescriptor();
            tTableDescriptor.setTableName(tTableName);
            TColumnFamilyDescriptor columnFamilyDescriptor = new TColumnFamilyDescriptor();
            columnFamilyDescriptor.setName(Bytes.toBytes(TABLENAME));
            tTableDescriptor.addToColumns(columnFamilyDescriptor);
            client.createTable(tTableDescriptor, new ArrayList<>());
            tableCreated = true;
        }
        Assert.assertTrue("tableCreated " + tableCreated, client.tableExists(tTableName));
    } finally {
        sock.close();
    }
}
Also used : TTableName(org.apache.hadoop.hbase.thrift2.generated.TTableName) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.layered.TFramedTransport) TColumnFamilyDescriptor(org.apache.hadoop.hbase.thrift2.generated.TColumnFamilyDescriptor) TTableDescriptor(org.apache.hadoop.hbase.thrift2.generated.TTableDescriptor) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket) THBaseService(org.apache.hadoop.hbase.thrift2.generated.THBaseService)

Example 24 with TCompactProtocol

use of org.apache.thrift.protocol.TCompactProtocol in project hbase by apache.

the class TestThriftServerCmdLine method talkToThriftServer.

protected void talkToThriftServer(int port) throws Exception {
    LOG.info("Talking to port={}", port);
    TSocket sock = new TSocket(InetAddress.getLoopbackAddress().getHostName(), port);
    TTransport transport = sock;
    if (specifyFramed || implType.isAlwaysFramed) {
        transport = new TFramedTransport(transport);
    }
    sock.open();
    try {
        TProtocol prot;
        if (specifyCompact) {
            prot = new TCompactProtocol(transport);
        } else {
            prot = new TBinaryProtocol(transport);
        }
        Hbase.Client client = new Hbase.Client(prot);
        if (!tableCreated) {
            TestThriftServer.createTestTables(client);
            tableCreated = true;
        }
        TestThriftServer.checkTableList(client);
    } finally {
        sock.close();
    }
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.layered.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) Hbase(org.apache.hadoop.hbase.thrift.generated.Hbase) TSocket(org.apache.thrift.transport.TSocket)

Example 25 with TCompactProtocol

use of org.apache.thrift.protocol.TCompactProtocol in project hive by apache.

the class HiveMetaStoreClientPreCatalog method open.

private void open() throws MetaException {
    isConnected = false;
    TTransportException tte = null;
    boolean useSSL = MetastoreConf.getBoolVar(conf, ConfVars.USE_SSL);
    boolean useSasl = MetastoreConf.getBoolVar(conf, ConfVars.USE_THRIFT_SASL);
    boolean useFramedTransport = MetastoreConf.getBoolVar(conf, ConfVars.USE_THRIFT_FRAMED_TRANSPORT);
    boolean useCompactProtocol = MetastoreConf.getBoolVar(conf, ConfVars.USE_THRIFT_COMPACT_PROTOCOL);
    int clientSocketTimeout = (int) MetastoreConf.getTimeVar(conf, ConfVars.CLIENT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
    for (int attempt = 0; !isConnected && attempt < retries; ++attempt) {
        for (URI store : metastoreUris) {
            LOG.info("Trying to connect to metastore with URI " + store);
            try {
                if (useSSL) {
                    try {
                        String trustStorePath = MetastoreConf.getVar(conf, ConfVars.SSL_TRUSTSTORE_PATH).trim();
                        if (trustStorePath.isEmpty()) {
                            throw new IllegalArgumentException(ConfVars.SSL_TRUSTSTORE_PATH.toString() + " Not configured for SSL connection");
                        }
                        String trustStorePassword = MetastoreConf.getPassword(conf, MetastoreConf.ConfVars.SSL_TRUSTSTORE_PASSWORD);
                        String trustStoreType = MetastoreConf.getVar(conf, ConfVars.SSL_TRUSTSTORE_TYPE).trim();
                        String trustStoreAlgorithm = MetastoreConf.getVar(conf, ConfVars.SSL_TRUSTMANAGERFACTORY_ALGORITHM).trim();
                        // Create an SSL socket and connect
                        transport = SecurityUtils.getSSLSocket(store.getHost(), store.getPort(), clientSocketTimeout, trustStorePath, trustStorePassword, trustStoreType, trustStoreAlgorithm);
                        LOG.info("Opened an SSL connection to metastore, current connections: " + connCount.incrementAndGet());
                    } catch (IOException e) {
                        throw new IllegalArgumentException(e);
                    } catch (TTransportException e) {
                        tte = e;
                        throw new MetaException(e.toString());
                    }
                } else {
                    try {
                        transport = new TSocket(new TConfiguration(), store.getHost(), store.getPort(), clientSocketTimeout);
                    } catch (TTransportException e) {
                        tte = e;
                        throw new MetaException(e.toString());
                    }
                }
                if (useSasl) {
                    // Wrap thrift connection with SASL for secure connection.
                    try {
                        HadoopThriftAuthBridge.Client authBridge = HadoopThriftAuthBridge.getBridge().createClient();
                        // check if we should use delegation tokens to authenticate
                        // the call below gets hold of the tokens if they are set up by hadoop
                        // this should happen on the map/reduce tasks if the client added the
                        // tokens into hadoop's credential store in the front end during job
                        // submission.
                        String tokenSig = MetastoreConf.getVar(conf, ConfVars.TOKEN_SIGNATURE);
                        // tokenSig could be null
                        tokenStrForm = SecurityUtils.getTokenStrForm(tokenSig);
                        if (tokenStrForm != null) {
                            LOG.info("HMSC::open(): Found delegation token. Creating DIGEST-based thrift connection.");
                            // authenticate using delegation tokens via the "DIGEST" mechanism
                            transport = authBridge.createClientTransport(null, store.getHost(), "DIGEST", tokenStrForm, transport, MetaStoreUtils.getMetaStoreSaslProperties(conf, useSSL));
                        } else {
                            LOG.info("HMSC::open(): Could not find delegation token. Creating KERBEROS-based thrift connection.");
                            String principalConfig = MetastoreConf.getVar(conf, ConfVars.KERBEROS_PRINCIPAL);
                            transport = authBridge.createClientTransport(principalConfig, store.getHost(), "KERBEROS", null, transport, MetaStoreUtils.getMetaStoreSaslProperties(conf, useSSL));
                        }
                    } catch (IOException ioe) {
                        LOG.error("Couldn't create client transport", ioe);
                        throw new MetaException(ioe.toString());
                    }
                } else {
                    if (useFramedTransport) {
                        try {
                            transport = new TFramedTransport(transport);
                        } catch (TTransportException e) {
                            LOG.error("Couldn't create client transport", e);
                            throw new MetaException(e.toString());
                        }
                    }
                }
                final TProtocol protocol;
                if (useCompactProtocol) {
                    protocol = new TCompactProtocol(transport);
                } else {
                    protocol = new TBinaryProtocol(transport);
                }
                client = new ThriftHiveMetastore.Client(protocol);
                try {
                    if (!transport.isOpen()) {
                        transport.open();
                        LOG.info("Opened a connection to metastore, current connections: " + connCount.incrementAndGet());
                    }
                    isConnected = true;
                } catch (TTransportException e) {
                    tte = e;
                    if (LOG.isDebugEnabled()) {
                        LOG.warn("Failed to connect to the MetaStore Server...", e);
                    } else {
                        // Don't print full exception trace if DEBUG is not on.
                        LOG.warn("Failed to connect to the MetaStore Server...");
                    }
                }
                if (isConnected && !useSasl && MetastoreConf.getBoolVar(conf, ConfVars.EXECUTE_SET_UGI)) {
                    // Call set_ugi, only in unsecure mode.
                    try {
                        UserGroupInformation ugi = SecurityUtils.getUGI();
                        client.set_ugi(ugi.getUserName(), Arrays.asList(ugi.getGroupNames()));
                    } catch (LoginException e) {
                        LOG.warn("Failed to do login. set_ugi() is not successful, " + "Continuing without it.", e);
                    } catch (IOException e) {
                        LOG.warn("Failed to find ugi of client set_ugi() is not successful, " + "Continuing without it.", e);
                    } catch (TException e) {
                        LOG.warn("set_ugi() not successful, Likely cause: new client talking to old server. " + "Continuing without it.", e);
                    }
                }
            } catch (MetaException e) {
                LOG.error("Unable to connect to metastore with URI " + store + " in attempt " + attempt, e);
            }
            if (isConnected) {
                break;
            }
        }
        // Wait before launching the next round of connection retries.
        if (!isConnected && retryDelaySeconds > 0) {
            try {
                LOG.info("Waiting " + retryDelaySeconds + " seconds before next connection attempt.");
                Thread.sleep(retryDelaySeconds * 1000);
            } catch (InterruptedException ignore) {
            }
        }
    }
    if (!isConnected) {
        throw new MetaException("Could not connect to meta store using any of the URIs provided." + " Most recent failure: " + StringUtils.stringifyException(tte));
    }
    snapshotActiveConf();
    LOG.info("Connected to metastore.");
}
Also used : TException(org.apache.thrift.TException) TTransportException(org.apache.thrift.transport.TTransportException) IOException(java.io.IOException) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) URI(java.net.URI) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) HadoopThriftAuthBridge(org.apache.hadoop.hive.metastore.security.HadoopThriftAuthBridge) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.layered.TFramedTransport) LoginException(javax.security.auth.login.LoginException) TConfiguration(org.apache.thrift.TConfiguration) TSocket(org.apache.thrift.transport.TSocket) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Aggregations

TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)54 TProtocol (org.apache.thrift.protocol.TProtocol)38 THttpClient (org.apache.thrift.transport.THttpClient)18 TSocket (org.apache.thrift.transport.TSocket)18 TException (org.apache.thrift.TException)15 TTransport (org.apache.thrift.transport.TTransport)11 TFramedTransport (org.apache.thrift.transport.TFramedTransport)9 IOException (java.io.IOException)8 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)7 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)7 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)7 TTransportException (org.apache.thrift.transport.TTransportException)5 TFramedTransport (org.apache.thrift.transport.layered.TFramedTransport)5 Test (org.junit.Test)5 LoginException (javax.security.auth.login.LoginException)4 Hello (org.tech.model.Hello)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 UGIAssumingTransport (org.apache.accumulo.core.rpc.UGIAssumingTransport)3 AccumuloProxy (org.apache.accumulo.proxy.thrift.AccumuloProxy)3