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());
}
}
}
Aggregations