Search in sources :

Example 1 with KsqlRestException

use of io.confluent.ksql.rest.server.resources.KsqlRestException in project ksql by confluentinc.

the class DefaultCommandQueueSync method waitFor.

@Override
public void waitFor(final KsqlEntityList previousCommands, final Class<? extends Statement> statementClass) {
    if (mustSync.test(statementClass)) {
        final ArrayList<KsqlEntity> reversed = new ArrayList<>(previousCommands);
        Collections.reverse(reversed);
        reversed.stream().filter(e -> e instanceof CommandStatusEntity).map(CommandStatusEntity.class::cast).map(CommandStatusEntity::getCommandSequenceNumber).findFirst().ifPresent(seqNum -> {
            try {
                commandQueue.ensureConsumedPast(seqNum, timeout);
            } catch (final InterruptedException e) {
                throw new KsqlRestException(Errors.serverShuttingDown());
            } catch (final TimeoutException e) {
                throw new KsqlRestException(Errors.commandQueueCatchUpTimeout(seqNum));
            }
        });
    }
}
Also used : KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) ArrayList(java.util.ArrayList) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with KsqlRestException

use of io.confluent.ksql.rest.server.resources.KsqlRestException in project ksql by confluentinc.

the class StreamedQueryResourceTest method shouldReturn400OnBadStatement.

@Test
public void shouldReturn400OnBadStatement() {
    // Given:
    when(mockStatementParser.parseSingleStatement(any())).thenThrow(new IllegalArgumentException("some error message"));
    // When:
    final KsqlRestException e = assertThrows(KsqlRestException.class, () -> testResource.streamQuery(securityContext, new KsqlRequest("query", Collections.emptyMap(), Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context));
    // Then:
    assertThat(e, exceptionStatusCode(is(BAD_REQUEST.code())));
    assertThat(e, exceptionErrorMessage(errorMessage(is("some error message"))));
    assertThat(e, exceptionErrorMessage(errorCode(is(ERROR_CODE_BAD_STATEMENT))));
}
Also used : KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) CompletableFuture(java.util.concurrent.CompletableFuture) MetricsCallbackHolder(io.confluent.ksql.api.server.MetricsCallbackHolder) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) Test(org.junit.Test)

Example 3 with KsqlRestException

use of io.confluent.ksql.rest.server.resources.KsqlRestException in project ksql by confluentinc.

the class StreamedQueryResourceTest method shouldSuggestAlternativesIfPrintTopicDoesNotExist.

@Test
public void shouldSuggestAlternativesIfPrintTopicDoesNotExist() {
    // Given:
    final PrintTopic cmd = mock(PrintTopic.class);
    when(cmd.getTopic()).thenReturn("TEST_TOPIC");
    print = PreparedStatement.of("print", cmd);
    when(mockStatementParser.<PrintTopic>parseSingleStatement(any())).thenReturn(print);
    when(mockKafkaTopicClient.isTopicExists(any())).thenReturn(false);
    when(mockKafkaTopicClient.listTopicNames()).thenReturn(ImmutableSet.of("aTopic", "test_topic", "Test_Topic"));
    // When:
    final KsqlRestException e = assertThrows(KsqlRestException.class, () -> testResource.streamQuery(securityContext, new KsqlRequest(PRINT_TOPIC, Collections.emptyMap(), Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context));
    // Then:
    assertThat(e, exceptionStatusCode(is(BAD_REQUEST.code())));
    assertThat(e, exceptionErrorMessage(errorMessage(containsString("Could not find topic 'TEST_TOPIC', " + "or the KSQL user does not have permissions to list the topic. " + "Topic names are case-sensitive." + System.lineSeparator() + "Did you mean:"))));
    assertThat(e, exceptionErrorMessage(errorMessage(containsString("\tprint test_topic;"))));
    assertThat(e, exceptionErrorMessage(errorMessage(containsString("\tprint Test_Topic;"))));
}
Also used : KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) CompletableFuture(java.util.concurrent.CompletableFuture) MetricsCallbackHolder(io.confluent.ksql.api.server.MetricsCallbackHolder) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) PrintTopic(io.confluent.ksql.parser.tree.PrintTopic) Test(org.junit.Test)

Example 4 with KsqlRestException

