Search in sources :

Example 16 with RunningQuery

use of io.confluent.ksql.rest.entity.RunningQuery in project ksql by confluentinc.

the class ListQueriesExecutorTest method shouldNotMergeDifferentRunningQueries.

@Test
public void shouldNotMergeDifferentRunningQueries() {
    // Given
    when(sessionProperties.getInternalRequest()).thenReturn(false);
    final ConfiguredStatement<ListQueries> showQueries = (ConfiguredStatement<ListQueries>) engine.configure("SHOW QUERIES;");
    final PersistentQueryMetadata localMetadata = givenPersistentQuery("id", RUNNING_QUERY_STATE);
    final PersistentQueryMetadata remoteMetadata = givenPersistentQuery("different Id", RUNNING_QUERY_STATE);
    final KsqlEngine engine = mock(KsqlEngine.class);
    when(engine.getAllLiveQueries()).thenReturn(ImmutableList.of(localMetadata));
    when(engine.getPersistentQueries()).thenReturn(ImmutableList.of(localMetadata));
    final List<RunningQuery> remoteRunningQueries = Collections.singletonList(persistentQueryMetadataToRunningQuery(remoteMetadata, QueryStatusCount.fromStreamsStateCounts(Collections.singletonMap(RUNNING_QUERY_STATE, 1))));
    when(remoteQueries.getQueries()).thenReturn(remoteRunningQueries);
    when(ksqlEntityList.get(anyInt())).thenReturn(remoteQueries);
    when(response.getResponse()).thenReturn(ksqlEntityList);
    queryStatusCount.updateStatusCount(RUNNING_QUERY_STATE, 1);
    // When
    final Queries queries = (Queries) CUSTOM_EXECUTORS.listQueries().execute(showQueries, sessionProperties, engine, serviceContext).getEntity().orElseThrow(IllegalStateException::new);
    // Then
    assertThat(queries.getQueries(), containsInAnyOrder(persistentQueryMetadataToRunningQuery(localMetadata, queryStatusCount), persistentQueryMetadataToRunningQuery(remoteMetadata, queryStatusCount)));
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) 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) Test(org.junit.Test)

Example 17 with RunningQuery

use of io.confluent.ksql.rest.entity.RunningQuery in project ksql by confluentinc.

the class TestKsqlRestApp method closePersistentQueries.

public void closePersistentQueries(final Optional<BasicCredentials> credentials) {
    try (final KsqlRestClient client = buildKsqlClient(credentials)) {
        // Filter source tables queries because they cannot be terminated manually
        final Set<String> queriesToTerminate = getPersistentQueries(client).stream().filter(query -> !query.getQueryString().startsWith("CREATE SOURCE TABLE")).map(RunningQuery::getId).map(QueryId::toString).collect(Collectors.toSet());
        terminateQueries(queriesToTerminate, client);
    }
}
Also used : Arrays(java.util.Arrays) StreamsList(io.confluent.ksql.rest.entity.StreamsList) URL(java.net.URL) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) InternalKsqlClientFactory(io.confluent.ksql.rest.server.services.InternalKsqlClientFactory) BiFunction(java.util.function.BiFunction) ServiceContext(io.confluent.ksql.services.ServiceContext) KsqlQueryType(io.confluent.ksql.util.KsqlConstants.KsqlQueryType) RestResponse(io.confluent.ksql.rest.client.RestResponse) KsqlErrorMessage(io.confluent.ksql.rest.entity.KsqlErrorMessage) NetworkState(io.confluent.ksql.rest.server.NetworkDisruptorClient.NetworkState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) QueryId(io.confluent.ksql.query.QueryId) URI(java.net.URI) ImmutableMap(com.google.common.collect.ImmutableMap) PropertiesUtil(io.confluent.ksql.properties.PropertiesUtil) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Collectors(java.util.stream.Collectors) SimpleKsqlClient(io.confluent.ksql.services.SimpleKsqlClient) TestRestServiceContextFactory(io.confluent.ksql.rest.server.services.TestRestServiceContextFactory) DisabledKsqlClient(io.confluent.ksql.services.DisabledKsqlClient) CountDownLatch(java.util.concurrent.CountDownLatch) ConsistencyLevel(io.confluent.ksql.util.ClientConfig.ConsistencyLevel) List(java.util.List) ExternalResource(org.junit.rules.ExternalResource) VersionCheckerAgent(io.confluent.ksql.version.metrics.VersionCheckerAgent) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) Iterables(com.google.common.collect.Iterables) InternalSimpleKsqlClientFactory(io.confluent.ksql.rest.server.services.TestRestServiceContextFactory.InternalSimpleKsqlClientFactory) EmbeddedSingleNodeKafkaCluster(io.confluent.ksql.test.util.EmbeddedSingleNodeKafkaCluster) KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) HashMap(java.util.HashMap) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) ReservedInternalTopics(io.confluent.ksql.util.ReservedInternalTopics) Supplier(java.util.function.Supplier) Stack(java.util.Stack) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) KsqlRestClient(io.confluent.ksql.rest.client.KsqlRestClient) TestDefaultKsqlClientFactory(io.confluent.ksql.rest.server.services.TestDefaultKsqlClientFactory) Objects.requireNonNull(java.util.Objects.requireNonNull) SourceDescriptionEntity(io.confluent.ksql.rest.entity.SourceDescriptionEntity) Queries(io.confluent.ksql.rest.entity.Queries) TablesList(io.confluent.ksql.rest.entity.TablesList) LinkedList(java.util.LinkedList) LinkedHashSet(java.util.LinkedHashSet) SocketAddress(io.vertx.core.net.SocketAddress) Iterator(java.util.Iterator) Vertx(io.vertx.core.Vertx) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) BasicCredentials(io.confluent.ksql.rest.client.BasicCredentials) SourceInfo(io.confluent.ksql.rest.entity.SourceInfo) ServiceContextFactory(io.confluent.ksql.services.ServiceContextFactory) Collections(java.util.Collections) KsqlRestClient(io.confluent.ksql.rest.client.KsqlRestClient) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery)

