Search in sources :

Example 1 with TTransport

use of org.apache.thrift.transport.TTransport 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 TTransport

use of org.apache.thrift.transport.TTransport in project hive by apache.

the class ThriftBinaryCLIService method run.

@Override
public void run() {
    try {
        // Server thread pool
        String threadPoolName = "HiveServer2-Handler-Pool";
        ExecutorService executorService = new ThreadPoolExecutorWithOomHook(minWorkerThreads, maxWorkerThreads, workerKeepAliveTime, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryWithGarbageCleanup(threadPoolName), oomHook);
        // Thrift configs
        hiveAuthFactory = new HiveAuthFactory(hiveConf);
        TTransportFactory transportFactory = hiveAuthFactory.getAuthTransFactory();
        TProcessorFactory processorFactory = hiveAuthFactory.getAuthProcFactory(this);
        TServerSocket serverSocket = null;
        List<String> sslVersionBlacklist = new ArrayList<String>();
        for (String sslVersion : hiveConf.getVar(ConfVars.HIVE_SSL_PROTOCOL_BLACKLIST).split(",")) {
            sslVersionBlacklist.add(sslVersion);
        }
        if (!hiveConf.getBoolVar(ConfVars.HIVE_SERVER2_USE_SSL)) {
            serverSocket = HiveAuthUtils.getServerSocket(hiveHost, portNum);
        } else {
            String keyStorePath = hiveConf.getVar(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH).trim();
            if (keyStorePath.isEmpty()) {
                throw new IllegalArgumentException(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH.varname + " Not configured for SSL connection");
            }
            String keyStorePassword = ShimLoader.getHadoopShims().getPassword(hiveConf, HiveConf.ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PASSWORD.varname);
            serverSocket = HiveAuthUtils.getServerSSLSocket(hiveHost, portNum, keyStorePath, keyStorePassword, sslVersionBlacklist);
        }
        // Server args
        int maxMessageSize = hiveConf.getIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE);
        int requestTimeout = (int) hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_LOGIN_TIMEOUT, TimeUnit.SECONDS);
        int beBackoffSlotLength = (int) hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_LOGIN_BEBACKOFF_SLOT_LENGTH, TimeUnit.MILLISECONDS);
        TThreadPoolServer.Args sargs = new TThreadPoolServer.Args(serverSocket).processorFactory(processorFactory).transportFactory(transportFactory).protocolFactory(new TBinaryProtocol.Factory()).inputProtocolFactory(new TBinaryProtocol.Factory(true, true, maxMessageSize, maxMessageSize)).requestTimeout(requestTimeout).requestTimeoutUnit(TimeUnit.SECONDS).beBackoffSlotLength(beBackoffSlotLength).beBackoffSlotLengthUnit(TimeUnit.MILLISECONDS).executorService(executorService);
        // TCP Server
        server = new TThreadPoolServer(sargs);
        server.setServerEventHandler(new TServerEventHandler() {

            @Override
            public ServerContext createContext(TProtocol input, TProtocol output) {
                Metrics metrics = MetricsFactory.getInstance();
                if (metrics != null) {
                    try {
                        metrics.incrementCounter(MetricsConstant.OPEN_CONNECTIONS);
                        metrics.incrementCounter(MetricsConstant.CUMULATIVE_CONNECTION_COUNT);
                    } catch (Exception e) {
                        LOG.warn("Error Reporting JDO operation to Metrics system", e);
                    }
                }
                return new ThriftCLIServerContext();
            }

            @Override
            public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
                Metrics metrics = MetricsFactory.getInstance();
                if (metrics != null) {
                    try {
                        metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS);
                    } catch (Exception e) {
                        LOG.warn("Error Reporting JDO operation to Metrics system", e);
                    }
                }
                ThriftCLIServerContext context = (ThriftCLIServerContext) serverContext;
                SessionHandle sessionHandle = context.getSessionHandle();
                if (sessionHandle != null) {
                    LOG.info("Session disconnected without closing properly. ");
                    try {
                        boolean close = cliService.getSessionManager().getSession(sessionHandle).getHiveConf().getBoolVar(ConfVars.HIVE_SERVER2_CLOSE_SESSION_ON_DISCONNECT);
                        LOG.info((close ? "" : "Not ") + "Closing the session: " + sessionHandle);
                        if (close) {
                            cliService.closeSession(sessionHandle);
                        }
                    } catch (HiveSQLException e) {
                        LOG.warn("Failed to close session: " + e, e);
                    }
                }
            }

            @Override
            public void preServe() {
            }

            @Override
            public void processContext(ServerContext serverContext, TTransport input, TTransport output) {
                currentServerContext.set(serverContext);
            }
        });
        String msg = "Starting " + ThriftBinaryCLIService.class.getSimpleName() + " on port " + portNum + " with " + minWorkerThreads + "..." + maxWorkerThreads + " worker threads";
        LOG.info(msg);
        server.serve();
    } catch (Throwable t) {
        LOG.error("Error starting HiveServer2: could not start " + ThriftBinaryCLIService.class.getSimpleName(), t);
        System.exit(-1);
    }
}
Also used : ThreadFactoryWithGarbageCleanup(org.apache.hive.service.server.ThreadFactoryWithGarbageCleanup) TServerEventHandler(org.apache.thrift.server.TServerEventHandler) ArrayList(java.util.ArrayList) HiveAuthFactory(org.apache.hive.service.auth.HiveAuthFactory) TProcessorFactory(org.apache.thrift.TProcessorFactory) MetricsFactory(org.apache.hadoop.hive.common.metrics.common.MetricsFactory) TTransportFactory(org.apache.thrift.transport.TTransportFactory) TProcessorFactory(org.apache.thrift.TProcessorFactory) TServerSocket(org.apache.thrift.transport.TServerSocket) Metrics(org.apache.hadoop.hive.common.metrics.common.Metrics) TProtocol(org.apache.thrift.protocol.TProtocol) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) TTransportFactory(org.apache.thrift.transport.TTransportFactory) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ServerContext(org.apache.thrift.server.ServerContext) ExecutorService(java.util.concurrent.ExecutorService) HiveAuthFactory(org.apache.hive.service.auth.HiveAuthFactory) TTransport(org.apache.thrift.transport.TTransport) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer)

