Skip to content

Commit

Permalink
Carry the triggered rule in subclasses of BlockException (#469)
Browse files Browse the repository at this point in the history
* Extract getRule in BlockException and refine override methods in subclasses

Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 authored Jan 31, 2019
1 parent 1368154 commit 412e1ec
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
package com.alibaba.csp.sentinel.slots.block;

/***
* Abstract exception indicating blocked by Sentinel due to flow control, degraded or system guard.
/**
* Abstract exception indicating blocked by Sentinel due to flow control,
* circuit breaking or system protection triggered.
*
* @author youji.zj
*/
Expand All @@ -42,13 +43,20 @@ public abstract class BlockException extends Exception {
THROW_OUT_EXCEPTION.setStackTrace(sentinelStackTrace);
}

protected AbstractRule rule;
private String ruleLimitApp;

public BlockException(String ruleLimitApp) {
super();
this.ruleLimitApp = ruleLimitApp;
}

public BlockException(String ruleLimitApp, AbstractRule rule) {
super();
this.ruleLimitApp = ruleLimitApp;
this.rule = rule;
}

public BlockException(String message, Throwable cause) {
super(message, cause);
}
Expand All @@ -58,6 +66,12 @@ public BlockException(String ruleLimitApp, String message) {
this.ruleLimitApp = ruleLimitApp;
}

public BlockException(String ruleLimitApp, String message, AbstractRule rule) {
super(message);
this.ruleLimitApp = ruleLimitApp;
this.rule = rule;
}

@Override
public Throwable fillInStackTrace() {
return this;
Expand Down Expand Up @@ -98,4 +112,8 @@ public static boolean isBlockException(Throwable t) {

return false;
}

public AbstractRule getRule() {
return rule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@

import com.alibaba.csp.sentinel.slots.block.BlockException;

/***
/**
* Block exception for request origin access (authority) control.
*
* @author youji.zj
* @author Eric Zhao
*/
public class AuthorityException extends BlockException {

public AuthorityException(String ruleLimitApp) {
super(ruleLimitApp);
}

public AuthorityException(String ruleLimitApp, AuthorityRule rule) {
super(ruleLimitApp, rule);
}

public AuthorityException(String message, Throwable cause) {
super(message, cause);
}
Expand All @@ -40,4 +46,15 @@ public Throwable fillInStackTrace() {
return this;
}

/**
* Get triggered rule.
* Note: the rule result is a reference to rule map and SHOULD NOT be modified.
*
* @return triggered rule
* @since 1.4.2
*/
@Override
public AuthorityRule getRule() {
return rule.as(AuthorityRule.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void checkBlackWhiteAuthority(ResourceWrapper resource, Context context) throws

for (AuthorityRule rule : rules) {
if (!AuthorityRuleChecker.passCheck(rule, context)) {
throw new AuthorityException(context.getOrigin());
throw new AuthorityException(context.getOrigin(), rule);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public DegradeException(String ruleLimitApp) {
super(ruleLimitApp);
}

public DegradeException(String ruleLimitApp, DegradeRule rule) {
super(ruleLimitApp, rule);
}

public DegradeException(String message, Throwable cause) {
super(message, cause);
}
Expand All @@ -38,4 +42,16 @@ public DegradeException(String ruleLimitApp, String message) {
public Throwable fillInStackTrace() {
return this;
}

/**
* Get triggered rule.
* Note: the rule result is a reference to rule map and SHOULD NOT be modified.
*
* @return triggered rule
* @since 1.4.2
*/
@Override
public DegradeRule getRule() {
return rule.as(DegradeRule.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static void checkDegrade(ResourceWrapper resource, Context context, Defau

for (DegradeRule rule : rules) {
if (!rule.passCheck(context, node, count)) {
throw new DegradeException(rule.getLimitApp());
throw new DegradeException(rule.getLimitApp(), rule);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
public class FlowException extends BlockException {

public FlowException(String ruleLimitApp) {

super(ruleLimitApp);
}

public FlowException(String ruleLimitApp, FlowRule rule) {
super(ruleLimitApp, rule);
}

public FlowException(String message, Throwable cause) {
super(message, cause);
}
Expand All @@ -40,4 +43,15 @@ public Throwable fillInStackTrace() {
return this;
}

/**
* Get triggered rule.
* Note: the rule result is a reference to rule map and SHOULD NOT be modified.
*
* @return triggered rule
* @since 1.4.2
*/
@Override
public FlowRule getRule() {
return rule.as(FlowRule.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void checkFlow(ResourceWrapper resource, Context context, DefaultNode node, int
if (rules != null) {
for (FlowRule rule : rules) {
if (!canPassCheck(rule, context, node, count, prioritized)) {
throw new FlowException(rule.getLimitApp());
throw new FlowException(rule.getLimitApp(), rule);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,34 @@
*/
public class SystemBlockException extends BlockException {

String resourceName;

public String getResourceName() {
return resourceName;
}
private final String resourceName;

public SystemBlockException(String resourceName, String message, Throwable cause) {
super(message, cause);
this.resourceName = resourceName;
}

public SystemBlockException(String resourceName, String ruleLimitApp) {
super(ruleLimitApp);
public SystemBlockException(String resourceName, String limitType) {
super(limitType);
this.resourceName = resourceName;
}

public String getResourceName() {
return resourceName;
}

@Override
public Throwable fillInStackTrace() {
return this;
}

/**
* Return the limit type of system rule.
*
* @return the limit type
* @since 1.4.2
*/
public String getLimitType() {
return getRuleLimitApp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ public ParamFlowException(String resourceName, String message, Throwable cause)
this.resourceName = resourceName;
}

public ParamFlowException(String resourceName, String message) {
super(message, message);
public ParamFlowException(String resourceName, String param) {
super(param, param);
this.resourceName = resourceName;
}

public ParamFlowException(String resourceName, String param, ParamFlowRule rule) {
super(param, param);
this.resourceName = resourceName;
this.rule = rule;
}

public String getResourceName() {
return resourceName;
}
Expand All @@ -45,4 +51,26 @@ public String getResourceName() {
public Throwable fillInStackTrace() {
return this;
}

/**
* Get the parameter value that triggered the parameter flow control.
*
* @return the parameter value
* @since 1.4.2
*/
public String getLimitParam() {
return getMessage();
}

/**
* Get triggered rule.
* Note: the rule result is a reference to rule map and SHOULD NOT be modified.
*
* @return triggered rule
* @since 1.4.2
*/
@Override
public ParamFlowRule getRule() {
return rule.as(ParamFlowRule.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ void checkFlow(ResourceWrapper resourceWrapper, int count, Object... args)
// Here we add the block count.
addBlockCount(resourceWrapper, count, args);

String message = "";
String triggeredParam = "";
if (args.length > rule.getParamIdx()) {
Object value = args[rule.getParamIdx()];
message = String.valueOf(value);
triggeredParam = String.valueOf(value);
}
throw new ParamFlowException(resourceWrapper.getName(), message);
throw new ParamFlowException(resourceWrapper.getName(), triggeredParam, rule);
}
}
}
Expand Down

0 comments on commit 412e1ec

Please sign in to comment.