diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/ClusterRuleConstant.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/ClusterRuleConstant.java
new file mode 100644
index 00000000..78cacf34
--- /dev/null
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/ClusterRuleConstant.java
@@ -0,0 +1,31 @@
+/*
+ * 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.slots.block;
+
+/**
+ * @author Eric Zhao
+ * @since 1.4.0
+ */
+public final class ClusterRuleConstant {
+
+ public static final int FLOW_CLUSTER_STRATEGY_NORMAL = 0;
+ public static final int FLOW_CLUSTER_STRATEGY_REF = 1;
+
+ public static final int FLOW_THRESHOLD_AVG_LOCAL = 0;
+ public static final int FLOW_THRESHOLD_GLOBAL = 1;
+
+ private ClusterRuleConstant() {}
+}
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/ClusterFlowConfig.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/ClusterFlowConfig.java
new file mode 100644
index 00000000..506701e2
--- /dev/null
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/ClusterFlowConfig.java
@@ -0,0 +1,154 @@
+/*
+ * 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.slots.block.flow;
+
+import com.alibaba.csp.sentinel.slots.block.ClusterRuleConstant;
+
+/**
+ * Flow rule config in cluster mode.
+ *
+ * @author Eric Zhao
+ * @since 1.4.0
+ */
+public class ClusterFlowConfig {
+
+ /**
+ * Global unique ID.
+ */
+ private Integer flowId;
+
+ /**
+ * Threshold type (average by local value or global value).
+ */
+ private int thresholdType = ClusterRuleConstant.FLOW_THRESHOLD_AVG_LOCAL;
+ private boolean fallbackToLocalWhenFail;
+
+ /**
+ * 0: normal; 1: using reference (borrow from reference).
+ */
+ private int strategy = ClusterRuleConstant.FLOW_CLUSTER_STRATEGY_NORMAL;
+
+ private Integer refFlowId;
+ private int refSampleCount = 10;
+ private double refRatio = 1d;
+
+ public Integer getFlowId() {
+ return flowId;
+ }
+
+ public ClusterFlowConfig setFlowId(Integer flowId) {
+ this.flowId = flowId;
+ return this;
+ }
+
+ public int getThresholdType() {
+ return thresholdType;
+ }
+
+ public ClusterFlowConfig setThresholdType(int thresholdType) {
+ this.thresholdType = thresholdType;
+ return this;
+ }
+
+ public int getStrategy() {
+ return strategy;
+ }
+
+ public ClusterFlowConfig setStrategy(int strategy) {
+ this.strategy = strategy;
+ return this;
+ }
+
+ public Integer getRefFlowId() {
+ return refFlowId;
+ }
+
+ public ClusterFlowConfig setRefFlowId(Integer refFlowId) {
+ this.refFlowId = refFlowId;
+ return this;
+ }
+
+ public int getRefSampleCount() {
+ return refSampleCount;
+ }
+
+ public ClusterFlowConfig setRefSampleCount(int refSampleCount) {
+ this.refSampleCount = refSampleCount;
+ return this;
+ }
+
+ public double getRefRatio() {
+ return refRatio;
+ }
+
+ public ClusterFlowConfig setRefRatio(double refRatio) {
+ this.refRatio = refRatio;
+ return this;
+ }
+
+ public boolean isFallbackToLocalWhenFail() {
+ return fallbackToLocalWhenFail;
+ }
+
+ public ClusterFlowConfig setFallbackToLocalWhenFail(boolean fallbackToLocalWhenFail) {
+ this.fallbackToLocalWhenFail = fallbackToLocalWhenFail;
+ return this;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) { return true; }
+ if (o == null || getClass() != o.getClass()) { return false; }
+
+ ClusterFlowConfig that = (ClusterFlowConfig)o;
+
+ if (thresholdType != that.thresholdType) { return false; }
+ if (fallbackToLocalWhenFail != that.fallbackToLocalWhenFail) { return false; }
+ if (strategy != that.strategy) { return false; }
+ if (refSampleCount != that.refSampleCount) { return false; }
+ if (Double.compare(that.refRatio, refRatio) != 0) { return false; }
+ if (flowId != null ? !flowId.equals(that.flowId) : that.flowId != null) { return false; }
+ return refFlowId != null ? refFlowId.equals(that.refFlowId) : that.refFlowId == null;
+ }
+
+ @Override
+ public int hashCode() {
+ int result;
+ long temp;
+ result = flowId != null ? flowId.hashCode() : 0;
+ result = 31 * result + thresholdType;
+ result = 31 * result + (fallbackToLocalWhenFail ? 1 : 0);
+ result = 31 * result + strategy;
+ result = 31 * result + (refFlowId != null ? refFlowId.hashCode() : 0);
+ result = 31 * result + refSampleCount;
+ temp = Double.doubleToLongBits(refRatio);
+ result = 31 * result + (int)(temp ^ (temp >>> 32));
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return "ClusterFlowConfig{" +
+ "flowId=" + flowId +
+ ", thresholdType=" + thresholdType +
+ ", fallbackToLocalWhenFail=" + fallbackToLocalWhenFail +
+ ", strategy=" + strategy +
+ ", refFlowId=" + refFlowId +
+ ", refSampleCount=" + refSampleCount +
+ ", refRatio=" + refRatio +
+ '}';
+ }
+}
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRule.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRule.java
index dc42a89e..e7413ea2 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRule.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRule.java
@@ -85,6 +85,12 @@ public class FlowRule extends AbstractRule {
*/
private int maxQueueingTimeMs = 500;
+ private boolean clusterMode;
+ /**
+ * Flow rule config for cluster mode.
+ */
+ private ClusterFlowConfig clusterConfig;
+
/**
* The traffic shaping (throttling) controller.
*/
@@ -162,6 +168,24 @@ public class FlowRule extends AbstractRule {
return this;
}
+ public boolean isClusterMode() {
+ return clusterMode;
+ }
+
+ public FlowRule setClusterMode(boolean clusterMode) {
+ this.clusterMode = clusterMode;
+ return this;
+ }
+
+ public ClusterFlowConfig getClusterConfig() {
+ return clusterConfig;
+ }
+
+ public FlowRule setClusterConfig(ClusterFlowConfig clusterConfig) {
+ this.clusterConfig = clusterConfig;
+ return this;
+ }
+
@Override
public boolean passCheck(Context context, DefaultNode node, int acquireCount, Object... args) {
return true;
@@ -169,43 +193,21 @@ public class FlowRule extends AbstractRule {
@Override
public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (!(o instanceof FlowRule)) {
- return false;
- }
- if (!super.equals(o)) {
- return false;
- }
-
- FlowRule flowRule = (FlowRule)o;
-
- if (grade != flowRule.grade) {
- return false;
- }
- if (Double.compare(flowRule.count, count) != 0) {
- return false;
- }
- if (strategy != flowRule.strategy) {
- return false;
- }
- if (refResource != null ? !refResource.equals(flowRule.refResource) : flowRule.refResource != null) {
- return false;
- }
- if (this.controlBehavior != flowRule.controlBehavior) {
- return false;
- }
-
- if (warmUpPeriodSec != flowRule.warmUpPeriodSec) {
- return false;
- }
-
- if (maxQueueingTimeMs != flowRule.maxQueueingTimeMs) {
- return false;
- }
+ if (this == o) { return true; }
+ if (o == null || getClass() != o.getClass()) { return false; }
+ if (!super.equals(o)) { return false; }
- return true;
+ FlowRule rule = (FlowRule)o;
+
+ if (grade != rule.grade) { return false; }
+ if (Double.compare(rule.count, count) != 0) { return false; }
+ if (strategy != rule.strategy) { return false; }
+ if (controlBehavior != rule.controlBehavior) { return false; }
+ if (warmUpPeriodSec != rule.warmUpPeriodSec) { return false; }
+ if (maxQueueingTimeMs != rule.maxQueueingTimeMs) { return false; }
+ if (clusterMode != rule.clusterMode) { return false; }
+ if (refResource != null ? !refResource.equals(rule.refResource) : rule.refResource != null) { return false; }
+ return clusterConfig != null ? clusterConfig.equals(rule.clusterConfig) : rule.clusterConfig == null;
}
@Override
@@ -217,10 +219,11 @@ public class FlowRule extends AbstractRule {
result = 31 * result + (int)(temp ^ (temp >>> 32));
result = 31 * result + strategy;
result = 31 * result + (refResource != null ? refResource.hashCode() : 0);
- result = 31 * result + (int)(temp ^ (temp >>> 32));
- result = 31 * result + warmUpPeriodSec;
result = 31 * result + controlBehavior;
+ result = 31 * result + warmUpPeriodSec;
result = 31 * result + maxQueueingTimeMs;
+ result = 31 * result + (clusterMode ? 1 : 0);
+ result = 31 * result + (clusterConfig != null ? clusterConfig.hashCode() : 0);
return result;
}
@@ -236,7 +239,9 @@ public class FlowRule extends AbstractRule {
", controlBehavior=" + controlBehavior +
", warmUpPeriodSec=" + warmUpPeriodSec +
", maxQueueingTimeMs=" + maxQueueingTimeMs +
+ ", clusterMode=" + clusterMode +
+ ", clusterConfig=" + clusterConfig +
", controller=" + controller +
- "}";
+ '}';
}
}
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleChecker.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleChecker.java
index 197a3bbf..7518d7c1 100644
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleChecker.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleChecker.java
@@ -15,7 +15,12 @@
*/
package com.alibaba.csp.sentinel.slots.block.flow;
+import com.alibaba.csp.sentinel.cluster.ClusterTokenClient;
+import com.alibaba.csp.sentinel.cluster.TokenClientProvider;
+import com.alibaba.csp.sentinel.cluster.TokenResultStatus;
+import com.alibaba.csp.sentinel.cluster.TokenResult;
import com.alibaba.csp.sentinel.context.Context;
+import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.node.Node;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
@@ -30,11 +35,25 @@ import com.alibaba.csp.sentinel.util.StringUtil;
final class FlowRuleChecker {
static boolean passCheck(/*@NonNull*/ FlowRule rule, Context context, DefaultNode node, int acquireCount) {
+ return passCheck(rule, context, node, acquireCount, false);
+ }
+
+ static boolean passCheck(/*@NonNull*/ FlowRule rule, Context context, DefaultNode node, int acquireCount,
+ boolean prioritized) {
String limitApp = rule.getLimitApp();
if (limitApp == null) {
return true;
}
+ if (rule.isClusterMode()) {
+ return passClusterCheck(rule, context, node, acquireCount, prioritized);
+ }
+
+ return passLocalCheck(rule, context, node, acquireCount, prioritized);
+ }
+
+ private static boolean passLocalCheck(FlowRule rule, Context context, DefaultNode node, int acquireCount,
+ boolean prioritized) {
Node selectedNode = selectNodeByRequesterAndStrategy(rule, context, node);
if (selectedNode == null) {
return true;
@@ -43,6 +62,38 @@ final class FlowRuleChecker {
return rule.getRater().canPass(selectedNode, acquireCount);
}
+ static boolean passClusterCheck(FlowRule rule, Context context, DefaultNode node, int acquireCount,
+ boolean prioritized) {
+ try {
+ ClusterTokenClient client = TokenClientProvider.getClient();
+ if (client != null) {
+ TokenResult result = client.requestToken(rule.getClusterConfig().getFlowId(), acquireCount,
+ prioritized);
+ switch (result.getStatus()) {
+ case TokenResultStatus.OK:
+ return true;
+ case TokenResultStatus.SHOULD_WAIT:
+ // Wait for next tick.
+ Thread.sleep(result.getWaitInMs());
+ return true;
+ case TokenResultStatus.NO_RULE_EXISTS:
+ case TokenResultStatus.BAD_REQUEST:
+ case TokenResultStatus.FAIL:
+ return passLocalCheck(rule, context, node, acquireCount, prioritized);
+ case TokenResultStatus.BLOCKED:
+ default:
+ return false;
+ }
+ }
+ // If client is absent, then fallback to local mode.
+ } catch (Throwable ex) {
+ RecordLog.warn("[FlowRuleChecker] Request cluster token unexpected failed", ex);
+ }
+ // TODO: choose whether fallback to local or inactivate the rule.
+ // Downgrade to local flow control when token client or server for this rule is not available.
+ return passLocalCheck(rule, context, node, acquireCount, prioritized);
+ }
+
static Node selectReferenceNode(FlowRule rule, Context context, DefaultNode node) {
String refResource = rule.getRefResource();
int strategy = rule.getStrategy();
@@ -103,4 +154,4 @@ final class FlowRuleChecker {
}
private FlowRuleChecker() {}
-}
+}
\ No newline at end of file
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleComparator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleComparator.java
index 688bed96..ed1b22c6 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleComparator.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleComparator.java
@@ -19,10 +19,23 @@ import java.util.Comparator;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
+/**
+ * Comparator for flow rules.
+ *
+ * @author jialiang.linjl
+ */
public class FlowRuleComparator implements Comparator
- * One resources can have multiple rules. And these rules take effects in the
- * following order:
+ * One resources can have multiple rules. And these rules take effects in the following order:
*
*