From b89be2c196d784a8aaeb8532437331efd965530d Mon Sep 17 00:00:00 2001 From: cdfive <176402936@qq.com> Date: Tue, 23 Apr 2019 22:01:37 +0800 Subject: [PATCH 1/6] Merge from master and use @SpiOrder and SpiLoader.loadInstanceListSorted(Class clazz) method. --- .../slots/DefaultSlotChainBuilder.java | 33 +++-- .../slots/block/authority/AuthoritySlot.java | 2 + .../slots/block/degrade/DegradeSlot.java | 2 + .../sentinel/slots/block/flow/FlowSlot.java | 2 + .../clusterbuilder/ClusterBuilderSlot.java | 2 + .../csp/sentinel/slots/logger/LogSlot.java | 2 + .../slots/nodeselector/NodeSelectorSlot.java | 2 + .../slots/statistic/StatisticSlot.java | 2 + .../csp/sentinel/slots/system/SystemSlot.java | 2 + .../alibaba/csp/sentinel/util/SpiLoader.java | 12 +- ...ibaba.csp.sentinel.slotchain.ProcessorSlot | 9 ++ .../slots/DefaultSlotChainBuilderTest.java | 89 ++++++++++++++ .../csp/sentinel/util/SpiLoaderTest.java | 114 ++++++++++++++++++ .../slots/HotParamSlotChainBuilder.java | 52 -------- .../slots/block/flow/param/ParamFlowSlot.java | 2 + ...ibaba.csp.sentinel.slotchain.ProcessorSlot | 1 + ...ba.csp.sentinel.slotchain.SlotChainBuilder | 1 - 17 files changed, 252 insertions(+), 77 deletions(-) create mode 100644 sentinel-core/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot create mode 100644 sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilderTest.java create mode 100644 sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java delete mode 100644 sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java create mode 100644 sentinel-extension/sentinel-parameter-flow-control/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot delete mode 100644 sentinel-extension/sentinel-parameter-flow-control/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilder.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilder.java index 9bf2596d30..025d12791d 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilder.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilder.java @@ -15,17 +15,15 @@ */ package com.alibaba.csp.sentinel.slots; +import com.alibaba.csp.sentinel.log.RecordLog; +import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot; import com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain; +import com.alibaba.csp.sentinel.slotchain.ProcessorSlot; import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; import com.alibaba.csp.sentinel.slotchain.SlotChainBuilder; -import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; -import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; -import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; -import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; -import com.alibaba.csp.sentinel.slots.logger.LogSlot; -import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; -import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; -import com.alibaba.csp.sentinel.slots.system.SystemSlot; +import com.alibaba.csp.sentinel.util.SpiLoader; + +import java.util.List; /** * Builder for a default {@link ProcessorSlotChain}. @@ -38,16 +36,17 @@ public class DefaultSlotChainBuilder implements SlotChainBuilder { @Override public ProcessorSlotChain build() { ProcessorSlotChain chain = new DefaultProcessorSlotChain(); - chain.addLast(new NodeSelectorSlot()); - chain.addLast(new ClusterBuilderSlot()); - chain.addLast(new LogSlot()); - chain.addLast(new StatisticSlot()); - chain.addLast(new SystemSlot()); - chain.addLast(new AuthoritySlot()); - chain.addLast(new FlowSlot()); - chain.addLast(new DegradeSlot()); + + List sortedSlotList = SpiLoader.loadInstanceListSorted(ProcessorSlot.class); + for (ProcessorSlot slot : sortedSlotList) { + if (!(slot instanceof AbstractLinkedProcessorSlot)) { + RecordLog.warn("The ProcessorSlot(" + slot.getClass().getCanonicalName() + ") is not an instance of AbstractLinkedProcessorSlot, can't be added into ProcessorSlotChain"); + continue; + } + + chain.addLast((AbstractLinkedProcessorSlot) slot); + } return chain; } - } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java index f7920baa9c..3146429a23 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java @@ -23,6 +23,7 @@ import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot; import com.alibaba.csp.sentinel.slotchain.ProcessorSlot; import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; +import com.alibaba.csp.sentinel.spi.SpiOrder; /** * A {@link ProcessorSlot} that dedicates to {@link AuthorityRule} checking. @@ -30,6 +31,7 @@ * @author leyou * @author Eric Zhao */ +@SpiOrder(-3000) public class AuthoritySlot extends AbstractLinkedProcessorSlot { @Override diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeSlot.java index f76d81f8cd..8e8dd19109 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeSlot.java @@ -20,12 +20,14 @@ import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot; import com.alibaba.csp.sentinel.slotchain.ProcessorSlot; import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; +import com.alibaba.csp.sentinel.spi.SpiOrder; /** * A {@link ProcessorSlot} dedicates to {@link DegradeRule} checking. * * @author leyou */ +@SpiOrder(-1000) public class DegradeSlot extends AbstractLinkedProcessorSlot { @Override diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlot.java index 9aa900ecd3..179ea8caa3 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlot.java @@ -23,6 +23,7 @@ import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot; import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.alibaba.csp.sentinel.spi.SpiOrder; /** *

@@ -133,6 +134,7 @@ * @author jialiang.linjl * @author Eric Zhao */ +@SpiOrder(-2000) public class FlowSlot extends AbstractLinkedProcessorSlot { @Override 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 1757e9dc35..1cd5f138a0 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 @@ -30,6 +30,7 @@ import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; +import com.alibaba.csp.sentinel.spi.SpiOrder; /** *

@@ -44,6 +45,7 @@ * * @author jialiang.linjl */ +@SpiOrder(-8000) public class ClusterBuilderSlot extends AbstractLinkedProcessorSlot { /** diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java index 802690a9ab..57b1ca19dc 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java @@ -21,11 +21,13 @@ import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot; import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.alibaba.csp.sentinel.spi.SpiOrder; /** * A {@link com.alibaba.csp.sentinel.slotchain.ProcessorSlot} that is response for logging block exceptions * to provide concrete logs for troubleshooting. */ +@SpiOrder(-7000) public class LogSlot extends AbstractLinkedProcessorSlot { @Override 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 5693ad87ae..a2dd085e67 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 @@ -25,6 +25,7 @@ import com.alibaba.csp.sentinel.node.EntranceNode; import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot; import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; +import com.alibaba.csp.sentinel.spi.SpiOrder; /** *

@@ -122,6 +123,7 @@ * @see EntranceNode * @see ContextUtil */ +@SpiOrder(-9000) public class NodeSelectorSlot extends AbstractLinkedProcessorSlot { /** diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java index d35a8bddc2..20bdee2465 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java @@ -20,6 +20,7 @@ import com.alibaba.csp.sentinel.slotchain.ProcessorSlotEntryCallback; import com.alibaba.csp.sentinel.slotchain.ProcessorSlotExitCallback; import com.alibaba.csp.sentinel.slots.block.flow.PriorityWaitException; +import com.alibaba.csp.sentinel.spi.SpiOrder; import com.alibaba.csp.sentinel.util.TimeUtil; import com.alibaba.csp.sentinel.Constants; import com.alibaba.csp.sentinel.EntryType; @@ -46,6 +47,7 @@ * @author jialiang.linjl * @author Eric Zhao */ +@SpiOrder(-6000) public class StatisticSlot extends AbstractLinkedProcessorSlot { @Override diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java index 1f67e4263e..f87cf68045 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java @@ -20,6 +20,7 @@ import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot; import com.alibaba.csp.sentinel.slotchain.ProcessorSlot; import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; +import com.alibaba.csp.sentinel.spi.SpiOrder; /** * A {@link ProcessorSlot} that dedicates to {@link SystemRule} checking. @@ -27,6 +28,7 @@ * @author jialiang.linjl * @author leyou */ +@SpiOrder(-4000) public class SystemSlot extends AbstractLinkedProcessorSlot { @Override diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java index 6632b792f0..af89ea7f83 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java @@ -92,6 +92,7 @@ public static T loadHighestPriorityInstance(Class clazz) { } /** + * Load and sorted SPI instance list. * Load the SPI instance list for provided SPI interface. * * @param clazz class of the SPI @@ -124,6 +125,8 @@ public static List loadInstanceList(Class clazz) { /** * Load the sorted SPI instance list for provided SPI interface. * + * Note: each call return new instances. + * * @param clazz class of the SPI * @param SPI type * @return sorted SPI instance list @@ -131,13 +134,8 @@ public static List loadInstanceList(Class clazz) { */ public static List loadInstanceListSorted(Class clazz) { try { - String key = clazz.getName(); - // Not thread-safe, as it's expected to be resolved in a thread-safe context. - ServiceLoader serviceLoader = SERVICE_LOADER_MAP.get(key); - if (serviceLoader == null) { - serviceLoader = ServiceLoader.load(clazz); - SERVICE_LOADER_MAP.put(key, serviceLoader); - } + // Call ServiceLoader.load(clazz) to get new instance + ServiceLoader serviceLoader = ServiceLoader.load(clazz); List> orderWrappers = new ArrayList<>(); for (T spi : serviceLoader) { diff --git a/sentinel-core/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot b/sentinel-core/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot new file mode 100644 index 0000000000..f4a3cf7955 --- /dev/null +++ b/sentinel-core/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot @@ -0,0 +1,9 @@ +# Sentinel default ProcessorSlots +com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot +com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot +com.alibaba.csp.sentinel.slots.logger.LogSlot +com.alibaba.csp.sentinel.slots.statistic.StatisticSlot +com.alibaba.csp.sentinel.slots.system.SystemSlot +com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot +com.alibaba.csp.sentinel.slots.block.flow.FlowSlot +com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot \ No newline at end of file diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilderTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilderTest.java new file mode 100644 index 0000000000..e90f2206fe --- /dev/null +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilderTest.java @@ -0,0 +1,89 @@ +/* + * 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; + +import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot; +import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; +import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; +import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; +import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; +import com.alibaba.csp.sentinel.slots.logger.LogSlot; +import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; +import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; +import com.alibaba.csp.sentinel.slots.system.SystemSlot; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test cases for {@link DefaultSlotChainBuilder}. + * + * @author cdfive + */ +public class DefaultSlotChainBuilderTest { + + @Test + public void testBuild() { + DefaultSlotChainBuilder builder = new DefaultSlotChainBuilder(); + ProcessorSlotChain slotChain = builder.build(); + assertNotNull(slotChain); + + // Verify the order of slot + AbstractLinkedProcessorSlot next = slotChain.getNext(); + assertTrue(next instanceof NodeSelectorSlot); + + // Store the first NodeSelectorSlot instance + NodeSelectorSlot nodeSelectorSlot = (NodeSelectorSlot) next; + + next = next.getNext(); + assertTrue(next instanceof ClusterBuilderSlot); + + next = next.getNext(); + assertTrue(next instanceof LogSlot); + + next = next.getNext(); + assertTrue(next instanceof StatisticSlot); + + next = next.getNext(); + assertTrue(next instanceof SystemSlot); + + next = next.getNext(); + assertTrue(next instanceof AuthoritySlot); + + next = next.getNext(); + assertTrue(next instanceof FlowSlot); + + next = next.getNext(); + assertTrue(next instanceof DegradeSlot); + + next = next.getNext(); + assertNull(next); + + // Build again to verify different instances + ProcessorSlotChain slotChain2 = builder.build(); + assertNotNull(slotChain2); + // Verify the two ProcessorSlotChain instances are different + assertNotSame(slotChain, slotChain2); + + next = slotChain2.getNext(); + assertTrue(next instanceof NodeSelectorSlot); + // Store the second NodeSelectorSlot instance + NodeSelectorSlot nodeSelectorSlot2 = (NodeSelectorSlot) next; + // Verify the two NodeSelectorSlot instances are different + assertNotSame(nodeSelectorSlot, nodeSelectorSlot2); + } +} diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java new file mode 100644 index 0000000000..35944be4b5 --- /dev/null +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java @@ -0,0 +1,114 @@ +/* + * 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.util; + +import com.alibaba.csp.sentinel.slotchain.ProcessorSlot; +import com.alibaba.csp.sentinel.slotchain.SlotChainBuilder; +import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder; +import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; +import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; +import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; +import com.alibaba.csp.sentinel.slots.logger.LogSlot; +import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; +import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; +import com.alibaba.csp.sentinel.slots.system.SystemSlot; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Test cases for {@link SpiLoader}. + * + * @author cdfive + */ +public class SpiLoaderTest { + + @Test + public void testLoadFirstInstance() { + ProcessorSlot processorSlot = SpiLoader.loadFirstInstance(ProcessorSlot.class); + assertNotNull(processorSlot); + + SlotChainBuilder slotChainBuilder = SpiLoader.loadFirstInstance(SlotChainBuilder.class); + assertNotNull(slotChainBuilder); + assertTrue(slotChainBuilder instanceof DefaultSlotChainBuilder); + } + + @Test + public void testLoadHighestPriorityInstance() { + ProcessorSlot processorSlot = SpiLoader.loadHighestPriorityInstance(ProcessorSlot.class); + assertNotNull(processorSlot); + + // NodeSelectorSlot is highest order with @SpiOrder(-9000), among all slots + assertTrue(processorSlot instanceof NodeSelectorSlot); + } + + @Test + public void testLoadInstanceList() { + List slots = SpiLoader.loadInstanceList(ProcessorSlot.class); + assertNotNull(slots); + + // Total 8 default slot in sentinel-core + assertEquals(8, slots.size()); + + // Store the first slot of slots + ProcessorSlot firstSlot = slots.get(0); + + // Call loadInstanceList again + List slots2 = SpiLoader.loadInstanceList(ProcessorSlot.class); + assertNotSame(slots, slots2); + + // Store the first slot of slots + ProcessorSlot firstSlot2 = slots2.get(0); + + // As SERVICE_LOADER_MAP in SpiLoader cached the instance, so they're same instances + assertSame(firstSlot, firstSlot2); + } + + @Test + public void testLoadInstanceListSorted() { + List sortedSlots = SpiLoader.loadInstanceListSorted(ProcessorSlot.class); + assertNotNull(sortedSlots); + + // Total 8 default slot in sentinel-core + assertEquals(8, sortedSlots.size()); + + // Verify the order of slot + int index = 0; + assertTrue(sortedSlots.get(index++) instanceof NodeSelectorSlot); + assertTrue(sortedSlots.get(index++) instanceof ClusterBuilderSlot); + assertTrue(sortedSlots.get(index++) instanceof LogSlot); + assertTrue(sortedSlots.get(index++) instanceof StatisticSlot); + assertTrue(sortedSlots.get(index++) instanceof SystemSlot); + assertTrue(sortedSlots.get(index++) instanceof AuthoritySlot); + assertTrue(sortedSlots.get(index++) instanceof FlowSlot); + assertTrue(sortedSlots.get(index++) instanceof DegradeSlot); + + // Verify each call return different instances + List sortedSlots2 = SpiLoader.loadInstanceListSorted(ProcessorSlot.class); + assertNotSame(sortedSlots, sortedSlots2); + assertEquals(sortedSlots.size(), sortedSlots2.size()); + for (int i = 0; i < sortedSlots.size(); i++) { + ProcessorSlot slot = sortedSlots.get(i); + ProcessorSlot slot2 = sortedSlots2.get(i); + assertNotSame(slot, slot2); + assertNotEquals(slot, slot2); + assertEquals(slot.getClass(), slot2.getClass()); + } + } +} diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java deleted file mode 100644 index 61705fe5df..0000000000 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java +++ /dev/null @@ -1,52 +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; - -import com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain; -import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; -import com.alibaba.csp.sentinel.slotchain.SlotChainBuilder; -import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; -import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; -import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; -import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowSlot; -import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; -import com.alibaba.csp.sentinel.slots.logger.LogSlot; -import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; -import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; -import com.alibaba.csp.sentinel.slots.system.SystemSlot; - -/** - * @author Eric Zhao - * @since 0.2.0 - */ -public class HotParamSlotChainBuilder implements SlotChainBuilder { - - @Override - public ProcessorSlotChain build() { - ProcessorSlotChain chain = new DefaultProcessorSlotChain(); - chain.addLast(new NodeSelectorSlot()); - chain.addLast(new ClusterBuilderSlot()); - chain.addLast(new LogSlot()); - chain.addLast(new StatisticSlot()); - chain.addLast(new ParamFlowSlot()); - chain.addLast(new SystemSlot()); - chain.addLast(new AuthoritySlot()); - chain.addLast(new FlowSlot()); - chain.addLast(new DegradeSlot()); - - return chain; - } -} diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java index 001f7d3fdf..77129dbf78 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java @@ -27,6 +27,7 @@ import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.alibaba.csp.sentinel.spi.SpiOrder; import com.alibaba.csp.sentinel.util.StringUtil; /** @@ -36,6 +37,7 @@ * @author Eric Zhao * @since 0.2.0 */ +@SpiOrder(-5000) public class ParamFlowSlot extends AbstractLinkedProcessorSlot { private static final Map metricsMap = new ConcurrentHashMap<>(); diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot b/sentinel-extension/sentinel-parameter-flow-control/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot new file mode 100644 index 0000000000..8832f16bbb --- /dev/null +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot @@ -0,0 +1 @@ +com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowSlot \ No newline at end of file diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder b/sentinel-extension/sentinel-parameter-flow-control/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder deleted file mode 100644 index a0354c1ed6..0000000000 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder +++ /dev/null @@ -1 +0,0 @@ -com.alibaba.csp.sentinel.slots.HotParamSlotChainBuilder \ No newline at end of file From 3331aa8b3e713f140e03c8cda432ec681b66845f Mon Sep 17 00:00:00 2001 From: cdfive Date: Fri, 31 Jan 2020 22:47:26 +0800 Subject: [PATCH 2/6] Fix conflicts and some improvement --- .../slots/DefaultSlotChainBuilder.java | 3 +- .../alibaba/csp/sentinel/util/SpiLoader.java | 35 +++++++++++++++++++ .../csp/sentinel/util/SpiLoaderTest.java | 34 +++++++++++++++++- 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilder.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilder.java index 025d12791d..797206bb7b 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilder.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilder.java @@ -37,7 +37,8 @@ public class DefaultSlotChainBuilder implements SlotChainBuilder { public ProcessorSlotChain build() { ProcessorSlotChain chain = new DefaultProcessorSlotChain(); - List sortedSlotList = SpiLoader.loadInstanceListSorted(ProcessorSlot.class); + // Note: the instances of ProcessorSlot should be different, since they are not stateless. + List sortedSlotList = SpiLoader.loadDifferentInstanceListSorted(ProcessorSlot.class); for (ProcessorSlot slot : sortedSlotList) { if (!(slot instanceof AbstractLinkedProcessorSlot)) { RecordLog.warn("The ProcessorSlot(" + slot.getClass().getCanonicalName() + ") is not an instance of AbstractLinkedProcessorSlot, can't be added into ProcessorSlotChain"); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java index 98679f8490..36e1ff4311 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java @@ -199,6 +199,41 @@ public static List loadInstanceListSorted(Class clazz) { } } + /** + * Load the sorted and different SPI instance list for provided SPI interface. + * + * Note: each call return new instances. + * + * @param clazz class of the SPI + * @param SPI type + * @return sorted and different SPI instance list + * @since 1.7.2 + */ + public static List loadDifferentInstanceListSorted(Class clazz) { + try { + // To make sure the instances loaded are different. + ServiceLoader serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz); + + List> orderWrappers = new ArrayList<>(); + for (T spi : serviceLoader) { + int order = SpiOrderResolver.resolveOrder(spi); + // Since SPI is lazy initialized in ServiceLoader, we use online sort algorithm here. + SpiOrderResolver.insertSorted(orderWrappers, spi, order); + RecordLog.info("[SpiLoader] Found {0} SPI: {1} with order " + order, clazz.getSimpleName(), + spi.getClass().getCanonicalName()); + } + List list = new ArrayList<>(); + for (int i = 0; i < orderWrappers.size(); i++) { + list.add(i, orderWrappers.get(i).spi); + } + return list; + } catch (Throwable t) { + RecordLog.warn("[SpiLoader] ERROR: loadInstanceListSorted failed", t); + t.printStackTrace(); + return new ArrayList<>(); + } + } + private static class SpiOrderResolver { private static void insertSorted(List> list, T spi, int order) { int idx = 0; diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java index 35944be4b5..e8127303a0 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java @@ -106,9 +106,41 @@ public void testLoadInstanceListSorted() { for (int i = 0; i < sortedSlots.size(); i++) { ProcessorSlot slot = sortedSlots.get(i); ProcessorSlot slot2 = sortedSlots2.get(i); + assertEquals(slot.getClass(), slot2.getClass()); + } + } + + @Test + public void testLoadDifferentInstanceListSorted() { + List sortedSlots = SpiLoader.loadInstanceListSorted(ProcessorSlot.class); + assertNotNull(sortedSlots); + + // Total 8 default slot in sentinel-core + assertEquals(8, sortedSlots.size()); + + // Verify the order of slot + int index = 0; + assertTrue(sortedSlots.get(index++) instanceof NodeSelectorSlot); + assertTrue(sortedSlots.get(index++) instanceof ClusterBuilderSlot); + assertTrue(sortedSlots.get(index++) instanceof LogSlot); + assertTrue(sortedSlots.get(index++) instanceof StatisticSlot); + assertTrue(sortedSlots.get(index++) instanceof SystemSlot); + assertTrue(sortedSlots.get(index++) instanceof AuthoritySlot); + assertTrue(sortedSlots.get(index++) instanceof FlowSlot); + assertTrue(sortedSlots.get(index++) instanceof DegradeSlot); + + // Verify each call return different instances + List sortedSlots2 = SpiLoader.loadDifferentInstanceListSorted(ProcessorSlot.class); + assertNotSame(sortedSlots, sortedSlots2); + assertEquals(sortedSlots.size(), sortedSlots2.size()); + for (int i = 0; i < sortedSlots.size(); i++) { + ProcessorSlot slot = sortedSlots.get(i); + ProcessorSlot slot2 = sortedSlots2.get(i); + assertEquals(slot.getClass(), slot2.getClass()); + + // Verify the instances are different assertNotSame(slot, slot2); assertNotEquals(slot, slot2); - assertEquals(slot.getClass(), slot2.getClass()); } } } From f9cd3df39c61fb927f7c176a447bcad0066c2fc4 Mon Sep 17 00:00:00 2001 From: cdfive Date: Fri, 13 Mar 2020 17:22:26 +0800 Subject: [PATCH 3/6] Make GatewayFlowSlot loaded by SPI, mark all slots with @SpiOrder from -10000 to -1000, improve comment --- .../gateway/common/slot/GatewayFlowSlot.java | 2 + .../common/slot/GatewaySlotChainBuilder.java | 57 ------------------- ...ibaba.csp.sentinel.slotchain.ProcessorSlot | 1 + ...ba.csp.sentinel.slotchain.SlotChainBuilder | 1 - .../slots/block/authority/AuthoritySlot.java | 2 +- .../clusterbuilder/ClusterBuilderSlot.java | 2 +- .../csp/sentinel/slots/logger/LogSlot.java | 2 +- .../slots/nodeselector/NodeSelectorSlot.java | 2 +- .../slots/statistic/StatisticSlot.java | 2 +- .../csp/sentinel/slots/system/SystemSlot.java | 2 +- .../alibaba/csp/sentinel/util/SpiLoader.java | 4 +- .../slots/DefaultSlotChainBuilderTest.java | 4 +- .../slots/block/flow/param/ParamFlowSlot.java | 2 +- 13 files changed, 14 insertions(+), 69 deletions(-) delete mode 100644 sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java create mode 100644 sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot delete mode 100644 sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder diff --git a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewayFlowSlot.java b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewayFlowSlot.java index 15151729d6..b0932b7c99 100644 --- a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewayFlowSlot.java +++ b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewayFlowSlot.java @@ -27,11 +27,13 @@ import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException; import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule; import com.alibaba.csp.sentinel.slots.block.flow.param.ParameterMetricStorage; +import com.alibaba.csp.sentinel.spi.SpiOrder; /** * @author Eric Zhao * @since 1.6.1 */ +@SpiOrder(-4000) public class GatewayFlowSlot extends AbstractLinkedProcessorSlot { @Override diff --git a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java deleted file mode 100644 index 383e465326..0000000000 --- a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 - * - * https://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.adapter.gateway.common.slot; - -import com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain; -import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; -import com.alibaba.csp.sentinel.slotchain.SlotChainBuilder; -import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; -import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; -import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; -import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowSlot; -import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; -import com.alibaba.csp.sentinel.slots.logger.LogSlot; -import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; -import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; -import com.alibaba.csp.sentinel.slots.system.SystemSlot; - -/** - * @author Eric Zhao - * @since 1.6.1 - */ -public class GatewaySlotChainBuilder implements SlotChainBuilder { - - @Override - public ProcessorSlotChain build() { - ProcessorSlotChain chain = new DefaultProcessorSlotChain(); - // Prepare slot - chain.addLast(new NodeSelectorSlot()); - chain.addLast(new ClusterBuilderSlot()); - // Stat slot - chain.addLast(new LogSlot()); - chain.addLast(new StatisticSlot()); - // Rule checking slot - chain.addLast(new AuthoritySlot()); - chain.addLast(new SystemSlot()); - chain.addLast(new GatewayFlowSlot()); - - chain.addLast(new ParamFlowSlot()); - chain.addLast(new FlowSlot()); - chain.addLast(new DegradeSlot()); - - return chain; - } -} diff --git a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot new file mode 100644 index 0000000000..190da2e814 --- /dev/null +++ b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot @@ -0,0 +1 @@ +com.alibaba.csp.sentinel.adapter.gateway.common.slot.GatewayFlowSlot \ No newline at end of file diff --git a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder deleted file mode 100644 index 5f9fde567e..0000000000 --- a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder +++ /dev/null @@ -1 +0,0 @@ -com.alibaba.csp.sentinel.adapter.gateway.common.slot.GatewaySlotChainBuilder \ No newline at end of file diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java index 3146429a23..617521a676 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthoritySlot.java @@ -31,7 +31,7 @@ * @author leyou * @author Eric Zhao */ -@SpiOrder(-3000) +@SpiOrder(-6000) public class AuthoritySlot extends AbstractLinkedProcessorSlot { @Override 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 f25289c9f6..62ea366a67 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 @@ -45,7 +45,7 @@ * * @author jialiang.linjl */ -@SpiOrder(-8000) +@SpiOrder(-9000) public class ClusterBuilderSlot extends AbstractLinkedProcessorSlot { /** diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java index 57b1ca19dc..a7515b0168 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java @@ -27,7 +27,7 @@ * A {@link com.alibaba.csp.sentinel.slotchain.ProcessorSlot} that is response for logging block exceptions * to provide concrete logs for troubleshooting. */ -@SpiOrder(-7000) +@SpiOrder(-8000) public class LogSlot extends AbstractLinkedProcessorSlot { @Override 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 fb26c6680b..c965137f06 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 @@ -123,7 +123,7 @@ * @see EntranceNode * @see ContextUtil */ -@SpiOrder(-9000) +@SpiOrder(-10000) public class NodeSelectorSlot extends AbstractLinkedProcessorSlot { /** diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java index 9a6b683b53..96005d31e2 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java @@ -48,7 +48,7 @@ * @author jialiang.linjl * @author Eric Zhao */ -@SpiOrder(-6000) +@SpiOrder(-7000) public class StatisticSlot extends AbstractLinkedProcessorSlot { @Override diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java index f87cf68045..5912e62210 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java @@ -28,7 +28,7 @@ * @author jialiang.linjl * @author leyou */ -@SpiOrder(-4000) +@SpiOrder(-5000) public class SystemSlot extends AbstractLinkedProcessorSlot { @Override diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java index 36e1ff4311..9ca6b6e840 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java @@ -211,7 +211,7 @@ public static List loadInstanceListSorted(Class clazz) { */ public static List loadDifferentInstanceListSorted(Class clazz) { try { - // To make sure the instances loaded are different. + // Not use SERVICE_LOADER_MAP, to make sure the instances loaded are different. ServiceLoader serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz); List> orderWrappers = new ArrayList<>(); @@ -228,7 +228,7 @@ public static List loadDifferentInstanceListSorted(Class clazz) { } return list; } catch (Throwable t) { - RecordLog.warn("[SpiLoader] ERROR: loadInstanceListSorted failed", t); + RecordLog.warn("[SpiLoader] ERROR: loadDifferentInstanceListSorted failed", t); t.printStackTrace(); return new ArrayList<>(); } diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilderTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilderTest.java index e90f2206fe..ecdea6dad1 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilderTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilderTest.java @@ -59,10 +59,10 @@ public void testBuild() { assertTrue(next instanceof StatisticSlot); next = next.getNext(); - assertTrue(next instanceof SystemSlot); + assertTrue(next instanceof AuthoritySlot); next = next.getNext(); - assertTrue(next instanceof AuthoritySlot); + assertTrue(next instanceof SystemSlot); next = next.getNext(); assertTrue(next instanceof FlowSlot); diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java index 3be42249d6..8bf0dff074 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java @@ -31,7 +31,7 @@ * @author Eric Zhao * @since 0.2.0 */ -@SpiOrder(-5000) +@SpiOrder(-3000) public class ParamFlowSlot extends AbstractLinkedProcessorSlot { @Override From ce0797b76f58cf9e9f077552f6099b26e67a0657 Mon Sep 17 00:00:00 2001 From: cdfive Date: Fri, 13 Mar 2020 17:58:43 +0800 Subject: [PATCH 4/6] Fix SpiLoaderTest test cases --- .../java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java index e8127303a0..ba353fa2d2 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java @@ -94,8 +94,8 @@ public void testLoadInstanceListSorted() { assertTrue(sortedSlots.get(index++) instanceof ClusterBuilderSlot); assertTrue(sortedSlots.get(index++) instanceof LogSlot); assertTrue(sortedSlots.get(index++) instanceof StatisticSlot); - assertTrue(sortedSlots.get(index++) instanceof SystemSlot); assertTrue(sortedSlots.get(index++) instanceof AuthoritySlot); + assertTrue(sortedSlots.get(index++) instanceof SystemSlot); assertTrue(sortedSlots.get(index++) instanceof FlowSlot); assertTrue(sortedSlots.get(index++) instanceof DegradeSlot); @@ -124,8 +124,8 @@ public void testLoadDifferentInstanceListSorted() { assertTrue(sortedSlots.get(index++) instanceof ClusterBuilderSlot); assertTrue(sortedSlots.get(index++) instanceof LogSlot); assertTrue(sortedSlots.get(index++) instanceof StatisticSlot); - assertTrue(sortedSlots.get(index++) instanceof SystemSlot); assertTrue(sortedSlots.get(index++) instanceof AuthoritySlot); + assertTrue(sortedSlots.get(index++) instanceof SystemSlot); assertTrue(sortedSlots.get(index++) instanceof FlowSlot); assertTrue(sortedSlots.get(index++) instanceof DegradeSlot); From b5925b8a861c9ca8b2f922ba1f361c2c128ce83d Mon Sep 17 00:00:00 2001 From: cdfive Date: Wed, 18 Mar 2020 22:13:00 +0800 Subject: [PATCH 5/6] Reserved gateway and hot param slot chain builder and mark them as @Deprecated --- .../common/slot/GatewaySlotChainBuilder.java | 62 +++++++++++++++++++ .../slots/HotParamSlotChainBuilder.java | 57 +++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java create mode 100644 sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java diff --git a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java new file mode 100644 index 0000000000..34be51e8e2 --- /dev/null +++ b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java @@ -0,0 +1,62 @@ +/* + * 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 + * + * https://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.adapter.gateway.common.slot; + +import com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain; +import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; +import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder; +import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; +import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; +import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowSlot; +import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; +import com.alibaba.csp.sentinel.slots.logger.LogSlot; +import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; +import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; +import com.alibaba.csp.sentinel.slots.system.SystemSlot; + +/** + * @author Eric Zhao + * @since 1.6.1 + * + * @deprecated since 1.7.2, we can use @SpiOrder(-4000) to adjust the order of slots, + * this class is reserved for compatibility with older versions. + * @see {@link GatewayFlowSlot} + */ +@Deprecated +public class GatewaySlotChainBuilder extends DefaultSlotChainBuilder { + + @Override + public ProcessorSlotChain build() { + ProcessorSlotChain chain = new DefaultProcessorSlotChain(); + // Prepare slot + chain.addLast(new NodeSelectorSlot()); + chain.addLast(new ClusterBuilderSlot()); + // Stat slot + chain.addLast(new LogSlot()); + chain.addLast(new StatisticSlot()); + // Rule checking slot + chain.addLast(new AuthoritySlot()); + chain.addLast(new SystemSlot()); + chain.addLast(new GatewayFlowSlot()); + + chain.addLast(new ParamFlowSlot()); + chain.addLast(new FlowSlot()); + chain.addLast(new DegradeSlot()); + + return chain; + } +} diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java new file mode 100644 index 0000000000..6c6b76a690 --- /dev/null +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java @@ -0,0 +1,57 @@ +/* + * 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; + +import com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain; +import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; +import com.alibaba.csp.sentinel.slotchain.SlotChainBuilder; +import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; +import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; +import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowSlot; +import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; +import com.alibaba.csp.sentinel.slots.logger.LogSlot; +import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; +import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; +import com.alibaba.csp.sentinel.slots.system.SystemSlot; + +/** + * @author Eric Zhao + * @since 0.2.0 + * + * @deprecated since 1.7.2, we can use @SpiOrder(-3000) to adjust the order of slots, + * this class is reserved for compatibility with older versions. + * @see {@link ParamFlowSlot} + */ +@Deprecated +public class HotParamSlotChainBuilder extends DefaultSlotChainBuilder { + + @Override + public ProcessorSlotChain build() { + ProcessorSlotChain chain = new DefaultProcessorSlotChain(); + chain.addLast(new NodeSelectorSlot()); + chain.addLast(new ClusterBuilderSlot()); + chain.addLast(new LogSlot()); + chain.addLast(new StatisticSlot()); + chain.addLast(new AuthoritySlot()); + chain.addLast(new SystemSlot()); + chain.addLast(new ParamFlowSlot()); + chain.addLast(new FlowSlot()); + chain.addLast(new DegradeSlot()); + + return chain; + } +} From 68be55c1b4bd56147c023f158a59ac5f7667ca8a Mon Sep 17 00:00:00 2001 From: cdfive Date: Thu, 26 Mar 2020 10:30:45 +0800 Subject: [PATCH 6/6] Keep GatewaySlotChainBuilder,HotParamSlotChainBuilder class empty and just extends from DefaultSlotChainBuilder,improve comment --- .../common/slot/GatewaySlotChainBuilder.java | 36 ++----------------- .../slots/HotParamSlotChainBuilder.java | 31 ++-------------- 2 files changed, 6 insertions(+), 61 deletions(-) diff --git a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java index 34be51e8e2..204b46198f 100644 --- a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java +++ b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/slot/GatewaySlotChainBuilder.java @@ -15,48 +15,18 @@ */ package com.alibaba.csp.sentinel.adapter.gateway.common.slot; -import com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain; -import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder; -import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; -import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; -import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; -import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowSlot; -import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; -import com.alibaba.csp.sentinel.slots.logger.LogSlot; -import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; -import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; -import com.alibaba.csp.sentinel.slots.system.SystemSlot; /** * @author Eric Zhao * @since 1.6.1 * - * @deprecated since 1.7.2, we can use @SpiOrder(-4000) to adjust the order of slots, + * @deprecated since 1.7.2, we can use @SpiOrder(-4000) to adjust the order of {@link GatewayFlowSlot}, * this class is reserved for compatibility with older versions. - * @see {@link GatewayFlowSlot} + * @see GatewayFlowSlot + * @see DefaultSlotChainBuilder */ @Deprecated public class GatewaySlotChainBuilder extends DefaultSlotChainBuilder { - @Override - public ProcessorSlotChain build() { - ProcessorSlotChain chain = new DefaultProcessorSlotChain(); - // Prepare slot - chain.addLast(new NodeSelectorSlot()); - chain.addLast(new ClusterBuilderSlot()); - // Stat slot - chain.addLast(new LogSlot()); - chain.addLast(new StatisticSlot()); - // Rule checking slot - chain.addLast(new AuthoritySlot()); - chain.addLast(new SystemSlot()); - chain.addLast(new GatewayFlowSlot()); - - chain.addLast(new ParamFlowSlot()); - chain.addLast(new FlowSlot()); - chain.addLast(new DegradeSlot()); - - return chain; - } } diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java index 6c6b76a690..d44aab3dfa 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/HotParamSlotChainBuilder.java @@ -15,43 +15,18 @@ */ package com.alibaba.csp.sentinel.slots; -import com.alibaba.csp.sentinel.slotchain.DefaultProcessorSlotChain; -import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; -import com.alibaba.csp.sentinel.slotchain.SlotChainBuilder; -import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; -import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; -import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowSlot; -import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; -import com.alibaba.csp.sentinel.slots.logger.LogSlot; -import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; -import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; -import com.alibaba.csp.sentinel.slots.system.SystemSlot; /** * @author Eric Zhao * @since 0.2.0 * - * @deprecated since 1.7.2, we can use @SpiOrder(-3000) to adjust the order of slots, + * @deprecated since 1.7.2, we can use @SpiOrder(-3000) to adjust the order of {@link ParamFlowSlot}, * this class is reserved for compatibility with older versions. - * @see {@link ParamFlowSlot} + * @see ParamFlowSlot + * @see DefaultSlotChainBuilder */ @Deprecated public class HotParamSlotChainBuilder extends DefaultSlotChainBuilder { - @Override - public ProcessorSlotChain build() { - ProcessorSlotChain chain = new DefaultProcessorSlotChain(); - chain.addLast(new NodeSelectorSlot()); - chain.addLast(new ClusterBuilderSlot()); - chain.addLast(new LogSlot()); - chain.addLast(new StatisticSlot()); - chain.addLast(new AuthoritySlot()); - chain.addLast(new SystemSlot()); - chain.addLast(new ParamFlowSlot()); - chain.addLast(new FlowSlot()); - chain.addLast(new DegradeSlot()); - - return chain; - } }