Search in sources :

Example 6 with VertxCompletableFuture

use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.

the class ApiTest method shouldUseDelimitedFormatWhenDelimitedHeaderInserts.

@Test
public void shouldUseDelimitedFormatWhenDelimitedHeaderInserts() 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").putHeader("accept", "application/vnd.ksqlapi.delimited.v1").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)

Example 7 with VertxCompletableFuture

use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.

the class ApiIntegrationTest method shouldExecutePushQueryNoLimit.

@Test
public void shouldExecutePushQueryNoLimit() throws Exception {
    KsqlEngine engine = (KsqlEngine) REST_APP.getEngine();
    // One persistent query for the agg table
    assertThatEventually(engine::numberOfLiveQueries, is(1));
    // Given:
    String sql = "SELECT * from " + TEST_STREAM + " EMIT CHANGES;";
    // Create a write stream to capture the incomplete response
    ReceiveStream writeStream = new ReceiveStream(vertx);
    // Make the request to stream a query
    JsonObject properties = new JsonObject();
    JsonObject requestBody = new JsonObject().put("sql", sql).put("properties", properties);
    VertxCompletableFuture<HttpResponse<Void>> responseFuture = new VertxCompletableFuture<>();
    client.post("/query-stream").as(BodyCodec.pipe(writeStream)).sendJsonObject(requestBody, responseFuture);
    assertThatEventually(engine::numberOfLiveQueries, is(2));
    // 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 -1;
        }
    }, greaterThanOrEqualTo(6));
    // The response shouldn't have ended yet
    assertThat(writeStream.isEnded(), is(false));
    QueryResponse queryResponse = new QueryResponse(writeStream.getBody().toString());
    String queryId = queryResponse.responseObject.getString("queryId");
    // Now send another request to close the query
    JsonObject closeQueryRequestBody = new JsonObject().put("queryId", queryId);
    HttpResponse<Buffer> closeQueryResponse = sendRequest(client, "/close-query", closeQueryRequestBody.toBuffer());
    assertThat(closeQueryResponse.statusCode(), is(200));
    // The response should now be ended
    assertThatEventually(writeStream::isEnded, is(true));
    HttpResponse<Void> response = responseFuture.get();
    assertThat(response.statusCode(), is(200));
    // Make sure it's cleaned up on the server
    assertThatEventually(engine::numberOfLiveQueries, is(1));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) ReceiveStream(io.confluent.ksql.api.utils.ReceiveStream) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 8 with VertxCompletableFuture

use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.

the class RestIntegrationTestUtil method rawRestRequest.

