From c70d6e92bc4b87197c7020576fc3ee232d3304db Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Mon, 3 Sep 2018 19:20:52 +0800 Subject: [PATCH] Update command handler for modifying rules - Refine code about writable datasource Signed-off-by: Eric Zhao --- .../handler/ModifyRulesCommandHandler.java | 92 ++++++++++--------- 1 file changed, 48 insertions(+), 44 deletions(-) diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java index de78311b..28556ba4 100755 --- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java @@ -22,9 +22,9 @@ import com.alibaba.csp.sentinel.command.CommandHandler; import com.alibaba.csp.sentinel.command.CommandRequest; import com.alibaba.csp.sentinel.command.CommandResponse; 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.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.AuthorityRuleManager; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; @@ -37,28 +37,29 @@ import com.alibaba.fastjson.JSONArray; /** * @author jialiang.linjl + * @author Eric Zhao */ @CommandMapping(name = "setRules") public class ModifyRulesCommandHandler implements CommandHandler { - static DataSource> flowDataSource = null; - static DataSource> authorityDataSource = null; - static DataSource> degradeDataSource = null; - static DataSource> systemSource = null; + private static WritableDataSource> flowDataSource = null; + private static WritableDataSource> authorityDataSource = null; + private static WritableDataSource> degradeDataSource = null; + private static WritableDataSource> systemSource = null; - public static synchronized void registerFlowDataSource(DataSource> datasource) { + public static synchronized void registerFlowDataSource(WritableDataSource> datasource) { flowDataSource = datasource; } - public static synchronized void registerAuthorityDataSource(DataSource> dataSource) { + public static synchronized void registerAuthorityDataSource(WritableDataSource> dataSource) { authorityDataSource = dataSource; } - public static synchronized void registerDegradeDataSource(DataSource> dataSource) { + public static synchronized void registerDegradeDataSource(WritableDataSource> dataSource) { degradeDataSource = dataSource; } - public static synchronized void registerSystemDataSource(DataSource> dataSource) { + public static synchronized void registerSystemDataSource(WritableDataSource> dataSource) { systemSource = dataSource; } @@ -71,67 +72,70 @@ public class ModifyRulesCommandHandler implements CommandHandler { try { data = URLDecoder.decode(data, "utf-8"); } 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"); } } - RecordLog.info("receive rule change:" + type); - RecordLog.info(data); + RecordLog.info(String.format("Receiving rule change (type: %s): %s", type, data)); String result = "success"; - if ("flow".equalsIgnoreCase(type)) { + if (FLOW_RULE_TYPE.equalsIgnoreCase(type)) { List flowRules = JSONArray.parseArray(data, FlowRule.class); 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); - } else if ("authority".equalsIgnoreCase(type)) { + } else if (AUTHORITY_RULE_TYPE.equalsIgnoreCase(type)) { List rules = JSONArray.parseArray(data, AuthorityRule.class); 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); - } else if ("degrade".equalsIgnoreCase(type)) { + } else if (DEGRADE_RULE_TYPE.equalsIgnoreCase(type)) { List rules = JSONArray.parseArray(data, DegradeRule.class); 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); - } else if ("system".equalsIgnoreCase(type)) { + } else if (SYSTEM_RULE_TYPE.equalsIgnoreCase(type)) { List rules = JSONArray.parseArray(data, SystemRule.class); 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.ofFailure(new IllegalArgumentException("invalid type")); + } + /** + * Write target value to given data source. + * + * @param dataSource writable data source + * @param value target value to save + * @param value type + * @return true if write successful or data source is empty; false if error occurs + */ + private boolean writeToDataSource(WritableDataSource 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"; }