HADOOP-14912. FairCallQueue may defer servicing calls. Contributed by Daryn Sharp

This commit is contained in:
Jason Lowe 2017-10-10 14:47:25 -05:00
parent f29e55bf5e
commit 1123f8f0b6

View File

@ -122,13 +122,15 @@ public FairCallQueue(int priorityLevels, int capacity, String ns,
private E removeNextElement() { private E removeNextElement() {
int priority = multiplexer.getAndAdvanceCurrentIndex(); int priority = multiplexer.getAndAdvanceCurrentIndex();
E e = queues.get(priority).poll(); E e = queues.get(priority).poll();
if (e == null) { // a semaphore permit has been acquired, so an element MUST be extracted
// or the semaphore and queued elements will go out of sync. loop to
// avoid race condition if elements are added behind the current position,
// awakening other threads that poll the elements ahead of our position.
while (e == null) {
for (int idx = 0; e == null && idx < queues.size(); idx++) { for (int idx = 0; e == null && idx < queues.size(); idx++) {
e = queues.get(idx).poll(); e = queues.get(idx).poll();
} }
} }
// guaranteed to find an element if caller acquired permit.
assert e != null : "consumer didn't acquire semaphore!";
return e; return e;
} }