Search in sources :

Example 1 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by caskdata.

the class HiveExploreServiceTimeoutTest method testTimeoutNoResults.

@Test
public void testTimeoutNoResults() throws Exception {
    Set<Long> beforeTxns = transactionManager.getCurrentState().getInProgress().keySet();
    QueryHandle handle = exploreService.execute(NAMESPACE_ID, "drop table if exists not_existing_table_name");
    Set<Long> queryTxns = Sets.difference(transactionManager.getCurrentState().getInProgress().keySet(), beforeTxns);
    Assert.assertFalse(queryTxns.isEmpty());
    QueryStatus status = waitForCompletionStatus(handle, 200, TimeUnit.MILLISECONDS, 20);
    Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
    Assert.assertFalse(status.hasResults());
    List<ColumnDesc> schema = exploreService.getResultSchema(handle);
    // Sleep for some time for txn to get closed
    TimeUnit.SECONDS.sleep(1);
    // Make sure that the transaction got closed
    Assert.assertEquals(ImmutableSet.<Long>of(), Sets.intersection(queryTxns, transactionManager.getCurrentState().getInProgress().keySet()).immutableCopy());
    // Check if calls using inactive handle still work
    Assert.assertEquals(status, exploreService.getStatus(handle));
    Assert.assertEquals(schema, exploreService.getResultSchema(handle));
    exploreService.close(handle);
    // Sleep for timeout to happen
    TimeUnit.SECONDS.sleep(INACTIVE_OPERATION_TIMEOUT_SECS + 3);
    try {
        exploreService.getStatus(handle);
        Assert.fail("Should throw HandleNotFoundException due to operation cleanup");
    } catch (HandleNotFoundException e) {
    // Expected exception due to timeout
    }
}
Also used : QueryHandle(io.cdap.cdap.proto.QueryHandle) ColumnDesc(io.cdap.cdap.proto.ColumnDesc) QueryStatus(io.cdap.cdap.proto.QueryStatus) Test(org.junit.Test)

Example 2 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by caskdata.

the class HiveExploreServiceTimeoutTest method testTimeoutFetchAllResults.

@Test
public void testTimeoutFetchAllResults() throws Exception {
    Set<Long> beforeTxns = transactionManager.getCurrentState().getInProgress().keySet();
    QueryHandle handle = exploreService.execute(NAMESPACE_ID, "select key, value from " + MY_TABLE_NAME);
    Set<Long> queryTxns = Sets.difference(transactionManager.getCurrentState().getInProgress().keySet(), beforeTxns);
    Assert.assertFalse(queryTxns.isEmpty());
    QueryStatus status = waitForCompletionStatus(handle, 200, TimeUnit.MILLISECONDS, 20);
    Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
    Assert.assertTrue(status.hasResults());
    List<ColumnDesc> schema = exploreService.getResultSchema(handle);
    // noinspection StatementWithEmptyBody
    while (!exploreService.nextResults(handle, 100).isEmpty()) {
    // nothing to do
    }
    // Sleep for some time for txn to get closed
    TimeUnit.SECONDS.sleep(1);
    // Make sure that the transaction got closed
    Assert.assertEquals(ImmutableSet.<Long>of(), Sets.intersection(queryTxns, transactionManager.getCurrentState().getInProgress().keySet()).immutableCopy());
    // Check if calls using inactive handle still work
    Assert.assertEquals(status, exploreService.getStatus(handle));
    Assert.assertEquals(schema, exploreService.getResultSchema(handle));
    exploreService.close(handle);
    // Sleep for timeout to happen
    TimeUnit.SECONDS.sleep(INACTIVE_OPERATION_TIMEOUT_SECS + 3);
    try {
        exploreService.getStatus(handle);
        Assert.fail("Should throw HandleNotFoundException due to operation cleanup");
    } catch (HandleNotFoundException e) {
    // Expected exception due to timeout
    }
}
Also used : QueryHandle(io.cdap.cdap.proto.QueryHandle) ColumnDesc(io.cdap.cdap.proto.ColumnDesc) QueryStatus(io.cdap.cdap.proto.QueryStatus) Test(org.junit.Test)

