Search in sources :

Example 6 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project atlasdb by palantir.

the class CassandraClientFactory method getRawClient.

private static Cassandra.Client getRawClient(InetSocketAddress addr, CassandraKeyValueServiceConfig config) throws TException {
    TSocket thriftSocket = new TSocket(addr.getHostString(), addr.getPort(), config.socketTimeoutMillis());
    thriftSocket.open();
    try {
        thriftSocket.getSocket().setKeepAlive(true);
        thriftSocket.getSocket().setSoTimeout(config.socketQueryTimeoutMillis());
    } catch (SocketException e) {
        log.error("Couldn't set socket keep alive for host {}", SafeArg.of("address", CassandraLogHelper.host(addr)));
    }
    if (config.usingSsl()) {
        boolean success = false;
        try {
            final SSLSocketFactory factory;
            if (config.sslConfiguration().isPresent()) {
                factory = SslSocketFactories.createSslSocketFactory(config.sslConfiguration().get());
            } else {
                factory = sslSocketFactories.getUnchecked(addr);
            }
            SSLSocket socket = (SSLSocket) factory.createSocket(thriftSocket.getSocket(), addr.getHostString(), addr.getPort(), true);
            thriftSocket = new TSocket(socket);
            success = true;
        } catch (IOException e) {
            throw new TTransportException(e);
        } finally {
            if (!success) {
                thriftSocket.close();
            }
        }
    }
    TTransport thriftFramedTransport = new TFramedTransport(thriftSocket, CassandraConstants.CLIENT_MAX_THRIFT_FRAME_SIZE_BYTES);
    TProtocol protocol = new TBinaryProtocol(thriftFramedTransport);
    Cassandra.Client client = new Cassandra.Client(protocol);
    if (config.credentials().isPresent()) {
        try {
            login(client, config.credentials().get());
        } catch (TException e) {
            client.getOutputProtocol().getTransport().close();
            log.error("Exception thrown attempting to authenticate with config provided credentials", e);
            throw e;
        }
    }
    return client;
}
Also used : TException(org.apache.thrift.TException) SocketException(java.net.SocketException) Client(org.apache.cassandra.thrift.Cassandra.Client) SSLSocket(javax.net.ssl.SSLSocket) Cassandra(org.apache.cassandra.thrift.Cassandra) TTransportException(org.apache.thrift.transport.TTransportException) IOException(java.io.IOException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) QosClient(com.palantir.atlasdb.qos.QosClient) Client(org.apache.cassandra.thrift.Cassandra.Client) QosCassandraClient(com.palantir.atlasdb.keyvalue.cassandra.qos.QosCassandraClient) TSocket(org.apache.thrift.transport.TSocket)

Example 7 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project providence by morimekta.

the class NonblockingSocketServerTest method testWithPlainThriftClient.

@Test
public void testWithPlainThriftClient() throws IOException, TException, NotFound, InterruptedException {
    try (TSocket socket = new TSocket("localhost", port);
        TFramedTransport transport = new TFramedTransport(socket)) {
        socket.open();
        TProtocol protocol = factory.getProtocol(transport);
        net.morimekta.test.thrift.thrift.map.RemoteMap.Client client = new net.morimekta.test.thrift.thrift.map.RemoteMap.Client(protocol);
        client.put("a", "b");
        client.put("b", "");
        try {
            client.get("c");
            fail("no exception");
        } catch (net.morimekta.test.thrift.thrift.map.NotFound nfe) {
        // nothing to check.
        }
        verify(instrumentation, times(3)).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class));
        verifyNoMoreInteractions(instrumentation);
        assertThat(remoteMap, is(ImmutableMap.of("a", "b", "b", "")));
    }
    reset(instrumentation);
    remoteMap.clear();
    try (TSocket socket = new TSocket("localhost", port);
        TFramedTransport transport = new TFramedTransport(socket)) {
        socket.open();
        TProtocol protocol = factory.getProtocol(transport);
        net.morimekta.test.thrift.thrift.map.RemoteMap.Client client = new net.morimekta.test.thrift.thrift.map.RemoteMap.Client(protocol);
        client.put("a", "b123");
        client.put("b", "a2345");
        try {
            client.get("c");
            fail("no exception");
        } catch (net.morimekta.test.thrift.thrift.map.NotFound nfe) {
        // nothing to check.
        }
        verify(instrumentation, times(3)).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class));
        verifyNoMoreInteractions(instrumentation);
        assertThat(remoteMap, is(ImmutableMap.of("a", "b123", "b", "a2345")));
    }
}
Also used : RemoteMap(net.morimekta.test.providence.thrift.map.RemoteMap) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) PServiceCall(net.morimekta.providence.PServiceCall) TSocket(org.apache.thrift.transport.TSocket) Test(org.junit.Test)

