Search in sources :

Example 1 with WriteTimeoutException

use of com.datastax.driver.core.exceptions.WriteTimeoutException in project cassandra by apache.

the class ViewLongTest method testConflictResolution.

@Test
public void testConflictResolution() throws Throwable {
    final int writers = 96;
    final int insertsPerWriter = 50;
    final Map<Integer, Exception> failedWrites = new ConcurrentHashMap<>();
    createTable("CREATE TABLE %s (" + "a int," + "b int," + "c int," + "PRIMARY KEY (a, b))");
    executeNet(protocolVersion, "USE " + keyspace());
    createView("mv", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE c IS NOT NULL AND a IS NOT NULL AND b IS NOT NULL PRIMARY KEY (c, a, b)");
    CyclicBarrier semaphore = new CyclicBarrier(writers);
    Thread[] threads = new Thread[writers];
    for (int i = 0; i < writers; i++) {
        final int writer = i;
        Thread t = NamedThreadFactory.createThread(new WrappedRunnable() {

            public void runMayThrow() {
                try {
                    int writerOffset = writer * insertsPerWriter;
                    semaphore.await();
                    for (int i = 0; i < insertsPerWriter; i++) {
                        try {
                            executeNet(protocolVersion, "INSERT INTO %s (a, b, c) VALUES (?, ?, ?) USING TIMESTAMP 1", 1, 1, i + writerOffset);
                        } catch (NoHostAvailableException | WriteTimeoutException e) {
                            failedWrites.put(i + writerOffset, e);
                        }
                    }
                } catch (Throwable e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.start();
        threads[i] = t;
    }
    for (int i = 0; i < writers; i++) threads[i].join();
    for (int i = 0; i < writers * insertsPerWriter; i++) {
        if (executeNet(protocolVersion, "SELECT COUNT(*) FROM system.batches").one().getLong(0) == 0)
            break;
        try {
            // This will throw exceptions whenever there are exceptions trying to push the view values out, caused
            // by the view becoming overwhelmed.
            BatchlogManager.instance.startBatchlogReplay().get();
        } catch (Throwable ignore) {
        }
    }
    int value = executeNet(protocolVersion, "SELECT c FROM %s WHERE a = 1 AND b = 1").one().getInt("c");
    List<Row> rows = executeNet(protocolVersion, "SELECT c FROM " + keyspace() + ".mv").all();
    boolean containsC = false;
    StringBuilder others = new StringBuilder();
    StringBuilder overlappingFailedWrites = new StringBuilder();
    for (Row row : rows) {
        int c = row.getInt("c");
        if (c == value)
            containsC = true;
        else {
            if (others.length() != 0)
                others.append(' ');
            others.append(c);
            if (failedWrites.containsKey(c)) {
                if (overlappingFailedWrites.length() != 0)
                    overlappingFailedWrites.append(' ');
                overlappingFailedWrites.append(c).append(':').append(failedWrites.get(c).getMessage());
            }
        }
    }
    if (rows.size() > 1) {
        throw new AssertionError(String.format("Expected 1 row, but found %d; %s c = %d, and (%s) of which (%s) failed to insert", rows.size(), containsC ? "found row with" : "no rows contained", value, others, overlappingFailedWrites));
    } else if (rows.isEmpty()) {
        throw new AssertionError(String.format("Could not find row with c = %d", value));
    } else if (rows.size() == 1 && !containsC) {
        throw new AssertionError(String.format("Single row had c = %d, expected %d", rows.get(0).getInt("c"), value));
    }
}
Also used : WrappedRunnable(org.apache.cassandra.utils.WrappedRunnable) WriteTimeoutException(com.datastax.driver.core.exceptions.WriteTimeoutException) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Row(com.datastax.driver.core.Row) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

Row (com.datastax.driver.core.Row)1 NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)1 WriteTimeoutException (com.datastax.driver.core.exceptions.WriteTimeoutException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 WrappedRunnable (org.apache.cassandra.utils.WrappedRunnable)1 Test (org.junit.Test)1