Example 3 with TTransport

use of org.apache.thrift.transport.TTransport in project alluxio by Alluxio.

the class ThriftClientPool method createNewResource.

/**
   * Creates a thrift client instance.
   *
   * @return the thrift client created
   * @throws IOException if it fails to create a thrift client
   */
@Override
protected T createNewResource() throws IOException {
    TTransport transport = mTransportProvider.getClientTransport(mParentSubject, mAddress);
    TProtocol binaryProtocol = new TBinaryProtocol(transport);
    T client = createThriftClient(new TMultiplexedProtocol(binaryProtocol, mServiceName));
    TException exception;
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_MS, MAX_SLEEP_MS, RPC_MAX_NUM_RETRY);
    do {
        LOG.info("Alluxio client (version {}) is trying to connect with {} {} @ {}", RuntimeConstants.VERSION, mServiceName, mAddress);
        try {
            if (!transport.isOpen()) {
                transport.open();
            }
            if (transport.isOpen()) {
                checkVersion(client);
            }
            LOG.info("Client registered with {} @ {}", mServiceName, mAddress);
            return client;
        } catch (TTransportException e) {
            if (e.getCause() instanceof java.net.SocketTimeoutException) {
                // Do not retry if socket timeout.
                String message = "Thrift transport open times out. Please check whether the " + "authentication types match between client and server. Note that NOSASL client " + "is not able to connect to servers with SIMPLE security mode.";
                throw new IOException(message, e);
            }
            LOG.warn("Failed to connect ({}) to {} @ {}: {}", retryPolicy.getRetryCount(), mServiceName, mAddress, e.getMessage());
            exception = e;
        }
    } while (retryPolicy.attemptRetry());
    LOG.error("Failed after " + retryPolicy.getRetryCount() + " retries.");
    Preconditions.checkNotNull(exception);
    throw new IOException(exception);
}
Also used : TException(org.apache.thrift.TException) ExponentialBackoffRetry(alluxio.retry.ExponentialBackoffRetry) TTransportException(org.apache.thrift.transport.TTransportException) IOException(java.io.IOException) TMultiplexedProtocol(org.apache.thrift.protocol.TMultiplexedProtocol) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TTransport(org.apache.thrift.transport.TTransport) RetryPolicy(alluxio.retry.RetryPolicy)