Example 8 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport 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 9 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project cogcomp-nlp by CogComp.

the class CuratorClient method addRecordViewFromCurator.

/**
 * Does the network call to the Curator and fetches a record that has a particular view.
 *
 * @param text The raw text (this will be used if {@link #respectTokenization} is false.
 * @param sentences The list of tokenized sentences (will be {@code null} if
 *        {@link #respectTokenization} is true.
 * @param viewName The view to get (according to the Curator lingo.)
 * @return A {@link edu.illinois.cs.cogcomp.thrift.curator.Record} with the requested view
 */
private Record addRecordViewFromCurator(String text, List<String> sentences, String viewName) throws ServiceUnavailableException, AnnotationFailedException, TException, SocketException {
    viewName = convertCuratorViewName(viewName);
    TTransport transport = new TSocket(this.curatorHost, this.curatorPort);
    logger.debug("Calling curator on host '" + curatorHost + "', port '" + curatorPort + "' for view '" + viewName + "'...");
    try {
        ((TSocket) transport).getSocket().setReuseAddress(true);
    } catch (SocketException e) {
        logger.error("Unable to setReuseAddress!", e);
        throw e;
    }
    transport = new TFramedTransport(transport);
    TProtocol protocol = new TBinaryProtocol(transport);
    transport.open();
    Curator.Client client = new Curator.Client(protocol);
    Record newRecord;
    if (respectTokenization) {
        newRecord = client.wsprovide(viewName, sentences, forceUpdate);
    } else {
        newRecord = client.provide(viewName, text, forceUpdate);
    }
    transport.close();
    return newRecord;
}
Also used : SocketException(java.net.SocketException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) Curator(edu.illinois.cs.cogcomp.thrift.curator.Curator) Record(edu.illinois.cs.cogcomp.thrift.curator.Record) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 10 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project whirr by apache.

the class CassandraServiceTest method client.

private Cassandra.Client client(Instance instance) throws TException {
    TTransport trans = new TFramedTransport(new TSocket(instance.getPublicIp(), CassandraClusterActionHandler.CLIENT_PORT));
    trans.open();
    TBinaryProtocol protocol = new TBinaryProtocol(trans);
    return new Cassandra.Client(protocol);
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Aggregations

TFramedTransport (org.apache.thrift.transport.TFramedTransport)45 TSocket (org.apache.thrift.transport.TSocket)41 TTransport (org.apache.thrift.transport.TTransport)32 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)25 TProtocol (org.apache.thrift.protocol.TProtocol)24 TException (org.apache.thrift.TException)14 IOException (java.io.IOException)11 TTransportException (org.apache.thrift.transport.TTransportException)11 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)10 Cassandra (org.apache.cassandra.thrift.Cassandra)4 Hello (org.tech.model.Hello)4 SocketException (java.net.SocketException)3 HashMap (java.util.HashMap)3 GameRPC (com.code.server.rpc.idl.GameRPC)2 UnknownHostException (java.net.UnknownHostException)2 SSLSocket (javax.net.ssl.SSLSocket)2 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)2 ConfigurationException (org.apache.cassandra.config.ConfigurationException)2 AuthenticationRequest (org.apache.cassandra.thrift.AuthenticationRequest)2 TBinaryProtocol (org.apache.cassandra.thrift.TBinaryProtocol)2