Light Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[BUG] Yi Ge Guan Yu SentinelResourceAspect De Yi Si BUG #3597

Open
Open
[BUG] Yi Ge Guan Yu SentinelResourceAspect De Yi Si BUG#3597

Description

Shi Yong Zhu Jie He Pao Chu Yi Chang Liang Chong Fang Shi Dui Tong Yang De Zi Yuan He Gui Ze Jin Xing Liu Liang Kong Zhi Shi ,Zhi Xing Jie Guo Bu Yi Yang ,Yi Xia Shi Shi Yong Pao Chu Yi Chang Fang Shi De Dai Ma :

package com.alibaba.csp.sentinel.demo.test;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

public class SentinelFlowThread {

public static void main(String[] args) throws InterruptedException {
initFlowRule();

for (int i = 0; i < 4; i++) {
// for (int i = 0; i < 1; i++) {
final int j = i;
new Thread(() -> {
Entry entry = null;
Object[] argArr = {"1"};
try {
if (j == 2) {
Thread.sleep(1000);
}
if (j == 3) {
Thread.sleep(4000);
}
// entry = SphU.entry("doSomething");
entry = SphU.entry("doSomething", 0, EntryType.OUT, argArr);
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName() + " Zhi Xing Wan Cheng ");
} catch (BlockException e) {
System.out.println(Thread.currentThread().getName() + " Bei Xian Liu ");
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (entry != null) {
// entry.exit();
entry.exit(1, argArr);
}
}
}, "Thread-" + i).start();
}

Thread.sleep(10000);
}

private static void initFlowRule() {
List rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("doSomething");
rule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
rule.setCount(2);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}

}

Zhi Xing Jie Guo Wei :

INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: C:\Users\Administrator\logs\csp
INFO: Sentinel log name use pid is: false
INFO: Sentinel log level is: INFO
Thread-2 Bei Xian Liu
Thread-0 Zhi Xing Wan Cheng
Thread-1 Zhi Xing Wan Cheng
Thread-3 Zhi Xing Wan Cheng

Yi Xia Shi Shi Yong Zhu Jie Fang Shi De Dai Ma :

package com.alibaba.csp.sentinel.demo.test;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

public class TestAnnotation {

@SentinelResource(value = "doSomething", blockHandler = "usingAnnotationMethodBlockHandler")
public void usingAnnotationMethod() throws InterruptedException {
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName() + " Zhi Xing Wan Cheng ");
}

public void usingAnnotationMethodBlockHandler(BlockException e) {
System.out.println(Thread.currentThread().getName() + " Bei Xian Liu ");
}

public static void main(String[] args) throws InterruptedException {
initFlowRule();

TestAnnotation testAnnotation = new TestAnnotation();

for (int i = 0; i < 4; i++) {
// for (int i = 0; i < 1; i++) {
final int j = i;
new Thread(() -> {
try {
if (j == 2) {
Thread.sleep(1000);
}
if (j == 3) {
Thread.sleep(4000);
}
testAnnotation.usingAnnotationMethod();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}, "Thread-" + i).start();
}

Thread.sleep(10000);
}

private static void initFlowRule() {
List rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("doSomething");
rule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
rule.setCount(2);
// rule.setCount(3);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}

}

Zhi Xing Jie Guo Wei :

INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: C:\Users\Administrator\logs\csp
INFO: Sentinel log name use pid is: false
INFO: Sentinel log level is: INFO
Thread-0 Bei Xian Liu
Thread-1 Bei Xian Liu
Thread-3 Bei Xian Liu
Thread-2 Zhi Xing Wan Cheng

Ran Hou ,Wo Ba SentinelResourceAspect Lei Jin Xing Xiu Gai ,Gai Wan Ru Xia :

/*
* 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;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import java.lang.reflect.Method;

/**
* Aspect for methods with {@link SentinelResource} annotation.
*
* @author Eric Zhao
*/
@Aspect
public class SentinelResourceAspect extends AbstractSentinelAspectSupport {

// @Pointcut("@annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)")
// @Pointcut("execution(* *(..)) && @annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)")
@Pointcut("call(* *(..)) && @annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)")
public void sentinelResourceAnnotationPointcut() {
}

@Around("sentinelResourceAnnotationPointcut()")
public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable {

// System.out.println(Thread.currentThread().getName());

Method originMethod = resolveMethod(pjp);

SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class);
if (annotation == null) {
// Should not go through here.
throw new IllegalStateException("Wrong state for SentinelResource annotation");
}
String resourceName = getResourceName(annotation.value(), originMethod);
EntryType entryType = annotation.entryType();
int resourceType = annotation.resourceType();
Entry entry = null;
try {
entry = SphU.entry(resourceName, resourceType, entryType, pjp.getArgs());
return pjp.proceed();
} catch (BlockException ex) {
return handleBlockException(pjp, annotation, ex);
} catch (Throwable ex) {
Class[] exceptionsToIgnore = annotation.exceptionsToIgnore();
// The ignore list will be checked first.
if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
throw ex;
}
if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {
traceException(ex);
return handleFallback(pjp, annotation, ex);
}

// No fallback function can handle the exception, so throw it out.
throw ex;
} finally {
if (entry != null) {
entry.exit(1, pjp.getArgs());
}
}
}
}

Ye Jiu Shi Ba pointcut Gai Wei @pointcut("execution(* (..)) && @annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)") Huo Zhe @pointcut("call( *(..)) && @annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)"),Jie Zhao Zai Zhi Xing Shi Yong Zhu Jie Fang Shi De TestAnnotation,Zhi Xing Jie Guo Wei :

INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: C:\Users\Administrator\logs\csp
INFO: Sentinel log name use pid is: false
INFO: Sentinel log level is: INFO
Thread-2 Bei Xian Liu
Thread-1 Zhi Xing Wan Cheng
Thread-0 Zhi Xing Wan Cheng
Thread-3 Zhi Xing Wan Cheng

Yu Shi Yong Pao Chu Yi Chang Fang Shi Ding Yi Zi Yuan SentinelFlowThread De Zhi Xing Jie Guo Yi Jing Yi Yang ,Zhe Shuo Ming AspectJ Zai Yi Fang Fa Wei Mu Biao Shi Hui Cun Zai call He execution Liang Chong join point,Jian Guan Fang Wen Dang pointcuts He joinPoints,Yin Ci ,SentinelResourceAspect De invokeResourceWithSentinel Fang Fa Jiang Hui Bei Tong Yi Ge Xian Cheng Zhi Xing Liang Ci ,Dao Zhi Sentinel Tong Ji Xian Cheng Shu Shi Bu Zhun Que ,Ken Qing Sentinel De Gong Xian Zhe Men Pai Cha Zhe Shi Bu Shi Yi Ge BUG?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions