|
3 | 3 | import vproxy.base.util.time.TimeElem; |
4 | 4 | import vproxy.base.util.time.TimeQueue; |
5 | 5 |
|
6 | | -import java.util.PriorityQueue; |
| 6 | +import java.util.*; |
7 | 7 |
|
8 | 8 | public class TimeQueueImpl<T> implements TimeQueue<T> { |
9 | | - PriorityQueue<TimeElemImpl<T>> queue = new PriorityQueue<>((a, b) -> (int) (a.triggerTime - b.triggerTime)); |
| 9 | + private static final int TIME_WHEEL_LEVEL = 4; |
| 10 | + private static final int MAX_TIME_WHEEL_INTERVAL = 1 << (TIME_WHEEL_LEVEL * TimeWheel.WHEEL_SIZE_POWER); |
| 11 | + |
| 12 | + private final PriorityQueue<TimeElemImpl<T>> queue = new PriorityQueue<>(Comparator.comparingLong(x -> x.triggerTime)); |
| 13 | + |
| 14 | + private final ArrayList<TimeWheel<T>> timeWheels; |
| 15 | + |
| 16 | + private long lastTickTimestamp; |
| 17 | + |
| 18 | + public TimeQueueImpl() { |
| 19 | + this(System.currentTimeMillis()); |
| 20 | + } |
| 21 | + |
| 22 | + public TimeQueueImpl(long currentTimestamp) { |
| 23 | + this.timeWheels = new ArrayList<>(TIME_WHEEL_LEVEL); |
| 24 | + for (int i = 0; i < TIME_WHEEL_LEVEL; i++) { |
| 25 | + this.timeWheels.add(new TimeWheel<>(1 << (i * TimeWheel.WHEEL_SIZE_POWER), currentTimestamp)); |
| 26 | + } |
| 27 | + this.lastTickTimestamp = currentTimestamp; |
| 28 | + } |
10 | 29 |
|
11 | 30 | @Override |
12 | 31 | public TimeElem<T> add(long currentTimestamp, int timeout, T elem) { |
13 | | - TimeElemImpl<T> event = new TimeElemImpl<>(currentTimestamp + timeout, elem, this); |
14 | | - queue.add(event); |
| 32 | + final TimeElemImpl<T> event = new TimeElemImpl<>(currentTimestamp + timeout, elem, this); |
| 33 | + addTimeElem(event, currentTimestamp); |
15 | 34 | return event; |
16 | 35 | } |
17 | 36 |
|
| 37 | + private void addTimeElem(TimeElemImpl<T> event, long currentTimestamp) { |
| 38 | + long timeout = event.triggerTime - currentTimestamp; |
| 39 | + if (timeout >= MAX_TIME_WHEEL_INTERVAL) { |
| 40 | + queue.add(event); |
| 41 | + } |
| 42 | + // already timeout task put into the lowest time wheel |
| 43 | + else if (timeout <= 0) { |
| 44 | + this.timeWheels.get(0).add(event, currentTimestamp); |
| 45 | + } |
| 46 | + // long timeout task put into queue |
| 47 | + else { |
| 48 | + var index = findTimeWheelIndex(timeout); |
| 49 | + this.timeWheels.get(index).add(event, currentTimestamp); |
| 50 | + } |
| 51 | + } |
| 52 | + |
18 | 53 | @Override |
19 | 54 | public T poll() { |
20 | | - TimeElemImpl<T> elem = queue.poll(); |
21 | | - if (elem == null) |
| 55 | + TimeElem<T> elem = timeWheels.get(0).poll(); |
| 56 | + if (elem == null) { |
22 | 57 | return null; |
23 | | - return elem.elem; |
| 58 | + } |
| 59 | + return elem.get(); |
24 | 60 | } |
25 | 61 |
|
26 | 62 | @Override |
27 | 63 | public boolean isEmpty() { |
28 | | - return queue.isEmpty(); |
| 64 | + for (TimeWheel<T> timeWheel : timeWheels) { |
| 65 | + if (!timeWheel.isEmpty()) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + } |
| 69 | + return true; |
29 | 70 | } |
30 | 71 |
|
31 | 72 | @Override |
32 | 73 | public int nextTime(long currentTimestamp) { |
| 74 | + tickTimeWheel(currentTimestamp); |
| 75 | + for (TimeWheel<T> timeWheel : timeWheels) { |
| 76 | + if (timeWheel.isEmpty()) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + return timeWheel.nextTime(currentTimestamp); |
| 80 | + } |
| 81 | + |
33 | 82 | TimeElemImpl<T> elem = queue.peek(); |
34 | | - if (elem == null) |
| 83 | + if (elem == null) { |
35 | 84 | return Integer.MAX_VALUE; |
| 85 | + } |
36 | 86 | long triggerTime = elem.triggerTime; |
37 | 87 | return Math.max((int) (triggerTime - currentTimestamp), 0); |
38 | 88 | } |
| 89 | + |
| 90 | + private void tickTimeWheel(long currentTimestamp) { |
| 91 | + for (int i = TIME_WHEEL_LEVEL - 1; i > 0; i--) { |
| 92 | + final var wheel = timeWheels.get(i); |
| 93 | + while (wheel.tryTick(currentTimestamp)) { |
| 94 | + final Collection<TimeElemImpl<T>> events = wheel.tick(currentTimestamp); |
| 95 | + for (TimeElemImpl<T> event : events) { |
| 96 | + addTimeElem(event, currentTimestamp); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // move elements from queue to time wheels |
| 102 | + while (!queue.isEmpty()) { |
| 103 | + final TimeElemImpl<T> elem = queue.peek(); |
| 104 | + long timeout = elem.triggerTime - currentTimestamp; |
| 105 | + if (timeout >= MAX_TIME_WHEEL_INTERVAL) { |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + addTimeElem(elem, currentTimestamp); |
| 110 | + queue.poll(); |
| 111 | + } |
| 112 | + |
| 113 | + this.lastTickTimestamp = currentTimestamp; |
| 114 | + } |
| 115 | + |
| 116 | + public void remove(TimeElemImpl<T> elem) { |
| 117 | + long timeout = elem.triggerTime - this.lastTickTimestamp; |
| 118 | + if (timeout >= MAX_TIME_WHEEL_INTERVAL) { |
| 119 | + queue.remove(elem); |
| 120 | + } else { |
| 121 | + timeWheels.get(findTimeWheelIndex(timeout)).remove(elem); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static int findTimeWheelIndex(long timeout) { |
| 126 | + if (timeout <= 0) { |
| 127 | + return 0; |
| 128 | + } |
| 129 | + int bits = 63 - Long.numberOfLeadingZeros(timeout); |
| 130 | + return bits / TimeWheel.WHEEL_SIZE_POWER; |
| 131 | + } |
39 | 132 | } |
0 commit comments