From b44f83cdf201da34ca0085d5e6c19636ceb7ea5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BE=BD?= Date: Thu, 9 Jan 2020 19:50:46 +0800 Subject: [PATCH] fix --- .../motan/cluster/support/ClusterSupport.java | 21 +++++++++---------- .../weibo/api/motan/common/URLParamType.java | 2 +- .../api/motan/config/ProtocolConfig.java | 13 ++++++++++-- .../api/motan/cluster/SelectUrlsTest.java | 2 +- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/motan-core/src/main/java/com/weibo/api/motan/cluster/support/ClusterSupport.java b/motan-core/src/main/java/com/weibo/api/motan/cluster/support/ClusterSupport.java index 31a6727fe..365379469 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/cluster/support/ClusterSupport.java +++ b/motan-core/src/main/java/com/weibo/api/motan/cluster/support/ClusterSupport.java @@ -66,7 +66,6 @@ public class ClusterSupport implements NotifyListener { protected ConcurrentHashMap> registryUrlsMap = new ConcurrentHashMap<>(); protected ConcurrentHashMap> registryActiveUrlsMap = new ConcurrentHashMap<>(); - protected ConcurrentHashMap> groupUrlsMap = new ConcurrentHashMap<>(); private int selectNodeCount; public ClusterSupport(Class interfaceClass, List registryUrls) { @@ -75,9 +74,9 @@ public ClusterSupport(Class interfaceClass, List registryUrls) { String urlStr = StringTools.urlDecode(registryUrls.get(0).getParameter(URLParamType.embed.getName())); this.url = URL.valueOf(urlStr); protocol = getDecorateProtocol(url.getProtocol()); - int connectionCount = this.url.getIntParameter(URLParamType.clientConnectionCount.getName(), URLParamType.clientConnectionCount.getIntValue()); + int maxConnectionCount = this.url.getIntParameter(URLParamType.maxConnectionPerGroup.getName(), URLParamType.maxConnectionPerGroup.getIntValue()); int maxClientConnection = this.url.getIntParameter(URLParamType.maxClientConnection.getName(), URLParamType.maxClientConnection.getIntValue()); - selectNodeCount = connectionCount / maxClientConnection; + selectNodeCount = maxConnectionCount / maxClientConnection; if (selectNodeCount != 0) { executorService = Executors.newScheduledThreadPool(1); @@ -196,7 +195,6 @@ public synchronized void notify(URL registryUrl, List urls) { // 判断urls中是否包含权重信息,并通知loadbalance。 processWeights(urls); - registryUrlsMap.put(registryUrl, urls); List serviceUrls = urls; if (selectNodeCount > 0 && MotanSwitcherUtil.switcherIsOpenWithDefault("feature.motan.partial.server", true)) { serviceUrls = selectUrls(registryUrl, urls); @@ -243,9 +241,8 @@ protected List selectUrls(URL registryUrl, List urls) { for (Map.Entry> 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; } @@ -261,21 +258,21 @@ protected List selectUrlsByGroup(URL registryUrl, String group, List n if (activeUrls == null) { activeUrls = new ArrayList<>(); } - List oldNotifyUrls = groupUrlsMap.get(group); - if (oldNotifyUrls == null) { - oldNotifyUrls = new ArrayList<>(); + List lastNotifyUrls = registryUrlsMap.get(registryUrl); + if (lastNotifyUrls == null) { + lastNotifyUrls = new ArrayList<>(); } List sameUrls = new ArrayList<>(notifyUrls); sameUrls.retainAll(activeUrls); Collections.shuffle(sameUrls); List addedUrls = new ArrayList<>(notifyUrls); - addedUrls.removeAll(oldNotifyUrls); + addedUrls.removeAll(lastNotifyUrls); Collections.shuffle(addedUrls); List 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); @@ -307,6 +304,8 @@ protected List selectUrlsByGroup(URL registryUrl, String group, List n public void checkReferers() { for (Map.Entry>> entry : registryReferers.entrySet()) { URL registryUrl = entry.getKey(); + LoggerUtil.info("ClusterSupport checkReferers: registry={} service={}", + registryUrl.getUri(), url.getIdentity()); int available = 0; for (Referer referer : entry.getValue()) { if (referer.isAvailable()) { diff --git a/motan-core/src/main/java/com/weibo/api/motan/common/URLParamType.java b/motan-core/src/main/java/com/weibo/api/motan/common/URLParamType.java index e4beb6c01..65021ee67 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/common/URLParamType.java +++ b/motan-core/src/main/java/com/weibo/api/motan/common/URLParamType.java @@ -50,7 +50,7 @@ public enum URLParamType { * pool max conn number **/ maxClientConnection("maxClientConnection", 10), - clientConnectionCount("clientConnectionCount", 0), + maxConnectionPerGroup("maxConnectionPerGroup", 0), /** * pool max conn number **/ diff --git a/motan-core/src/main/java/com/weibo/api/motan/config/ProtocolConfig.java b/motan-core/src/main/java/com/weibo/api/motan/config/ProtocolConfig.java index 216c6f331..627c8c944 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/config/ProtocolConfig.java +++ b/motan-core/src/main/java/com/weibo/api/motan/config/ProtocolConfig.java @@ -16,10 +16,10 @@ package com.weibo.api.motan.config; -import java.util.Map; - import com.weibo.api.motan.config.annotation.ConfigDesc; +import java.util.Map; + /** * * protocol @@ -48,6 +48,7 @@ public class ProtocolConfig extends AbstractConfig { protected Integer minClientConnection; // client最大连接数 protected Integer maxClientConnection; + protected Integer maxConnectionPerGroup; // 最小工作pool线程数 protected Integer minWorkerThread; // 最大工作pool线程数 @@ -164,6 +165,14 @@ public void setMaxClientConnection(Integer maxClientConnection) { this.maxClientConnection = maxClientConnection; } + public Integer getMaxConnectionPerGroup() { + return maxConnectionPerGroup; + } + + public void setMaxConnectionPerGroup(Integer maxConnectionPerGroup) { + this.maxConnectionPerGroup = maxConnectionPerGroup; + } + public Integer getMinWorkerThread() { return minWorkerThread; } diff --git a/motan-core/src/test/java/com/weibo/api/motan/cluster/SelectUrlsTest.java b/motan-core/src/test/java/com/weibo/api/motan/cluster/SelectUrlsTest.java index 7b513238c..2be22d0d0 100644 --- a/motan-core/src/test/java/com/weibo/api/motan/cluster/SelectUrlsTest.java +++ b/motan-core/src/test/java/com/weibo/api/motan/cluster/SelectUrlsTest.java @@ -39,7 +39,7 @@ public class SelectUrlsTest { private static List mockRegistryUrls() { URL refUrl = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, IHello.class.getName()); refUrl.addParameter(URLParamType.check.getName(), "false"); - refUrl.addParameter(URLParamType.clientConnectionCount.getName(), String.valueOf(count * URLParamType.maxClientConnection.getIntValue())); + refUrl.addParameter(URLParamType.maxConnectionPerGroup.getName(), String.valueOf(count * URLParamType.maxClientConnection.getIntValue())); URL url1 = new URL("reg_1", "192.168.1.1", 18081, RegistryService.class.getName()); url1.addParameter(URLParamType.embed.getName(), StringTools.urlEncode(refUrl.toFullStr()));