use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.
the class AbstractExploreClient method getFutureResultsFromHandle.
/**
* Create a {@link ListenableFuture} object by polling the Explore service using the
* {@link ListenableFuture} containing a {@link QueryHandle}.
*/
private ListenableFuture<ExploreExecutionResult> getFutureResultsFromHandle(final ListenableFuture<QueryHandle> futureHandle) {
final StatementExecutionFuture resultFuture = new StatementExecutionFuture(this, futureHandle);
Futures.addCallback(futureHandle, new FutureCallback<QueryHandle>() {
@Override
public void onSuccess(final QueryHandle handle) {
boolean mustCloseHandle;
try {
QueryStatus status = getStatus(handle);
if (!status.getStatus().isDone()) {
final String userId = SecurityRequestContext.getUserId();
final String userIp = SecurityRequestContext.getUserIP();
executor.schedule(new Runnable() {
@Override
public void run() {
SecurityRequestContext.setUserId(userId);
SecurityRequestContext.setUserIP(userIp);
onSuccess(handle);
}
}, 300, TimeUnit.MILLISECONDS);
return;
}
if (QueryStatus.OpStatus.ERROR.equals(status.getStatus())) {
throw new SQLException(status.getErrorMessage(), status.getSqlState());
}
ExploreExecutionResult result = new ClientExploreExecutionResult(AbstractExploreClient.this, handle, status);
mustCloseHandle = !resultFuture.set(result) || !status.hasResults();
} catch (Exception e) {
mustCloseHandle = true;
resultFuture.setException(e);
}
if (mustCloseHandle) {
try {
close(handle);
} catch (Throwable t) {
LOG.warn("Failed to close handle {}", handle, t);
}
}
}
@Override
public void onFailure(Throwable t) {
resultFuture.setException(t);
}
}, executor);
return resultFuture;
}
Aggregations