Search in sources :

Example 1 with KsqlServerException

use of io.confluent.ksql.util.KsqlServerException in project ksql by confluentinc.

the class LocalCommands method processLocalCommandFiles.

public void processLocalCommandFiles(final ServiceContext serviceContext) {
    final FilenameFilter filter = (dir, fileName) -> fileName.endsWith(LOCAL_COMMANDS_FILE_SUFFIX);
    final File[] files = directory.listFiles(filter);
    if (files == null) {
        throw new KsqlServerException("Bad local commands directory " + directory.getAbsolutePath() + ". Please check your configuration for " + KsqlRestConfig.KSQL_LOCAL_COMMANDS_LOCATION_CONFIG);
    }
    for (final File file : files) {
        if (file.equals(currentLocalCommands.getFile())) {
            continue;
        }
        try (LocalCommandsFile localCommandsFile = LocalCommandsFile.createReadonly(file)) {
            final List<LocalCommand> localCommands = localCommandsFile.readRecords();
            cleanUpTransientQueryState(localCommands, serviceContext);
            markFileAsProcessed(file);
        } catch (Exception e) {
            LOG.error("Error processing local commands " + file.getAbsolutePath() + ". There may be orphaned transient topics or abandoned state stores.", e);
        }
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) Logger(org.slf4j.Logger) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) Files(java.nio.file.Files) ServiceContext(io.confluent.ksql.services.ServiceContext) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) Random(java.util.Random) Collectors(java.util.stream.Collectors) File(java.io.File) TransientQueryMetadata(io.confluent.ksql.util.TransientQueryMetadata) KsqlServerException(io.confluent.ksql.util.KsqlServerException) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) List(java.util.List) Closeable(java.io.Closeable) KsqlException(io.confluent.ksql.util.KsqlException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) FilenameFilter(java.io.FilenameFilter) File(java.io.File) KsqlServerException(io.confluent.ksql.util.KsqlServerException) IOException(java.io.IOException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) KsqlException(io.confluent.ksql.util.KsqlException)

Example 2 with KsqlServerException

use of io.confluent.ksql.util.KsqlServerException in project ksql by confluentinc.

the class DistributingExecutor method checkAuthorization.

private void checkAuthorization(final ConfiguredStatement<?> configured, final KsqlSecurityContext userSecurityContext, final KsqlExecutionContext serverExecutionContext) {
    final Statement statement = configured.getStatement();
    final MetaStore metaStore = serverExecutionContext.getMetaStore();
    // Check the User will be permitted to execute this statement
    authorizationValidator.ifPresent(validator -> validator.checkAuthorization(userSecurityContext, metaStore, statement));
    try {
        // Check the KSQL service principal will be permitted too
        authorizationValidator.ifPresent(validator -> validator.checkAuthorization(new KsqlSecurityContext(Optional.empty(), serverExecutionContext.getServiceContext()), metaStore, statement));
    } catch (final Exception e) {
        throw new KsqlServerException("The KSQL server is not permitted to execute the command", e);
    }
}
Also used : MetaStore(io.confluent.ksql.metastore.MetaStore) KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) TimeoutException(org.apache.kafka.common.errors.TimeoutException) AuthorizationException(org.apache.kafka.common.errors.AuthorizationException) OutOfOrderSequenceException(org.apache.kafka.common.errors.OutOfOrderSequenceException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) KsqlException(io.confluent.ksql.util.KsqlException) KsqlServerException(io.confluent.ksql.util.KsqlServerException)

Example 3 with KsqlServerException

use of io.confluent.ksql.util.KsqlServerException in project ksql by confluentinc.

the class ValidatedCommandFactory method createForAlterSystemQuery.