static VertxCompletableFuture<Void> rawRestRequest(final TestKsqlRestApp restApp, final HttpVersion httpVersion, final HttpMethod method, final String uri, final Object requestBody, final String mediaType, final Consumer<Buffer> chunkConsumer, final Optional<BasicCredentials> credentials) {
    final byte[] bytes;
    try {
        bytes = ApiJsonMapper.INSTANCE.get().writeValueAsBytes(requestBody);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    HttpClientOptions options = new HttpClientOptions().setDefaultPort(restApp.getHttpListener().getPort()).setDefaultHost(restApp.getHttpListener().getHost()).setVerifyHost(false);
    if (httpVersion == HttpVersion.HTTP_2) {
        options.setProtocolVersion(HttpVersion.HTTP_2);
    }
    final Vertx vertx = Vertx.vertx();
    final HttpClient httpClient = vertx.createHttpClient(options);
    final VertxCompletableFuture<Void> vcf = new VertxCompletableFuture<>();
    final HttpClientRequest httpClientRequest = httpClient.request(method, uri, resp -> {
        resp.handler(buffer -> {
            try {
                chunkConsumer.accept(buffer);
            } catch (final Throwable t) {
                vcf.completeExceptionally(t);
            }
        });
        resp.endHandler(v -> {
            chunkConsumer.accept(null);
            vcf.complete(null);
        });
    }).exceptionHandler(vcf::completeExceptionally);
    httpClientRequest.putHeader("Accept", mediaType);
    credentials.ifPresent(basicCredentials -> httpClientRequest.putHeader("Authorization", createBasicAuthHeader(basicCredentials)));
    Buffer bodyBuffer = Buffer.buffer(bytes);
    httpClientRequest.end(bodyBuffer);
    // cleanup
    vcf.handle((v, throwable) -> {
        httpClient.close();
        vertx.close();
        return null;
    });
    return vcf;
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) MultiMap(io.vertx.core.MultiMap) CommandStatuses(io.confluent.ksql.rest.entity.CommandStatuses) RestResponse(io.confluent.ksql.rest.client.RestResponse) KsqlErrorMessage(io.confluent.ksql.rest.entity.KsqlErrorMessage) HttpVersion(io.vertx.core.http.HttpVersion) ACCEPT(io.netty.handler.codec.http.HttpHeaderNames.ACCEPT) Map(java.util.Map) URI(java.net.URI) KsqlRequestConfig(io.confluent.ksql.util.KsqlRequestConfig) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) ImmutableMap(com.google.common.collect.ImmutableMap) TestKsqlRestApp(io.confluent.ksql.rest.server.TestKsqlRestApp) UrlEscapers(com.google.common.net.UrlEscapers) ApiJsonMapper(io.confluent.ksql.rest.ApiJsonMapper) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Base64(java.util.Base64) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) Status(io.confluent.ksql.rest.entity.CommandStatus.Status) Optional(java.util.Optional) CONTENT_TYPE(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) AUTHORIZATION(io.netty.handler.codec.http.HttpHeaderNames.AUTHORIZATION) HttpClient(io.vertx.core.http.HttpClient) HttpResponse(io.vertx.ext.web.client.HttpResponse) StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) WebClient(io.vertx.ext.web.client.WebClient) POST(io.vertx.core.http.HttpMethod.POST) BodyCodec(io.vertx.ext.web.codec.BodyCodec) KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) CompletableFuture(java.util.concurrent.CompletableFuture) WebsocketVersion(io.vertx.core.http.WebsocketVersion) ServerClusterId(io.confluent.ksql.rest.entity.ServerClusterId) ArrayList(java.util.ArrayList) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) HttpClientRequest(io.vertx.core.http.HttpClientRequest) ServerInfo(io.confluent.ksql.rest.entity.ServerInfo) KsqlRestClient(io.confluent.ksql.rest.client.KsqlRestClient) Charset(java.nio.charset.Charset) WriteStream(io.vertx.core.streams.WriteStream) HttpClientOptions(io.vertx.core.http.HttpClientOptions) TestDataProvider(io.confluent.ksql.util.TestDataProvider) HTTP_1_1(io.vertx.core.http.HttpVersion.HTTP_1_1) Credentials(io.confluent.ksql.test.util.secure.Credentials) Vertx(io.vertx.core.Vertx) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) BasicCredentials(io.confluent.ksql.rest.client.BasicCredentials) ServerMetadata(io.confluent.ksql.rest.entity.ServerMetadata) KsqlMediaType(io.confluent.ksql.rest.entity.KsqlMediaType) HttpRequest(io.vertx.ext.web.client.HttpRequest) Consumer(java.util.function.Consumer) HttpMethod(io.vertx.core.http.HttpMethod) HealthCheckResponse(io.confluent.ksql.rest.entity.HealthCheckResponse) Collections(java.util.Collections) Buffer(io.vertx.core.buffer.Buffer) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) Vertx(io.vertx.core.Vertx) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient)

Example 9 with VertxCompletableFuture

use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.

the class RestIntegrationTestUtil method rawRestRequest.

