Search in sources :

Example 1 with KsqlWarning

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

the class ListConnectorsExecutor method execute.

@SuppressWarnings("OptionalGetWithoutIsPresent")
public StatementExecutorResponse execute(final ConfiguredStatement<ListConnectors> configuredStatement, final SessionProperties sessionProperties, final KsqlExecutionContext ksqlExecutionContext, final ServiceContext serviceContext) {
    final ConnectClient connectClient = serviceContext.getConnectClient();
    final ConnectResponse<List<String>> connectors = serviceContext.getConnectClient().connectors();
    if (connectors.error().isPresent()) {
        return StatementExecutorResponse.handled(connectErrorHandler.handle(configuredStatement, connectors));
    }
    final List<SimpleConnectorInfo> infos = new ArrayList<>();
    final List<KsqlWarning> warnings = new ArrayList<>();
    final Scope scope = configuredStatement.getStatement().getScope();
    for (final String name : connectors.datum().get()) {
        final ConnectResponse<ConnectorInfo> response = connectClient.describe(name);
        if (response.datum().filter(i -> inScope(i.type(), scope)).isPresent()) {
            final ConnectResponse<ConnectorStateInfo> status = connectClient.status(name);
            infos.add(fromConnectorInfoResponse(name, response, status));
        } else if (response.error().isPresent()) {
            if (scope == Scope.ALL) {
                infos.add(new SimpleConnectorInfo(name, ConnectorType.UNKNOWN, null, null));
            }
            warnings.add(new KsqlWarning(String.format("Could not describe connector %s: %s", name, response.error().get())));
        }
    }
    return StatementExecutorResponse.handled(Optional.of(new ConnectorList(configuredStatement.getStatementText(), warnings, infos)));
}
Also used : SessionProperties(io.confluent.ksql.rest.SessionProperties) Scope(io.confluent.ksql.parser.tree.ListConnectors.Scope) ConnectClient(io.confluent.ksql.services.ConnectClient) ServiceContext(io.confluent.ksql.services.ServiceContext) SimpleConnectorInfo(io.confluent.ksql.rest.entity.SimpleConnectorInfo) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) ArrayList(java.util.ArrayList) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) List(java.util.List) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) ConnectorList(io.confluent.ksql.rest.entity.ConnectorList) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) State(org.apache.kafka.connect.runtime.AbstractStatus.State) ConnectResponse(io.confluent.ksql.services.ConnectClient.ConnectResponse) Optional(java.util.Optional) ListConnectors(io.confluent.ksql.parser.tree.ListConnectors) AbstractState(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo.AbstractState) ConnectorType(org.apache.kafka.connect.runtime.rest.entities.ConnectorType) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) SimpleConnectorInfo(io.confluent.ksql.rest.entity.SimpleConnectorInfo) SimpleConnectorInfo(io.confluent.ksql.rest.entity.SimpleConnectorInfo) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) ArrayList(java.util.ArrayList) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) ConnectorList(io.confluent.ksql.rest.entity.ConnectorList) Scope(io.confluent.ksql.parser.tree.ListConnectors.Scope) ConnectClient(io.confluent.ksql.services.ConnectClient) ArrayList(java.util.ArrayList) List(java.util.List) ConnectorList(io.confluent.ksql.rest.entity.ConnectorList) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo)

Example 2 with KsqlWarning

use of io.confluent.ksql.rest.entity.KsqlWarning 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 3 with KsqlWarning

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

the class DescribeConnectorExecutor method execute.

