Search in sources :

Example 1 with RunningQuery

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

the class ListSourceExecutor method describeSource.

private static SourceDescriptionWithWarnings describeSource(final KsqlConfig ksqlConfig, final KsqlExecutionContext ksqlExecutionContext, final ServiceContext serviceContext, final SourceName name, final boolean extended, final ConfiguredStatement<? extends StatementWithExtendedClause> statement, final SessionProperties sessionProperties, final Collection<SourceDescription> remoteSourceDescriptions) {
    final DataSource dataSource = ksqlExecutionContext.getMetaStore().getSource(name);
    if (dataSource == null) {
        throw new KsqlStatementException(String.format("Could not find STREAM/TABLE '%s' in the Metastore", name.text()), statement.getStatementText());
    }
    final List<RunningQuery> readQueries = getQueries(ksqlExecutionContext, q -> q.getSourceNames().contains(dataSource.getName()));
    final List<RunningQuery> writeQueries = getQueries(ksqlExecutionContext, q -> q.getSinkName().equals(Optional.of(dataSource.getName())));
    Optional<TopicDescription> topicDescription = Optional.empty();
    List<QueryOffsetSummary> queryOffsetSummaries = Collections.emptyList();
    List<String> sourceConstraints = Collections.emptyList();
    final List<KsqlWarning> warnings = new LinkedList<>();
    try {
        topicDescription = Optional.of(serviceContext.getTopicClient().describeTopic(dataSource.getKafkaTopicName()));
        sourceConstraints = getSourceConstraints(name, ksqlExecutionContext.getMetaStore());
    } catch (final KafkaException | KafkaResponseGetFailedException e) {
        warnings.add(new KsqlWarning("Error from Kafka: " + e.getMessage()));
    }
    if (extended) {
        queryOffsetSummaries = queryOffsetSummaries(ksqlConfig, serviceContext, writeQueries);
        return new SourceDescriptionWithWarnings(warnings, SourceDescriptionFactory.create(dataSource, extended, readQueries, writeQueries, topicDescription, queryOffsetSummaries, sourceConstraints, remoteSourceDescriptions.stream().flatMap(sd -> sd.getClusterStatistics().stream()), remoteSourceDescriptions.stream().flatMap(sd -> sd.getClusterErrorStats().stream()), sessionProperties.getKsqlHostInfo(), ksqlExecutionContext.metricCollectors()));
    }
    return new SourceDescriptionWithWarnings(warnings, SourceDescriptionFactory.create(dataSource, extended, readQueries, writeQueries, topicDescription, queryOffsetSummaries, sourceConstraints, java.util.stream.Stream.empty(), java.util.stream.Stream.empty(), sessionProperties.getKsqlHostInfo(), ksqlExecutionContext.metricCollectors()));
}
Also used : KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) LinkedList(java.util.LinkedList) DataSource(io.confluent.ksql.metastore.model.DataSource) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) QueryOffsetSummary(io.confluent.ksql.rest.entity.QueryOffsetSummary) KafkaResponseGetFailedException(io.confluent.ksql.exception.KafkaResponseGetFailedException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) KafkaException(org.apache.kafka.common.KafkaException)

Example 2 with RunningQuery

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

the class ListSourceExecutor method queryOffsetSummaries.