static HttpResponse<Buffer> rawRestRequest(final Vertx vertx, final WebClient webClient, final TestKsqlRestApp restApp, final HttpVersion httpVersion, final HttpMethod method, final String uri, final Object requestBody, final String mediaType, final Optional<WriteStream<Buffer>> writeStream, final Optional<BasicCredentials> credentials) {
    try {
        byte[] bytes = ApiJsonMapper.INSTANCE.get().writeValueAsBytes(requestBody);
        Buffer bodyBuffer = Buffer.buffer(bytes);
        // When:
        VertxCompletableFuture<HttpResponse<Buffer>> requestFuture = new VertxCompletableFuture<>();
        HttpRequest<Buffer> request = webClient.request(method, uri).putHeader(CONTENT_TYPE.toString(), "application/json").putHeader(ACCEPT.toString(), mediaType);
        credentials.ifPresent(basicCredentials -> request.putHeader("Authorization", createBasicAuthHeader(basicCredentials)));
        if (bodyBuffer != null) {
            request.sendBuffer(bodyBuffer, requestFuture);
        } else {
            request.send(requestFuture);
        }
        writeStream.ifPresent(s -> request.as(BodyCodec.pipe(s)));
        return requestFuture.get();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpResponse(io.vertx.ext.web.client.HttpResponse) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture)

Example 10 with VertxCompletableFuture

use of io.confluent.ksql.util.VertxCompletableFuture in project ksql by confluentinc.

the class ApiIntegrationTest method shouldExecutePushQueryFromLatestOffset.

@Test
public void shouldExecutePushQueryFromLatestOffset() {
    KsqlEngine engine = (KsqlEngine) REST_APP.getEngine();
    // One persistent query for the agg table
    assertThatEventually(engine::numberOfLiveQueries, is(1));
    // Given:
    String sql = "SELECT * from " + TEST_STREAM + " EMIT CHANGES LIMIT 1;";
    // Create a write stream to capture the incomplete response
    ReceiveStream writeStream = new ReceiveStream(vertx);
    // Make the request to stream a query
    JsonObject queryProperties = new JsonObject().put("auto.offset.reset", "latest");
    JsonObject queryRequestBody = new JsonObject().put("sql", sql).put("properties", queryProperties);
    VertxCompletableFuture<HttpResponse<Void>> responseFuture = new VertxCompletableFuture<>();
    client.post("/query-stream").as(BodyCodec.pipe(writeStream)).sendJsonObject(queryRequestBody, responseFuture);
    assertThatEventually(engine::numberOfLiveQueries, is(2));
    // New row to insert
    JsonObject row = new JsonObject().put("K", new JsonObject().put("F1", new JsonArray().add("my_key_shouldExecutePushQueryFromLatestOffset"))).put("STR", "Value_shouldExecutePushQueryFromLatestOffset").put("LONG", 2000L).put("DEC", // JsonObject does not accept BigDecimal
    12.34).put("BYTES_", new byte[] { 0, 1, 2 }).put("ARRAY", new JsonArray().add("a_shouldExecutePushQueryFromLatestOffset")).put("MAP", new JsonObject().put("k1", "v1_shouldExecutePushQueryFromLatestOffset")).put("STRUCT", new JsonObject().put("F1", 3)).put("COMPLEX", COMPLEX_FIELD_VALUE);
    // Insert a new row and wait for it to arrive
    assertThatEventually(() -> {
        try {
            // Attempt the insert multiple times, in case the query hasn't started yet
            shouldInsert(row);
            Buffer buff = writeStream.getBody();
            QueryResponse queryResponse = new QueryResponse(buff.toString());
            return queryResponse.rows.size();
        } catch (Throwable t) {
            return Integer.MAX_VALUE;
        }
    }, is(1));
    // Verify that the received row is the expected one
    Buffer buff = writeStream.getBody();
    QueryResponse queryResponse = new QueryResponse(buff.toString());
    assertThat(queryResponse.rows.get(0).getJsonObject(0), is(new JsonObject().put("F1", new JsonArray().add("my_key_shouldExecutePushQueryFromLatestOffset"))));
    assertThat(queryResponse.rows.get(0).getString(1), is("Value_shouldExecutePushQueryFromLatestOffset"));
    assertThat(queryResponse.rows.get(0).getLong(2), is(2000L));
    assertThat(queryResponse.rows.get(0).getDouble(3), is(12.34));
    assertThat(queryResponse.rows.get(0).getBinary(4), is(new byte[] { 0, 1, 2 }));
    assertThat(queryResponse.rows.get(0).getJsonArray(5), is(new JsonArray().add("a_shouldExecutePushQueryFromLatestOffset")));
    assertThat(queryResponse.rows.get(0).getJsonObject(6), is(new JsonObject().put("k1", "v1_shouldExecutePushQueryFromLatestOffset")));
    assertThat(queryResponse.rows.get(0).getJsonObject(7), is(new JsonObject().put("F1", 3)));
    assertThat(queryResponse.rows.get(0).getJsonObject(8), is(COMPLEX_FIELD_VALUE));
    // Check that query is cleaned up on the server
    assertThatEventually(engine::numberOfLiveQueries, is(1));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) ReceiveStream(io.confluent.ksql.api.utils.ReceiveStream) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) IntegrationTest(io.confluent.common.utils.IntegrationTest) 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