From 4b1ccd93e2a439352e04e7975c9368398bc0c0e9 Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Wed, 13 Mar 2019 22:10:20 +0800 Subject: [PATCH] Refactor Node interface: change return type of `xxxQps` method from long to double (#564) - Update test cases (use assertEquals(e, a, delta) instead) - Also add `totalPass` method in Node interface Signed-off-by: Eric Zhao --- ...uxSentinelOperatorTestIntegrationTest.java | 2 +- .../SentinelWebFluxIntegrationTest.java | 4 +- .../adapter/servlet/CommonFilterTest.java | 6 +- .../servletmethod/CommonFilterMethodTest.java | 10 ++- .../csp/sentinel/node/DefaultNode.java | 6 +- .../csp/sentinel/node/EntranceNode.java | 30 +++++--- .../com/alibaba/csp/sentinel/node/Node.java | 72 +++++++++++++------ .../csp/sentinel/node/StatisticNode.java | 35 +++++---- .../slots/block/degrade/DegradeRule.java | 2 +- .../flow/controller/WarmUpController.java | 4 +- .../WarmUpRateLimiterController.java | 18 ++--- .../slots/statistic/metric/DebugSupport.java | 28 ++++++++ .../csp/sentinel/node/ClusterNodeTest.java | 6 +- .../csp/sentinel/node/StatisticNodeTest.java | 8 +-- .../slots/block/degrade/DegradeTest.java | 10 +-- .../flow/FlowPartialIntegrationTest.java | 13 ++++ .../slots/block/flow/FlowRuleTest.java | 23 +++--- .../controller/DefaultControllerTest.java | 2 +- .../flow/controller/WarmUpControllerTest.java | 14 ++-- .../WarmUpRateLimiterControllerTest.java | 10 +-- .../csp/sentinel/command/vo/NodeVo.java | 24 +++---- 21 files changed, 200 insertions(+), 127 deletions(-) create mode 100644 sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/DebugSupport.java diff --git a/sentinel-adapter/sentinel-reactor-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/reactor/FluxSentinelOperatorTestIntegrationTest.java b/sentinel-adapter/sentinel-reactor-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/reactor/FluxSentinelOperatorTestIntegrationTest.java index 7b144760..ba0fd9e4 100644 --- a/sentinel-adapter/sentinel-reactor-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/reactor/FluxSentinelOperatorTestIntegrationTest.java +++ b/sentinel-adapter/sentinel-reactor-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/reactor/FluxSentinelOperatorTestIntegrationTest.java @@ -45,7 +45,7 @@ public class FluxSentinelOperatorTestIntegrationTest { ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName); assertNotNull(cn); - assertEquals(1, cn.passQps()); + assertEquals(1, cn.passQps(), 0.01); assertEquals(1, cn.totalException()); } diff --git a/sentinel-adapter/sentinel-spring-webflux-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webflux/SentinelWebFluxIntegrationTest.java b/sentinel-adapter/sentinel-spring-webflux-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webflux/SentinelWebFluxIntegrationTest.java index d9934969..91d7b923 100644 --- a/sentinel-adapter/sentinel-spring-webflux-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webflux/SentinelWebFluxIntegrationTest.java +++ b/sentinel-adapter/sentinel-spring-webflux-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/spring/webflux/SentinelWebFluxIntegrationTest.java @@ -68,7 +68,7 @@ public class SentinelWebFluxIntegrationTest { ClusterNode cn = ClusterBuilderSlot.getClusterNode(url); assertNotNull(cn); - assertEquals(1, cn.passQps()); + assertEquals(1, cn.passQps(), 0.01); } @Test @@ -94,7 +94,7 @@ public class SentinelWebFluxIntegrationTest { .expectBody(String.class).isEqualTo("Hello 2"); ClusterNode cn = ClusterBuilderSlot.getClusterNode(fooPrefix + "*"); - assertEquals(2, cn.passQps()); + assertEquals(2, cn.passQps(), 0.01); assertNull(ClusterBuilderSlot.getClusterNode(url1)); assertNull(ClusterBuilderSlot.getClusterNode(url2)); diff --git a/sentinel-adapter/sentinel-web-servlet/src/test/java/com/alibaba/csp/sentinel/adapter/servlet/CommonFilterTest.java b/sentinel-adapter/sentinel-web-servlet/src/test/java/com/alibaba/csp/sentinel/adapter/servlet/CommonFilterTest.java index 14e8b2a1..bd42a89b 100644 --- a/sentinel-adapter/sentinel-web-servlet/src/test/java/com/alibaba/csp/sentinel/adapter/servlet/CommonFilterTest.java +++ b/sentinel-adapter/sentinel-web-servlet/src/test/java/com/alibaba/csp/sentinel/adapter/servlet/CommonFilterTest.java @@ -83,7 +83,7 @@ public class CommonFilterTest { ClusterNode cn = ClusterBuilderSlot.getClusterNode(url); assertNotNull(cn); - assertEquals(1, cn.passQps()); + assertEquals(1, cn.passQps(), 0.01); testCommonBlockAndRedirectBlockPage(url, cn); @@ -99,7 +99,7 @@ public class CommonFilterTest { this.mvc.perform(get(url).accept(MediaType.TEXT_PLAIN)) .andExpect(status().isOk()) .andExpect(content().string(FilterUtil.DEFAULT_BLOCK_MSG)); - assertEquals(1, cn.blockQps()); + assertEquals(1, cn.blockQps(), 0.01); // Test for redirect. String redirectUrl = "http://some-location.com"; @@ -132,7 +132,7 @@ public class CommonFilterTest { .andExpect(status().isOk()) .andExpect(content().string("Hello 2")); ClusterNode cn = ClusterBuilderSlot.getClusterNode(fooPrefix + "*"); - assertEquals(2, cn.passQps()); + assertEquals(2, cn.passQps(), 0.01); assertNull(ClusterBuilderSlot.getClusterNode(url1)); assertNull(ClusterBuilderSlot.getClusterNode(url2)); diff --git a/sentinel-adapter/sentinel-web-servlet/src/test/java/com/alibaba/csp/sentinel/adapter/servletmethod/CommonFilterMethodTest.java b/sentinel-adapter/sentinel-web-servlet/src/test/java/com/alibaba/csp/sentinel/adapter/servletmethod/CommonFilterMethodTest.java index 3df4c7d5..5d9a006a 100644 --- a/sentinel-adapter/sentinel-web-servlet/src/test/java/com/alibaba/csp/sentinel/adapter/servletmethod/CommonFilterMethodTest.java +++ b/sentinel-adapter/sentinel-web-servlet/src/test/java/com/alibaba/csp/sentinel/adapter/servletmethod/CommonFilterMethodTest.java @@ -86,7 +86,7 @@ public class CommonFilterMethodTest { ClusterNode cnGet = ClusterBuilderSlot.getClusterNode(GET + COLON + url); assertNotNull(cnGet); - assertEquals(1, cnGet.passQps()); + assertEquals(1, cnGet.passQps(), 0.01); ClusterNode cnPost = ClusterBuilderSlot.getClusterNode(POST + COLON + url); @@ -98,10 +98,9 @@ public class CommonFilterMethodTest { cnPost = ClusterBuilderSlot.getClusterNode(POST + COLON + url); assertNotNull(cnPost); - assertEquals(1, cnPost.passQps()); + assertEquals(1, cnPost.passQps(), 0.01); testCommonBlockAndRedirectBlockPage(url, cnGet, cnPost); - } private void testCommonBlockAndRedirectBlockPage(String url, ClusterNode cnGet, ClusterNode cnPost) throws Exception { @@ -110,21 +109,20 @@ public class CommonFilterMethodTest { this.mvc.perform(get(url).accept(MediaType.TEXT_PLAIN)) .andExpect(status().isOk()) .andExpect(content().string(FilterUtil.DEFAULT_BLOCK_MSG)); - assertEquals(1, cnGet.blockQps()); + assertEquals(1, cnGet.blockQps(), 0.01); // Test for post pass this.mvc.perform(post(url)) .andExpect(status().isOk()) .andExpect(content().string(HELLO_POST_STR)); - assertEquals(2, cnPost.passQps()); + assertEquals(2, cnPost.passQps(), 0.01); FlowRuleManager.loadRules(null); WebServletConfig.setBlockPage(""); } - @After public void cleanUp() { FlowRuleManager.loadRules(null); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/DefaultNode.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/DefaultNode.java index 2ef5fc4c..537ab687 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/DefaultNode.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/DefaultNode.java @@ -48,7 +48,7 @@ public class DefaultNode extends StatisticNode { /** * The list of all child nodes. */ - private volatile Set childList = new HashSet(); + private volatile Set childList = new HashSet<>(); /** * Associated cluster node. @@ -85,7 +85,7 @@ public class DefaultNode extends StatisticNode { if (!childList.contains(node)) { synchronized (this) { if (!childList.contains(node)) { - Set newSet = new HashSet(childList.size() + 1); + Set newSet = new HashSet<>(childList.size() + 1); newSet.addAll(childList); newSet.add(node); childList = newSet; @@ -99,7 +99,7 @@ public class DefaultNode extends StatisticNode { * Reset the child node list. */ public void removeChildList() { - this.childList = new HashSet(); + this.childList = new HashSet<>(); } public Set getChildList() { diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/EntranceNode.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/EntranceNode.java index 518e4482..1872e864 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/EntranceNode.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/EntranceNode.java @@ -43,9 +43,9 @@ public class EntranceNode extends DefaultNode { } @Override - public long avgRt() { - long total = 0; - long totalQps = 0; + public double avgRt() { + double total = 0; + double totalQps = 0; for (Node node : getChildList()) { total += node.avgRt() * node.passQps(); totalQps += node.passQps(); @@ -54,8 +54,8 @@ public class EntranceNode extends DefaultNode { } @Override - public long blockQps() { - long blockQps = 0; + public double blockQps() { + double blockQps = 0; for (Node node : getChildList()) { blockQps += node.blockQps(); } @@ -81,8 +81,8 @@ public class EntranceNode extends DefaultNode { } @Override - public long totalQps() { - long r = 0; + public double totalQps() { + double r = 0; for (Node node : getChildList()) { r += node.totalQps(); } @@ -90,8 +90,8 @@ public class EntranceNode extends DefaultNode { } @Override - public long successQps() { - long r = 0; + public double successQps() { + double r = 0; for (Node node : getChildList()) { r += node.successQps(); } @@ -99,8 +99,8 @@ public class EntranceNode extends DefaultNode { } @Override - public long passQps() { - long r = 0; + public double passQps() { + double r = 0; for (Node node : getChildList()) { r += node.passQps(); } @@ -116,4 +116,12 @@ public class EntranceNode extends DefaultNode { return r; } + @Override + public long totalPass() { + long r = 0; + for (Node node : getChildList()) { + r += node.totalPass(); + } + return r; + } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/Node.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/Node.java index b212c782..d87eefdb 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/Node.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/Node.java @@ -19,98 +19,125 @@ import java.util.Map; import com.alibaba.csp.sentinel.Entry; import com.alibaba.csp.sentinel.node.metric.MetricNode; +import com.alibaba.csp.sentinel.slots.statistic.metric.DebugSupport; /** - * This class holds real-time statistics for a resource. + * Holds real-time statistics for resources. * * @author qinan.qn * @author leyou * @author Eric Zhao */ -public interface Node { +public interface Node extends DebugSupport { /** - * Get incoming request per minute. {@code pass + block} + * Get incoming request per minute ({@code pass + block}). + * + * @return total request count per minute */ long totalRequest(); + /** + * Get pass count per minute. + * + * @return total passed request count per minute + * @since 1.5.0 + */ + long totalPass(); + /** * Get {@link Entry#exit()} count per minute. * - * @return Outgoing request per minute. + * @return total completed request count per minute */ long totalSuccess(); /** - * Get block request count per minute. + * Get blocked request count per minute (totalBlockRequest). + * + * @return total blocked request count per minute */ long blockRequest(); /** * Get exception count per minute. + * + * @return total business exception count per minute */ long totalException(); /** * Get pass request per second. + * + * @return QPS of passed requests */ - long passQps(); + double passQps(); /** * Get block request per second. + * + * @return QPS of blocked requests */ - long blockQps(); + double blockQps(); /** * Get {@link #passQps()} + {@link #blockQps()} request per second. + * + * @return QPS of passed and blocked requests */ - long totalQps(); + double totalQps(); /** * Get {@link Entry#exit()} request per second. + * + * @return QPS of completed requests */ - long successQps(); + double successQps(); /** * Get estimated max success QPS till now. * - * @return max success QPS + * @return max completed QPS */ - long maxSuccessQps(); + double maxSuccessQps(); /** * Get exception count per second. + * + * @return QPS of exception occurs */ - long exceptionQps(); + double exceptionQps(); /** * Get average rt per second. * * @return average response time per second */ - long avgRt(); + double avgRt(); /** * Get minimal response time. * * @return recorded minimal response time */ - long minRt(); + double minRt(); /** * Get current active thread count. + * + * @return current active thread count */ int curThreadNum(); /** * Get last second block QPS. */ - long previousBlockQps(); + double previousBlockQps(); /** * Last window QPS. */ - long previousPassQps(); + double previousPassQps(); /** * Fetch all valid metric nodes of resources. @@ -129,18 +156,22 @@ public interface Node { /** * Add rt and success count. * - * @param rt response time + * @param rt response time * @param success success count to add */ void addRtAndSuccess(long rt, int success); /** * Increase the block count. + * + * @param count count to add */ void increaseBlockQps(int count); /** - * Increase the biz exception count. + * Add the biz exception count. + * + * @param count count to add */ void increaseExceptionQps(int count); @@ -159,9 +190,4 @@ public interface Node { * {@link SampleCountProperty#SAMPLE_COUNT} is changed. */ void reset(); - - /** - * Debug only. - */ - void debug(); } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java index a27ef0fe..9f6cd336 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java @@ -151,28 +151,33 @@ public class StatisticNode implements Node { return totalRequest; } + @Override + public long totalPass() { + return rollingCounterInMinute.pass(); + } + @Override public long blockRequest() { return rollingCounterInMinute.block(); } @Override - public long blockQps() { - return rollingCounterInSecond.block() / (long) rollingCounterInSecond.getWindowIntervalInSec(); + public double blockQps() { + return rollingCounterInSecond.block() / rollingCounterInSecond.getWindowIntervalInSec(); } @Override - public long previousBlockQps() { + public double previousBlockQps() { return this.rollingCounterInMinute.previousWindowBlock(); } @Override - public long previousPassQps() { + public double previousPassQps() { return this.rollingCounterInMinute.previousWindowPass(); } @Override - public long totalQps() { + public double totalQps() { return passQps() + blockQps(); } @@ -182,8 +187,8 @@ public class StatisticNode implements Node { } @Override - public long exceptionQps() { - return rollingCounterInSecond.exception() / (long) rollingCounterInSecond.getWindowIntervalInSec(); + public double exceptionQps() { + return rollingCounterInSecond.exception() / rollingCounterInSecond.getWindowIntervalInSec(); } @Override @@ -192,32 +197,32 @@ public class StatisticNode implements Node { } @Override - public long passQps() { - return rollingCounterInSecond.pass() / (long) rollingCounterInSecond.getWindowIntervalInSec(); + public double passQps() { + return rollingCounterInSecond.pass() / rollingCounterInSecond.getWindowIntervalInSec(); } @Override - public long successQps() { - return rollingCounterInSecond.success() / (long) rollingCounterInSecond.getWindowIntervalInSec(); + public double successQps() { + return rollingCounterInSecond.success() / rollingCounterInSecond.getWindowIntervalInSec(); } @Override - public long maxSuccessQps() { + public double maxSuccessQps() { return rollingCounterInSecond.maxSuccess() * rollingCounterInSecond.getSampleCount(); } @Override - public long avgRt() { + public double avgRt() { long successCount = rollingCounterInSecond.success(); if (successCount == 0) { return 0; } - return rollingCounterInSecond.rt() / successCount; + return rollingCounterInSecond.rt() * 1.0 / successCount; } @Override - public long minRt() { + public double minRt() { return rollingCounterInSecond.minRt(); } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRule.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRule.java index 8abb1ddb..1401281c 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRule.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRule.java @@ -184,7 +184,7 @@ public class DegradeRule extends AbstractRule { } else if (grade == RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) { double exception = clusterNode.exceptionQps(); double success = clusterNode.successQps(); - long total = clusterNode.totalQps(); + double total = clusterNode.totalQps(); // if total qps less than RT_MAX_EXCEED_N, pass. if (total < RT_MAX_EXCEED_N) { return true; diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpController.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpController.java index a4f69318..1d143e75 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpController.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpController.java @@ -112,9 +112,9 @@ public class WarmUpController implements TrafficShapingController { @Override public boolean canPass(Node node, int acquireCount, boolean prioritized) { - long passQps = node.passQps(); + long passQps = (long) node.passQps(); - long previousQps = node.previousPassQps(); + long previousQps = (long) node.previousPassQps(); syncToken(previousQps); // 开始计算它的斜率 diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpRateLimiterController.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpRateLimiterController.java index ca8d953b..c0f40eed 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpRateLimiterController.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpRateLimiterController.java @@ -22,19 +22,16 @@ import com.alibaba.csp.sentinel.util.TimeUtil; /** * @author jialiang.linjl + * @since 1.4.0 */ public class WarmUpRateLimiterController extends WarmUpController { - final int timeOutInMs; - final AtomicLong latestPassedTime = new AtomicLong(-1); + private final int timeoutInMs; + private final AtomicLong latestPassedTime = new AtomicLong(-1); - /** - * @param count - * @param warmUpPeriodSec - */ public WarmUpRateLimiterController(double count, int warmUpPeriodSec, int timeOutMs, int coldFactor) { super(count, warmUpPeriodSec, coldFactor); - this.timeOutInMs = timeOutMs; + this.timeoutInMs = timeOutMs; } @Override @@ -44,7 +41,7 @@ public class WarmUpRateLimiterController extends WarmUpController { @Override public boolean canPass(Node node, int acquireCount, boolean prioritized) { - long previousQps = node.previousPassQps(); + long previousQps = (long) node.previousPassQps(); syncToken(previousQps); long currentTime = TimeUtil.currentTimeMillis(); @@ -68,13 +65,13 @@ public class WarmUpRateLimiterController extends WarmUpController { return true; } else { long waitTime = costTime + latestPassedTime.get() - currentTime; - if (waitTime > timeOutInMs) { + if (waitTime > timeoutInMs) { return false; } else { long oldTime = latestPassedTime.addAndGet(costTime); try { waitTime = oldTime - TimeUtil.currentTimeMillis(); - if (waitTime > timeOutInMs) { + if (waitTime > timeoutInMs) { latestPassedTime.addAndGet(-costTime); return false; } @@ -89,4 +86,3 @@ public class WarmUpRateLimiterController extends WarmUpController { return false; } } - diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/DebugSupport.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/DebugSupport.java new file mode 100644 index 00000000..5806f253 --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/DebugSupport.java @@ -0,0 +1,28 @@ +/* + * Copyright 1999-2019 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; + +/** + * @author Eric Zhao + * @since 1.5.0 + */ +public interface DebugSupport { + + /** + * For debug; + */ + void debug(); +} diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/ClusterNodeTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/ClusterNodeTest.java index 258ac76d..f16db1af 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/ClusterNodeTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/ClusterNodeTest.java @@ -139,18 +139,18 @@ public class ClusterNodeTest { // test count<=0, no exceptionQps added clusterNode.trace(exception, 0); clusterNode.trace(exception, -1); - assertEquals(0, clusterNode.exceptionQps()); + assertEquals(0, clusterNode.exceptionQps(), 0.01); assertEquals(0, clusterNode.totalException()); // test count=1, not BlockException, 1 exceptionQps added clusterNode.trace(exception, 1); - assertEquals(1, clusterNode.exceptionQps()); + assertEquals(1, clusterNode.exceptionQps(), 0.01); assertEquals(1, clusterNode.totalException()); // test count=1, BlockException, no exceptionQps added FlowException flowException = new FlowException("flow"); clusterNode.trace(flowException, 1); - assertEquals(1, clusterNode.exceptionQps()); + assertEquals(1, clusterNode.exceptionQps(), 0.01); assertEquals(1, clusterNode.totalException()); } } diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java index 4520b5bc..02724342 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java @@ -92,9 +92,9 @@ public class StatisticNodeTest { tickEs.shutdown(); // now no biz method execute, so there is no curThreadNum,passQps,successQps - assertEquals(0, node.curThreadNum()); - assertEquals(0, node.passQps()); - assertEquals(0, node.successQps()); + assertEquals(0, node.curThreadNum(), 0.01); + assertEquals(0, node.passQps(), 0.01); + assertEquals(0, node.successQps(), 0.01); // note: total time cost should be controlled within 1 minute, // as the node.totalRequest() holding statistics of recent 60 seconds @@ -105,7 +105,7 @@ public class StatisticNodeTest { assertEquals(totalRequest, node.totalSuccess()); // now there are no data in time span, so the minRT should be equals to TIME_DROP_VALVE - assertEquals(node.minRt(), Constants.TIME_DROP_VALVE); + assertEquals(node.minRt(), Constants.TIME_DROP_VALVE, 0.01); log("===================================================="); log("testStatisticThreadNumAndQps done, cost " + (TimeUtil.currentTimeMillis() - testStartTime) + "ms"); diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeTest.java index faf78931..b2518a80 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeTest.java @@ -45,7 +45,7 @@ public class DegradeTest { Context context = mock(Context.class); DefaultNode node = mock(DefaultNode.class); when(node.getClusterNode()).thenReturn(cn); - when(cn.avgRt()).thenReturn(2L); + when(cn.avgRt()).thenReturn(2d); DegradeRule rule = new DegradeRule(); rule.setCount(1); @@ -69,9 +69,9 @@ public class DegradeTest { public void testExceptionRatioModeDegrade() throws Throwable { String key = "test_degrade_exception_ratio"; ClusterNode cn = mock(ClusterNode.class); - when(cn.exceptionQps()).thenReturn(2L); + when(cn.exceptionQps()).thenReturn(2d); // Indicates that there are QPS more than min threshold. - when(cn.totalQps()).thenReturn(12L); + when(cn.totalQps()).thenReturn(12d); ClusterBuilderSlot.getClusterNodeMap().put(new StringResourceWrapper(key, EntryType.IN), cn); Context context = mock(Context.class); @@ -84,7 +84,7 @@ public class DegradeTest { rule.setTimeWindow(2); rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO); - when(cn.successQps()).thenReturn(8L); + when(cn.successQps()).thenReturn(8d); // Will fail. assertFalse(rule.passCheck(context, node, 1)); @@ -92,7 +92,7 @@ public class DegradeTest { // Restore from the degrade timeout. TimeUnit.MILLISECONDS.sleep(2200); - when(cn.successQps()).thenReturn(20L); + when(cn.successQps()).thenReturn(20d); // Will pass. assertTrue(rule.passCheck(context, node, 1)); } diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java index a507b5af..ae43a852 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java @@ -18,9 +18,12 @@ package com.alibaba.csp.sentinel.slots.block.flow; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import com.alibaba.csp.sentinel.Entry; @@ -34,6 +37,16 @@ import com.alibaba.csp.sentinel.slots.block.RuleConstant; */ public class FlowPartialIntegrationTest { + @Before + public void setUp() throws Exception { + FlowRuleManager.loadRules(new ArrayList()); + } + + @After + public void tearDown() throws Exception { + FlowRuleManager.loadRules(new ArrayList()); + } + @Test public void testQPSGrade() { FlowRule flowRule = new FlowRule(); diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleTest.java index 6bc26b4c..0180501e 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleTest.java @@ -15,7 +15,7 @@ */ package com.alibaba.csp.sentinel.slots.block.flow; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -52,15 +52,15 @@ public class FlowRuleTest { when(context.getOrigin()).thenReturn(""); when(node.getClusterNode()).thenReturn(cn); - when(cn.passQps()).thenReturn(1l); + when(cn.passQps()).thenReturn(1d); - assertTrue(flowRule.passCheck(context, node, 1, new Object[0]) == false); + assertFalse(flowRule.passCheck(context, node, 1)); flowRule.setGrade(RuleConstant.FLOW_GRADE_THREAD); defaultController = new DefaultController(1, flowRule.getGrade()); flowRule.setRater(defaultController); when(cn.curThreadNum()).thenReturn(1); - assertTrue(flowRule.passCheck(context, node, 1, new Object[0]) == false); + assertTrue(!flowRule.passCheck(context, node, 1)); } @Test @@ -79,17 +79,16 @@ public class FlowRuleTest { DefaultNode dn = mock(DefaultNode.class); when(context.getName()).thenReturn("entry1"); - when(dn.passQps()).thenReturn(1l); - assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]) == false); + when(dn.passQps()).thenReturn(1d); + assertFalse(flowRule.passCheck(context, dn, 1)); when(context.getName()).thenReturn("entry2"); - assertTrue(flowRule.passCheck(context, dn, 1, new Object[0])); + assertTrue(flowRule.passCheck(context, dn, 1)); // Strategy == relate flowRule.setStrategy(RuleConstant.STRATEGY_CHAIN); ClusterNode cn = mock(ClusterNode.class); - assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]) == true); - + assertTrue(flowRule.passCheck(context, dn, 1)); } @Test @@ -106,7 +105,7 @@ public class FlowRuleTest { Context context = mock(Context.class); DefaultNode dn = mock(DefaultNode.class); when(context.getOrigin()).thenReturn("origin1"); - when(dn.passQps()).thenReturn(1l); + when(dn.passQps()).thenReturn(1d); when(context.getOriginNode()).thenReturn(dn); /* @@ -115,9 +114,9 @@ public class FlowRuleTest { */ ClusterNode cn = mock(ClusterNode.class); when(dn.getClusterNode()).thenReturn(cn); - when(cn.passQps()).thenReturn(1l); + when(cn.passQps()).thenReturn(1d); assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]) == false); - when(cn.passQps()).thenReturn(0l); + when(cn.passQps()).thenReturn(0d); assertTrue(flowRule.passCheck(context, dn, 1, new Object[0])); flowRule.setStrategy(RuleConstant.STRATEGY_CHAIN); diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/DefaultControllerTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/DefaultControllerTest.java index 4b21fe44..16289ea8 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/DefaultControllerTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/DefaultControllerTest.java @@ -16,7 +16,7 @@ public class DefaultControllerTest { @Test public void testCanPassForQps() { - long threshold = 10; + double threshold = 10; TrafficShapingController controller = new DefaultController(threshold, RuleConstant.FLOW_GRADE_QPS); Node node = mock(Node.class); when(node.passQps()).thenReturn(threshold - 1) diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpControllerTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpControllerTest.java index e91f2a36..62cf908b 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpControllerTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpControllerTest.java @@ -38,26 +38,26 @@ public class WarmUpControllerTest extends AbstractTimeBasedTest { Node node = mock(Node.class); - when(node.passQps()).thenReturn(8L); - when(node.previousPassQps()).thenReturn(1L); + when(node.passQps()).thenReturn(8d); + when(node.previousPassQps()).thenReturn(1d); assertFalse(warmupController.canPass(node, 1)); - when(node.passQps()).thenReturn(1L); - when(node.previousPassQps()).thenReturn(1L); + when(node.passQps()).thenReturn(1d); + when(node.previousPassQps()).thenReturn(1d); assertTrue(warmupController.canPass(node, 1)); - when(node.previousPassQps()).thenReturn(10L); + when(node.previousPassQps()).thenReturn(10d); for (int i = 0; i < 100; i++) { sleep(100); warmupController.canPass(node, 1); } - when(node.passQps()).thenReturn(8L); + when(node.passQps()).thenReturn(8d); assertTrue(warmupController.canPass(node, 1)); - when(node.passQps()).thenReturn(10L); + when(node.passQps()).thenReturn(10d); assertFalse(warmupController.canPass(node, 1)); } } diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpRateLimiterControllerTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpRateLimiterControllerTest.java index 386b1a9f..178f59e8 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpRateLimiterControllerTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpRateLimiterControllerTest.java @@ -22,15 +22,15 @@ public class WarmUpRateLimiterControllerTest { Node node = mock(Node.class); - when(node.passQps()).thenReturn(100L); - when(node.previousPassQps()).thenReturn(100L); + when(node.passQps()).thenReturn(100d); + when(node.previousPassQps()).thenReturn(100d); assertTrue(controller.canPass(node, 1)); long start = System.currentTimeMillis(); assertTrue(controller.canPass(node, 1)); long cost = System.currentTimeMillis() - start; - assertTrue(cost >= 100 && cost <= 110); + assertTrue(cost >= 100 && cost <= 120); } @Test @@ -39,8 +39,8 @@ public class WarmUpRateLimiterControllerTest { Node node = mock(Node.class); - when(node.passQps()).thenReturn(100L); - when(node.previousPassQps()).thenReturn(100L); + when(node.passQps()).thenReturn(100d); + when(node.previousPassQps()).thenReturn(100d); assertTrue(controller.canPass(node, 1)); diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/vo/NodeVo.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/vo/NodeVo.java index ce8e4927..d3e0ebbd 100755 --- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/vo/NodeVo.java +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/vo/NodeVo.java @@ -63,12 +63,12 @@ public class NodeVo { vo.parentId = parentId; vo.resource = node.getId().getShowName(); vo.threadNum = node.curThreadNum(); - vo.passQps = node.passQps(); - vo.blockQps = node.blockQps(); - vo.totalQps = node.totalQps(); - vo.averageRt = node.avgRt(); - vo.successQps = node.successQps(); - vo.exceptionQps = node.exceptionQps(); + vo.passQps = (long) node.passQps(); + vo.blockQps = (long) node.blockQps(); + vo.totalQps = (long) node.totalQps(); + vo.averageRt = (long) node.avgRt(); + vo.successQps = (long) node.successQps(); + vo.exceptionQps = (long) node.exceptionQps(); vo.oneMinuteException = node.totalException(); vo.oneMinutePass = node.totalRequest() - node.blockRequest(); vo.oneMinuteBlock = node.blockRequest(); @@ -102,12 +102,12 @@ public class NodeVo { NodeVo vo = new NodeVo(); vo.resource = name; vo.threadNum = node.curThreadNum(); - vo.passQps = node.passQps(); - vo.blockQps = node.blockQps(); - vo.totalQps = node.totalQps(); - vo.averageRt = node.avgRt(); - vo.successQps = node.successQps(); - vo.exceptionQps = node.exceptionQps(); + vo.passQps = (long) node.passQps(); + vo.blockQps = (long) node.blockQps(); + vo.totalQps = (long) node.totalQps(); + vo.averageRt = (long) node.avgRt(); + vo.successQps = (long) node.successQps(); + vo.exceptionQps = (long) node.exceptionQps(); vo.oneMinuteException = node.totalException(); vo.oneMinutePass = node.totalRequest() - node.blockRequest(); vo.oneMinuteBlock = node.blockRequest();