提交 7ca9e43e 编写于 作者: oldratlee's avatar oldratlee 🔥

提取常量CLUSTER_SWITCH_FACTOR DUBBO-577

上级 7988d105
...@@ -73,13 +73,7 @@ public class SwitchCluster implements Cluster { ...@@ -73,13 +73,7 @@ public class SwitchCluster implements Cluster {
return effectiveInvokers; return effectiveInvokers;
} }
static final int WIN_FACTOR = 2; static <T> Invoker<T> getSuitableInvoker(List<Invoker<T>> invokers, Directory<T> directory) {
/**
* 优先返回前面的Inovker,除非后面的Invoker的invoker.count值 >= 2倍。
* TODO Hard Code了因子 2!
*/
static <T> Invoker<T> getSuitableInvoker(List<Invoker<T>> invokers) {
if(invokers.isEmpty()) { if(invokers.isEmpty()) {
throw new RpcException("No provider available in " + invokers); throw new RpcException("No provider available in " + invokers);
} }
...@@ -87,13 +81,15 @@ public class SwitchCluster implements Cluster { ...@@ -87,13 +81,15 @@ public class SwitchCluster implements Cluster {
return invokers.get(0); return invokers.get(0);
} }
final double factor = directory.getUrl().getParameter(
Constants.CLUSTER_SWITCH_FACTOR, Constants.DEFAULT_CLUSTER_SWITCH_FACTOR);
int i = 0; int i = 0;
LOOP_BEFORE: LOOP_BEFORE:
for (; i < invokers.size(); i++) { for (; i < invokers.size(); i++) {
Invoker<T> before = invokers.get(i); Invoker<T> before = invokers.get(i);
for (int j = i + 1; j < invokers.size(); j++) { for (int j = i + 1; j < invokers.size(); j++) {
Invoker<T> after = invokers.get(j); Invoker<T> after = invokers.get(j);
if(WIN_FACTOR * getInvokerCount(before) <= getInvokerCount(after)) { if(factor * getInvokerCount(before) <= getInvokerCount(after)) {
// 被后面的打败了! 重找 // 被后面的打败了! 重找
continue LOOP_BEFORE; continue LOOP_BEFORE;
} }
...@@ -108,7 +104,7 @@ public class SwitchCluster implements Cluster { ...@@ -108,7 +104,7 @@ public class SwitchCluster implements Cluster {
return new AbstractClusterInvoker<T>(directory) { return new AbstractClusterInvoker<T>(directory) {
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException { public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
List<Invoker<T>> effectiveInvokers = getEffectiveInvokers(invokers); List<Invoker<T>> effectiveInvokers = getEffectiveInvokers(invokers);
return getSuitableInvoker(effectiveInvokers).invoke(invocation); return getSuitableInvoker(effectiveInvokers, directory).invoke(invocation);
} }
}; };
} }
......
...@@ -408,6 +408,17 @@ public class Constants { ...@@ -408,6 +408,17 @@ public class Constants {
*/ */
public static final String INVOKER_INSIDE_INVOKER_COUNT_KEY = "inside.invoker.count"; public static final String INVOKER_INSIDE_INVOKER_COUNT_KEY = "inside.invoker.count";
/**
* Switch cluster
* see SwitchCluster
*/
public static final String CLUSTER_SWITCH_FACTOR = "cluster.switch.factor";
/**
* default Switch cluster factor
*/
public static final double DEFAULT_CLUSTER_SWITCH_FACTOR = 2;
/** /**
* 集群时是否排除非available的invoker * 集群时是否排除非available的invoker
*/ */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册