From c325421927b8f9eca4567efac4da4cb9aa38325a Mon Sep 17 00:00:00 2001 From: Yury Yaroshevich Date: Wed, 12 Dec 2018 19:37:28 +0300 Subject: [PATCH] Do not explicitly create threads in PacketQueue (#157) * Do not explicitly create threads in PacketQueue, but borrow threads for execution from ExecutorService. * Added method to release created packet in PacketQueue. * Added constructor overload to override default ExecutorService of PacketQueue. * Fixed inconsistent formatting. * Added throttling support to PacketQueue. * Added performance tests for various configurations of PacketQueue. * Updated comments. * Added test for fixed size thread pool to compare it with ForkJoinPool. * Fixed comments. * Changed default values of PacketQueue configuration to closer match old implementation. * Removed unused constants. * Added tests to reveal introduced bug. Fixed bug. * Fixed comments in PacketsQueue. * Do not hard code class name in thread pool name. * Return 0 when handler is null (should be not be reachable, but anyway). * Added test to check capacity constraint of PacketQueue. * Replaced manual capacity check with checking offer return value. * Made throttler class static. * Added test to check that PacketQueue thread is release without work. * Do GC before running performance benchmark. * Fixed formatting and added timeout. * Added test to check cooperative multi-tasking behavior when executor is shared between queues and has less threads then executor has. * Minor code rearrangement. * Use dedicated syncronization object for sate of AsyncPacketReader. * Stop PacketQueue when benchmark is finished. * Added constant for number of benchmark iterations. * Minor code style enhancements. * Added myself to the list of authors. * Added test to check that million of PacketQueue can share single thread without eating CPU when idle. * Extracted AsyncQueueHandler class. * Fixed imports. * Minor fixes. * Minimize diff by undoing unnecessary changes. * Minimized diff. * Reduce diff. * Added constructor argument null checks. * Removed throttling and simplified AsyncQueueHandler. * Reviewed usage of syncronized blocks. * Moved variable closer to usage. * Fixed according to code review. * Added extra constructor. * Removed cancel from onYield because it's unnecessary after removing throttling. * Rearranged onYield and handleQueueItemsUntilEmpty, introduced rescheduleReader * Updated copyright. --- .../org/ice4j/util/AsyncQueueHandler.java | 285 ++++++++++++++++ src/main/java/org/ice4j/util/PacketQueue.java | 231 +++++++------ src/test/java/org/ice4j/util/DummyQueue.java | 62 ++++ .../ice4j/util/PacketQueueBenchmarkTests.java | 236 +++++++++++++ .../java/org/ice4j/util/PacketQueueTests.java | 314 ++++++++++++++++++ 5 files changed, 1033 insertions(+), 95 deletions(-) create mode 100644 src/main/java/org/ice4j/util/AsyncQueueHandler.java create mode 100644 src/test/java/org/ice4j/util/DummyQueue.java create mode 100644 src/test/java/org/ice4j/util/PacketQueueBenchmarkTests.java create mode 100644 src/test/java/org/ice4j/util/PacketQueueTests.java diff --git a/src/main/java/org/ice4j/util/AsyncQueueHandler.java b/src/main/java/org/ice4j/util/AsyncQueueHandler.java new file mode 100644 index 0000000..95ff248 --- /dev/null +++ b/src/main/java/org/ice4j/util/AsyncQueueHandler.java @@ -0,0 +1,285 @@ +/* + * Copyright @ 2018 8x8, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.ice4j.util; + +import java.util.concurrent.*; +import java.util.concurrent.atomic.*; +import java.util.logging.Logger; // Disambiguation + +/** + * Asynchronously reads items from provided {@link #queue} on separate thread + * borrowed from {@link #executor} and process items with specified handler. + * Thread is not blocked when queue is empty and returned back to + * {@link #executor} pool. New or existing thread is borrowed from + * {@link #executor} when queue is non empty and {@link #reader} is not + * running + * + * @author Yura Yaroshevich + */ +public final class AsyncQueueHandler +{ + /** + * The {@link java.util.logging.Logger} used by the + * {@link AsyncQueueHandler} class and its instances for logging output. + */ + private static final java.util.logging.Logger logger + = Logger.getLogger(AsyncQueueHandler.class.getName()); + + /** + * The default ExecutorService to run AsyncQueueHandler + * when there is no executor injected in constructor. + */ + private final static ExecutorService sharedExecutor + = Executors.newCachedThreadPool( + new CustomizableThreadFactory( + AsyncQueueHandler.class.getName() + "-executor-", true)); + + /** + * Executor service to run {@link #reader}, which asynchronously + * invokes specified {@link #handler} on queued items. + */ + private final ExecutorService executor; + + /** + * An {@link BlockingQueue } whose items read on separate thread and + * processed by provided {@link #handler}. + */ + private final BlockingQueue queue; + + /** + * The {@link Handler} used to handle items read from + * {@link #queue} by {@link #reader}. + */ + private final Handler handler; + + /** + * An identifier of current reader which is used for debugging purpose. + */ + private final String id; + + /** + * Specifies the number of items allowed to be handled sequentially + * without yielding control to executor's thread. Specifying positive + * number allows implementation of cooperative multi-tasking + * between different {@link AsyncQueueHandler} sharing + * same {@link ExecutorService}. + */ + private final long maxSequentiallyHandledItems; + + /** + * A flag which indicates if reading of {@link #queue} is allowed + * to continue. + */ + private final AtomicBoolean running = new AtomicBoolean(); + + /** + * Synchronization object of current instance state, in particular + * used to resolve races between {@link #handleQueueItemsUntilEmpty()} + * and {@link #reader} exit. In particular synchronization object used to + * access to field {@link #readerFuture}. + */ + private final Object syncRoot = new Object(); + + /** + * Stores Future of currently executing {@link #reader} + */ + private Future readerFuture; + + /** + * Perpetually reads item from {@link #queue} and uses + * {@link #handler} on each of them. + */ + private final Runnable reader = new Runnable() + { + @Override + public void run() + { + long sequentiallyHandledItems = 0; + + while (running.get()) + { + if (maxSequentiallyHandledItems > 0 && + sequentiallyHandledItems >= maxSequentiallyHandledItems) + { + onYield(); + return; + } + + T item; + + synchronized (syncRoot) + { + item = queue.poll(); + + if (item == null) + { + cancel(false); + return; + } + } + + sequentiallyHandledItems++; + + try + { + handler.handleItem(item); + } + catch (Throwable e) + { + logger.warning("Failed to handle item: " + e); + } + } + } + }; + + /** + * Constructs instance of {@link AsyncQueueHandler} which is capable of + * asynchronous reading provided queue from thread borrowed from executor to + * process items with provided handler. + * @param queue thread-safe queue which holds items to process + * @param handler an implementation of handler routine which will be + * invoked per each item placed in the queue. + * @param id optional identifier of current handler for debug purpose + * @param executor optional executor service to borrow threads from + */ + public AsyncQueueHandler( + BlockingQueue queue, + Handler handler, + String id, + ExecutorService executor) + { + this(queue, handler, id, executor, -1); + } + + /** + * Constructs instance of {@link AsyncQueueHandler} which is capable of + * asynchronous reading provided queue from thread borrowed from executor to + * process items with provided handler. + * @param queue thread-safe queue which holds items to process + * @param handler an implementation of handler routine which will be + * invoked per each item placed in the queue. + * @param id optional identifier of current handler for debug purpose + * @param executor optional executor service to borrow threads from + * @param maxSequentiallyHandledItems maximum number of items sequentially + * handled on thread borrowed from {@link #executor} before temporary + * releasing thread and re-acquiring it from {@link #executor}. + */ + public AsyncQueueHandler( + BlockingQueue queue, + Handler handler, + String id, + ExecutorService executor, + long maxSequentiallyHandledItems) + { + if (queue == null) + { + throw new IllegalArgumentException("queue is null"); + } + + if (handler == null) + { + throw new IllegalArgumentException("handler is null"); + } + + this.executor = executor != null ? executor : sharedExecutor; + this.queue = queue; + this.handler = handler; + this.id = id; + this.maxSequentiallyHandledItems = maxSequentiallyHandledItems; + } + + /** + * Attempts to stop execution of {@link #reader} if running + */ + public void cancel() + { + cancel(true); + } + + /** + * Checks if {@link #reader} is running on one of {@link #executor} + * thread and if no submits execution of {@link #reader} on executor. + */ + public void handleQueueItemsUntilEmpty() + { + synchronized (syncRoot) + { + if (readerFuture == null || readerFuture.isDone()) + { + rescheduleReader(); + } + } + } + + /** + * Invoked when execution of {@link #reader} is about to temporary + * cancel and further execution need to be re-scheduled. + */ + private void onYield() + { + if (logger.isLoggable(java.util.logging.Level.FINE)) + { + logger.fine("Yielding AsyncQueueHandler with ID = " + id); + } + + rescheduleReader(); + } + + /** + * Attempts to cancel currently running reader. + * @param mayInterruptIfRunning indicates if {@link #reader} allowed + * to be interrupted if running + */ + private void cancel(boolean mayInterruptIfRunning) + { + synchronized (syncRoot) + { + running.set(false); + + if (readerFuture != null) + { + readerFuture.cancel(mayInterruptIfRunning); + readerFuture = null; + } + } + } + + /** + * Reschedules execution of {@link #reader} on {@link #executor}'s thread. + */ + private void rescheduleReader() + { + synchronized (syncRoot) + { + running.set(true); + readerFuture = executor.submit(reader); + } + } + + /** + * A simple interface to handle enqueued {@link T} items. + * @param the type of the item. + */ + public interface Handler + { + /** + * Does something with an item. + * @param item the item to do something with. + */ + void handleItem(T item); + } +} diff --git a/src/main/java/org/ice4j/util/PacketQueue.java b/src/main/java/org/ice4j/util/PacketQueue.java index 2e5c56b..880a4bd 100644 --- a/src/main/java/org/ice4j/util/PacketQueue.java +++ b/src/main/java/org/ice4j/util/PacketQueue.java @@ -15,7 +15,6 @@ */ package org.ice4j.util; -import java.util.*; import java.util.concurrent.*; import java.util.logging.Logger; // Disambiguation. @@ -30,6 +29,7 @@ import java.util.logging.Logger; // Disambiguation. *
MultiplexingSocket#received (and the rest of Multiplex* classes). * * @author Boris Grozev + * @author Yura Yaroshevich */ public abstract class PacketQueue { @@ -65,9 +65,11 @@ public abstract class PacketQueue } /** - * The underlying {@link Queue} which holds packets. + * The underlying {@link BlockingQueue} which holds packets. + * Used as synchronization object between {@link #close()}, {@link #get()} + * and {@link #doAdd(Object)}. */ - private final Queue queue; + private final BlockingQueue queue; /** * Whether this {@link PacketQueue} should store the {@code byte[]} or @@ -76,13 +78,6 @@ public abstract class PacketQueue */ private final boolean copy; - /** - * The capacity of this {@link PacketQueue}. If one of the {@code add} - * methods is called while the queue holds this many packets, the first - * packet in the queue will be dropped. - */ - private final int capacity; - /** * The {@link QueueStatistics} instance optionally used to collect and print * detailed statistics about this queue. @@ -90,16 +85,11 @@ public abstract class PacketQueue private final QueueStatistics queueStatistics; /** - * The {@link Thread} optionally used to perpetually read packets from this - * queue and handle them using {@link #handler}. + * The optionally used {@link AsyncQueueHandler} to perpetually read packets + * from {@link #queue} on separate thread and handle them with provided + * packet handler. */ - private final Thread thread; - - /** - * The {@link org.ice4j.util.PacketQueue.PacketHandler} optionally used to - * handle packets read from this queue by {@link #thread}. - */ - private final PacketHandler handler; + private final AsyncQueueHandler asyncQueueHandler; /** * A string used to identify this {@link PacketQueue} for logging purposes. @@ -107,9 +97,10 @@ public abstract class PacketQueue private final String id; /** - * Whether this queue has been closed. + * Whether this queue has been closed. Field is denoted as volatile, + * because it is set in one thread and could be read in while loop in other. */ - private boolean closed = false; + private volatile boolean closed = false; /** * The number of packets which were dropped from this {@link PacketQueue} as @@ -163,9 +154,37 @@ public abstract class PacketQueue public PacketQueue(int capacity, boolean copy, boolean enableStatistics, String id, PacketHandler packetHandler) + { + this(capacity, copy, enableStatistics, id, packetHandler, null); + } + + /** + * Initializes a new {@link PacketQueue} instance. + * @param capacity the capacity of the queue. + * @param copy whether the queue is to store the instances it is given via + * the various {@code add} methods, or create a copy. + * @param enableStatistics whether detailed statistics should be calculated + * and printed. WARNING: this will produce copious output (one line per + * packet added or removed). + * @param id the ID of the packet queue, to be used for logging. + * @param packetHandler An optional handler to be used by the queue for + * packets read from it. If a non-null value is passed the queue will + * start its own thread, which will read packets from the queue and execute + * {@code handler.handlePacket} on them. If set to null, no thread will be + * created, and the queue will provide access to the head element via + * {@link #get()} and {@link #poll()}. + * @param executor An optional executor service to use to execute + * packetHandler for items added to queue. + */ + public PacketQueue( + int capacity, + boolean copy, + boolean enableStatistics, + String id, + PacketHandler packetHandler, + ExecutorService executor) { this.copy = copy; - this.capacity = capacity; this.id = id; queue = new ArrayBlockingQueue<>(capacity); @@ -174,83 +193,21 @@ public abstract class PacketQueue if (packetHandler != null) { - handler = packetHandler; - - thread = new Thread(){ - @Override - public void run() - { - runInReadingThread(); - } - }; - thread.setName(getClass().getName() + "-" + id); - thread.setDaemon(true); - thread.start(); + asyncQueueHandler = new AsyncQueueHandler( + queue, + new HandlerAdapter(packetHandler), + id, + executor, + packetHandler.maxSequentiallyProcessedPackets()); } else { - thread = null; - handler = null; + asyncQueueHandler = null; } logger.fine("Initialized a PacketQueue instance with ID " + id); } - /** - * Perpetually reads packets from this {@link PacketQueue} and uses - * {@link #handler} on each of them. - */ - private void runInReadingThread() - { - if (Thread.currentThread() != thread) - { - logger.warning("runInReadingThread executing in " - + Thread.currentThread()); - return; - } - - if (handler == null) - { - logger.warning("No handler set, the reading thread will be stopped."); - return; - } - - while (!closed) - { - T pkt; - - synchronized (queue) - { - pkt = queue.poll(); - if (pkt == null) - { - try - { - queue.wait(100); - } - catch (InterruptedException ie) - { - } - continue; - } - } - - if (queueStatistics != null) - { - queueStatistics.remove(System.currentTimeMillis()); - } - - try - { - handler.handlePacket(pkt); - } - catch (Exception e) - { - logger.warning("Failed to handle packet: " + e); - } - } - } - /** * Adds a packet represented by a {@code byte[]} with a corresponding * offset and length to this queue. @@ -324,7 +281,7 @@ public abstract class PacketQueue synchronized (queue) { - if (queue.size() >= capacity) + while (!queue.offer(pkt)) { // Drop from the head of the queue. T p = queue.poll(); @@ -346,10 +303,14 @@ public abstract class PacketQueue { queueStatistics.add(System.currentTimeMillis()); } - queue.offer(pkt); queue.notifyAll(); } + + if (asyncQueueHandler != null) + { + asyncQueueHandler.handleQueueItemsUntilEmpty(); + } } /** @@ -360,7 +321,7 @@ public abstract class PacketQueue */ public T get() { - if (handler != null) + if (asyncQueueHandler != null) { // If the queue was configured with a handler, it is running its // own reading thread, and reading from it via this interface would @@ -407,7 +368,7 @@ public abstract class PacketQueue if (closed) return null; - if (handler != null) + if (asyncQueueHandler != null) { // If the queue was configured with a handler, it is running its // own reading thread, and reading from it via this interface would @@ -428,12 +389,23 @@ public abstract class PacketQueue } } + /** + * Closes current PacketQueue instance. No items will be added + * to queue when it's closed. Threads which were blocked in {@link #get()} + * will receive null. Asynchronous queue processing by + * {@link #asyncQueueHandler} is stopped. + */ public void close() { if (!closed) { closed = true; + if (asyncQueueHandler != null) + { + asyncQueueHandler.cancel(); + } + synchronized (queue) { queue.notifyAll(); @@ -481,6 +453,18 @@ public abstract class PacketQueue protected abstract T createPacket( byte[] buf, int off, int len, Object context); + /** + * Releases packet when it is handled by provided packet handler. + * This method is not called when PacketQueue was created without + * handler and hence no automatic queue processing is done. + * Default implementation is empty, but it might be used to implement + * packet pooling to re-use them. + * @param pkt packet to release + */ + protected void releasePacket(T pkt) + { + } + /** * A simple interface to handle packets. * @param the type of the packets. @@ -494,5 +478,62 @@ public abstract class PacketQueue * {@code false} otherwise. */ boolean handlePacket(T pkt); + + /** + * Specifies the number of packets allowed to be processed sequentially + * without yielding control to executor's thread. Specifying positive + * number will allow other possible queues sharing same + * {@link ExecutorService} to process their packets. + * @return positive value to specify max number of packets which allows + * implementation of cooperative multi-tasking between different + * {@link PacketQueue} sharing same {@link ExecutorService}. + */ + default long maxSequentiallyProcessedPackets() + { + return -1; + } + } + + /** + * An adapter class implementing {@link AsyncQueueHandler.Handler} + * to wrap {@link PacketHandler}. + */ + private final class HandlerAdapter implements AsyncQueueHandler.Handler + { + /** + * An actual handler of packets. + */ + private final PacketHandler handler; + + /** + * Constructs adapter of {@link PacketHandler} to + * {@link AsyncQueueHandler.Handler} interface. + * @param handler an handler instance to adapt + */ + HandlerAdapter(PacketHandler handler) + { + this.handler = handler; + } + + /** + * {@inheritDoc} + */ + @Override + public void handleItem(T item) + { + if (queueStatistics != null) + { + queueStatistics.remove(System.currentTimeMillis()); + } + + try + { + handler.handlePacket(item); + } + finally + { + releasePacket(item); + } + } } } diff --git a/src/test/java/org/ice4j/util/DummyQueue.java b/src/test/java/org/ice4j/util/DummyQueue.java new file mode 100644 index 0000000..63d4e91 --- /dev/null +++ b/src/test/java/org/ice4j/util/DummyQueue.java @@ -0,0 +1,62 @@ +package org.ice4j.util; + +import java.util.concurrent.*; + +/** + * Concrete dummy implementation of PacketQueue. Intended for launching tests + * against base implementation of PacketQueue. + * + * @author Yura Yaroshevich + */ +class DummyQueue + extends PacketQueue +{ + DummyQueue( + int capacity, + PacketHandler packetHandler, + ExecutorService executor) + { + super(capacity, false, false, "DummyQueue", packetHandler, + executor); + } + + DummyQueue(int capacity) + { + super(capacity, false, false, "DummyQueue", null, null); + } + + @Override + public byte[] getBuffer(Dummy pkt) + { + return null; + } + + @Override + public int getOffset(Dummy pkt) + { + return 0; + } + + @Override + public int getLength(Dummy pkt) + { + return 0; + } + + @Override + public Object getContext(Dummy pkt) + { + return null; + } + + @Override + protected Dummy createPacket(byte[] buf, int off, int len, + Object context) + { + return new Dummy(); + } + + static class Dummy { + int seed; + } +} diff --git a/src/test/java/org/ice4j/util/PacketQueueBenchmarkTests.java b/src/test/java/org/ice4j/util/PacketQueueBenchmarkTests.java new file mode 100644 index 0000000..a395772 --- /dev/null +++ b/src/test/java/org/ice4j/util/PacketQueueBenchmarkTests.java @@ -0,0 +1,236 @@ +package org.ice4j.util; + +import org.junit.*; + +import java.time.*; +import java.util.*; +import java.util.concurrent.*; +import java.util.logging.Logger; // Disambiguation + +/** + * Contains test for checking performance aspects of various + * PacketQueue configuration + * + * @author Yura Yaroshevich + */ +@Ignore("Check only performance aspect of PacketQueue") +public class PacketQueueBenchmarkTests +{ + /** + * The Logger used by the PacketQueueBenchmarkTests class + * and its instances for logging output. + */ + private static final java.util.logging.Logger logger + = Logger.getLogger(PacketQueueBenchmarkTests.class.getName()); + + /** + * Number of iteration to run benchmark and compute + * average execution time. + */ + private final int numberOfBenchmarkIterations = 100; + + /** + * Simulates number of concurrent existing PacketQueue instances inside + * application. In case of JVB it is linear to number of connected peers + * to JVB instance. + */ + private final int numberOfQueues = 800; + + /** + * Simulates number of messages processed via single PacketQueue + */ + private final int numberOfItemsInQueue = 1000; + + /** + * Simulates the computation "weight" of single item processed by + * PacketQueue + */ + private final int singleQueueItemProcessingWeight = 300; + + @Test + public void testMultiplePacketQueueThroughputWithThreadPerQueue() + throws Exception + { + /* + * This test roughly simulates initial implementation of PacketQueue + * when each PacketQueue instance has it's own processing thread + */ + measureBenchmark("ThreadPerQueuePool", () -> { + final ExecutorService executorService + = Executors.newFixedThreadPool(numberOfQueues); + Duration duration = runBenchmark( + executorService, + -1 /* Disable cooperative multi-tasking mode, + which is not relevant when each queue has it's own processing + thread*/); + executorService.shutdownNow(); + return duration; + }); + } + + @Test + public void testMultiplePacketQueueThroughputWithCachedThreadPerQueue() + throws Exception + { + /* + * This test is slight modification of previous test, but now threads + * are re-used between PacketQueues when possible. + */ + measureBenchmark("CachedThreadPerQueuePool", () -> { + final ExecutorService executorService + = Executors.newCachedThreadPool(); + Duration duration = runBenchmark( + executorService, + -1 /* Disable cooperative multi-tasking mode, + which is not relevant when each queue has it's own processing + thread*/); + executorService.shutdownNow(); + return duration; + }); + } + + @Test + public void testMultiplePacketQueueThroughputWithFixedSizePool() + throws Exception + { + /* + * This test creates pool with limited number of threads, all + * PacketQueues share threads in cooperative multi-tasking mode. + */ + measureBenchmark("FixedSizeCPUBoundPool", () -> { + final ExecutorService executorService + = Executors.newFixedThreadPool( + Runtime.getRuntime().availableProcessors()); + Duration duration = runBenchmark( + executorService, + 50 /* Because queues will share executor + with limited number of threads, so configure cooperative + multi-tasking mode*/); + executorService.shutdownNow(); + return duration; + }); + } + + @Test + public void testMultiplePacketQueueThroughputWithForkJoinPool() + throws Exception + { + /* + * This test check proposed change to PacketQueue implementation when + * all created PacketQueues share single ExecutorService with limited + * number of threads. Execution starvation is resolved by implementing + * cooperative multi-tasking when each PacketQueue release it's thread + * borrowed for ExecutorService so other PacketQueue instances can + * proceed with execution. + * This modification has noticeable better performance when executed + * on system which is already loaded by other concurrent tasks. + */ + measureBenchmark("ForkJoinCPUBoundPool", () -> { + final ExecutorService executorService + = Executors.newWorkStealingPool( + Runtime.getRuntime().availableProcessors()); + Duration duration = runBenchmark( + executorService, + 50 /* Because queues will share executor + with limited number of threads, so configure cooperative + multi-tasking mode*/); + executorService.shutdownNow(); + return duration; + }); + } + + private Duration runBenchmark( + final ExecutorService executor, + final long maxSequentiallyPackets) + throws InterruptedException + { + final CountDownLatch completionGuard + = new CountDownLatch(numberOfItemsInQueue * numberOfQueues); + + final ArrayList queues = new ArrayList<>(); + for (int i = 0; i < numberOfQueues; i++) { + queues.add(new DummyQueue( + numberOfItemsInQueue, + new PacketQueue.PacketHandler() + { + @Override + public boolean handlePacket(DummyQueue.Dummy pkt) + { + double result = 0; + // some dummy computationally exp + final int end + = pkt.seed + singleQueueItemProcessingWeight; + + for (int i = pkt.seed; i < end; i++) + { + result += Math.log(Math.sqrt(i)); + } + completionGuard.countDown(); + return result > 0; + } + + @Override + public long maxSequentiallyProcessedPackets() + { + return maxSequentiallyPackets; + } + }, + executor)); + } + + long startTime = System.nanoTime(); + + for (DummyQueue queue : queues) + { + for (int i = 0; i < numberOfItemsInQueue; i++) + { + queue.add(new DummyQueue.Dummy()); + } + } + + completionGuard.await(); + long endTime = System.nanoTime(); + + for (DummyQueue queue : queues) { + queue.close(); + } + + return Duration.ofNanos(endTime - startTime); + } + + private void measureBenchmark(String name, Callable runWithDuration) throws Exception + { + final ArrayList experimentDuration = new ArrayList<>(); + for (int i = 0; i < 1 + numberOfBenchmarkIterations; i++) + { + System.gc(); + final Duration duration = runWithDuration.call(); + if (i != 0) + { + experimentDuration.add(duration); + } + } + + long totalNanos = 0; + for (Duration duration : experimentDuration) + { + totalNanos += duration.toNanos(); + } + long averageNanos = totalNanos / experimentDuration.size(); + + long sumSquares = 0; + + for (Duration duration : experimentDuration) + { + long diff = Math.abs(duration.toNanos() - averageNanos); + sumSquares = diff * diff; + } + + double stdDev + = Math.sqrt((1.0 / (experimentDuration.size() - 1)) * sumSquares); + + System.out.println(name + + " : avg = " + TimeUnit.NANOSECONDS.toMillis(averageNanos) + " ms" + + ", std_dev = " + TimeUnit.NANOSECONDS.toMillis((long)stdDev) + " ms"); + } +} diff --git a/src/test/java/org/ice4j/util/PacketQueueTests.java b/src/test/java/org/ice4j/util/PacketQueueTests.java new file mode 100644 index 0000000..65c4130 --- /dev/null +++ b/src/test/java/org/ice4j/util/PacketQueueTests.java @@ -0,0 +1,314 @@ +package org.ice4j.util; + +import org.junit.*; + +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.*; +import java.util.function.*; +import java.util.logging.Logger; // Disambiguation + +/** + * Test various aspects of {@link PacketQueue} implementation. + * + * @author Yura Yaroshevich + */ +public class PacketQueueTests +{ + /** + * The Logger used by the PacketQueueTests class and + * its instances for logging output. + */ + private static final java.util.logging.Logger logger + = Logger.getLogger(PacketQueueTests.class.getName()); + + @Test + public void testAddingItemToQueueNotifiesBlockedThreadsImmediately() + throws Exception + { + final DummyQueue dummyQueue = new DummyQueue(10); + final CompletableFuture dummyItem = + CompletableFuture.supplyAsync(dummyQueue::get); + + try + { + // This block surrounded with try/catch necessary to ensure that + // thread calling PacketQueue::get blocked before items is added + // to queue. Giving 50ms for CompletableFuture to stuck on get. + final DummyQueue.Dummy nullDummy + = dummyItem.get(50, TimeUnit.MILLISECONDS); + Assert.fail("There is no items in queue, must not be here"); + } + catch (TimeoutException e) + { + // no item is added during 50 ms into queue. + } + + final DummyQueue.Dummy pushedItem = new DummyQueue.Dummy(); + + dummyQueue.add(pushedItem); + + try + { + // checks that thread stuck in PacketQueue::get notified + // "immediately" when item added to queue. Giving a few ms for + // CompletableFuture to transit to completed state. + final DummyQueue.Dummy poppedItem = dummyItem.get( + 50, TimeUnit.MILLISECONDS); + Assert.assertEquals(pushedItem, poppedItem); + } + catch (TimeoutException e) + { + Assert.fail("Expected that blocked thread notified immediately " + + "about item added to queue"); + } + } + + @Test + public void testClosingQueueImmediatelyNotifiesAllThreadsBlockedOnGet() + throws Exception + { + final DummyQueue dummyQueue = new DummyQueue(10); + final ArrayList> + dummyItems = new ArrayList<>(); + for (int i = 0; i < ForkJoinPool.getCommonPoolParallelism(); i++) + { + dummyItems.add(CompletableFuture.supplyAsync(dummyQueue::get)); + } + + for (CompletableFuture dummyItem : dummyItems) + { + try + { + // This block surrounded with try/catch necessary to ensure that + // thread calling PacketQueue::get blocked before items is added + // to queue. Giving 50ms for CompletableFuture to stuck on get. + final DummyQueue.Dummy nullDummy + = dummyItem.get(50, TimeUnit.MILLISECONDS); + Assert.fail("There is no items in queue, must not be here"); + } + catch (TimeoutException e) + { + // no item is added during 50 ms into queue. + } + } + + dummyQueue.close(); + + for (CompletableFuture dummyItem : dummyItems) + { + try + { + // checks that thread stuck in PacketQueue::get notified + // "immediately" when PacketQueue is stopped. Giving 1 ms for + // CompletableFuture to transit to completed state. + final DummyQueue.Dummy poppedItem = dummyItem.get( + 1, TimeUnit.MILLISECONDS); + Assert.assertNull("When PacketQueue is closed " + + "null must be returned", poppedItem); + } + catch (TimeoutException e) + { + Assert.fail("Expected that blocked thread notified immediately " + + "when queue is stopped"); + } + } + } + + @Test + public void testAddingWhenCapacityReachedRemovesOldestItem() + { + final int capacity = 10; + final DummyQueue dummyQueue = new DummyQueue(capacity); + + for (int i = 0; i < capacity + 1; i++) + { + DummyQueue.Dummy item = new DummyQueue.Dummy(); + item.seed = i; + + dummyQueue.add(item); + } + + for (int i = 0; i < capacity + 1; i++) + { + final DummyQueue.Dummy item = dummyQueue.poll(); + if (i == capacity) + { + Assert.assertNull(item); + } + else + { + Assert.assertNotEquals("Oldest item must be removed when " + + "item exceeding capacity added", 0, item.seed); + } + } + } + + @Test + public void testPacketQueueReaderThreadIsReleasedWhenPacketQueueEmpty() + throws Exception + { + final ExecutorService singleThreadExecutor + = Executors.newSingleThreadExecutor(); + + final CountDownLatch queueCompletion = new CountDownLatch(1); + + final DummyQueue queue = new DummyQueue( + 10, + pkt -> { + queueCompletion.countDown(); + return true; + }, + singleThreadExecutor); + + queue.add(new DummyQueue.Dummy()); + + final boolean completed + = queueCompletion.await(50, TimeUnit.MILLISECONDS); + Assert.assertTrue("Expected all queued items are handled " + + "at this time point", completed); + + Future executorCompletion = singleThreadExecutor.submit(() -> { + // do nothing, just pump Runnable via executor's thread to + // verify it's not stuck + }); + + try + { + executorCompletion.get(10, TimeUnit.MILLISECONDS); + } + catch (TimeoutException e) + { + Assert.fail("Executors thread must be released by PacketQueue " + + "when queue is empty"); + } + + singleThreadExecutor.shutdownNow(); + } + + @Test + public void testPacketQueueCooperativeMultiTaskingWhenSharingExecutor() + throws Exception + { + final int maxSequentiallyProcessedPackets = 1; + + final int queueCapacity = 10 * maxSequentiallyProcessedPackets; + + final CountDownLatch completionGuard + = new CountDownLatch(2 * queueCapacity); + + final ExecutorService singleThreadExecutor + = Executors.newSingleThreadExecutor(); + + final AtomicInteger queue1Counter = new AtomicInteger(); + + final AtomicInteger queue2Counter = new AtomicInteger(); + + final AtomicBoolean queuesEvenlyProcessed + = new AtomicBoolean(true); + + final BiFunction> + newPacketQueue = (AtomicInteger self, AtomicInteger other) -> + new PacketQueue.PacketHandler() + { + @Override + public boolean handlePacket(DummyQueue.Dummy pkt) + { + int diff = Math.abs( + self.incrementAndGet() - other.get()); + + queuesEvenlyProcessed.set(queuesEvenlyProcessed.get() + && diff <= maxSequentiallyProcessedPackets()); + + completionGuard.countDown(); + + return false; + } + + @Override + public long maxSequentiallyProcessedPackets() + { + return maxSequentiallyProcessedPackets; + } + }; + + final DummyQueue queue1 = new DummyQueue( + queueCapacity, + newPacketQueue.apply(queue1Counter, queue2Counter), + singleThreadExecutor); + + final DummyQueue queue2 = new DummyQueue( + queueCapacity, + newPacketQueue.apply(queue2Counter, queue1Counter), + singleThreadExecutor); + + for (int i = 0; i < queueCapacity; i++) + { + queue1.add(new DummyQueue.Dummy()); + queue2.add(new DummyQueue.Dummy()); + } + + final boolean completed + = completionGuard.await(1, TimeUnit.SECONDS); + Assert.assertTrue("Expected all queued items are handled " + + "at this time point", completed); + + Assert.assertTrue( + "Queues sharing same thread with configured cooperative" + + " multi-tasking must yield execution to be processed evenly", + queuesEvenlyProcessed.get()); + + singleThreadExecutor.shutdownNow(); + } + + @Test + public void testManyQueuesCanShareSingleThread() + throws Exception + { + final ExecutorService singleThreadedExecutor + = Executors.newSingleThreadExecutor(); + + final int numberOfQueues = 1_000_000; + + final ArrayList queues = new ArrayList<>(); + + final CountDownLatch completionGuard + = new CountDownLatch(numberOfQueues); + + for (int i = 0; i < numberOfQueues; i++) + { + queues.add(new DummyQueue(1, + pkt -> { + completionGuard.countDown(); + return true; + }, + singleThreadedExecutor)); + } + + final DummyQueue.Dummy dummyPacket = new DummyQueue.Dummy(); + for (DummyQueue queue : queues) + { + // Push item for processing to cause borrowing execution + // thread from ExecutorService + queue.add(dummyPacket); + } + + final boolean completed + = completionGuard.await(1, TimeUnit.SECONDS); + Assert.assertTrue("Expected all queued items are handled " + + "at this time point", completed); + + final List packetReaders + = singleThreadedExecutor.shutdownNow(); + + Assert.assertEquals("Queues must not utilize thread when" + + "there is no work.", 0, packetReaders.size()); + + for (DummyQueue queue : queues) + { + queue.close(); + } + } +} \ No newline at end of file