use of io.confluent.ksql.rest.entity.CommandId in project ksql by confluentinc.
the class BackupRollbackIntegrationTest method shouldEnterDegradedStateWithBackupEnabled.
@Test
public void shouldEnterDegradedStateWithBackupEnabled() {
// Given
TEST_HARNESS.ensureTopics("topic1", "topic2");
makeKsqlRequest("CREATE STREAM TOPIC1 (ID INT) " + "WITH (KAFKA_TOPIC='topic1', VALUE_FORMAT='JSON');");
makeKsqlRequest("CREATE STREAM TOPIC2 (ID INT) " + "WITH (KAFKA_TOPIC='topic2', VALUE_FORMAT='JSON');");
makeKsqlRequest("CREATE STREAM stream1 AS SELECT * FROM topic1;");
makeKsqlRequest("CREATE STREAM stream2 AS SELECT * FROM topic2;");
// When
final CommandId commandId = new CommandId("TOPIC", "entity", "CREATE");
final Command command = new Command("statement", Collections.emptyMap(), Collections.emptyMap(), Optional.empty(), Optional.of(Command.VERSION + 1), Command.VERSION + 1);
produceToCommandTopic(commandId, command);
// Server should enter degraded state due to incompatible command
assertThatEventually("Degraded State", this::isDegradedState, is(true));
try {
List<String> allLines = Files.readAllLines(backupFile);
assertThat("All commands backed up from command topic", allLines.size() == 5);
assertThat("Incompatible command in backup", allLines.get(allLines.size() - 1).contains("\"version\":" + (Command.VERSION + 1)));
} catch (IOException e) {
e.printStackTrace();
}
// Re-load the command topic
REST_APP.stop();
REST_APP.start();
// Then
final List<String> streamsNames = showStreams();
assertThat("Should have TOPIC1", streamsNames.contains("TOPIC1"), is(true));
assertThat("Should have TOPIC2", streamsNames.contains("TOPIC2"), is(true));
assertThat("Should have STREAM1", streamsNames.contains("STREAM1"), is(true));
assertThat("Should have STREAM2", streamsNames.contains("STREAM2"), is(true));
assertThat("Server should be in degraded state", isDegradedState(), is(true));
}
use of io.confluent.ksql.rest.entity.CommandId in project ksql by confluentinc.
the class RestApiTest method shouldExecuteStatusesRequest.
@Test
public void shouldExecuteStatusesRequest() {
// When:
final CommandStatuses response = RestIntegrationTestUtil.makeStatusesRequest(REST_APP);
// Then:
CommandId expected = new CommandId(Type.STREAM, "`" + PAGE_VIEW_STREAM + "`", Action.CREATE);
assertThat(response.containsKey(expected), is(true));
}
use of io.confluent.ksql.rest.entity.CommandId in project ksql by confluentinc.
the class KsqlRestoreCommandTopic method checkValidCommands.
/**
* Checks all CommandId and Command pairs to see if they're compatible with the current
* server version. If skipIncompatibleCommands is true, skip the command and try to clean up
* streams state stores and internal topics if the command being skipped is a query.
* If false, throw an exception when an incomptaible command is detected.
*
* @param records a list of CommandId and Command pairs
* @param skipIncompatibleCommands whether or not to throw an exception on incompatible commands
* @param ksqlConfig the {@link KsqlConfig} used by the program
* @return a list of compatible CommandId and Command pairs
*/
private static List<Pair<byte[], byte[]>> checkValidCommands(final List<Pair<byte[], byte[]>> records, final boolean skipIncompatibleCommands, final KsqlConfig ksqlConfig) {
int n = 0;
int numFilteredCommands = 0;
final List<Pair<byte[], byte[]>> filteredRecords = new ArrayList<>();
final List<byte[]> incompatibleCommands = new ArrayList<>();
for (final Pair<byte[], byte[]> record : records) {
n++;
try {
InternalTopicSerdes.deserializer(CommandId.class).deserialize(null, record.getLeft());
} catch (final Exception e) {
throw new KsqlException(String.format("Invalid CommandId string (line %d): %s (%s)", n, new String(record.getLeft(), StandardCharsets.UTF_8), e.getMessage()));
}
try {
InternalTopicSerdes.deserializer(Command.class).deserialize(null, record.getRight());
} catch (final SerializationException | IncompatibleKsqlCommandVersionException e) {
if (skipIncompatibleCommands) {
incompatibleCommands.add(record.getRight());
numFilteredCommands++;
continue;
} else {
throw new KsqlException(String.format("Incompatible Command string (line %d): %s (%s)", n, new String(record.getLeft(), StandardCharsets.UTF_8), e.getMessage()));
}
} catch (final Exception e) {
throw new KsqlException(String.format("Invalid Command string (line %d): %s (%s)", n, new String(record.getRight(), StandardCharsets.UTF_8), e.getMessage()));
}
filteredRecords.add(record);
}
if (skipIncompatibleCommands) {
System.out.println(String.format("%s incompatible command(s) skipped from backup file.", numFilteredCommands));
incompatibleCommands.forEach(command -> maybeCleanUpQuery(command, ksqlConfig));
}
return filteredRecords;
}
use of io.confluent.ksql.rest.entity.CommandId in project ksql by confluentinc.
the class StatusResource method getStatus.
public EndpointResponse getStatus(final String type, final String entity, final String action) {
final CommandId commandId = new CommandId(type, entity, action);
final Optional<CommandStatus> commandStatus = statementExecutor.getStatus(commandId);
return commandStatus.map(EndpointResponse::ok).orElseGet(() -> Errors.notFound("Command not found"));
}
use of io.confluent.ksql.rest.entity.CommandId in project ksql by confluentinc.
the class KsqlResourceTest method setUp.
@Before
public void setUp() throws IOException, RestClientException {
commandStatus = new QueuedCommandStatus(0, new CommandStatusFuture(new CommandId(TOPIC, "whateva", CREATE)));
commandStatus1 = new QueuedCommandStatus(1, new CommandStatusFuture(new CommandId(TABLE, "something", DROP)));
final QueuedCommandStatus commandStatus2 = new QueuedCommandStatus(2, new CommandStatusFuture(new CommandId(STREAM, "something", EXECUTE)));
kafkaTopicClient = new FakeKafkaTopicClient();
kafkaConsumerGroupClient = new FakeKafkaConsumerGroupClient();
serviceContext = TestServiceContext.create(kafkaTopicClient, kafkaConsumerGroupClient);
schemaRegistryClient = serviceContext.getSchemaRegistryClient();
registerValueSchema(schemaRegistryClient);
ksqlRestConfig = new KsqlRestConfig(getDefaultKsqlConfig());
ksqlConfig = new KsqlConfig(ksqlRestConfig.getKsqlConfigProperties());
final KsqlExecutionContext.ExecuteResult result = mock(KsqlExecutionContext.ExecuteResult.class);
when(sandbox.execute(any(), any(ConfiguredKsqlPlan.class))).thenReturn(result);
when(result.getQuery()).thenReturn(Optional.empty());
MutableFunctionRegistry fnRegistry = new InternalFunctionRegistry();
final Metrics metrics = new Metrics();
UserFunctionLoader.newInstance(ksqlConfig, fnRegistry, ".", metrics).load();
metaStore = new MetaStoreImpl(fnRegistry);
final MetricCollectors metricCollectors = new MetricCollectors(metrics);
realEngine = KsqlEngineTestUtil.createKsqlEngine(serviceContext, metaStore, (engine) -> new KsqlEngineMetrics("", engine, Collections.emptyMap(), Optional.empty(), metricCollectors), new SequentialQueryIdGenerator(), ksqlConfig, metricCollectors);
securityContext = new KsqlSecurityContext(Optional.empty(), serviceContext);
when(commandRunner.getCommandQueue()).thenReturn(commandStore);
when(commandRunnerWarning.get()).thenReturn("");
when(commandStore.createTransactionalProducer()).thenReturn(transactionalProducer);
ksqlEngine = realEngine;
when(sandbox.getMetaStore()).thenAnswer(inv -> metaStore.copy());
addTestTopicAndSources();
when(commandStore.enqueueCommand(any(), any(), any(Producer.class))).thenReturn(commandStatus).thenReturn(commandStatus1).thenReturn(commandStatus2);
streamName = KsqlIdentifierTestUtil.uniqueIdentifierName();
when(schemaInjectorFactory.apply(any())).thenReturn(sandboxSchemaInjector);
when(schemaInjectorFactory.apply(serviceContext)).thenReturn(schemaInjector);
when(topicInjectorFactory.apply(any())).thenReturn(sandboxTopicInjector);
when(topicInjectorFactory.apply(ksqlEngine)).thenReturn(topicInjector);
when(sandboxSchemaInjector.inject(any())).thenAnswer(inv -> inv.getArgument(0));
when(schemaInjector.inject(any())).thenAnswer(inv -> inv.getArgument(0));
when(sandboxTopicInjector.inject(any())).thenAnswer(inv -> inv.getArgument(0));
when(topicInjector.inject(any())).thenAnswer(inv -> inv.getArgument(0));
when(errorsHandler.generateResponse(any(), any())).thenAnswer(new Answer<EndpointResponse>() {
@Override
public EndpointResponse answer(final InvocationOnMock invocation) throws Throwable {
final Object[] args = invocation.getArguments();
return (EndpointResponse) args[1];
}
});
setUpKsqlResource();
}
Aggregations