@SuppressWarnings("OptionalGetWithoutIsPresent")
public StatementExecutorResponse execute(final ConfiguredStatement<DescribeConnector> configuredStatement, final SessionProperties sessionProperties, final KsqlExecutionContext ksqlExecutionContext, final ServiceContext serviceContext) {
    final String connectorName = configuredStatement.getStatement().getConnectorName();
    final ConnectResponse<ConnectorStateInfo> statusResponse = serviceContext.getConnectClient().status(connectorName);
    if (statusResponse.error().isPresent()) {
        return StatementExecutorResponse.handled(connectErrorHandler.handle(configuredStatement, statusResponse));
    }
    final ConnectResponse<ConnectorInfo> infoResponse = serviceContext.getConnectClient().describe(connectorName);
    if (infoResponse.error().isPresent()) {
        return StatementExecutorResponse.handled(connectErrorHandler.handle(configuredStatement, infoResponse));
    }
    final ConnectorStateInfo status = statusResponse.datum().get();
    final ConnectorInfo info = infoResponse.datum().get();
    final Optional<Connector> connector = connectorFactory.apply(info);
    final List<KsqlWarning> warnings;
    final List<String> topics;
    if (connector.isPresent()) {
        // Small optimization. If a connector's info is not found in the response, don't query for
        // active topics with the given connectorName
        final ConnectResponse<Map<String, Map<String, List<String>>>> topicsResponse = serviceContext.getConnectClient().topics(connectorName);
        // server logs.
        if (topicsResponse.error().isPresent() && topicsResponse.httpCode() == HttpStatus.SC_NOT_FOUND) {
            topics = ImmutableList.of();
            warnings = ImmutableList.of();
            LOG.warn("Could not list related topics due to error: " + topicsResponse.error().get());
        } else if (topicsResponse.error().isPresent()) {
            topics = ImmutableList.of();
            warnings = ImmutableList.of(new KsqlWarning("Could not list related topics due to error: " + topicsResponse.error().get()));
        } else {
            topics = topicsResponse.datum().get().get(connectorName).getOrDefault(TOPICS_KEY, ImmutableList.of());
            warnings = ImmutableList.of();
        }
    } else {
        topics = ImmutableList.of();
        warnings = ImmutableList.of();
    }
    final List<SourceDescription> sources;
    if (connector.isPresent()) {
        sources = ksqlExecutionContext.getMetaStore().getAllDataSources().values().stream().filter(source -> topics.contains(source.getKafkaTopicName())).map(source -> SourceDescriptionFactory.create(source, false, ImmutableList.of(), ImmutableList.of(), Optional.empty(), ImmutableList.of(), ImmutableList.of(), ksqlExecutionContext.metricCollectors())).collect(Collectors.toList());
    } else {
        sources = ImmutableList.of();
    }
    final ConnectorDescription description = new ConnectorDescription(configuredStatement.getStatementText(), info.config().get(ConnectorConfig.CONNECTOR_CLASS_CONFIG), status, sources, topics, warnings);
    return StatementExecutorResponse.handled(Optional.of(description));
}
Also used : Connectors(io.confluent.ksql.connect.supported.Connectors) SessionProperties(io.confluent.ksql.rest.SessionProperties) ServiceContext(io.confluent.ksql.services.ServiceContext) LoggerFactory(org.slf4j.LoggerFactory) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription) Function(java.util.function.Function) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) DescribeConnector(io.confluent.ksql.parser.tree.DescribeConnector) Logger(org.slf4j.Logger) Connector(io.confluent.ksql.connect.Connector) SourceDescriptionFactory(io.confluent.ksql.rest.entity.SourceDescriptionFactory) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Collectors(java.util.stream.Collectors) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) List(java.util.List) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) ConnectorDescription(io.confluent.ksql.rest.entity.ConnectorDescription) ConnectResponse(io.confluent.ksql.services.ConnectClient.ConnectResponse) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) HttpStatus(org.apache.hc.core5.http.HttpStatus) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) DescribeConnector(io.confluent.ksql.parser.tree.DescribeConnector) Connector(io.confluent.ksql.connect.Connector) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) ConnectorDescription(io.confluent.ksql.rest.entity.ConnectorDescription) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription)

Example 4 with KsqlWarning

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

the class ListSourceExecutorTest method shouldAddWarningOnClientExceptionForDescription.

