Skip to content

Commit

Permalink
Add exceptionsToIgnore configuration support in @SentinelResource ann…
Browse files Browse the repository at this point in the history
…otation (alibaba#683)
  • Loading branch information
yikangfeng01 authored and sczyh30 committed Apr 21, 2019
1 parent cb93351 commit 08611fa
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,14 @@
* @since 1.5.1
*/
Class<? extends Throwable>[] exceptionsToTrace() default {Throwable.class};

/**
* Indicates the exceptions to be ignored. Note that {@code exceptionsToTrace} should
* not appear with {@code exceptionsToIgnore} at the same time, or {@code exceptionsToIgnore}
* will be of higher precedence.
*
* @return the list of exception classes to ignore, empty by default
* @since 1.6.0
*/
Class<? extends Throwable>[] exceptionsToIgnore() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected Object handleBlockException(ProceedingJoinPoint pjp, SentinelResource
}

protected Object handleBlockException(ProceedingJoinPoint pjp, String fallback, String blockHandler,
Class<?>[] blockHandlerClass, BlockException ex) throws Exception {
Class<?>[] blockHandlerClass, BlockException ex) throws Exception {
// Execute fallback for degrading if configured.
Object[] originArgs = pjp.getArgs();
if (isDegradeFailure(ex)) {
Expand All @@ -78,6 +78,11 @@ protected Object handleBlockException(ProceedingJoinPoint pjp, String fallback,
}

protected void traceException(Throwable ex, SentinelResource annotation) {
Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
// The ignore list will be checked first.
if (exceptionsToIgnore.length > 0 && isIgnoredException(ex, exceptionsToIgnore)) {
return;
}
if (isTracedException(ex, annotation.exceptionsToTrace())) {
Tracer.trace(ex);
}
Expand All @@ -86,8 +91,10 @@ protected void traceException(Throwable ex, SentinelResource annotation) {
/**
* Check whether the exception is in tracked list of exception classes.
*
* @param ex provided throwable
* @param exceptionsToTrace list of exceptions to trace
* @param ex
* provided throwable
* @param exceptionsToTrace
* list of exceptions to trace
* @return true if it should be traced, otherwise false
*/
private boolean isTracedException(Throwable ex, Class<? extends Throwable>[] exceptionsToTrace) {
Expand All @@ -102,6 +109,18 @@ private boolean isTracedException(Throwable ex, Class<? extends Throwable>[] exc
return false;
}

private boolean isIgnoredException(Throwable ex, Class<? extends Throwable>[] exceptionsToIgnore) {
if (exceptionsToIgnore == null) {
return false;
}
for (Class<? extends Throwable> exceptionToIgnore : exceptionsToIgnore) {
if (exceptionToIgnore.isAssignableFrom(ex.getClass())) {
return true;
}
}
return false;
}

private boolean isDegradeFailure(/*@NonNull*/ BlockException ex) {
return ex instanceof DegradeException;
}
Expand Down Expand Up @@ -176,8 +195,8 @@ private Method findMethod(boolean mustStatic, Class<?> clazz, String name, Class
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
if (name.equals(method.getName()) && checkStatic(mustStatic, method)
&& returnType.isAssignableFrom(method.getReturnType())
&& Arrays.equals(parameterTypes, method.getParameterTypes())) {
&& returnType.isAssignableFrom(method.getReturnType())
&& Arrays.equals(parameterTypes, method.getParameterTypes())) {

RecordLog.info("Resolved method [{0}] in class [{1}]", name, clazz.getCanonicalName());
return method;
Expand All @@ -190,7 +209,7 @@ private Method findMethod(boolean mustStatic, Class<?> clazz, String name, Class
} else {
String methodType = mustStatic ? " static" : "";
RecordLog.warn("Cannot find{0} method [{1}] in class [{2}] with parameters {3}",
methodType, name, clazz.getCanonicalName(), Arrays.toString(parameterTypes));
methodType, name, clazz.getCanonicalName(), Arrays.toString(parameterTypes));
return null;
}
}
Expand All @@ -204,7 +223,7 @@ protected Method resolveMethod(ProceedingJoinPoint joinPoint) {
Class<?> targetClass = joinPoint.getTarget().getClass();

Method method = getDeclaredMethodFor(targetClass, signature.getName(),
signature.getMethod().getParameterTypes());
signature.getMethod().getParameterTypes());
if (method == null) {
throw new IllegalStateException("Cannot resolve target method: " + signature.getMethod().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ public void testBlockHandlerNotFound() {
fooService.baz("Sentinel");
}

@Test
public void testAnnotationExceptionsToIgnore() {
assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
String resourceName = "apiBaz";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertThat(cn).isNotNull();
assertThat(cn.passQps()).isPositive();

try {
fooService.baz("fail");
fail("should not reach here");
} catch (IllegalMonitorStateException ex) {
assertThat(cn.exceptionQps()).isZero();
}
}

@Test
public void testNormalBlockHandlerAndFallback() throws Exception {
assertThat(fooService.foo(1)).isEqualTo("Hello for 1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ public int random() {
return ThreadLocalRandom.current().nextInt(0, 30000);
}

@SentinelResource(value = "apiBaz", blockHandler = "bazBlockHandler")
@SentinelResource(value = "apiBaz", blockHandler = "bazBlockHandler",
exceptionsToIgnore = {IllegalMonitorStateException.class})
public String baz(String name) {
if (name.equals("fail")) {
throw new IllegalMonitorStateException("boom!");
}
return "cheers, " + name;
}

Expand Down

0 comments on commit 08611fa

Please sign in to comment.