Search in sources :

Example 6 with CommandId

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

the class InteractiveStatementExecutorTest method shouldSkipStartWhenReplayingLog.

@Test
public void shouldSkipStartWhenReplayingLog() {
    // Given:
    final QueryId queryId = new QueryId("csas-query-id");
    final String name = "foo";
    final PersistentQueryMetadata mockQuery = mockReplayCSAS(queryId);
    final Command command = new Command("CSAS", emptyMap(), emptyMap(), Optional.of(plan));
    when(commandDeserializer.deserialize(any(), any())).thenReturn(command);
    // When:
    statementExecutorWithMocks.handleRestore(new QueuedCommand(new CommandId(Type.STREAM, name, Action.CREATE), command, Optional.empty(), 0L));
    // Then:
    verify(mockQueryIdGenerator, times(1)).setNextId(anyLong());
    verify(mockQuery, times(0)).start();
}
Also used : QueryId(io.confluent.ksql.query.QueryId) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CommandId(io.confluent.ksql.rest.entity.CommandId) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) Test(org.junit.Test)

Example 7 with CommandId

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

the class InteractiveStatementExecutorTest method shouldThrowOnUnexpectedException.

@Test
public void shouldThrowOnUnexpectedException() {
    // Given:
    final String statementText = "mama said knock you out";
    final RuntimeException exception = new RuntimeException("i'm gonna knock you out");
    when(mockParser.parseSingleStatement(statementText)).thenThrow(exception);
    final Command command = new Command(statementText, emptyMap(), emptyMap(), Optional.empty());
    final CommandId commandId = new CommandId(CommandId.Type.STREAM, "_CSASGen", CommandId.Action.CREATE);
    // When:
    try {
        handleStatement(statementExecutorWithMocks, command, commandId, Optional.empty(), 0);
        Assert.fail("handleStatement should throw");
    } catch (final RuntimeException caughtException) {
        // Then:
        assertThat(caughtException, is(exception));
    }
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CommandId(io.confluent.ksql.rest.entity.CommandId) Test(org.junit.Test)

Example 8 with CommandId

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

the class InteractiveStatementExecutorTest method shouldDoIdempotentTerminate.

@Test
public void shouldDoIdempotentTerminate() {
    // Given:
    final String queryStatement = "a persistent query";
    final TerminateQuery terminate = mock(TerminateQuery.class);
    when(terminate.getQueryId()).thenReturn(Optional.of(new QueryId("foo")));
    when(mockParser.parseSingleStatement(any())).thenReturn(PreparedStatement.of(queryStatement, terminate));
    final PersistentQueryMetadata query = mock(PersistentQueryMetadata.class);
    when(mockEngine.getPersistentQuery(new QueryId("foo"))).thenReturn(Optional.of(query)).thenReturn(Optional.empty());
    final Command command = new Command("terminate all", emptyMap(), emptyMap(), Optional.empty());
    when(commandDeserializer.deserialize(any(), any())).thenReturn(command);
    final QueuedCommand cmd = new QueuedCommand(new CommandId(Type.TERMINATE, "-", Action.EXECUTE), command, Optional.empty(), 0L);
    // When:
    statementExecutorWithMocks.handleStatement(cmd);
    statementExecutorWithMocks.handleStatement(cmd);
// Then should not throw
}
Also used : TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) QueryId(io.confluent.ksql.query.QueryId) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CommandId(io.confluent.ksql.rest.entity.CommandId) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) Test(org.junit.Test)

Example 9 with CommandId

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

the class InteractiveStatementExecutorTest method shouldEnforceReferentialIntegrity.

