Search in sources :

Example 1 with MigrationConfig

use of io.confluent.ksql.tools.migrations.MigrationConfig 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)

Example 2 with MigrationConfig

use of io.confluent.ksql.tools.migrations.MigrationConfig in project ksql by confluentinc.

the class MetadataUtil method getOptionalInfoForVersions.

public static Map<Integer, Optional<MigrationVersionInfo>> getOptionalInfoForVersions(final List<Integer> versions, final MigrationConfig config, final Client ksqlClient) {
    if (serverSupportsMultiKeyPullQuery(ksqlClient, config)) {
        // issue a single, multi-key pull query
        final String migrationTableName = config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_NAME);
        final BatchedQueryResult result = ksqlClient.executeQuery("SELECT version, checksum, previous, state, name, started_on, completed_on, error_reason " + "FROM " + migrationTableName + " WHERE version_key IN ('" + versions.stream().map(String::valueOf).collect(Collectors.joining("', '")) + "');");
        final Map<Integer, MigrationVersionInfo> resultSet;
        try {
            resultSet = result.get().stream().map(MigrationVersionInfo::fromResultRow).collect(Collectors.toMap(MigrationVersionInfo::getVersion, vInfo -> vInfo));
        } catch (InterruptedException | ExecutionException e) {
            throw new MigrationException(String.format("Failed to query state for migration with versions %s: %s", versions, e.getMessage()));
        }
        return versions.stream().collect(Collectors.toMap(v -> v, v -> Optional.ofNullable(resultSet.get(v))));
    } else {
        // issue multiple, single-key pull queries
        return versions.stream().collect(Collectors.toMap(v -> v, v -> getOptionalInfoForVersion(String.valueOf(v), config, ksqlClient)));
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Row(io.confluent.ksql.api.client.Row) MigrationConfig(io.confluent.ksql.tools.migrations.MigrationConfig) ServerVersionUtil.versionSupportsMultiKeyPullQuery(io.confluent.ksql.tools.migrations.util.ServerVersionUtil.versionSupportsMultiKeyPullQuery) KsqlObject(io.confluent.ksql.api.client.KsqlObject) CompletableFuture(java.util.concurrent.CompletableFuture) ServerInfo(io.confluent.ksql.api.client.ServerInfo) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Map(java.util.Map) Optional(java.util.Optional) KsqlArray(io.confluent.ksql.api.client.KsqlArray) Client(io.confluent.ksql.api.client.Client) ServerVersionUtil.getServerInfo(io.confluent.ksql.tools.migrations.util.ServerVersionUtil.getServerInfo) ExecutionException(java.util.concurrent.ExecutionException) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Aggregations

Client (io.confluent.ksql.api.client.Client)2 KsqlObject (io.confluent.ksql.api.client.KsqlObject)2 MigrationConfig (io.confluent.ksql.tools.migrations.MigrationConfig)2 MigrationException (io.confluent.ksql.tools.migrations.MigrationException)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 VisibleForTesting (com.google.common.annotations.VisibleForTesting)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