Search in sources :

Example 6 with BatchedQueryResult

use of io.confluent.ksql.api.client.BatchedQueryResult in project ksql by confluentinc.

the class ValidateMigrationsCommandTest method givenAppliedMigrations.

/**
 * @param versions versions, in the order they were applied
 * @param checksums corresponding checksums (ordered according to {@code versions})
 * @param states corresponding migration states (ordered according to {@code versions})
 */
private void givenAppliedMigrations(final List<String> versions, final List<String> checksums, final List<MigrationState> states) throws Exception {
    String version = versions.size() > 0 ? versions.get(versions.size() - 1) : MetadataUtil.NONE_VERSION;
    Row row = mock(Row.class);
    BatchedQueryResult queryResult = mock(BatchedQueryResult.class);
    when(ksqlClient.executeQuery("SELECT VERSION FROM " + MIGRATIONS_TABLE + " WHERE version_key = '" + CURRENT_VERSION_KEY + "';")).thenReturn(queryResult);
    when(queryResult.get()).thenReturn(ImmutableList.of(row));
    when(row.getString("VERSION")).thenReturn(version);
    for (int i = versions.size() - 1; i >= 0; i--) {
        version = versions.get(i);
        String prevVersion = i > 0 ? versions.get(i - 1) : MetadataUtil.NONE_VERSION;
        row = mock(Row.class);
        queryResult = mock(BatchedQueryResult.class);
        when(ksqlClient.executeQuery("SELECT version, checksum, previous, state, name, started_on, completed_on, error_reason FROM " + MIGRATIONS_TABLE + " WHERE version_key = '" + version + "';")).thenReturn(queryResult);
        when(queryResult.get()).thenReturn(ImmutableList.of(row));
        when(row.getString(1)).thenReturn(version);
        when(row.getString(2)).thenReturn(checksums.get(i));
        when(row.getString(3)).thenReturn(prevVersion);
        when(row.getString(4)).thenReturn(states.get(i).toString());
        when(row.getString(5)).thenReturn("name");
        when(row.getString(6)).thenReturn("N/A");
        when(row.getString(7)).thenReturn("N/A");
        when(row.getString(8)).thenReturn("no_error");
    }
}
Also used : Row(io.confluent.ksql.api.client.Row) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Example 7 with BatchedQueryResult

use of io.confluent.ksql.api.client.BatchedQueryResult in project ksql by confluentinc.

the class MigrationInfoCommandTest method givenCurrentMigrationVersion.

private void givenCurrentMigrationVersion(final String version) throws Exception {
    Row row = mock(Row.class);
    BatchedQueryResult queryResult = mock(BatchedQueryResult.class);
    when(ksqlClient.executeQuery("SELECT VERSION FROM " + MIGRATIONS_TABLE + " WHERE version_key = '" + CURRENT_VERSION_KEY + "';")).thenReturn(queryResult);
    when(queryResult.get()).thenReturn(ImmutableList.of(row));
    when(row.getString("VERSION")).thenReturn(version);
}
Also used : Row(io.confluent.ksql.api.client.Row) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Example 8 with BatchedQueryResult

use of io.confluent.ksql.api.client.BatchedQueryResult in project ksql by confluentinc.

the class MetadataUtil method getCurrentVersion.

public static String getCurrentVersion(final MigrationConfig config, final Client client) {
    final String migrationTableName = config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_NAME);
    final BatchedQueryResult result = client.executeQuery("SELECT VERSION FROM " + migrationTableName + " WHERE version_key = '" + CURRENT_VERSION_KEY + "';");
    try {
        final List<Row> resultRows = result.get();
        if (resultRows.size() == 0) {
            return NONE_VERSION;
        }
        return resultRows.get(0).getString("VERSION");
    } catch (InterruptedException | ExecutionException e) {
        throw new MigrationException(String.format("Could not query %s: %s", migrationTableName, e.getMessage()));
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Row(io.confluent.ksql.api.client.Row) ExecutionException(java.util.concurrent.ExecutionException) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Example 9 with BatchedQueryResult

use of io.confluent.ksql.api.client.BatchedQueryResult 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 10 with BatchedQueryResult

use of io.confluent.ksql.api.client.BatchedQueryResult in project ksql by confluentinc.

the class MetadataUtil method getOptionalInfoForVersion.

public static Optional<MigrationVersionInfo> getOptionalInfoForVersion(final String version, final MigrationConfig config, final Client ksqlClient) {
    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 = '" + version + "';");
    final Row resultRow;
    try {
        final List<Row> resultRows = result.get();
        if (resultRows.size() == 0) {
            return Optional.empty();
        }
        resultRow = resultRows.get(0);
    } catch (InterruptedException | ExecutionException e) {
        throw new MigrationException(String.format("Failed to query state for migration with version %s: %s", version, e.getMessage()));
    }
    return Optional.of(MigrationVersionInfo.fromResultRow(resultRow));
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Row(io.confluent.ksql.api.client.Row) ExecutionException(java.util.concurrent.ExecutionException) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Aggregations

BatchedQueryResult (io.confluent.ksql.api.client.BatchedQueryResult)16 Test (org.junit.Test)9 Row (io.confluent.ksql.api.client.Row)8 IntegrationTest (io.confluent.common.utils.IntegrationTest)7 ExecutionException (java.util.concurrent.ExecutionException)5 MigrationException (io.confluent.ksql.tools.migrations.MigrationException)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 KsqlArray (io.confluent.ksql.api.client.KsqlArray)2 KsqlObject (io.confluent.ksql.api.client.KsqlObject)2 KsqlClientException (io.confluent.ksql.api.client.exception.KsqlClientException)2 List (java.util.List)2 ZooKeeperClientException (kafka.zookeeper.ZooKeeperClientException)2 ImmutableList (com.google.common.collect.ImmutableList)1 Client (io.confluent.ksql.api.client.Client)1 ServerInfo (io.confluent.ksql.api.client.ServerInfo)1 ClientImpl (io.confluent.ksql.api.client.impl.ClientImpl)1 ConnectorList (io.confluent.ksql.rest.entity.ConnectorList)1 StreamedRow (io.confluent.ksql.rest.entity.StreamedRow)1 MigrationConfig (io.confluent.ksql.tools.migrations.MigrationConfig)1 ServerVersionUtil.getServerInfo (io.confluent.ksql.tools.migrations.util.ServerVersionUtil.getServerInfo)1