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));
}
}
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));
}
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;
}
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);
}
}
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));
}
Aggregations