diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java
index 29236a3..9a646a0 100644
--- a/src/main/java/org/ice4j/ice/Agent.java
+++ b/src/main/java/org/ice4j/ice/Agent.java
@@ -17,6 +17,15 @@
*/
package org.ice4j.ice;
+import org.ice4j.*;
+import org.ice4j.ice.harvest.*;
+import org.ice4j.stack.*;
+import org.ice4j.util.PeriodicRunnable;
+import org.jitsi.utils.collections.*;
+import org.jitsi.utils.concurrent.*;
+import org.jitsi.utils.logging2.Logger;
+import org.jitsi.utils.logging2.*;
+
import java.beans.*;
import java.io.*;
import java.math.*;
@@ -27,14 +36,6 @@ import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;
-import org.ice4j.*;
-import org.ice4j.ice.harvest.*;
-import org.ice4j.stack.*;
-import org.ice4j.util.*;
-import org.jitsi.utils.collections.*;
-import org.jitsi.utils.logging2.*;
-import org.jitsi.utils.logging2.Logger;
-
/**
* An Agent could be described as the main class (i.e. the chef
* d'orchestre) of an ICE implementation.
diff --git a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java
index 07de0d1..07e5bff 100644
--- a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java
+++ b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java
@@ -20,7 +20,7 @@ package org.ice4j.ice.harvest;
import org.ice4j.*;
import org.ice4j.attribute.*;
import org.ice4j.message.*;
-import org.ice4j.util.*;
+import org.jitsi.utils.queue.*;
import java.io.*;
import java.net.*;
@@ -456,8 +456,7 @@ public abstract class AbstractUdpListener
this.remoteAddress = remoteAddress;
if (logger.isLoggable(Level.FINEST))
{
- queueStatistics = new QueueStatistics(
- "SinglePort" + remoteAddress.toString().replace('/', '-'));
+ queueStatistics = new QueueStatistics();
}
else
{
diff --git a/src/main/java/org/ice4j/stack/NetAccessManager.java b/src/main/java/org/ice4j/stack/NetAccessManager.java
index 5747a6f..15988b0 100644
--- a/src/main/java/org/ice4j/stack/NetAccessManager.java
+++ b/src/main/java/org/ice4j/stack/NetAccessManager.java
@@ -17,6 +17,12 @@
*/
package org.ice4j.stack;
+import org.ice4j.*;
+import org.ice4j.message.*;
+import org.ice4j.socket.*;
+import org.jitsi.utils.concurrent.*;
+import org.jitsi.utils.queue.*;
+
import java.io.*;
import java.net.*;
import java.util.*;
@@ -24,12 +30,6 @@ import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.function.*;
import java.util.logging.*;
-import java.util.logging.Logger; // Disambiguation
-
-import org.ice4j.*;
-import org.ice4j.message.*;
-import org.ice4j.socket.*;
-import org.ice4j.util.*;
/**
* Manages Connectors and MessageProcessingTask execution and
@@ -146,7 +146,7 @@ class NetAccessManager
*/
private final QueueStatistics queueStatistics
= logger.isLoggable(Level.FINEST)
- ? new QueueStatistics(this.toString())
+ ? new QueueStatistics()
: null;
/**
diff --git a/src/main/java/org/ice4j/stack/StunClientTransaction.java b/src/main/java/org/ice4j/stack/StunClientTransaction.java
index 77828f8..d0c56ca 100644
--- a/src/main/java/org/ice4j/stack/StunClientTransaction.java
+++ b/src/main/java/org/ice4j/stack/StunClientTransaction.java
@@ -17,16 +17,17 @@
*/
package org.ice4j.stack;
+import org.ice4j.*;
+import org.ice4j.message.*;
+import org.ice4j.util.PeriodicRunnable;
+import org.jitsi.utils.concurrent.*;
+
import java.io.*;
import java.time.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.logging.*;
-import org.ice4j.*;
-import org.ice4j.message.*;
-import org.ice4j.util.*;
-
/**
* The {@code StunClientTransaction} class retransmits requests as specified by
* RFC 3489.
diff --git a/src/main/java/org/ice4j/stack/StunStack.java b/src/main/java/org/ice4j/stack/StunStack.java
index cd29741..fc332c7 100644
--- a/src/main/java/org/ice4j/stack/StunStack.java
+++ b/src/main/java/org/ice4j/stack/StunStack.java
@@ -17,6 +17,14 @@
*/
package org.ice4j.stack;
+import org.ice4j.*;
+import org.ice4j.attribute.*;
+import org.ice4j.message.*;
+import org.ice4j.security.*;
+import org.ice4j.socket.*;
+import org.jitsi.utils.concurrent.*;
+
+import javax.crypto.*;
import java.io.*;
import java.net.*;
import java.security.*;
@@ -24,15 +32,6 @@ import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;
-import javax.crypto.*;
-
-import org.ice4j.*;
-import org.ice4j.attribute.*;
-import org.ice4j.message.*;
-import org.ice4j.security.*;
-import org.ice4j.socket.*;
-import org.ice4j.util.*;
-
/**
* The entry point to the Stun4J stack. The class is used to start, stop and
* configure the stack.
diff --git a/src/main/java/org/ice4j/util/AsyncQueueHandler.java b/src/main/java/org/ice4j/util/AsyncQueueHandler.java
deleted file mode 100644
index 95ff248..0000000
--- a/src/main/java/org/ice4j/util/AsyncQueueHandler.java
+++ /dev/null
@@ -1,285 +0,0 @@
-/*
- * 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/CustomizableThreadFactory.java b/src/main/java/org/ice4j/util/CustomizableThreadFactory.java
deleted file mode 100644
index 3d2b913..0000000
--- a/src/main/java/org/ice4j/util/CustomizableThreadFactory.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
- *
- * Copyright @ 2018 Jitsi.org
- *
- * 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.*;
-
-/**
- * A thread factory which supports customizing name prefix of created threads
- * and if produced threads are daemons or not.
- */
-public final class CustomizableThreadFactory implements ThreadFactory
-{
- private final ThreadFactory defaultThreadFactory
- = Executors.defaultThreadFactory();
-
- private final String threadNamePrefix;
-
- private final boolean isDaemon;
-
- public CustomizableThreadFactory(String threadNamePrefix, boolean isDaemon)
- {
- this.threadNamePrefix = threadNamePrefix;
- this.isDaemon = isDaemon;
- }
-
- @Override
- public Thread newThread(Runnable r)
- {
- Thread thread = this.defaultThreadFactory.newThread(r);
- if (this.threadNamePrefix != null && !threadNamePrefix.isEmpty())
- {
- thread.setName(this.threadNamePrefix + thread.getName());
- }
- if (thread.isDaemon() != this.isDaemon)
- {
- thread.setDaemon(this.isDaemon);
- }
- return thread;
- }
-}
diff --git a/src/main/java/org/ice4j/util/ExecutorFactory.java b/src/main/java/org/ice4j/util/ExecutorFactory.java
deleted file mode 100644
index 41528a9..0000000
--- a/src/main/java/org/ice4j/util/ExecutorFactory.java
+++ /dev/null
@@ -1,156 +0,0 @@
-/*
- * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
- *
- * Copyright @ 2018 Jitsi.org
- *
- * 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.*;
-
-/**
- * Helper class which contains functions to create pre-configured executors
- *
- * @author Yura Yaroshevich
- */
-public class ExecutorFactory
-{
- /**
- * Create {@link ScheduledExecutorService} with single executor thread
- * @param threadNamePrefix - name prefix for threads created by pool
- * @param threadKeepAliveTime - keep alive time before idle thread is freed
- * @param timeUnit - time unit of threadKeepAliveTime
- * @return pre-configured {@link ScheduledExecutorService}
- */
- public static ScheduledExecutorService createSingleThreadScheduledExecutor(
- String threadNamePrefix,
- int threadKeepAliveTime,
- TimeUnit timeUnit)
- {
- return createScheduledExecutor(
- 1,
- threadNamePrefix,
- threadKeepAliveTime,
- timeUnit);
- }
-
- /**
- * Create {@link ScheduledExecutorService} with number of threads up to
- * number of CPU cores on machine
- * @param threadNamePrefix - name prefix for threads created by pool
- * @param threadKeepAliveTime - keep alive time before idle thread is freed
- * @param timeUnit - time unit of threadKeepAliveTime
- * @return pre-configured {@link ScheduledExecutorService}
- */
- public static ScheduledExecutorService createCPUBoundScheduledExecutor(
- String threadNamePrefix,
- int threadKeepAliveTime,
- TimeUnit timeUnit)
- {
- return createScheduledExecutor(
- Runtime.getRuntime().availableProcessors(),
- threadNamePrefix,
- threadKeepAliveTime,
- timeUnit);
- }
- /**
- * Creates pre-configured {@link ScheduledExecutorService} instance with
- * defaults suitable for ice4j
- * @param threadNamePrefix - name prefix for threads created by pool
- * @param poolSize - max number of threads to keep in pool.
- * @param threadKeepAliveTime - keep alive time before idle thread is freed
- * @param timeUnit - time unit of threadKeepAliveTime
- * @return pre-configured {@link ScheduledExecutorService}
- */
- private static ScheduledExecutorService createScheduledExecutor(
- int poolSize,
- String threadNamePrefix,
- int threadKeepAliveTime,
- TimeUnit timeUnit)
- {
- CustomizableThreadFactory threadFactory
- = new CustomizableThreadFactory(threadNamePrefix, true);
-
- // Motivation:
- // The desired behaviour from pool is that it creates limited number
- // of threads based on current load, as well as releasing threads when
- // there is no work to execute.
- // Based on these requirements the following default configuration is
- // chosen.
- // corePoolSize for {@link ScheduledThreadPoolExecutor} is
- // behaved both as corePoolSize and maxPoolSize, so
- // it is actually fixed-size pool.
- // Even so spec says that corePoolSize is number of
- // threads to keep in pool they are actually created on demand, so if
- // there is no load, then threads are not created. But until pool has
- // less than corePoolSize threads, pool will create new thread,
- // to execute scheduled task, even though already created threads are
- // idle and have no tasks to execute.
- // keepAliveTime is configurable and specifies timeout before
- // idle core thread deleted from pool
- // removeOnCancelPolicy is set to true, to immediately remove
- // queued task from pool queue, because some task might be scheduled
- // with very big delay causing having reference to creator from pool
- //
- // Having corePoolSize is set to 0 with unlimited pool size
- // maximumPoolSize is observed to create only 1 thread in pool
- // no matter how many task are queued and become eligible to execute.
- final ScheduledThreadPoolExecutor executor
- = new ScheduledThreadPoolExecutor(poolSize, threadFactory);
- executor.setKeepAliveTime(threadKeepAliveTime, timeUnit);
- executor.allowCoreThreadTimeOut(true);
- executor.setRemoveOnCancelPolicy(true);
- return executor;
- }
-
- /**
- * Creates a {@link ExecutorService} with limited number of threads which
- * are released after idle timeout.
- *
- * @param threadsLimit - numbers of threads in pool
- * @param threadNamePrefix - name prefix for threads created by pool
- * @return pre-configured {@link ExecutorService}
- */
- public static ExecutorService createFixedThreadPool(
- int threadsLimit,
- String threadNamePrefix)
- {
- final CustomizableThreadFactory threadFactory
- = new CustomizableThreadFactory(threadNamePrefix, true);
-
- final ThreadPoolExecutor executor = new ThreadPoolExecutor(
- threadsLimit, threadsLimit, 60L, TimeUnit.SECONDS,
- new LinkedBlockingDeque<>(), threadFactory);
- executor.allowCoreThreadTimeOut(true);
-
- return executor;
- }
-
- /**
- * Creates an {@link ExecutorService} with an unlimited number of threads
- * which are released after idle timeout.
- *
- * @param threadNamePrefix - name prefix for threads created by pool
- * @return pre-configured {@link ExecutorService}
- */
- public static ExecutorService createCachedThreadPool(
- String threadNamePrefix)
- {
- final CustomizableThreadFactory threadFactory
- = new CustomizableThreadFactory(threadNamePrefix, true);
-
- return Executors.newCachedThreadPool(threadFactory);
- }
-}
diff --git a/src/main/java/org/ice4j/util/PacketQueue.java b/src/main/java/org/ice4j/util/PacketQueue.java
deleted file mode 100644
index 3b7e3fc..0000000
--- a/src/main/java/org/ice4j/util/PacketQueue.java
+++ /dev/null
@@ -1,549 +0,0 @@
-/*
- * Copyright @ 2015 Atlassian Pty Ltd
- *
- * 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.
-
-/**
- * An abstract queue of packets. This is meant to eventually be able to be used
- * in the following classes (in ice4j and libjitsi) in place of their ad-hoc
- * queue implementations (which is the reason the class is parameterized):
- *
RTPConnectorOutputStream.Queue
- *
PushSourceStreamImpl#readQ
- *
OutputDataStreamImpl#writeQ
- *
SinglePortHarvester.MySocket#queue
- *
MultiplexingSocket#received (and the rest of Multiplex* classes).
- *
- * @author Boris Grozev
- * @author Yura Yaroshevich
- */
-public abstract class PacketQueue
-{
- /**
- * The {@link Logger} used by the {@link PacketQueue} class and its
- * instances for logging output.
- */
- private static final Logger logger
- = Logger.getLogger(PacketQueue.class.getName());
-
- /**
- * The default capacity of a {@link PacketQueue}.
- */
- private final static int DEFAULT_CAPACITY = 256;
-
- /**
- * The capacity of the {@code byte[]} cache, if it is enabled.
- */
- private final static int CACHE_CAPACITY = 100;
-
- /**
- * Returns true if a warning should be logged after a queue has dropped
- * {@code numDroppedPackets} packets.
- * @param numDroppedPackets the number of dropped packets.
- * @return {@code true} if a warning should be logged.
- */
- public static boolean logDroppedPacket(int numDroppedPackets)
- {
- return
- numDroppedPackets == 1 ||
- (numDroppedPackets <= 1000 && numDroppedPackets % 100 == 0) ||
- numDroppedPackets % 1000 == 0;
- }
-
- /**
- * The underlying {@link BlockingQueue} which holds packets.
- * Used as synchronization object between {@link #close()}, {@link #get()}
- * and {@link #doAdd(Object)}.
- */
- private final BlockingQueue queue;
-
- /**
- * Whether this {@link PacketQueue} should store the {@code byte[]} or
- * {@code T} instances added to it via one of the {@code add} methods (if
- * {@code false}), or create and store a copy (if {@code true}).
- */
- private final boolean copy;
-
- /**
- * The {@link QueueStatistics} instance optionally used to collect and print
- * detailed statistics about this queue.
- */
- private final QueueStatistics queueStatistics;
-
- /**
- * The optionally used {@link AsyncQueueHandler} to perpetually read packets
- * from {@link #queue} on separate thread and handle them with provided
- * packet handler.
- */
- private final AsyncQueueHandler asyncQueueHandler;
-
- /**
- * A string used to identify this {@link PacketQueue} for logging purposes.
- */
- private final String id;
-
- /**
- * 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 volatile boolean closed = false;
-
- /**
- * The number of packets which were dropped from this {@link PacketQueue} as
- * a result of a packet being added while the queue is at full capacity.
- */
- private final AtomicInteger numDroppedPackets = new AtomicInteger();
-
- /**
- * Initializes a new {@link PacketQueue} instance.
- */
- public PacketQueue()
- {
- this(false, "PacketQueue", null);
- }
-
- /**
- * Initializes a new {@link PacketQueue} instance.
- * @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()}.
- */
- public PacketQueue(
- boolean enableStatistics, String id, PacketHandler packetHandler)
- {
- this(DEFAULT_CAPACITY, true, enableStatistics, id, packetHandler);
- }
-
- /**
- * 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()}.
- */
- 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.id = id;
- queue = new ArrayBlockingQueue<>(capacity);
-
- queueStatistics
- = enableStatistics ? new QueueStatistics(id) : null;
-
- if (packetHandler != null)
- {
- asyncQueueHandler = new AsyncQueueHandler(
- queue,
- new HandlerAdapter(packetHandler),
- id,
- executor,
- packetHandler.maxSequentiallyProcessedPackets());
- }
- else
- {
- asyncQueueHandler = null;
- }
-
- logger.fine("Initialized a PacketQueue instance with ID " + id);
- }
-
- /**
- * Adds a packet represented by a {@code byte[]} with a corresponding
- * offset and length to this queue.
- * @param buf the {@code byte[]} to add.
- * @param off the offset into {@code byte[]} where data begins.
- * @param len the length of the data.
- */
- public void add(byte[] buf, int off, int len)
- {
- add(buf, off, len, null);
- }
-
- /**
- * Adds a packet represented by a {@code byte[]} with a corresponding
- * offset and length, and a context object to this queue.
- * @param buf the {@code byte[]} to add.
- * @param off the offset into {@code byte[]} where data begins.
- * @param len the length of the data.
- * @param context an object which will be added to the queue as part of the
- * packet.
- */
- public void add(byte[] buf, int off, int len, Object context)
- {
- if (copy)
- {
- byte[] newBuf = getByteArray(len);
- System.arraycopy(buf, off, newBuf, 0, len);
- doAdd(createPacket(newBuf, 0, len, context));
- }
- else
- {
- doAdd(createPacket(buf, off, len, context));
- }
- }
-
- /**
- * Adds a specific packet ({@code T}) instance to the queue.
- * @param pkt the packet to add.
- */
- public void add(T pkt)
- {
- if (copy)
- {
- // create a new instance
- add(getBuffer(pkt), getOffset(pkt), getLength(pkt), getContext(pkt));
- }
- else
- {
- doAdd(pkt);
- }
- }
-
- /**
- * Get an unused {@link byte[]} instance with length at least {@code len}.
- * @param len the minimum length of the returned instance.
- * @return a {@link byte[]} instance with length at least {@code len}.
- */
- private byte[] getByteArray(int len)
- {
- return new byte[len];
- }
-
- /**
- * Adds a specific packet ({@code T}) instance to the queue.
- * @param pkt the packet to add.
- */
- private void doAdd(T pkt)
- {
- if (closed)
- return;
-
- while (!queue.offer(pkt))
- {
- // Drop from the head of the queue.
- T p = queue.poll();
- if (p != null)
- {
- if (queueStatistics != null)
- {
- queueStatistics.remove(System.currentTimeMillis());
- }
- final int numDroppedPackets =
- this.numDroppedPackets.incrementAndGet();
- if (logDroppedPacket(numDroppedPackets))
- {
- logger.warning(
- "Packets dropped (id=" + id + "): " + numDroppedPackets);
- }
-
- // Call release on dropped packet to allow proper implementation
- // of object pooling by PacketQueue users
- releasePacket(p);
- }
- }
-
- if (queueStatistics != null)
- {
- queueStatistics.add(System.currentTimeMillis());
- }
-
- synchronized (queue)
- {
- // notify single thread because only 1 item was added into queue
- queue.notify();
- }
-
- if (asyncQueueHandler != null)
- {
- asyncQueueHandler.handleQueueItemsUntilEmpty();
- }
- }
-
- /**
- * Removes and returns the packet ({@code T}) at the head of this queue.
- * Blocks until there is a packet in the queue. Returns {@code null} if
- * the queue is closed or gets closed while waiting for a packet to be added.
- * @return the packet at the head of this queue.
- */
- public T get()
- {
- 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
- // not provide consistent results.
- throw new IllegalStateException(
- "Trying to read from a queue with a configured handler.");
- }
-
- while (true)
- {
- if (closed)
- return null;
- synchronized (queue)
- {
- T pkt = queue.poll();
- if (pkt != null)
- {
- if (queueStatistics != null)
- {
- queueStatistics.remove(System.currentTimeMillis());
- }
- return pkt;
- }
-
- try
- {
- queue.wait();
- }
- catch (InterruptedException ie)
- {}
- }
- }
- }
-
- /**
- * Removes and returns the packet ({@code T}) at the head of this queue, if
- * the queue is non-empty. If the queue is closed or empty, returns null
- * without blocking.
- * @return the packet at the head of this queue, or null if the queue is
- * empty.
- */
- public T poll()
- {
- if (closed)
- return 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
- // not provide consistent results.
- throw new IllegalStateException(
- "Trying to read from a queue with a configured handler.");
- }
-
- synchronized (queue)
- {
- T pkt = queue.poll();
- if (pkt != null && queueStatistics != null)
- {
- queueStatistics.remove(System.currentTimeMillis());
- }
-
- return pkt;
- }
- }
-
- /**
- * 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)
- {
- // notify all threads because PacketQueue is closed and all
- // threads waiting on queue must stop reading it.
- queue.notifyAll();
- }
- }
- }
-
- /**
- * Extracts the underlying {@code byte[]} from a packet.
- * @param pkt the packet to get the {@code byte[]} from.
- * @return the underlying {@code byte[]} of {@code pkt}.
- */
- public abstract byte[] getBuffer(T pkt);
-
- /**
- * Extracts the offset of a packet.
- * @param pkt the packet to get the offset of.
- * @return the offset of {@code pkt}.
- */
- public abstract int getOffset(T pkt);
-
- /**
- * Extracts the length of a packet.
- * @param pkt the packet to get the length of.
- * @return the length of {@code pkt}.
- */
- public abstract int getLength(T pkt);
-
- /**
- * Extracts the context of a packet.
- * @param pkt the packet to get the context of.
- * @return the context of {@code pkt}.
- */
- public abstract Object getContext(T pkt);
-
- /**
- * Creates a new packet ({@link T} instance) with the given {@code byte[]},
- * offset, length and context.
- * @param buf the {@code byte[]} of the new instance.
- * @param off the offset of the new instance.
- * @param len the length of the new instance.
- * @param context the context of the new instance.
- * @return a new packet ({@link T} instance).
- */
- 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.
- */
- public interface PacketHandler
- {
- /**
- * Does something with a packet.
- * @param pkt the packet to do something with.
- * @return {@code true} if the operation was successful, and
- * {@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/main/java/org/ice4j/util/QueueStatistics.java b/src/main/java/org/ice4j/util/QueueStatistics.java
deleted file mode 100644
index d110541..0000000
--- a/src/main/java/org/ice4j/util/QueueStatistics.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * Copyright @ 2015 Atlassian Pty Ltd
- *
- * 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.logging.*;
-import java.util.logging.Logger; // Disambiguation.
-
-public class QueueStatistics
-{
- /**
- * The scale to use for {@link RateStatistics}. This makes their output in
- * units (e.g. packets) per second.
- */
- private static final int SCALE = 1000;
-
- /**
- * The interval (in number of calls to {@link #add(long)} or
- * {@link #remove(long)}) at which the gathered statistics will be printed.
- */
- private static final int DEFAULT_PRINT_INTERVAL = 500;
-
- /**
- * Calculate the average rate of addition of packets in a 200ms window.
- */
- private final RateStatistics addRateStatistics = new RateStatistics(200, SCALE);
-
- /**
- * Calculate the average rate of removal of packets in a 200ms window.
- */
- private final RateStatistics removeRateStatistics = new RateStatistics(200, SCALE);
-
- /**
- * The {@link Logger} instance used for logging output.
- */
- private final Logger logger;
-
- private int head = 0;
- private int size = 0;
- private int[] sizes = new int[DEFAULT_PRINT_INTERVAL];
- private long[] timestamps = new long[DEFAULT_PRINT_INTERVAL];
- private long[] addRates = new long[DEFAULT_PRINT_INTERVAL];
- private long[] removeRates = new long[DEFAULT_PRINT_INTERVAL];
- private int[] totalPacketsAddedHistory = new int[DEFAULT_PRINT_INTERVAL];
- private int totalPacketsAdded = 0;
- private String logHeader;
-
- /**
- * Initializes a new {@link QueueStatistics} instance.
- *
- * @param id Identifier to distinguish the log output of multiple
- * {@link QueueStatistics} instances.
- */
- public QueueStatistics(String id)
- {
- logger = Logger.getLogger("QueueStatistics-" + id);
- logHeader = "QueueStatistics-" + id + ": ";
-
- // We let the users of this class decide whether to enable logging (by
- // creating a QueueStatistic instance) or not.
- logger.setLevel(Level.ALL);
- }
-
- /**
- * Registers the addition of a packet to the corresponding queue.
- * @param now the time (in milliseconds since the epoch) at which the
- * packet was added.
- */
- public synchronized void add(long now)
- {
- addRateStatistics.update(1, now);
- size++;
- totalPacketsAdded++;
- update(now);
- }
-
- /**
- * Registers the removal of a packet from the corresponding queue.
- * @param now the time (in milliseconds since the epoch) at which the
- * packet was removed.
- */
- public synchronized void remove(long now)
- {
- removeRateStatistics.update(1, now);
- size--;
- update(now);
- }
-
- private synchronized void update(long now)
- {
- if (head == sizes.length)
- {
- print();
- head = 0;
- }
-
- sizes[head] = size;
- timestamps[head] = now;
- addRates[head] = addRateStatistics.getRate(now);
- removeRates[head] = removeRateStatistics.getRate(now);
- totalPacketsAddedHistory[head] = totalPacketsAdded;
- head++;
- }
-
- private void print()
- {
- StringBuilder s = new StringBuilder();
- for (int i =0; i8*1000 to go to bits/s.
- */
- public RateStatistics(int windowSizeMs, float scale)
- {
- buckets = new long[windowSizeMs + 1]; // N ms in (N+1) buckets.
- this.scale = scale / (buckets.length - 1);
- }
-
- private synchronized void eraseOld(long nowMs)
- {
- long newOldestTime = nowMs - buckets.length + 1;
-
- if (newOldestTime <= oldestTime)
- return;
-
- while (oldestTime < newOldestTime)
- {
- long countInOldestBucket = buckets[oldestIndex];
-
- accumulatedCount -= countInOldestBucket;
- buckets[oldestIndex] = 0L;
- if (++oldestIndex >= buckets.length)
- {
- oldestIndex = 0;
- }
- ++oldestTime;
- if (accumulatedCount == 0L)
- {
- // This guarantees we go through all the buckets at most once,
- // even if newOldestTime is far greater than oldestTime.
- break;
- }
- }
- oldestTime = newOldestTime;
- }
-
- public long getRate()
- {
- return getRate(System.currentTimeMillis());
- }
-
- public long getRate(long nowMs)
- {
- eraseOld(nowMs);
- return (long) (accumulatedCount * scale + 0.5F);
- }
-
- public long getAccumulatedCount()
- {
- return getAccumulatedCount(System.currentTimeMillis());
- }
-
- public long getAccumulatedCount(long nowMs)
- {
- eraseOld(nowMs);
- return accumulatedCount;
- }
-
-
- public void update(int count, long nowMs)
- {
- if (nowMs < oldestTime) // Too old data is ignored.
- return;
-
- synchronized (this)
- {
- eraseOld(nowMs);
-
- int nowOffset = (int) (nowMs - oldestTime);
- int index = oldestIndex + nowOffset;
-
- if (index >= buckets.length)
- index -= buckets.length;
- buckets[index] += count;
- accumulatedCount += count;
- }
- }
-}
diff --git a/src/test/java/org/ice4j/util/DummyQueue.java b/src/test/java/org/ice4j/util/DummyQueue.java
deleted file mode 100644
index f1694dc..0000000
--- a/src/test/java/org/ice4j/util/DummyQueue.java
+++ /dev/null
@@ -1,62 +0,0 @@
-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 id;
- }
-}
diff --git a/src/test/java/org/ice4j/util/PacketQueueBenchmarkTests.java b/src/test/java/org/ice4j/util/PacketQueueBenchmarkTests.java
deleted file mode 100644
index 8b74c75..0000000
--- a/src/test/java/org/ice4j/util/PacketQueueBenchmarkTests.java
+++ /dev/null
@@ -1,236 +0,0 @@
-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.id + singleQueueItemProcessingWeight;
-
- for (int i = pkt.id; 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
deleted file mode 100644
index 35586ae..0000000
--- a/src/test/java/org/ice4j/util/PacketQueueTests.java
+++ /dev/null
@@ -1,357 +0,0 @@
-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.id = 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.id);
- }
- }
- }
-
- @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();
- }
- }
-
- @Test
- public void testReleasePacketCalledForPacketsPoppedDueToQueueOverflow()
- throws Exception
- {
-
- final ExecutorService singleThreadedExecutor
- = Executors.newSingleThreadExecutor();
-
- final List releasedPackets = new ArrayList<>();
-
- final int queueCapacity = 1;
-
- final DummyQueue queue = new DummyQueue(queueCapacity)
- {
- @Override
- protected void releasePacket(Dummy pkt)
- {
- releasedPackets.add(pkt);
- }
- };
-
- final int itemsToEnqueue = 10;
-
- for (int i = 0; i < itemsToEnqueue; i++)
- {
- final DummyQueue.Dummy dummy = new DummyQueue.Dummy();
- dummy.id = i + 1;
- queue.add(dummy);
- }
-
- Assert.assertEquals(
- itemsToEnqueue - queueCapacity, releasedPackets.size());
-
- int seed = 1;
- for (DummyQueue.Dummy releasedPacket : releasedPackets)
- {
- Assert.assertEquals(seed, releasedPacket.id);
- seed++;
- }
-
- singleThreadedExecutor.shutdown();
- }
-}
\ No newline at end of file