Search in sources :

Example 6 with Client

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;
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Client(io.confluent.ksql.api.client.Client) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with Client

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

Example 8 with Client

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;
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Client(io.confluent.ksql.api.client.Client) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Client (io.confluent.ksql.api.client.Client)8 MigrationException (io.confluent.ksql.tools.migrations.MigrationException)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 KsqlObject (io.confluent.ksql.api.client.KsqlObject)2 MigrationConfig (io.confluent.ksql.tools.migrations.MigrationConfig)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 ExecutionException (java.util.concurrent.ExecutionException)2 Collectors (java.util.stream.Collectors)2 Command (com.github.rvesse.airline.annotations.Command)1 Option (com.github.rvesse.airline.annotations.Option)1 Once (com.github.rvesse.airline.annotations.restrictions.Once)1 RequireOnlyOne (com.github.rvesse.airline.annotations.restrictions.RequireOnlyOne)1 IntegerRange (com.github.rvesse.airline.annotations.restrictions.ranges.IntegerRange)1 ImmutableList (com.google.common.collect.ImmutableList)1 BatchedQueryResult (io.confluent.ksql.api.client.BatchedQueryResult)1 FieldInfo (io.confluent.ksql.api.client.FieldInfo)1 KsqlArray (io.confluent.ksql.api.client.KsqlArray)1 Row (io.confluent.ksql.api.client.Row)1