if (upstreamResults == null) {
result.set(TaskResult.EMPTY_RESULT);
return;
}
final MergeOperation mergeOperation = new MergeOperation(
clusterService, settings, transportActionProvider, symbolVisitor, mergeNode);
final AtomicInteger countdown = new AtomicInteger(upstreamResults.size());
final UUID operationId = UUID.randomUUID();
statsTables.operationStarted(operationId, mergeNode.contextId(), mergeNode.id());
Futures.addCallback(mergeOperation.result(), new FutureCallback<Object[][]>() {
@Override
public void onSuccess(@Nullable Object[][] rows) {
statsTables.operationFinished(operationId, null);
result.set(new QueryResult(rows));
}
@Override
public void onFailure(@Nonnull Throwable t) {
statsTables.operationFinished(operationId, Exceptions.messageOf(t));
result.setException(t);
}
});
for (final ListenableFuture<TaskResult> upstreamResult : upstreamResults) {
Futures.addCallback(upstreamResult, new FutureCallback<TaskResult>() {
@Override
public void onSuccess(@Nullable TaskResult rows) {
assert rows != null;
traceLogResult(rows);
boolean shouldContinue;
try {
shouldContinue = mergeOperation.addRows(rows.rows());
} catch (Exception ex) {
statsTables.operationFinished(operationId, Exceptions.messageOf(ex));
result.setException(ex);
logger.error("Failed to add rows", ex);
return;
}
if (countdown.decrementAndGet() == 0 || !shouldContinue) {
mergeOperation.finished();
}
}
@Override
public void onFailure(@Nonnull Throwable t) {