From 5991c1af76a6b8e4178c2002fcea0a1a1d4e3a12 Mon Sep 17 00:00:00 2001 From: congzhou2603 Date: Mon, 8 May 2023 09:41:35 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90bugfix=E3=80=91=E4=BF=AE=E5=A4=8Dleast?= =?UTF-8?q?conn=E6=A8=A1=E5=BC=8F=E5=B9=B6=E5=8F=91=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E4=B8=8B=E6=9C=89=E5=B0=8F=E6=A6=82=E7=8E=87=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=AE=8C=E5=85=A8=E8=B4=9F=E8=BD=BD=E5=9D=87=E8=A1=A1=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../postgresql/quickautobalance/Cluster.java | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/pgjdbc/src/main/java/org/postgresql/quickautobalance/Cluster.java b/pgjdbc/src/main/java/org/postgresql/quickautobalance/Cluster.java index c38d4f7..86832cf 100644 --- a/pgjdbc/src/main/java/org/postgresql/quickautobalance/Cluster.java +++ b/pgjdbc/src/main/java/org/postgresql/quickautobalance/Cluster.java @@ -299,11 +299,9 @@ public class Cluster { * @param hostSpecs host specs * @return hostSpec list */ - public List sortDnsByLeastConn(List hostSpecs) { - Map dataNodeCompareInfoMap; + public synchronized List sortDnsByLeastConn(List hostSpecs) { // Copy dataNodeCompareInfo from cachedDnList. - synchronized (cachedDnList) { - dataNodeCompareInfoMap = hostSpecs.stream() + Map dataNodeCompareInfoMap = hostSpecs.stream() .collect(Collectors.toMap(Function.identity(), hostSpec -> { DataNode dataNode = cachedDnList.get(hostSpec); int cachedConnectionListSize = dataNode.getCachedConnectionListSize(); @@ -311,7 +309,6 @@ public class Cluster { boolean dataNodeState = dataNode.getDataNodeState(); return new DataNodeCompareInfo(cachedConnectionListSize, cachedCreatingConnectionSize, dataNodeState); })); - } hostSpecs.sort((o1, o2) -> { boolean o1State = dataNodeCompareInfoMap.get(o1).getDataNodeState(); boolean o2State = dataNodeCompareInfoMap.get(o2).getDataNodeState(); @@ -376,11 +373,11 @@ public class Cluster { @Override public String toString() { - return "{" + - "connectionListSize=" + connectionListSize + - ", cachedCreatedConnectionSize=" + cachedCreatedConnectionSize + - ", dataNodeState=" + dataNodeState + - '}'; + return "{" + + "connectionListSize=" + connectionListSize + + ", cachedCreatedConnectionSize=" + cachedCreatedConnectionSize + + ", dataNodeState=" + dataNodeState + + '}'; } } @@ -597,19 +594,21 @@ public class Cluster { @Override public boolean equals(final Object o) { - if (this == o) + if (this == o) { return true; - if (o == null || getClass() != o.getClass()) + } + if (o == null || getClass() != o.getClass()) { return false; + } final Cluster that = (Cluster) o; return Objects.equals(urlIdentifier, that.urlIdentifier) && Objects.equals(dns, that.dns); } /** - * Close connections from abandonedConnectionList. - * Jdbc will continue to pop connections from abandonedConnectionList and determine whether each connection can be closed. - * If it does, the connection will be close. If it does not, the connection won't be put back into abandonedConnectionList. - * The number of connections closed at each cluster for each scheduled task at most: 20% * totalAbandonedConnectionSize. + * Close connections from abandonedConnectionList.Jdbc will continue to pop connections from abandonedConnectionList + * and determine whether each connection can be closed. If it does, the connection will be close. If it does not, + * the connection won't be put back into abandonedConnectionList. The number of connections closed at each cluster + * for each scheduled task at most: 20% * totalAbandonedConnectionSize. * * @return the number of connections which are closed */ @@ -651,9 +650,9 @@ public class Cluster { this.quickAutoBalanceStartTime = 0; this.totalAbandonedConnectionSize = 0; } - LOGGER.info(GT.tr("Close connections execute in cluster: {0}, closed connections: {1}, " + - "size of abandonedConnectionList before closing: {2}," + - " size of abandonedConnectionList after closing: {3}.", + LOGGER.info(GT.tr("Close connections execute in cluster: {0}, closed connections: {1}, " + + "size of abandonedConnectionList before closing: {2}," + + " size of abandonedConnectionList after closing: {3}.", urlIdentifier, closed, oldSize, abandonedConnectionList.size())); } return closed; -- Gitee