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
Modifier and TypeFieldDescriptionstatic final int
Considering the node implementation:java.util.LinkedList.Node<E extends Object>
, the overhead is three references. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends T> c) void
clear()
boolean
boolean
containsAll
(Collection<?> c) int
drainTo
(Collection<? super T> c) int
drainTo
(Collection<? super T> c, int maxElements) element()
long
boolean
isEmpty()
iterator()
boolean
boolean
peek()
poll()
void
int
long
remove()
boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) int
size()
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, wait
Methods 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:
put
in interfaceBlockingQueue<T extends Measurable>
- Throws:
InterruptedException
-
take
- Specified by:
take
in interfaceBlockingQueue<T extends Measurable>
- Throws:
InterruptedException
-
offer
- Specified by:
offer
in interfaceBlockingQueue<T extends Measurable>
- Throws:
InterruptedException
-
add
- Specified by:
add
in interfaceBlockingQueue<T extends Measurable>
- Specified by:
add
in interfaceCollection<T extends Measurable>
- Specified by:
add
in interfaceQueue<T extends Measurable>
-
offer
- Specified by:
offer
in interfaceBlockingQueue<T extends Measurable>
- Specified by:
offer
in interfaceQueue<T extends Measurable>
-
remove
- Specified by:
remove
in interfaceQueue<T extends Measurable>
-
poll
- Specified by:
poll
in interfaceQueue<T extends Measurable>
-
element
- Specified by:
element
in interfaceQueue<T extends Measurable>
-
peek
- Specified by:
peek
in interfaceQueue<T extends Measurable>
-
poll
- Specified by:
poll
in interfaceBlockingQueue<T extends Measurable>
- Throws:
InterruptedException
-
remainingCapacity
public int remainingCapacity()- Specified by:
remainingCapacity
in interfaceBlockingQueue<T extends Measurable>
-
remove
- Specified by:
remove
in interfaceBlockingQueue<T extends Measurable>
- Specified by:
remove
in interfaceCollection<T extends Measurable>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<T extends Measurable>
-
addAll
- Specified by:
addAll
in interfaceCollection<T extends Measurable>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<T extends Measurable>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<T extends Measurable>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<T extends Measurable>
-
size
public int size()- Specified by:
size
in interfaceCollection<T extends Measurable>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<T extends Measurable>
-
contains
- Specified by:
contains
in interfaceBlockingQueue<T extends Measurable>
- Specified by:
contains
in interfaceCollection<T extends Measurable>
-
iterator
- Specified by:
iterator
in interfaceCollection<T extends Measurable>
- Specified by:
iterator
in interfaceIterable<T extends Measurable>
-
toArray
- Specified by:
toArray
in interfaceCollection<T extends Measurable>
-
toArray
public <T1> T1[] toArray(T1[] a) - Specified by:
toArray
in interfaceCollection<T extends Measurable>
-
drainTo
- Specified by:
drainTo
in interfaceBlockingQueue<T extends Measurable>
-
drainTo
- Specified by:
drainTo
in interfaceBlockingQueue<T extends Measurable>
-