1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.util.concurrent.BlockingQueue;
22 import java.util.concurrent.PriorityBlockingQueue;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.locks.Condition;
25 import java.util.concurrent.locks.Lock;
26 import java.util.concurrent.locks.ReentrantLock;
27
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29
30
31
32
33
34
35
36
37
38
39
40
41 @InterfaceAudience.Private
42 public class StealJobQueue<T> extends PriorityBlockingQueue<T> {
43
44 private BlockingQueue<T> stealFromQueue;
45
46 private final Lock lock = new ReentrantLock();
47 private final Condition notEmpty = lock.newCondition();
48
49 public StealJobQueue() {
50 this.stealFromQueue = new PriorityBlockingQueue<T>() {
51
52 @Override
53 public boolean offer(T t) {
54 lock.lock();
55 try {
56 notEmpty.signal();
57 return super.offer(t);
58 } finally {
59 lock.unlock();
60 }
61 }
62 };
63 }
64
65 public StealJobQueue(int initCapacity, int stealFromQueueInitCapacity) {
66 super(initCapacity);
67 this.stealFromQueue = new PriorityBlockingQueue<T>(stealFromQueueInitCapacity) {
68
69 @Override
70 public boolean offer(T t) {
71 lock.lock();
72 try {
73 notEmpty.signal();
74 return super.offer(t);
75 } finally {
76 lock.unlock();
77 }
78 }
79 };
80 }
81
82
83
84
85
86 public BlockingQueue<T> getStealFromQueue() {
87 return stealFromQueue;
88 }
89
90 @Override
91 public boolean offer(T t) {
92 lock.lock();
93 try {
94 notEmpty.signal();
95 return super.offer(t);
96 } finally {
97 lock.unlock();
98 }
99 }
100
101
102 @Override
103 public T take() throws InterruptedException {
104 lock.lockInterruptibly();
105 try {
106 while (true) {
107 T retVal = this.poll();
108 if (retVal == null) {
109 retVal = stealFromQueue.poll();
110 }
111 if (retVal == null) {
112 notEmpty.await();
113 } else {
114 return retVal;
115 }
116 }
117 } finally {
118 lock.unlock();
119 }
120 }
121
122 @Override
123 public T poll(long timeout, TimeUnit unit) throws InterruptedException {
124 long nanos = unit.toNanos(timeout);
125 lock.lockInterruptibly();
126 try {
127 while (true) {
128 T retVal = this.poll();
129 if (retVal == null) {
130 retVal = stealFromQueue.poll();
131 }
132 if (retVal == null) {
133 if (nanos <= 0) {
134 return null;
135 }
136 nanos = notEmpty.awaitNanos(nanos);
137 } else {
138 return retVal;
139 }
140 }
141 } finally {
142 lock.unlock();
143 }
144 }
145 }
146