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 6e728bb9..01cf5263 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 @@ -57,12 +57,12 @@ public final class Constants { * 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()); + new ClusterNode(ROOT_ID, ResourceTypeConstants.COMMON)); /** * Global statistic node for inbound traffic. Usually used for {@link SystemRule} checking. */ - public final static ClusterNode ENTRY_NODE = new ClusterNode(); + public final static ClusterNode ENTRY_NODE = new ClusterNode(TOTAL_IN_RESOURCE_NAME, ResourceTypeConstants.COMMON); /** * Response time that exceeds TIME_DROP_VALVE will be calculated as TIME_DROP_VALVE. Default value is 4900 ms. 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 0bb9c411..5c5b137b 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 @@ -71,7 +71,7 @@ public class CtSph implements Sph { } if (context == null) { // Using default context. - context = MyContextUtil.myEnter(Constants.CONTEXT_DEFAULT_NAME, "", resourceWrapper.getType()); + context = InternalContextUtil.internalEnter(Constants.CONTEXT_DEFAULT_NAME); } // Global switch is turned off, so no rule checking will be done. @@ -125,7 +125,7 @@ public class CtSph implements Sph { if (context == null) { // Using default context. - context = MyContextUtil.myEnter(Constants.CONTEXT_DEFAULT_NAME, "", resourceWrapper.getType()); + context = InternalContextUtil.internalEnter(Constants.CONTEXT_DEFAULT_NAME); } // Global switch is close, no rule checking will do. @@ -245,8 +245,12 @@ public class CtSph implements Sph { /** * This class is used for skip context name checking. */ - private final static class MyContextUtil extends ContextUtil { - static Context myEnter(String name, String origin, EntryType type) { + private final static class InternalContextUtil extends ContextUtil { + static Context internalEnter(String name) { + return trueEnter(name, ""); + } + + static Context internalEnter(String name, String origin) { return trueEnter(name, origin); } } @@ -329,4 +333,24 @@ public class CtSph implements Sph { StringResourceWrapper resource = new StringResourceWrapper(name, type); return entryWithPriority(resource, count, prioritized, args); } + + @Override + public Entry entryWithType(String name, int resourceType, EntryType entryType, int count, Object[] args) + throws BlockException { + return entryWithType(name, resourceType, entryType, count, false, args); + } + + @Override + public Entry entryWithType(String name, int resourceType, EntryType entryType, int count, boolean prioritized, + Object[] args) throws BlockException { + StringResourceWrapper resource = new StringResourceWrapper(name, entryType, resourceType); + return entryWithPriority(resource, count, prioritized, args); + } + + @Override + public AsyncEntry asyncEntryWithType(String name, int resourceType, EntryType entryType, int count, + boolean prioritized, Object[] args) throws BlockException { + StringResourceWrapper resource = new StringResourceWrapper(name, entryType, resourceType); + return asyncEntryWithPriorityInternal(resource, count, prioritized, args); + } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/ResourceTypeConstants.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/ResourceTypeConstants.java new file mode 100644 index 00000000..1ae421cc --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/ResourceTypeConstants.java @@ -0,0 +1,31 @@ +/* + * 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; + +/** + * @author Eric Zhao + * @since 1.7.0 + */ +public final class ResourceTypeConstants { + + public static final int COMMON = 0; + public static final int COMMON_WEB = 1; + public static final int COMMON_RPC = 2; + public static final int COMMON_API_GATEWAY = 3; + public static final int COMMON_DB_SQL = 4; + + private ResourceTypeConstants() {} +} 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 bedbd33b..32643b46 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 @@ -29,7 +29,7 @@ import com.alibaba.csp.sentinel.slots.block.BlockException; * @author leyou * @author Eric Zhao */ -public interface Sph { +public interface Sph extends SphResourceTypeSupport { /** * Create a protected resource. diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphResourceTypeSupport.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphResourceTypeSupport.java new file mode 100644 index 00000000..4349473f --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/SphResourceTypeSupport.java @@ -0,0 +1,69 @@ +/* + * 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; + +import com.alibaba.csp.sentinel.slots.block.BlockException; + +/** + * @author Eric Zhao + * @since 1.7.0 + */ +public interface SphResourceTypeSupport { + + /** + * Create a protected resource with provided classification. + * + * @param name the unique name of the protected resource + * @param resourceType the classification of the resource + * @param entryType the traffic entry type (IN/OUT) of the resource + * @param count tokens required + * @param args extra parameters + * @return new entry of the resource + * @throws BlockException if the block criteria is met + */ + Entry entryWithType(String name, int resourceType, EntryType entryType, int count, Object[] args) + throws BlockException; + + /** + * Create a protected resource with provided classification. + * + * @param name the unique name of the protected resource + * @param resourceType the classification of the resource + * @param entryType the traffic entry type (IN/OUT) of the resource + * @param count tokens required + * @param prioritized whether the entry is prioritized + * @param args extra parameters + * @return new entry of the resource + * @throws BlockException if the block criteria is met + */ + Entry entryWithType(String name, int resourceType, EntryType entryType, int count, boolean prioritized, + Object[] args) throws BlockException; + + /** + * Create an asynchronous resource with provided classification. + * + * @param name the unique name of the protected resource + * @param resourceType the classification of the resource + * @param entryType the traffic entry type (IN/OUT) of the resource + * @param count tokens required + * @param prioritized whether the entry is prioritized + * @param args extra parameters + * @return new entry of the resource + * @throws BlockException if the block criteria is met + */ + AsyncEntry asyncEntryWithType(String name, int resourceType, EntryType entryType, int count, boolean prioritized, + Object[] args) 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 696b686f..a7ad2aa1 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 @@ -76,6 +76,8 @@ public class SphU { private static final Object[] OBJECTS0 = new Object[0]; + private SphU() {} + /** * Checking all {@link Rule}s about the resource. * @@ -246,7 +248,7 @@ public class SphU { /** * Checking all {@link Rule}s related the resource. The entry is prioritized. * - * @param name the unique name for the protected resource + * @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 */ @@ -257,14 +259,97 @@ public class SphU { /** * 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} + * @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); } + + /** + * Record statistics and check all rules of the resource. + * + * @param name the unique name for the protected resource + * @param resourceType classification of the resource (e.g. Web or RPC) + * @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.7.0 + */ + public static Entry entry(String name, int resourceType, EntryType type) throws BlockException { + return Env.sph.entryWithType(name, resourceType, type, 1, OBJECTS0); + } + + /** + * Record statistics and check all rules of the resource. + * + * @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} + * @param resourceType classification of the resource (e.g. Web or RPC) + * @param args extra parameters. + * @throws BlockException if the block criteria is met, eg. when any rule's threshold is exceeded + * @since 1.7.0 + */ + public static Entry entry(String name, int resourceType, EntryType type, Object[] args) + throws BlockException { + return Env.sph.entryWithType(name, resourceType, type, 1, args); + } + + /** + * Record statistics and check all rules of the resource. + * + * @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} + * @param resourceType classification of the resource (e.g. Web or RPC) + * @throws BlockException if the block criteria is met, eg. when any rule's threshold is exceeded + * @since 1.7.0 + */ + public static AsyncEntry asyncEntry(String name, int resourceType, EntryType type) + throws BlockException { + return Env.sph.asyncEntryWithType(name, resourceType, type, 1, false, OBJECTS0); + } + + /** + * Record statistics and check all rules of the resource. + * + * @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} + * @param resourceType classification of the resource (e.g. Web or RPC) + * @param args extra parameters + * @throws BlockException if the block criteria is met, eg. when any rule's threshold is exceeded + * @since 1.7.0 + */ + public static AsyncEntry asyncEntry(String name, int resourceType, EntryType type, Object[] args) + throws BlockException { + return Env.sph.asyncEntryWithType(name, resourceType, type, 1, false, args); + } + + /** + * Record statistics and check all rules of the resource. + * + * @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} + * @param resourceType classification of the resource (e.g. Web or RPC) + * @param acquireCount tokens required + * @param args extra parameters + * @throws BlockException if the block criteria is met, eg. when any rule's threshold is exceeded + * @since 1.7.0 + */ + public static AsyncEntry asyncEntry(String name, int resourceType, EntryType type, int acquireCount, + Object[] args) throws BlockException { + return Env.sph.asyncEntryWithType(name, resourceType, type, acquireCount, false, args); + } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java index a0fa11d6..95f65848 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java @@ -40,6 +40,12 @@ public @interface SentinelResource { */ EntryType entryType() default EntryType.OUT; + /** + * @return the classification (type) of the resource + * @since 1.7.0 + */ + int resourceType() default 0; + /** * @return name of the block exception function, empty by default */ diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/ClusterNode.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/ClusterNode.java index c342697d..6f5f4ef4 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/ClusterNode.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/ClusterNode.java @@ -19,8 +19,10 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.ReentrantLock; +import com.alibaba.csp.sentinel.ResourceTypeConstants; import com.alibaba.csp.sentinel.context.ContextUtil; import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.alibaba.csp.sentinel.util.AssertUtil; /** *
@@ -42,6 +44,19 @@ import com.alibaba.csp.sentinel.slots.block.BlockException; */ public class ClusterNode extends StatisticNode { + private final String name; + private final int resourceType; + + public ClusterNode(String name) { + this(name, ResourceTypeConstants.COMMON); + } + + public ClusterNode(String name, int resourceType) { + AssertUtil.notEmpty(name, "name cannot be empty"); + this.name = name; + this.resourceType = resourceType; + } + /** *
The origin map holds the pair: (origin, originNode) for one specific resource.
*@@ -50,10 +65,30 @@ public class ClusterNode extends StatisticNode { * at the very beginning while concurrent map will hold the lock all the time. *
*/ - private MapGet {@link Node} of the specific origin. Usually the origin is the Service Consumer's app name.
*If the origin node for given origin is absent, then a new {@link StatisticNode}
@@ -84,7 +119,7 @@ public class ClusterNode extends StatisticNode {
return statisticNode;
}
- public synchronized Map