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));
}
}
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());
}
}
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());
}
}
Aggregations