Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global fallback support for AspectJ annotation extension #3383

Open
wants to merge 3 commits into
base: 1.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.alibaba.csp.sentinel.annotation;

import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.fallback.DefaultGlobalFallback;
import com.alibaba.csp.sentinel.fallback.IGlobalFallback;

import java.lang.annotation.*;

Expand Down Expand Up @@ -77,6 +79,16 @@
*/
String defaultFallback() default "";


/**
* The {@code globalFallback} is used as the global fallback handler class.
* The returned class should implement the {@link IGlobalFallback} interface.
* If no specific global fallback handler is provided, the default {@link DefaultGlobalFallback} class will be used.
* @return The class that serves as the global fallback handler.
*/
Class<? extends IGlobalFallback> globalFallback() default DefaultGlobalFallback.class;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the scenario the related issue(#3110) aims to solve is to have a global fallback even when neither fallback nor defaultFallback is configured in the annotations. So, the global fallback should be configured in the construction method of SentinelResourceAspect, rather than through annotations (like what defaultFallback did).



/**
* The {@code fallback} is located in the same class with the original method by default.
* However, if some methods share the same signature and intend to set the same fallback,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.fallback;

import java.lang.reflect.Method;

/**
* Default global fallback
*
* @author luffy
*/
public class DefaultGlobalFallback implements IGlobalFallback {

Check warning on line 25 in sentinel-core/src/main/java/com/alibaba/csp/sentinel/fallback/DefaultGlobalFallback.java

View check run for this annotation

Codecov / codecov/patch

sentinel-core/src/main/java/com/alibaba/csp/sentinel/fallback/DefaultGlobalFallback.java#L25

Added line #L25 was not covered by tests

@Override
public Object handle(Method originalMethod, Object[] args, Throwable t) throws Throwable{
throw t;

Check warning on line 29 in sentinel-core/src/main/java/com/alibaba/csp/sentinel/fallback/DefaultGlobalFallback.java

View check run for this annotation

Codecov / codecov/patch

sentinel-core/src/main/java/com/alibaba/csp/sentinel/fallback/DefaultGlobalFallback.java#L29

Added line #L29 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.fallback;

import java.lang.reflect.Method;

/**
* Global fallback interface
*
* @author luffy
*/
public interface IGlobalFallback {

/**
*
* @param originalMethod the original method called
* @param args The parameters of the method that was originally called
* @param t The exception thrown by the method originally called
* @return The result of the global fallback handler execution
* @throws Throwable
*/
Object handle(Method originalMethod, Object[] args, Throwable t) throws Throwable;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.fallback.IGlobalFallback;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.MethodUtil;
Expand Down Expand Up @@ -85,6 +86,19 @@ protected Object handleFallback(ProceedingJoinPoint pjp, SentinelResource annota
return handleFallback(pjp, annotation.fallback(), annotation.defaultFallback(), annotation.fallbackClass(), ex);
}

protected Object handleGlobalFallback(ProceedingJoinPoint pjp,
Class<? extends IGlobalFallback> globalFallbackClazz,
Throwable ex) throws Throwable {
Object[] originArgs = pjp.getArgs();
Method originMethod = resolveMethod(pjp);
IGlobalFallback globalFallback = ResourceMetadataRegistry.lookupGlobalHandler(globalFallbackClazz);
if(globalFallback == null){
globalFallback = ResourceMetadataRegistry.updateGlobalFallBackFor(globalFallbackClazz);
}
return globalFallback.handle(originMethod,originArgs,ex);
}


protected Object handleFallback(ProceedingJoinPoint pjp, String fallback, String defaultFallback,
Class<?>[] fallbackClass, Throwable ex) throws Throwable {
Object[] originArgs = pjp.getArgs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.csp.sentinel.fallback.IGlobalFallback;
import com.alibaba.csp.sentinel.util.StringUtil;

/**
Expand All @@ -32,6 +33,9 @@
private static final Map<String, MethodWrapper> DEFAULT_FALLBACK_MAP = new ConcurrentHashMap<>();
private static final Map<String, MethodWrapper> BLOCK_HANDLER_MAP = new ConcurrentHashMap<>();

private static final Map<String, IGlobalFallback> GLOBAL_FALLBACK_MAP = new ConcurrentHashMap<>();


static MethodWrapper lookupFallback(Class<?> clazz, String name) {
return FALLBACK_MAP.get(getKey(clazz, name));
}
Expand All @@ -44,6 +48,10 @@
return BLOCK_HANDLER_MAP.get(getKey(clazz, name));
}

static IGlobalFallback lookupGlobalHandler(Class<?> clazz){
return GLOBAL_FALLBACK_MAP.get(clazz.getCanonicalName());
}

static void updateFallbackFor(Class<?> clazz, String name, Method method) {
if (clazz == null || StringUtil.isBlank(name)) {
throw new IllegalArgumentException("Bad argument");
Expand All @@ -65,6 +73,22 @@
BLOCK_HANDLER_MAP.put(getKey(clazz, name), MethodWrapper.wrap(method));
}

static IGlobalFallback updateGlobalFallBackFor(Class<? extends IGlobalFallback> clazz){
if(clazz==null){
throw new IllegalArgumentException("Bad argument");

Check warning on line 78 in sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/ResourceMetadataRegistry.java

View check run for this annotation

Codecov / codecov/patch

sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/ResourceMetadataRegistry.java#L78

Added line #L78 was not covered by tests
}
IGlobalFallback instance = null;
try{
instance = clazz.newInstance();
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Bad argument");
} catch (InstantiationException e) {
throw new IllegalArgumentException("Bad argument");

Check warning on line 86 in sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/ResourceMetadataRegistry.java

View check run for this annotation

Codecov / codecov/patch

sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/ResourceMetadataRegistry.java#L83-L86

Added lines #L83 - L86 were not covered by tests
}
GLOBAL_FALLBACK_MAP.put(clazz.getCanonicalName(),instance);
return instance;
}

private static String getKey(Class<?> clazz, String name) {
return String.format("%s:%s", clazz.getCanonicalName(), name);
}
Expand All @@ -82,4 +106,11 @@
static void clearBlockHandlerMap() {
BLOCK_HANDLER_MAP.clear();
}

/**
* Only for internal test.
*/
static void clearGlobalHandlerMap(){
GLOBAL_FALLBACK_MAP.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwab
return handleFallback(pjp, annotation, ex);
}

// No fallback function can handle the exception, so throw it out.
throw ex;
// Global fallback function handle the exception.
return handleGlobalFallback(pjp,annotation.globalFallback(),ex);
} finally {
if (entry != null) {
entry.exit(1, pjp.getArgs());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.csp.sentinel.annotation.aspectj;

import com.alibaba.csp.sentinel.annotation.aspectj.integration.fallback.AnnotationGlobalFallback;
import com.alibaba.csp.sentinel.annotation.aspectj.integration.service.FooService;
import org.junit.After;
import org.junit.Before;
Expand All @@ -33,12 +34,14 @@ public class ResourceMetadataRegistryTest {
public void setUp() throws Exception {
ResourceMetadataRegistry.clearBlockHandlerMap();
ResourceMetadataRegistry.clearFallbackMap();
ResourceMetadataRegistry.clearGlobalHandlerMap();
}

@After
public void tearDown() throws Exception {
ResourceMetadataRegistry.clearBlockHandlerMap();
ResourceMetadataRegistry.clearFallbackMap();
ResourceMetadataRegistry.clearGlobalHandlerMap();
}

@Test
Expand Down Expand Up @@ -73,6 +76,16 @@ public void testUpdateThenLookupBlockHandler() {
assertThat(wrapper.getMethod()).isSameAs(method);
}

@Test
public void testUpdateThenLookupGlobalFallback() {
Class clazz = AnnotationGlobalFallback.class;
assertThat(ResourceMetadataRegistry.lookupGlobalHandler(clazz)).isNull();

ResourceMetadataRegistry.updateGlobalFallBackFor(clazz);
assertThat(ResourceMetadataRegistry.lookupGlobalHandler(clazz)).isNotNull();

}

@Test(expected = IllegalArgumentException.class)
public void testUpdateBlockHandlerBadArgument() {
ResourceMetadataRegistry.updateBlockHandlerFor(null, "sxs", String.class.getMethods()[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ public void testFallBackPrivateMethod() throws Exception {
assertThat(fooService.fooWithPrivateFallback(2221)).isEqualTo("Oops, 2221");
}

@Test
public void testAnnotationGlobalFallback() throws Exception {
assertThat(fooService.fooWithAnnotationGlobalFallback(1)).isEqualTo("Hello for 1");
// Fallback should take effect.
assertThat(fooService.fooWithAnnotationGlobalFallback(5758)).isEqualTo("AnnotationGlobalFallback");

}

@Test(expected = IllegalAccessException.class)
public void testDefaultGlobalFallback() throws Exception {
fooService.fooWithDefaultGlobalFallback(5758);
}

@Before
public void setUp() throws Exception {
FlowRuleManager.loadRules(new ArrayList<FlowRule>());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alibaba.csp.sentinel.annotation.aspectj.integration.fallback;

import com.alibaba.csp.sentinel.fallback.IGlobalFallback;

import java.lang.reflect.Method;

/**
* @author luffy
* @version 1.0
* @date 2023/5/5 5:18 下午
*/
public class AnnotationGlobalFallback implements IGlobalFallback {

@Override
public Object handle(Method originalMethod, Object[] args, Throwable t) throws Throwable{
return "AnnotationGlobalFallback";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.alibaba.csp.sentinel.annotation.aspectj.integration.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.annotation.aspectj.integration.fallback.AnnotationGlobalFallback;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;

import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -53,6 +53,25 @@ public String fooWithFallback(int i) throws Exception {
return "Hello for " + i;
}

@SentinelResource(value = "apiFooWithFallback", globalFallback = AnnotationGlobalFallback.class,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If convenient, could you please add relevant example code in the sentinel-demo-annotation-spring-aop module?

exceptionsToTrace = {IllegalArgumentException.class})
public String fooWithAnnotationGlobalFallback(int i) throws Exception {
if (i == 5758) {
throw new IllegalAccessException();
}
return "Hello for " + i;
}

@SentinelResource(value = "apiFooWithDefaultFallback",exceptionsToTrace = {IllegalArgumentException.class})
public String fooWithDefaultGlobalFallback(int i) throws Exception {
if (i == 5758) {
throw new IllegalAccessException();
}
return "Hello for " + i;
}



@SentinelResource(value = "apiAnotherFooWithDefaultFallback", defaultFallback = "globalDefaultFallback",
fallbackClass = {FooUtil.class})
public String anotherFoo(int i) {
Expand Down
Loading