Invoke releasePacket on dropped items in PacketQueue. (#164)

* Invoke releasePacket on dropped items in PacketQueue.

* Fixed comment.

* Use Object.notify instead of Object.notifyAll in PacketQueue.doAdd

* Fixed formatting.

* Use atomic for numDroppedPackets because it is no longer under synchronized block.

* Copy number of dropped packets to local variable.
This commit is contained in:
Yury Yaroshevich 2018-12-17 20:16:16 +03:00 committed by bbaldino
parent c325421927
commit 6beca2903c
4 changed files with 83 additions and 30 deletions

View File

@ -16,6 +16,7 @@
package org.ice4j.util; package org.ice4j.util;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.logging.Logger; // Disambiguation. import java.util.logging.Logger; // Disambiguation.
/** /**
@ -106,7 +107,7 @@ public abstract class PacketQueue<T>
* The number of packets which were dropped from this {@link PacketQueue} as * 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. * a result of a packet being added while the queue is at full capacity.
*/ */
private int numDroppedPackets = 0; private final AtomicInteger numDroppedPackets = new AtomicInteger();
/** /**
* Initializes a new {@link PacketQueue} instance. * Initializes a new {@link PacketQueue} instance.
@ -279,32 +280,39 @@ public abstract class PacketQueue<T>
if (closed) if (closed)
return; 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) synchronized (queue)
{ {
while (!queue.offer(pkt)) // notify single thread because only 1 item was added into queue
{ queue.notify();
// Drop from the head of the queue.
T p = queue.poll();
if (p != null)
{
if (queueStatistics != null)
{
queueStatistics.remove(System.currentTimeMillis());
}
if (logDroppedPacket(++numDroppedPackets))
{
logger.warning(
"Packets dropped (id=" + id + "): " + numDroppedPackets);
}
}
}
if (queueStatistics != null)
{
queueStatistics.add(System.currentTimeMillis());
}
queue.notifyAll();
} }
if (asyncQueueHandler != null) if (asyncQueueHandler != null)
@ -408,6 +416,8 @@ public abstract class PacketQueue<T>
synchronized (queue) synchronized (queue)
{ {
// notify all threads because PacketQueue is closed and all
// threads waiting on queue must stop reading it.
queue.notifyAll(); queue.notifyAll();
} }
} }

View File

@ -57,6 +57,6 @@ class DummyQueue
} }
static class Dummy { static class Dummy {
int seed; int id;
} }
} }

View File

@ -159,9 +159,9 @@ public class PacketQueueBenchmarkTests
double result = 0; double result = 0;
// some dummy computationally exp // some dummy computationally exp
final int end final int end
= pkt.seed + singleQueueItemProcessingWeight; = pkt.id + singleQueueItemProcessingWeight;
for (int i = pkt.seed; i < end; i++) for (int i = pkt.id; i < end; i++)
{ {
result += Math.log(Math.sqrt(i)); result += Math.log(Math.sqrt(i));
} }

View File

@ -124,7 +124,7 @@ public class PacketQueueTests
for (int i = 0; i < capacity + 1; i++) for (int i = 0; i < capacity + 1; i++)
{ {
DummyQueue.Dummy item = new DummyQueue.Dummy(); DummyQueue.Dummy item = new DummyQueue.Dummy();
item.seed = i; item.id = i;
dummyQueue.add(item); dummyQueue.add(item);
} }
@ -139,7 +139,7 @@ public class PacketQueueTests
else else
{ {
Assert.assertNotEquals("Oldest item must be removed when " Assert.assertNotEquals("Oldest item must be removed when "
+ "item exceeding capacity added", 0, item.seed); + "item exceeding capacity added", 0, item.id);
} }
} }
} }
@ -311,4 +311,47 @@ public class PacketQueueTests
queue.close(); 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();
}
} }