Browse Source

Update command handler for modifying rules

- Refine code about writable datasource

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 6 years ago
parent
commit
c70d6e92bc
1 changed files with 48 additions and 44 deletions
  1. +48
    -44
      sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java

+ 48
- 44
sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java View File

@@ -22,9 +22,9 @@ import com.alibaba.csp.sentinel.command.CommandHandler;
import com.alibaba.csp.sentinel.command.CommandRequest; import com.alibaba.csp.sentinel.command.CommandRequest;
import com.alibaba.csp.sentinel.command.CommandResponse; import com.alibaba.csp.sentinel.command.CommandResponse;
import com.alibaba.csp.sentinel.command.annotation.CommandMapping; import com.alibaba.csp.sentinel.command.annotation.CommandMapping;
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.csp.sentinel.datasource.DataSource;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule; import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager; import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
@@ -37,28 +37,29 @@ import com.alibaba.fastjson.JSONArray;


/** /**
* @author jialiang.linjl * @author jialiang.linjl
* @author Eric Zhao
*/ */
@CommandMapping(name = "setRules") @CommandMapping(name = "setRules")
public class ModifyRulesCommandHandler implements CommandHandler<String> { public class ModifyRulesCommandHandler implements CommandHandler<String> {


static DataSource<?, List<FlowRule>> flowDataSource = null;
static DataSource<?, List<AuthorityRule>> authorityDataSource = null;
static DataSource<?, List<DegradeRule>> degradeDataSource = null;
static DataSource<?, List<SystemRule>> systemSource = null;
private static WritableDataSource<List<FlowRule>> flowDataSource = null;
private static WritableDataSource<List<AuthorityRule>> authorityDataSource = null;
private static WritableDataSource<List<DegradeRule>> degradeDataSource = null;
private static WritableDataSource<List<SystemRule>> systemSource = null;


public static synchronized void registerFlowDataSource(DataSource<?, List<FlowRule>> datasource) {
public static synchronized void registerFlowDataSource(WritableDataSource<List<FlowRule>> datasource) {
flowDataSource = datasource; flowDataSource = datasource;
} }


public static synchronized void registerAuthorityDataSource(DataSource<?, List<AuthorityRule>> dataSource) {
public static synchronized void registerAuthorityDataSource(WritableDataSource<List<AuthorityRule>> dataSource) {
authorityDataSource = dataSource; authorityDataSource = dataSource;
} }


public static synchronized void registerDegradeDataSource(DataSource<?, List<DegradeRule>> dataSource) {
public static synchronized void registerDegradeDataSource(WritableDataSource<List<DegradeRule>> dataSource) {
degradeDataSource = dataSource; degradeDataSource = dataSource;
} }


public static synchronized void registerSystemDataSource(DataSource<?, List<SystemRule>> dataSource) {
public static synchronized void registerSystemDataSource(WritableDataSource<List<SystemRule>> dataSource) {
systemSource = dataSource; systemSource = dataSource;
} }


@@ -71,67 +72,70 @@ public class ModifyRulesCommandHandler implements CommandHandler<String> {
try { try {
data = URLDecoder.decode(data, "utf-8"); data = URLDecoder.decode(data, "utf-8");
} catch (Exception e) { } catch (Exception e) {
RecordLog.info("decode rule data error", e);
RecordLog.info("Decode rule data error", e);
return CommandResponse.ofFailure(e, "decode rule data error"); return CommandResponse.ofFailure(e, "decode rule data error");
} }
} }


RecordLog.info("receive rule change:" + type);
RecordLog.info(data);
RecordLog.info(String.format("Receiving rule change (type: %s): %s", type, data));


String result = "success"; String result = "success";


if ("flow".equalsIgnoreCase(type)) {
if (FLOW_RULE_TYPE.equalsIgnoreCase(type)) {
List<FlowRule> flowRules = JSONArray.parseArray(data, FlowRule.class); List<FlowRule> flowRules = JSONArray.parseArray(data, FlowRule.class);
FlowRuleManager.loadRules(flowRules); FlowRuleManager.loadRules(flowRules);
if (flowDataSource != null) {
try {
flowDataSource.writeDataSource(flowRules);
} catch (Exception e) {
result = "partial success";
RecordLog.info(e.getMessage(), e);
}
if (!writeToDataSource(flowDataSource, flowRules)) {
result = WRITE_DS_FAILURE_MSG;
} }
return CommandResponse.ofSuccess(result); return CommandResponse.ofSuccess(result);
} else if ("authority".equalsIgnoreCase(type)) {
} else if (AUTHORITY_RULE_TYPE.equalsIgnoreCase(type)) {
List<AuthorityRule> rules = JSONArray.parseArray(data, AuthorityRule.class); List<AuthorityRule> rules = JSONArray.parseArray(data, AuthorityRule.class);
AuthorityRuleManager.loadRules(rules); AuthorityRuleManager.loadRules(rules);
if (authorityDataSource != null) {
try {
authorityDataSource.writeDataSource(rules);
} catch (Exception e) {
result = "partial success";
RecordLog.info(e.getMessage(), e);
}
if (!writeToDataSource(authorityDataSource, rules)) {
result = WRITE_DS_FAILURE_MSG;
} }
return CommandResponse.ofSuccess(result); return CommandResponse.ofSuccess(result);
} else if ("degrade".equalsIgnoreCase(type)) {
} else if (DEGRADE_RULE_TYPE.equalsIgnoreCase(type)) {
List<DegradeRule> rules = JSONArray.parseArray(data, DegradeRule.class); List<DegradeRule> rules = JSONArray.parseArray(data, DegradeRule.class);
DegradeRuleManager.loadRules(rules); DegradeRuleManager.loadRules(rules);
if (degradeDataSource != null) {
try {
degradeDataSource.writeDataSource(rules);
} catch (Exception e) {
result = "partial success";
RecordLog.info(e.getMessage(), e);
}
if (!writeToDataSource(degradeDataSource, rules)) {
result = WRITE_DS_FAILURE_MSG;
} }
return CommandResponse.ofSuccess(result); return CommandResponse.ofSuccess(result);
} else if ("system".equalsIgnoreCase(type)) {
} else if (SYSTEM_RULE_TYPE.equalsIgnoreCase(type)) {
List<SystemRule> rules = JSONArray.parseArray(data, SystemRule.class); List<SystemRule> rules = JSONArray.parseArray(data, SystemRule.class);
SystemRuleManager.loadRules(rules); SystemRuleManager.loadRules(rules);
if (systemSource != null) {
try {
systemSource.writeDataSource(rules);
} catch (Exception e) {
result = "partial success";
RecordLog.info(e.getMessage(), e);
}
if (!writeToDataSource(systemSource, rules)) {
result = WRITE_DS_FAILURE_MSG;
} }
return CommandResponse.ofSuccess(result); return CommandResponse.ofSuccess(result);
} }
return CommandResponse.ofFailure(new IllegalArgumentException("invalid type")); return CommandResponse.ofFailure(new IllegalArgumentException("invalid type"));
}


/**
* Write target value to given data source.
*
* @param dataSource writable data source
* @param value target value to save
* @param <T> value type
* @return true if write successful or data source is empty; false if error occurs
*/
private <T> boolean writeToDataSource(WritableDataSource<T> dataSource, T value) {
if (dataSource != null) {
try {
dataSource.write(value);
} catch (Exception e) {
RecordLog.warn("Write data source failed", e);
return false;
}
}
return true;
} }


private static final String WRITE_DS_FAILURE_MSG = "partial success (write data source failed)";
private static final String FLOW_RULE_TYPE = "flow";
private static final String DEGRADE_RULE_TYPE = "degrade";
private static final String SYSTEM_RULE_TYPE = "system";
private static final String AUTHORITY_RULE_TYPE = "authority";
} }

Loading…
Cancel
Save