Search in sources :

Example 16 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project eiger by wlloyd.

the class CliMain method connect.

/**
 * Establish a thrift connection to cassandra instance
 *
 * @param server - hostname or IP of the server
 * @param port   - Thrift port number
 */
public static void connect(String server, int port) {
    TSocket socket = new TSocket(server, port);
    if (transport != null)
        transport.close();
    if (sessionState.framed) {
        transport = new TFramedTransport(socket);
    } else {
        transport = socket;
    }
    TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport, true, true);
    Cassandra.Client cassandraClient = new Cassandra.Client(binaryProtocol);
    try {
        transport.open();
    } catch (Exception e) {
        e.printStackTrace(sessionState.err);
        String error = (e.getCause() == null) ? e.getMessage() : e.getCause().getMessage();
        throw new RuntimeException("Exception connecting to " + server + "/" + port + ". Reason: " + error + ".");
    }
    thriftClient = cassandraClient;
    cliClient = new CliClient(sessionState, thriftClient);
    if ((sessionState.username != null) && (sessionState.password != null)) {
        // Authenticate
        Map<String, String> credentials = new HashMap<String, String>();
        credentials.put(IAuthenticator.USERNAME_KEY, sessionState.username);
        credentials.put(IAuthenticator.PASSWORD_KEY, sessionState.password);
        AuthenticationRequest authRequest = new AuthenticationRequest(credentials);
        try {
            thriftClient.login(authRequest, LamportClock.sendTimestamp());
            cliClient.setUsername(sessionState.username);
        } catch (AuthenticationException e) {
            thriftClient = null;
            sessionState.err.println("Exception during authentication to the cassandra node, " + "Verify the keyspace exists, and that you are using the correct credentials.");
            return;
        } catch (AuthorizationException e) {
            thriftClient = null;
            sessionState.err.println("You are not authorized to use keyspace: " + sessionState.keyspace);
            return;
        } catch (TException e) {
            thriftClient = null;
            sessionState.err.println("Login failure. Did you specify 'keyspace', 'username' and 'password'?");
            return;
        }
    }
    if (sessionState.keyspace != null) {
        try {
            sessionState.keyspace = CliCompiler.getKeySpace(sessionState.keyspace, thriftClient.describe_keyspaces());
            ;
            thriftClient.set_keyspace(sessionState.keyspace, LamportClock.sendTimestamp());
            cliClient.setKeySpace(sessionState.keyspace);
            updateCompletor(CliUtils.getCfNamesByKeySpace(cliClient.getKSMetaData(sessionState.keyspace)));
        } catch (InvalidRequestException e) {
            sessionState.err.println("Keyspace " + sessionState.keyspace + " not found");
            return;
        } catch (TException e) {
            sessionState.err.println("Did you specify 'keyspace'?");
            return;
        } catch (NotFoundException e) {
            sessionState.err.println("Keyspace " + sessionState.keyspace + " not found");
            return;
        }
    }
    // Lookup the cluster name, this is to make it clear which cluster the user is connected to
    String clusterName;
    try {
        clusterName = thriftClient.describe_cluster_name();
    } catch (Exception e) {
        sessionState.err.println("Exception retrieving information about the cassandra node, check you have connected to the thrift port.");
        e.printStackTrace(sessionState.err);
        return;
    }
    sessionState.out.printf("Connected to: \"%s\" on %s/%d%n", clusterName, server, port);
}
Also used : TException(org.apache.thrift.TException) CharacterCodingException(java.nio.charset.CharacterCodingException) TException(org.apache.thrift.TException) IOException(java.io.IOException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 17 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project eiger by wlloyd.

the class Session method getClient.

/**
 * Thrift client connection
 * @param setKeyspace - should we set keyspace for client or not
 * @return cassandra client connection
 */
public Cassandra.Client getClient(boolean setKeyspace) {
    // random node selection for fake load balancing
    String currentNode = nodes[Stress.randomizer.nextInt(nodes.length)];
    TSocket socket = new TSocket(currentNode, port);
    TTransport transport = (isUnframed()) ? socket : new TFramedTransport(socket);
    Cassandra.Client client = new Cassandra.Client(new TBinaryProtocol(transport));
    try {
        transport.open();
        if (setKeyspace) {
            client.set_keyspace("Keyspace1", LamportClock.sendTimestamp());
        }
    } catch (InvalidRequestException e) {
        throw new RuntimeException(e.getWhy());
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return client;
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) UnknownHostException(java.net.UnknownHostException) ConfigurationException(org.apache.cassandra.config.ConfigurationException) TSocket(org.apache.thrift.transport.TSocket)

Example 18 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project brisk by riptano.

the class CassandraStorage method createConnection.

private static Cassandra.Client createConnection(String host, Integer port, boolean framed) throws IOException {
    TSocket socket = new TSocket(host, port);
    TTransport trans = framed ? new TFramedTransport(socket) : socket;
    try {
        trans.open();
    } catch (TTransportException e) {
        throw new IOException("unable to connect to server", e);
    }
    return new Cassandra.Client(new TBinaryProtocol(trans));
}
Also used : TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransportException(org.apache.thrift.transport.TTransportException) TTransport(org.apache.thrift.transport.TTransport) IOException(java.io.IOException) TSocket(org.apache.thrift.transport.TSocket)

Example 19 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project brisk by riptano.

the class Session method getClient.

/**
 * Thrift client connection
 * @param setKeyspace - should we set keyspace for client or not
 * @return cassandra client connection
 */
public Cassandra.Client getClient(boolean setKeyspace) {
    // random node selection for fake load balancing
    String currentNode = nodes[Pricer.randomizer.nextInt(nodes.length)];
    TSocket socket = new TSocket(currentNode, port);
    TTransport transport = (isUnframed()) ? socket : new TFramedTransport(socket);
    Cassandra.Client client = new Cassandra.Client(new TBinaryProtocol(transport));
    try {
        transport.open();
        if (setKeyspace) {
            client.set_keyspace("PortfolioDemo");
        }
    } catch (InvalidRequestException e) {
        throw new RuntimeException(e.getWhy());
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return client;
}
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)

Example 20 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project titan by thinkaurelius.

the class CTConnectionFactory method makeRawConnection.

/**
 * Create a Cassandra-Thrift connection, but do not attempt to
 * set a keyspace on the connection.
 *
 * @return A CTConnection ready to talk to a Cassandra cluster
 * @throws TTransportException on any Thrift transport failure
 */
public CTConnection makeRawConnection() throws TTransportException {
    final Config cfg = cfgRef.get();
    String hostname = cfg.getRandomHost();
    if (log.isDebugEnabled())
        log.debug("Creating TSocket({}, {}, {}, {}, {})", hostname, cfg.port, cfg.username, cfg.password, cfg.timeoutMS);
    TTransport transport = new TFramedTransport(new TSocket(hostname, cfg.port, cfg.timeoutMS), cfg.frameSize);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    Cassandra.Client client = new Cassandra.Client(protocol);
    transport.open();
    if (cfg.username != null) {
        Map<String, String> credentials = new HashMap<String, String>() {

            {
                put(IAuthenticator.USERNAME_KEY, cfg.username);
                put(IAuthenticator.PASSWORD_KEY, cfg.password);
            }
        };
        try {
            client.login(new AuthenticationRequest(credentials));
        } catch (Exception e) {
            // TTransportException will propagate authentication/authorization failure
            throw new TTransportException(e);
        }
    }
    return new CTConnection(transport, client, cfg);
}
Also used : TTransportException(org.apache.thrift.transport.TTransportException) TTransportException(org.apache.thrift.transport.TTransportException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) 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