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);
}
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()));
}
}
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)));
}
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)));
});
}
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()))));
}
Aggregations