Skip to content

Commit

Permalink
Code and javadoc refinement
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 committed Sep 27, 2018
1 parent 5f5443a commit 87076a6
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public MetricWriter(long singleFileSize, int totalFileCount) {
if (singleFileSize <= 0 || totalFileCount <= 0) {
throw new IllegalArgumentException();
}
RecordLog.info("new MetricWriter, singleFileSize=" + singleFileSize + ", totalFileCount=" + totalFileCount);
RecordLog.info("[MetricWriter] Creating new MetricWriter, singleFileSize=" + singleFileSize + ", totalFileCount=" + totalFileCount);
this.baseDir = METRIC_BASE_DIR;
File dir = new File(baseDir);
if (!dir.exists()) {
Expand All @@ -100,7 +100,7 @@ public MetricWriter(long singleFileSize, int totalFileCount) {
try {
this.timeSecondBase = df.parse("1970-01-01 00:00:00").getTime() / 1000;
} catch (Exception e) {
RecordLog.info("new MetricWriter error: ", e);
RecordLog.warn("[MetricWriter] Create new MetricWriter error", e);
}
}

Expand Down Expand Up @@ -286,9 +286,9 @@ private void removeMoreFiles() throws Exception {
String fileName = list.get(i);
String indexFile = formIndexFileName(fileName);
new File(fileName).delete();
RecordLog.info("remove metric file: " + fileName);
RecordLog.info("[MetricWriter] Removing metric file: " + fileName);
new File(indexFile).delete();
RecordLog.info("remove metric index file: " + indexFile);
RecordLog.info("[MetricWriter] Removing metric index file: " + indexFile);
}
}

Expand All @@ -307,8 +307,8 @@ private void closeAndNewFile(String fileName) throws Exception {
;
curMetricIndexFile = new File(idxFile);
outIndex = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(idxFile, append)));
RecordLog.info("create new metric file: " + fileName);
RecordLog.info("create new metric index file: " + idxFile);
RecordLog.info("[MetricWriter] New metric file created: " + fileName);
RecordLog.info("[MetricWriter] New metric index file created: " + idxFile);
}

