diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java index a80946af..f3469161 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java @@ -18,10 +18,13 @@ package com.alibaba.csp.sentinel.slots.statistic.base; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReferenceArray; +import java.util.concurrent.locks.ReentrantLock; import com.alibaba.csp.sentinel.util.TimeUtil; /** + * Basic data structure for statistic metrics. + * * @param type of data wrapper * @author jialiang.linjl * @author Eric Zhao @@ -32,27 +35,86 @@ public abstract class LeapArray { protected int sampleCount; protected int intervalInMs; - protected AtomicReferenceArray> array; + protected final AtomicReferenceArray> array; + + private final ReentrantLock updateLock = new ReentrantLock(); public LeapArray(int windowLength, int intervalInSec) { this.windowLength = windowLength; - this.sampleCount = intervalInSec * 1000 / windowLength; this.intervalInMs = intervalInSec * 1000; + this.sampleCount = intervalInMs / windowLength; this.array = new AtomicReferenceArray>(sampleCount); } + /** + * Get the window at current timestamp. + * + * @return the window at current timestamp + */ public WindowWrap currentWindow() { return currentWindow(TimeUtil.currentTimeMillis()); } + /** + * Create a new bucket. + * + * @return the new empty bucket + */ + public abstract T newEmptyBucket(); + + /** + * Reset current window to provided start time and reset all counters. + * + * @param startTime the start time of the window + * @param windowWrap current window + * @return new clean window wrap + */ + protected abstract WindowWrap resetWindowTo(WindowWrap windowWrap, long startTime); + /** * Get window at provided timestamp. * * @param time a valid timestamp * @return the window at provided timestamp */ - abstract public WindowWrap currentWindow(long time); + public WindowWrap currentWindow(long time) { + long timeId = time / windowLength; + // Calculate current index. + int idx = (int)(timeId % array.length()); + + // Cut the time to current window start. + time = time - time % windowLength; + + while (true) { + WindowWrap old = array.get(idx); + if (old == null) { + WindowWrap window = new WindowWrap(windowLength, time, newEmptyBucket()); + if (array.compareAndSet(idx, null, window)) { + return window; + } else { + Thread.yield(); + } + } else if (time == old.windowStart()) { + return old; + } else if (time > old.windowStart()) { + if (updateLock.tryLock()) { + try { + // if (old is deprecated) then [LOCK] resetTo currentTime. + return resetWindowTo(old, time); + } finally { + updateLock.unlock(); + } + } else { + Thread.yield(); + } + + } else if (time < old.windowStart()) { + // Cannot go through here. + return new WindowWrap(windowLength, time, newEmptyBucket()); + } + } + } public WindowWrap getPreviousWindow(long time) { long timeId = (time - windowLength) / windowLength; @@ -87,16 +149,12 @@ public abstract class LeapArray { return old.value(); } - AtomicReferenceArray> array() { - return array; - } - private boolean isWindowDeprecated(WindowWrap windowWrap) { return TimeUtil.currentTimeMillis() - windowWrap.windowStart() >= intervalInMs; } public List> list() { - ArrayList> result = new ArrayList>(); + List> result = new ArrayList>(); for (int i = 0; i < array.length(); i++) { WindowWrap windowWrap = array.get(i); @@ -110,7 +168,7 @@ public abstract class LeapArray { } public List values() { - ArrayList result = new ArrayList(); + List result = new ArrayList(); for (int i = 0; i < array.length(); i++) { WindowWrap windowWrap = array.get(i); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/Window.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/MetricBucket.java similarity index 96% rename from sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/Window.java rename to sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/MetricBucket.java index e5a87b04..eb5c75ce 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/Window.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/MetricBucket.java @@ -23,7 +23,7 @@ import com.alibaba.csp.sentinel.Constants; * @author jialiang.linjl * @author Eric Zhao */ -public class Window { +public class MetricBucket { private final LongAdder pass = new LongAdder(); private final LongAdder block = new LongAdder(); @@ -33,7 +33,7 @@ public class Window { private volatile long minRt; - public Window() { + public MetricBucket() { initMinRt(); } @@ -46,7 +46,7 @@ public class Window { * * @return new clean window */ - public Window reset() { + public MetricBucket reset() { pass.reset(); block.reset(); exception.reset(); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java index 433a681d..b7df1efa 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java @@ -20,27 +20,27 @@ import java.util.List; import com.alibaba.csp.sentinel.Constants; import com.alibaba.csp.sentinel.node.metric.MetricNode; -import com.alibaba.csp.sentinel.slots.statistic.base.Window; +import com.alibaba.csp.sentinel.slots.statistic.base.MetricBucket; import com.alibaba.csp.sentinel.slots.statistic.base.WindowWrap; /** - * The basic metric class in Sentinel using a {@link WindowLeapArray} internal. + * The basic metric class in Sentinel using a {@link MetricsLeapArray} internal. * * @author jialiang.linjl * @author Eric Zhao */ public class ArrayMetric implements Metric { - private final WindowLeapArray data; + private final MetricsLeapArray data; public ArrayMetric(int windowLength, int interval) { - this.data = new WindowLeapArray(windowLength, interval); + this.data = new MetricsLeapArray(windowLength, interval); } /** * For unit test. */ - public ArrayMetric(WindowLeapArray array) { + public ArrayMetric(MetricsLeapArray array) { this.data = array; } @@ -49,8 +49,8 @@ public class ArrayMetric implements Metric { data.currentWindow(); long success = 0; - List list = data.values(); - for (Window window : list) { + List list = data.values(); + for (MetricBucket window : list) { success += window.success(); } return success; @@ -61,8 +61,8 @@ public class ArrayMetric implements Metric { data.currentWindow(); long success = 0; - List list = data.values(); - for (Window window : list) { + List list = data.values(); + for (MetricBucket window : list) { if (window.success() > success) { success = window.success(); } @@ -74,8 +74,8 @@ public class ArrayMetric implements Metric { public long exception() { data.currentWindow(); long exception = 0; - List list = data.values(); - for (Window window : list) { + List list = data.values(); + for (MetricBucket window : list) { exception += window.exception(); } return exception; @@ -85,8 +85,8 @@ public class ArrayMetric implements Metric { public long block() { data.currentWindow(); long block = 0; - List list = data.values(); - for (Window window : list) { + List list = data.values(); + for (MetricBucket window : list) { block += window.block(); } return block; @@ -96,9 +96,9 @@ public class ArrayMetric implements Metric { public long pass() { data.currentWindow(); long pass = 0; - List list = data.values(); + List list = data.values(); - for (Window window : list) { + for (MetricBucket window : list) { pass += window.pass(); } return pass; @@ -108,8 +108,8 @@ public class ArrayMetric implements Metric { public long rt() { data.currentWindow(); long rt = 0; - List list = data.values(); - for (Window window : list) { + List list = data.values(); + for (MetricBucket window : list) { rt += window.rt(); } return rt; @@ -119,8 +119,8 @@ public class ArrayMetric implements Metric { public long minRt() { data.currentWindow(); long rt = Constants.TIME_DROP_VALVE; - List list = data.values(); - for (Window window : list) { + List list = data.values(); + for (MetricBucket window : list) { if (window.minRt() < rt) { rt = window.minRt(); } @@ -133,7 +133,7 @@ public class ArrayMetric implements Metric { public List details() { List details = new ArrayList(); data.currentWindow(); - for (WindowWrap window : data.list()) { + for (WindowWrap window : data.list()) { if (window == null) { continue; } @@ -156,38 +156,38 @@ public class ArrayMetric implements Metric { } @Override - public Window[] windows() { + public MetricBucket[] windows() { data.currentWindow(); - return data.values().toArray(new Window[data.values().size()]); + return data.values().toArray(new MetricBucket[data.values().size()]); } @Override public void addException() { - WindowWrap wrap = data.currentWindow(); + WindowWrap wrap = data.currentWindow(); wrap.value().addException(); } @Override public void addBlock() { - WindowWrap wrap = data.currentWindow(); + WindowWrap wrap = data.currentWindow(); wrap.value().addBlock(); } @Override public void addSuccess() { - WindowWrap wrap = data.currentWindow(); + WindowWrap wrap = data.currentWindow(); wrap.value().addSuccess(); } @Override public void addPass() { - WindowWrap wrap = data.currentWindow(); + WindowWrap wrap = data.currentWindow(); wrap.value().addPass(); } @Override public void addRT(long rt) { - WindowWrap wrap = data.currentWindow(); + WindowWrap wrap = data.currentWindow(); wrap.value().addRT(rt); } @@ -196,7 +196,7 @@ public class ArrayMetric implements Metric { data.currentWindow(); StringBuilder sb = new StringBuilder(); sb.append(Thread.currentThread().getId()).append("_"); - for (WindowWrap windowWrap : data.list()) { + for (WindowWrap windowWrap : data.list()) { sb.append(windowWrap.windowStart()).append(":").append(windowWrap.value().pass()).append(":") .append(windowWrap.value().block()); @@ -208,7 +208,7 @@ public class ArrayMetric implements Metric { @Override public long previousWindowBlock() { - WindowWrap wrap = data.currentWindow(); + WindowWrap wrap = data.currentWindow(); wrap = data.getPreviousWindow(); if (wrap == null) { return 0; @@ -218,12 +218,11 @@ public class ArrayMetric implements Metric { @Override public long previousWindowPass() { - WindowWrap wrap = data.currentWindow(); + WindowWrap wrap = data.currentWindow(); wrap = data.getPreviousWindow(); if (wrap == null) { return 0; } return wrap.value().pass(); } - } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/Metric.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/Metric.java index fc56bf9e..3a41e4e6 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/Metric.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/Metric.java @@ -18,7 +18,7 @@ package com.alibaba.csp.sentinel.slots.statistic.metric; import java.util.List; import com.alibaba.csp.sentinel.node.metric.MetricNode; -import com.alibaba.csp.sentinel.slots.statistic.base.Window; +import com.alibaba.csp.sentinel.slots.statistic.base.MetricBucket; /** * Represents a basic structure recording invocation metrics of protected resources. @@ -79,7 +79,7 @@ public interface Metric { * * @return window metric array */ - Window[] windows(); + MetricBucket[] windows(); /** * Increment by one the current exception count. diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/MetricsLeapArray.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/MetricsLeapArray.java new file mode 100755 index 00000000..0c2dcb86 --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/MetricsLeapArray.java @@ -0,0 +1,45 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.slots.statistic.metric; + +import com.alibaba.csp.sentinel.slots.statistic.base.LeapArray; +import com.alibaba.csp.sentinel.slots.statistic.base.MetricBucket; +import com.alibaba.csp.sentinel.slots.statistic.base.WindowWrap; + +/** + * The fundamental data structure for metric statistics in a time window. + * + * @author jialiang.linjl + * @author Eric Zhao + */ +public class MetricsLeapArray extends LeapArray { + + public MetricsLeapArray(int windowLengthInMs, int intervalInSec) { + super(windowLengthInMs, intervalInSec); + } + + @Override + public MetricBucket newEmptyBucket() { + return new MetricBucket(); + } + + @Override + protected WindowWrap resetWindowTo(WindowWrap w, long startTime) { + w.resetTo(startTime); + w.value().reset(); + return w; + } +} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/WindowLeapArray.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/WindowLeapArray.java deleted file mode 100755 index 82a94554..00000000 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/WindowLeapArray.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.csp.sentinel.slots.statistic.metric; - -import java.util.concurrent.locks.ReentrantLock; - -import com.alibaba.csp.sentinel.slots.statistic.base.LeapArray; -import com.alibaba.csp.sentinel.slots.statistic.base.Window; -import com.alibaba.csp.sentinel.slots.statistic.base.WindowWrap; - -/** - * The fundamental data structure for metric statistics in a time window. - * - * @author jialiang.linjl - * @author Eric Zhao - */ -public class WindowLeapArray extends LeapArray { - - public WindowLeapArray(int windowLengthInMs, int intervalInSec) { - super(windowLengthInMs, intervalInSec); - } - - private ReentrantLock addLock = new ReentrantLock(); - - /** - * Reset current window to provided start time and reset all counters. - * - * @param startTime the start time of the window - * @return new clean window wrap - */ - private WindowWrap resetWindowTo(WindowWrap w, long startTime) { - w.resetTo(startTime); - w.value().reset(); - return w; - } - - @Override - public WindowWrap currentWindow(long time) { - long timeId = time / windowLength; - // Calculate current index. - int idx = (int)(timeId % array.length()); - - // Cut the time to current window start. - time = time - time % windowLength; - - while (true) { - WindowWrap old = array.get(idx); - if (old == null) { - WindowWrap window = new WindowWrap(windowLength, time, new Window()); - if (array.compareAndSet(idx, null, window)) { - return window; - } else { - Thread.yield(); - } - } else if (time == old.windowStart()) { - return old; - } else if (time > old.windowStart()) { - if (addLock.tryLock()) { - try { - // if (old is deprecated) then [LOCK] resetTo currentTime. - return resetWindowTo(old, time); - } finally { - addLock.unlock(); - } - } else { - Thread.yield(); - } - - } else if (time < old.windowStart()) { - // Cannot go through here. - return new WindowWrap(windowLength, time, new Window()); - } - } - } -} diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/ArrayMetricTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/ArrayMetricTest.java index 8bfbff4c..02e33732 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/ArrayMetricTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/ArrayMetricTest.java @@ -19,10 +19,10 @@ import java.util.ArrayList; import org.junit.Test; -import com.alibaba.csp.sentinel.slots.statistic.base.Window; +import com.alibaba.csp.sentinel.slots.statistic.base.MetricBucket; import com.alibaba.csp.sentinel.slots.statistic.base.WindowWrap; import com.alibaba.csp.sentinel.slots.statistic.metric.ArrayMetric; -import com.alibaba.csp.sentinel.slots.statistic.metric.WindowLeapArray; +import com.alibaba.csp.sentinel.slots.statistic.metric.MetricsLeapArray; import static org.junit.Assert.*; @@ -40,10 +40,10 @@ public class ArrayMetricTest { @Test public void testOperateArrayMetric() { - WindowLeapArray leapArray = mock(WindowLeapArray.class); - final WindowWrap windowWrap = new WindowWrap(windowLengthInMs, 0, new Window()); + MetricsLeapArray leapArray = mock(MetricsLeapArray.class); + final WindowWrap windowWrap = new WindowWrap(windowLengthInMs, 0, new MetricBucket()); when(leapArray.currentWindow()).thenReturn(windowWrap); - when(leapArray.values()).thenReturn(new ArrayList() {{ add(windowWrap.value()); }}); + when(leapArray.values()).thenReturn(new ArrayList() {{ add(windowWrap.value()); }}); ArrayMetric metric = new ArrayMetric(leapArray); diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/WindowLeapArrayTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/MetricsLeapArrayTest.java similarity index 74% rename from sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/WindowLeapArrayTest.java rename to sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/MetricsLeapArrayTest.java index 63a60c03..735838e4 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/WindowLeapArrayTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/MetricsLeapArrayTest.java @@ -21,30 +21,30 @@ import java.util.List; import java.util.Set; import java.util.concurrent.CountDownLatch; +import com.alibaba.csp.sentinel.slots.statistic.base.MetricBucket; import com.alibaba.csp.sentinel.util.TimeUtil; -import com.alibaba.csp.sentinel.slots.statistic.base.Window; import com.alibaba.csp.sentinel.slots.statistic.base.WindowWrap; -import com.alibaba.csp.sentinel.slots.statistic.metric.WindowLeapArray; +import com.alibaba.csp.sentinel.slots.statistic.metric.MetricsLeapArray; import org.junit.Test; import static org.junit.Assert.*; /** - * Test cases for {@link WindowLeapArray}. + * Test cases for {@link MetricsLeapArray}. * * @author Eric Zhao */ -public class WindowLeapArrayTest { +public class MetricsLeapArrayTest { private final int windowLengthInMs = 1000; private final int intervalInSec = 2; @Test public void testNewWindow() { - WindowLeapArray leapArray = new WindowLeapArray(windowLengthInMs, intervalInSec); + MetricsLeapArray leapArray = new MetricsLeapArray(windowLengthInMs, intervalInSec); long time = TimeUtil.currentTimeMillis(); - WindowWrap window = leapArray.currentWindow(time); + WindowWrap window = leapArray.currentWindow(time); assertEquals(window.windowLength(), windowLengthInMs); assertEquals(window.windowStart(), time - time % windowLengthInMs); @@ -54,11 +54,11 @@ public class WindowLeapArrayTest { @Test public void testLeapArrayWindowStart() { - WindowLeapArray leapArray = new WindowLeapArray(windowLengthInMs, intervalInSec); + MetricsLeapArray leapArray = new MetricsLeapArray(windowLengthInMs, intervalInSec); long firstTime = TimeUtil.currentTimeMillis(); long previousWindowStart = firstTime - firstTime % windowLengthInMs; - WindowWrap window = leapArray.currentWindow(firstTime); + WindowWrap window = leapArray.currentWindow(firstTime); assertEquals(windowLengthInMs, window.windowLength()); assertEquals(previousWindowStart, window.windowStart()); @@ -66,15 +66,15 @@ public class WindowLeapArrayTest { @Test public void testWindowAfterOneInterval() { - WindowLeapArray leapArray = new WindowLeapArray(windowLengthInMs, intervalInSec); + MetricsLeapArray leapArray = new MetricsLeapArray(windowLengthInMs, intervalInSec); long firstTime = TimeUtil.currentTimeMillis(); long previousWindowStart = firstTime - firstTime % windowLengthInMs; - WindowWrap window = leapArray.currentWindow(previousWindowStart); + WindowWrap window = leapArray.currentWindow(previousWindowStart); assertEquals(windowLengthInMs, window.windowLength()); assertEquals(previousWindowStart, window.windowStart()); - Window currentWindow = window.value(); + MetricBucket currentWindow = window.value(); assertNotNull(currentWindow); currentWindow.addPass(); @@ -87,7 +87,7 @@ public class WindowLeapArrayTest { window = leapArray.currentWindow(middleTime); assertEquals(previousWindowStart, window.windowStart()); - Window middleWindow = window.value(); + MetricBucket middleWindow = window.value(); middleWindow.addPass(); assertSame(currentWindow, middleWindow); assertEquals(2L, middleWindow.pass()); @@ -106,18 +106,18 @@ public class WindowLeapArrayTest { @Deprecated public void testWindowDeprecatedRefresh() { - WindowLeapArray leapArray = new WindowLeapArray(windowLengthInMs, intervalInSec); + MetricsLeapArray leapArray = new MetricsLeapArray(windowLengthInMs, intervalInSec); final int len = intervalInSec * 1000 / windowLengthInMs; long firstTime = TimeUtil.currentTimeMillis(); - List> firstIterWindowList = new ArrayList>(len); + List> firstIterWindowList = new ArrayList>(len); for (int i = 0; i < len; i++) { - WindowWrap w = leapArray.currentWindow(firstTime + windowLengthInMs * i); + WindowWrap w = leapArray.currentWindow(firstTime + windowLengthInMs * i); w.value().addPass(); firstIterWindowList.add(i, w); } for (int i = len; i < len * 2; i++) { - WindowWrap w = leapArray.currentWindow(firstTime + windowLengthInMs * i); + WindowWrap w = leapArray.currentWindow(firstTime + windowLengthInMs * i); assertNotSame(w, firstIterWindowList.get(i - len)); } } @@ -126,7 +126,7 @@ public class WindowLeapArrayTest { public void testMultiThreadUpdateEmptyWindow() throws Exception { final long time = TimeUtil.currentTimeMillis(); final int nThreads = 16; - final WindowLeapArray leapArray = new WindowLeapArray(windowLengthInMs, intervalInSec); + final MetricsLeapArray leapArray = new MetricsLeapArray(windowLengthInMs, intervalInSec); final CountDownLatch latch = new CountDownLatch(nThreads); Runnable task = new Runnable() { @Override @@ -147,9 +147,9 @@ public class WindowLeapArrayTest { @Test public void testGetPreviousWindow() { - WindowLeapArray leapArray = new WindowLeapArray(windowLengthInMs, intervalInSec); + MetricsLeapArray leapArray = new MetricsLeapArray(windowLengthInMs, intervalInSec); long time = TimeUtil.currentTimeMillis(); - WindowWrap previousWindow = leapArray.currentWindow(time); + WindowWrap previousWindow = leapArray.currentWindow(time); assertNull(leapArray.getPreviousWindow(time)); long nextTime = time + windowLengthInMs; @@ -165,16 +165,16 @@ public class WindowLeapArrayTest { final int intervalInSec = 1; final int intervalInMs = intervalInSec * 1000; - WindowLeapArray leapArray = new WindowLeapArray(windowLengthInMs, intervalInSec); + MetricsLeapArray leapArray = new MetricsLeapArray(windowLengthInMs, intervalInSec); long time = TimeUtil.currentTimeMillis(); - Set> windowWraps = new HashSet>(); + Set> windowWraps = new HashSet>(); windowWraps.add(leapArray.currentWindow(time)); windowWraps.add(leapArray.currentWindow(time + windowLengthInMs)); - List> list = leapArray.list(); - for (WindowWrap wrap : list) { + List> list = leapArray.list(); + for (WindowWrap wrap : list) { assertTrue(windowWraps.contains(wrap)); } @@ -191,18 +191,18 @@ public class WindowLeapArrayTest { final int windowLengthInMs = 100; final int intervalInSec = 1; - WindowLeapArray leapArray = new WindowLeapArray(windowLengthInMs, intervalInSec); + MetricsLeapArray leapArray = new MetricsLeapArray(windowLengthInMs, intervalInSec); long time = TimeUtil.currentTimeMillis(); - Set> windowWraps = new HashSet>(); + Set> windowWraps = new HashSet>(); windowWraps.add(leapArray.currentWindow(time)); windowWraps.add(leapArray.currentWindow(time + windowLengthInMs)); Thread.sleep(intervalInSec * 1000 + windowLengthInMs * 3); - List> list = leapArray.list(); - for (WindowWrap wrap : list) { + List> list = leapArray.list(); + for (WindowWrap wrap : list) { assertTrue(windowWraps.contains(wrap)); } diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/WarmUpFlowTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/WarmUpFlowTest.java index 6cda853d..953b66cf 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/WarmUpFlowTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/WarmUpFlowTest.java @@ -39,12 +39,6 @@ public class WarmUpFlowTest { FlowRuleManager.loadRules(Arrays.asList(flowRule)); - //ContextUtil.enter(null); - - //when(flowRule.selectNodeByRequsterAndStrategy(null, null, null)).thenReturn(value) - - // flowRule.passCheck(null, DefaultNode, acquireCount, args) - // when(leapArray.values()).thenReturn(new ArrayList() {{ add(windowWrap.value()); }}); ContextUtil.enter("test"); ContextUtil.exit();