Search in sources :

Example 16 with VertxCompletableFuture

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;
    });
}
Also used : WorkerExecutor(io.vertx.core.WorkerExecutor) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException)

Example 17 with VertxCompletableFuture

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));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) Test(org.junit.Test)

Example 18 with VertxCompletableFuture

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"));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) Test(org.junit.Test)

Example 19 with VertxCompletableFuture

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));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ReceiveStream(io.confluent.ksql.api.utils.ReceiveStream) HttpResponse(io.vertx.ext.web.client.HttpResponse) JsonObject(io.vertx.core.json.JsonObject) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) PushQueryId(io.confluent.ksql.rest.entity.PushQueryId) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) Test(org.junit.Test)

Example 20 with VertxCompletableFuture

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));
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) InsertsResponse(io.confluent.ksql.api.utils.InsertsResponse) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) Test(org.junit.Test)

Aggregations

VertxCompletableFuture (io.confluent.ksql.util.VertxCompletableFuture)20 HttpResponse (io.vertx.ext.web.client.HttpResponse)16 Buffer (io.vertx.core.buffer.Buffer)14 JsonObject (io.vertx.core.json.JsonObject)12 Test (org.junit.Test)12 QueryResponse (io.confluent.ksql.api.utils.QueryResponse)6 ReceiveStream (io.confluent.ksql.api.utils.ReceiveStream)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 InsertsResponse (io.confluent.ksql.api.utils.InsertsResponse)3 ArrayList (java.util.ArrayList)3 IntegrationTest (io.confluent.common.utils.IntegrationTest)2 KsqlEngine (io.confluent.ksql.engine.KsqlEngine)2 CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)2 CommandStatuses (io.confluent.ksql.rest.entity.CommandStatuses)2 HealthCheckResponse (io.confluent.ksql.rest.entity.HealthCheckResponse)2 KsqlEntityList (io.confluent.ksql.rest.entity.KsqlEntityList)2 KsqlRequest (io.confluent.ksql.rest.entity.KsqlRequest)2 ServerClusterId (io.confluent.ksql.rest.entity.ServerClusterId)2 ServerInfo (io.confluent.ksql.rest.entity.ServerInfo)2 ServerMetadata (io.confluent.ksql.rest.entity.ServerMetadata)2