Search in sources :

Example 71 with SessionHandle

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

the class TestOperationLogManager method testOperationLogManager.

@Test
public void testOperationLogManager() throws Exception {
    MyThriftBinaryCLIService service = new MyThriftBinaryCLIService();
    service.init(hiveConf);
    ThriftCLIServiceClient client = new ThriftCLIServiceClient(service);
    SessionManager sessionManager = ((CLIService) service.getService()).getSessionManager();
    SessionHandle session1 = client.openSession("user1", "foobar", Collections.<String, String>emptyMap());
    OperationHandle opHandle1 = client.executeStatement(session1, "select 1 + 1", null);
    Operation operation1 = sessionManager.getOperationManager().getOperation(opHandle1);
    String logLocation = operation1.getOperationLog().toString();
    // as the historic log is enabled, the log dir of the operation1 should be under the historic dir
    assertTrue(logLocation.startsWith(OperationLogManager.getHistoricLogDir()));
    File operationLogFile = new File(operation1.getOperationLog().toString());
    assertTrue(operationLogFile.exists());
    client.closeOperation(opHandle1);
    // now close the session1
    client.closeSession(session1);
    // check that the log of operation1 is exist even if the session1 has been closed
    assertTrue(operationLogFile.exists());
    SessionHandle session2 = client.openSession("user1", "foobar", Collections.<String, String>emptyMap());
    OperationHandle opHandle2 = client.executeStatement(session2, "select 1 + 1", null);
    Operation operation2 = sessionManager.getOperationManager().getOperation(opHandle2);
    // as the historic log is enabled, the log dir of the operation2 should be under the historic dir
    logLocation = operation2.getOperationLog().toString();
    assertTrue(logLocation.startsWith(OperationLogManager.getHistoricLogDir()));
    // remove the query info of operation1 from the cache
    client.closeOperation(opHandle2);
    // the operation1 becomes unreachable
    OperationManager operationManager = sessionManager.getOperationManager();
    assertTrue(operationManager.getAllCachedQueryIds().size() == 1 && operationManager.getLiveQueryInfos().isEmpty());
    assertNull(operationManager.getQueryInfo(opHandle1.getHandleIdentifier().toString()));
    // now the session1 is closed and has no cached query info, the historic session log dir should be returned.
    OperationLogManager logManager = sessionManager.getLogManager().get();
    List<File> expiredLogDirs = logManager.getExpiredSessionLogDirs();
    List<File> expiredOperationLogs = logManager.getExpiredOperationLogFiles();
    assertEquals(operation1.getQueryId(), expiredOperationLogs.get(0).getName());
    assertEquals(session1.getHandleIdentifier().toString(), expiredLogDirs.get(0).getName());
    logManager.removeExpiredOperationLogAndDir();
    // the historic session log dir has been cleanup
    assertFalse(operationLogFile.exists());
    assertFalse(expiredLogDirs.get(0).exists());
    // though session2 is closed, but there exists his operation(operation2) in cache and
    // log file under the historic session log dir, so the historic log dir of session2 would not be cleaned
    client.closeSession(session2);
    assertNotNull(operationManager.getQueryInfo(opHandle2.getHandleIdentifier().toString()));
    assertTrue(operationManager.getAllCachedQueryIds().size() == 1 && operationManager.getLiveQueryInfos().isEmpty());
    expiredOperationLogs = logManager.getExpiredOperationLogFiles();
    expiredLogDirs = logManager.getExpiredSessionLogDirs();
    assertTrue(expiredLogDirs.isEmpty());
    assertTrue(expiredOperationLogs.isEmpty());
    logManager.removeExpiredOperationLogAndDir();
    assertTrue(new File(logLocation).getParentFile().exists());
    FileUtils.deleteQuietly(new File(OperationLogManager.getHistoricLogDir()));
}
Also used : SessionManager(org.apache.hive.service.cli.session.SessionManager) SessionHandle(org.apache.hive.service.cli.SessionHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) File(java.io.File) CLIService(org.apache.hive.service.cli.CLIService) EmbeddedThriftBinaryCLIService(org.apache.hive.service.cli.thrift.EmbeddedThriftBinaryCLIService) ThriftCLIServiceClient(org.apache.hive.service.cli.thrift.ThriftCLIServiceClient) Test(org.junit.Test)

Example 72 with SessionHandle

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

the class TestPluggableHiveSessionImpl method testSessionImpl.

