Search in sources :

Example 1 with CountdownFutureCallback

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;
}
Also used : CountdownFutureCallback(io.crate.concurrent.CountdownFutureCallback)

Example 2 with CountdownFutureCallback

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;
}
Also used : RowN(io.crate.data.RowN) CountdownFutureCallback(io.crate.concurrent.CountdownFutureCallback) BatchConsumerToResultReceiver(io.crate.action.sql.BatchConsumerToResultReceiver) UUID(java.util.UUID) Plan(io.crate.planner.Plan) BatchConsumerToResultReceiver(io.crate.action.sql.BatchConsumerToResultReceiver) ResultReceiver(io.crate.action.sql.ResultReceiver) BatchConsumer(io.crate.data.BatchConsumer)

Example 3 with CountdownFutureCallback

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);
}
Also used : CountdownFutureCallback(io.crate.concurrent.CountdownFutureCallback) UUID(java.util.UUID)

Aggregations

CountdownFutureCallback (io.crate.concurrent.CountdownFutureCallback)3 UUID (java.util.UUID)2 BatchConsumerToResultReceiver (io.crate.action.sql.BatchConsumerToResultReceiver)1 ResultReceiver (io.crate.action.sql.ResultReceiver)1 BatchConsumer (io.crate.data.BatchConsumer)1 RowN (io.crate.data.RowN)1 Plan (io.crate.planner.Plan)1