Example 3 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by caskdata.

the class BaseHiveExploreService method getQueries.

@Override
public List<QueryInfo> getQueries(NamespaceId namespace) throws ExploreException, SQLException {
    startAndWait();
    List<QueryInfo> result = new ArrayList<>();
    String namespaceHiveDb = getHiveDatabase(namespace.getNamespace());
    for (Map.Entry<QueryHandle, OperationInfo> entry : activeHandleCache.asMap().entrySet()) {
        try {
            if (namespaceHiveDb.equals(entry.getValue().getHiveDatabase())) {
                // we use empty query statement for get tables, get schemas, we don't need to return it this method call.
                if (!entry.getValue().getStatement().isEmpty()) {
                    QueryStatus status = getStatus(entry.getKey());
                    result.add(new QueryInfo(entry.getValue().getTimestamp(), entry.getValue().getStatement(), entry.getKey(), status, true));
                }
            }
        } catch (HandleNotFoundException e) {
        // ignore the handle not found exception. this method returns all queries and handle, if the
        // handle is removed from the internal cache, then there is no point returning them from here.
        }
    }
    for (Map.Entry<QueryHandle, InactiveOperationInfo> entry : inactiveHandleCache.asMap().entrySet()) {
        InactiveOperationInfo inactiveOperationInfo = entry.getValue();
        if (namespaceHiveDb.equals(inactiveOperationInfo.getHiveDatabase())) {
            // we use empty query statement for get tables, get schemas, we don't need to return it this method call.
            if (!inactiveOperationInfo.getStatement().isEmpty()) {
                if (inactiveOperationInfo.getStatus() == null) {
                    LOG.error("Null status for query {}, handle {}", inactiveOperationInfo.getStatement(), entry.getKey());
                }
                result.add(new QueryInfo(inactiveOperationInfo.getTimestamp(), inactiveOperationInfo.getStatement(), entry.getKey(), inactiveOperationInfo.getStatus(), false));
            }
        }
    }
    Collections.sort(result);
    return result;
}
Also used : HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) ArrayList(java.util.ArrayList) QueryInfo(io.cdap.cdap.proto.QueryInfo) QueryHandle(io.cdap.cdap.proto.QueryHandle) Map(java.util.Map) HashMap(java.util.HashMap) QueryStatus(io.cdap.cdap.proto.QueryStatus)

Example 4 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by caskdata.

the class BaseHiveExploreService method deleteNamespace.

@Override
public QueryHandle deleteNamespace(NamespaceId namespace) throws ExploreException, SQLException {
    startAndWait();
    String customHiveDatabase;
    try {
        customHiveDatabase = namespaceQueryAdmin.get(namespace).getConfig().getHiveDatabase();
    } catch (Exception e) {
        throw new ExploreException(String.format("Failed to get namespace meta for the namespace %s", namespace));
    }
    if (Strings.isNullOrEmpty(customHiveDatabase)) {
        // no custom hive database was given for this namespace so we need to delete it
        try {
            SessionHandle sessionHandle = null;
            OperationHandle operationHandle = null;
            Map<String, String> sessionConf = startSession();
            String database = getHiveDatabase(namespace.getNamespace());
            try {
                sessionHandle = openHiveSession(sessionConf);
                String statement = String.format("DROP DATABASE IF EXISTS %s", database);
                operationHandle = executeAsync(sessionHandle, statement);
                QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, statement, database);
                LOG.info("Deleting database {} with handle {}", database, handle);
                return handle;
            } catch (Throwable e) {
                closeInternal(getQueryHandle(sessionConf), new ReadOnlyOperationInfo(sessionHandle, operationHandle, sessionConf, "", database));
                throw e;
            }
        } catch (HiveSQLException e) {
            throw getSqlException(e);
        } catch (Throwable e) {
            throw new ExploreException(e);
        }
    } else {
        // a custom hive database was provided for this namespace do we don't need to delete it.
        LOG.debug("Custom hive database {}. Skipping delete.", customHiveDatabase, namespace);
        return QueryHandle.NO_OP;
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(io.cdap.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) SQLException(java.sql.SQLException) TableNotFoundException(io.cdap.cdap.explore.service.TableNotFoundException) TException(org.apache.thrift.TException) IOException(java.io.IOException) HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) TransactionFailureException(org.apache.tephra.TransactionFailureException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) ExploreException(io.cdap.cdap.explore.service.ExploreException) FileNotFoundException(java.io.FileNotFoundException) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 5 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by caskdata.