private static Command createForAlterSystemQuery(final ConfiguredStatement<? extends Statement> statement, final KsqlExecutionContext context) {
    final AlterSystemProperty alterSystemProperty = (AlterSystemProperty) statement.getStatement();
    final String propertyName = alterSystemProperty.getPropertyName();
    final String propertyValue = alterSystemProperty.getPropertyValue();
    // raise exception if feature flag is set
    if (!context.getKsqlConfig().getBoolean(KsqlConfig.KSQL_SHARED_RUNTIME_ENABLED)) {
        throw new KsqlServerException("Cannot alter system configs " + "when KSQL_SHARED_RUNTIME_ENABLED is turned off.");
    }
    // validate
    context.alterSystemProperty(propertyName, propertyValue);
    if (!Property.isEditable(propertyName)) {
        throw new ConfigException(String.format("Failed to set %s to %s. Caused by: " + "Not recognizable as ksql, streams, consumer, or producer property: %s %n", propertyName, propertyValue, propertyName), null);
    }
    // verify that no persistent query is running when attempting to change 'processing.guarantee'
    final KsqlConfigResolver resolver = new KsqlConfigResolver();
    final Optional<ConfigItem> resolvedItem = resolver.resolve(propertyName, false);
    if (resolvedItem.isPresent() && Objects.equals(resolvedItem.get().getPropertyName(), PROCESSING_GUARANTEE_CONFIG) && !context.getPersistentQueries().isEmpty()) {
        final Collection<QueryId> runningQueries = context.getPersistentQueries().stream().map(QueryMetadata::getQueryId).collect(Collectors.toList());
        LOG.error("Failed to set {} to {} due to the {} persistent queries currently running: {}", propertyName, propertyValue, runningQueries.size(), runningQueries);
        throw new ConfigException(String.format("Unable to set %s to %s, as the %s may not be changed for running" + " persistent queries which have already processed data under a" + " different %s. To modify %s you must first terminate all running" + " persistent queries.", propertyName, propertyValue, propertyName, propertyName, propertyName));
    }
    return Command.of(statement);
}
Also used : KsqlConfigResolver(io.confluent.ksql.config.KsqlConfigResolver) QueryId(io.confluent.ksql.query.QueryId) AlterSystemProperty(io.confluent.ksql.parser.tree.AlterSystemProperty) ConfigItem(io.confluent.ksql.config.ConfigItem) ConfigException(org.apache.kafka.common.config.ConfigException) KsqlServerException(io.confluent.ksql.util.KsqlServerException)

Example 4 with KsqlServerException

use of io.confluent.ksql.util.KsqlServerException in project ksql by confluentinc.

the class CommandStore method waitForCommandConsumer.

