Search in sources :

Example 76 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project hive by apache.

the class SessionManager method startTimeoutChecker.

private void startTimeoutChecker() {
    // minimum 3 seconds
    final long interval = Math.max(checkInterval, 3000l);
    final Runnable timeoutChecker = new Runnable() {

        @Override
        public void run() {
            sleepFor(interval);
            while (!shutdown) {
                long current = System.currentTimeMillis();
                for (HiveSession session : new ArrayList<HiveSession>(handleToSession.values())) {
                    if (shutdown) {
                        break;
                    }
                    if (sessionTimeout > 0 && session.getLastAccessTime() + sessionTimeout <= current && (!checkOperation || session.getNoOperationTime() > sessionTimeout)) {
                        SessionHandle handle = session.getSessionHandle();
                        LOG.warn("Session " + handle + " is Timed-out (last access : " + new Date(session.getLastAccessTime()) + ") and will be closed");
                        try {
                            closeSession(handle);
                        } catch (HiveSQLException e) {
                            LOG.warn("Exception is thrown closing session " + handle, e);
                        } finally {
                            Metrics metrics = MetricsFactory.getInstance();
                            if (metrics != null) {
                                metrics.incrementCounter(MetricsConstant.HS2_ABANDONED_SESSIONS);
                            }
                        }
                    } else {
                        session.closeExpiredOperations();
                    }
                }
                sleepFor(interval);
            }
        }

        private void sleepFor(long interval) {
            synchronized (timeoutCheckerLock) {
                try {
                    timeoutCheckerLock.wait(interval);
                } catch (InterruptedException e) {
                // Ignore, and break.
                }
            }
        }
    };
    backgroundOperationPool.execute(timeoutChecker);
}
Also used : Metrics(org.apache.hadoop.hive.common.metrics.common.Metrics) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) ArrayList(java.util.ArrayList) SessionHandle(org.apache.hive.service.cli.SessionHandle) Date(java.util.Date)

Example 77 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project hive by apache.

the class ThriftCLIServiceClient method openSession.

/* (non-Javadoc)
   * @see org.apache.hive.service.cli.ICLIService#openSession(java.lang.String, java.lang.String, java.util.Map)
   */
@Override
public SessionHandle openSession(String username, String password, Map<String, String> configuration) throws HiveSQLException {
    try {
        TOpenSessionReq req = new TOpenSessionReq();
        req.setUsername(username);
        req.setPassword(password);
        req.setConfiguration(configuration);
        TOpenSessionResp resp = cliService.OpenSession(req);
        checkStatus(resp.getStatus());
        return new SessionHandle(resp.getSessionHandle(), resp.getServerProtocolVersion());
    } catch (HiveSQLException e) {
        throw e;
    } catch (Exception e) {
        throw new HiveSQLException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) TOpenSessionReq(org.apache.hive.service.rpc.thrift.TOpenSessionReq) TOpenSessionResp(org.apache.hive.service.rpc.thrift.TOpenSessionResp) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) TException(org.apache.thrift.TException)

Example 78 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project hive by apache.

the class TestHS2HttpServer method testApiServletActiveSessions.

@Test
public void testApiServletActiveSessions() throws Exception {
    String sessionsRoute = "/sessions";
    String initNoSessionsResponse = readFromUrl(apiBaseURL + sessionsRoute);
    Assert.assertTrue("[]".equals(initNoSessionsResponse));
    SessionHandle handle1 = sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap());
    String oneSessionResponse = readFromUrl(apiBaseURL + sessionsRoute);
    List<JsonNode> sessionNodes = getListOfNodes(oneSessionResponse);
    Assert.assertEquals(sessionNodes.size(), 1);
    JsonNode session = sessionNodes.get(0);
    Assert.assertEquals(session.path("sessionId").asText(), handle1.getSessionId().toString());
    Assert.assertEquals(session.path("username").asText(), "user");
    Assert.assertEquals(session.path("ipAddress").asText(), "127.0.0.1");
    Assert.assertEquals(session.path("operationCount").asInt(), 0);
    Assert.assertTrue(session.path("activeTime").canConvertToInt());
    Assert.assertTrue(session.path("idleTime").canConvertToInt());
    SessionHandle handle2 = sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap());
    String twoSessionsResponse = readFromUrl(apiBaseURL + sessionsRoute);
    List<JsonNode> twoSessionsNodes = getListOfNodes(twoSessionsResponse);
    Assert.assertEquals(twoSessionsNodes.size(), 2);
    sm.closeSession(handle1);
    sm.closeSession(handle2);
    String endNoSessionsResponse = readFromUrl(apiBaseURL + sessionsRoute);
    Assert.assertTrue("[]".equals(endNoSessionsResponse));
}
Also used : HashMap(java.util.HashMap) SessionHandle(org.apache.hive.service.cli.SessionHandle) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 79 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project hive by apache.

