Search in sources :

Example 11 with CommandStatus

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

the class StatementExecutor method handleStatementWithTerminatedQueries.

/**
 * Attempt to execute a single statement.
 *
 * @param command The string containing the statement to be executed
 * @param commandId The ID to be used to track the status of the command
 * @param terminatedQueries An optional map from terminated query IDs to the commands that
 *     requested their termination
 * @param wasDropped was this table/stream subsequently dropped
 * @throws Exception TODO: Refine this.
 */
private void handleStatementWithTerminatedQueries(Command command, CommandId commandId, Map<QueryId, CommandId> terminatedQueries, boolean wasDropped) throws Exception {
    try {
        String statementString = command.getStatement();
        statusStore.put(commandId, new CommandStatus(CommandStatus.Status.PARSING, "Parsing statement"));
        Statement statement = statementParser.parseSingleStatement(statementString);
        statusStore.put(commandId, new CommandStatus(CommandStatus.Status.EXECUTING, "Executing statement"));
        executeStatement(statement, command, commandId, terminatedQueries, wasDropped);
    } catch (WakeupException exception) {
        throw exception;
    } catch (Exception exception) {
        log.error("Failed to handle: " + command, exception);
        CommandStatus errorStatus = new CommandStatus(CommandStatus.Status.ERROR, ExceptionUtil.stackTraceToString(exception));
        statusStore.put(commandId, errorStatus);
        completeStatusFuture(commandId, errorStatus);
    }
}
Also used : DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) Statement(io.confluent.ksql.parser.tree.Statement) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) WakeupException(org.apache.kafka.common.errors.WakeupException) WakeupException(org.apache.kafka.common.errors.WakeupException) KsqlException(io.confluent.ksql.util.KsqlException)

Example 12 with CommandStatus

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

the class StatementExecutor method terminateQuery.

private void terminateQuery(TerminateQuery terminateQuery) throws Exception {
    final QueryId queryId = terminateQuery.getQueryId();
    final QueryMetadata queryMetadata = ksqlEngine.getPersistentQueries().get(queryId);
    if (!ksqlEngine.terminateQuery(queryId, true)) {
        throw new Exception(String.format("No running query with id %s was found", queryId));
    }
    CommandId.Type commandType;
    DataSource.DataSourceType sourceType = queryMetadata.getOutputNode().getTheSourceNode().getDataSourceType();
    switch(sourceType) {
        case KTABLE:
            commandType = CommandId.Type.TABLE;
            break;
        case KSTREAM:
            commandType = CommandId.Type.STREAM;
            break;
        default:
            throw new Exception(String.format("Unexpected source type for running query: %s", sourceType));
    }
    String queryEntity = ((KsqlStructuredDataOutputNode) queryMetadata.getOutputNode()).getKsqlTopic().getName();
    CommandId queryStatementId = new CommandId(commandType, queryEntity, CommandId.Action.CREATE);
    statusStore.put(queryStatementId, new CommandStatus(CommandStatus.Status.TERMINATED, "Query terminated"));
}
Also used : PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) QueryId(io.confluent.ksql.query.QueryId) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) WakeupException(org.apache.kafka.common.errors.WakeupException) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.serde.DataSource)

Example 13 with CommandStatus

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

the class StatementExecutor method registerQueuedStatement.

/**
 * Register the existence of a new statement that has been written to the command topic. All other
 * statement status information is updated exclusively by the current {@link StatementExecutor}
 * instance, but in the (unlikely but possible) event that a statement is written to the command
 * topic but never picked up by this instance, it should be possible to know that it was at least
 * written to the topic in the first place.
 *
 * @param commandId The ID of the statement that has been written to the command topic.
 */
public Future<CommandStatus> registerQueuedStatement(CommandId commandId) {
    statusStore.put(commandId, new CommandStatus(CommandStatus.Status.QUEUED, "Statement written to command topic"));
    CommandStatusFuture result;
    synchronized (statusFutures) {
        result = statusFutures.get(commandId);
        if (result != null) {
            return result;
        } else {
            result = new CommandStatusFuture(commandId, statusFutures::remove);
            statusFutures.put(commandId, result);
            return result;
        }
    }
}
Also used : CommandStatus(io.confluent.ksql.rest.entity.CommandStatus)

Aggregations

CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)13 CommandId (io.confluent.ksql.rest.server.computation.CommandId)5 KsqlException (io.confluent.ksql.util.KsqlException)5 WakeupException (org.apache.kafka.common.errors.WakeupException)4 CommandStatusEntity (io.confluent.ksql.rest.entity.CommandStatusEntity)3 Map (java.util.Map)3 Test (org.junit.Test)3 DdlStatement (io.confluent.ksql.parser.tree.DdlStatement)2 QueryId (io.confluent.ksql.query.QueryId)2 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)2 HashMap (java.util.HashMap)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GenericRow (io.confluent.ksql.GenericRow)1 DdlCommandResult (io.confluent.ksql.ddl.commands.DdlCommandResult)1 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)1 Expression (io.confluent.ksql.parser.tree.Expression)1 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)1 RegisterTopic (io.confluent.ksql.parser.tree.RegisterTopic)1 Relation (io.confluent.ksql.parser.tree.Relation)1