@Override public Void call() throws Exception { try { QueryStatus status = exploreService.fetchStatus(opInfo); // If operation is still not complete, cancel it. if (status.getStatus() != QueryStatus.OpStatus.FINISHED && status.getStatus() != QueryStatus.OpStatus.CLOSED && status.getStatus() != QueryStatus.OpStatus.CANCELED && status.getStatus() != QueryStatus.OpStatus.ERROR) { LOG.info("Cancelling handle {} with status {} due to timeout", handle.getHandle(), status.getStatus()); // This operation is aysnc, except with Hive CDH 4, in which case cancel throws an unsupported exception exploreService.cancelInternal(handle); } } catch (Throwable e) { LOG.error("Could not cancel handle {} due to exception", handle.getHandle(), e); } finally { LOG.debug("Timing out handle {}", handle); try { // Finally close the operation exploreService.closeInternal(handle, opInfo); } catch (Throwable e) { LOG.error("Exception while closing handle {}", handle, e); } } return null; } });
@Override public Void call() throws Exception { try { QueryStatus status = exploreService.fetchStatus(opInfo); // If operation is still not complete, cancel it. if (status.getStatus() != QueryStatus.OpStatus.FINISHED && status.getStatus() != QueryStatus.OpStatus.CLOSED && status.getStatus() != QueryStatus.OpStatus.CANCELED && status.getStatus() != QueryStatus.OpStatus.ERROR) { LOG.info("Cancelling handle {} with status {} due to timeout", handle.getHandle(), status.getStatus()); // This operation is aysnc, except with Hive CDH 4, in which case cancel throws an unsupported exception exploreService.cancelInternal(handle); } } catch (Throwable e) { LOG.error("Could not cancel handle {} due to exception", handle.getHandle(), e); } finally { LOG.debug("Timing out handle {}", handle); try { // Finally close the operation exploreService.closeInternal(handle, opInfo); } catch (Throwable e) { LOG.error("Exception while closing handle {}", handle, e); } } return null; } });
public QueryInfo(long timestamp, String query, QueryHandle handle, QueryStatus status, boolean isActive) { this.timestamp = timestamp; this.statement = query; this.queryHandle = handle.getHandle(); this.status = status.getStatus(); this.hasResults = status.hasResults(); this.isActive = isActive; }
public QueryInfo(long timestamp, String query, QueryHandle handle, QueryStatus status, boolean isActive) { this.timestamp = timestamp; this.statement = query; this.queryHandle = handle.getHandle(); this.status = status.getStatus(); this.hasResults = status.hasResults(); this.isActive = isActive; }
@Override public List<QueryResult> nextResults(QueryHandle handle, int size) throws ExploreException, HandleNotFoundException, SQLException { startAndWait(); InactiveOperationInfo inactiveOperationInfo = inactiveHandleCache.getIfPresent(handle); if (inactiveOperationInfo != null) { // Operation has been made inactive, so all results should have been fetched already - return empty list. LOG.trace("Returning empty result for inactive handle {}", handle); return ImmutableList.of(); } try { List<QueryResult> results = fetchNextResults(handle, size); QueryStatus status = getStatus(handle); if (results.isEmpty() && status.getStatus() == QueryStatus.OpStatus.FINISHED) { // Since operation has fetched all the results, handle can be timed out aggressively. timeoutAggressively(handle, getResultSchema(handle), status); } return results; } catch (HiveSQLException e) { throw getSqlException(e); } }
@Override public List<QueryResult> nextResults(QueryHandle handle, int size) throws ExploreException, HandleNotFoundException, SQLException { startAndWait(); InactiveOperationInfo inactiveOperationInfo = inactiveHandleCache.getIfPresent(handle); if (inactiveOperationInfo != null) { // Operation has been made inactive, so all results should have been fetched already - return empty list. LOG.trace("Returning empty result for inactive handle {}", handle); return ImmutableList.of(); } try { List<QueryResult> results = fetchNextResults(handle, size); QueryStatus status = getStatus(handle); if (results.isEmpty() && status.getStatus() == QueryStatus.OpStatus.FINISHED) { // Since operation has fetched all the results, handle can be timed out aggressively. timeoutAggressively(handle, getResultSchema(handle), status); } return results; } catch (HiveSQLException e) { throw getSqlException(e); } }
@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); } }
@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); } }
private void doDownloadQueryResults(HttpResponder responder, QueryHandle handle) throws ExploreException, IOException { try { if (handle.equals(QueryHandle.NO_OP) || !exploreService.getStatus(handle).getStatus().equals(QueryStatus.OpStatus.FINISHED)) { responder.sendStatus(HttpResponseStatus.CONFLICT); return; } QueryResultsBodyProducer queryResultsBodyProducer = new QueryResultsBodyProducer(exploreService, handle); responder.sendContent(HttpResponseStatus.OK, queryResultsBodyProducer, EmptyHttpHeaders.INSTANCE); } catch (IllegalArgumentException e) { LOG.debug("Got exception:", e); responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (SQLException e) { LOG.debug("Got exception:", e); responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage())); } catch (HandleNotFoundException e) { if (e.isInactive()) { responder.sendString(HttpResponseStatus.CONFLICT, "Query is inactive"); } else { responder.sendStatus(HttpResponseStatus.NOT_FOUND); } } } }
private void doDownloadQueryResults(HttpResponder responder, QueryHandle handle) throws ExploreException, IOException { try { if (handle.equals(QueryHandle.NO_OP) || !exploreService.getStatus(handle).getStatus().equals(QueryStatus.OpStatus.FINISHED)) { responder.sendStatus(HttpResponseStatus.CONFLICT); return; } QueryResultsBodyProducer queryResultsBodyProducer = new QueryResultsBodyProducer(exploreService, handle); responder.sendContent(HttpResponseStatus.OK, queryResultsBodyProducer, EmptyHttpHeaders.INSTANCE); } catch (IllegalArgumentException e) { LOG.debug("Got exception:", e); responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (SQLException e) { LOG.debug("Got exception:", e); responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage())); } catch (HandleNotFoundException e) { if (e.isInactive()) { responder.sendString(HttpResponseStatus.CONFLICT, "Query is inactive"); } else { responder.sendStatus(HttpResponseStatus.NOT_FOUND); } } } }
private void closeTransaction(QueryHandle handle, OperationInfo opInfo) { try { String txCommitted = opInfo.getSessionConf().get(Constants.Explore.TX_QUERY_CLOSED); if (txCommitted != null && Boolean.parseBoolean(txCommitted)) { LOG.trace("Transaction for handle {} has already been closed", handle); return; } Transaction tx = ConfigurationUtil.get(opInfo.getSessionConf(), Constants.Explore.TX_QUERY_KEY, TxnCodec.INSTANCE); LOG.trace("Closing transaction {} for handle {}", tx, handle); if (opInfo.isReadOnly() || (opInfo.getStatus() != null && opInfo.getStatus().getStatus() == QueryStatus.OpStatus.FINISHED)) { try { txClient.commitOrThrow(tx); } catch (TransactionFailureException e) { txClient.invalidate(tx.getWritePointer()); LOG.info("Invalidating transaction: {}", tx); } } else { txClient.invalidate(tx.getWritePointer()); } } catch (Throwable e) { LOG.error("Got exception while closing transaction.", e); } finally { opInfo.getSessionConf().put(Constants.Explore.TX_QUERY_CLOSED, "true"); } }
private void closeTransaction(QueryHandle handle, OperationInfo opInfo) { try { String txCommitted = opInfo.getSessionConf().get(Constants.Explore.TX_QUERY_CLOSED); if (txCommitted != null && Boolean.parseBoolean(txCommitted)) { LOG.trace("Transaction for handle {} has already been closed", handle); return; } Transaction tx = ConfigurationUtil.get(opInfo.getSessionConf(), Constants.Explore.TX_QUERY_KEY, TxnCodec.INSTANCE); LOG.trace("Closing transaction {} for handle {}", tx, handle); if (opInfo.isReadOnly() || (opInfo.getStatus() != null && opInfo.getStatus().getStatus() == QueryStatus.OpStatus.FINISHED)) { try { txClient.commitOrThrow(tx); } catch (TransactionFailureException e) { txClient.invalidate(tx.getWritePointer()); LOG.info("Invalidating transaction: {}", tx); } } else { txClient.invalidate(tx.getWritePointer()); } } catch (Throwable e) { LOG.error("Got exception while closing transaction.", e); } finally { opInfo.getSessionConf().put(Constants.Explore.TX_QUERY_CLOSED, "true"); } }
executionResult.close(); QueryStatus.OpStatus opStatus = executionResult.getStatus().getStatus(); if (opStatus != QueryStatus.OpStatus.FINISHED) { throw new SQLException(String.format("Query '%s' execution did not finish successfully. " +
executionResult.close(); QueryStatus.OpStatus opStatus = executionResult.getStatus().getStatus(); if (opStatus != QueryStatus.OpStatus.FINISHED) { throw new SQLException(String.format("Query '%s' execution did not finish successfully. " +
try { QueryStatus status = getStatus(handle); if (!status.getStatus().isDone()) { final String userId = SecurityRequestContext.getUserId(); final String userIp = SecurityRequestContext.getUserIP(); return; if (QueryStatus.OpStatus.ERROR.equals(status.getStatus())) { throw new SQLException(status.getErrorMessage(), status.getSqlState());
try { QueryStatus status = getStatus(handle); if (!status.getStatus().isDone()) { final String userId = SecurityRequestContext.getUserId(); final String userIp = SecurityRequestContext.getUserIP(); return; if (QueryStatus.OpStatus.ERROR.equals(status.getStatus())) { throw new SQLException(status.getErrorMessage(), status.getSqlState());
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());
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());
protected QueryStatus fetchStatus(OperationInfo operationInfo) throws ExploreException, HandleNotFoundException, HiveSQLException { QueryStatus queryStatus; try { queryStatus = doFetchStatus(operationInfo.getOperationHandle()); if (QueryStatus.OpStatus.ERROR.equals(queryStatus.getStatus()) && queryStatus.getErrorMessage() == null) { queryStatus = new QueryStatus("Operation failed. See the log for more details.", null); } } catch (HiveSQLException e) { // if this is a sql exception, record it in the query status. // it means that query execution failed, but we can successfully retrieve the status. if (e.getSQLState() != null) { queryStatus = new QueryStatus(e.getMessage(), e.getSQLState()); } else { // this is an internal error - we are not able to retrieve the status throw new ExploreException(e.getMessage(), e); } } operationInfo.setStatus(queryStatus); return queryStatus; }
protected QueryStatus fetchStatus(OperationInfo operationInfo) throws ExploreException, HandleNotFoundException, HiveSQLException { QueryStatus queryStatus; try { queryStatus = doFetchStatus(operationInfo.getOperationHandle()); if (QueryStatus.OpStatus.ERROR.equals(queryStatus.getStatus()) && queryStatus.getErrorMessage() == null) { queryStatus = new QueryStatus("Operation failed. See the log for more details.", null); } } catch (HiveSQLException e) { // if this is a sql exception, record it in the query status. // it means that query execution failed, but we can successfully retrieve the status. if (e.getSQLState() != null) { queryStatus = new QueryStatus(e.getMessage(), e.getSQLState()); } else { // this is an internal error - we are not able to retrieve the status throw new ExploreException(e.getMessage(), e); } } operationInfo.setStatus(queryStatus); return queryStatus; }