1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable { Iterator var8; ProcessorSlotEntryCallback handler; try { this.fireEntry(context, resourceWrapper, node, count, prioritized, args); node.increaseThreadNum(); node.addPassRequest(count); if (context.getCurEntry().getOriginNode() != null) { context.getCurEntry().getOriginNode().increaseThreadNum(); context.getCurEntry().getOriginNode().addPassRequest(count); }
if (resourceWrapper.getEntryType() == EntryType.IN) { Constants.ENTRY_NODE.increaseThreadNum(); Constants.ENTRY_NODE.addPassRequest(count); }
Iterator var13 = StatisticSlotCallbackRegistry.getEntryCallbacks().iterator();
while(var13.hasNext()) { ProcessorSlotEntryCallback<DefaultNode> handler = (ProcessorSlotEntryCallback)var13.next(); handler.onPass(context, resourceWrapper, node, count, args); } } catch (PriorityWaitException var10) { node.increaseThreadNum(); if (context.getCurEntry().getOriginNode() != null) { context.getCurEntry().getOriginNode().increaseThreadNum(); }
if (resourceWrapper.getEntryType() == EntryType.IN) { Constants.ENTRY_NODE.increaseThreadNum(); }
var8 = StatisticSlotCallbackRegistry.getEntryCallbacks().iterator();
while(var8.hasNext()) { handler = (ProcessorSlotEntryCallback)var8.next(); handler.onPass(context, resourceWrapper, node, count, args); } } catch (BlockException var11) { BlockException e = var11; context.getCurEntry().setError(var11); node.increaseBlockQps(count); if (context.getCurEntry().getOriginNode() != null) { context.getCurEntry().getOriginNode().increaseBlockQps(count); }
if (resourceWrapper.getEntryType() == EntryType.IN) { Constants.ENTRY_NODE.increaseBlockQps(count); }
var8 = StatisticSlotCallbackRegistry.getEntryCallbacks().iterator();
while(var8.hasNext()) { handler = (ProcessorSlotEntryCallback)var8.next(); handler.onBlocked(e, context, resourceWrapper, node, count, args); }
throw e; } catch (Throwable var12) { context.getCurEntry().setError(var12); node.increaseExceptionQps(count); if (context.getCurEntry().getOriginNode() != null) { context.getCurEntry().getOriginNode().increaseExceptionQps(count); }
if (resourceWrapper.getEntryType() == EntryType.IN) { Constants.ENTRY_NODE.increaseExceptionQps(count); }
throw var12; }
}
public void addPassRequest(int count) { super.addPassRequest(count); this.clusterNode.addPassRequest(count); }
public void addPassRequest(int count) { this.rollingCounterInSecond.addPass(count); this.rollingCounterInMinute.addPass(count); } private transient volatile Metric rollingCounterInSecond; private transient Metric rollingCounterInMinute; private LongAdder curThreadNum; private long lastFetchTime;
public StatisticNode() { this.rollingCounterInSecond = new ArrayMetric(SampleCountProperty.SAMPLE_COUNT, IntervalProperty.INTERVAL); this.rollingCounterInMinute = new ArrayMetric(60, 60000, false); this.curThreadNum = new LongAdder(); this.lastFetchTime = -1L; } 看下ArrayMetric public ArrayMetric(int sampleCount, int intervalInMs) { this.data = new OccupiableBucketLeapArray(sampleCount, intervalInMs); } public OccupiableBucketLeapArray(int sampleCount, int intervalInMs) { super(sampleCount, intervalInMs); this.borrowArray = new FutureBucketLeapArray(sampleCount, intervalInMs); } public LeapArray(int sampleCount, int intervalInMs) { AssertUtil.isTrue(sampleCount > 0, "bucket count is invalid: " + sampleCount); AssertUtil.isTrue(intervalInMs > 0, "total time interval of the sliding window should be positive"); AssertUtil.isTrue(intervalInMs % sampleCount == 0, "time span needs to be evenly divided"); this.windowLengthInMs = intervalInMs / sampleCount; this.intervalInMs = intervalInMs; this.sampleCount = sampleCount; this.array = new AtomicReferenceArray(sampleCount); }
public void addSuccess(int count) { WindowWrap<MetricBucket> wrap = this.data.currentWindow(); ((MetricBucket)wrap.value()).addSuccess(count); } public WindowWrap<T> currentWindow() { return this.currentWindow(TimeUtil.currentTimeMillis()); } public WindowWrap<T> currentWindow(long timeMillis) { if (timeMillis < 0L) { return null; } else { int idx = this.calculateTimeIdx(timeMillis); long windowStart = this.calculateWindowStart(timeMillis);
while(true) { while(true) { WindowWrap<T> old = (WindowWrap)this.array.get(idx); WindowWrap window; if (old == null) { window = new WindowWrap((long)this.windowLengthInMs, windowStart, this.newEmptyBucket(timeMillis)); if (this.array.compareAndSet(idx, (Object)null, window)) { return window; }
Thread.yield(); } else { if (windowStart == old.windowStart()) { return old; } if (windowStart > old.windowStart()) { if (this.updateLock.tryLock()) { try { window = this.resetWindowTo(old, windowStart); } finally { this.updateLock.unlock(); }
return window; }
Thread.yield(); } else if (windowStart < old.windowStart()) { return new WindowWrap((long)this.windowLengthInMs, windowStart, this.newEmptyBucket(timeMillis)); } } } } } } private int calculateTimeIdx(long timeMillis) { long timeId = timeMillis / (long)this.windowLengthInMs; return (int)(timeId % (long)this.array.length()); } protected long calculateWindowStart(long timeMillis) { return timeMillis - timeMillis % (long)this.windowLengthInMs; } 通过对当前时间获取到当前时间对应的时间窗口的开始时间,来判断是要开始一个新的窗口还是使用旧窗口,然后统计当前窗口的请求,用于FlowSlot的限流判断依据
|