@Test
public void shouldEnforceReferentialIntegrity() {
    createStreamsAndStartTwoPersistentQueries();
    // Now try to drop streams/tables to test referential integrity
    assertThrows(KsqlStatementException.class, () -> tryDropThatViolatesReferentialIntegrity());
    // Terminate the queries using the stream/table
    terminateQueries();
    // Drop user1pv stream, which is linked to the pageview stream.
    // The user1pv query will be terminated during the drop.
    final Command dropStreamCommand1 = commandWithPlan("drop stream user1pv;", ksqlConfig.getAllConfigPropsWithSecretsObfuscated());
    final CommandId dropStreamCommandId1 = new CommandId(Type.STREAM, "_user1pv", CommandId.Action.DROP);
    handleStatement(statementExecutor, dropStreamCommand1, dropStreamCommandId1, Optional.empty(), 4);
    // Now drop should be successful
    final Command dropTableCommand2 = commandWithPlan("drop table table1;", ksqlConfig.getAllConfigPropsWithSecretsObfuscated());
    final CommandId dropTableCommandId2 = new CommandId(CommandId.Type.TABLE, "_TABLE1", CommandId.Action.DROP);
    handleStatement(statementExecutor, dropTableCommand2, dropTableCommandId2, Optional.empty(), 4);
    // DROP should succeed since no query is using the table
    final Optional<CommandStatus> dropTableCommandStatus2 = statementExecutor.getStatus(dropTableCommandId2);
    Assert.assertTrue(dropTableCommandStatus2.isPresent());
    assertThat(dropTableCommandStatus2.get().getStatus(), equalTo(CommandStatus.Status.SUCCESS));
    // DROP should succeed since no query is using the stream.
    final Command dropStreamCommand3 = commandWithPlan("drop stream pageview;", ksqlConfig.getAllConfigPropsWithSecretsObfuscated());
    final CommandId dropStreamCommandId3 = new CommandId(CommandId.Type.STREAM, "_user1pv", CommandId.Action.DROP);
    handleStatement(statementExecutor, dropStreamCommand3, dropStreamCommandId3, Optional.empty(), 5);
    final Optional<CommandStatus> dropStreamCommandStatus3 = statementExecutor.getStatus(dropStreamCommandId3);
    assertThat(dropStreamCommandStatus3.get().getStatus(), CoreMatchers.equalTo(CommandStatus.Status.SUCCESS));
}
Also used : CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) CommandId(io.confluent.ksql.rest.entity.CommandId) Test(org.junit.Test)

Example 10 with CommandId

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

the class InteractiveStatementExecutorTest method shouldSetNextQueryIdToNextOffsetWhenExecutingRestoreCommand.

@Test
public void shouldSetNextQueryIdToNextOffsetWhenExecutingRestoreCommand() {
    // Given:
    mockReplayCSAS(new QueryId("csas-query-id"));
    final Command command = new Command("CSAS", emptyMap(), emptyMap(), Optional.of(plan));
    when(commandDeserializer.deserialize(any(), any())).thenReturn(command);
    // When:
    statementExecutorWithMocks.handleRestore(new QueuedCommand(new CommandId(Type.STREAM, "foo", Action.CREATE), command, Optional.empty(), 2L));
    // Then:
    verify(mockQueryIdGenerator).setNextId(3L);
}
Also used : QueryId(io.confluent.ksql.query.QueryId) CommandId(io.confluent.ksql.rest.entity.CommandId) Test(org.junit.Test)

Aggregations

CommandId (io.confluent.ksql.rest.entity.CommandId)29 Test (org.junit.Test)20 CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)13 CommandStatusEntity (io.confluent.ksql.rest.entity.CommandStatusEntity)7 QueryId (io.confluent.ksql.query.QueryId)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 BaseApiTest (io.confluent.ksql.api.BaseApiTest)5 KsqlException (io.confluent.ksql.util.KsqlException)5 Command (io.confluent.ksql.rest.server.computation.Command)4 JsonObject (io.vertx.core.json.JsonObject)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 IntegrationTest (io.confluent.common.utils.IntegrationTest)3 KsqlServerException (io.confluent.ksql.util.KsqlServerException)3 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 KsqlPlan (io.confluent.ksql.engine.KsqlPlan)2