Search in sources :

Example 1 with MessageFilters

use of org.apache.cassandra.distributed.shared.MessageFilters in project cassandra by apache.

the class MessageFiltersTest method simpleFiltersTest.

private static void simpleFiltersTest(boolean inbound) {
    int VERB1 = Verb.READ_REQ.id;
    int VERB2 = Verb.READ_RSP.id;
    int VERB3 = Verb.READ_REPAIR_REQ.id;
    int i1 = 1;
    int i2 = 2;
    int i3 = 3;
    String MSG1 = "msg1";
    String MSG2 = "msg2";
    MessageFilters filters = new MessageFilters();
    Permit permit = inbound ? filters::permitInbound : filters::permitOutbound;
    IMessageFilters.Filter filter = filters.allVerbs().inbound(inbound).from(1).drop();
    Assert.assertFalse(permit.test(i1, i2, msg(VERB1, MSG1)));
    Assert.assertFalse(permit.test(i1, i2, msg(VERB2, MSG1)));
    Assert.assertFalse(permit.test(i1, i2, msg(VERB3, MSG1)));
    Assert.assertTrue(permit.test(i2, i1, msg(VERB1, MSG1)));
    filter.off();
    Assert.assertTrue(permit.test(i1, i2, msg(VERB1, MSG1)));
    filters.reset();
    filters.verbs(VERB1).inbound(inbound).from(1).to(2).drop();
    Assert.assertFalse(permit.test(i1, i2, msg(VERB1, MSG1)));
    Assert.assertTrue(permit.test(i1, i2, msg(VERB2, MSG1)));
    Assert.assertTrue(permit.test(i2, i1, msg(VERB1, MSG1)));
    Assert.assertTrue(permit.test(i2, i3, msg(VERB2, MSG1)));
    filters.reset();
    AtomicInteger counter = new AtomicInteger();
    filters.verbs(VERB1).inbound(inbound).from(1).to(2).messagesMatching((from, to, msg) -> {
        counter.incrementAndGet();
        return Arrays.equals(msg.bytes(), MSG1.getBytes());
    }).drop();
    Assert.assertFalse(permit.test(i1, i2, msg(VERB1, MSG1)));
    Assert.assertEquals(counter.get(), 1);
    Assert.assertTrue(permit.test(i1, i2, msg(VERB1, MSG2)));
    Assert.assertEquals(counter.get(), 2);
    // filter chain gets interrupted because a higher level filter returns no match
    Assert.assertTrue(permit.test(i2, i1, msg(VERB1, MSG1)));
    Assert.assertEquals(counter.get(), 2);
    Assert.assertTrue(permit.test(i2, i1, msg(VERB2, MSG1)));
    Assert.assertEquals(counter.get(), 2);
    filters.reset();
    filters.allVerbs().inbound(inbound).from(3, 2).to(2, 1).drop();
    Assert.assertFalse(permit.test(i3, i1, msg(VERB1, MSG1)));
    Assert.assertFalse(permit.test(i3, i2, msg(VERB1, MSG1)));
    Assert.assertFalse(permit.test(i2, i1, msg(VERB1, MSG1)));
    Assert.assertTrue(permit.test(i2, i3, msg(VERB1, MSG1)));
    Assert.assertTrue(permit.test(i1, i2, msg(VERB1, MSG1)));
    Assert.assertTrue(permit.test(i1, i3, msg(VERB1, MSG1)));
    filters.reset();
    counter.set(0);
    filters.allVerbs().inbound(inbound).from(1).to(2).messagesMatching((from, to, msg) -> {
        counter.incrementAndGet();
        return false;
    }).drop();
    Assert.assertTrue(permit.test(i1, i2, msg(VERB1, MSG1)));
    Assert.assertTrue(permit.test(i1, i3, msg(VERB1, MSG1)));
    Assert.assertTrue(permit.test(i1, i2, msg(VERB1, MSG1)));
    Assert.assertEquals(2, 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) IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) MessageFilters(org.apache.cassandra.distributed.shared.MessageFilters)

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