use classes in jitsi-utils (#188)

* use queuestatistics/ratestatistcs from jitsi-utils

* remove packetqueue (exists in jitsi-utils)

* use executorfactory in jitsi-utils

* use customizablethreadfactory on jitsi-utils

* remove asyncqueuehandler (exists in jitsi-utils)
This commit is contained in:
bbaldino 2019-09-25 08:54:06 -07:00 committed by GitHub
parent e854e4efaf
commit 43a9f282d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 31 additions and 2009 deletions

View File

@ -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 <tt>Agent</tt> could be described as the main class (i.e. the chef
* d'orchestre) of an ICE implementation.

View File

@ -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
{

View File

@ -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 <tt>Connector</tt>s and <tt>MessageProcessingTask</tt> execution and
@ -146,7 +146,7 @@ class NetAccessManager
*/
private final QueueStatistics queueStatistics
= logger.isLoggable(Level.FINEST)
? new QueueStatistics(this.toString())
? new QueueStatistics()
: null;
/**

View File

@ -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.

View File

@ -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.

View File

@ -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<T>
{
/**
* 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 <tt>ExecutorService</tt> to run <tt>AsyncQueueHandler</tt>
* 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 <T>} whose items read on separate thread and
* processed by provided {@link #handler}.
*/
private final BlockingQueue<T> queue;
/**
* The {@link Handler<T>} used to handle items read from
* {@link #queue} by {@link #reader}.
*/
private final Handler<T> 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<T>} 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 <tt>Future</tt> 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<T>} 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<T> queue,
Handler<T> handler,
String id,
ExecutorService executor)
{
this(queue, handler, id, executor, -1);
}
/**
* Constructs instance of {@link AsyncQueueHandler<T>} 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<T> queue,
Handler<T> 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 <T> the type of the item.
*/
public interface Handler<T>
{
/**
* Does something with an item.
* @param item the item to do something with.
*/
void handleItem(T item);
}
}

View File

@ -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;
}
}

View File

@ -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 <tt>threadKeepAliveTime</tt>
* @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 <tt>threadKeepAliveTime</tt>
* @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 <tt>threadKeepAliveTime</tt>
* @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.
// <tt>corePoolSize</tt> for {@link ScheduledThreadPoolExecutor} is
// behaved both as <tt>corePoolSize</tt> and <tt>maxPoolSize</tt>, 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 <tt>corePoolSize</tt> threads, pool will create new thread,
// to execute scheduled task, even though already created threads are
// idle and have no tasks to execute.
// <tt>keepAliveTime</tt> is configurable and specifies timeout before
// idle core thread deleted from pool
// <tt>removeOnCancelPolicy</tt> 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 <tt>corePoolSize</tt> is set to 0 with unlimited pool size
// <tt>maximumPoolSize</tt> 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);
}
}

View File

@ -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):
* <br> RTPConnectorOutputStream.Queue
* <br> PushSourceStreamImpl#readQ
* <br> OutputDataStreamImpl#writeQ
* <br> SinglePortHarvester.MySocket#queue
* <br> MultiplexingSocket#received (and the rest of Multiplex* classes).
*
* @author Boris Grozev
* @author Yura Yaroshevich
*/
public abstract class PacketQueue<T>
{
/**
* 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<T> 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<T> 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<T> 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<T> 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<T>(
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 <tt>PacketQueue</tt> instance. No items will be added
* to queue when it's closed. Threads which were blocked in {@link #get()}
* will receive <tt>null</tt>. 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 <tt>PacketQueue</tt> 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 <T> the type of the packets.
*/
public interface PacketHandler<T>
{
/**
* 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<T>}
* to wrap {@link PacketHandler<T>}.
*/
private final class HandlerAdapter implements AsyncQueueHandler.Handler<T>
{
/**
* An actual handler of packets.
*/
private final PacketHandler<T> handler;
/**
* Constructs adapter of {@link PacketHandler<T>} to
* {@link AsyncQueueHandler.Handler<T>} interface.
* @param handler an handler instance to adapt
*/
HandlerAdapter(PacketHandler<T> 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);
}
}
}
}

View File

@ -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; i<sizes.length; i++)
{
s.append(logHeader).
append(timestamps[i]).append(' ').
append(sizes[i]).append(' ').
append(addRates[i]).append(' ').
append(removeRates[i]).append(' ').
append(totalPacketsAddedHistory[i]).append('\n');
}
logger.fine(s.toString());
}
}

View File

@ -1,146 +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;
/**
* This originally comes from webrtc.org but has been moved here so that it can
* be reused more easily.
*
* webrtc/modules/remote_bitrate_estimator/rate_statistics.cc
* webrtc/modules/remote_bitrate_estimator/rate_statistics.h
*
* @author Lyubomir Marinov
*/
public class RateStatistics
{
/**
* Total count recorded in buckets.
*/
private long accumulatedCount;
/**
* Counters are kept in buckets (circular buffer), with one bucket per
* millisecond.
*/
private final long[] buckets;
/**
* Bucket index of oldest counter recorded in buckets.
*/
private int oldestIndex;
/**
* Oldest time recorded in buckets.
*/
private long oldestTime;
/**
* To convert counts/ms to desired units.
*/
private final float scale;
/**
* Initializes a new {@link RateStatistics} instance with a default scale
* of 8000 (i.e. if the input is in bytes, the result will be in bits per
* second).
* @param windowSizeMs window size in ms for the rate estimation
*/
public RateStatistics(int windowSizeMs)
{
this(windowSizeMs, 8000F);
}
/**
* @param windowSizeMs window size in ms for the rate estimation
* @param scale coefficient to convert counts/ms to desired units. For
* example, if counts represents bytes, use <tt>8*1000</tt> 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;
}
}
}

View File

@ -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.Dummy>
{
DummyQueue(
int capacity,
PacketHandler<Dummy> 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;
}
}

View File

@ -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 <tt>Logger</tt> used by the <tt>PacketQueueBenchmarkTests</tt> 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<DummyQueue> queues = new ArrayList<>();
for (int i = 0; i < numberOfQueues; i++) {
queues.add(new DummyQueue(
numberOfItemsInQueue,
new PacketQueue.PacketHandler<DummyQueue.Dummy>()
{
@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<Duration> runWithDuration) throws Exception
{
final ArrayList<Duration> 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");
}
}

View File

@ -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 <tt>Logger</tt> used by the <tt>PacketQueueTests</tt> 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<DummyQueue.Dummy> 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<CompletableFuture<DummyQueue.Dummy>>
dummyItems = new ArrayList<>();
for (int i = 0; i < ForkJoinPool.getCommonPoolParallelism(); i++)
{
dummyItems.add(CompletableFuture.supplyAsync(dummyQueue::get));
}
for (CompletableFuture<DummyQueue.Dummy> 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<DummyQueue.Dummy> 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<AtomicInteger,
AtomicInteger,
PacketQueue.PacketHandler<DummyQueue.Dummy>>
newPacketQueue = (AtomicInteger self, AtomicInteger other) ->
new PacketQueue.PacketHandler<DummyQueue.Dummy>()
{
@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<DummyQueue> 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<Runnable> 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<DummyQueue.Dummy> 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();
}
}