From 516e36fd83af2e9bcdc64d3813175d3dfb0be8ee Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Sun, 26 Apr 2020 20:59:48 +0800 Subject: [PATCH] Polish Tracer with entry.setError(ex) mechanism Signed-off-by: Eric Zhao --- .../java/com/alibaba/csp/sentinel/Tracer.java | 65 +++++++++---------- .../csp/sentinel/node/DefaultNode.java | 23 ------- .../csp/sentinel/node/ClusterNodeTest.java | 29 --------- 3 files changed, 30 insertions(+), 87 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 e05d3e75..f29d6c5c 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 @@ -18,11 +18,7 @@ package com.alibaba.csp.sentinel; import com.alibaba.csp.sentinel.context.Context; import com.alibaba.csp.sentinel.context.ContextUtil; import com.alibaba.csp.sentinel.context.NullContext; -import com.alibaba.csp.sentinel.metric.extension.MetricExtensionProvider; -import com.alibaba.csp.sentinel.node.ClusterNode; -import com.alibaba.csp.sentinel.node.DefaultNode; import com.alibaba.csp.sentinel.slots.block.BlockException; -import com.alibaba.csp.sentinel.metric.extension.MetricExtension; import com.alibaba.csp.sentinel.util.AssertUtil; /** @@ -39,32 +35,33 @@ public class Tracer { protected Tracer() {} /** - * Trace provided {@link Throwable} and increment exception count to entry in current context. + * Trace provided {@link Throwable} to the resource entry in current context. * * @param e exception to record */ public static void trace(Throwable e) { - trace(e, 1); + traceContext(e, ContextUtil.getContext()); } /** - * Trace provided {@link Throwable} and add exception count to entry in current context. + * Trace provided {@link Throwable} to current entry in current context. * * @param e exception to record * @param count exception count to add */ + @Deprecated public static void trace(Throwable e, int count) { traceContext(e, count, ContextUtil.getContext()); } /** - * Trace provided {@link Throwable} and add exception count to current entry in provided context. + * Trace provided {@link Throwable} to current entry of given entrance context. * * @param e exception to record - * @param count exception count to add - * @since 1.4.2 + * @param context target entrance context + * @since 1.8.0 */ - public static void traceContext(Throwable e, int count, Context context) { + public static void traceContext(Throwable e, Context context) { if (!shouldTrace(e)) { return; } @@ -72,49 +69,47 @@ public class Tracer { if (context == null || context instanceof NullContext) { return; } - - DefaultNode curNode = (DefaultNode)context.getCurNode(); - traceExceptionToNode(e, count, context.getCurEntry(), curNode); + traceEntryInternal(e, context.getCurEntry()); } /** - * Trace provided {@link Throwable} and increment exception count to provided entry. + * Trace provided {@link Throwable} and add exception count to current entry in provided context. * - * @param e exception to record + * @param e exception to record + * @param count exception count to add * @since 1.4.2 */ - public static void traceEntry(Throwable e, Entry entry) { - traceEntry(e, 1, entry); + @Deprecated + public static void traceContext(Throwable e, int count, Context context) { + if (!shouldTrace(e)) { + return; + } + + if (context == null || context instanceof NullContext) { + return; + } + traceEntryInternal(e, context.getCurEntry()); } /** - * Trace provided {@link Throwable} and add exception count to provided entry. + * Trace provided {@link Throwable} to the given resource entry. * - * @param e exception to record - * @param count exception count to add + * @param e exception to record * @since 1.4.2 */ - public static void traceEntry(Throwable e, int count, Entry entry) { + public static void traceEntry(Throwable e, Entry entry) { if (!shouldTrace(e)) { return; } - if (entry == null || entry.getCurNode() == null) { - return; - } - - DefaultNode curNode = (DefaultNode)entry.getCurNode(); - traceExceptionToNode(e, count, entry, curNode); + traceEntryInternal(e, entry); } - private static void traceExceptionToNode(Throwable t, int count, Entry entry, DefaultNode curNode) { - if (curNode == null) { + private static void traceEntryInternal(/*@NeedToTrace*/ Throwable e, Entry entry) { + if (entry == null) { return; } - for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) { - m.addException(entry.getResourceWrapper().getName(), count, t); - } - curNode.trace(t, count); + entry.setError(e); } /** @@ -203,4 +198,4 @@ public class Tracer { } return true; } -} +} \ No newline at end of file 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 fadeb78b..f8786375 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,7 +23,6 @@ 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; /** @@ -147,28 +146,6 @@ 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 29c96306..e13ba553 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,10 +15,6 @@ */ 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; import java.util.ArrayList; @@ -129,29 +125,4 @@ 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 - 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 - 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"); - defaultNode.trace(flowException, 1); - assertEquals(1, defaultNode.exceptionQps(), 0.01); - assertEquals(1, defaultNode.totalException()); - } }