@Override
public void waitForCommandConsumer() {
    try {
        final long endOffset = getCommandTopicOffset();
        ensureConsumedPast(endOffset - 1, commandQueueCatchupTimeout);
    } catch (final InterruptedException e) {
        final String errorMsg = "Interrupted while waiting for command topic consumer to process command topic";
        throw new KsqlServerException(errorMsg);
    } catch (final TimeoutException e) {
        final String errorMsg = "Timeout while waiting for command topic consumer to process command topic";
        throw new KsqlServerException(errorMsg);
    }
}
Also used : KsqlServerException(io.confluent.ksql.util.KsqlServerException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with KsqlServerException

use of io.confluent.ksql.util.KsqlServerException in project ksql by confluentinc.

the class KsqlEngine method getEndOffsetsForStreamPullQuery.

private ImmutableMap<TopicPartition, Long> getEndOffsetsForStreamPullQuery(final Admin admin, final TopicDescription topicDescription) {
    final Map<TopicPartition, OffsetSpec> topicPartitions = topicDescription.partitions().stream().map(td -> new TopicPartition(topicDescription.name(), td.partition())).collect(toMap(identity(), tp -> OffsetSpec.latest()));
    final ListOffsetsResult listOffsetsResult = admin.listOffsets(topicPartitions, // so we should do the same when checking end offsets.
    new ListOffsetsOptions(IsolationLevel.READ_UNCOMMITTED));
    final ImmutableMap<TopicPartition, Long> startOffsetsForStreamPullQuery = getStartOffsetsForStreamPullQuery(admin, topicDescription);
    try {
        final Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> partitionResultMap = listOffsetsResult.all().get(10, TimeUnit.SECONDS);
        final Map<TopicPartition, Long> result = partitionResultMap.entrySet().stream().filter(e -> e.getValue().offset() > 0L && e.getValue().offset() > startOffsetsForStreamPullQuery.get(e.getKey())).collect(toMap(Entry::getKey, e -> e.getValue().offset()));
        return ImmutableMap.copyOf(result);
    } catch (final InterruptedException e) {
        log.error("Admin#listOffsets(" + topicDescription.name() + ") interrupted", e);
        throw new KsqlServerException("Interrupted");
    } catch (final ExecutionException e) {
        log.error("Error executing Admin#listOffsets(" + topicDescription.name() + ")", e);
        throw new KsqlServerException("Internal Server Error");
    } catch (final TimeoutException e) {
        log.error("Admin#listOffsets(" + topicDescription.name() + ") timed out", e);
        throw new KsqlServerException("Backend timed out");
    }
}
Also used : Query(io.confluent.ksql.parser.tree.Query) UnaryOperator.identity(java.util.function.UnaryOperator.identity) SourceName(io.confluent.ksql.name.SourceName) ServiceContext(io.confluent.ksql.services.ServiceContext) LoggerFactory(org.slf4j.LoggerFactory) RoutingOptions(io.confluent.ksql.execution.streams.RoutingOptions) TimeoutException(java.util.concurrent.TimeoutException) ProcessingLogContext(io.confluent.ksql.logging.processing.ProcessingLogContext) MutableMetaStore(io.confluent.ksql.metastore.MutableMetaStore) Context(io.vertx.core.Context) TransientQueryMetadata(io.confluent.ksql.util.TransientQueryMetadata) ListOffsetsResult(org.apache.kafka.clients.admin.ListOffsetsResult) TransientQueryCleanupListener(io.confluent.ksql.internal.TransientQueryCleanupListener) RewrittenAnalysis(io.confluent.ksql.analyzer.RewrittenAnalysis) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) QueryLogger(io.confluent.ksql.logging.query.QueryLogger) QueryId(io.confluent.ksql.query.QueryId) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) TopicPartition(org.apache.kafka.common.TopicPartition) ImmutableAnalysis(io.confluent.ksql.analyzer.ImmutableAnalysis) ImmutableMap(com.google.common.collect.ImmutableMap) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) ScalablePushQueryMetadata(io.confluent.ksql.util.ScalablePushQueryMetadata) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) ScalablePushQueryMetrics(io.confluent.ksql.internal.ScalablePushQueryMetrics) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) KafkaFuture(org.apache.kafka.common.KafkaFuture) ExecutableDdlStatement(io.confluent.ksql.parser.tree.ExecutableDdlStatement) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) MetaStoreImpl(io.confluent.ksql.metastore.MetaStoreImpl) List(java.util.List) PullQueryExecutorMetrics(io.confluent.ksql.internal.PullQueryExecutorMetrics) QueryPlannerOptions(io.confluent.ksql.planner.QueryPlannerOptions) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) ConsistencyOffsetVector(io.confluent.ksql.util.ConsistencyOffsetVector) StreamPullQueryMetadata(io.confluent.ksql.util.StreamPullQueryMetadata) Entry(java.util.Map.Entry) KsqlEngineMetrics(io.confluent.ksql.internal.KsqlEngineMetrics) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Statement(io.confluent.ksql.parser.tree.Statement) Builder(com.google.common.collect.ImmutableList.Builder) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) PullQueryResult(io.confluent.ksql.physical.pull.PullQueryResult) HARouting(io.confluent.ksql.physical.pull.HARouting) StreamsConfig(org.apache.kafka.streams.StreamsConfig) PushRouting(io.confluent.ksql.physical.scalablepush.PushRouting) HashMap(java.util.HashMap) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) Function(java.util.function.Function) ImmutableList(com.google.common.collect.ImmutableList) Analysis(io.confluent.ksql.analyzer.Analysis) ConfiguredKsqlPlan(io.confluent.ksql.planner.plan.ConfiguredKsqlPlan) MetaStore(io.confluent.ksql.metastore.MetaStore) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Admin(org.apache.kafka.clients.admin.Admin) PushRoutingOptions(io.confluent.ksql.physical.scalablepush.PushRoutingOptions) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) QueryIdGenerator(io.confluent.ksql.query.id.QueryIdGenerator) QueryContainer(io.confluent.ksql.parser.tree.QueryContainer) Logger(org.slf4j.Logger) QueryAnalyzer(io.confluent.ksql.analyzer.QueryAnalyzer) ServiceInfo(io.confluent.ksql.ServiceInfo) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) OffsetSpec(org.apache.kafka.clients.admin.OffsetSpec) KsqlServerException(io.confluent.ksql.util.KsqlServerException) IsolationLevel(org.apache.kafka.common.IsolationLevel) StreamsErrorCollector(io.confluent.ksql.metrics.StreamsErrorCollector) Closeable(java.io.Closeable) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ListOffsetsOptions(org.apache.kafka.clients.admin.ListOffsetsOptions) Collections(java.util.Collections) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) ListOffsetsOptions(org.apache.kafka.clients.admin.ListOffsetsOptions) KsqlServerException(io.confluent.ksql.util.KsqlServerException) ListOffsetsResult(org.apache.kafka.clients.admin.ListOffsetsResult) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetSpec(org.apache.kafka.clients.admin.OffsetSpec) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

KsqlServerException (io.confluent.ksql.util.KsqlServerException)21 KsqlException (io.confluent.ksql.util.KsqlException)13 ExecutionException (java.util.concurrent.ExecutionException)10 RetryException (com.github.rholder.retry.RetryException)8 URISyntaxException (java.net.URISyntaxException)8 List (java.util.List)6 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableMap (com.google.common.collect.ImmutableMap)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 MetaStore (io.confluent.ksql.metastore.MetaStore)3 Statement (io.confluent.ksql.parser.tree.Statement)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Builder (com.google.common.collect.ImmutableList.Builder)2 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)2 ServiceInfo (io.confluent.ksql.ServiceInfo)2 Analysis (io.confluent.ksql.analyzer.Analysis)2 ImmutableAnalysis (io.confluent.ksql.analyzer.ImmutableAnalysis)2 QueryAnalyzer (io.confluent.ksql.analyzer.QueryAnalyzer)2 RewrittenAnalysis (io.confluent.ksql.analyzer.RewrittenAnalysis)2