Class MemoryBoundBlockingQueue<T extends Measurable>
java.lang.Object
com.linkedin.venice.utils.collections.MemoryBoundBlockingQueue<T>
- Type Parameters:
T-
- All Implemented Interfaces:
Iterable<T>,Collection<T>,BlockingQueue<T>,Queue<T>
public class MemoryBoundBlockingQueue<T extends Measurable>
extends Object
implements BlockingQueue<T>
This class is a generic implementation of a memory bound blocking queue.
This blocking queue is bounded by the memory usage of each
Measurable object buffered inside.
To guarantee some kind of fairness, you need to choose suitable notifyDeltaInByte
according to the max size of messages, which could be buffered.
The reason behind this design:
Considering some thread could put various sizes of messages into the shared queue, MemoryBoundBlockingQueue
won't notify the waiting thread (the 'put' thread)
right away when some message gets processed until the freed memory hit the follow config: notifyDeltaInByte.
The reason behind this design:
When the buffered queue is full, and the processing thread keeps processing small message, the bigger message won't
have chance to get queued into the buffer since the memory freed by the processed small message is not enough to
fit the bigger message.
With this delta config, MemoryBoundBlockingQueue will guarantee some kind of fairness
among various sizes of messages when buffered queue is full.
When tuning this config, we need to consider the following tradeoffs:
1. notifyDeltaInByte must be smaller than memoryCapacityInByte;
2. If the delta is too big, it will waste some buffer space since it won't notify the waiting threads even there
are some memory available (less than the delta);
3. If the delta is too small, the big message may not be able to get chance to be buffered when the queue is full;-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intConsidering the node implementation:java.util.LinkedList.Node<E extends Object>, the overhead is three references. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends T> c) voidclear()booleanbooleancontainsAll(Collection<?> c) intdrainTo(Collection<? super T> c) intdrainTo(Collection<? super T> c, int maxElements) element()longbooleanisEmpty()iterator()booleanbooleanpeek()poll()voidintlongremove()booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()take()Object[]toArray()<T1> T1[]toArray(T1[] a) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
LINKED_LIST_NODE_SHALLOW_OVERHEAD
public static final int LINKED_LIST_NODE_SHALLOW_OVERHEADConsidering the node implementation:java.util.LinkedList.Node<E extends Object>, the overhead is three references.
-
-
Constructor Details
-
MemoryBoundBlockingQueue
public MemoryBoundBlockingQueue(long memoryCapacityInByte, long notifyDeltaInByte)
-
-
Method Details
-
getMemoryUsage
public long getMemoryUsage() -
remainingMemoryCapacityInByte
public long remainingMemoryCapacityInByte() -
put
- Specified by:
putin interfaceBlockingQueue<T extends Measurable>- Throws:
InterruptedException
-
take
- Specified by:
takein interfaceBlockingQueue<T extends Measurable>- Throws:
InterruptedException
-
offer
- Specified by:
offerin interfaceBlockingQueue<T extends Measurable>- Throws:
InterruptedException
-
add
- Specified by:
addin interfaceBlockingQueue<T extends Measurable>- Specified by:
addin interfaceCollection<T extends Measurable>- Specified by:
addin interfaceQueue<T extends Measurable>
-
offer
- Specified by:
offerin interfaceBlockingQueue<T extends Measurable>- Specified by:
offerin interfaceQueue<T extends Measurable>
-
remove
- Specified by:
removein interfaceQueue<T extends Measurable>
-
poll
- Specified by:
pollin interfaceQueue<T extends Measurable>
-
element
- Specified by:
elementin interfaceQueue<T extends Measurable>
-
peek
- Specified by:
peekin interfaceQueue<T extends Measurable>
-
poll
- Specified by:
pollin interfaceBlockingQueue<T extends Measurable>- Throws:
InterruptedException
-
remainingCapacity
public int remainingCapacity()- Specified by:
remainingCapacityin interfaceBlockingQueue<T extends Measurable>
-
remove
- Specified by:
removein interfaceBlockingQueue<T extends Measurable>- Specified by:
removein interfaceCollection<T extends Measurable>
-
containsAll
- Specified by:
containsAllin interfaceCollection<T extends Measurable>
-
addAll
- Specified by:
addAllin interfaceCollection<T extends Measurable>
-
removeAll
- Specified by:
removeAllin interfaceCollection<T extends Measurable>
-
retainAll
- Specified by:
retainAllin interfaceCollection<T extends Measurable>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<T extends Measurable>
-
size
public int size()- Specified by:
sizein interfaceCollection<T extends Measurable>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<T extends Measurable>
-
contains
- Specified by:
containsin interfaceBlockingQueue<T extends Measurable>- Specified by:
containsin interfaceCollection<T extends Measurable>
-
iterator
- Specified by:
iteratorin interfaceCollection<T extends Measurable>- Specified by:
iteratorin interfaceIterable<T extends Measurable>
-
toArray
- Specified by:
toArrayin interfaceCollection<T extends Measurable>
-
toArray
public <T1> T1[] toArray(T1[] a) - Specified by:
toArrayin interfaceCollection<T extends Measurable>
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<T extends Measurable>
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<T extends Measurable>
-