Search in sources :

Example 1 with MigrationException

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

the class ApplyMigrationCommand method apply.

private boolean apply(final MigrationConfig config, final Client ksqlClient, final String migrationsDir, final Clock clock) {
    String previous = MetadataUtil.getLatestMigratedVersion(config, ksqlClient);
    LOGGER.info("Loading migration files");
    final List<MigrationFile> migrations;
    try {
        migrations = loadMigrationsToApply(migrationsDir, previous);
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        return false;
    }
    if (migrations.size() == 0) {
        LOGGER.info("No eligible migrations found.");
    } else {
        LOGGER.info(migrations.size() + " migration file(s) loaded.");
    }
    for (MigrationFile migration : migrations) {
        if (!applyMigration(config, ksqlClient, migration, clock, previous)) {
            return false;
        }
        previous = Integer.toString(migration.getVersion());
    }
    return true;
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) MigrationFile(io.confluent.ksql.tools.migrations.util.MigrationFile)

Example 2 with MigrationException

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

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

the class DestroyMigrationsCommand method dropSource.

private static void dropSource(final Client ksqlClient, final String sourceName, final boolean isTable) {
    final String sourceType = isTable ? "table" : "stream";
    LOGGER.info("Dropping migrations metadata {}: {}", sourceType, sourceName);
    try {
        final String sql = String.format("DROP %s %s DELETE TOPIC;", sourceType.toUpperCase(), sourceName);
        ksqlClient.executeStatement(sql).get();
    } catch (InterruptedException | ExecutionException e) {
        throw new MigrationException(String.format("Failed to drop metadata %s '%s': %s", sourceType, sourceName, e.getMessage()));
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with MigrationException

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

the class DestroyMigrationsCommand method deleteMigrationsTable.

private boolean deleteMigrationsTable(final Client ksqlClient, final String tableName) {
    try {
        if (!sourceExists(ksqlClient, tableName, true)) {
            LOGGER.info("Metadata table does not exist. Skipping cleanup.");
            return true;
        }
        final SourceDescription tableInfo = getSourceInfo(ksqlClient, tableName, true);
        terminateQueryForTable(ksqlClient, tableInfo);
        dropSource(ksqlClient, tableName, true);
        return true;
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        return false;
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) SourceDescription(io.confluent.ksql.api.client.SourceDescription)

Example 5 with MigrationException

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

the class DestroyMigrationsCommand method terminateQueryForTable.

private static void terminateQueryForTable(final Client ksqlClient, final SourceDescription tableDesc) {
    final List<QueryInfo> queries = tableDesc.writeQueries();
    if (queries.size() == 0) {
        LOGGER.info("Found 0 queries writing to the metadata table");
        return;
    }
    if (queries.size() > 1) {
        throw new MigrationException("Found multiple queries writing to the metadata table. Query IDs: " + queries.stream().map(QueryInfo::getId).collect(Collectors.joining("', '", "'", "'.")));
    }
    final String queryId = queries.get(0).getId();
    LOGGER.info("Found 1 query writing to the metadata table. Query ID: {}", queryId);
    LOGGER.info("Terminating query with ID: {}", queryId);
    try {
        ksqlClient.executeStatement("TERMINATE " + queryId + ";").get();
    } catch (InterruptedException | ExecutionException e) {
        throw new MigrationException(String.format("Failed to terminate query populating metadata table. Query ID: %s. Error: %s", queryId, e.getMessage()));
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) QueryInfo(io.confluent.ksql.api.client.QueryInfo) ExecutionException(java.util.concurrent.ExecutionException)

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