the class TestHS2HttpServer method testApiServletHistoricalQueries.

@Test
public void testApiServletHistoricalQueries() throws Exception {
    String historicalQueriesRoute = "/queries/historical";
    final SessionHandle handle = sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap());
    String queryString = "SET " + HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY.varname + " = false";
    OperationHandle opHandle = client.executeStatement(handle, queryString, new HashMap());
    client.closeOperation(opHandle);
    opHandle = client.executeStatement(handle, "SELECT 1", new HashMap());
    client.closeOperation(opHandle);
    String queriesResponse = readFromUrl(apiBaseURL + historicalQueriesRoute);
    List<JsonNode> historicalQueries = getListOfNodes(queriesResponse);
    Assert.assertTrue(historicalQueries.size() == 1);
    JsonNode historicalQuery = historicalQueries.get(0);
    Assert.assertEquals(historicalQuery.path("running").asBoolean(), false);
    Assert.assertEquals(historicalQuery.path("state").asText(), "FINISHED");
    Assert.assertTrue(historicalQuery.path("runtime").canConvertToInt());
    Assert.assertTrue(historicalQuery.path("queryDisplay").isObject());
}
Also used : HashMap(java.util.HashMap) SessionHandle(org.apache.hive.service.cli.SessionHandle) JsonNode(com.fasterxml.jackson.databind.JsonNode) OperationHandle(org.apache.hive.service.cli.OperationHandle) Test(org.junit.Test)

Example 80 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project hive by apache.

the class TestSessionHooks method testProxyUser.

/**
 * Create session with proxy user property. Verify the effective session user
 * @throws Exception
 */
@Test
public void testProxyUser() throws Exception {
    String connectingUser = "user1";
    String proxyUser = System.getProperty("user.name");
    Map<String, String> sessConf = new HashMap<String, String>();
    sessConf.put(HiveAuthConstants.HS2_PROXY_USER, proxyUser);
    sessionUserName = proxyUser;
    SessionHandle sessionHandle = client.openSession(connectingUser, "foobar", sessConf);
    Assert.assertEquals(1, SessionHookTest.runCount.get());
    client.closeSession(sessionHandle);
}
Also used : HashMap(java.util.HashMap) SessionHandle(org.apache.hive.service.cli.SessionHandle) Test(org.junit.Test)

Aggregations

SessionHandle (org.apache.hive.service.cli.SessionHandle)83 OperationHandle (org.apache.hive.service.cli.OperationHandle)47 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)39 Test (org.junit.Test)36 IOException (java.io.IOException)20 TException (org.apache.thrift.TException)18 UnknownHostException (java.net.UnknownHostException)16 LoginException (javax.security.auth.login.LoginException)16 ServiceException (org.apache.hive.service.ServiceException)16 HashMap (java.util.HashMap)13 ExploreException (co.cask.cdap.explore.service.ExploreException)12 QueryHandle (co.cask.cdap.proto.QueryHandle)11 CLIServiceClient (org.apache.hive.service.cli.CLIServiceClient)11 RowSet (org.apache.hive.service.cli.RowSet)8 TimeoutException (java.util.concurrent.TimeoutException)7 CLIService (org.apache.hive.service.cli.CLIService)6 ThriftCLIServiceClient (org.apache.hive.service.cli.thrift.ThriftCLIServiceClient)6 File (java.io.File)4 HiveConf (org.apache.hadoop.hive.conf.HiveConf)4 ArrayList (java.util.ArrayList)3