the class BaseHiveExploreService method execute.

@Override
public QueryHandle execute(NamespaceId namespace, String[] statements) throws ExploreException, SQLException {
    Preconditions.checkArgument(statements.length > 0, "There must be at least one statement");
    startAndWait();
    try {
        SessionHandle sessionHandle = null;
        OperationHandle operationHandle = null;
        Map<String, String> sessionConf = startSession(namespace);
        String database = getHiveDatabase(namespace.getNamespace());
        try {
            sessionHandle = openHiveSession(sessionConf);
            // Switch database to the one being passed in.
            setCurrentDatabase(database);
            // synchronously execute all but the last statement
            for (int i = 0; i < statements.length - 1; i++) {
                String statement = statements[i];
                LOG.trace("Executing statement synchronously: {}", statement);
                operationHandle = executeSync(sessionHandle, statement);
                QueryStatus status = doFetchStatus(operationHandle);
                if (QueryStatus.OpStatus.ERROR == status.getStatus()) {
                    throw new HiveSQLException(status.getErrorMessage(), status.getSqlState());
                }
                if (QueryStatus.OpStatus.FINISHED != status.getStatus()) {
                    throw new ExploreException("Expected operation status FINISHED for statement '{}' but received " + status.getStatus());
                }
            }
            String statement = statements[statements.length - 1];
            operationHandle = executeAsync(sessionHandle, statement);
            QueryHandle handle = saveReadWriteOperation(operationHandle, sessionHandle, sessionConf, statement, database);
            LOG.trace("Executing statement: {} with handle {}", statement, handle);
            return handle;
        } catch (Throwable e) {
            closeInternal(getQueryHandle(sessionConf), new ReadWriteOperationInfo(sessionHandle, operationHandle, sessionConf, "", database));
            throw e;
        }
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    } catch (Throwable e) {
        throw new ExploreException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(io.cdap.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) QueryStatus(io.cdap.cdap.proto.QueryStatus) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Aggregations

QueryHandle (io.cdap.cdap.proto.QueryHandle)35 ExploreException (io.cdap.cdap.explore.service.ExploreException)20 SQLException (java.sql.SQLException)13 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)11 OperationHandle (org.apache.hive.service.cli.OperationHandle)11 SessionHandle (org.apache.hive.service.cli.SessionHandle)11 HandleNotFoundException (io.cdap.cdap.explore.service.HandleNotFoundException)9 QueryStatus (io.cdap.cdap.proto.QueryStatus)8 Path (javax.ws.rs.Path)8 IOException (java.io.IOException)7 Callable (java.util.concurrent.Callable)5 POST (javax.ws.rs.POST)5 Test (org.junit.Test)5 JsonObject (com.google.gson.JsonObject)4 UnsupportedTypeException (io.cdap.cdap.api.data.schema.UnsupportedTypeException)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)3 BadRequestException (io.cdap.cdap.common.BadRequestException)3 ColumnDesc (io.cdap.cdap.proto.ColumnDesc)3 QueryResult (io.cdap.cdap.proto.QueryResult)3