use of io.trino.execution.buffer.BufferResult in project trino by trinodb.
the class TestSqlTask method testSimpleQuery.
@Test(timeOut = 30_000)
public void testSimpleQuery() throws Exception {
SqlTask sqlTask = createInitialTask();
assertEquals(sqlTask.getTaskStatus().getState(), TaskState.RUNNING);
assertEquals(sqlTask.getTaskStatus().getVersion(), STARTING_VERSION);
sqlTask.updateTask(TEST_SESSION, Optional.of(PLAN_FRAGMENT), ImmutableList.of(new SplitAssignment(TABLE_SCAN_NODE_ID, ImmutableSet.of(SPLIT), true)), createInitialEmptyOutputBuffers(PARTITIONED).withBuffer(OUT, 0).withNoMoreBufferIds(), ImmutableMap.of());
TaskInfo taskInfo = sqlTask.getTaskInfo(STARTING_VERSION).get();
assertEquals(taskInfo.getTaskStatus().getState(), TaskState.FLUSHING);
assertEquals(taskInfo.getTaskStatus().getVersion(), STARTING_VERSION + 1);
// completed future should be returned immediately when old caller's version is used
assertTrue(sqlTask.getTaskInfo(STARTING_VERSION).isDone());
BufferResult results = sqlTask.getTaskResults(OUT, 0, DataSize.of(1, MEGABYTE)).get();
assertFalse(results.isBufferComplete());
assertEquals(results.getSerializedPages().size(), 1);
assertEquals(getSerializedPagePositionCount(results.getSerializedPages().get(0)), 1);
for (boolean moreResults = true; moreResults; moreResults = !results.isBufferComplete()) {
results = sqlTask.getTaskResults(OUT, results.getToken() + results.getSerializedPages().size(), DataSize.of(1, MEGABYTE)).get();
}
assertEquals(results.getSerializedPages().size(), 0);
// complete the task by calling destroy on it
TaskInfo info = sqlTask.destroyTaskResults(OUT);
assertEquals(info.getOutputBuffers().getState(), BufferState.FINISHED);
taskInfo = sqlTask.getTaskInfo(info.getTaskStatus().getVersion()).get();
assertEquals(taskInfo.getTaskStatus().getState(), TaskState.FINISHED);
// completed future should be returned immediately when task is finished
assertTrue(sqlTask.getTaskInfo(STARTING_VERSION + 100).isDone());
taskInfo = sqlTask.getTaskInfo();
assertEquals(taskInfo.getTaskStatus().getState(), TaskState.FINISHED);
}
use of io.trino.execution.buffer.BufferResult in project trino by trinodb.
the class TaskResource method getResults.
@ResourceSecurity(INTERNAL_ONLY)
@GET
@Path("{taskId}/results/{bufferId}/{token}")
@Produces(TRINO_PAGES)
public void getResults(@PathParam("taskId") TaskId taskId, @PathParam("bufferId") OutputBufferId bufferId, @PathParam("token") long token, @HeaderParam(TRINO_MAX_SIZE) DataSize maxSize, @Suspended AsyncResponse asyncResponse) {
requireNonNull(taskId, "taskId is null");
requireNonNull(bufferId, "bufferId is null");
if (injectFailure(taskManager.getTraceToken(taskId), taskId, RequestType.GET_RESULTS, asyncResponse)) {
return;
}
TaskState state = taskManager.getTaskStatus(taskId).getState();
boolean taskFailed = state == TaskState.ABORTED || state == TaskState.FAILED;
long start = System.nanoTime();
ListenableFuture<BufferResult> bufferResultFuture = taskManager.getTaskResults(taskId, bufferId, token, maxSize);
Duration waitTime = randomizeWaitTime(DEFAULT_MAX_WAIT_TIME);
bufferResultFuture = addTimeout(bufferResultFuture, () -> BufferResult.emptyResults(taskManager.getTaskInstanceId(taskId), token, false), waitTime, timeoutExecutor);
ListenableFuture<Response> responseFuture = Futures.transform(bufferResultFuture, result -> {
List<Slice> serializedPages = result.getSerializedPages();
GenericEntity<?> entity = null;
Status status;
if (serializedPages.isEmpty()) {
status = Status.NO_CONTENT;
} else {
entity = new GenericEntity<>(serializedPages, new TypeToken<List<Slice>>() {
}.getType());
status = Status.OK;
}
return Response.status(status).entity(entity).header(TRINO_TASK_INSTANCE_ID, result.getTaskInstanceId()).header(TRINO_PAGE_TOKEN, result.getToken()).header(TRINO_PAGE_NEXT_TOKEN, result.getNextToken()).header(TRINO_BUFFER_COMPLETE, result.isBufferComplete()).header(TRINO_TASK_FAILED, taskFailed).build();
}, directExecutor());
// For hard timeout, add an additional time to max wait for thread scheduling contention and GC
Duration timeout = new Duration(waitTime.toMillis() + ADDITIONAL_WAIT_TIME.toMillis(), MILLISECONDS);
bindAsyncResponse(asyncResponse, responseFuture, responseExecutor).withTimeout(timeout, Response.status(Status.NO_CONTENT).header(TRINO_TASK_INSTANCE_ID, taskManager.getTaskInstanceId(taskId)).header(TRINO_PAGE_TOKEN, token).header(TRINO_PAGE_NEXT_TOKEN, token).header(TRINO_BUFFER_COMPLETE, false).header(TRINO_TASK_FAILED, taskFailed).build());
responseFuture.addListener(() -> readFromOutputBufferTime.add(Duration.nanosSince(start)), directExecutor());
asyncResponse.register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));
}
Aggregations