use of com.facebook.presto.execution.buffer.BufferResult in project presto by prestodb.
the class TestSqlTask method testBufferCloseOnFinish.
@Test
public void testBufferCloseOnFinish() throws Exception {
SqlTask sqlTask = createInitialTask();
OutputBuffers outputBuffers = createInitialEmptyOutputBuffers(PARTITIONED).withBuffer(OUT, 0).withNoMoreBufferIds();
updateTask(sqlTask, EMPTY_SOURCES, outputBuffers);
ListenableFuture<BufferResult> bufferResult = sqlTask.getTaskResults(OUT, 0, new DataSize(1, MEGABYTE));
assertFalse(bufferResult.isDone());
// close the sources (no splits will ever be added)
updateTask(sqlTask, ImmutableList.of(new TaskSource(TABLE_SCAN_NODE_ID, ImmutableSet.of(), true)), outputBuffers);
// finish the task by calling abort on it
sqlTask.abortTaskResults(OUT);
// buffer will be closed by cancel event (wait for event to fire)
bufferResult.get(1, SECONDS);
// verify the buffer is closed
bufferResult = sqlTask.getTaskResults(OUT, 0, new DataSize(1, MEGABYTE));
assertTrue(bufferResult.isDone());
assertTrue(bufferResult.get().isBufferComplete());
}
use of com.facebook.presto.execution.buffer.BufferResult in project presto by prestodb.
the class TestSqlTask method testBufferNotCloseOnFail.
@Test
public void testBufferNotCloseOnFail() throws Exception {
SqlTask sqlTask = createInitialTask();
updateTask(sqlTask, EMPTY_SOURCES, createInitialEmptyOutputBuffers(PARTITIONED).withBuffer(OUT, 0).withNoMoreBufferIds());
ListenableFuture<BufferResult> bufferResult = sqlTask.getTaskResults(OUT, 0, new DataSize(1, MEGABYTE));
assertFalse(bufferResult.isDone());
TaskState taskState = sqlTask.getTaskInfo().getTaskStatus().getState();
sqlTask.failed(new Exception("test"));
assertEquals(sqlTask.getTaskInfo(taskState).get(1, SECONDS).getTaskStatus().getState(), TaskState.FAILED);
// buffer will not be closed by fail event. event is async so wait a bit for event to fire
try {
assertTrue(bufferResult.get(1, SECONDS).isBufferComplete());
fail("expected TimeoutException");
} catch (TimeoutException expected) {
// expected
}
assertFalse(sqlTask.getTaskResults(OUT, 0, new DataSize(1, MEGABYTE)).isDone());
}
use of com.facebook.presto.execution.buffer.BufferResult in project presto by prestodb.
the class MockExchangeRequestProcessor method handle.
@Override
public Response handle(Request request) {
if (request.getMethod().equalsIgnoreCase("DELETE")) {
return new TestingResponse(HttpStatus.NO_CONTENT, ImmutableListMultimap.of(), new byte[0]);
}
// verify we got a data size and it parses correctly
assertTrue(!request.getHeaders().get(PrestoHeaders.PRESTO_MAX_SIZE).isEmpty());
DataSize maxSize = DataSize.valueOf(request.getHeader(PrestoHeaders.PRESTO_MAX_SIZE));
assertTrue(maxSize.compareTo(expectedMaxSize) <= 0);
requestMaxSizes.add(maxSize);
RequestLocation requestLocation = new RequestLocation(request.getUri());
URI location = requestLocation.getLocation();
BufferResult result = buffers.getUnchecked(location).getPages(requestLocation.getSequenceId(), maxSize);
byte[] bytes = new byte[0];
HttpStatus status;
if (!result.getSerializedPages().isEmpty()) {
DynamicSliceOutput sliceOutput = new DynamicSliceOutput(64);
PagesSerdeUtil.writeSerializedPages(sliceOutput, result.getSerializedPages());
bytes = dataChanger.apply(sliceOutput.slice().getBytes());
status = HttpStatus.OK;
} else {
status = HttpStatus.NO_CONTENT;
}
return new TestingResponse(status, ImmutableListMultimap.of(CONTENT_TYPE, PRESTO_PAGES, PRESTO_TASK_INSTANCE_ID, String.valueOf(result.getTaskInstanceId()), PRESTO_PAGE_TOKEN, String.valueOf(result.getToken()), PRESTO_PAGE_NEXT_TOKEN, String.valueOf(result.getNextToken()), PRESTO_BUFFER_COMPLETE, String.valueOf(result.isBufferComplete())), bytes);
}
use of com.facebook.presto.execution.buffer.BufferResult in project presto by prestodb.
the class AsyncPageTransportServlet method processRequest.
protected void processRequest(String requestURI, TaskId taskId, OutputBufferId bufferId, long token, HttpServletRequest request, HttpServletResponse response) throws IOException {
DataSize maxSize = DataSize.valueOf(request.getHeader(PRESTO_MAX_SIZE));
AsyncContext asyncContext = request.startAsync(request, response);
// wait time to get results
Duration waitTime = randomizeWaitTime(DEFAULT_MAX_WAIT_TIME);
asyncContext.setTimeout(waitTime.toMillis() + pageTransportTimeout.toMillis());
asyncContext.addListener(new AsyncListener() {
public void onComplete(AsyncEvent event) {
}
public void onError(AsyncEvent event) throws IOException {
String errorMessage = format("Server error to process task result request %s : %s", requestURI, event.getThrowable().getMessage());
log.error(event.getThrowable(), errorMessage);
response.sendError(SC_INTERNAL_SERVER_ERROR, errorMessage);
}
public void onStartAsync(AsyncEvent event) {
}
public void onTimeout(AsyncEvent event) throws IOException {
String errorMessage = format("Server timeout to process task result request: %s", requestURI);
log.error(event.getThrowable(), errorMessage);
response.sendError(SC_INTERNAL_SERVER_ERROR, errorMessage);
}
});
ListenableFuture<BufferResult> bufferResultFuture = taskManager.getTaskResults(taskId, bufferId, token, maxSize);
bufferResultFuture = addTimeout(bufferResultFuture, () -> BufferResult.emptyResults(taskManager.getTaskInstanceId(taskId), token, false), waitTime, timeoutExecutor);
ServletOutputStream out = response.getOutputStream();
addCallback(bufferResultFuture, new FutureCallback<BufferResult>() {
@Override
public void onSuccess(BufferResult bufferResult) {
response.setHeader(CONTENT_TYPE, PRESTO_PAGES);
response.setHeader(PRESTO_TASK_INSTANCE_ID, bufferResult.getTaskInstanceId());
response.setHeader(PRESTO_PAGE_TOKEN, String.valueOf(bufferResult.getToken()));
response.setHeader(PRESTO_PAGE_NEXT_TOKEN, String.valueOf(bufferResult.getNextToken()));
response.setHeader(PRESTO_BUFFER_COMPLETE, String.valueOf(bufferResult.isBufferComplete()));
List<SerializedPage> serializedPages = bufferResult.getSerializedPages();
if (serializedPages.isEmpty()) {
response.setStatus(SC_NO_CONTENT);
asyncContext.complete();
} else {
int contentLength = (serializedPages.size() * PAGE_METADATA_SIZE) + serializedPages.stream().mapToInt(SerializedPage::getSizeInBytes).sum();
response.setHeader(CONTENT_LENGTH, String.valueOf(contentLength));
out.setWriteListener(new SerializedPageWriteListener(serializedPages, asyncContext, out));
}
}
@Override
public void onFailure(Throwable thrown) {
String errorMessage = format("Error getting task result from TaskManager for request %s : %s", requestURI, thrown.getMessage());
log.error(thrown, errorMessage);
try {
response.sendError(SC_INTERNAL_SERVER_ERROR, errorMessage);
} catch (IOException e) {
log.error(e, "Failed to send response with error code: %s", e.getMessage());
}
asyncContext.complete();
}
}, responseExecutor);
}
Aggregations