From 5e9cfb0deba3d591dd80e3794bc32b7950a8ec59 Mon Sep 17 00:00:00 2001 From: johnli Date: Sun, 26 Apr 2020 20:48:39 +0800 Subject: [PATCH] fix: Tracer does not trace exception to DefaultNode (#1068) --- .../java/com/alibaba/csp/sentinel/Tracer.java | 7 +----- .../csp/sentinel/node/ClusterNode.java | 14 ----------- .../csp/sentinel/node/DefaultNode.java | 23 +++++++++++++++++++ .../csp/sentinel/node/ClusterNodeTest.java | 23 +++++++++++-------- 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java index 4d0e6da3..e05d3e75 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java @@ -114,12 +114,7 @@ public class Tracer { m.addException(entry.getResourceWrapper().getName(), count, t); } - // clusterNode can be null when Constants.ON is false. - ClusterNode clusterNode = curNode.getClusterNode(); - if (clusterNode == null) { - return; - } - clusterNode.trace(t, count); + curNode.trace(t, count); } /** diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/ClusterNode.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/ClusterNode.java index 0e085087..1f16ce5b 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/ClusterNode.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/ClusterNode.java @@ -123,18 +123,4 @@ public class ClusterNode extends StatisticNode { return originCountMap; } - /** - * Add exception count only when given {@code throwable} is not a {@link BlockException}. - * - * @param throwable target exception - * @param count count to add - */ - public void trace(Throwable throwable, int count) { - if (count <= 0) { - return; - } - if (!BlockException.isBlockException(throwable)) { - this.increaseExceptionQps(count); - } - } } 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 f8786375..fadeb78b 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 @@ -23,6 +23,7 @@ import com.alibaba.csp.sentinel.SphO; import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.context.Context; import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; +import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; /** @@ -146,6 +147,28 @@ public class DefaultNode extends StatisticNode { visitTree(0, this); } + + /** + * Add exception count only when given {@code throwable} is not a {@link BlockException}. + * + * @param throwable target exception + * @param count count to add + */ + public void trace(Throwable throwable, int count) { + if (count <= 0) { + return; + } + if (BlockException.isBlockException(throwable)) { + return; + } + super.increaseExceptionQps(count); + + // clusterNode can be null when Constants.ON is false. + if (clusterNode != null) { + clusterNode.increaseExceptionQps(count); + } + } + private void visitTree(int level, DefaultNode node) { for (int i = 0; i < level; ++i) { System.out.print("-"); 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 9c571057..29c96306 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 @@ -15,6 +15,8 @@ */ package com.alibaba.csp.sentinel.node; +import com.alibaba.csp.sentinel.EntryType; +import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; import com.alibaba.csp.sentinel.slots.block.flow.FlowException; import org.junit.Test; @@ -131,24 +133,25 @@ public class ClusterNodeTest { @Test public void testTraceException() { ClusterNode clusterNode = new ClusterNode("test"); + DefaultNode defaultNode = new DefaultNode(new StringResourceWrapper("test", EntryType.IN), clusterNode); Exception exception = new RuntimeException("test"); // test count<=0, no exceptionQps added - clusterNode.trace(exception, 0); - clusterNode.trace(exception, -1); - assertEquals(0, clusterNode.exceptionQps(), 0.01); - assertEquals(0, clusterNode.totalException()); + defaultNode.trace(exception, 0); + defaultNode.trace(exception, -1); + assertEquals(0, defaultNode.exceptionQps(), 0.01); + assertEquals(0, defaultNode.totalException()); // test count=1, not BlockException, 1 exceptionQps added - clusterNode.trace(exception, 1); - assertEquals(1, clusterNode.exceptionQps(), 0.01); - assertEquals(1, clusterNode.totalException()); + defaultNode.trace(exception, 1); + assertEquals(1, defaultNode.exceptionQps(), 0.01); + assertEquals(1, defaultNode.totalException()); // test count=1, BlockException, no exceptionQps added FlowException flowException = new FlowException("flow"); - clusterNode.trace(flowException, 1); - assertEquals(1, clusterNode.exceptionQps(), 0.01); - assertEquals(1, clusterNode.totalException()); + defaultNode.trace(flowException, 1); + assertEquals(1, defaultNode.exceptionQps(), 0.01); + assertEquals(1, defaultNode.totalException()); } }