From 468327bdcda7ebb2c8469dc7db0d9cc409078253 Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Mon, 25 Mar 2019 11:48:31 +0800 Subject: [PATCH] Remove useless global NodeBuilder in Env class Signed-off-by: Eric Zhao --- .../com/alibaba/csp/sentinel/Constants.java | 18 ++-- .../java/com/alibaba/csp/sentinel/Env.java | 3 - .../csp/sentinel/node/DefaultNodeBuilder.java | 37 -------- .../csp/sentinel/node/NodeBuilder.java | 1 + .../clusterbuilder/ClusterBuilderSlot.java | 3 +- .../slots/nodeselector/NodeSelectorSlot.java | 3 +- .../sentinel/node/DefaultNodeBuilderTest.java | 84 ------------------- 7 files changed, 14 insertions(+), 135 deletions(-) delete mode 100755 sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/DefaultNodeBuilder.java delete mode 100644 sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/DefaultNodeBuilderTest.java diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java index ba6e12d2..054507c1 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java @@ -38,26 +38,30 @@ public final class Constants { public final static String ROOT_ID = "machine-root"; public final static String CONTEXT_DEFAULT_NAME = "sentinel_default_context"; + /** + * A virtual resource identifier for total inbound statistics (since 1.5.0). + */ public final static String TOTAL_IN_RESOURCE_NAME = "__total_inbound_traffic__"; + /** + * Global ROOT statistic node that represents the universal parent node. + */ public final static DefaultNode ROOT = new EntranceNode(new StringResourceWrapper(ROOT_ID, EntryType.IN), new ClusterNode()); /** - * Statistics for {@link SystemRule} checking. + * Global statistic node for inbound traffic. Usually used for {@link SystemRule} checking. */ public final static ClusterNode ENTRY_NODE = new ClusterNode(); /** - * Response time that exceeds TIME_DROP_VALVE will be calculated as TIME_DROP_VALVE. - * Default value is 4900 ms - * It can be configured by property file or JVM parameter -Dcsp.sentinel.statistic.max.rt=xxx - * See {@link SentinelConfig#statisticMaxRt()} + * Response time that exceeds TIME_DROP_VALVE will be calculated as TIME_DROP_VALVE. Default value is 4900 ms. + * It can be configured by property file or JVM parameter via {@code -Dcsp.sentinel.statistic.max.rt=xxx}. */ - public static int TIME_DROP_VALVE = SentinelConfig.statisticMaxRt(); + public static final int TIME_DROP_VALVE = SentinelConfig.statisticMaxRt(); /** * The global switch for Sentinel. */ public static volatile boolean ON = true; -} \ No newline at end of file +} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Env.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Env.java index 20538f3b..5be93a34 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Env.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Env.java @@ -16,8 +16,6 @@ package com.alibaba.csp.sentinel; import com.alibaba.csp.sentinel.init.InitExecutor; -import com.alibaba.csp.sentinel.node.DefaultNodeBuilder; -import com.alibaba.csp.sentinel.node.NodeBuilder; /** * Sentinel Env. This class will trigger all initialization for Sentinel. @@ -31,7 +29,6 @@ import com.alibaba.csp.sentinel.node.NodeBuilder; */ public class Env { - public static final NodeBuilder nodeBuilder = new DefaultNodeBuilder(); public static final Sph sph = new CtSph(); static { diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/DefaultNodeBuilder.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/DefaultNodeBuilder.java deleted file mode 100755 index fffd0c99..00000000 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/DefaultNodeBuilder.java +++ /dev/null @@ -1,37 +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.node; - -import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; - -/** - * Default implementation of {@link NodeBuilder}. - * - * @author qinan.qn - */ -public class DefaultNodeBuilder implements NodeBuilder { - - @Override - public DefaultNode buildTreeNode(ResourceWrapper id, ClusterNode clusterNode) { - return new DefaultNode(id, clusterNode); - } - - @Override - public ClusterNode buildClusterNode() { - return new ClusterNode(); - } - -} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/NodeBuilder.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/NodeBuilder.java index 872b5371..b04d21a8 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/NodeBuilder.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/NodeBuilder.java @@ -22,6 +22,7 @@ import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; * * @author qinan.qn */ +@Deprecated public interface NodeBuilder { /** diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/clusterbuilder/ClusterBuilderSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/clusterbuilder/ClusterBuilderSlot.java index 7b7010b3..1757e9dc 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/clusterbuilder/ClusterBuilderSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/clusterbuilder/ClusterBuilderSlot.java @@ -18,7 +18,6 @@ package com.alibaba.csp.sentinel.slots.clusterbuilder; import java.util.HashMap; import java.util.Map; -import com.alibaba.csp.sentinel.Env; import com.alibaba.csp.sentinel.EntryType; import com.alibaba.csp.sentinel.context.Context; import com.alibaba.csp.sentinel.context.ContextUtil; @@ -79,7 +78,7 @@ public class ClusterBuilderSlot extends AbstractLinkedProcessorSlot synchronized (lock) { if (clusterNode == null) { // Create the cluster node. - clusterNode = Env.nodeBuilder.buildClusterNode(); + clusterNode = new ClusterNode(); HashMap newMap = new HashMap<>(Math.max(clusterNodeMap.size(), 16)); newMap.putAll(clusterNodeMap); newMap.put(node.getId(), clusterNode); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/nodeselector/NodeSelectorSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/nodeselector/NodeSelectorSlot.java index aad95c8a..5693ad87 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/nodeselector/NodeSelectorSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/nodeselector/NodeSelectorSlot.java @@ -18,7 +18,6 @@ package com.alibaba.csp.sentinel.slots.nodeselector; import java.util.HashMap; import java.util.Map; -import com.alibaba.csp.sentinel.Env; import com.alibaba.csp.sentinel.context.Context; import com.alibaba.csp.sentinel.context.ContextUtil; import com.alibaba.csp.sentinel.node.ClusterNode; @@ -156,7 +155,7 @@ public class NodeSelectorSlot extends AbstractLinkedProcessorSlot { synchronized (this) { node = map.get(context.getName()); if (node == null) { - node = Env.nodeBuilder.buildTreeNode(resourceWrapper, null); + node = new DefaultNode(resourceWrapper, null); HashMap cacheMap = new HashMap(map.size()); cacheMap.putAll(map); cacheMap.put(context.getName(), node); diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/DefaultNodeBuilderTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/DefaultNodeBuilderTest.java deleted file mode 100644 index 56872fa3..00000000 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/DefaultNodeBuilderTest.java +++ /dev/null @@ -1,84 +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.node; - -import com.alibaba.csp.sentinel.EntryType; -import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; -import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Test cases for {@link DefaultNodeBuilder}. - * - * @author cdfive - */ -public class DefaultNodeBuilderTest { - - @Test - public void testBuildTreeNode() { - DefaultNodeBuilder builder = new DefaultNodeBuilder(); - - ResourceWrapper id = new StringResourceWrapper("resA", EntryType.IN); - ClusterNode clusterNode = new ClusterNode(); - DefaultNode defaultNode = builder.buildTreeNode(id, clusterNode); - - assertNotNull(defaultNode); - assertEquals(id, defaultNode.getId()); - assertEquals(clusterNode, defaultNode.getClusterNode()); - - // verify each call returns a different instance - DefaultNode defaultNode2 = builder.buildTreeNode(id, clusterNode); - assertNotNull(defaultNode2); - assertNotSame(defaultNode, defaultNode2); - // now DefaultNode#equals(Object) is not implemented, they are not equal - assertNotEquals(defaultNode, defaultNode2); - } - - @Test - public void testBuildTreeNodeNullClusterNode() { - DefaultNodeBuilder builder = new DefaultNodeBuilder(); - - ResourceWrapper id = new StringResourceWrapper("resA", EntryType.IN); - DefaultNode defaultNode = builder.buildTreeNode(id, null); - - assertNotNull(defaultNode); - assertEquals(id, defaultNode.getId()); - assertNull(defaultNode.getClusterNode()); - - // verify each call returns a different instance - DefaultNode defaultNode2 = builder.buildTreeNode(id, null); - assertNotNull(defaultNode2); - assertNotSame(defaultNode, defaultNode2); - // now DefaultNode#equals(Object) is not implemented, they are not equal - assertNotEquals(defaultNode, defaultNode2); - } - - @Test - public void testBuildClusterNode() { - DefaultNodeBuilder builder = new DefaultNodeBuilder(); - ClusterNode clusterNode = builder.buildClusterNode(); - assertNotNull(clusterNode); - - // verify each call returns a different instance - ClusterNode clusterNode2 = builder.buildClusterNode(); - assertNotNull(clusterNode2); - assertNotSame(clusterNode, clusterNode2); - // as new a ClusterNode instance in DefaultNodeBuilder#buildClusterNode(), they are not equal - assertNotEquals(clusterNode, clusterNode2); - } -}