Search in sources :

Example 6 with SessionProperties

use of io.confluent.ksql.rest.SessionProperties in project ksql by confluentinc.

the class PropertyExecutorTest method shouldUnSetProperty.

@Test
public void shouldUnSetProperty() {
    // Given:
    engine.givenSource(DataSourceType.KSTREAM, "stream");
    final SessionProperties sessionProperties = new SessionProperties(Collections.singletonMap(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "none"), mock(KsqlHostInfo.class), mock(URL.class), false);
    final Map<String, Object> properties = sessionProperties.getMutableScopedProperties();
    // When:
    CUSTOM_EXECUTORS.unsetProperty().execute((ConfiguredStatement<UnsetProperty>) engine.configure("UNSET '" + ConsumerConfig.AUTO_OFFSET_RESET_CONFIG + "';"), sessionProperties, engine.getEngine(), engine.getServiceContext());
    // Then:
    assertThat(properties, not(hasKey(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG)));
}
Also used : SessionProperties(io.confluent.ksql.rest.SessionProperties) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) UnsetProperty(io.confluent.ksql.parser.tree.UnsetProperty) URL(java.net.URL) Test(org.junit.Test)

Example 7 with SessionProperties

use of io.confluent.ksql.rest.SessionProperties in project ksql by confluentinc.

the class PropertyOverriderTest method shouldFailOnUnknownUnsetProperty.

@Test
public void shouldFailOnUnknownUnsetProperty() {
    // Given:
    final Map<String, Object> properties = new HashMap<>();
    final SessionProperties sessionProperties = new SessionProperties(properties, mock(KsqlHostInfo.class), mock(URL.class), false);
    // When:
    final Exception e = assertThrows(KsqlStatementException.class, () -> CustomValidators.UNSET_PROPERTY.validate(ConfiguredStatement.of(PreparedStatement.of("UNSET 'consumer.invalid';", new UnsetProperty(Optional.empty(), "consumer.invalid")), SessionConfig.of(engine.getKsqlConfig(), new HashMap<>())), sessionProperties, engine.getEngine(), engine.getServiceContext()));
    // Then:
    assertThat(e.getMessage(), containsString("Unknown property: consumer.invalid"));
}
Also used : SessionProperties(io.confluent.ksql.rest.SessionProperties) HashMap(java.util.HashMap) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) UnsetProperty(io.confluent.ksql.parser.tree.UnsetProperty) Matchers.containsString(org.hamcrest.Matchers.containsString) URL(java.net.URL) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) Test(org.junit.Test)

Example 8 with SessionProperties

use of io.confluent.ksql.rest.SessionProperties in project ksql by confluentinc.

the class PropertyOverriderTest method shouldAllowSetKnownProperty.