@Test
public void shouldAddWarningOnClientExceptionForDescription() {
    // Given:
    final KsqlStream<?> stream1 = engine.givenSource(DataSourceType.KSTREAM, "STREAM1");
    final ServiceContext serviceContext = engine.getServiceContext();
    serviceContext.getTopicClient().deleteTopics(ImmutableList.of("STREAM1"));
    // When:
    final KsqlEntity entity = CUSTOM_EXECUTORS.showColumns().execute((ConfiguredStatement<ShowColumns>) engine.configure("DESCRIBE STREAM1 EXTENDED;"), SESSION_PROPERTIES, engine.getEngine(), serviceContext).getEntity().orElseThrow(IllegalStateException::new);
    // Then:
    assertThat(entity, instanceOf(SourceDescriptionEntity.class));
    final SourceDescriptionEntity description = (SourceDescriptionEntity) entity;
    assertThat(description.getSourceDescription(), equalTo(SourceDescriptionFactory.create(stream1, true, ImmutableList.of(), ImmutableList.of(), Optional.empty(), ImmutableList.of(), ImmutableList.of(), new MetricCollectors())));
    assertThat(description.getWarnings(), contains(new KsqlWarning("Error from Kafka: unknown topic: STREAM1")));
}
Also used : ServiceContext(io.confluent.ksql.services.ServiceContext) TestServiceContext(io.confluent.ksql.services.TestServiceContext) ShowColumns(io.confluent.ksql.parser.tree.ShowColumns) SourceDescriptionEntity(io.confluent.ksql.rest.entity.SourceDescriptionEntity) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) Test(org.junit.Test)

Example 5 with KsqlWarning

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

the class BackupRollbackIntegrationTest method isDegradedState.

private boolean isDegradedState() {
    // If in degraded state, then the following command will return a warning
    final List<KsqlEntity> response = makeKsqlRequest("Show Streams;");
    final List<KsqlWarning> warnings = response.get(0).getWarnings();
    final HealthCheckResponse res = RestIntegrationTestUtil.checkServerHealth(REST_APP);
    return !res.getDetails().get(COMMAND_RUNNER_CHECK_NAME).getIsHealthy() && (warnings.size() > 0 && warnings.get(0).getMessage().contains(DefaultErrorMessages.COMMAND_RUNNER_DEGRADED_INCOMPATIBLE_COMMANDS_ERROR_MESSAGE));
}
Also used : HealthCheckResponse(io.confluent.ksql.rest.entity.HealthCheckResponse) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity)

Aggregations

KsqlWarning (io.confluent.ksql.rest.entity.KsqlWarning)10 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)7 Test (org.junit.Test)5 SessionProperties (io.confluent.ksql.rest.SessionProperties)3 SourceDescriptionEntity (io.confluent.ksql.rest.entity.SourceDescriptionEntity)3 ServiceContext (io.confluent.ksql.services.ServiceContext)3 ImmutableList (com.google.common.collect.ImmutableList)2 IntegrationTest (io.confluent.common.utils.IntegrationTest)2 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)2 DataSource (io.confluent.ksql.metastore.model.DataSource)2 MetricCollectors (io.confluent.ksql.metrics.MetricCollectors)2 ListConnectors (io.confluent.ksql.parser.tree.ListConnectors)2 ShowColumns (io.confluent.ksql.parser.tree.ShowColumns)2 ConnectorList (io.confluent.ksql.rest.entity.ConnectorList)2 HealthCheckResponse (io.confluent.ksql.rest.entity.HealthCheckResponse)2 RunningQuery (io.confluent.ksql.rest.entity.RunningQuery)2 SimpleConnectorInfo (io.confluent.ksql.rest.entity.SimpleConnectorInfo)2 SourceDescriptionFactory (io.confluent.ksql.rest.entity.SourceDescriptionFactory)2 ConnectResponse (io.confluent.ksql.services.ConnectClient.ConnectResponse)2 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)2