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;