use of org.jgroups.util.MessageBatch in project JGroups by belaban.
the class MessageBatchTest method testAdd.
public void testAdd() {
MessageBatch batch = new MessageBatch(3);
List<Message> msgs = createMessages();
msgs.forEach(batch::add);
System.out.println("batch = " + batch);
assert batch.size() == msgs.size() : "batch: " + batch;
}
use of org.jgroups.util.MessageBatch in project JGroups by belaban.
the class MessageBatchTest method testCreationWithFilter.
public void testCreationWithFilter() {
List<Message> msgs = new ArrayList<>(10);
for (int i = 1; i <= 10; i++) msgs.add(new Message(null, i));
MessageBatch batch = new MessageBatch(null, null, null, true, msgs, msg -> msg != null && ((Integer) msg.getObject()) % 2 == 0);
System.out.println(batch.map(print_numbers));
assert batch.size() == 5;
for (Message msg : batch) assert ((Integer) msg.getObject()) % 2 == 0;
}
use of org.jgroups.util.MessageBatch in project JGroups by belaban.
the class SEQUENCER2 method removeAndDeliver.
protected void removeAndDeliver(Table<Message> win, Address sender) {
AtomicInteger adders = win.getAdders();
if (adders.getAndIncrement() != 0)
return;
final MessageBatch batch = new MessageBatch(win.size()).dest(local_addr).sender(sender).multicast(false);
Supplier<MessageBatch> batch_creator = () -> batch;
do {
try {
batch.reset();
win.removeMany(true, 0, null, batch_creator, BATCH_ACCUMULATOR);
} catch (Throwable t) {
log.error("failed removing messages from table for " + sender, t);
}
if (!batch.isEmpty()) {
// batch is guaranteed to NOT contain any OOB messages as the drop_oob_msgs_filter removed them
deliverBatch(batch);
}
} while (adders.decrementAndGet() != 0);
}
use of org.jgroups.util.MessageBatch 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 = 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);
for (Protocol p = nak; p != null; p = p.getDownProtocol()) p.setAddress(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");
}
use of org.jgroups.util.MessageBatch in project JGroups by belaban.
the class ParseMessages method main.
public static void main(String[] args) throws Exception {
String file = null;
boolean print_vers = false, binary_to_ascii = true, parse_discovery_responses = true, tcp = false;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-file")) {
file = args[++i];
continue;
}
if ("-version".equals(args[i])) {
print_vers = true;
continue;
}
if ("-tcp".equalsIgnoreCase(args[i])) {
tcp = true;
continue;
}
if ("-mappings".equalsIgnoreCase(args[i])) {
readMappings(args[++i]);
continue;
}
if ("-binary-to-ascii".equalsIgnoreCase(args[i])) {
binary_to_ascii = Boolean.parseBoolean(args[++i]);
continue;
}
if ("-parse-discovery-responses".equalsIgnoreCase(args[i])) {
parse_discovery_responses = Boolean.parseBoolean(args[++i]);
continue;
}
if ("-show-views".equalsIgnoreCase(args[i])) {
show_views = Boolean.parseBoolean(args[++i]);
continue;
}
help();
return;
}
final boolean print_version = print_vers, parse = parse_discovery_responses;
final AtomicInteger cnt = new AtomicInteger(1);
BiConsumer<Short, Message> msg_consumer = (version, msg) -> {
if (parse) {
try {
parseDiscoveryResponse(msg);
} catch (Exception e) {
System.err.printf("failed parsing discovery response from %s: %s\n", msg.src(), e);
}
}
View view = show_views ? getView(msg) : null;
System.out.printf("%d:%s %s, hdrs: %s %s\n", cnt.getAndIncrement(), print_version ? String.format(" [%s]", Version.print(version)) : "", msg, msg.printHeaders(), view == null ? "" : "(view: " + view + ")");
};
BiConsumer<Short, MessageBatch> batch_consumer = (version, batch) -> {
System.out.printf("%d:%s batch to %s from %s (%d messages):\n", cnt.getAndIncrement(), print_version ? String.format(" [%s]", Version.print(version)) : "", batch.dest() != null ? batch.dest() : "<all>", batch.sender(), batch.size());
int index = 1;
for (Message msg : batch) {
if (parse) {
try {
parseDiscoveryResponse(msg);
} catch (Exception e) {
System.err.printf("failed parsing discovery response from %s: %s\n", msg.src(), e);
}
}
View view = show_views ? getView(msg) : null;
System.out.printf("%d:%s %s, hdrs: %s %s\n", cnt.getAndIncrement(), print_version ? String.format(" [%s]", Version.print(version)) : "", msg, msg.printHeaders(), view == null ? "" : "(view: " + view + ")");
System.out.printf(" %d: [%d bytes%s], hdrs: %s %s\n", index++, msg.getLength(), msg.getFlags() > 0 ? ", flags=" + Util.flagsToString(msg.getFlags()) : "", msg.printHeaders(), view == null ? "" : "(view: " + view + ")");
}
};
InputStream in = file != null ? new FileInputStream(file) : System.in;
if (binary_to_ascii)
in = new BinaryToAsciiInputStream(in);
parse(in, msg_consumer, batch_consumer, tcp);
}
Aggregations