Search in sources :

Example 1 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project lucida by claritylab.

the class QAClient method main.

public static void main(String[] args) {
    // Collect the port number.
    int port = 8083;
    if (args.length >= 1) {
        port = Integer.parseInt(args[0]);
    }
    // User.
    String LUCID = "Clinc";
    QuerySpec create_spec = new QuerySpec();
    // Knowledge.
    final QueryInput knowledge_text = createQueryInput("text", "Clinc is created by Jason and Lingjia.", "1234567");
    final QueryInput knowledge_url = createQueryInput("url", "https://en.wikipedia.org/wiki/Apple_Inc.", "abcdefg");
    final QuerySpec knowledge = createQuerySpec("knowledge", new ArrayList<QueryInput>() {

        {
            add(knowledge_text);
            add(knowledge_url);
        }
    });
    // Unlearn.
    final QueryInput knowledge_unlearn_input = createQueryInput("unlearn", "", "abcdefg");
    final QuerySpec knowledge_unlearn_spec = createQuerySpec("unlearn knowledge", new ArrayList<QueryInput>() {

        {
            add(knowledge_unlearn_input);
        }
    });
    // Query.
    final QueryInput query_input = createQueryInput("text", "Who created Clinc?", "");
    final QuerySpec query = createQuerySpec("query", new ArrayList<QueryInput>() {

        {
            add(query_input);
        }
    });
    // Initialize thrift objects.
    // TTransport transport = new TSocket("clarity08.eecs.umich.edu", port);
    TTransport transport = new TSocket("localhost", port);
    TProtocol protocol = new TBinaryProtocol(new TFramedTransport(transport));
    LucidaService.Client client = new LucidaService.Client(protocol);
    try {
        // Talk to the server.
        transport.open();
        System.out.println("///// Connecting to OpenEphyra at port " + port + " ... /////");
        // Learn and ask.
        client.create(LUCID, create_spec);
        client.learn(LUCID, knowledge);
        System.out.println("///// Query input: /////");
        System.out.println(query_input.data.get(0));
        String answer = client.infer(LUCID, query);
        // Print the answer.
        System.out.println("///// Answer: /////");
        System.out.println(answer);
        // Unlearn and ask again.
        client.learn(LUCID, knowledge_unlearn_spec);
        System.out.println("///// Query input: /////");
        System.out.println(query_input.data.get(0));
        answer = client.infer(LUCID, query);
        // Print the answer.
        System.out.println("///// Answer: /////");
        System.out.println(answer);
        transport.close();
    } catch (TException x) {
        x.printStackTrace();
    }
}
Also used : TException(org.apache.thrift.TException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 2 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project buck by facebook.

the class ThriftCoordinatorClient method start.

public ThriftCoordinatorClient start() throws IOException {
    transport = new TFramedTransport(new TSocket(remoteHost, remotePort));
    Stopwatch stopwatch = Stopwatch.createStarted();
    while (true) {
        try {
            transport.open();
            break;
        } catch (TTransportException e) {
            if (stopwatch.elapsed(TimeUnit.SECONDS) > MAX_CONNECT_TIMEOUT_SECONDS) {
                throw new IOException(String.format("Failed to connect. Coordinator is still not healthy after [%d] seconds.", MAX_CONNECT_TIMEOUT_SECONDS));
            }
            LOG.debug("Coordinator server currently not available. Retrying in a bit...");
            try {
                Thread.sleep(TimeUnit.SECONDS.toMillis(RETRY_TIMEOUT_SECONDS));
            } catch (InterruptedException innerException) {
                throw new RuntimeException(innerException);
            }
        }
    }
    TProtocol protocol = new TBinaryProtocol(transport);
    client = new CoordinatorService.Client(protocol);
    return this;
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) Stopwatch(com.google.common.base.Stopwatch) TTransportException(org.apache.thrift.transport.TTransportException) CoordinatorService(com.facebook.buck.distributed.thrift.CoordinatorService) IOException(java.io.IOException) TSocket(org.apache.thrift.transport.TSocket)

Example 3 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project heron by twitter.

the class ScribeSink method open.

// Open the TTransport connection and client to scribe server
private boolean open() {
    try {
        TSocket socket = new TSocket((String) config.get(KEY_SCRIBE_HOST), TypeUtils.getInteger(config.get(KEY_SCRIBE_PORT)), TypeUtils.getInteger(config.get(KEY_SCRIBE_TIMEOUT_MS)));
        transport = new TFramedTransport(socket);
        transport.open();
    } catch (TException tx) {
        LOG.log(Level.SEVERE, "Failed to open connection to scribe server " + connectionString(), tx);
        return false;
    }
    LOG.info("Opened connection to scribe server " + connectionString());
    TProtocol protocol = new TBinaryProtocol(transport);
    client = new scribe.Client(protocol);
    return true;
}
Also used : TException(org.apache.thrift.TException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TSocket(org.apache.thrift.transport.TSocket) org.apache.scribe.scribe(org.apache.scribe.scribe)

Example 4 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project janusgraph by JanusGraph.

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();
    log.debug("Creating TSocket({}, {}, {}, {}, {})", hostname, cfg.port, cfg.username, cfg.password, cfg.timeoutMS);
    TSocket socket;
    if (null != cfg.sslTruststoreLocation && !cfg.sslTruststoreLocation.isEmpty()) {
        TSSLTransportFactory.TSSLTransportParameters params = new TSSLTransportFactory.TSSLTransportParameters() {

            {
                setTrustStore(cfg.sslTruststoreLocation, cfg.sslTruststorePassword);
            }
        };
        socket = TSSLTransportFactory.getClientSocket(hostname, cfg.port, cfg.timeoutMS, params);
    } else {
        socket = new TSocket(hostname, cfg.port, cfg.timeoutMS);
    }
    TTransport transport = new TFramedTransport(socket, cfg.frameSize);
    log.trace("Created transport {}", transport);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    Cassandra.Client client = new Cassandra.Client(protocol);
    if (!transport.isOpen()) {
        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 : HashMap(java.util.HashMap) Cassandra(org.apache.cassandra.thrift.Cassandra) TTransportException(org.apache.thrift.transport.TTransportException) TSSLTransportFactory(org.apache.thrift.transport.TSSLTransportFactory) TTransportException(org.apache.thrift.transport.TTransportException) TException(org.apache.thrift.TException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) AuthenticationRequest(org.apache.cassandra.thrift.AuthenticationRequest) TSocket(org.apache.thrift.transport.TSocket)

Example 5 with TFramedTransport

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

the class CassandraClientPoolingContainer method eagerlyCleanupReadBuffersFromIdleConnection.

private static void eagerlyCleanupReadBuffersFromIdleConnection(CassandraClient idleClient, InetSocketAddress host) {
    // eagerly cleanup idle-connection read buffer to keep a smaller memory footprint
    try {
        TTransport transport = idleClient.getInputProtocol().getTransport();
        if (transport instanceof TFramedTransport) {
            Field readBuffer = ((TFramedTransport) transport).getClass().getDeclaredField("readBuffer_");
            readBuffer.setAccessible(true);
            TMemoryInputTransport memoryInputTransport = (TMemoryInputTransport) readBuffer.get(transport);
            byte[] underlyingBuffer = memoryInputTransport.getBuffer();
            if (underlyingBuffer != null && memoryInputTransport.getBytesRemainingInBuffer() == 0) {
                log.debug("During {} check-in, cleaned up a read buffer of {} bytes of host {}", UnsafeArg.of("pool", idleClient), SafeArg.of("bufferLength", underlyingBuffer.length), SafeArg.of("host", CassandraLogHelper.host(host)));
                memoryInputTransport.reset(PtBytes.EMPTY_BYTE_ARRAY);
            }
        }
    } catch (Exception e) {
        log.debug("Couldn't clean up read buffers on pool check-in.", e);
    }
}
Also used : Field(java.lang.reflect.Field) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TMemoryInputTransport(org.apache.thrift.transport.TMemoryInputTransport) TTransportException(org.apache.thrift.transport.TTransportException) FunctionCheckedException(com.palantir.common.base.FunctionCheckedException)

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