Search in sources :

Example 1 with Client

use of io.confluent.ksql.api.client.Client in project ksql by confluentinc.

the class ApplyMigrationCommand method command.

// CHECKSTYLE_RULES.OFF: NPathComplexity
@VisibleForTesting
int command(final MigrationConfig config, final BiFunction<MigrationConfig, String, Client> clientSupplier, final String migrationsDir, final Clock clock) {
    // CHECKSTYLE_RULES.ON: NPathComplexity
    final Client ksqlClient;
    try {
        ksqlClient = clientSupplier.apply(config, headersFile);
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        return 1;
    }
    if (!validateMetadataInitialized(ksqlClient, config)) {
        ksqlClient.close();
        return 1;
    }
    if (dryRun) {
        LOGGER.info("This is a dry run. No ksqlDB statements will be submitted " + "to the ksqlDB server.");
    }
    boolean success;
    try {
        success = validateCurrentState(config, ksqlClient, migrationsDir) && apply(config, ksqlClient, migrationsDir, clock);
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        success = false;
    } finally {
        ksqlClient.close();
    }
    return success ? 0 : 1;
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Client(io.confluent.ksql.api.client.Client) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with Client

use of io.confluent.ksql.api.client.Client in project ksql by confluentinc.

the class DestroyMigrationsCommand method command.

@VisibleForTesting
int command(final MigrationConfig config, final Function<MigrationConfig, Client> clientSupplier) {
    final String streamName = config.getString(MigrationConfig.KSQL_MIGRATIONS_STREAM_NAME);
    final String tableName = config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_NAME);
    final Client ksqlClient;
    try {
        ksqlClient = clientSupplier.apply(config);
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        return 1;
    }
    LOGGER.info("Cleaning migrations metadata stream and table from ksqlDB server");
    if (ServerVersionUtil.serverVersionCompatible(ksqlClient, config) && deleteMigrationsTable(ksqlClient, tableName) && deleteMigrationsStream(ksqlClient, streamName)) {
        LOGGER.info("Migrations metadata cleaned successfully");
        ksqlClient.close();
        return 0;
    } else {
        ksqlClient.close();
        return 1;
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Client(io.confluent.ksql.api.client.Client) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with Client

use of io.confluent.ksql.api.client.Client in project ksql by confluentinc.

the class MigrationInfoCommand method command.

@VisibleForTesting
int command(final MigrationConfig config, final Function<MigrationConfig, Client> clientSupplier, final String migrationsDir) {
    final Client ksqlClient;
    try {
        ksqlClient = clientSupplier.apply(config);
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        return 1;
    }
    if (!validateMetadataInitialized(ksqlClient, config)) {
        ksqlClient.close();
        return 1;
    }
    boolean success;
    try {
        printCurrentVersion(config, ksqlClient);
        printVersionInfoTable(config, ksqlClient, migrationsDir);
        success = true;
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        success = false;
    } finally {
        ksqlClient.close();
    }
    return success ? 0 : 1;
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Client(io.confluent.ksql.api.client.Client) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with Client

use of io.confluent.ksql.api.client.Client in project ksql by confluentinc.

the class MigrationConfig method getServiceId.

private static String getServiceId(final Map<String, String> configs) throws MigrationException {
    final String ksqlServerUrl = configs.get(KSQL_SERVER_URL);
    if (ksqlServerUrl == null) {
        throw new MigrationException("Missing required property: " + MigrationConfig.KSQL_SERVER_URL);
    }
    final Client ksqlClient = MigrationsUtil.getKsqlClient(ksqlServerUrl, configs.get(KSQL_BASIC_AUTH_USERNAME), configs.get(KSQL_BASIC_AUTH_PASSWORD), configs.get(SSL_TRUSTSTORE_LOCATION), configs.get(SSL_TRUSTSTORE_PASSWORD), configs.get(SSL_KEYSTORE_LOCATION), configs.get(SSL_KEYSTORE_PASSWORD), configs.get(SSL_KEY_PASSWORD), configs.get(SSL_KEY_ALIAS), configs.getOrDefault(SSL_ALPN, "false").equalsIgnoreCase("true"), configs.getOrDefault(SSL_VERIFY_HOST, "true").equalsIgnoreCase("true"), null);
    final String serviceId;
    try {
        serviceId = ServerVersionUtil.getServerInfo(ksqlClient, ksqlServerUrl).getKsqlServiceId();
        ksqlClient.close();
        return serviceId;
    } catch (MigrationException e) {
        ksqlClient.close();
        throw e;
    }
}
Also used : Client(io.confluent.ksql.api.client.Client)

Example 5 with Client

use of io.confluent.ksql.api.client.Client in project ksql by confluentinc.

the class ApplyMigrationCommand method executeCommands.

/**
 * If validateOnly is set to true, then this parses each of the commands but only executes
 * DEFINE/UNDEFINE commands (variables are needed for parsing INSERT INTO... VALUES, SET/UNSET
 * and DEFINE commands). If validateOnly is set to false, then each command will execute after
 * parsing.
 */
private void executeCommands(final List<String> commands, final Client ksqlClient, final MigrationConfig config, final String executionStart, final MigrationFile migration, final Clock clock, final String previous, final boolean validateOnly) {
    setUpJavaClientVariables(ksqlClient);
    final Map<String, Object> properties = new HashMap<>();
    for (final String command : commands) {
        try {
            final Map<String, String> variables = ksqlClient.getVariables().entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString()));
            executeCommand(CommandParser.transformToSqlCommand(command, variables), ksqlClient, properties, validateOnly);
        } catch (InterruptedException | ExecutionException | MigrationException e) {
            final String action = validateOnly ? "parse" : "execute";
            final String errorMsg = String.format("Failed to %s sql: %s. Error: %s", action, command, e.getMessage());
            updateState(config, ksqlClient, MigrationState.ERROR, executionStart, migration, clock, previous, Optional.of(errorMsg));
            throw new MigrationException(errorMsg);
        }
    }
}
Also used : Option(com.github.rvesse.airline.annotations.Option) MetadataUtil(io.confluent.ksql.tools.migrations.util.MetadataUtil) MigrationState(io.confluent.ksql.tools.migrations.util.MetadataUtil.MigrationState) FieldInfo(io.confluent.ksql.api.client.FieldInfo) MigrationFile(io.confluent.ksql.tools.migrations.util.MigrationFile) MigrationConfig(io.confluent.ksql.tools.migrations.MigrationConfig) IntegerRange(com.github.rvesse.airline.annotations.restrictions.ranges.IntegerRange) SqlCreateConnectorStatement(io.confluent.ksql.tools.migrations.util.CommandParser.SqlCreateConnectorStatement) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) CommandParser(io.confluent.ksql.tools.migrations.util.CommandParser) HashMap(java.util.HashMap) RequireOnlyOne(com.github.rvesse.airline.annotations.restrictions.RequireOnlyOne) SqlCommand(io.confluent.ksql.tools.migrations.util.CommandParser.SqlCommand) MigrationsDirectoryUtil.getAllMigrations(io.confluent.ksql.tools.migrations.util.MigrationsDirectoryUtil.getAllMigrations) SqlInsertValues(io.confluent.ksql.tools.migrations.util.CommandParser.SqlInsertValues) Map(java.util.Map) Once(com.github.rvesse.airline.annotations.restrictions.Once) MigrationsDirectoryUtil.getMigrationsDirFromConfigFile(io.confluent.ksql.tools.migrations.util.MigrationsDirectoryUtil.getMigrationsDirFromConfigFile) SqlDropConnectorStatement(io.confluent.ksql.tools.migrations.util.CommandParser.SqlDropConnectorStatement) Command(com.github.rvesse.airline.annotations.Command) SqlUndefineVariableCommand(io.confluent.ksql.tools.migrations.util.CommandParser.SqlUndefineVariableCommand) SqlDefineVariableCommand(io.confluent.ksql.tools.migrations.util.CommandParser.SqlDefineVariableCommand) VariableParser(io.confluent.ksql.parser.VariableParser) MigrationsDirectoryUtil(io.confluent.ksql.tools.migrations.util.MigrationsDirectoryUtil) Logger(org.slf4j.Logger) Expression(io.confluent.ksql.execution.expression.tree.Expression) MigrationsUtil(io.confluent.ksql.tools.migrations.util.MigrationsUtil) KsqlObject(io.confluent.ksql.api.client.KsqlObject) MigrationsDirectoryUtil.getMigrationForVersion(io.confluent.ksql.tools.migrations.util.MigrationsDirectoryUtil.getMigrationForVersion) RetryUtil(io.confluent.ksql.util.RetryUtil) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) SqlPropertyCommand(io.confluent.ksql.tools.migrations.util.CommandParser.SqlPropertyCommand) MigrationException(io.confluent.ksql.tools.migrations.MigrationException) SqlStatement(io.confluent.ksql.tools.migrations.util.CommandParser.SqlStatement) KsqlException(io.confluent.ksql.util.KsqlException) Clock(java.time.Clock) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Client(io.confluent.ksql.api.client.Client) CommandParser.preserveCase(io.confluent.ksql.tools.migrations.util.CommandParser.preserveCase) Collections(java.util.Collections) MigrationException(io.confluent.ksql.tools.migrations.MigrationException) HashMap(java.util.HashMap) KsqlObject(io.confluent.ksql.api.client.KsqlObject) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Client (io.confluent.ksql.api.client.Client)8 MigrationException (io.confluent.ksql.tools.migrations.MigrationException)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 KsqlObject (io.confluent.ksql.api.client.KsqlObject)2 MigrationConfig (io.confluent.ksql.tools.migrations.MigrationConfig)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 ExecutionException (java.util.concurrent.ExecutionException)2 Collectors (java.util.stream.Collectors)2 Command (com.github.rvesse.airline.annotations.Command)1 Option (com.github.rvesse.airline.annotations.Option)1 Once (com.github.rvesse.airline.annotations.restrictions.Once)1 RequireOnlyOne (com.github.rvesse.airline.annotations.restrictions.RequireOnlyOne)1 IntegerRange (com.github.rvesse.airline.annotations.restrictions.ranges.IntegerRange)1 ImmutableList (com.google.common.collect.ImmutableList)1 BatchedQueryResult (io.confluent.ksql.api.client.BatchedQueryResult)1 FieldInfo (io.confluent.ksql.api.client.FieldInfo)1 KsqlArray (io.confluent.ksql.api.client.KsqlArray)1 Row (io.confluent.ksql.api.client.Row)1