use of io.airlift.http.client.Request in project presto by prestodb.
the class TestHttpPageBufferClient method testExceptionFromResponseHandler.
@Test
public void testExceptionFromResponseHandler() throws Exception {
TestingTicker ticker = new TestingTicker();
AtomicReference<Duration> tickerIncrement = new AtomicReference<>(new Duration(0, TimeUnit.SECONDS));
TestingHttpClient.Processor processor = (input) -> {
Duration delta = tickerIncrement.get();
ticker.increment(delta.toMillis(), TimeUnit.MILLISECONDS);
throw new RuntimeException("Foo");
};
CyclicBarrier requestComplete = new CyclicBarrier(2);
TestingClientCallback callback = new TestingClientCallback(requestComplete);
URI location = URI.create("http://localhost:8080");
HttpPageBufferClient client = new HttpPageBufferClient(new TestingHttpClient(processor, executor), new DataSize(10, Unit.MEGABYTE), new Duration(1, TimeUnit.MINUTES), new Duration(1, TimeUnit.MINUTES), location, callback, executor, ticker);
assertStatus(client, location, "queued", 0, 0, 0, 0, "not scheduled");
// request processor will throw exception, verify the request is marked a completed
// this starts the error stopwatch
client.scheduleRequest();
requestComplete.await(10, TimeUnit.SECONDS);
assertEquals(callback.getPages().size(), 0);
assertEquals(callback.getCompletedRequests(), 1);
assertEquals(callback.getFinishedBuffers(), 0);
assertEquals(callback.getFailedBuffers(), 0);
assertStatus(client, location, "queued", 0, 1, 1, 1, "not scheduled");
// advance time forward, but not enough to fail the client
tickerIncrement.set(new Duration(30, TimeUnit.SECONDS));
// verify that the client has not failed
client.scheduleRequest();
requestComplete.await(10, TimeUnit.SECONDS);
assertEquals(callback.getPages().size(), 0);
assertEquals(callback.getCompletedRequests(), 2);
assertEquals(callback.getFinishedBuffers(), 0);
assertEquals(callback.getFailedBuffers(), 0);
assertStatus(client, location, "queued", 0, 2, 2, 2, "not scheduled");
// advance time forward beyond the minimum error duration
tickerIncrement.set(new Duration(31, TimeUnit.SECONDS));
// verify that the client has failed
client.scheduleRequest();
requestComplete.await(10, TimeUnit.SECONDS);
assertEquals(callback.getPages().size(), 0);
assertEquals(callback.getCompletedRequests(), 3);
assertEquals(callback.getFinishedBuffers(), 0);
assertEquals(callback.getFailedBuffers(), 1);
assertInstanceOf(callback.getFailure(), PageTransportTimeoutException.class);
assertContains(callback.getFailure().getMessage(), WORKER_NODE_ERROR + " (http://localhost:8080/0 - 3 failures, time since last success 61.00s)");
assertStatus(client, location, "queued", 0, 3, 3, 3, "not scheduled");
}
use of io.airlift.http.client.Request in project presto by prestodb.
the class TestServer method testTransactionSupport.
@Test
public void testTransactionSupport() throws Exception {
Request request = preparePost().setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8)).setHeader(PRESTO_USER, "user").setHeader(PRESTO_SOURCE, "source").setHeader(PRESTO_TRANSACTION_ID, "none").build();
JsonResponse<QueryResults> queryResults = client.execute(request, createFullJsonResponseHandler(jsonCodec(QueryResults.class)));
ImmutableList.Builder<List<Object>> data = ImmutableList.builder();
while (true) {
if (queryResults.getValue().getData() != null) {
data.addAll(queryResults.getValue().getData());
}
if (queryResults.getValue().getNextUri() == null) {
break;
}
queryResults = client.execute(prepareGet().setUri(queryResults.getValue().getNextUri()).build(), createFullJsonResponseHandler(jsonCodec(QueryResults.class)));
}
assertNull(queryResults.getValue().getError());
assertNotNull(queryResults.getHeader(PRESTO_STARTED_TRANSACTION_ID));
}
use of io.airlift.http.client.Request in project presto by prestodb.
the class TestServer method testNoTransactionSupport.
@Test
public void testNoTransactionSupport() throws Exception {
Request request = preparePost().setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8)).setHeader(PRESTO_USER, "user").setHeader(PRESTO_SOURCE, "source").build();
QueryResults queryResults = client.execute(request, createJsonResponseHandler(jsonCodec(QueryResults.class)));
while (queryResults.getNextUri() != null) {
queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(jsonCodec(QueryResults.class)));
}
assertNotNull(queryResults.getError());
assertEquals(queryResults.getError().getErrorCode(), INCOMPATIBLE_CLIENT.toErrorCode().getCode());
}
use of io.airlift.http.client.Request in project presto by prestodb.
the class TestServer method testQuery.
@Test
public void testQuery() throws Exception {
// start query
Request request = preparePost().setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)).setHeader(PRESTO_USER, "user").setHeader(PRESTO_SOURCE, "source").setHeader(PRESTO_CATALOG, "catalog").setHeader(PRESTO_SCHEMA, "schema").setHeader(PRESTO_CLIENT_INFO, "{\"clientVersion\":\"testVersion\"}").addHeader(PRESTO_SESSION, QUERY_MAX_MEMORY + "=1GB").addHeader(PRESTO_SESSION, DISTRIBUTED_JOIN + "=true," + HASH_PARTITION_COUNT + " = 43").addHeader(PRESTO_PREPARED_STATEMENT, "foo=select * from bar").build();
QueryResults queryResults = client.execute(request, createJsonResponseHandler(jsonCodec(QueryResults.class)));
// get the query info
QueryInfo queryInfo = server.getQueryManager().getQueryInfo(new QueryId(queryResults.getId()));
// verify session properties
assertEquals(queryInfo.getSession().getSystemProperties(), ImmutableMap.builder().put(QUERY_MAX_MEMORY, "1GB").put(DISTRIBUTED_JOIN, "true").put(HASH_PARTITION_COUNT, "43").build());
// verify client info in session
assertEquals(queryInfo.getSession().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
// verify prepared statements
assertEquals(queryInfo.getSession().getPreparedStatements(), ImmutableMap.builder().put("foo", "select * from bar").build());
ImmutableList.Builder<List<Object>> data = ImmutableList.builder();
if (queryResults.getData() != null) {
data.addAll(queryResults.getData());
}
while (queryResults.getNextUri() != null) {
queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(jsonCodec(QueryResults.class)));
if (queryResults.getData() != null) {
data.addAll(queryResults.getData());
}
}
assertNull(queryResults.getError());
// only the system catalog exists by default
List<List<Object>> rows = data.build();
assertEquals(rows, ImmutableList.of(ImmutableList.of("system")));
}
use of io.airlift.http.client.Request in project presto by prestodb.
the class StatementClient method advance.
public boolean advance() {
URI nextUri = current().getNextUri();
if (isClosed() || (nextUri == null)) {
valid.set(false);
return false;
}
Request request = prepareRequest(prepareGet(), nextUri).build();
Exception cause = null;
long start = System.nanoTime();
long attempts = 0;
do {
// back-off on retry
if (attempts > 0) {
try {
MILLISECONDS.sleep(attempts * 100);
} catch (InterruptedException e) {
try {
close();
} finally {
Thread.currentThread().interrupt();
}
throw new RuntimeException("StatementClient thread was interrupted");
}
}
attempts++;
JsonResponse<QueryResults> response;
try {
response = httpClient.execute(request, responseHandler);
} catch (RuntimeException e) {
cause = e;
continue;
}
if (response.getStatusCode() == HttpStatus.OK.code() && response.hasValue()) {
processResponse(response);
return true;
}
if (response.getStatusCode() != HttpStatus.SERVICE_UNAVAILABLE.code()) {
throw requestFailedException("fetching next", request, response);
}
} while (((System.nanoTime() - start) < requestTimeoutNanos) && !isClosed());
gone.set(true);
throw new RuntimeException("Error fetching next", cause);
}
Aggregations