From 2237727a11e3eca7cb3f590023518e334b761fe5 Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Wed, 21 Nov 2018 16:22:41 +0800 Subject: [PATCH] Add prioritized entry support in ProcessorSlot and SphU (#255) - Refactor the slot interface to support prioritized entry - Add `entryWithPriority` in SphU Signed-off-by: Eric Zhao --- .../java/com/alibaba/csp/sentinel/CtSph.java | 56 ++++++++++++------- .../java/com/alibaba/csp/sentinel/Sph.java | 14 +++++ .../java/com/alibaba/csp/sentinel/SphU.java | 25 +++++++++ .../AbstractLinkedProcessorSlot.java | 8 +-- .../slotchain/DefaultProcessorSlotChain.java | 8 +-- .../csp/sentinel/slotchain/ProcessorSlot.java | 15 +++-- .../sentinel/slots/block/RuleConstant.java | 2 +- .../slots/block/authority/AuthoritySlot.java | 4 +- .../slots/block/degrade/DegradeSlot.java | 4 +- .../sentinel/slots/block/flow/FlowSlot.java | 6 +- .../clusterbuilder/ClusterBuilderSlot.java | 4 +- .../csp/sentinel/slots/logger/LogSlot.java | 4 +- .../slots/nodeselector/NodeSelectorSlot.java | 4 +- .../slots/statistic/StatisticSlot.java | 6 +- .../csp/sentinel/slots/system/SystemSlot.java | 6 +- .../com/alibaba/csp/sentinel/CtSphTest.java | 6 +- .../csp/sentinel/demo/slot/DemoSlot.java | 4 +- .../slots/block/flow/param/ParamFlowSlot.java | 6 +- .../block/flow/param/ParamFlowSlotTest.java | 6 +- 19 files changed, 123 insertions(+), 65 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/CtSph.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/CtSph.java index e8750710ce..fcd8df6d70 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/CtSph.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/CtSph.java @@ -61,7 +61,8 @@ private AsyncEntry asyncEntryWithNoChain(ResourceWrapper resourceWrapper, Contex return entry; } - private AsyncEntry asyncEntryInternal(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException { + private AsyncEntry asyncEntryWithPriorityInternal(ResourceWrapper resourceWrapper, int count, boolean prioritized, + Object... args) throws BlockException { Context context = ContextUtil.getContext(); if (context instanceof NullContext) { // The {@link NullContext} indicates that the amount of context has exceeded the threshold, @@ -87,7 +88,7 @@ private AsyncEntry asyncEntryInternal(ResourceWrapper resourceWrapper, int count AsyncEntry asyncEntry = new AsyncEntry(resourceWrapper, chain, context); try { - chain.entry(context, resourceWrapper, null, count, args); + chain.entry(context, resourceWrapper, null, count, prioritized, args); // Initiate the async context only when the entry successfully passed the slot chain. asyncEntry.initAsyncContext(); // The asynchronous call may take time in background, and current context should not be hanged on it. @@ -108,23 +109,12 @@ private AsyncEntry asyncEntryInternal(ResourceWrapper resourceWrapper, int count return asyncEntry; } - /** - * Do all {@link Rule}s checking about the resource. - * - *

Each distinct resource will use a {@link ProcessorSlot} to do rules checking. Same resource will use - * same {@link ProcessorSlot} globally.

- * - *

Note that total {@link ProcessorSlot} count must not exceed {@link Constants#MAX_SLOT_CHAIN_SIZE}, - * otherwise no rules checking will do. In this condition, all requests will pass directly, with no checking - * or exception.

- * - * @param resourceWrapper resource name - * @param count tokens needed - * @param args arguments of user method call - * @return {@link Entry} represents this call - * @throws BlockException if any rule's threshold is exceeded - */ - public Entry entry(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException { + private AsyncEntry asyncEntryInternal(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException { + return asyncEntryWithPriorityInternal(resourceWrapper, count, false, args); + } + + private Entry entryWithPriority(ResourceWrapper resourceWrapper, int count, boolean prioritized, Object... args) + throws BlockException { Context context = ContextUtil.getContext(); if (context instanceof NullContext) { // The {@link NullContext} indicates that the amount of context has exceeded the threshold, @@ -154,7 +144,7 @@ public Entry entry(ResourceWrapper resourceWrapper, int count, Object... args) t Entry e = new CtEntry(resourceWrapper, chain, context); try { - chain.entry(context, resourceWrapper, null, count, args); + chain.entry(context, resourceWrapper, null, count, prioritized, args); } catch (BlockException e1) { e.exit(count, args); throw e1; @@ -165,6 +155,26 @@ public Entry entry(ResourceWrapper resourceWrapper, int count, Object... args) t return e; } + /** + * Do all {@link Rule}s checking about the resource. + * + *

Each distinct resource will use a {@link ProcessorSlot} to do rules checking. Same resource will use + * same {@link ProcessorSlot} globally.

+ * + *

Note that total {@link ProcessorSlot} count must not exceed {@link Constants#MAX_SLOT_CHAIN_SIZE}, + * otherwise no rules checking will do. In this condition, all requests will pass directly, with no checking + * or exception.

+ * + * @param resourceWrapper resource name + * @param count tokens needed + * @param args arguments of user method call + * @return {@link Entry} represents this call + * @throws BlockException if any rule's threshold is exceeded + */ + public Entry entry(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException { + return entryWithPriority(resourceWrapper, count, false, args); + } + /** * Get {@link ProcessorSlotChain} of the resource. new {@link ProcessorSlotChain} will * be created if the resource doesn't relate one. @@ -305,4 +315,10 @@ public AsyncEntry asyncEntry(String name, EntryType type, int count, Object... a StringResourceWrapper resource = new StringResourceWrapper(name, type); return asyncEntryInternal(resource, count, args); } + + @Override + public Entry entryWithPriority(String name, EntryType type, int count, boolean prioritized) throws BlockException { + StringResourceWrapper resource = new StringResourceWrapper(name, type); + return entryWithPriority(resource, count, prioritized); + } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Sph.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Sph.java index 8564db56d4..7e1f2a90bc 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Sph.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Sph.java @@ -155,4 +155,18 @@ public interface Sph { * @since 0.2.0 */ AsyncEntry asyncEntry(String name, EntryType type, int count, Object... args) throws BlockException; + + /** + * Create a protected resource with priority. + * + * @param name the unique name for the protected resource + * @param type the resource is an inbound or an outbound method. This is used + * to mark whether it can be blocked when the system is unstable + * @param count the count that the resource requires + * @param prioritized whether the entry is prioritized + * @return entry get + * @throws BlockException if the block criteria is met + * @since 1.4.0 + */ + Entry entryWithPriority(String name, EntryType type, int count, boolean prioritized) throws BlockException; } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphU.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphU.java index ee3f769db3..696b686fe6 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphU.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphU.java @@ -242,4 +242,29 @@ public static AsyncEntry asyncEntry(String name, EntryType type) throws BlockExc public static AsyncEntry asyncEntry(String name, EntryType type, int count, Object... args) throws BlockException { return Env.sph.asyncEntry(name, type, count, args); } + + /** + * Checking all {@link Rule}s related the resource. The entry is prioritized. + * + * @param name the unique name for the protected resource + * @throws BlockException if the block criteria is met, eg. when any rule's threshold is exceeded. + * @since 1.4.0 + */ + public static Entry entryWithPriority(String name) throws BlockException { + return Env.sph.entryWithPriority(name, EntryType.OUT, 1, true); + } + + /** + * Checking all {@link Rule}s related the resource. The entry is prioritized. + * + * @param name the unique name for the protected resource + * @param type the resource is an inbound or an outbound method. This is used + * to mark whether it can be blocked when the system is unstable, + * only inbound traffic could be blocked by {@link SystemRule} + * @throws BlockException if the block criteria is met, eg. when any rule's threshold is exceeded. + * @since 1.4.0 + */ + public static Entry entryWithPriority(String name, EntryType type) throws BlockException { + return Env.sph.entryWithPriority(name, type, 1, true); + } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/AbstractLinkedProcessorSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/AbstractLinkedProcessorSlot.java index 3d10186f8a..2da213a21b 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/AbstractLinkedProcessorSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/AbstractLinkedProcessorSlot.java @@ -26,18 +26,18 @@ public abstract class AbstractLinkedProcessorSlot implements ProcessorSlot private AbstractLinkedProcessorSlot next = null; @Override - public void fireEntry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, Object... args) + public void fireEntry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, boolean prioritized, Object... args) throws Throwable { if (next != null) { - next.transformEntry(context, resourceWrapper, obj, count, args); + next.transformEntry(context, resourceWrapper, obj, count, prioritized, args); } } @SuppressWarnings("unchecked") - void transformEntry(Context context, ResourceWrapper resourceWrapper, Object o, int count, Object... args) + void transformEntry(Context context, ResourceWrapper resourceWrapper, Object o, int count, boolean prioritized, Object... args) throws Throwable { T t = (T)o; - entry(context, resourceWrapper, t, count, args); + entry(context, resourceWrapper, t, count, prioritized, args); } @Override diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/DefaultProcessorSlotChain.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/DefaultProcessorSlotChain.java index b48301e2b8..5906d1792b 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/DefaultProcessorSlotChain.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/DefaultProcessorSlotChain.java @@ -26,9 +26,9 @@ public class DefaultProcessorSlotChain extends ProcessorSlotChain { AbstractLinkedProcessorSlot first = new AbstractLinkedProcessorSlot() { @Override - public void entry(Context context, ResourceWrapper resourceWrapper, Object t, int count, Object... args) + public void entry(Context context, ResourceWrapper resourceWrapper, Object t, int count, boolean prioritized, Object... args) throws Throwable { - super.fireEntry(context, resourceWrapper, t, count, args); + super.fireEntry(context, resourceWrapper, t, count, prioritized, args); } @Override @@ -70,9 +70,9 @@ public AbstractLinkedProcessorSlot getNext() { } @Override - public void entry(Context context, ResourceWrapper resourceWrapper, Object t, int count, Object... args) + public void entry(Context context, ResourceWrapper resourceWrapper, Object t, int count, boolean prioritized, Object... args) throws Throwable { - first.transformEntry(context, resourceWrapper, t, count, args); + first.transformEntry(context, resourceWrapper, t, count, prioritized, args); } @Override diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/ProcessorSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/ProcessorSlot.java index dfe5449d91..ee387055b9 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/ProcessorSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/ProcessorSlot.java @@ -23,6 +23,7 @@ * @author qinan.qn * @author jialiang.linjl * @author leyou(lihao) + * @author Eric Zhao */ public interface ProcessorSlot { @@ -31,26 +32,28 @@ public interface ProcessorSlot { * * @param context current {@link Context} * @param resourceWrapper current resource - * @param param Generics parameter, usually is a {@link com.alibaba.csp.sentinel.node.Node} + * @param param generics parameter, usually is a {@link com.alibaba.csp.sentinel.node.Node} * @param count tokens needed + * @param prioritized whether the entry is prioritized * @param args parameters of the original call * @throws Throwable blocked exception or unexpected error */ - void entry(Context context, ResourceWrapper resourceWrapper, T param, int count, Object... args) - throws Throwable; + void entry(Context context, ResourceWrapper resourceWrapper, T param, int count, boolean prioritized, + Object... args) throws Throwable; /** - * Means finish of {@link #entry(Context, ResourceWrapper, Object, int, Object...)}. + * Means finish of {@link #entry(Context, ResourceWrapper, Object, int, boolean, Object...)}. * * @param context current {@link Context} * @param resourceWrapper current resource * @param obj relevant object (e.g. Node) * @param count tokens needed + * @param prioritized whether the entry is prioritized * @param args parameters of the original call * @throws Throwable blocked exception or unexpected error */ - void fireEntry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, Object... args) - throws Throwable; + void fireEntry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, boolean prioritized, + Object... args) throws Throwable; /** * Exit of this slot. diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/RuleConstant.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/RuleConstant.java index 60fe15d350..60e898a903 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/RuleConstant.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/RuleConstant.java @@ -17,7 +17,7 @@ import com.alibaba.csp.sentinel.node.IntervalProperty; -/*** +/** * @author youji.zj * @author jialiang.linjl */ 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 f313d71148..e9786c1a82 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 @@ -33,10 +33,10 @@ public class AuthoritySlot extends AbstractLinkedProcessorSlot { @Override - public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) + public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable { checkBlackWhiteAuthority(resourceWrapper, context); - fireEntry(context, resourceWrapper, node, count, args); + fireEntry(context, resourceWrapper, node, count, prioritized, args); } @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 533a935081..f76d81f8cd 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 @@ -29,10 +29,10 @@ public class DegradeSlot extends AbstractLinkedProcessorSlot { @Override - public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) + public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable { DegradeRuleManager.checkDegrade(resourceWrapper, context, node, count); - fireEntry(context, resourceWrapper, node, count, args); + fireEntry(context, resourceWrapper, node, count, prioritized, args); } @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 ca34f2740c..4bd8ca5d3d 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 @@ -136,11 +136,11 @@ public class FlowSlot extends AbstractLinkedProcessorSlot { @Override - public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) - throws Throwable { + public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, + boolean prioritized, Object... args) throws Throwable { checkFlow(resourceWrapper, context, node, count); - fireEntry(context, resourceWrapper, node, count, args); + fireEntry(context, resourceWrapper, node, count, prioritized, args); } void checkFlow(ResourceWrapper resource, Context context, DefaultNode node, int count) throws BlockException { 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 9b851cfdfe..e5e91a1528 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 @@ -74,7 +74,7 @@ public class ClusterBuilderSlot extends AbstractLinkedProcessorSlot private ClusterNode clusterNode = null; @Override - public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) + public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable { if (clusterNode == null) { synchronized (lock) { @@ -100,7 +100,7 @@ public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode context.getCurEntry().setOriginNode(originNode); } - fireEntry(context, resourceWrapper, node, count, args); + fireEntry(context, resourceWrapper, node, count, prioritized, args); } @Override 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 e2bdff321f..b43cd32c64 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 @@ -29,10 +29,10 @@ public class LogSlot extends AbstractLinkedProcessorSlot { @Override - public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode obj, int count, Object... args) + public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode obj, int count, boolean prioritized, Object... args) throws Throwable { try { - fireEntry(context, resourceWrapper, obj, count, args); + fireEntry(context, resourceWrapper, obj, count, prioritized, args); } catch (BlockException e) { EagleEyeLogUtil.log(resourceWrapper.getName(), e.getClass().getSimpleName(), e.getRuleLimitApp(), context.getOrigin(), count); 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 cccb1cca84..7b475b89f6 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 @@ -131,7 +131,7 @@ public class NodeSelectorSlot extends AbstractLinkedProcessorSlot { private volatile Map map = new HashMap(10); @Override - public void entry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, Object... args) + public void entry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, boolean prioritized, Object... args) throws Throwable { /* * It's interesting that we use context name rather resource name as the map key. @@ -168,7 +168,7 @@ public void entry(Context context, ResourceWrapper resourceWrapper, Object obj, } context.setCurNode(node); - fireEntry(context, resourceWrapper, node, count, args); + fireEntry(context, resourceWrapper, node, count, prioritized, args); } @Override 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 8a6c377910..c4243729ae 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,11 +48,11 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot { @Override - public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) - throws Throwable { + public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, + boolean prioritized, Object... args) throws Throwable { try { // Do some checking. - fireEntry(context, resourceWrapper, node, count, args); + fireEntry(context, resourceWrapper, node, count, prioritized, args); // Request passed, add thread count and pass count. node.increaseThreadNum(); 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 e75192705c..1f67e4263e 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 @@ -30,10 +30,10 @@ public class SystemSlot extends AbstractLinkedProcessorSlot { @Override - public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) - throws Throwable { + public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, + boolean prioritized, Object... args) throws Throwable { SystemRuleManager.checkSystem(resourceWrapper); - fireEntry(context, resourceWrapper, node, count, args); + fireEntry(context, resourceWrapper, node, count, prioritized, args); } @Override diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/CtSphTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/CtSphTest.java index 568bc62e82..8c203f00cb 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/CtSphTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/CtSphTest.java @@ -308,7 +308,7 @@ private MustBlockSlot addMustBlockSlot(ResourceWrapper resourceWrapper) { private class ShouldNotPassSlot extends AbstractLinkedProcessorSlot { @Override public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode param, int count, - Object... args) { + boolean prioritized, Object... args) { throw new IllegalStateException("Should not enter this slot!"); } @@ -323,7 +323,7 @@ private class MustBlockSlot extends AbstractLinkedProcessorSlot { @Override public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode param, int count, - Object... args) throws Throwable { + boolean prioritized, Object... args) throws Throwable { throw new BlockException("custom") {}; } @@ -339,7 +339,7 @@ private class ShouldPassSlot extends AbstractLinkedProcessorSlot { @Override public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode param, int count, - Object... args) { + boolean prioritized, Object... args) { entered = true; } diff --git a/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlot.java b/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlot.java index b85d1cf3ac..ea9f329fd7 100644 --- a/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlot.java +++ b/sentinel-demo/sentinel-demo-slot-chain-spi/src/main/java/com/alibaba/csp/sentinel/demo/slot/DemoSlot.java @@ -28,12 +28,12 @@ public class DemoSlot extends AbstractLinkedProcessorSlot { @Override - public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) + public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable { System.out.println("Current context: " + context.getName()); System.out.println("Current entry resource: " + context.getCurEntry().getResourceWrapper().getName()); - fireEntry(context, resourceWrapper, node, count, args); + fireEntry(context, resourceWrapper, node, count, prioritized, args); } @Override 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 c95d328d71..b3f7bec9f0 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 @@ -47,16 +47,16 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot { private final Object LOCK = new Object(); @Override - public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) + public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable { if (!ParamFlowRuleManager.hasRules(resourceWrapper.getName())) { - fireEntry(context, resourceWrapper, node, count, args); + fireEntry(context, resourceWrapper, node, count, prioritized, args); return; } checkFlow(resourceWrapper, count, args); - fireEntry(context, resourceWrapper, node, count, args); + fireEntry(context, resourceWrapper, node, count, prioritized, args); } @Override diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlotTest.java b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlotTest.java index 7b03922cf2..32d6dc6fe5 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlotTest.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlotTest.java @@ -43,7 +43,7 @@ public class ParamFlowSlotTest { public void testEntryWhenParamFlowRuleNotExists() throws Throwable { String resourceName = "testEntryWhenParamFlowRuleNotExists"; ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN); - paramFlowSlot.entry(null, resourceWrapper, null, 1, "abc"); + paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc"); // The parameter metric instance will not be created. assertNull(ParamFlowSlot.getParamMetric(resourceWrapper)); } @@ -68,10 +68,10 @@ public void testEntryWhenParamFlowExists() throws Throwable { ParamFlowSlot.getMetricsMap().put(resourceWrapper, metric); // The first entry will pass. - paramFlowSlot.entry(null, resourceWrapper, null, 1, argToGo); + paramFlowSlot.entry(null, resourceWrapper, null, 1, false, argToGo); // The second entry will be blocked. try { - paramFlowSlot.entry(null, resourceWrapper, null, 1, argToGo); + paramFlowSlot.entry(null, resourceWrapper, null, 1, false, argToGo); } catch (ParamFlowException ex) { assertEquals(String.valueOf(argToGo), ex.getMessage()); assertEquals(resourceName, ex.getResourceName());