@Test
public void testSessionImpl() throws Exception {
    HiveConf hiveConf = new HiveConf();
    hiveConf.setVar(HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER, HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER.getDefaultValue());
    hiveConf.setVar(HiveConf.ConfVars.HIVE_SESSION_IMPL_CLASSNAME, SampleHiveSessionImpl.class.getName());
    hiveConf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_ENABLE_DOAS, false);
    CLIService cliService = new CLIService(null, true);
    cliService.init(hiveConf);
    ThriftBinaryCLIService service = new ThriftBinaryCLIService(cliService);
    service.init(hiveConf);
    ThriftCLIServiceClient client = new ThriftCLIServiceClient(service);
    SessionHandle sessionHandle = null;
    sessionHandle = client.openSession("tom", "password");
    assertEquals(SampleHiveSessionImpl.class.getName(), service.getHiveConf().getVar(HiveConf.ConfVars.HIVE_SESSION_IMPL_CLASSNAME));
    HiveSession session = cliService.getSessionManager().getSession(sessionHandle);
    assertEquals(SampleHiveSessionImpl.MAGIC_RETURN_VALUE, session.getNoOperationTime());
    client.closeSession(sessionHandle);
}
Also used : ThriftBinaryCLIService(org.apache.hive.service.cli.thrift.ThriftBinaryCLIService) HiveConf(org.apache.hadoop.hive.conf.HiveConf) SessionHandle(org.apache.hive.service.cli.SessionHandle) CLIService(org.apache.hive.service.cli.CLIService) ThriftBinaryCLIService(org.apache.hive.service.cli.thrift.ThriftBinaryCLIService) ThriftCLIServiceClient(org.apache.hive.service.cli.thrift.ThriftCLIServiceClient) Test(org.junit.Test)

Example 73 with SessionHandle

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

the class TestSessionManagerMetrics method testOpenSessionTimeMetrics.

@Test
public void testOpenSessionTimeMetrics() throws Exception {
    String json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_OPEN_SESSION_TIME, "NaN");
    long firstSessionOpen = System.currentTimeMillis();
    SessionHandle handle = sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>());
    json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_OPEN_SESSION_TIME, (double) (System.currentTimeMillis() - firstSessionOpen), 100d);
    long secondSessionOpen = System.currentTimeMillis();
    sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>());
    json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_OPEN_SESSION_TIME, (double) (System.currentTimeMillis() - firstSessionOpen + System.currentTimeMillis() - secondSessionOpen) / 2d, 100d);
    sm.closeSession(handle);
    json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_OPEN_SESSION_TIME, (double) (System.currentTimeMillis() - secondSessionOpen), 100d);
}
Also used : SessionHandle(org.apache.hive.service.cli.SessionHandle) Test(org.junit.Test)

Example 74 with SessionHandle

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

the class TestSessionManagerMetrics method testActiveSessionTimeMetrics.

@Test
public void testActiveSessionTimeMetrics() throws Exception {
    final CyclicBarrier ready = new CyclicBarrier(2);
    CyclicBarrier completed = new CyclicBarrier(2);
    String json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_ACTIVE_SESSION_TIME, "NaN");
    SessionHandle handle = sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>());
    final HiveSession session = sm.getSession(handle);
    OperationManager operationManager = mock(OperationManager.class);
    when(operationManager.newGetTablesOperation(session, "catalog", "schema", "table", null)).thenReturn(new BlockingOperation(session, OperationType.GET_TABLES, ready, completed));
    session.setOperationManager(operationManager);
    long sessionActivateTime = System.currentTimeMillis();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Hive.set(session.getSessionHive());
                OperationHandle handle = session.getTables("catalog", "schema", "table", null);
                session.closeOperation(handle);
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    ready.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                // ignore
                }
            }
        }
    }).start();
    ready.await(2, TimeUnit.SECONDS);
    ready.reset();
    json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_ACTIVE_SESSION_TIME, (double) System.currentTimeMillis() - sessionActivateTime, 100d);
    completed.await(2, TimeUnit.SECONDS);
    ready.await(2, TimeUnit.SECONDS);
    json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_ACTIVE_SESSION_TIME, "NaN");
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) SessionHandle(org.apache.hive.service.cli.SessionHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) TimeoutException(java.util.concurrent.TimeoutException) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) OperationManager(org.apache.hive.service.cli.operation.OperationManager) Test(org.junit.Test)

Example 75 with SessionHandle

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

the class TestSessionManagerMetrics method testOpenSessionMetrics.

@Test
public void testOpenSessionMetrics() throws Exception {
    String json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_OPEN_SESSIONS, 0);
    SessionHandle handle = sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>());
    json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_OPEN_SESSIONS, 1);
    sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>());
    json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_OPEN_SESSIONS, 2);
    sm.closeSession(handle);
    json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_OPEN_SESSIONS, 1);
}
Also used : 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