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;
}
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);
}
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);
}
}
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);
}
Aggregations