From d1d944c1a50ea1a2eb610fac7e874d7807b40c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Thu, 21 May 2020 11:50:23 +0800 Subject: [PATCH 1/5] tmp sumbit --- .../sentinel/annotation/SentinelResource.java | 3 +- .../AbstractSentinelAspectSupport.java | 9 +++- .../SentinelAnnotationIntegrationTest.java | 26 ++++++++++ .../integration/service/BarService.java | 48 +++++++++++++++++++ .../integration/service/GlobalFallback.java | 12 +++++ 5 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java create mode 100644 sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java 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 95f6584841..19ff18a252 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 @@ -23,9 +23,10 @@ * The annotation indicates a definition of Sentinel resource. * * @author Eric Zhao + * @author zhaoyuguang * @since 0.1.1 */ -@Target(ElementType.METHOD) +@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface SentinelResource { diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java index b67a219e94..f239e1e886 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java @@ -33,6 +33,7 @@ * Some common functions for Sentinel annotation aspect. * * @author Eric Zhao + * @author zhaoyuguang */ public abstract class AbstractSentinelAspectSupport { @@ -186,7 +187,13 @@ private Method extractFallbackMethod(ProceedingJoinPoint pjp, String fallbackNam private Method extractDefaultFallbackMethod(ProceedingJoinPoint pjp, String defaultFallback, Class[] locationClass) { if (StringUtil.isBlank(defaultFallback)) { - return null; + SentinelResource annotationClass = pjp.getTarget().getClass().getAnnotation(SentinelResource.class); + if (annotationClass != null && StringUtil.isBlank(annotationClass.defaultFallback())) { + defaultFallback = annotationClass.defaultFallback(); + locationClass = annotationClass.fallbackClass(); + } else { + return null; + } } boolean mustStatic = locationClass != null && locationClass.length >= 1; Class clazz = mustStatic ? locationClass[0] : pjp.getTarget().getClass(); diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java index bf127cc932..c53f1936f9 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java @@ -16,6 +16,7 @@ package com.alibaba.csp.sentinel.annotation.aspectj.integration; import com.alibaba.csp.sentinel.annotation.aspectj.integration.config.AopTestConfig; +import com.alibaba.csp.sentinel.annotation.aspectj.integration.service.BarService; import com.alibaba.csp.sentinel.annotation.aspectj.integration.service.FooService; import com.alibaba.csp.sentinel.annotation.aspectj.integration.service.FooUtil; import com.alibaba.csp.sentinel.node.ClusterNode; @@ -47,6 +48,8 @@ public class SentinelAnnotationIntegrationTest extends AbstractJUnit4SpringConte @Autowired private FooService fooService; + @Autowired + private BarService barService; @Test public void testProxySuccessful() { @@ -181,6 +184,29 @@ public void testNormalBlockHandlerAndFallback() throws Exception { assertThat(cn.blockQps()).isPositive(); } + @Test + public void testClassLevelDefaultFallbackWithSingleParam() { + assertThat(barService.anotherBar(1)).isEqualTo("Hello for 1"); + String resourceName = "apiAnotherBarWithDefaultFallback"; + ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName); + assertThat(cn).isNotNull(); + assertThat(cn.passQps()).isPositive(); + + assertThat(barService.doSomething(1)).isEqualTo("do something"); + String resourceName1 = "com.alibaba.csp.sentinel.annotation.aspectj.integration.service.BarService:doSomething(int)"; + ClusterNode cn1 = ClusterBuilderSlot.getClusterNode(resourceName1); + assertThat(cn1).isNotNull(); + assertThat(cn1.passQps()).isPositive(); + + assertThat(barService.anotherBar(5758)).isEqualTo("eee..."); + assertThat(cn.exceptionQps()).isPositive(); + assertThat(cn.blockQps()).isZero(); + + assertThat(barService.doSomething(5758)).isEqualTo("GlobalFallback:doFallback:1"); + assertThat(cn1.exceptionQps()).isPositive(); + assertThat(cn1.blockQps()).isZero(); + } + @Before public void setUp() throws Exception { FlowRuleManager.loadRules(new ArrayList()); diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java new file mode 100644 index 0000000000..ce9f60d4a5 --- /dev/null +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java @@ -0,0 +1,48 @@ +/* + * 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.annotation.aspectj.integration.service; + +import com.alibaba.csp.sentinel.annotation.SentinelResource; +import org.springframework.stereotype.Service; + +/** + * @author zhaoyuguang + */ +@Service +@SentinelResource(defaultFallback = "doFallback", fallbackClass = GlobalFallback.class) +public class BarService { + + @SentinelResource(value = "apiAnotherBarWithDefaultFallback", defaultFallback = "fallbackFunc") + public String anotherBar(int i) { + if (i == 5758) { + throw new IllegalArgumentException("oops"); + } + return "Hello for " + i; + } + + @SentinelResource() + public String doSomething(int i) { + if (i == 5758) { + throw new IllegalArgumentException("oops"); + } + return "do something"; + } + + public String fallbackFunc(Throwable t) { + System.out.println(t.getMessage()); + return "eee..."; + } +} diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java new file mode 100644 index 0000000000..32db4c67f4 --- /dev/null +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java @@ -0,0 +1,12 @@ +package com.alibaba.csp.sentinel.annotation.aspectj.integration.service; + +/** + * @author zhaoyuguang + */ + +public class GlobalFallback { + + public static String doFallback(int i) { + return "GlobalFallback:doFallback:" + i; + } +} From dc414a371f0894b58c4380fd9d288dffb471e0bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Thu, 21 May 2020 14:32:30 +0800 Subject: [PATCH 2/5] fix --- .../annotation/aspectj/AbstractSentinelAspectSupport.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java index f239e1e886..f096c91649 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java @@ -188,7 +188,7 @@ private Method extractDefaultFallbackMethod(ProceedingJoinPoint pjp, String defa Class[] locationClass) { if (StringUtil.isBlank(defaultFallback)) { SentinelResource annotationClass = pjp.getTarget().getClass().getAnnotation(SentinelResource.class); - if (annotationClass != null && StringUtil.isBlank(annotationClass.defaultFallback())) { + if (annotationClass != null && StringUtil.isNotBlank(annotationClass.defaultFallback())) { defaultFallback = annotationClass.defaultFallback(); locationClass = annotationClass.fallbackClass(); } else { From dc86fbf122e9093bddae4c24da369f485afcd8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Thu, 21 May 2020 14:40:41 +0800 Subject: [PATCH 3/5] fix ci --- .../integration/SentinelAnnotationIntegrationTest.java | 2 +- .../aspectj/integration/service/GlobalFallback.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java index c53f1936f9..fa2df95839 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java @@ -202,7 +202,7 @@ public void testClassLevelDefaultFallbackWithSingleParam() { assertThat(cn.exceptionQps()).isPositive(); assertThat(cn.blockQps()).isZero(); - assertThat(barService.doSomething(5758)).isEqualTo("GlobalFallback:doFallback:1"); + assertThat(barService.doSomething(5758)).isEqualTo("GlobalFallback:doFallback"); assertThat(cn1.exceptionQps()).isPositive(); assertThat(cn1.blockQps()).isZero(); } diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java index 32db4c67f4..e7f79a1ae8 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java @@ -6,7 +6,7 @@ public class GlobalFallback { - public static String doFallback(int i) { - return "GlobalFallback:doFallback:" + i; + public static String doFallback(Throwable t) { + return "GlobalFallback:doFallback"; } } From 0124edf76afd6d173d0b894f72d0221ec546322d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Sun, 24 May 2020 23:24:36 +0800 Subject: [PATCH 4/5] fix --- .../sentinel/annotation/SentinelResource.java | 2 +- .../aspectj/AbstractSentinelAspectSupport.java | 2 +- .../SentinelAnnotationIntegrationTest.java | 3 ++- .../aspectj/integration/service/BarService.java | 2 +- .../integration/service/GlobalFallback.java | 16 +++++++++++++++- 5 files changed, 20 insertions(+), 5 deletions(-) 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 19ff18a252..fe1ab396dd 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 @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2020 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. diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java index f096c91649..108d3b0341 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2020 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. diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java index fa2df95839..370df67ac7 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2020 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. @@ -42,6 +42,7 @@ * Integration test for Sentinel annotation AspectJ extension. * * @author Eric Zhao + * @author zhaoyuguang */ @ContextConfiguration(classes = {SentinelAnnotationIntegrationTest.class, AopTestConfig.class}) public class SentinelAnnotationIntegrationTest extends AbstractJUnit4SpringContextTests { diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java index ce9f60d4a5..69674ea12b 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/BarService.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2020 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. diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java index e7f79a1ae8..3571b57720 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/GlobalFallback.java @@ -1,9 +1,23 @@ +/* + * Copyright 1999-2020 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.annotation.aspectj.integration.service; /** * @author zhaoyuguang */ - public class GlobalFallback { public static String doFallback(Throwable t) { From cad7ed9d9ab9a87ff6ec577f2f810c900315e7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Tue, 26 May 2020 14:16:02 +0800 Subject: [PATCH 5/5] fix --- .../annotation/aspectj/AbstractSentinelAspectSupport.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java index 108d3b0341..b2040dca42 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java +++ b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java @@ -190,7 +190,9 @@ private Method extractDefaultFallbackMethod(ProceedingJoinPoint pjp, String defa SentinelResource annotationClass = pjp.getTarget().getClass().getAnnotation(SentinelResource.class); if (annotationClass != null && StringUtil.isNotBlank(annotationClass.defaultFallback())) { defaultFallback = annotationClass.defaultFallback(); - locationClass = annotationClass.fallbackClass(); + if (locationClass == null || locationClass.length < 1) { + locationClass = annotationClass.fallbackClass(); + } } else { return null; }