Search in sources :

Example 1 with Instance

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

the class MessageFiltersTest method testMessageMatching.

@Test
public void testMessageMatching() throws Throwable {
    String read = "SELECT * FROM " + KEYSPACE + ".tbl";
    String write = "INSERT INTO " + KEYSPACE + ".tbl (pk, ck, v) VALUES (1, 1, 1)";
    try (ICluster<IInvokableInstance> cluster = builder().withNodes(2).withConfig(c -> c.set("range_request_timeout", "2000ms")).start()) {
        cluster.schemaChange("CREATE KEYSPACE " + KEYSPACE + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': " + cluster.size() + "};");
        cluster.schemaChange("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck))");
        AtomicInteger counter = new AtomicInteger();
        Set<Integer> verbs = Sets.newHashSet(Arrays.asList(Verb.RANGE_REQ.id, Verb.RANGE_RSP.id, Verb.MUTATION_REQ.id, Verb.MUTATION_RSP.id));
        for (boolean inbound : Arrays.asList(true, false)) {
            counter.set(0);
            // Reads and writes are going to time out in both directions
            IMessageFilters.Filter filter = cluster.filters().allVerbs().inbound(inbound).from(1).to(2).messagesMatching((from, to, msg) -> {
                // Decode and verify message on instance; return the result back here
                Integer id = cluster.get(1).callsOnInstance((IIsolatedExecutor.SerializableCallable<Integer>) () -> {
                    Message decoded = Instance.deserializeMessage(msg);
                    return (Integer) decoded.verb().id;
                }).call();
                Assert.assertTrue(verbs.contains(id));
                counter.incrementAndGet();
                return false;
            }).drop();
            for (int i : new int[] { 1, 2 }) cluster.coordinator(i).execute(read, ConsistencyLevel.ALL);
            for (int i : new int[] { 1, 2 }) cluster.coordinator(i).execute(write, ConsistencyLevel.ALL);
            filter.off();
            Assert.assertEquals(4, counter.get());
        }
    }
}
Also used : Instance(org.apache.cassandra.distributed.impl.Instance) IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(org.apache.cassandra.net.Message) IMessage(org.apache.cassandra.distributed.api.IMessage) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NoPayload(org.apache.cassandra.net.NoPayload) IIsolatedExecutor(org.apache.cassandra.distributed.api.IIsolatedExecutor) MessageFilters(org.apache.cassandra.distributed.shared.MessageFilters) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) MessagingService(org.apache.cassandra.net.MessagingService) Set(java.util.Set) ICluster(org.apache.cassandra.distributed.api.ICluster) Test(org.junit.Test) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) Verb(org.apache.cassandra.net.Verb) InetSocketAddress(java.net.InetSocketAddress) Sets(com.google.common.collect.Sets) CountDownLatch(java.util.concurrent.CountDownLatch) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Cluster(org.apache.cassandra.distributed.Cluster) Assert(org.junit.Assert) HintMessage(org.apache.cassandra.hints.HintMessage) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) Message(org.apache.cassandra.net.Message) IMessage(org.apache.cassandra.distributed.api.IMessage) HintMessage(org.apache.cassandra.hints.HintMessage) IIsolatedExecutor(org.apache.cassandra.distributed.api.IIsolatedExecutor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

Sets (com.google.common.collect.Sets)1 InetSocketAddress (java.net.InetSocketAddress)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Cluster (org.apache.cassandra.distributed.Cluster)1 ConsistencyLevel (org.apache.cassandra.distributed.api.ConsistencyLevel)1 GOSSIP (org.apache.cassandra.distributed.api.Feature.GOSSIP)1 NETWORK (org.apache.cassandra.distributed.api.Feature.NETWORK)1 ICluster (org.apache.cassandra.distributed.api.ICluster)1 IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)1 IIsolatedExecutor (org.apache.cassandra.distributed.api.IIsolatedExecutor)1 IMessage (org.apache.cassandra.distributed.api.IMessage)1 IMessageFilters (org.apache.cassandra.distributed.api.IMessageFilters)1 Instance (org.apache.cassandra.distributed.impl.Instance)1 MessageFilters (org.apache.cassandra.distributed.shared.MessageFilters)1 HintMessage (org.apache.cassandra.hints.HintMessage)1