use of io.crate.concurrent.CountdownFutureCallback in project crate by crate.
the class JobContextService method killContexts.
private ListenableFuture<Integer> killContexts(Collection<UUID> toKill) {
assert !toKill.isEmpty() : "toKill must not be empty";
int numKilled = 0;
CountdownFutureCallback countDownFuture = new CountdownFutureCallback(toKill.size());
for (UUID jobId : toKill) {
JobExecutionContext ctx = activeContexts.get(jobId);
if (ctx != null) {
ctx.completionFuture().whenComplete(countDownFuture);
ctx.kill();
numKilled++;
} else {
// no kill but we need to count down
countDownFuture.onSuccess();
}
}
final SettableFuture<Integer> result = SettableFuture.create();
final int finalNumKilled = numKilled;
countDownFuture.whenComplete((r, e) -> result.set(finalNumKilled));
return result;
}
use of io.crate.concurrent.CountdownFutureCallback in project crate by crate.
the class BatchPortal method sync.
@Override
public CompletableFuture<Void> sync(Planner planner, JobsLogs jobsLogs) {
CountdownFutureCallback completionCallback = new CountdownFutureCallback(analysis.size());
for (int i = 0; i < analysis.size(); i++) {
UUID jobId = UUID.randomUUID();
Plan plan;
String stmt = queries.get(i);
try {
plan = planner.plan(analysis.get(i), jobId, 0, 0);
} catch (Throwable t) {
jobsLogs.logPreExecutionFailure(jobId, stmt, SQLExceptions.messageOf(t));
throw t;
}
ResultReceiver resultReceiver = resultReceivers.get(i);
jobsLogs.logExecutionStart(jobId, stmt);
JobsLogsUpdateListener jobsLogsUpdateListener = new JobsLogsUpdateListener(jobId, jobsLogs);
resultReceiver.completionFuture().whenComplete(jobsLogsUpdateListener).whenComplete(completionCallback);
BatchConsumer consumer = new BatchConsumerToResultReceiver(resultReceiver, 0);
portalContext.getExecutor().execute(plan, consumer, new RowN(batchParams.toArray()));
}
synced = true;
return completionCallback;
}
use of io.crate.concurrent.CountdownFutureCallback in project crate by crate.
the class TasksService method killTasks.
private CompletableFuture<Integer> killTasks(Collection<UUID> toKill, String userName, @Nullable String reason) {
assert !toKill.isEmpty() : "toKill must not be empty";
int numKilled = 0;
CountdownFutureCallback countDownFuture = new CountdownFutureCallback(toKill.size());
boolean isSuperUser = userName.equals(User.CRATE_USER.name());
for (UUID jobId : toKill) {
RootTask ctx = activeTasks.get(jobId);
if (ctx == null) {
// no kill but we need to count down
countDownFuture.onSuccess();
continue;
}
// superuser can always kill jobs; normal users only their own jobs
if (isSuperUser || ctx.userName().equals(userName)) {
recentlyFailed.put(jobId, failedSentinel);
ctx.completionFuture().whenComplete(countDownFuture);
ctx.kill(reason);
numKilled++;
} else {
// no kill but we need to count down
countDownFuture.onSuccess();
}
}
final int finalNumKilled = numKilled;
return countDownFuture.handle((r, f) -> finalNumKilled);
}
Aggregations