Search in sources :

Example 1 with QueryId

use of io.confluent.ksql.query.QueryId in project ksql by confluentinc.

the class KsqlContextTest method shouldRunSimpleStatements.

@Test
public void shouldRunSimpleStatements() throws Exception {
    AdminClient adminClient = mock(AdminClient.class);
    KafkaTopicClient kafkaTopicClient = mock(KafkaTopicClientImpl.class);
    KsqlEngine ksqlEngine = mock(KsqlEngine.class);
    Map<QueryId, PersistentQueryMetadata> liveQueryMap = new HashMap<>();
    KsqlContext ksqlContext = new KsqlContext(adminClient, kafkaTopicClient, ksqlEngine);
    expect(ksqlEngine.buildMultipleQueries(statement1, Collections.emptyMap())).andReturn(Collections.emptyList());
    expect(ksqlEngine.buildMultipleQueries(statement2, Collections.emptyMap())).andReturn(getQueryMetadata(new QueryId("CSAS_BIGORDERS"), DataSource.DataSourceType.KSTREAM));
    expect(ksqlEngine.getPersistentQueries()).andReturn(liveQueryMap);
    replay(ksqlEngine);
    ksqlContext.sql(statement1);
    ksqlContext.sql(statement2);
    verify(ksqlEngine);
}
Also used : KafkaTopicClient(io.confluent.ksql.util.KafkaTopicClient) HashMap(java.util.HashMap) QueryId(io.confluent.ksql.query.QueryId) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.junit.Test)

Example 2 with QueryId

use of io.confluent.ksql.query.QueryId in project ksql by confluentinc.

the class StatementExecutor method startQuery.

private boolean startQuery(String queryString, Query query, CommandId commandId, Map<QueryId, CommandId> terminatedQueries, Command command, boolean wasDropped) throws Exception {
    if (query.getQueryBody() instanceof QuerySpecification) {
        QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
        Relation into = querySpecification.getInto();
        if (into instanceof Table) {
            Table table = (Table) into;
            if (ksqlEngine.getMetaStore().getSource(table.getName().getSuffix()) != null) {
                throw new Exception(String.format("Sink specified in INTO clause already exists: %s", table.getName().getSuffix().toUpperCase()));
            }
        }
    }
    QueryMetadata queryMetadata = ksqlEngine.buildMultipleQueries(queryString, command.getKsqlProperties()).get(0);
    if (queryMetadata instanceof PersistentQueryMetadata) {
        PersistentQueryMetadata persistentQueryMetadata = (PersistentQueryMetadata) queryMetadata;
        final QueryId queryId = persistentQueryMetadata.getId();
        if (terminatedQueries != null && terminatedQueries.containsKey(queryId)) {
            CommandId terminateId = terminatedQueries.get(queryId);
            statusStore.put(terminateId, new CommandStatus(CommandStatus.Status.SUCCESS, "Termination request granted"));
            statusStore.put(commandId, new CommandStatus(CommandStatus.Status.TERMINATED, "Query terminated"));
            ksqlEngine.terminateQuery(queryId, false);
            return false;
        } else if (wasDropped) {
            ksqlEngine.terminateQuery(queryId, false);
            return false;
        } else {
            persistentQueryMetadata.getKafkaStreams().start();
            return true;
        }
    } else {
        throw new Exception(String.format("Unexpected query metadata type: %s; was expecting %s", queryMetadata.getClass().getCanonicalName(), PersistentQueryMetadata.class.getCanonicalName()));
    }
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Relation(io.confluent.ksql.parser.tree.Relation) Table(io.confluent.ksql.parser.tree.Table) 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) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 3 with QueryId

use of io.confluent.ksql.query.QueryId in project ksql by confluentinc.

the class CommandStoreTest method shouldCollectTerminatedQueries.

