Search in sources :

Example 1 with QueryStatus

use of co.cask.cdap.proto.QueryStatus 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(co.cask.cdap.proto.QueryHandle) ColumnDesc(co.cask.cdap.proto.ColumnDesc) QueryStatus(co.cask.cdap.proto.QueryStatus) Test(org.junit.Test)

Example 2 with QueryStatus

use of co.cask.cdap.proto.QueryStatus 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(co.cask.cdap.proto.QueryHandle) ColumnDesc(co.cask.cdap.proto.ColumnDesc) QueryStatus(co.cask.cdap.proto.QueryStatus) Test(org.junit.Test)

Example 3 with QueryStatus

use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.

the class BaseHiveExploreService method getStatus.

@Override
public QueryStatus getStatus(QueryHandle handle) throws ExploreException, HandleNotFoundException, SQLException {
    startAndWait();
    InactiveOperationInfo inactiveOperationInfo = inactiveHandleCache.getIfPresent(handle);
    if (inactiveOperationInfo != null) {
        // Operation has been made inactive, so return the saved status.
        LOG.trace("Returning saved status for inactive handle {}", handle);
        return inactiveOperationInfo.getStatus();
    }
    try {
        // Fetch status from Hive
        QueryStatus status = fetchStatus(getActiveOperationInfo(handle));
        LOG.trace("Status of handle {} is {}", handle, status);
        // No results or error, so can be timed out aggressively
        if (status.getStatus() == QueryStatus.OpStatus.FINISHED && !status.hasResults()) {
            // In case of a query that writes to a Dataset, we will always fall into this condition,
            // and timing out aggressively will also close the transaction and make the writes visible
            timeoutAggressively(handle, getResultSchema(handle), status);
        } else if (status.getStatus() == QueryStatus.OpStatus.ERROR) {
            // getResultSchema will fail if the query is in error
            timeoutAggressively(handle, ImmutableList.<ColumnDesc>of(), status);
        }
        return status;
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) ColumnDesc(co.cask.cdap.proto.ColumnDesc) QueryStatus(co.cask.cdap.proto.QueryStatus)

Example 4 with QueryStatus

use of co.cask.cdap.proto.QueryStatus 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(co.cask.cdap.explore.service.HandleNotFoundException) ArrayList(java.util.ArrayList) QueryInfo(co.cask.cdap.proto.QueryInfo) QueryHandle(co.cask.cdap.proto.QueryHandle) Map(java.util.Map) HashMap(java.util.HashMap) QueryStatus(co.cask.cdap.proto.QueryStatus)

Example 5 with QueryStatus

use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.

the class Hive14ExploreService method doFetchStatus.

@Override
protected QueryStatus doFetchStatus(OperationHandle operationHandle) throws HiveSQLException, ExploreException, HandleNotFoundException {
    OperationStatus operationStatus;
    CLIService cliService = getCliService();
    // Call the getOperationStatus method based on the number of arguments it expects.
    try {
        if (getOperationStatus.getParameterTypes().length == 2) {
            operationStatus = (OperationStatus) getOperationStatus.invoke(cliService, operationHandle, true);
        } else {
            operationStatus = (OperationStatus) getOperationStatus.invoke(cliService, operationHandle);
        }
    } catch (IndexOutOfBoundsException | IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException("Failed to get the status of the operation.", e);
    }
    @SuppressWarnings("ThrowableResultOfMethodCallIgnored") HiveSQLException hiveExn = operationStatus.getOperationException();
    if (hiveExn != null) {
        return new QueryStatus(hiveExn.getMessage(), hiveExn.getSQLState());
    }
    return new QueryStatus(QueryStatus.OpStatus.valueOf(operationStatus.getState().toString()), operationHandle.hasResultSet());
}
Also used : OperationStatus(org.apache.hive.service.cli.OperationStatus) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) QueryStatus(co.cask.cdap.proto.QueryStatus) CLIService(org.apache.hive.service.cli.CLIService) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

QueryStatus (co.cask.cdap.proto.QueryStatus)18 QueryHandle (co.cask.cdap.proto.QueryHandle)8 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)7 ExploreException (co.cask.cdap.explore.service.ExploreException)4 HandleNotFoundException (co.cask.cdap.explore.service.HandleNotFoundException)3 ColumnDesc (co.cask.cdap.proto.ColumnDesc)3 OperationStatus (org.apache.hive.service.cli.OperationStatus)3 Test (org.junit.Test)3 QueryResult (co.cask.cdap.proto.QueryResult)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 SQLException (java.sql.SQLException)2 ServiceUnavailableException (co.cask.cdap.common.ServiceUnavailableException)1 UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)1 ExploreExecutionResult (co.cask.cdap.explore.client.ExploreExecutionResult)1 QueryInfo (co.cask.cdap.proto.QueryInfo)1 DatasetId (co.cask.cdap.proto.id.DatasetId)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1