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();
}
}
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;
}
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;
}
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);
}
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);
}
}
Aggregations