Search in sources :

Example 1 with SuccessResult

use of com.datastax.oss.simulacron.common.result.SuccessResult in project dsbulk by datastax.

the class JsonEndToEndSimulacronIT method load_errors.

@Test
void load_errors() throws Exception {
    primeIpByCountryTable(simulacron);
    Map<String, Object> params = new HashMap<>();
    params.put("country_name", "Sweden");
    RequestPrime prime1 = createParameterizedQuery(INSERT_INTO_IP_BY_COUNTRY, params, new SuccessResult(emptyList(), new LinkedHashMap<>()));
    simulacron.prime(new Prime(prime1));
    // recoverable errors only
    params.put("country_name", "France");
    prime1 = createParameterizedQuery(INSERT_INTO_IP_BY_COUNTRY, params, new ReadTimeoutResult(LOCAL_ONE, 1, 0, false));
    simulacron.prime(new Prime(prime1));
    params.put("country_name", "Gregistan");
    prime1 = createParameterizedQuery(INSERT_INTO_IP_BY_COUNTRY, params, new WriteTimeoutResult(LOCAL_ONE, 0, 0, WriteType.BATCH_LOG));
    simulacron.prime(new Prime(prime1));
    params.put("country_name", "Andybaijan");
    prime1 = createParameterizedQuery(INSERT_INTO_IP_BY_COUNTRY, params, new WriteFailureResult(ONE, 0, 0, emptyMap(), WriteType.BATCH));
    simulacron.prime(new Prime(prime1));
    params = new HashMap<>();
    params.put("country_name", "United States");
    prime1 = createParameterizedQuery(INSERT_INTO_IP_BY_COUNTRY, params, new FunctionFailureResult("keyspace", "function", emptyList(), "bad function call"));
    simulacron.prime(new Prime(prime1));
    String[] args = { "load", "-c", "json", "--connector.json.url", StringUtils.quoteJson(JsonUtils.JSON_RECORDS_ERROR), "--driver.advanced.retry-policy.max-retries", "1", "--schema.keyspace", "ks1", "--schema.query", INSERT_INTO_IP_BY_COUNTRY, "--schema.mapping", IP_BY_COUNTRY_MAPPING_NAMED };
    ExitStatus status = new DataStaxBulkLoader(addCommonSettings(args)).run();
    assertStatus(status, STATUS_COMPLETED_WITH_ERRORS);
    // There are 24 rows of data, but two extra queries due to the retry for the write timeout and
    // the unavailable.
    validateQueryCount(simulacron, 26, "INSERT INTO ip_by_country", LOCAL_ONE);
    validateNumberOfBadRecords(4);
    validateExceptionsLog(4, "Source:", "load-errors.log");
}
Also used : Prime(com.datastax.oss.simulacron.common.stubbing.Prime) RequestPrime(com.datastax.oss.simulacron.common.cluster.RequestPrime) ReadTimeoutResult(com.datastax.oss.simulacron.common.result.ReadTimeoutResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FunctionFailureResult(com.datastax.oss.simulacron.common.result.FunctionFailureResult) SuccessResult(com.datastax.oss.simulacron.common.result.SuccessResult) DataStaxBulkLoader(com.datastax.oss.dsbulk.runner.DataStaxBulkLoader) LinkedHashMap(java.util.LinkedHashMap) ExitStatus(com.datastax.oss.dsbulk.runner.ExitStatus) WriteFailureResult(com.datastax.oss.simulacron.common.result.WriteFailureResult) RequestPrime(com.datastax.oss.simulacron.common.cluster.RequestPrime) WriteTimeoutResult(com.datastax.oss.simulacron.common.result.WriteTimeoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with SuccessResult

use of com.datastax.oss.simulacron.common.result.SuccessResult in project dsbulk by datastax.

the class CSVEndToEndSimulacronIT method load_errors.

@Test
void load_errors() throws Exception {
    primeIpByCountryTable(simulacron);
    Map<String, Object> params = new HashMap<>();
    params.put("country_name", "Sweden");
    RequestPrime prime1 = createParameterizedQuery(INSERT_INTO_IP_BY_COUNTRY, params, new SuccessResult(emptyList(), new LinkedHashMap<>()));
    simulacron.prime(new Prime(prime1));
    // recoverable errors only
    params.put("country_name", "France");
    prime1 = createParameterizedQuery(INSERT_INTO_IP_BY_COUNTRY, params, new ReadTimeoutResult(LOCAL_ONE, 1, 0, false));
    simulacron.prime(new Prime(prime1));
    params.put("country_name", "Gregistan");
    prime1 = createParameterizedQuery(INSERT_INTO_IP_BY_COUNTRY, params, new WriteTimeoutResult(LOCAL_ONE, 0, 0, WriteType.BATCH_LOG));
    simulacron.prime(new Prime(prime1));
    params.put("country_name", "Andybaijan");
    prime1 = createParameterizedQuery(INSERT_INTO_IP_BY_COUNTRY, params, new WriteFailureResult(ONE, 0, 0, emptyMap(), WriteType.BATCH));
    simulacron.prime(new Prime(prime1));
    params = new HashMap<>();
    params.put("country_name", "United States");
    prime1 = createParameterizedQuery(INSERT_INTO_IP_BY_COUNTRY, params, new FunctionFailureResult("keyspace", "function", emptyList(), "bad function call"));
    simulacron.prime(new Prime(prime1));
    String[] args = { "load", "-header", "false", "--connector.csv.url", StringUtils.quoteJson(CsvUtils.CSV_RECORDS_ERROR), "--driver.advanced.retry-policy.max-retries", "1", "--schema.keyspace", "ks1", "--schema.query", INSERT_INTO_IP_BY_COUNTRY, "--schema.mapping", IP_BY_COUNTRY_MAPPING_INDEXED };
    ExitStatus status = new DataStaxBulkLoader(addCommonSettings(args)).run();
    assertStatus(status, STATUS_COMPLETED_WITH_ERRORS);
    // There are 24 rows of data, but two extra queries due to the retry for the write timeout and
    // the unavailable.
    validateQueryCount(simulacron, 26, "INSERT INTO ip_by_country", LOCAL_ONE);
    validateNumberOfBadRecords(4);
    validateExceptionsLog(4, "Source:", "load-errors.log");
}
Also used : Prime(com.datastax.oss.simulacron.common.stubbing.Prime) RequestPrime(com.datastax.oss.simulacron.common.cluster.RequestPrime) ReadTimeoutResult(com.datastax.oss.simulacron.common.result.ReadTimeoutResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FunctionFailureResult(com.datastax.oss.simulacron.common.result.FunctionFailureResult) SuccessResult(com.datastax.oss.simulacron.common.result.SuccessResult) DataStaxBulkLoader(com.datastax.oss.dsbulk.runner.DataStaxBulkLoader) LinkedHashMap(java.util.LinkedHashMap) ExitStatus(com.datastax.oss.dsbulk.runner.ExitStatus) WriteFailureResult(com.datastax.oss.simulacron.common.result.WriteFailureResult) RequestPrime(com.datastax.oss.simulacron.common.cluster.RequestPrime) WriteTimeoutResult(com.datastax.oss.simulacron.common.result.WriteTimeoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with SuccessResult

use of com.datastax.oss.simulacron.common.result.SuccessResult in project dsbulk by datastax.

the class SimulacronUtils method primeTables.

public static void primeTables(BoundCluster simulacron, Keyspace... keyspaces) {
    List<LinkedHashMap<String, Object>> allKeyspacesRows = new ArrayList<>();
    List<LinkedHashMap<String, Object>> allTablesRows = new ArrayList<>();
    List<LinkedHashMap<String, Object>> allColumnsRows = new ArrayList<>();
    for (Keyspace keyspace : keyspaces) {
        LinkedHashMap<String, Object> keyspaceRow = new LinkedHashMap<>();
        keyspaceRow.put("keyspace_name", keyspace.name);
        keyspaceRow.put("durable_writes", true);
        keyspaceRow.put("replication", ImmutableMap.of("class", "org.apache.cassandra.locator.SimpleStrategy", "replication_factor", "1"));
        allKeyspacesRows.add(keyspaceRow);
        Query whenSelectKeyspace = new Query(SELECT_KEYSPACES + " WHERE keyspace_name = '" + keyspace.name + '\'');
        SuccessResult thenReturnKeyspace = new SuccessResult(Collections.singletonList(keyspaceRow), new LinkedHashMap<>(KEYSPACE_COLUMNS));
        RequestPrime primeKeyspace = new RequestPrime(whenSelectKeyspace, thenReturnKeyspace);
        simulacron.prime(new Prime(primeKeyspace));
        for (Table table : keyspace.tables) {
            LinkedHashMap<String, Object> tableRow = new LinkedHashMap<>();
            tableRow.put("keyspace_name", keyspace.name);
            tableRow.put("table_name", table.name);
            tableRow.put("bloom_filter_fp_chance", 0.01d);
            tableRow.put("caching", ImmutableMap.of("keys", "ALL", "rows_per_partition", "NONE"));
            tableRow.put("cdc", null);
            tableRow.put("comment", "");
            tableRow.put("compaction", ImmutableMap.of("class", "org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy", "max_threshold", "32", "min_threshold", "4"));
            tableRow.put("compression", ImmutableMap.of("chunk_length_in_kb", "64", "class", "org.apache.cassandra.io.compress.LZ4Compressor"));
            tableRow.put("crc_check_chance", 1d);
            tableRow.put("dclocal_read_repair_chance", 0.1d);
            tableRow.put("default_time_to_live", 0);
            tableRow.put("extensions", null);
            tableRow.put("flags", ImmutableSet.of("compound"));
            tableRow.put("gc_grace_seconds", 864000);
            tableRow.put("id", UUID.randomUUID());
            tableRow.put("max_index_interval", 2048);
            tableRow.put("memtable_flush_period_in_ms", 0);
            tableRow.put("min_index_interval", 128);
            tableRow.put("read_repair_chance", 0d);
            tableRow.put("speculative_retry", "99PERCENTILE");
            allTablesRows.add(tableRow);
            Query whenSelectTable = new Query(SELECT_TABLES + " WHERE keyspace_name = '" + keyspace.name + "'  AND table_name = '" + table.name + '\'');
            SuccessResult thenReturnTable = new SuccessResult(Collections.singletonList(tableRow), new LinkedHashMap<>(TABLE_COLUMNS));
            RequestPrime primeTable = new RequestPrime(whenSelectTable, thenReturnTable);
            simulacron.prime(new Prime(primeTable));
            List<LinkedHashMap<String, Object>> tableColumnsRows = new ArrayList<>();
            int position = 0;
            for (Column column : table.partitionKey) {
                LinkedHashMap<String, Object> columnRow = new LinkedHashMap<>();
                columnRow.put("keyspace_name", keyspace.name);
                columnRow.put("table_name", table.name);
                columnRow.put("column_name", column.name);
                columnRow.put("clustering_order", "none");
                columnRow.put("column_name_bytes", column.name.getBytes(StandardCharsets.UTF_8));
                columnRow.put("kind", "partition_key");
                columnRow.put("position", position++);
                columnRow.put("type", column.getTypeAsString());
                tableColumnsRows.add(columnRow);
            }
            position = 0;
            for (Column column : table.clusteringColumns) {
                LinkedHashMap<String, Object> columnRow = new LinkedHashMap<>();
                columnRow.put("keyspace_name", keyspace.name);
                columnRow.put("table_name", table.name);
                columnRow.put("column_name", column.name);
                columnRow.put("clustering_order", "asc");
                columnRow.put("column_name_bytes", column.name.getBytes(StandardCharsets.UTF_8));
                columnRow.put("kind", "clustering");
                columnRow.put("position", position++);
                columnRow.put("type", column.getTypeAsString());
                tableColumnsRows.add(columnRow);
            }
            for (Column column : table.otherColumns) {
                LinkedHashMap<String, Object> columnRow = new LinkedHashMap<>();
                columnRow.put("keyspace_name", keyspace.name);
                columnRow.put("table_name", table.name);
                columnRow.put("column_name", column.name);
                columnRow.put("clustering_order", "none");
                columnRow.put("column_name_bytes", column.name.getBytes(StandardCharsets.UTF_8));
                columnRow.put("kind", "regular");
                columnRow.put("position", -1);
                columnRow.put("type", column.getTypeAsString());
                tableColumnsRows.add(columnRow);
            }
            Query whenSelectTableColumns = new Query(SELECT_COLUMNS + " WHERE keyspace_name = '" + keyspace.name + "'  AND table_name = '" + table.name + '\'');
            SuccessResult thenReturnTableColumns = new SuccessResult(tableColumnsRows, new LinkedHashMap<>(TABLE_COLUMNS));
            RequestPrime primeAllTableColumns = new RequestPrime(whenSelectTableColumns, thenReturnTableColumns);
            simulacron.prime(new Prime(primeAllTableColumns));
            allColumnsRows.addAll(tableColumnsRows);
            // INSERT INTO table
            Query whenInsertIntoTable = new Query(String.format("INSERT INTO %s.%s (%s) VALUES (%s)", asCql(keyspace.name), asCql(table.name), table.allColumns().stream().map(col -> asCql(col.name)).collect(COMMA), table.allColumns().stream().map(col -> ":" + asCql(col.name)).collect(COMMA)), emptyList(), new LinkedHashMap<String, Object>(), table.allColumnTypes());
            simulacron.prime(new Prime(new RequestPrime(whenInsertIntoTable, new SuccessResult(emptyList(), new LinkedHashMap<String, String>()))));
            // UPDATE table
            Query whenUpdateIntoTable = new Query(String.format("UPDATE %s.%s SET %s", asCql(keyspace.name), asCql(table.name), table.allColumns().stream().map(col -> asCql(col.name) + "=:" + asCql(col.name)).collect(COMMA)), emptyList(), new LinkedHashMap<String, Object>(), table.allColumnTypes());
            simulacron.prime(new Prime(new RequestPrime(whenUpdateIntoTable, new SuccessResult(emptyList(), new LinkedHashMap<String, String>()))));
            // SELECT cols from table
            Query whenSelectFromTable = new Query(String.format("SELECT %s FROM %s.%s", table.allColumns().stream().map(col -> asCql(col.name)).collect(COMMA), asCql(keyspace.name), asCql(table.name)));
            simulacron.prime(new Prime(new RequestPrime(whenSelectFromTable, new SuccessResult(table.rows, table.allColumnTypes()))));
            // SELECT from table WHERE token...
            Query whenSelectFromTableWhere = new Query(String.format("SELECT %s FROM %s.%s WHERE token(%s) > ? AND token(%s) <= ?", table.allColumns().stream().map(col -> asCql(col.name)).collect(COMMA), asCql(keyspace.name), asCql(table.name), table.partitionKey.stream().map(col -> asCql(col.name)).collect(COMMA), table.partitionKey.stream().map(col -> asCql(col.name)).collect(COMMA)));
            simulacron.prime(new Prime(new RequestPrime(whenSelectFromTableWhere, new SuccessResult(table.rows, table.allColumnTypes()))));
            whenSelectFromTableWhere = new Query(String.format("SELECT %s FROM %s.%s WHERE token(%s) > :start AND token(%s) <= :end", table.allColumns().stream().map(col -> asCql(col.name)).collect(COMMA), asCql(keyspace.name), asCql(table.name), table.partitionKey.stream().map(col -> asCql(col.name)).collect(COMMA), table.partitionKey.stream().map(col -> asCql(col.name)).collect(COMMA)));
            simulacron.prime(new Prime(new RequestPrime(whenSelectFromTableWhere, new SuccessResult(table.rows, table.allColumnTypes()))));
        }
    }
    Query whenSelectAllKeyspaces = new Query(SELECT_KEYSPACES);
    SuccessResult thenReturnAllKeyspaces = new SuccessResult(allKeyspacesRows, new LinkedHashMap<>(KEYSPACE_COLUMNS));
    RequestPrime primeAllKeyspaces = new RequestPrime(whenSelectAllKeyspaces, thenReturnAllKeyspaces);
    simulacron.prime(new Prime(primeAllKeyspaces));
    Query whenSelectAllTables = new Query(SELECT_TABLES);
    SuccessResult thenReturnAllTables = new SuccessResult(allTablesRows, new LinkedHashMap<>(TABLE_COLUMNS));
    RequestPrime primeAllTables = new RequestPrime(whenSelectAllTables, thenReturnAllTables);
    simulacron.prime(new Prime(primeAllTables));
    Query whenSelectAllColumns = new Query(SELECT_COLUMNS);
    SuccessResult thenReturnAllColumns = new SuccessResult(allColumnsRows, new LinkedHashMap<>(COLUMN_COLUMNS));
    RequestPrime primeAllColumns = new RequestPrime(whenSelectAllColumns, thenReturnAllColumns);
    simulacron.prime(new Prime(primeAllColumns));
}
Also used : Prime(com.datastax.oss.simulacron.common.stubbing.Prime) BoundCluster(com.datastax.oss.simulacron.server.BoundCluster) Arrays(java.util.Arrays) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ImmutableSet(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet) HashMap(java.util.HashMap) Sets(org.assertj.core.util.Sets) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) ArrayList(java.util.ArrayList) RequestPrime(com.datastax.oss.simulacron.common.cluster.RequestPrime) LinkedHashMap(java.util.LinkedHashMap) ErrorResult(com.datastax.oss.simulacron.common.result.ErrorResult) Map(java.util.Map) ServerErrorResult(com.datastax.oss.simulacron.common.result.ServerErrorResult) Collector(java.util.stream.Collector) SimpleEntry(java.util.AbstractMap.SimpleEntry) Query(com.datastax.oss.simulacron.common.request.Query) SuccessResult(com.datastax.oss.simulacron.common.result.SuccessResult) Collections.emptyList(java.util.Collections.emptyList) DataType(com.datastax.oss.driver.api.core.type.DataType) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) DefaultProtocolVersion(com.datastax.oss.driver.api.core.DefaultProtocolVersion) List(java.util.List) BoundNode(com.datastax.oss.simulacron.server.BoundNode) Collections(java.util.Collections) TEXT(com.datastax.oss.driver.api.core.type.DataTypes.TEXT) Prime(com.datastax.oss.simulacron.common.stubbing.Prime) RequestPrime(com.datastax.oss.simulacron.common.cluster.RequestPrime) Query(com.datastax.oss.simulacron.common.request.Query) ArrayList(java.util.ArrayList) SuccessResult(com.datastax.oss.simulacron.common.result.SuccessResult) LinkedHashMap(java.util.LinkedHashMap) RequestPrime(com.datastax.oss.simulacron.common.cluster.RequestPrime)

Example 4 with SuccessResult

use of com.datastax.oss.simulacron.common.result.SuccessResult in project dsbulk by datastax.

the class EndToEndUtils method createSimpleParameterizedQuery.

public static RequestPrime createSimpleParameterizedQuery(String query) {
    LinkedHashMap<String, String> paramTypes = new LinkedHashMap<>();
    paramTypes.put("country_code", "varchar");
    paramTypes.put("country_name", "varchar");
    paramTypes.put("beginning_ip_address", "inet");
    paramTypes.put("ending_ip_address", "inet");
    paramTypes.put("beginning_ip_number", "bigint");
    paramTypes.put("ending_ip_number", "bigint");
    Query when = new Query(query, Collections.emptyList(), new LinkedHashMap<>(), paramTypes);
    SuccessResult then = new SuccessResult(new ArrayList<>(), new LinkedHashMap<>());
    return new RequestPrime(when, then);
}
Also used : Query(com.datastax.oss.simulacron.common.request.Query) RequestPrime(com.datastax.oss.simulacron.common.cluster.RequestPrime) SuccessResult(com.datastax.oss.simulacron.common.result.SuccessResult) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with SuccessResult

use of com.datastax.oss.simulacron.common.result.SuccessResult in project dsbulk by datastax.

the class EndToEndUtils method createQueryWithResultSetWithQuotes.

public static RequestPrime createQueryWithResultSetWithQuotes(String query, int numOfResults) {
    Query when = new Query(query);
    LinkedHashMap<String, String> columnTypes = new LinkedHashMap<>();
    columnTypes.put("country_code", "varchar");
    columnTypes.put("country_name", "varchar");
    columnTypes.put("beginning_ip_address", "inet");
    columnTypes.put("ending_ip_address", "inet");
    columnTypes.put("beginning_ip_number", "bigint");
    columnTypes.put("ending_ip_number", "bigint");
    List<LinkedHashMap<String, Object>> rows = new ArrayList<>();
    for (int i = 0; i < numOfResults; i++) {
        LinkedHashMap<String, Object> row = new LinkedHashMap<>();
        row.put("country_code", "country" + ";" + i);
        row.put("country_name", "country" + ";" + i);
        row.put("beginning_ip_address", "127.0.0." + i);
        row.put("ending_ip_address", "127.2.0." + i);
        row.put("beginning_ip_number", Integer.toString(i));
        row.put("ending_ip_number", Integer.toString(i));
        rows.add(row);
    }
    SuccessResult then = new SuccessResult(rows, columnTypes);
    return new RequestPrime(when, then);
}
Also used : Query(com.datastax.oss.simulacron.common.request.Query) RequestPrime(com.datastax.oss.simulacron.common.cluster.RequestPrime) ArrayList(java.util.ArrayList) SuccessResult(com.datastax.oss.simulacron.common.result.SuccessResult) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

RequestPrime (com.datastax.oss.simulacron.common.cluster.RequestPrime)6 SuccessResult (com.datastax.oss.simulacron.common.result.SuccessResult)6 LinkedHashMap (java.util.LinkedHashMap)6 Query (com.datastax.oss.simulacron.common.request.Query)4 Prime (com.datastax.oss.simulacron.common.stubbing.Prime)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 DataStaxBulkLoader (com.datastax.oss.dsbulk.runner.DataStaxBulkLoader)2 ExitStatus (com.datastax.oss.dsbulk.runner.ExitStatus)2 FunctionFailureResult (com.datastax.oss.simulacron.common.result.FunctionFailureResult)2 ReadTimeoutResult (com.datastax.oss.simulacron.common.result.ReadTimeoutResult)2 WriteFailureResult (com.datastax.oss.simulacron.common.result.WriteFailureResult)2 WriteTimeoutResult (com.datastax.oss.simulacron.common.result.WriteTimeoutResult)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 DefaultProtocolVersion (com.datastax.oss.driver.api.core.DefaultProtocolVersion)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 TEXT (com.datastax.oss.driver.api.core.type.DataTypes.TEXT)1 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)1