Search in sources :

Example 11 with MutableDigest

use of org.jgroups.util.MutableDigest in project JGroups by belaban.

the class NAKACK_StressTest method start.

private static void start(final int num_threads, final int num_msgs, boolean oob) {
    final NAKACK2 nak = new NAKACK2();
    final AtomicInteger counter = new AtomicInteger(num_msgs);
    final AtomicLong seqno = new AtomicLong(1);
    final AtomicInteger delivered_msgs = new AtomicInteger(0);
    final Lock lock = new ReentrantLock();
    final Condition all_msgs_delivered = lock.newCondition();
    final ConcurrentLinkedQueue<Long> delivered_msg_list = new ConcurrentLinkedQueue<>();
    final Address local_addr = Util.createRandomAddress("A");
    final Address sender = Util.createRandomAddress("B");
    nak.setDownProtocol(new Protocol() {

        public Object down(Event evt) {
            return null;
        }
    });
    nak.setUpProtocol(new Protocol() {

        public Object up(Message msg) {
            delivered_msgs.incrementAndGet();
            NakAckHeader2 hdr = msg.getHeader(NAKACK_ID);
            if (hdr != null)
                delivered_msg_list.add(hdr.getSeqno());
            if (delivered_msgs.get() >= num_msgs) {
                lock.lock();
                try {
                    all_msgs_delivered.signalAll();
                } finally {
                    lock.unlock();
                }
            }
            return null;
        }

        public void up(MessageBatch batch) {
            for (Message msg : batch) {
                delivered_msgs.incrementAndGet();
                NakAckHeader2 hdr = (NakAckHeader2) msg.getHeader(NAKACK_ID);
                if (hdr != null)
                    delivered_msg_list.add(hdr.getSeqno());
                if (delivered_msgs.get() >= num_msgs) {
                    lock.lock();
                    try {
                        all_msgs_delivered.signalAll();
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }
    });
    nak.setDiscardDeliveredMsgs(true);
    nak.down(new Event(Event.SET_LOCAL_ADDRESS, local_addr));
    nak.down(new Event(Event.BECOME_SERVER));
    View view = View.create(local_addr, 1, local_addr, sender);
    nak.down(new Event(Event.VIEW_CHANGE, view));
    MutableDigest digest = new MutableDigest(view.getMembersRaw());
    digest.set(local_addr, 0, 0);
    digest.set(sender, 0, 0);
    nak.down(new Event(Event.SET_DIGEST, digest));
    final CountDownLatch latch = new CountDownLatch(1);
    Sender[] adders = new Sender[num_threads];
    for (int i = 0; i < adders.length; i++) {
        adders[i] = new Sender(nak, latch, counter, seqno, oob, sender);
        adders[i].start();
    }
    long start = System.currentTimeMillis();
    // starts all adders
    latch.countDown();
    int max_tries = 30;
    lock.lock();
    try {
        while (delivered_msgs.get() < num_msgs && max_tries-- > 0) {
            try {
                all_msgs_delivered.await(1000, TimeUnit.MILLISECONDS);
                System.out.println("received " + delivered_msgs.get() + " msgs");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    } finally {
        lock.unlock();
    }
    long time = System.currentTimeMillis() - start;
    double requests_sec = num_msgs / (time / 1000.0);
    System.out.printf("\nTime: %d ms, %.2f requests / sec\n", time, requests_sec);
    System.out.println("Delivered messages: " + delivered_msg_list.size());
    if (delivered_msg_list.size() < 100)
        System.out.println("Elements: " + delivered_msg_list);
    nak.stop();
    List<Long> results = new ArrayList<>(delivered_msg_list);
    if (oob)
        Collections.sort(results);
    assert results.size() == num_msgs : "expected " + num_msgs + ", but got " + results.size();
    System.out.println("Checking results consistency");
    int i = 1;
    for (Long num : results) {
        if (num != i) {
            assert i == num : "expected " + i + " but got " + num;
            return;
        }
        i++;
    }
    System.out.println("OK");
}
Also used : ArrayList(java.util.ArrayList) MessageBatch(org.jgroups.util.MessageBatch) Protocol(org.jgroups.stack.Protocol) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) CountDownLatch(java.util.concurrent.CountDownLatch) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) NAKACK2(org.jgroups.protocols.pbcast.NAKACK2) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) NakAckHeader2(org.jgroups.protocols.pbcast.NakAckHeader2) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) MutableDigest(org.jgroups.util.MutableDigest)

Example 12 with MutableDigest

use of org.jgroups.util.MutableDigest in project JGroups by belaban.

the class MergeTest3 method createPartition.

protected void createPartition(JChannel... channels) {
    // find the highest view-id +1
    long view_id = 1;
    for (JChannel ch : channels) view_id = Math.max(ch.getView().getViewId().getId(), view_id);
    view_id++;
    List<Address> members = getMembers(channels);
    Collections.sort(members);
    Address coord = members.get(0);
    View view = new View(coord, view_id, members);
    MutableDigest digest = new MutableDigest(view.getMembersRaw());
    for (JChannel ch : channels) {
        NAKACK2 nakack = (NAKACK2) ch.getProtocolStack().findProtocol(NAKACK2.class);
        digest.merge(nakack.getDigest(ch.getAddress()));
    }
    for (JChannel ch : channels) {
        GMS gms = (GMS) ch.getProtocolStack().findProtocol(GMS.class);
        gms.installView(view, digest);
    }
}
Also used : NAKACK2(org.jgroups.protocols.pbcast.NAKACK2) MutableDigest(org.jgroups.util.MutableDigest) GMS(org.jgroups.protocols.pbcast.GMS)

Example 13 with MutableDigest

use of org.jgroups.util.MutableDigest in project JGroups by belaban.

the class DigestTest method testIncrementHighSeqno.

public void testIncrementHighSeqno() {
    md = new MutableDigest(members);
    md.set(a1, 1, 100).set(a2, 3, 300).set(a3, 7, 700);
    long tmp = md.get(a1)[0];
    md.set(a1, tmp + 1, tmp + 1);
    Assert.assertEquals(md.get(a1)[0], tmp + 1);
    tmp = md.get(a2)[0];
    md.set(a2, tmp + 1, tmp + 1);
    Assert.assertEquals(md.get(a2)[0], tmp + 1);
    tmp = md.get(a3)[0];
    md.set(a3, tmp + 1, tmp + 1);
    Assert.assertEquals(md.get(a3)[0], tmp + 1);
}
Also used : MutableDigest(org.jgroups.util.MutableDigest)

Example 14 with MutableDigest

use of org.jgroups.util.MutableDigest in project JGroups by belaban.

the class DigestTest method testMerge.

public void testMerge() {
    MutableDigest digest = new MutableDigest(members).set(a1, 499, 502).set(a2, 26, 27).set(a3, 26, 35);
    System.out.println("d: " + d);
    System.out.println("digest: " + digest);
    digest.merge(d);
    System.out.println("merged digest: " + digest);
    Assert.assertEquals(d.capacity(), 3);
    Assert.assertEquals(digest.capacity(), 3);
    Assert.assertEquals(digest.get(a1)[0], 500);
    Assert.assertEquals(digest.get(a1)[1], 502);
    Assert.assertEquals(digest.get(a2)[0], 26);
    Assert.assertEquals(digest.get(a2)[1], 27);
    Assert.assertEquals(digest.get(a3)[0], 26);
    Assert.assertEquals(digest.get(a3)[1], 35);
}
Also used : MutableDigest(org.jgroups.util.MutableDigest)

Example 15 with MutableDigest

use of org.jgroups.util.MutableDigest in project JGroups by belaban.

the class DigestTest method testConstructor1.

public void testConstructor1() {
    Digest dd = new MutableDigest(members);
    Assert.assertEquals(dd.capacity(), members.length);
}
Also used : MutableDigest(org.jgroups.util.MutableDigest) Digest(org.jgroups.util.Digest) MutableDigest(org.jgroups.util.MutableDigest)

Aggregations

MutableDigest (org.jgroups.util.MutableDigest)18 Digest (org.jgroups.util.Digest)4 NAKACK2 (org.jgroups.protocols.pbcast.NAKACK2)3 Address (org.jgroups.Address)2 GMS (org.jgroups.protocols.pbcast.GMS)2 Protocol (org.jgroups.stack.Protocol)2 ArrayList (java.util.ArrayList)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 View (org.jgroups.View)1 NakAckHeader2 (org.jgroups.protocols.pbcast.NakAckHeader2)1 MessageBatch (org.jgroups.util.MessageBatch)1 BeforeMethod (org.testng.annotations.BeforeMethod)1