@Test
public void shouldAllowSetKnownProperty() {
    // Given:
    final SessionProperties sessionProperties = new SessionProperties(new HashedMap<>(), mock(KsqlHostInfo.class), mock(URL.class), false);
    final Map<String, Object> properties = sessionProperties.getMutableScopedProperties();
    // When:
    CustomValidators.SET_PROPERTY.validate(ConfiguredStatement.of(PreparedStatement.of("SET '" + ConsumerConfig.AUTO_OFFSET_RESET_CONFIG + "' = 'earliest';", new SetProperty(Optional.empty(), ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")), SessionConfig.of(engine.getKsqlConfig(), ImmutableMap.of())), sessionProperties, engine.getEngine(), engine.getServiceContext());
    // Then:
    assertThat(properties, hasEntry(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"));
}
Also used : SessionProperties(io.confluent.ksql.rest.SessionProperties) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) Matchers.containsString(org.hamcrest.Matchers.containsString) SetProperty(io.confluent.ksql.parser.tree.SetProperty) URL(java.net.URL) Test(org.junit.Test)

Example 9 with SessionProperties

use of io.confluent.ksql.rest.SessionProperties in project ksql by confluentinc.

the class TestExecutorUtil method execute.

/**
 * @param srClient if supplied, then schemas can be inferred from the schema registry.
 * @return a list of persistent queries that should be run by the test executor, if a query was
 *         replaced via a CREATE OR REPLACE statement it will only appear once in the output list
 */
@SuppressWarnings("OptionalGetWithoutIsPresent")
private static List<PersistentQueryAndSources> execute(final KsqlEngine engine, final TestCase testCase, final KsqlConfig ksqlConfig, final ServiceContext serviceContext, final Optional<SchemaRegistryClient> srClient, final StubKafkaService stubKafkaService, final TestExecutionListener listener) {
    final Map<QueryId, PersistentQueryAndSources> queries = new LinkedHashMap<>();
    int idx = 0;
    final Iterator<PlannedStatement> plans = planTestCase(engine, testCase, ksqlConfig, serviceContext, srClient, stubKafkaService);
    try {
        while (plans.hasNext()) {
            ++idx;
            final PlannedStatement planned = plans.next();
            if (planned.insertValues.isPresent()) {
                final ConfiguredStatement<InsertValues> insertValues = planned.insertValues.get();
                final SessionProperties sessionProperties = new SessionProperties(insertValues.getSessionConfig().getOverrides(), new KsqlHostInfo("host", 50), buildUrl(), false);
                StubInsertValuesExecutor.of(stubKafkaService).execute(insertValues, sessionProperties, engine, engine.getServiceContext());
                continue;
            }
            final ConfiguredKsqlPlan plan = planned.plan.orElseThrow(IllegalStateException::new);
            listener.acceptPlan(plan);
            final ExecuteResultAndSources result = executePlan(engine, plan);
            if (!result.getSources().isPresent()) {
                continue;
            }
            final PersistentQueryMetadata query = (PersistentQueryMetadata) result.getExecuteResult().getQuery().get();
            listener.acceptQuery(query);
            queries.put(query.getQueryId(), new PersistentQueryAndSources(query, result.getSources().get()));
        }
        return ImmutableList.copyOf(queries.values());
    } catch (final KsqlStatementException e) {
        if (testCase.expectedException().isPresent() && plans.hasNext()) {
            throw new AssertionError("Only the last statement in a negative test should fail. " + "Yet in this case statement " + idx + " failed.", e);
        }
        throw e;
    }
}
Also used : ConfiguredKsqlPlan(io.confluent.ksql.planner.plan.ConfiguredKsqlPlan) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) QueryId(io.confluent.ksql.query.QueryId) LinkedHashMap(java.util.LinkedHashMap) SessionProperties(io.confluent.ksql.rest.SessionProperties) InsertValues(io.confluent.ksql.parser.tree.InsertValues) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 10 with SessionProperties

use of io.confluent.ksql.rest.SessionProperties in project ksql by confluentinc.

the class KsqlResource method handleKsqlStatements.

public EndpointResponse handleKsqlStatements(final KsqlSecurityContext securityContext, final KsqlRequest request) {
    LOG.info("Received: " + request);
    throwIfNotConfigured();
    activenessRegistrar.updateLastRequestTime();
    try {
        CommandStoreUtil.httpWaitForCommandSequenceNumber(commandRunner.getCommandQueue(), request, distributedCmdResponseTimeout);
        final Map<String, Object> configProperties = request.getConfigOverrides();
        denyListPropertyValidator.validateAll(configProperties);
        final KsqlRequestConfig requestConfig = new KsqlRequestConfig(request.getRequestProperties());
        final List<ParsedStatement> statements = ksqlEngine.parse(request.getKsql());
        validator.validate(SandboxedServiceContext.create(securityContext.getServiceContext()), statements, new SessionProperties(configProperties, localHost, localUrl, requestConfig.getBoolean(KsqlRequestConfig.KSQL_REQUEST_INTERNAL_REQUEST), request.getSessionVariables()), request.getKsql());
        // log validated statements for query anonymization
        statements.forEach(s -> {
            if (s.getStatementText().toLowerCase().contains("terminate") || s.getStatementText().toLowerCase().contains("drop")) {
                QueryLogger.info("Query terminated", s.getStatementText());
            } else {
                QueryLogger.info("Query created", s.getStatementText());
            }
        });
        final KsqlEntityList entities = handler.execute(securityContext, statements, new SessionProperties(configProperties, localHost, localUrl, requestConfig.getBoolean(KsqlRequestConfig.KSQL_REQUEST_INTERNAL_REQUEST), request.getSessionVariables()));
        LOG.info("Processed successfully: " + request);
        addCommandRunnerWarning(entities, commandRunnerWarning);
        return EndpointResponse.ok(entities);
    } catch (final KsqlRestException e) {
        LOG.info("Processed unsuccessfully: " + request + ", reason: ", e);
        throw e;
    } catch (final KsqlStatementException e) {
        LOG.info("Processed unsuccessfully: " + request + ", reason: ", e);
        return Errors.badStatement(e.getRawMessage(), e.getSqlStatement());
    } catch (final KsqlException e) {
        LOG.info("Processed unsuccessfully: " + request + ", reason: ", e);
        return errorHandler.generateResponse(e, Errors.badRequest(e));
    } catch (final Exception e) {
        LOG.info("Processed unsuccessfully: " + request + ", reason: ", e);
        return errorHandler.generateResponse(e, Errors.serverErrorForStatement(e, request.getKsql()));
    }
}
Also used : KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) SessionProperties(io.confluent.ksql.rest.SessionProperties) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlRequestConfig(io.confluent.ksql.util.KsqlRequestConfig) KsqlException(io.confluent.ksql.util.KsqlException) PatternSyntaxException(java.util.regex.PatternSyntaxException) KsqlException(io.confluent.ksql.util.KsqlException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException)

Aggregations

SessionProperties (io.confluent.ksql.rest.SessionProperties)15 KsqlHostInfo (io.confluent.ksql.util.KsqlHostInfo)7 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)6 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)6 URL (java.net.URL)6 List (java.util.List)6 Optional (java.util.Optional)6 Test (org.junit.Test)6 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)5 ServiceContext (io.confluent.ksql.services.ServiceContext)4 Map (java.util.Map)4 SetProperty (io.confluent.ksql.parser.tree.SetProperty)3 UnsetProperty (io.confluent.ksql.parser.tree.UnsetProperty)3 KsqlWarning (io.confluent.ksql.rest.entity.KsqlWarning)3 Collectors (java.util.stream.Collectors)3 ImmutableList (com.google.common.collect.ImmutableList)2 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)2 QueryId (io.confluent.ksql.query.QueryId)2 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)2 KsqlEntityList (io.confluent.ksql.rest.entity.KsqlEntityList)2