Example 18 with RunningQuery

use of io.confluent.ksql.rest.entity.RunningQuery in project ksql by confluentinc.

the class ListQueriesExecutorTest method shouldScatterGatherAndMergeShowQueries.

@Test
public void shouldScatterGatherAndMergeShowQueries() {
    // Given
    when(sessionProperties.getInternalRequest()).thenReturn(false);
    final ConfiguredStatement<ListQueries> showQueries = (ConfiguredStatement<ListQueries>) engine.configure("SHOW QUERIES;");
    final PersistentQueryMetadata localMetadata = givenPersistentQuery("id", RUNNING_QUERY_STATE);
    final PersistentQueryMetadata remoteMetadata = givenPersistentQuery("id", ERROR_QUERY_STATE);
    final KsqlEngine engine = mock(KsqlEngine.class);
    when(engine.getAllLiveQueries()).thenReturn(ImmutableList.of(localMetadata));
    when(engine.getPersistentQueries()).thenReturn(ImmutableList.of(localMetadata));
    final List<RunningQuery> remoteRunningQueries = Collections.singletonList(persistentQueryMetadataToRunningQuery(remoteMetadata, QueryStatusCount.fromStreamsStateCounts(Collections.singletonMap(ERROR_QUERY_STATE, 1))));
    when(remoteQueries.getQueries()).thenReturn(remoteRunningQueries);
    when(ksqlEntityList.get(anyInt())).thenReturn(remoteQueries);
    when(response.getResponse()).thenReturn(ksqlEntityList);
    queryStatusCount.updateStatusCount(RUNNING_QUERY_STATE, 1);
    queryStatusCount.updateStatusCount(ERROR_QUERY_STATE, 1);
    // When
    final Queries queries = (Queries) CUSTOM_EXECUTORS.listQueries().execute(showQueries, sessionProperties, engine, serviceContext).getEntity().orElseThrow(IllegalStateException::new);
    // Then
    assertThat(queries.getQueries(), containsInAnyOrder(persistentQueryMetadataToRunningQuery(localMetadata, queryStatusCount)));
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) 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) Test(org.junit.Test)

Example 19 with RunningQuery

use of io.confluent.ksql.rest.entity.RunningQuery in project ksql by confluentinc.

the class KsqlResourceTest method shouldDescribeStatement.

