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; }
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()); mustCloseHandle = !resultFuture.set(result) || !status.hasResults(); } catch (Exception e) { mustCloseHandle = true;
@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; } });
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; }
@Override public boolean canContainResults() { return status.hasResults(); } }
@Override protected QueryStatus doFetchStatus(OperationHandle operationHandle) throws HiveSQLException, ExploreException, HandleNotFoundException { try { // In Hive 12, CLIService.getOperationStatus returns OperationState. // In Hive 13, CLIService.getOperationStatus returns OperationStatus. // Since we use Hive 13 for dev, we need the following workaround to get Hive 12 working. Class<? extends CLIService> cliServiceClass = getCliService().getClass(); Method m = cliServiceClass.getMethod("getOperationStatus", OperationHandle.class); OperationState operationState = (OperationState) m.invoke(getCliService(), operationHandle); return new QueryStatus(QueryStatus.OpStatus.valueOf(operationState.toString()), operationHandle.hasResultSet()); } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { throw Throwables.propagate(e); } }
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()); mustCloseHandle = !resultFuture.set(result) || !status.hasResults(); } catch (Exception e) { mustCloseHandle = true;
@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; } });
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; }
@Override public boolean canContainResults() { return status.hasResults(); } }
@Override protected QueryStatus doFetchStatus(OperationHandle operationHandle) throws HiveSQLException, ExploreException, HandleNotFoundException { try { // In Hive 12, CLIService.getOperationStatus returns OperationState. // In Hive 13, CLIService.getOperationStatus returns OperationStatus. // Since we use Hive 13 for dev, we need the following workaround to get Hive 12 working. Class<? extends CLIService> cliServiceClass = getCliService().getClass(); Method m = cliServiceClass.getMethod("getOperationStatus", OperationHandle.class); OperationState operationState = (OperationState) m.invoke(getCliService(), operationHandle); return new QueryStatus(QueryStatus.OpStatus.valueOf(operationState.toString()), operationHandle.hasResultSet()); } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { throw Throwables.propagate(e); } }
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 protected QueryResult computeNext() { if (!status.hasResults()) { return endOfData(); } if (delegate != null && delegate.hasNext()) { return delegate.next(); } try { // call the endpoint 'next' to get more results and set delegate List<QueryResult> nextResults = convertRows(exploreClient.nextResults(handle, fetchSize)); delegate = nextResults.iterator(); // At this point, if delegate has no result, there are no more results at all if (!delegate.hasNext()) { return endOfData(); } return delegate.next(); } catch (ExploreException e) { LOG.error("Exception while iterating through the results of query {}", handle.getHandle(), e); throw Throwables.propagate(e); } catch (HandleNotFoundException e) { // Handle may have timed out, or the handle given is just unknown LOG.debug("Received exception", e); return endOfData(); } }
@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()); }
@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 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 protected QueryResult computeNext() { if (!status.hasResults()) { return endOfData(); } if (delegate != null && delegate.hasNext()) { return delegate.next(); } try { // call the endpoint 'next' to get more results and set delegate List<QueryResult> nextResults = convertRows(exploreClient.nextResults(handle, fetchSize)); delegate = nextResults.iterator(); // At this point, if delegate has no result, there are no more results at all if (!delegate.hasNext()) { return endOfData(); } return delegate.next(); } catch (ExploreException e) { LOG.error("Exception while iterating through the results of query {}", handle.getHandle(), e); throw Throwables.propagate(e); } catch (HandleNotFoundException e) { // Handle may have timed out, or the handle given is just unknown LOG.debug("Received exception", e); return endOfData(); } }