-
Notifications
You must be signed in to change notification settings - Fork 8.1k
feat: add global fallback support for AspectJ annotation extension (#3110) #3617
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| */ | ||
| 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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import java.lang.reflect.Method; | ||
|
|
||
| import static org.assertj.core.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| /** | ||
| * @author Eric Zhao | ||
|
|
@@ -36,4 +37,23 @@ public void testGetResourceName() throws Exception { | |
| assertThat(getResourceName(null, method)).isEqualTo(expectedResolvedName); | ||
| assertThat(getResourceName("", method)).isEqualTo(expectedResolvedName); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGlobalFallbackDefaultIsNull() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.