Search in sources :

Example 11 with KsqlEngine

use of io.confluent.ksql.engine.KsqlEngine in project ksql by confluentinc.

the class LocalCommandsTest method shouldWriteAppIdToCommandFile.

@Test
public void shouldWriteAppIdToCommandFile() throws IOException {
    // Given
    final File dir = commandsDir.newFolder();
    LocalCommands localCommands = LocalCommands.open(ksqlEngine, dir);
    File processedFile = localCommands.getCurrentLocalCommandsFile();
    // When
    localCommands.write(metadata1);
    localCommands.write(metadata2);
    // Then
    // Need to create a new local commands in order not to skip the "current" file we just wrote.
    localCommands = LocalCommands.open(ksqlEngine, dir);
    localCommands.write(metadata3);
    localCommands.processLocalCommandFiles(serviceContext);
    verify(ksqlEngine).cleanupOrphanedInternalTopics(any(), eq(ImmutableSet.of(QUERY_APP_ID1, QUERY_APP_ID2)));
    List<Path> paths = Files.list(dir.toPath()).collect(Collectors.toList());
    String expectedProcessedFileName = processedFile.getAbsolutePath() + LOCAL_COMMANDS_PROCESSED_SUFFIX;
    assertThat(paths.size(), is(2));
    assertThat(paths.stream().anyMatch(path -> path.toFile().getAbsolutePath().equals(expectedProcessedFileName)), is(true));
}
Also used : Path(java.nio.file.Path) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) ServiceContext(io.confluent.ksql.services.ServiceContext) RunWith(org.junit.runner.RunWith) Mockito.doThrow(org.mockito.Mockito.doThrow) TransientQueryMetadata(io.confluent.ksql.util.TransientQueryMetadata) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) KsqlTestFolder(io.confluent.ksql.test.util.KsqlTestFolder) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Path(java.nio.file.Path) Before(org.junit.Before) ImmutableSet(com.google.common.collect.ImmutableSet) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) Files(java.nio.file.Files) LOCAL_COMMANDS_PROCESSED_SUFFIX(io.confluent.ksql.rest.server.LocalCommands.LOCAL_COMMANDS_PROCESSED_SUFFIX) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) KsqlServerException(io.confluent.ksql.util.KsqlServerException) List(java.util.List) Rule(org.junit.Rule) Paths(java.nio.file.Paths) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) TemporaryFolder(org.junit.rules.TemporaryFolder) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 12 with KsqlEngine

use of io.confluent.ksql.engine.KsqlEngine in project ksql by confluentinc.

the class JsonFormatTest method before.

@Before
public void before() {
    streamName = "STREAM_" + COUNTER.getAndIncrement();
    ksqlConfig = KsqlConfigTestUtil.create(TEST_HARNESS.kafkaBootstrapServers());
    serviceContext = ServiceContextFactory.create(ksqlConfig, DisabledKsqlClient::instance);
    functionRegistry = new InternalFunctionRegistry();
    UserFunctionLoader.newInstance(ksqlConfig, functionRegistry, ".", new Metrics()).load();
    ksqlEngine = new KsqlEngine(serviceContext, ProcessingLogContext.create(), functionRegistry, ServiceInfo.create(ksqlConfig), new SequentialQueryIdGenerator(), ksqlConfig, Collections.emptyList(), new MetricCollectors());
    topicClient = serviceContext.getTopicClient();
    metaStore = ksqlEngine.getMetaStore();
    createInitTopics();
    produceInitData();
    execInitCreateStreamQueries();
}
Also used : KsqlEngine(io.confluent.ksql.engine.KsqlEngine) Metrics(org.apache.kafka.common.metrics.Metrics) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) SequentialQueryIdGenerator(io.confluent.ksql.query.id.SequentialQueryIdGenerator) InternalFunctionRegistry(io.confluent.ksql.function.InternalFunctionRegistry) Before(org.junit.Before)

Example 13 with KsqlEngine

use of io.confluent.ksql.engine.KsqlEngine 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 14 with KsqlEngine

use of io.confluent.ksql.engine.KsqlEngine in project ksql by confluentinc.

the class RecoveryTest method shouldRecover.

