use of io.confluent.ksql.api.client.Client 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.api.client.Client in project ksql by confluentinc.
the class DestroyMigrationsCommand 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 Client ksqlClient;
try {
ksqlClient = clientSupplier.apply(config);
} catch (MigrationException e) {
LOGGER.error(e.getMessage());
return 1;
}
LOGGER.info("Cleaning migrations metadata stream and table from ksqlDB server");
if (ServerVersionUtil.serverVersionCompatible(ksqlClient, config) && deleteMigrationsTable(ksqlClient, tableName) && deleteMigrationsStream(ksqlClient, streamName)) {
LOGGER.info("Migrations metadata cleaned successfully");
ksqlClient.close();
return 0;
} else {
ksqlClient.close();
return 1;
}
}
use of io.confluent.ksql.api.client.Client in project ksql by confluentinc.
the class MigrationInfoCommand 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 {
printCurrentVersion(config, ksqlClient);
printVersionInfoTable(config, ksqlClient, migrationsDir);
success = true;
} catch (MigrationException e) {
LOGGER.error(e.getMessage());
success = false;
} finally {
ksqlClient.close();
}
return success ? 0 : 1;
}
use of io.confluent.ksql.api.client.Client in project ksql by confluentinc.
the class MigrationConfig method getServiceId.
private static String getServiceId(final Map<String, String> configs) throws MigrationException {
final String ksqlServerUrl = configs.get(KSQL_SERVER_URL);
if (ksqlServerUrl == null) {
throw new MigrationException("Missing required property: " + MigrationConfig.KSQL_SERVER_URL);
}
final Client ksqlClient = MigrationsUtil.getKsqlClient(ksqlServerUrl, configs.get(KSQL_BASIC_AUTH_USERNAME), configs.get(KSQL_BASIC_AUTH_PASSWORD), configs.get(SSL_TRUSTSTORE_LOCATION), configs.get(SSL_TRUSTSTORE_PASSWORD), configs.get(SSL_KEYSTORE_LOCATION), configs.get(SSL_KEYSTORE_PASSWORD), configs.get(SSL_KEY_PASSWORD), configs.get(SSL_KEY_ALIAS), configs.getOrDefault(SSL_ALPN, "false").equalsIgnoreCase("true"), configs.getOrDefault(SSL_VERIFY_HOST, "true").equalsIgnoreCase("true"), null);
final String serviceId;
try {
serviceId = ServerVersionUtil.getServerInfo(ksqlClient, ksqlServerUrl).getKsqlServiceId();
ksqlClient.close();
return serviceId;
} catch (MigrationException e) {
ksqlClient.close();
throw e;
}
}
use of io.confluent.ksql.api.client.Client 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);
}
}
}
Aggregations