use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.
the class OldApiUtils method streamEndpointResponse.
private static void streamEndpointResponse(final Server server, final RoutingContext routingContext, final StreamingOutput streamingOutput, final Optional<MetricsCallbackHolder> metricsCallbackHolder, final long startTimeNanos) {
final WorkerExecutor workerExecutor = server.getWorkerExecutor();
final VertxCompletableFuture<Void> vcf = new VertxCompletableFuture<>();
workerExecutor.executeBlocking(promise -> {
final OutputStream ros = new ResponseOutputStream(routingContext.response(), streamingOutput.getWriteTimeoutMs());
routingContext.request().connection().closeHandler(v -> {
// Close the OutputStream on close of the HTTP connection
try {
streamingOutput.close();
} catch (final Throwable t) {
promise.fail(t);
}
try {
ros.close();
} catch (IOException e) {
promise.fail(e);
}
});
try {
streamingOutput.write(new BufferedOutputStream(ros));
promise.complete();
} catch (Exception e) {
promise.fail(e);
} finally {
try {
ros.close();
} catch (IOException ignore) {
// Ignore - it might already be closed
}
}
}, false, /*if this is true, worker execution blocks the main event loop*/
vcf);
vcf.handle((v, throwable) -> {
reportMetrics(routingContext, metricsCallbackHolder, startTimeNanos);
return null;
});
}
use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.
the class ApiTest method shouldUseDelimitedFormatWhenDelimitedAcceptHeaderQuery.
@Test
public void shouldUseDelimitedFormatWhenDelimitedAcceptHeaderQuery() throws Exception {
// When
JsonObject requestBody = new JsonObject().put("sql", DEFAULT_PULL_QUERY);
VertxCompletableFuture<HttpResponse<Buffer>> requestFuture = new VertxCompletableFuture<>();
client.post("/query-stream").putHeader("accept", "application/vnd.ksqlapi.delimited.v1").sendBuffer(requestBody.toBuffer(), requestFuture);
// Then
HttpResponse<Buffer> response = requestFuture.get();
QueryResponse queryResponse = new QueryResponse(response.bodyAsString());
assertThat(queryResponse.rows, hasSize(DEFAULT_JSON_ROWS.size()));
assertThat(response.bodyAsString().contains("\n"), is(true));
assertThat(response.statusCode(), is(200));
}
use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.
the class ApiTest method shouldIncludeContentTypeHeaderInResponse.
@Test
public void shouldIncludeContentTypeHeaderInResponse() throws Exception {
// When
JsonObject requestBody = new JsonObject().put("ksql", "show streams;");
VertxCompletableFuture<HttpResponse<Buffer>> requestFuture = new VertxCompletableFuture<>();
client.post("/ksql").sendBuffer(requestBody.toBuffer(), requestFuture);
// Then
HttpResponse<Buffer> response = requestFuture.get();
assertThat(response.getHeader("content-type"), is("application/json"));
}
use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.
the class ApiTest method shouldCloseQuery.
@Test
@CoreApiTest
public void shouldCloseQuery() throws Exception {
// Create a write stream to capture the incomplete response
ReceiveStream writeStream = new ReceiveStream(vertx);
VertxCompletableFuture<HttpResponse<Void>> responseFuture = new VertxCompletableFuture<>();
// Make the request to stream a query
sendPostRequest("/query-stream", (request) -> request.as(BodyCodec.pipe(writeStream)).sendJsonObject(DEFAULT_PUSH_QUERY_REQUEST_BODY, responseFuture));
// Wait for all rows in the response to arrive
assertThatEventually(() -> {
try {
Buffer buff = writeStream.getBody();
QueryResponse queryResponse = new QueryResponse(buff.toString());
return queryResponse.rows.size();
} catch (Throwable t) {
return Integer.MAX_VALUE;
}
}, is(DEFAULT_JSON_ROWS.size()));
// The response shouldn't have ended yet
assertThat(writeStream.isEnded(), is(false));
// Assert the query is still live on the server
QueryResponse queryResponse = new QueryResponse(writeStream.getBody().toString());
String queryId = queryResponse.responseObject.getString("queryId");
assertThat(server.getQueryIDs().contains(new PushQueryId(queryId)), is(true));
assertThat(server.getQueryIDs(), hasSize(1));
assertThat(testEndpoints.getQueryPublishers(), hasSize(1));
// Now send another request to close the query
JsonObject closeQueryRequestBody = new JsonObject().put("queryId", queryId);
HttpResponse<Buffer> closeQueryResponse = sendPostRequest("/close-query", closeQueryRequestBody.toBuffer());
assertThat(closeQueryResponse.statusCode(), is(200));
// Assert the query no longer exists on the server
assertThat(server.getQueryIDs(), not(hasItem(new PushQueryId(queryId))));
assertThat(server.getQueryIDs(), hasSize(0));
// The response should now be ended
assertThatEventually(writeStream::isEnded, is(true));
HttpResponse<Void> response = responseFuture.get();
assertThat(response.statusCode(), is(200));
}
use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.
the class ApiTest method shouldUseDelimitedFormatWhenNoAcceptHeaderInserts.
@Test
public void shouldUseDelimitedFormatWhenNoAcceptHeaderInserts() throws Exception {
// When
JsonObject params = new JsonObject().put("target", "test-stream");
List<JsonObject> rows = DEFAULT_INSERT_ROWS;
Buffer requestBody = Buffer.buffer();
requestBody.appendBuffer(params.toBuffer()).appendString("\n");
for (JsonObject row : rows) {
requestBody.appendBuffer(row.toBuffer()).appendString("\n");
}
VertxCompletableFuture<HttpResponse<Buffer>> requestFuture = new VertxCompletableFuture<>();
client.post("/inserts-stream").sendBuffer(requestBody, requestFuture);
// Then
HttpResponse<Buffer> response = requestFuture.get();
String responseBody = response.bodyAsString();
InsertsResponse insertsResponse = new InsertsResponse(responseBody);
assertThat(insertsResponse.acks, hasSize(rows.size()));
for (int i = 0; i < insertsResponse.acks.size(); i++) {
final JsonObject ackLine = new JsonObject().put("status", "ok").put("seq", i);
assertThat(insertsResponse.acks.get(i), is(ackLine));
}
}
Aggregations