Search in sources :

Example 1 with IsolatedExecutor

use of org.apache.cassandra.distributed.impl.IsolatedExecutor in project cassandra by apache.

the class MessageForwardingTest method mutationsForwardedToAllReplicasTest.

@Test
public void mutationsForwardedToAllReplicasTest() {
    String originalTraceTimeout = TracingUtil.setWaitForTracingEventTimeoutSecs("1");
    final int numInserts = 100;
    Map<InetAddress, Integer> forwardFromCounts = new HashMap<>();
    Map<InetAddress, Integer> commitCounts = new HashMap<>();
    try (Cluster cluster = (Cluster) init(builder().withDC("dc0", 1).withDC("dc1", 3).start())) {
        cluster.schemaChange("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck int, v text, PRIMARY KEY (pk, ck))");
        cluster.forEach(instance -> commitCounts.put(instance.broadcastAddress().getAddress(), 0));
        final UUID sessionId = UUIDGen.getTimeUUID();
        Stream<Future<Object[][]>> inserts = IntStream.range(0, numInserts).mapToObj((idx) -> {
            return cluster.coordinator(1).asyncExecuteWithTracing(sessionId, "INSERT INTO " + KEYSPACE + ".tbl(pk,ck,v) VALUES (1, 1, 'x')", ConsistencyLevel.ALL);
        });
        // Wait for each of the futures to complete before checking the traces, don't care
        // about the result so
        // noinspection ResultOfMethodCallIgnored
        inserts.map(IsolatedExecutor::waitOn).collect(Collectors.toList());
        cluster.stream("dc1").forEach(instance -> forwardFromCounts.put(instance.broadcastAddress().getAddress(), 0));
        cluster.forEach(instance -> commitCounts.put(instance.broadcastAddress().getAddress(), 0));
        List<TracingUtil.TraceEntry> traces = TracingUtil.getTrace(cluster, sessionId, ConsistencyLevel.ALL);
        traces.forEach(traceEntry -> {
            if (traceEntry.activity.contains("Appending to commitlog")) {
                commitCounts.compute(traceEntry.source, (k, v) -> (v != null ? v : 0) + 1);
            } else if (traceEntry.activity.contains("Enqueuing forwarded write to ")) {
                forwardFromCounts.compute(traceEntry.source, (k, v) -> (v != null ? v : 0) + 1);
            }
        });
        // Check that each node in dc1 was the forwarder at least once.  There is a (1/3)^numInserts chance
        // that the same node will be picked, but the odds of that are ~2e-48.
        forwardFromCounts.forEach((source, count) -> Assert.assertTrue(source + " should have been randomized to forward messages", count > 0));
        // Check that each node received the forwarded messages once (and only once)
        commitCounts.forEach((source, count) -> Assert.assertEquals(source + " appending to commitlog traces", (long) numInserts, (long) count));
    } catch (IOException e) {
        Assert.fail("Threw exception: " + e);
    } finally {
        TracingUtil.setWaitForTracingEventTimeoutSecs(originalTraceTimeout);
    }
}
Also used : IntStream(java.util.stream.IntStream) IsolatedExecutor(org.apache.cassandra.distributed.impl.IsolatedExecutor) IOException(java.io.IOException) HashMap(java.util.HashMap) Test(org.junit.Test) UUID(java.util.UUID) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) Collectors(java.util.stream.Collectors) UUIDGen(org.apache.cassandra.utils.UUIDGen) InetAddress(java.net.InetAddress) List(java.util.List) Future(java.util.concurrent.Future) Stream(java.util.stream.Stream) Map(java.util.Map) Cluster(org.apache.cassandra.distributed.Cluster) Assert(org.junit.Assert) TracingUtil(org.apache.cassandra.distributed.impl.TracingUtil) HashMap(java.util.HashMap) Cluster(org.apache.cassandra.distributed.Cluster) IOException(java.io.IOException) Future(java.util.concurrent.Future) UUID(java.util.UUID) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Future (java.util.concurrent.Future)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 Cluster (org.apache.cassandra.distributed.Cluster)1 ConsistencyLevel (org.apache.cassandra.distributed.api.ConsistencyLevel)1 IsolatedExecutor (org.apache.cassandra.distributed.impl.IsolatedExecutor)1 TracingUtil (org.apache.cassandra.distributed.impl.TracingUtil)1 UUIDGen (org.apache.cassandra.utils.UUIDGen)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1