use of io.confluent.ksql.rest.server.resources.KsqlRestException in project ksql by confluentinc.

the class StreamedQueryResourceTest method shouldThrowOnHandleStatementIfNotConfigured.

@Test
public void shouldThrowOnHandleStatementIfNotConfigured() {
    // Given:
    testResource = new StreamedQueryResource(mockKsqlEngine, ksqlRestConfig, mockStatementParser, commandQueue, DISCONNECT_CHECK_INTERVAL, COMMAND_QUEUE_CATCHUP_TIMOEUT, activenessRegistrar, Optional.of(authorizationValidator), errorsHandler, denyListPropertyValidator, queryExecutor);
    // When:
    final KsqlRestException e = assertThrows(KsqlRestException.class, () -> testResource.streamQuery(securityContext, new KsqlRequest("query", Collections.emptyMap(), Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context));
    // Then:
    assertThat(e, exceptionStatusCode(is(SERVICE_UNAVAILABLE.code())));
    assertThat(e, exceptionErrorMessage(errorMessage(Matchers.is("Server initializing"))));
}
Also used : KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) CompletableFuture(java.util.concurrent.CompletableFuture) MetricsCallbackHolder(io.confluent.ksql.api.server.MetricsCallbackHolder) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) Test(org.junit.Test)

Example 5 with KsqlRestException

use of io.confluent.ksql.rest.server.resources.KsqlRestException in project ksql by confluentinc.

the class StreamedQueryResource method handlePrintTopic.

private EndpointResponse handlePrintTopic(final ServiceContext serviceContext, final Map<String, Object> streamProperties, final PreparedStatement<PrintTopic> statement, final CompletableFuture<Void> connectionClosedFuture) {
    final PrintTopic printTopic = statement.getStatement();
    final String topicName = printTopic.getTopic();
    if (!serviceContext.getTopicClient().isTopicExists(topicName)) {
        final Collection<String> possibleAlternatives = findPossibleTopicMatches(topicName, serviceContext);
        final String reverseSuggestion = possibleAlternatives.isEmpty() ? "" : possibleAlternatives.stream().map(name -> "\tprint " + name + ";").collect(Collectors.joining(System.lineSeparator(), System.lineSeparator() + "Did you mean:" + System.lineSeparator(), ""));
        throw new KsqlRestException(Errors.badRequest("Could not find topic '" + topicName + "', " + "or the KSQL user does not have permissions to list the topic. " + "Topic names are case-sensitive." + reverseSuggestion));
    }
    final Map<String, Object> propertiesWithOverrides = new HashMap<>(ksqlConfig.getKsqlStreamConfigProps());
    propertiesWithOverrides.putAll(streamProperties);
    final TopicStreamWriter topicStreamWriter = TopicStreamWriter.create(serviceContext, propertiesWithOverrides, printTopic, disconnectCheckInterval, connectionClosedFuture);
    log.info("Printing topic '{}'", topicName);
    return EndpointResponse.ok(topicStreamWriter);
}
Also used : KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) HashMap(java.util.HashMap) PrintTopic(io.confluent.ksql.parser.tree.PrintTopic)

Aggregations

KsqlRestException (io.confluent.ksql.rest.server.resources.KsqlRestException)11 Test (org.junit.Test)8 MetricsCallbackHolder (io.confluent.ksql.api.server.MetricsCallbackHolder)4 KsqlRequest (io.confluent.ksql.rest.entity.KsqlRequest)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 PrintTopic (io.confluent.ksql.parser.tree.PrintTopic)3 TimeoutException (java.util.concurrent.TimeoutException)3 CommandStatusEntity (io.confluent.ksql.rest.entity.CommandStatusEntity)2 InsertInto (io.confluent.ksql.parser.tree.InsertInto)1 Query (io.confluent.ksql.parser.tree.Query)1 CommandId (io.confluent.ksql.rest.entity.CommandId)1 CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)1 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)1 KsqlErrorMessage (io.confluent.ksql.rest.entity.KsqlErrorMessage)1 StatementExecutorResponse (io.confluent.ksql.rest.server.execution.StatementExecutorResponse)1 KsqlConfig (io.confluent.ksql.util.KsqlConfig)1 KsqlException (io.confluent.ksql.util.KsqlException)1 KsqlServerException (io.confluent.ksql.util.KsqlServerException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1