@Test
public void shouldDescribeStatement() {
    // Given:
    final List<RunningQuery> queries = createRunningQueries("CREATE STREAM described_stream AS SELECT * FROM test_stream;" + "CREATE STREAM down_stream AS SELECT * FROM described_stream;", emptyMap());
    // When:
    final SourceDescriptionEntity description = makeSingleRequest("DESCRIBE DESCRIBED_STREAM;", SourceDescriptionEntity.class);
    // Then:
    final SourceDescription expectedDescription = SourceDescriptionFactory.create(ksqlEngine.getMetaStore().getSource(SourceName.of("DESCRIBED_STREAM")), false, Collections.singletonList(queries.get(1)), Collections.singletonList(queries.get(0)), Optional.empty(), Collections.emptyList(), Collections.emptyList(), new MetricCollectors());
    assertThat(description.getSourceDescription(), is(expectedDescription));
}
Also used : RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) SourceDescriptionEntity(io.confluent.ksql.rest.entity.SourceDescriptionEntity) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription) Test(org.junit.Test)

Example 20 with RunningQuery

use of io.confluent.ksql.rest.entity.RunningQuery in project ksql by confluentinc.

the class ListSourceExecutorTest method shouldShowColumnsSource.

@Test
public void shouldShowColumnsSource() {
    // Given:
    engine.givenSource(DataSourceType.KSTREAM, "SOURCE");
    final ExecuteResult result = engine.getEngine().execute(engine.getServiceContext(), engine.configure("CREATE STREAM SINK AS SELECT * FROM source;"));
    final PersistentQueryMetadata metadata = (PersistentQueryMetadata) result.getQuery().orElseThrow(IllegalArgumentException::new);
    final DataSource stream = engine.getEngine().getMetaStore().getSource(SourceName.of("SINK"));
    // When:
    final SourceDescriptionEntity sourceDescription = (SourceDescriptionEntity) CUSTOM_EXECUTORS.showColumns().execute(ConfiguredStatement.of(PreparedStatement.of("DESCRIBE SINK;", new ShowColumns(SourceName.of("SINK"), false)), SessionConfig.of(engine.getKsqlConfig(), ImmutableMap.of())), SESSION_PROPERTIES, engine.getEngine(), engine.getServiceContext()).getEntity().orElseThrow(IllegalStateException::new);
    // Then:
    final QueryStatusCount queryStatusCount = QueryStatusCount.fromStreamsStateCounts(Collections.singletonMap(metadata.getState(), 1));
    assertThat(sourceDescription.getSourceDescription(), equalTo(SourceDescriptionFactory.create(stream, false, ImmutableList.of(), ImmutableList.of(new RunningQuery(metadata.getStatementString(), ImmutableSet.of(metadata.getSinkName().get().toString(FormatOptions.noEscape())), ImmutableSet.of(metadata.getResultTopic().get().getKafkaTopicName()), metadata.getQueryId(), queryStatusCount, KsqlConstants.KsqlQueryType.PERSISTENT)), Optional.empty(), ImmutableList.of(), ImmutableList.of(), new MetricCollectors())));
}
Also used : RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) SourceDescriptionEntity(io.confluent.ksql.rest.entity.SourceDescriptionEntity) ShowColumns(io.confluent.ksql.parser.tree.ShowColumns) QueryStatusCount(io.confluent.ksql.rest.entity.QueryStatusCount) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) DataSource(io.confluent.ksql.metastore.model.DataSource) Test(org.junit.Test)

Aggregations

RunningQuery (io.confluent.ksql.rest.entity.RunningQuery)25 QueryId (io.confluent.ksql.query.QueryId)16 Test (org.junit.Test)16 Queries (io.confluent.ksql.rest.entity.Queries)11 SourceDescriptionEntity (io.confluent.ksql.rest.entity.SourceDescriptionEntity)11 KsqlEntityList (io.confluent.ksql.rest.entity.KsqlEntityList)9 FieldInfo (io.confluent.ksql.rest.entity.FieldInfo)7 ArrayList (java.util.ArrayList)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 QueryOffsetSummary (io.confluent.ksql.rest.entity.QueryOffsetSummary)4 QueryStatusCount (io.confluent.ksql.rest.entity.QueryStatusCount)4 Map (java.util.Map)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 BaseApiTest (io.confluent.ksql.api.BaseApiTest)3 MetricCollectors (io.confluent.ksql.metrics.MetricCollectors)3 CommandStatusEntity (io.confluent.ksql.rest.entity.CommandStatusEntity)3 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)3 PushQueryId (io.confluent.ksql.rest.entity.PushQueryId)3 QueryTopicOffsetSummary (io.confluent.ksql.rest.entity.QueryTopicOffsetSummary)3 SourceDescription (io.confluent.ksql.rest.entity.SourceDescription)3