Search in sources :

Example 1 with TerminateQueryEntity

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

the class TerminateQueryExecutorTest method shouldTerminateTransientQuery.

@Test
public void shouldTerminateTransientQuery() {
    // Given:
    final ConfiguredStatement<TerminateQuery> terminateTransient = (ConfiguredStatement<TerminateQuery>) engine.configure("TERMINATE TRANSIENT_QUERY;");
    final TransientQueryMetadata transientQueryMetadata = givenTransientQuery("TRANSIENT_QUERY", RUNNING_QUERY_STATE);
    final QueryId transientQueryId = transientQueryMetadata.getQueryId();
    final KsqlEngine engine = mock(KsqlEngine.class);
    when(engine.getQuery(transientQueryId)).thenReturn(Optional.of(transientQueryMetadata));
    // When:
    final Optional<KsqlEntity> ksqlEntity = CUSTOM_EXECUTORS.terminateQuery().execute(terminateTransient, mock(SessionProperties.class), engine, this.engine.getServiceContext()).getEntity();
    // Then:
    assertThat(ksqlEntity, is(Optional.of(new TerminateQueryEntity(terminateTransient.getStatementText(), transientQueryId.toString(), true))));
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) TerminateQueryEntity(io.confluent.ksql.rest.entity.TerminateQueryEntity) QueryId(io.confluent.ksql.query.QueryId) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) TransientQueryMetadata(io.confluent.ksql.util.TransientQueryMetadata) Test(org.junit.Test)

Example 2 with TerminateQueryEntity

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

the class TerminateQueryExecutor method execute.

public static StatementExecutorResponse execute(final ConfiguredStatement<TerminateQuery> statement, final SessionProperties sessionProperties, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
    final TerminateQuery terminateQuery = statement.getStatement();
    // do default behaviour for TERMINATE ALL
    if (!terminateQuery.getQueryId().isPresent()) {
        return StatementExecutorResponse.notHandled();
    }
    final QueryId queryId = terminateQuery.getQueryId().get();
    final RemoteHostExecutor remoteHostExecutor = RemoteHostExecutor.create(statement, sessionProperties, executionContext, serviceContext.getKsqlClient());
    if (executionContext.getPersistentQuery(queryId).isPresent() || statement.getStatementText().equals(TerminateCluster.TERMINATE_CLUSTER_STATEMENT_TEXT)) {
        // do default behaviour for terminating persistent queries
        return StatementExecutorResponse.notHandled();
    } else {
        // propagate terminate query to other nodes
        if (executionContext.getQuery(queryId).isPresent()) {
            executionContext.getQuery(queryId).get().close();
        } else {
            final boolean wasTerminatedRemotely = remoteHostExecutor.fetchAllRemoteResults().getLeft().values().stream().map(TerminateQueryEntity.class::cast).map(TerminateQueryEntity::getWasTerminated).anyMatch(b -> b.equals(true));
            if (!wasTerminatedRemotely) {
                throw new KsqlException(String.format("Failed to terminate query with query ID: '%s'", queryId));
            }
        }
        return StatementExecutorResponse.handled(Optional.of(new TerminateQueryEntity(statement.getStatementText(), queryId.toString(), true)));
    }
}
Also used : TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) TerminateQueryEntity(io.confluent.ksql.rest.entity.TerminateQueryEntity) QueryId(io.confluent.ksql.query.QueryId) KsqlException(io.confluent.ksql.util.KsqlException)

Example 3 with TerminateQueryEntity

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

the class ConsoleTest method shouldPrintTerminateQuery.

@Test
public void shouldPrintTerminateQuery() {
    // Given:
    final KsqlEntity entity = new TerminateQueryEntity("statementText", "queryId", true);
    // When:
    console.printKsqlEntityList(ImmutableList.of(entity));
    // Then:
    final String output = terminal.getOutputString();
    Approvals.verify(output, approvalOptions);
}
Also used : TerminateQueryEntity(io.confluent.ksql.rest.entity.TerminateQueryEntity) Matchers.containsString(org.hamcrest.Matchers.containsString) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) Test(org.junit.Test)

Aggregations

TerminateQueryEntity (io.confluent.ksql.rest.entity.TerminateQueryEntity)3 TerminateQuery (io.confluent.ksql.parser.tree.TerminateQuery)2 QueryId (io.confluent.ksql.query.QueryId)2 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)2 Test (org.junit.Test)2 KsqlEngine (io.confluent.ksql.engine.KsqlEngine)1 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)1 KsqlException (io.confluent.ksql.util.KsqlException)1 TransientQueryMetadata (io.confluent.ksql.util.TransientQueryMetadata)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1