Search in sources :

Example 6 with NakAckHeader2

use of org.jgroups.protocols.pbcast.NakAckHeader2 in project JGroups by belaban.

the class EncryptTest method testCapturingOfMessageByNonMemberAndResending.

/**
 * Tests the scenario where the non-member R captures a message from some cluster member in {A,B,C}, then
 * increments the NAKACK2 seqno and resends that message. The message must not be received by {A,B,C};
 * it should be discarded. see https://issues.jboss.org/browse/JGRP-2273
 */
public void testCapturingOfMessageByNonMemberAndResending() throws Exception {
    // this one won't succeed
    for (JChannel ch : Arrays.asList(a, b, c)) {
        SERIALIZE s = new SERIALIZE();
        ch.getProtocolStack().insertProtocol(s, ProtocolStack.Position.ABOVE, Encrypt.class);
        s.init();
    }
    rogue.setReceiver(new Receiver() {

        public void receive(Message msg) {
            System.out.printf("rogue: modifying and resending msg %s, hdrs: %s\n", msg, msg.printHeaders());
            // to prevent recursive cycle
            rogue.setReceiver(null);
            try {
                short prot_id = ClassConfigurator.getProtocolId(NAKACK2.class);
                NakAckHeader2 hdr = msg.getHeader(prot_id);
                if (hdr != null) {
                    long seqno = hdr.getSeqno();
                    Util.setField(Util.getField(NakAckHeader2.class, "seqno"), hdr, seqno + 1);
                } else {
                    System.out.printf("Rogue was not able to get the %s header, fabricating one with seqno=50\n", NAKACK2.class.getSimpleName());
                    NakAckHeader2 hdr2 = NakAckHeader2.createMessageHeader(50);
                    msg.putHeader(prot_id, hdr2);
                }
                rogue.send(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    a.send(null, "Hello world from A");
    // everybody in {A,B,C} should receive this message, but NOT the rogue's resent message
    for (int i = 0; i < 10; i++) {
        if (ra.size() > 1 || rb.size() > 1 || rc.size() > 1)
            // this should NOT happen
            break;
        Util.sleep(500);
    }
    Stream.of(ra, rb, rc).map(MyReceiver::list).map(l -> l.stream().map(msg -> (String) msg.getObject()).collect(Collectors.toList())).forEach(System.out::println);
    assert ra.size() == 1 : String.format("received msgs from non-member: '%s'; this should not be the case", print(ra.list()));
    assert rb.size() == 1 : String.format("received msgs from non-member: '%s'; this should not be the case", print(rb.list()));
    assert rc.size() == 1 : String.format("received msgs from non-member: '%s'; this should not be the case", print(rc.list()));
}
Also used : Protocol(org.jgroups.stack.Protocol) Arrays(java.util.Arrays) Util(org.jgroups.util.Util) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Test(org.testng.annotations.Test) GMS(org.jgroups.protocols.pbcast.GMS) NakAckHeader2(org.jgroups.protocols.pbcast.NakAckHeader2) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) MyReceiver(org.jgroups.util.MyReceiver) List(java.util.List) Stream(java.util.stream.Stream) NAKACK2(org.jgroups.protocols.pbcast.NAKACK2) ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream) org.jgroups(org.jgroups) ClassConfigurator(org.jgroups.conf.ClassConfigurator) SecretKey(javax.crypto.SecretKey) ByteArray(org.jgroups.util.ByteArray) KeyStoreGenerator(org.jgroups.demos.KeyStoreGenerator) ProtocolStack(org.jgroups.stack.ProtocolStack) NAKACK2(org.jgroups.protocols.pbcast.NAKACK2) NakAckHeader2(org.jgroups.protocols.pbcast.NakAckHeader2) MyReceiver(org.jgroups.util.MyReceiver) MyReceiver(org.jgroups.util.MyReceiver)

Example 7 with NakAckHeader2

use of org.jgroups.protocols.pbcast.NakAckHeader2 in project JGroups by belaban.

the class MessageTestBase method addHeaders.

protected static void addHeaders(Message msg) {
    TpHeader tp_hdr = new TpHeader("DemoChannel2");
    msg.putHeader(UDP_ID, tp_hdr);
    PingHeader ping_hdr = new PingHeader(PingHeader.GET_MBRS_REQ).clusterName("demo-cluster");
    msg.putHeader(PING_ID, ping_hdr);
    NakAckHeader2 nak_hdr = NakAckHeader2.createXmitRequestHeader(Util.createRandomAddress("S"));
    msg.putHeader(NAKACK_ID, nak_hdr);
}
Also used : NakAckHeader2(org.jgroups.protocols.pbcast.NakAckHeader2) PingHeader(org.jgroups.protocols.PingHeader) TpHeader(org.jgroups.protocols.TpHeader)

Aggregations

NakAckHeader2 (org.jgroups.protocols.pbcast.NakAckHeader2)7 ArrayList (java.util.ArrayList)2 PingHeader (org.jgroups.protocols.PingHeader)2 TpHeader (org.jgroups.protocols.TpHeader)2 NAKACK2 (org.jgroups.protocols.pbcast.NAKACK2)2 Protocol (org.jgroups.stack.Protocol)2 Field (java.lang.reflect.Field)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Condition (java.util.concurrent.locks.Condition)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1