Skip to content
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 @@ -37,6 +37,30 @@
*/
public abstract class AbstractSentinelAspectSupport {

/**
* Global fallback handler, invoked as last resort when no per-method fallback
* or default fallback is configured.
*/
private volatile SentinelAnnotationGlobalFallback globalFallback;

/**
* Set the global fallback handler.
*
* @param globalFallback the global fallback handler (may be {@code null} to clear)
*/
public void setGlobalFallback(SentinelAnnotationGlobalFallback globalFallback) {
this.globalFallback = globalFallback;
}

/**
* Get the current global fallback handler.
*
* @return the global fallback handler, or {@code null} if not set
*/
public SentinelAnnotationGlobalFallback getGlobalFallback() {
return globalFallback;
}

protected void traceException(Throwable ex) {
Tracer.trace(ex);
}
Expand Down Expand Up @@ -118,6 +142,12 @@ protected Object handleDefaultFallback(ProceedingJoinPoint pjp, String defaultFa
return invoke(pjp, fallbackMethod, args);
}

// If no default fallback is present, try the global fallback.
if (globalFallback != null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The volatile field is read twice (null check, then invocation). If another thread calls setGlobalFallback(null) between the check and globalFallback.handle(...), this throws a NullPointerException. Low risk since the fallback is typically configured once at startup, but reading it once into a local variable would make the check-and-invoke atomic given the aspect bean is shared across all advised calls.

Method originMethod = resolveMethod(pjp);
return globalFallback.handle(originMethod, pjp.getArgs(), ex);
}

// If no any fallback is present, then directly throw the exception.
throw ex;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 1999-2024 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;

import java.lang.reflect.Method;

/**
* Global fallback handler for {@link com.alibaba.csp.sentinel.annotation.SentinelResource} annotations.
* <p>
* When set on {@link SentinelResourceAspect}, this handler serves as the last-resort fallback
* for all {@code @SentinelResource}-annotated methods, even when neither {@code fallback}
* nor {@code defaultFallback} is specified in the annotation.
* <p>
* The global fallback is invoked only after both per-method fallback and default fallback
* have been exhausted (i.e., not configured or not found).
*
* @author EvanYao826
* @since 1.8.8

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@SInCE 1.8.8 does not match the target branch: master (this PR's base) is versioned 2.0.0-alpha2-SNAPSHOT, so this new public API would first ship in the 2.0 line, not 1.8.8. Please update the @SInCE tag (e.g. 2.0.0) so the javadoc does not misdocument when the interface was introduced.

*/
public interface SentinelAnnotationGlobalFallback {

/**
* Handle the fallback when a {@code @SentinelResource}-annotated method is blocked
* or throws a traced exception.
*
* @param originalMethod the original annotated method
* @param args the arguments passed to the original method
* @param t the exception that triggered the fallback (may be a {@link
* com.alibaba.csp.sentinel.slots.block.BlockException} or a traced business exception)
* @return the fallback result
* @throws Throwable if the fallback itself fails
*/
Object handle(Method originalMethod, Object[] args, Throwable t) throws Throwable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.Method;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* @author Eric Zhao
Expand All @@ -36,4 +37,23 @@ public void testGetResourceName() throws Exception {
assertThat(getResourceName(null, method)).isEqualTo(expectedResolvedName);
assertThat(getResourceName("", method)).isEqualTo(expectedResolvedName);
}

@Test
public void testGlobalFallbackDefaultIsNull() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These three tests only cover getter/setter/clear of the new field; the actual feature - globalFallback.handle() being reached from handleDefaultFallback - has zero test coverage. Please add behavioral tests through the aspect flow before merge: (1) a blocked call (BlockException) on a @SentinelResource method with no fallback/defaultFallback returns the global fallback result and receives the BlockException; (2) a traced business exception also reaches the global fallback; (3) when a per-method fallback/blockHandler or defaultFallback is configured, the global fallback is NOT invoked (resolution order per-method -> default -> global); (4) exceptions matched by exceptionsToIgnore are still rethrown. The existing Spring AOP integration test infrastructure in this module (src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration, SentinelAnnotationIntegrationTest) can host these.

assertThat(getGlobalFallback()).isNull();
}

@Test
public void testSetAndGetGlobalFallback() throws Throwable {
SentinelAnnotationGlobalFallback fallback = mock(SentinelAnnotationGlobalFallback.class);
setGlobalFallback(fallback);
assertThat(getGlobalFallback()).isSameAs(fallback);
}

@Test
public void testClearGlobalFallback() {
setGlobalFallback(mock(SentinelAnnotationGlobalFallback.class));
setGlobalFallback(null);
assertThat(getGlobalFallback()).isNull();
}
}
Loading