Example 4 with TTransport

use of org.apache.thrift.transport.TTransport in project alluxio by Alluxio.

the class TransportProviderTest method customAuthenticationEmptyPassword.

/**
   * In CUSTOM mode, if client's password is empty, an exception should be thrown in server side.
   */
@Test
public void customAuthenticationEmptyPassword() throws Exception {
    Configuration.set(PropertyKey.SECURITY_AUTHENTICATION_TYPE, AuthType.CUSTOM.getAuthName());
    Configuration.set(PropertyKey.SECURITY_AUTHENTICATION_CUSTOM_PROVIDER_CLASS, ExactlyMatchAuthenticationProvider.class.getName());
    mTransportProvider = TransportProvider.Factory.create();
    // start server
    startServerThread();
    // check case that password is empty
    mThrown.expect(TTransportException.class);
    mThrown.expectMessage("Peer indicated failure: Plain authentication failed: No password provided");
    TTransport client = ((PlainSaslTransportProvider) mTransportProvider).getClientTransport(ExactlyMatchAuthenticationProvider.USERNAME, "", mServerAddress);
    try {
        client.open();
    } finally {
        mServer.stop();
    }
}
Also used : TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.Test)

Example 5 with TTransport

use of org.apache.thrift.transport.TTransport in project alluxio by Alluxio.

the class TransportProviderTest method customAuthenticationExactNamePasswordMatch.

/**
   * In CUSTOM mode, the TTransport mechanism is PLAIN. When server authenticate the connected
   * client user, it use configured AuthenticationProvider. If the username:password pair matches, a
   * connection should be built.
   */
@Test
public void customAuthenticationExactNamePasswordMatch() throws Exception {
    Configuration.set(PropertyKey.SECURITY_AUTHENTICATION_TYPE, AuthType.CUSTOM.getAuthName());
    Configuration.set(PropertyKey.SECURITY_AUTHENTICATION_CUSTOM_PROVIDER_CLASS, ExactlyMatchAuthenticationProvider.class.getName());
    mTransportProvider = TransportProvider.Factory.create();
    // start server
    startServerThread();
    // when connecting, authentication happens. User's name:pwd pair matches and auth pass.
    TTransport client = ((PlainSaslTransportProvider) mTransportProvider).getClientTransport(ExactlyMatchAuthenticationProvider.USERNAME, ExactlyMatchAuthenticationProvider.PASSWORD, mServerAddress);
    client.open();
    Assert.assertTrue(client.isOpen());
    // clean up
    client.close();
    mServer.stop();
}
Also used : TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.Test)

Aggregations

TTransport (org.apache.thrift.transport.TTransport)192 TProtocol (org.apache.thrift.protocol.TProtocol)81 TSocket (org.apache.thrift.transport.TSocket)80 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)75 TTransportException (org.apache.thrift.transport.TTransportException)46 Test (org.junit.Test)41 TException (org.apache.thrift.TException)38 TFramedTransport (org.apache.thrift.transport.TFramedTransport)34 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)22 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)17 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)14 TMessage (org.apache.thrift.protocol.TMessage)10 Hbase (org.apache.hadoop.hbase.thrift.generated.Hbase)8 THttpClient (org.apache.thrift.transport.THttpClient)8 TSaslClientTransport (org.apache.thrift.transport.TSaslClientTransport)8 GameRPC (com.code.server.rpc.idl.GameRPC)7 Socket (java.net.Socket)7 HashMap (java.util.HashMap)6 SerializerException (net.morimekta.providence.serializer.SerializerException)6