|
15 | 15 | */
|
16 | 16 | package com.alibaba.csp.sentinel.util;
|
17 | 17 |
|
| 18 | +import java.util.List; |
18 | 19 | import java.util.concurrent.TimeUnit;
|
| 20 | +import java.util.concurrent.atomic.LongAdder; |
| 21 | + |
| 22 | +import com.alibaba.csp.sentinel.log.RecordLog; |
| 23 | +import com.alibaba.csp.sentinel.slots.statistic.base.LeapArray; |
| 24 | +import com.alibaba.csp.sentinel.slots.statistic.base.WindowWrap; |
| 25 | +import com.alibaba.csp.sentinel.util.function.Tuple2; |
19 | 26 |
|
20 | 27 | /**
|
21 |
| - * Provides millisecond-level time of OS. |
| 28 | + * <p>Provides millisecond-level time of OS.</p> |
| 29 | + * <p> |
| 30 | + * Here we should see that not all the time TimeUtil should |
| 31 | + * keep looping 1_000 times every second (Actually about 800/s due to some losses). |
| 32 | + * <pre> |
| 33 | + * * In idle conditions it just acts as System.currentTimeMillis(); |
| 34 | + * * In busy conditions (significantly more than 1_000/s) it keeps loop to reduce costs. |
| 35 | + * </pre> |
| 36 | + * For detail design and proposals please goto |
| 37 | + * <a href="https://github.com/alibaba/Sentinel/issues/1702#issuecomment-692151160">https://github.com/alibaba/Sentinel/issues/1702</a> |
22 | 38 | *
|
23 | 39 | * @author qinan.qn
|
| 40 | + * @author jason |
24 | 41 | */
|
25 |
| -public final class TimeUtil { |
| 42 | +public final class TimeUtil implements Runnable { |
| 43 | + private static final long CHECK_INTERVAL = 3000; |
| 44 | + private static final long HITS_LOWER_BOUNDARY = 800; |
| 45 | + private static final long HITS_UPPER_BOUNDARY = 1200; |
| 46 | + |
| 47 | + public static enum STATE { |
| 48 | + IDLE, |
| 49 | + PREPARE, |
| 50 | + RUNNING; |
| 51 | + } |
| 52 | + |
| 53 | + private static class Statistic { |
| 54 | + private final LongAdder writes = new LongAdder(); |
| 55 | + private final LongAdder reads = new LongAdder(); |
| 56 | + |
| 57 | + public LongAdder getWrites() { |
| 58 | + return writes; |
| 59 | + } |
| 60 | + |
| 61 | + public LongAdder getReads() { |
| 62 | + return reads; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static TimeUtil INSTANCE; |
| 67 | + |
| 68 | + private volatile long currentTimeMillis; |
| 69 | + private volatile STATE state = STATE.IDLE; |
| 70 | + |
| 71 | + private LeapArray<Statistic> statistics; |
26 | 72 |
|
27 |
| - private static volatile long currentTimeMillis; |
| 73 | + /** |
| 74 | + * thread private variables |
| 75 | + */ |
| 76 | + private long lastCheck = 0; |
28 | 77 |
|
29 | 78 | static {
|
30 |
| - currentTimeMillis = System.currentTimeMillis(); |
31 |
| - Thread daemon = new Thread(new Runnable() { |
| 79 | + INSTANCE = new TimeUtil(); |
| 80 | + } |
| 81 | + |
| 82 | + public TimeUtil() { |
| 83 | + this.statistics = new LeapArray<TimeUtil.Statistic>(3, 3000) { |
| 84 | + |
32 | 85 | @Override
|
33 |
| - public void run() { |
34 |
| - while (true) { |
35 |
| - currentTimeMillis = System.currentTimeMillis(); |
36 |
| - try { |
37 |
| - TimeUnit.MILLISECONDS.sleep(1); |
38 |
| - } catch (Throwable e) { |
39 |
| - |
40 |
| - } |
41 |
| - } |
| 86 | + public Statistic newEmptyBucket(long timeMillis) { |
| 87 | + return new Statistic(); |
42 | 88 | }
|
43 |
| - }); |
| 89 | + |
| 90 | + @Override |
| 91 | + protected WindowWrap<Statistic> resetWindowTo(WindowWrap<Statistic> windowWrap, long startTime) { |
| 92 | + Statistic val = windowWrap.value(); |
| 93 | + val.getReads().reset(); |
| 94 | + val.getWrites().reset(); |
| 95 | + windowWrap.resetTo(startTime); |
| 96 | + return windowWrap; |
| 97 | + } |
| 98 | + }; |
| 99 | + this.currentTimeMillis = System.currentTimeMillis(); |
| 100 | + this.lastCheck = this.currentTimeMillis; |
| 101 | + Thread daemon = new Thread(this); |
44 | 102 | daemon.setDaemon(true);
|
45 | 103 | daemon.setName("sentinel-time-tick-thread");
|
46 | 104 | daemon.start();
|
47 | 105 | }
|
48 | 106 |
|
| 107 | + @Override |
| 108 | + public void run() { |
| 109 | + while (true) { |
| 110 | + // Mechanism optimized since 1.8.2 |
| 111 | + this.check(); |
| 112 | + if (this.state == STATE.RUNNING) { |
| 113 | + this.currentTimeMillis = System.currentTimeMillis(); |
| 114 | + this.statistics.currentWindow(this.currentTimeMillis).value().getWrites().increment(); |
| 115 | + try { |
| 116 | + TimeUnit.MILLISECONDS.sleep(1); |
| 117 | + } catch (Throwable e) { |
| 118 | + } |
| 119 | + continue; |
| 120 | + } |
| 121 | + if (this.state == STATE.IDLE) { |
| 122 | + try { |
| 123 | + TimeUnit.MILLISECONDS.sleep(300); |
| 124 | + } catch (Throwable e) { |
| 125 | + } |
| 126 | + continue; |
| 127 | + } |
| 128 | + if (this.state == STATE.PREPARE) { |
| 129 | + RecordLog.debug("TimeUtil switches to RUNNING"); |
| 130 | + this.currentTimeMillis = System.currentTimeMillis(); |
| 131 | + this.state = STATE.RUNNING; |
| 132 | + continue; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Current running state |
| 139 | + * |
| 140 | + * @return |
| 141 | + */ |
| 142 | + public STATE getState() { |
| 143 | + return state; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Current qps statistics (including reads and writes request) |
| 148 | + * excluding current working time window for accurate result. |
| 149 | + * |
| 150 | + * @param now |
| 151 | + * @return |
| 152 | + */ |
| 153 | + public Tuple2<Long, Long> currentQps(long now) { |
| 154 | + List<WindowWrap<Statistic>> list = this.statistics.listAll(); |
| 155 | + long reads = 0; |
| 156 | + long writes = 0; |
| 157 | + int cnt = 0; |
| 158 | + for (WindowWrap<Statistic> windowWrap : list) { |
| 159 | + if (windowWrap.isTimeInWindow(now)) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + cnt++; |
| 163 | + reads += windowWrap.value().getReads().longValue(); |
| 164 | + writes += windowWrap.value().getWrites().longValue(); |
| 165 | + } |
| 166 | + if (cnt < 1) { |
| 167 | + return new Tuple2<Long, Long>(0L, 0L); |
| 168 | + } |
| 169 | + return new Tuple2<Long, Long>(reads / cnt, writes / cnt); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Check and operate the state if necessary. |
| 174 | + * ATTENTION: It's called in daemon thread. |
| 175 | + */ |
| 176 | + private void check() { |
| 177 | + long now = currentTime(true); |
| 178 | + // every period |
| 179 | + if (now - this.lastCheck < CHECK_INTERVAL) { |
| 180 | + return; |
| 181 | + } |
| 182 | + this.lastCheck = now; |
| 183 | + Tuple2<Long, Long> qps = currentQps(now); |
| 184 | + if (this.state == STATE.IDLE && qps.r1 > HITS_UPPER_BOUNDARY) { |
| 185 | + RecordLog.info("TimeUtil switches to PREPARE for better performance, reads={}/s, writes={}/s", qps.r1, qps.r2); |
| 186 | + this.state = STATE.PREPARE; |
| 187 | + } else if (this.state == STATE.RUNNING && qps.r1 < HITS_LOWER_BOUNDARY) { |
| 188 | + RecordLog.info("TimeUtil switches to IDLE due to not enough load, reads={}/s, writes={}/s", qps.r1, qps.r2); |
| 189 | + this.state = STATE.IDLE; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private long currentTime(boolean innerCall) { |
| 194 | + long now = this.currentTimeMillis; |
| 195 | + Statistic val = this.statistics.currentWindow(now).value(); |
| 196 | + if (!innerCall) { |
| 197 | + val.getReads().increment(); |
| 198 | + } |
| 199 | + if (this.state == STATE.IDLE || this.state == STATE.PREPARE) { |
| 200 | + now = System.currentTimeMillis(); |
| 201 | + this.currentTimeMillis = now; |
| 202 | + if (!innerCall) { |
| 203 | + val.getWrites().increment(); |
| 204 | + } |
| 205 | + } |
| 206 | + return now; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Current timestamp in milliseconds. |
| 211 | + * |
| 212 | + * @return |
| 213 | + */ |
| 214 | + public long getTime() { |
| 215 | + return this.currentTime(false); |
| 216 | + } |
| 217 | + |
| 218 | + public static TimeUtil instance() { |
| 219 | + return INSTANCE; |
| 220 | + } |
| 221 | + |
49 | 222 | public static long currentTimeMillis() {
|
50 |
| - return currentTimeMillis; |
| 223 | + return INSTANCE.getTime(); |
51 | 224 | }
|
52 | 225 | }
|
0 commit comments