private void shouldRecover(final List<QueuedCommand> commands) {
    // Given:
    final KsqlServer executeServer = new KsqlServer(commands);
    executeServer.executeCommands();
    final KsqlEngine engine = executeServer.ksqlEngine;
    // When:
    final KsqlServer recoverServer = new KsqlServer(commands);
    recoverServer.recover();
    final KsqlEngine recovered = recoverServer.ksqlEngine;
    // Then:
    assertThat(recovered.getMetaStore(), sameStore(engine.getMetaStore()));
    final Map<QueryId, PersistentQueryMetadata> queries = queriesById(engine.getPersistentQueries());
    final Map<QueryId, PersistentQueryMetadata> recoveredQueries = queriesById(recovered.getPersistentQueries());
    assertThat(queries.keySet(), equalTo(recoveredQueries.keySet()));
    queries.forEach((queryId, query) -> assertThat(query, sameQuery(recoveredQueries.get(queryId))));
}
Also used : KsqlEngine(io.confluent.ksql.engine.KsqlEngine) QueryId(io.confluent.ksql.query.QueryId) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 15 with KsqlEngine

use of io.confluent.ksql.engine.KsqlEngine in project ksql by confluentinc.

the class ListQueriesExecutorTest method shouldIncludeUnresponsiveIfShowQueriesErrorResponse.

@Test
public void shouldIncludeUnresponsiveIfShowQueriesErrorResponse() {
    // Given
    when(sessionProperties.getInternalRequest()).thenReturn(false);
    final ConfiguredStatement<ListQueries> showQueries = (ConfiguredStatement<ListQueries>) engine.configure("SHOW QUERIES;");
    final PersistentQueryMetadata metadata = givenPersistentQuery("id", RUNNING_QUERY_STATE);
    final KsqlEngine engine = mock(KsqlEngine.class);
    when(engine.getAllLiveQueries()).thenReturn(ImmutableList.of(metadata));
    when(engine.getPersistentQueries()).thenReturn(ImmutableList.of(metadata));
    when(response.isErroneous()).thenReturn(true);
    when(response.getErrorMessage()).thenReturn(new KsqlErrorMessage(10000, "error"));
    queryStatusCount.updateStatusCount(RUNNING_QUERY_STATE, 1);
    queryStatusCount.updateStatusCount(KsqlQueryStatus.UNRESPONSIVE, 1);
    // When
    final Queries queries = (Queries) CUSTOM_EXECUTORS.listQueries().execute(showQueries, sessionProperties, engine, serviceContext).getEntity().orElseThrow(IllegalStateException::new);
    // Then
    assertThat(queries.getQueries(), containsInAnyOrder(persistentQueryMetadataToRunningQuery(metadata, queryStatusCount)));
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) Queries(io.confluent.ksql.rest.entity.Queries) ListQueries(io.confluent.ksql.parser.tree.ListQueries) ListQueries(io.confluent.ksql.parser.tree.ListQueries) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) KsqlErrorMessage(io.confluent.ksql.rest.entity.KsqlErrorMessage) Test(org.junit.Test)

Aggregations

KsqlEngine (io.confluent.ksql.engine.KsqlEngine)38 Test (org.junit.Test)25 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)24 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)22 InternalFunctionRegistry (io.confluent.ksql.function.InternalFunctionRegistry)14 SequentialQueryIdGenerator (io.confluent.ksql.query.id.SequentialQueryIdGenerator)14 KsqlConfig (io.confluent.ksql.util.KsqlConfig)14 MetricCollectors (io.confluent.ksql.metrics.MetricCollectors)13 ServiceContext (io.confluent.ksql.services.ServiceContext)13 MutableFunctionRegistry (io.confluent.ksql.function.MutableFunctionRegistry)11 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)11 KsqlHostInfoEntity (io.confluent.ksql.rest.entity.KsqlHostInfoEntity)11 Queries (io.confluent.ksql.rest.entity.Queries)11 List (java.util.List)11 MetaStoreImpl (io.confluent.ksql.metastore.MetaStoreImpl)10 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)10 Matchers.is (org.hamcrest.Matchers.is)10 ImmutableList (com.google.common.collect.ImmutableList)9 SessionConfig (io.confluent.ksql.config.SessionConfig)9 KsqlEngineMetrics (io.confluent.ksql.internal.KsqlEngineMetrics)9