kylixs commented on a change in pull request #9015:
URL: https://github.com/apache/dubbo/pull/9015#discussion_r739002365
##########
File path:
dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/zookeeper/AbstractZookeeperTransporter.java
##########
@@ -54,25 +60,98 @@ public ZookeeperClient connect(URL url) {
ZookeeperClient zookeeperClient;
// address format: {[username:password@]address}
List<String> addressList = getURLBackupAddress(url);
- // The field define the zookeeper server , including protocol, host,
port, username, password
- if ((zookeeperClient =
fetchAndUpdateZookeeperClientCache(addressList)) != null &&
zookeeperClient.isConnected()) {
- logger.info("find valid zookeeper client from the cache for
address: " + url);
- return zookeeperClient;
- }
+ String application = url.getParameter(APPLICATION_KEY,
StringUtils.EMPTY_STRING);
// avoid creating too many connections, so add lock
- synchronized (zookeeperClientMap) {
- if ((zookeeperClient =
fetchAndUpdateZookeeperClientCache(addressList)) != null &&
zookeeperClient.isConnected()) {
+ synchronized (zookeeperApplicationMap) {
+ if ((zookeeperClient =
fetchAndUpdateZookeeperClientCache(addressList, application)) != null &&
+ zookeeperClient.isConnected()) {
logger.info("find valid zookeeper client from the cache for
address: " + url);
return zookeeperClient;
}
zookeeperClient = createZookeeperClient(url);
logger.info("No valid zookeeper client found from cache, therefore
create a new client for url. " + url);
writeToClientMap(addressList, zookeeperClient);
+ Set<String> appSet = new HashSet<>();
+ appSet.add(application);
+ zookeeperApplicationMap.put(zookeeperClient, appSet);
}
return zookeeperClient;
}
+ /**
+ * close the zookeeper client if it is no longer used by any application.
+ *
+ * @param zookeeperClient
+ * @param application the application which is going to be destroyed.
+ */
+ @Override
+ public void close(ZookeeperClient zookeeperClient, String application) {
+ synchronized (zookeeperApplicationMap) {
+ Set<String> appSet = zookeeperApplicationMap.get(zookeeperClient);
+ if (appSet == null) {
+ // zookeeperClient might be closed by other zookeeper registry
of the same application.
Review comment:
不能出现zk client 被多个组件轮流关闭的情况,这将导致后面使用zk client的组件出现错误。 有两个时机可以安全清理zk
clients:
1. Application 关闭时
2. Framework destroy 时
It is not possible that the zk client is closed by multiple components in
turn, which will cause errors in the components that use the zk client later.
There are two opportunities to safely clean up zk clients:
1. When Application is closed
2. When Framework destroy
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]