Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnights committed Jan 10, 2020
1 parent ee8fb9b commit 37d0018
Showing 1 changed file with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public class ClusterSupport<T> implements NotifyListener {

protected ConcurrentHashMap<URL, List<URL>> registryUrlsMap = new ConcurrentHashMap<>();
protected ConcurrentHashMap<URL, List<URL>> registryActiveUrlsMap = new ConcurrentHashMap<>();
protected ConcurrentHashMap<String, List<URL>> groupUrlsMap = new ConcurrentHashMap<>();
private int selectNodeCount;

public ClusterSupport(Class<T> interfaceClass, List<URL> registryUrls) {
Expand Down Expand Up @@ -196,7 +195,6 @@ public synchronized void notify(URL registryUrl, List<URL> urls) {
// 判断urls中是否包含权重信息,并通知loadbalance。
processWeights(urls);

registryUrlsMap.put(registryUrl, urls);
List<URL> serviceUrls = urls;
if (selectNodeCount > 0 && MotanSwitcherUtil.switcherIsOpenWithDefault("feature.motan.partial.server", true)) {
serviceUrls = selectUrls(registryUrl, urls);
Expand Down Expand Up @@ -243,9 +241,8 @@ protected List<URL> selectUrls(URL registryUrl, List<URL> urls) {
for (Map.Entry<String, List<URL>> entry : groupUrlsMap.entrySet()) {
result.addAll(selectUrlsByGroup(registryUrl, entry.getKey(), entry.getValue()));
}
registryUrlsMap.put(registryUrl, urls);
registryActiveUrlsMap.put(registryUrl, result);
this.groupUrlsMap.clear();
this.groupUrlsMap.putAll(groupUrlsMap);
return result;
}

Expand All @@ -261,21 +258,21 @@ protected List<URL> selectUrlsByGroup(URL registryUrl, String group, List<URL> n
if (activeUrls == null) {
activeUrls = new ArrayList<>();
}
List<URL> oldNotifyUrls = groupUrlsMap.get(group);
if (oldNotifyUrls == null) {
oldNotifyUrls = new ArrayList<>();
List<URL> lastNotifyUrls = registryUrlsMap.get(registryUrl);
if (lastNotifyUrls == null) {
lastNotifyUrls = new ArrayList<>();
}

List<URL> sameUrls = new ArrayList<>(notifyUrls);
sameUrls.retainAll(activeUrls);
Collections.shuffle(sameUrls);
List<URL> addedUrls = new ArrayList<>(notifyUrls);
addedUrls.removeAll(oldNotifyUrls);
addedUrls.removeAll(lastNotifyUrls);
Collections.shuffle(addedUrls);

List<URL> groupUrls = new ArrayList<>();
// 计算重用节点数量
int newCount = Math.round((float) selectNodeCount * addedUrls.size() / (oldNotifyUrls.size() + addedUrls.size()));
int newCount = Math.round((float) selectNodeCount * addedUrls.size() / notifyUrls.size());
int remainCount = selectNodeCount - newCount;
// 至少三分之一的节点参与随机选择
remainCount = Math.min(remainCount, 2 * selectNodeCount / 3);
Expand Down

0 comments on commit 37d0018

Please # to comment.