Search in sources :

Example 1 with Client

use of org.apache.cassandra.thrift.Cassandra.Client 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 2 with Client

use of org.apache.cassandra.thrift.Cassandra.Client in project atlasdb by palantir.

the class CassandraClientFactory method getRawClientWithKeyspace.

private static Cassandra.Client getRawClientWithKeyspace(InetSocketAddress addr, CassandraKeyValueServiceConfig config) throws Exception {
    Client ret = getRawClient(addr, config);
    try {
        ret.set_keyspace(config.getKeyspaceOrThrow());
        log.debug("Created new client for {}/{}{}{}", SafeArg.of("address", CassandraLogHelper.host(addr)), UnsafeArg.of("keyspace", config.getKeyspaceOrThrow()), SafeArg.of("usingSsl", config.usingSsl() ? " over SSL" : ""), UnsafeArg.of("usernameConfig", config.credentials().isPresent() ? " as user " + config.credentials().get().username() : ""));
        return ret;
    } catch (Exception e) {
        ret.getOutputProtocol().getTransport().close();
        throw e;
    }
}
Also used : QosClient(com.palantir.atlasdb.qos.QosClient) Client(org.apache.cassandra.thrift.Cassandra.Client) QosCassandraClient(com.palantir.atlasdb.keyvalue.cassandra.qos.QosCassandraClient) AtlasDbDependencyException(com.palantir.common.exception.AtlasDbDependencyException) TTransportException(org.apache.thrift.transport.TTransportException) SocketException(java.net.SocketException) TException(org.apache.thrift.TException) IOException(java.io.IOException)

Aggregations

QosCassandraClient (com.palantir.atlasdb.keyvalue.cassandra.qos.QosCassandraClient)2 QosClient (com.palantir.atlasdb.qos.QosClient)2 IOException (java.io.IOException)2 SocketException (java.net.SocketException)2 Client (org.apache.cassandra.thrift.Cassandra.Client)2 TException (org.apache.thrift.TException)2 TTransportException (org.apache.thrift.transport.TTransportException)2 AtlasDbDependencyException (com.palantir.common.exception.AtlasDbDependencyException)1 SSLSocket (javax.net.ssl.SSLSocket)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 Cassandra (org.apache.cassandra.thrift.Cassandra)1 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)1 TProtocol (org.apache.thrift.protocol.TProtocol)1 TFramedTransport (org.apache.thrift.transport.TFramedTransport)1 TSocket (org.apache.thrift.transport.TSocket)1 TTransport (org.apache.thrift.transport.TTransport)1