private boolean validSize() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void updateValue(T newValue) {
if (isEqual(value, newValue)) {
return;
}
RecordLog.info("SentinelProperty, config is real updated to: " + newValue);
RecordLog.info("[DynamicSentinelProperty] Config will be updated to: " + newValue);

value = newValue;
for (PropertyListener<T> listener : listeners) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@

/***
* @author youji.zj
* @author Eric Zhao
*/
public abstract class AbstractRule implements Rule {

/*** 规则的资源描述 ***/
/**
* Resource name.
*/
private String resource;

/*** 被限制的应用,授权时候为逗号分隔的应用集合,限流时为单个应用 ***/
/**
* <p>
* Application name that will be limited by origin.
* Multiple application name can be separated with comma (',').
* </p>
* <p>The default limitApp is `default`, which means allowing all origin apps.</p>
* <p>For example: limitApp = `appA,appB` will limit requests from appA and appB.</p>
*/
private String limitApp;

public String getResource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author youji.zj
* @author jialiang.linjl
*/
public class RuleConstant {
public final class RuleConstant {

public static final int FLOW_GRADE_THREAD = 0;
public static final int FLOW_GRADE_QPS = 1;
Expand All @@ -38,4 +38,8 @@ public class RuleConstant {
public static final int CONTROL_BEHAVIOR_WARM_UP = 1;
public static final int CONTROL_BEHAVIOR_RATE_LIMITER = 2;

public static final String LIMIT_APP_DEFAULT = "default";
public static final String LIMIT_APP_OTHER = "other";

private RuleConstant() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.csp.sentinel.property.DynamicSentinelProperty;
import com.alibaba.csp.sentinel.property.PropertyListener;
import com.alibaba.csp.sentinel.property.SentinelProperty;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;

/**
* Manager for authority rules.
Expand Down Expand Up @@ -115,7 +115,7 @@ private Map<String, List<AuthorityRule>> loadAuthorityConf(List<AuthorityRule> l
}

if (StringUtil.isBlank(rule.getLimitApp())) {
rule.setLimitApp(FlowRule.LIMIT_APP_DEFAULT);
rule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
}

String identity = rule.getResource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.alibaba.csp.sentinel.property.SentinelProperty;
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.util.StringUtil;

/***
Expand Down Expand Up @@ -150,7 +150,7 @@ private Map<String, List<DegradeRule>> loadDegradeConf(List<DegradeRule> list) {
}

if (StringUtil.isBlank(rule.getLimitApp())) {
rule.setLimitApp(FlowRule.LIMIT_APP_DEFAULT);
rule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
}

String identity = rule.getResource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@
*/
public class FlowRule extends AbstractRule {

public static final String LIMIT_APP_DEFAULT = "default";
public static final String LIMIT_APP_OTHER = "other";

public FlowRule(){
super();
setLimitApp(LIMIT_APP_DEFAULT);
setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
}

/**
Expand Down Expand Up @@ -196,7 +193,7 @@ private Node selectNodeByRequesterAndStrategy(String origin, Context context, De
return node;
}

} else if (LIMIT_APP_DEFAULT.equals(limitApp)) {
} else if (RuleConstant.LIMIT_APP_DEFAULT.equals(limitApp)) {
if (strategy == RuleConstant.STRATEGY_DIRECT) {
return node.getClusterNode();
}
Expand All @@ -216,7 +213,7 @@ private Node selectNodeByRequesterAndStrategy(String origin, Context context, De
return node;
}

} else if (LIMIT_APP_OTHER.equals(limitApp) && FlowRuleManager.isOtherOrigin(origin, getResource())) {
} else if (RuleConstant.LIMIT_APP_OTHER.equals(limitApp) && FlowRuleManager.isOtherOrigin(origin, getResource())) {
if (strategy == RuleConstant.STRATEGY_DIRECT) {
return context.getOriginNode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.Comparator;

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

public class FlowRuleComparator implements Comparator<FlowRule> {

@Override
Expand All @@ -30,9 +32,9 @@ public int compare(FlowRule o1, FlowRule o2) {
return 0;
}

if (FlowRule.LIMIT_APP_DEFAULT.equals(o1.getLimitApp())) {
if (RuleConstant.LIMIT_APP_DEFAULT.equals(o1.getLimitApp())) {
return 1;
} else if (FlowRule.LIMIT_APP_DEFAULT.equals(o2.getLimitApp())) {
} else if (RuleConstant.LIMIT_APP_DEFAULT.equals(o2.getLimitApp())) {
return -1;
} else {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static Map<String, List<FlowRule>> loadFlowConf(List<FlowRule> list) {
continue;
}
if (StringUtil.isBlank(rule.getLimitApp())) {
rule.setLimitApp(FlowRule.LIMIT_APP_DEFAULT);
rule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
}

Controller rater = new DefaultController(rule.getCount(), rule.getGrade());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class SystemRuleManager {
private static volatile boolean maxRtIsSet = false;
private static volatile boolean maxThreadIsSet = false;

static AtomicBoolean checkSystemStatus = new AtomicBoolean(false);
private static AtomicBoolean checkSystemStatus = new AtomicBoolean(false);

private static SystemStatusListener statusListener = null;
private final static SystemPropertyListener listener = new SystemPropertyListener();
Expand Down Expand Up @@ -185,11 +185,9 @@ public void configUpdate(List<SystemRule> rules) {
checkSystemStatus.set(false);
}

RecordLog.info("current system system status : " + checkSystemStatus.get());
RecordLog.info("current highestSystemLoad status : " + highestSystemLoad);
RecordLog.info("current maxRt : " + maxRt);
RecordLog.info("current maxThread : " + maxThread);
RecordLog.info("current qps : " + qps);

RecordLog.info(String.format("[SystemRuleManager] Current system check status: %s, highestSystemLoad: "
+ highestSystemLoad + ", " + "maxRt: %d, maxThread: %d, maxQps: " + qps, checkSystemStatus.get(), maxRt, maxThread));
}

protected void restoreSetting() {
Expand Down Expand Up @@ -222,9 +220,8 @@ public static void setHighestSystemLoad(double highestSystemLoad) {
}

public static void loadSystemConf(SystemRule rule) {

boolean checkStatus = false;
// 首先判断是否有效
// Check if it's valid.

if (rule.getHighestSystemLoad() >= 0) {
highestSystemLoad = Math.min(highestSystemLoad, rule.getHighestSystemLoad());
Expand Down Expand Up @@ -260,9 +257,8 @@ public static void loadSystemConf(SystemRule rule) {
* @throws BlockException when any system rule's threshold is exceeded.
*/
public static void checkSystem(ResourceWrapper resourceWrapper) throws BlockException {

// 确定开关开了
if (checkSystemStatus.get() == false) {
// Ensure the checking switch is on.
if (!checkSystemStatus.get()) {
return;
}

Expand All @@ -288,7 +284,7 @@ public static void checkSystem(ResourceWrapper resourceWrapper) throws BlockExce
throw new SystemBlockException(resourceWrapper.getName(), "rt");
}

// 完全按照RT,BBR算法来
// BBR algorithm.
if (highestSystemLoadIsSet && getCurrentSystemAvgLoad() > highestSystemLoad) {
if (currentThread > 1 &&
currentThread > Constants.ENTRY_NODE.maxSuccessQps() * Constants.ENTRY_NODE.minRt() / 1000) {
Expand All @@ -301,5 +297,4 @@ public static void checkSystem(ResourceWrapper resourceWrapper) throws BlockExce
public static double getCurrentSystemAvgLoad() {
return statusListener.getSystemAverageLoad();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private AppNameUtil() {

static {
resolveAppName();
RecordLog.info("app name resolved: " + appName);
RecordLog.info("App name resolved: " + appName);
}

public static void resolveAppName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.alibaba.csp.sentinel.property.DynamicSentinelProperty;
import com.alibaba.csp.sentinel.property.PropertyListener;
import com.alibaba.csp.sentinel.property.SentinelProperty;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.util.StringUtil;

/**
Expand Down Expand Up @@ -169,7 +169,7 @@ private Map<String, List<ParamFlowRule>> aggregateHotParamRules(List<ParamFlowRu
}

if (StringUtil.isBlank(rule.getLimitApp())) {
rule.setLimitApp(FlowRule.LIMIT_APP_DEFAULT);
rule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
}

if (rule.getParamFlowItemList() == null) {
Expand Down

0 comments on commit 87076a6

Please sign in to comment.