@Test
public void shouldCollectTerminatedQueries() {
    final CommandId terminated = new CommandId(CommandId.Type.TERMINATE, "queryId", CommandId.Action.EXECUTE);
    final ConsumerRecords<CommandId, Command> records = new ConsumerRecords<>(Collections.singletonMap(new TopicPartition("topic", 0), Collections.singletonList(new ConsumerRecord<>("topic", 0, 0, terminated, new Command("terminate query 'queryId'", Collections.emptyMap())))));
    EasyMock.expect(commandConsumer.partitionsFor(COMMAND_TOPIC)).andReturn(Collections.emptyList());
    EasyMock.expect(commandConsumer.poll(anyLong())).andReturn(records).andReturn(new ConsumerRecords<>(Collections.emptyMap()));
    EasyMock.replay(commandConsumer);
    final CommandStore commandStore = new CommandStore(COMMAND_TOPIC, commandConsumer, commandProducer, new CommandIdAssigner(new MetaStoreImpl()));
    final RestoreCommands restoreCommands = commandStore.getRestoreCommands();
    assertThat(restoreCommands.terminatedQueries(), equalTo(Collections.singletonMap(new QueryId("queryId"), terminated)));
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) MetaStoreImpl(io.confluent.ksql.metastore.MetaStoreImpl) QueryId(io.confluent.ksql.query.QueryId) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) Test(org.junit.Test)

Example 4 with QueryId

use of io.confluent.ksql.query.QueryId in project ksql by confluentinc.

the class RestoreCommandsTest method shouldHaveMapContainingTerminatedQueriesThatWereIssuedAfterCreate.

@Test
public void shouldHaveMapContainingTerminatedQueriesThatWereIssuedAfterCreate() {
    restoreCommands.addCommand(createId, createCommand);
    restoreCommands.addCommand(terminateId, terminateCommand);
    restoreCommands.forEach((commandId, command, terminatedQueries, droppedEntities) -> {
        assertThat(commandId, equalTo(createId));
        assertThat(command, equalTo(createCommand));
        assertThat(terminatedQueries, equalTo(Collections.singletonMap(new QueryId("queryId"), terminateId)));
    });
}
Also used : QueryId(io.confluent.ksql.query.QueryId) Test(org.junit.Test)

Example 5 with QueryId

use of io.confluent.ksql.query.QueryId in project ksql by confluentinc.

the class RestoreCommandsTest method shouldNotHaveTerminatedQueryWhenDroppedAndRecreatedAfterTerminate.

@Test
public void shouldNotHaveTerminatedQueryWhenDroppedAndRecreatedAfterTerminate() {
    restoreCommands.addCommand(createId, createCommand);
    restoreCommands.addCommand(terminateId, terminateCommand);
    // drop
    restoreCommands.addCommand(dropId, dropCommand);
    // recreate
    restoreCommands.addCommand(createId, createCommand);
    final List<Pair<CommandId, Map<QueryId, CommandId>>> results = new ArrayList<>();
    restoreCommands.forEach((commandId, command, terminatedQueries, droppedEntities) -> {
        results.add(new Pair<>(commandId, terminatedQueries));
    });
    assertThat(results, equalTo(Arrays.asList(new Pair<>(createId, Collections.singletonMap(new QueryId("queryId"), terminateId)), new Pair<>(dropId, Collections.<QueryId, CommandId>emptyMap()), new Pair<>(createId, Collections.<QueryId, CommandId>emptyMap()))));
}
Also used : QueryId(io.confluent.ksql.query.QueryId) ArrayList(java.util.ArrayList) Pair(io.confluent.ksql.util.Pair) Test(org.junit.Test)

Aggregations

QueryId (io.confluent.ksql.query.QueryId)10 Test (org.junit.Test)6 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)5 KsqlException (io.confluent.ksql.util.KsqlException)3 HashMap (java.util.HashMap)3 CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)2 SourceDescription (io.confluent.ksql.rest.entity.SourceDescription)2 QueryMetadata (io.confluent.ksql.util.QueryMetadata)2 ArrayList (java.util.ArrayList)2 WakeupException (org.apache.kafka.common.errors.WakeupException)2 FakeException (io.confluent.ksql.FakeException)1 KsqlStream (io.confluent.ksql.metastore.KsqlStream)1 KsqlTable (io.confluent.ksql.metastore.KsqlTable)1 MetaStoreImpl (io.confluent.ksql.metastore.MetaStoreImpl)1 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)1 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)1 Relation (io.confluent.ksql.parser.tree.Relation)1 Table (io.confluent.ksql.parser.tree.Table)1 KsqlStructuredDataOutputNode (io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode)1 CommandStatusEntity (io.confluent.ksql.rest.entity.CommandStatusEntity)1