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;
}
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;
}
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()));
}
}
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;
}
}
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()));
}
}
Aggregations