use of io.confluent.ksql.api.client.Client 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;
}
}
use of io.confluent.ksql.api.client.Client 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)));
}
}
use of io.confluent.ksql.api.client.Client in project ksql by confluentinc.
the class InitializeMigrationCommand 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 String eventStreamCommand = createEventStream(streamName, config.getString(MigrationConfig.KSQL_MIGRATIONS_STREAM_TOPIC_NAME), config.getInt(MigrationConfig.KSQL_MIGRATIONS_TOPIC_REPLICAS));
final String versionTableCommand = createVersionTable(tableName, streamName, config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_TOPIC_NAME));
final Client ksqlClient;
try {
ksqlClient = clientSupplier.apply(config);
} catch (MigrationException e) {
LOGGER.error(e.getMessage());
return 1;
}
LOGGER.info("Initializing migrations metadata");
if (ServerVersionUtil.serverVersionCompatible(ksqlClient, config) && tryCreate(ksqlClient, eventStreamCommand, streamName, true) && tryCreate(ksqlClient, versionTableCommand, tableName, false)) {
LOGGER.info("Migrations metadata initialized successfully");
ksqlClient.close();
} else {
ksqlClient.close();
return 1;
}
return 0;
}
Aggregations