Search in sources :

Example 16 with MigrationException

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

the class CommandParserTest method shouldThrowOnMissingSemicolon.

@Test
public void shouldThrowOnMissingSemicolon() {
    // When:
    final MigrationException e = assertThrows(MigrationException.class, () -> parse("create stream foo as select * from no_semicolon_after_this"));
    // Then:
    assertThat(e.getMessage(), containsString("Unmatched command at end of file; missing semicolon"));
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Test(org.junit.Test)

Example 17 with MigrationException

use of io.confluent.ksql.tools.migrations.MigrationException 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 18 with MigrationException

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

the class ValidateMigrationsCommand 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 {
        success = validate(config, migrationsDir, ksqlClient);
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        success = false;
    } finally {
        ksqlClient.close();
    }
    if (success) {
        LOGGER.info("Successfully validated checksums for migrations that have already been applied");
        return 0;
    } else {
        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 19 with MigrationException

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

the class MetadataUtil method getCurrentVersion.

public static String getCurrentVersion(final MigrationConfig config, final Client client) {
    final String migrationTableName = config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_NAME);
    final BatchedQueryResult result = client.executeQuery("SELECT VERSION FROM " + migrationTableName + " WHERE version_key = '" + CURRENT_VERSION_KEY + "';");
    try {
        final List<Row> resultRows = result.get();
        if (resultRows.size() == 0) {
            return NONE_VERSION;
        }
        return resultRows.get(0).getString("VERSION");
    } catch (InterruptedException | ExecutionException e) {
        throw new MigrationException(String.format("Could not query %s: %s", migrationTableName, e.getMessage()));
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Row(io.confluent.ksql.api.client.Row) ExecutionException(java.util.concurrent.ExecutionException) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Example 20 with MigrationException

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

MigrationException (io.confluent.ksql.tools.migrations.MigrationException)34 Test (org.junit.Test)11 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7 Client (io.confluent.ksql.api.client.Client)7 ExecutionException (java.util.concurrent.ExecutionException)6 IOException (java.io.IOException)4 List (java.util.List)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 BatchedQueryResult (io.confluent.ksql.api.client.BatchedQueryResult)3 Row (io.confluent.ksql.api.client.Row)3 MigrationFile (io.confluent.ksql.tools.migrations.util.MigrationFile)3 File (java.io.File)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 Matcher (java.util.regex.Matcher)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 KsqlObject (io.confluent.ksql.api.client.KsqlObject)2 ServerInfo (io.confluent.ksql.api.client.ServerInfo)2 AstBuilder (io.confluent.ksql.parser.AstBuilder)2