Search in sources :

Example 1 with QueryMetadataHolder

use of io.confluent.ksql.rest.server.query.QueryMetadataHolder in project ksql by confluentinc.

the class StreamedQueryResource method handleStatement.

@SuppressWarnings("unchecked")
private EndpointResponse handleStatement(final KsqlSecurityContext securityContext, final KsqlRequest request, final PreparedStatement<?> statement, final CompletableFuture<Void> connectionClosedFuture, final Optional<Boolean> isInternalRequest, final MetricsCallbackHolder metricsCallbackHolder, final Context context) {
    try {
        authorizationValidator.ifPresent(validator -> validator.checkAuthorization(securityContext, ksqlEngine.getMetaStore(), statement.getStatement()));
        final Map<String, Object> configProperties = request.getConfigOverrides();
        denyListPropertyValidator.validateAll(configProperties);
        if (statement.getStatement() instanceof Query) {
            if (shouldMigrateToQueryStream(request.getConfigOverrides())) {
                return EndpointResponse.ok(new NextHandlerOutput());
            }
            final QueryMetadataHolder queryMetadataHolder = queryExecutor.handleStatement(securityContext.getServiceContext(), request.getConfigOverrides(), request.getRequestProperties(), statement, isInternalRequest, metricsCallbackHolder, context, false);
            return handleQuery((PreparedStatement<Query>) statement, connectionClosedFuture, queryMetadataHolder);
        } else if (statement.getStatement() instanceof PrintTopic) {
            return handlePrintTopic(securityContext.getServiceContext(), configProperties, (PreparedStatement<PrintTopic>) statement, connectionClosedFuture);
        } else {
            return Errors.badRequest(String.format("Statement type `%s' not supported for this resource", statement.getClass().getName()));
        }
    } catch (final TopicAuthorizationException e) {
        return errorHandler.accessDeniedFromKafkaResponse(e);
    } catch (final KsqlStatementException e) {
        return Errors.badStatement(e.getRawMessage(), e.getSqlStatement());
    } catch (final KsqlException e) {
        return errorHandler.generateResponse(e, Errors.badRequest(e));
    }
}
Also used : QueryMetadataHolder(io.confluent.ksql.rest.server.query.QueryMetadataHolder) Query(io.confluent.ksql.parser.tree.Query) PrintTopic(io.confluent.ksql.parser.tree.PrintTopic) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlException(io.confluent.ksql.util.KsqlException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) NextHandlerOutput(io.confluent.ksql.api.server.NextHandlerOutput)

Example 2 with QueryMetadataHolder

use of io.confluent.ksql.rest.server.query.QueryMetadataHolder in project ksql by confluentinc.

the class WSQueryEndpoint method handleQuery.

private void handleQuery(final RequestContext info, final Query query, final long startTimeNanos, final Context context) {
    final WebSocketSubscriber<StreamedRow> streamSubscriber = new WebSocketSubscriber<>(info.websocket);
    attachCloseHandler(info.websocket, streamSubscriber);
    final PreparedStatement<Query> statement = PreparedStatement.of(info.request.getKsql(), query);
    final MetricsCallbackHolder metricsCallbackHolder = new MetricsCallbackHolder();
    final QueryMetadataHolder queryMetadataHolder = queryExecutor.handleStatement(info.securityContext.getServiceContext(), info.request.getConfigOverrides(), info.request.getRequestProperties(), statement, Optional.empty(), metricsCallbackHolder, context, true);
    if (queryMetadataHolder.getPullQueryResult().isPresent()) {
        new PullQueryPublisher(exec, queryMetadataHolder.getPullQueryResult().get(), metricsCallbackHolder, startTimeNanos).subscribe(streamSubscriber);
    } else if (queryMetadataHolder.getPushQueryMetadata().isPresent()) {
        new PushQueryPublisher(exec, queryMetadataHolder.getPushQueryMetadata().get(), metricsCallbackHolder, startTimeNanos).subscribe(streamSubscriber);
    } else {
        throw new KsqlStatementException("Unknown query type", statement.getStatementText());
    }
}
Also used : StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) QueryMetadataHolder(io.confluent.ksql.rest.server.query.QueryMetadataHolder) Query(io.confluent.ksql.parser.tree.Query) MetricsCallbackHolder(io.confluent.ksql.api.server.MetricsCallbackHolder) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException)

Example 3 with QueryMetadataHolder

use of io.confluent.ksql.rest.server.query.QueryMetadataHolder in project ksql by confluentinc.

the class QueryEndpoint method createQueryPublisher.

public QueryPublisher createQueryPublisher(final String sql, final Map<String, Object> properties, final Map<String, Object> sessionVariables, final Map<String, Object> requestProperties, final Context context, final WorkerExecutor workerExecutor, final ServiceContext serviceContext, final MetricsCallbackHolder metricsCallbackHolder, final Optional<Boolean> isInternalRequest) {
    // Must be run on worker as all this stuff is slow
    VertxUtils.checkIsWorker();
    final ConfiguredStatement<Query> statement = createStatement(sql, properties, sessionVariables);
    final QueryMetadataHolder queryMetadataHolder = queryExecutor.handleStatement(serviceContext, properties, requestProperties, statement.getPreparedStatement(), isInternalRequest, metricsCallbackHolder, context, false);
    if (queryMetadataHolder.getPullQueryResult().isPresent()) {
        final PullQueryResult result = queryMetadataHolder.getPullQueryResult().get();
        final BlockingQueryPublisher publisher = new BlockingQueryPublisher(context, workerExecutor);
        publisher.setQueryHandle(new KsqlPullQueryHandle(result, pullQueryMetrics, statement.getPreparedStatement().getStatementText()), true, false);
        // Start from the worker thread so that errors can bubble up, and we can get a proper response
        // code rather than waiting until later after the header has been written and all we can do
        // is write an error message.
        publisher.startFromWorkerThread();
        return publisher;
    } else if (queryMetadataHolder.getPushQueryMetadata().isPresent()) {
        final PushQueryMetadata metadata = queryMetadataHolder.getPushQueryMetadata().get();
        final BlockingQueryPublisher publisher = new BlockingQueryPublisher(context, workerExecutor);
        publisher.setQueryHandle(new KsqlQueryHandle(metadata), false, queryMetadataHolder.getScalablePushQueryMetadata().isPresent());
        return publisher;
    } else {
        throw new KsqlStatementException("Unexpected metadata for query", statement.getStatementText());
    }
}
Also used : PushQueryMetadata(io.confluent.ksql.util.PushQueryMetadata) QueryMetadataHolder(io.confluent.ksql.rest.server.query.QueryMetadataHolder) Query(io.confluent.ksql.parser.tree.Query) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) PullQueryResult(io.confluent.ksql.physical.pull.PullQueryResult)

Aggregations

Query (io.confluent.ksql.parser.tree.Query)3 QueryMetadataHolder (io.confluent.ksql.rest.server.query.QueryMetadataHolder)3 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)3 MetricsCallbackHolder (io.confluent.ksql.api.server.MetricsCallbackHolder)1 NextHandlerOutput (io.confluent.ksql.api.server.NextHandlerOutput)1 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)1 PrintTopic (io.confluent.ksql.parser.tree.PrintTopic)1 PullQueryResult (io.confluent.ksql.physical.pull.PullQueryResult)1 StreamedRow (io.confluent.ksql.rest.entity.StreamedRow)1 KsqlException (io.confluent.ksql.util.KsqlException)1 PushQueryMetadata (io.confluent.ksql.util.PushQueryMetadata)1 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)1