private static List<QueryOffsetSummary> queryOffsetSummaries(final KsqlConfig ksqlConfig, final ServiceContext serviceContext, final List<RunningQuery> writeQueries) {
    final Map<String, Map<TopicPartition, OffsetAndMetadata>> offsetsPerQuery = new HashMap<>(writeQueries.size());
    final Map<String, Set<String>> topicsPerQuery = new HashMap<>();
    final Set<String> allTopics = new HashSet<>();
    // Get topics and offsets per running query
    for (RunningQuery query : writeQueries) {
        final QueryId queryId = query.getId();
        final String applicationId = QueryApplicationId.build(ksqlConfig, true, queryId);
        final Map<TopicPartition, OffsetAndMetadata> topicAndConsumerOffsets = serviceContext.getConsumerGroupClient().listConsumerGroupOffsets(applicationId);
        offsetsPerQuery.put(applicationId, topicAndConsumerOffsets);
        final Set<String> topics = topicAndConsumerOffsets.keySet().stream().map(TopicPartition::topic).collect(Collectors.toSet());
        topicsPerQuery.put(applicationId, topics);
        allTopics.addAll(topics);
    }
    // Get topics descriptions and start/end offsets
    final Map<String, TopicDescription> sourceTopicDescriptions = serviceContext.getTopicClient().describeTopics(allTopics);
    final Map<TopicPartition, Long> topicAndStartOffsets = serviceContext.getTopicClient().listTopicsStartOffsets(allTopics);
    final Map<TopicPartition, Long> topicAndEndOffsets = serviceContext.getTopicClient().listTopicsEndOffsets(allTopics);
    // Build consumer offsets summary
    final List<QueryOffsetSummary> offsetSummaries = new ArrayList<>();
    for (Entry<String, Set<String>> entry : topicsPerQuery.entrySet()) {
        final List<QueryTopicOffsetSummary> topicSummaries = new ArrayList<>();
        for (String topic : entry.getValue()) {
            topicSummaries.add(new QueryTopicOffsetSummary(topic, consumerPartitionOffsets(sourceTopicDescriptions.get(topic), topicAndStartOffsets, topicAndEndOffsets, offsetsPerQuery.get(entry.getKey()))));
        }
        offsetSummaries.add(new QueryOffsetSummary(entry.getKey(), topicSummaries));
    }
    return offsetSummaries;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) QueryId(io.confluent.ksql.query.QueryId) ArrayList(java.util.ArrayList) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) TopicPartition(org.apache.kafka.common.TopicPartition) QueryOffsetSummary(io.confluent.ksql.rest.entity.QueryOffsetSummary) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) QueryTopicOffsetSummary(io.confluent.ksql.rest.entity.QueryTopicOffsetSummary) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 3 with RunningQuery

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

the class Console method printQueries.

private void printQueries(final List<RunningQuery> queries, final String type, final String operation) {
    if (!queries.isEmpty()) {
        writer().println(String.format("%n%-20s%n%-20s", "Queries that " + operation + " from this " + type, "-----------------------------------"));
        for (final RunningQuery writeQuery : queries) {
            writer().println(writeQuery.getId() + " (" + writeQuery.getState().orElse("N/A") + ") : " + writeQuery.getQuerySingleLine());
        }
        writer().println("\nFor query topology and execution plan please run: EXPLAIN <QueryId>");
    }
}
Also used : RunningQuery(io.confluent.ksql.rest.entity.RunningQuery)

Example 4 with RunningQuery

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

the class TestKsqlRestApp method getQueries.

private Set<RunningQuery> getQueries(final KsqlRestClient client, final KsqlQueryType queryType) {
    final RestResponse<KsqlEntityList> response = makeKsqlRequest(client, "SHOW QUERIES;");
    if (response.isErroneous()) {
        throw new AssertionError("Failed to get persistent queries." + " msg:" + response.getErrorMessage());
    }
    final Queries queries = (Queries) response.getResponse().get(0);
    return queries.getQueries().stream().filter(query -> query.getQueryType() == queryType).collect(Collectors.toSet());
}
Also used : KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) 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) Queries(io.confluent.ksql.rest.entity.Queries)

Example 5 with RunningQuery

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

the class ShowQueriesMultiNodeFunctionalTest method getShowQueriesResult.

private static String getShowQueriesResult(final TestKsqlRestApp restApp) {
    final List<KsqlEntity> results = RestIntegrationTestUtil.makeKsqlRequest(restApp, "Show Queries;");
    if (results.size() != 1) {
        return "Expected 1 response, got " + results.size();
    }
    final KsqlEntity result = results.get(0);
    if (!(result instanceof Queries)) {
        return "Expected Queries, got " + result;
    }
    final List<RunningQuery> runningQueries = ((Queries) result).getQueries();
    if (runningQueries.size() != 1) {
        return "Expected 1 running query, got " + runningQueries.size();
    }
    return runningQueries.get(0).getStatusCount().toString();
}
Also used : Queries(io.confluent.ksql.rest.entity.Queries) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity)

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