Search in sources :

Example 1 with ResultReceiver

use of io.crate.action.sql.ResultReceiver 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 2 with ResultReceiver

use of io.crate.action.sql.ResultReceiver in project crate by crate.

the class BulkPortal method emitResults.

private void emitResults(UUID jobId, JobsLogs jobsLogs, List<CompletableFuture<Long>> completedResultFutures) {
    assert completedResultFutures.size() == resultReceivers.size() : "number of result must match number of rowReceivers";
    Long[] cells = new Long[1];
    RowN row = new RowN(cells);
    for (int i = 0; i < completedResultFutures.size(); i++) {
        CompletableFuture<Long> completedResultFuture = completedResultFutures.get(i);
        ResultReceiver resultReceiver = resultReceivers.get(i);
        try {
            Long rowCount = completedResultFuture.join();
            cells[0] = rowCount == null ? Executor.ROWCOUNT_ERROR : rowCount;
        } catch (Throwable t) {
            cells[0] = Executor.ROWCOUNT_ERROR;
        }
        resultReceiver.setNextRow(row);
        resultReceiver.allFinished(false);
    }
    jobsLogs.logExecutionEnd(jobId, null);
}
Also used : RowN(io.crate.data.RowN) ResultReceiver(io.crate.action.sql.ResultReceiver)

Example 3 with ResultReceiver

use of io.crate.action.sql.ResultReceiver in project crate by crate.

the class QueryStats method updateAndLastGetMetricValue.

private double updateAndLastGetMetricValue(String query, MetricType type, boolean overall) {
    try {
        String queryUID = query + type;
        String queryPattern = String.format(Locale.ENGLISH, QUERY_PATTERN, query);
        long lastTs = updateAndGetLastExecutedTsFor(queryUID);
        session.bind(UNNAMED, NAME, Arrays.asList(lastTs, queryPattern, lastTs, queryPattern), null);
        session.execute(UNNAMED, 0, new ResultReceiver() {

            private final CompletableFuture<Boolean> completionFuture = new CompletableFuture<>();

            private final List<Row> rows = new ArrayList<>();

            @Override
            public void setNextRow(Row row) {
                rows.add(row);
            }

            @Override
            public void allFinished(boolean interrupted) {
                double value = overall ? getTotalMetricValue(rows, type.ordinal()) : getMetricValue(rows, query, type.ordinal());
                metrics.put(queryUID, value);
                completionFuture.complete(interrupted);
            }

            @Override
            public void fail(@Nonnull Throwable t) {
                logger.error("Failed to process metric results!", t);
                completionFuture.completeExceptionally(t);
            }

            @Override
            public CompletableFuture<?> completionFuture() {
                return completionFuture;
            }

            @Override
            public void batchFinished() {
            }
        });
        session.sync();
        return metrics.getOrDefault(queryUID, .0);
    } catch (Throwable t) {
        throw SQLExceptions.createSQLActionException(t);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Row(io.crate.data.Row) ResultReceiver(io.crate.action.sql.ResultReceiver)

Example 4 with ResultReceiver

use of io.crate.action.sql.ResultReceiver in project crate by crate.

the class ConnectionContext method handleExecute.

/**
     * Execute Message
     * Header:
     * | 'E' | int32 len
     * <p>
     * Body:
     * | string portalName
     * | int32 maxRows (0 = unlimited)
     */
private void handleExecute(ChannelBuffer buffer, Channel channel) {
    String portalName = readCString(buffer);
    int maxRows = buffer.readInt();
    String query = session.getQuery(portalName);
    if (query.isEmpty()) {
        // remove portal so that it doesn't stick around and no attempt to batch it with follow up statement is made
        session.close((byte) 'P', portalName);
        Messages.sendEmptyQueryResponse(channel);
        return;
    }
    List<? extends DataType> outputTypes = session.getOutputTypes(portalName);
    ResultReceiver resultReceiver;
    if (outputTypes == null) {
        // this is a DML query
        maxRows = 0;
        resultReceiver = new RowCountReceiver(query, channel);
    } else {
        // query with resultSet
        resultReceiver = new ResultSetReceiver(query, channel, outputTypes, session.getResultFormatCodes(portalName));
    }
    session.execute(portalName, maxRows, resultReceiver);
}
Also used : ResultReceiver(io.crate.action.sql.ResultReceiver)

Aggregations

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