From 66f3db9fe0788684cfabcd7a018ea24550ca54de Mon Sep 17 00:00:00 2001 From: LUA Date: Tue, 9 Aug 2022 10:29:20 +0800 Subject: [PATCH] perf: reduce the number of test connections (#3343) --- .../dble/config/ConfigInitializer.java | 61 +++++++++---------- .../actiontech/dble/config/ServerConfig.java | 6 +- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/actiontech/dble/config/ConfigInitializer.java b/src/main/java/com/actiontech/dble/config/ConfigInitializer.java index 010aa73ae..f6effc60f 100644 --- a/src/main/java/com/actiontech/dble/config/ConfigInitializer.java +++ b/src/main/java/com/actiontech/dble/config/ConfigInitializer.java @@ -34,6 +34,7 @@ import com.actiontech.dble.services.manager.response.ChangeItemType; import com.actiontech.dble.services.manager.response.ChangeType; import com.actiontech.dble.singleton.TraceManager; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -245,6 +246,7 @@ public class ConfigInitializer implements ProblemReporter { // check whether dbInstance is connected String dbGroupName; PhysicalDbGroup dbGroup; + Set dbGroupTested = Sets.newHashSet(); for (ChangeItem changeItem : changeItemList) { ChangeType type = changeItem.getType(); @@ -255,18 +257,10 @@ public class ConfigInitializer implements ProblemReporter { if (itemType == ChangeItemType.PHYSICAL_DB_GROUP) { //test dbGroup dbGroup = (PhysicalDbGroup) item; - dbGroupName = dbGroup.getGroupName(); - // sharding group - List> schemaList = checkDbGroupMaxConn(hostSchemaMap, dbGroup); - - for (PhysicalDbInstance ds : dbGroup.getDbInstances(true)) { - //test dbInstance - boolean testResult = checkAndTestDbInstance(ds, dbGroupName, schemaList); - if (!testResult) { - isAllDbInstanceConnected = false; - errDbInstanceNames.add("dbInstance[" + dbGroupName + "." + ds.getName() + "]"); - } + boolean isDbInstanceConnected = testDbGroup(dbGroup, hostSchemaMap, dbGroupTested, errDbInstanceNames); + if (!isDbInstanceConnected) { + isAllDbInstanceConnected = false; } } else if (itemType == ChangeItemType.PHYSICAL_DB_INSTANCE) { PhysicalDbInstance ds = (PhysicalDbInstance) item; @@ -282,17 +276,10 @@ public class ConfigInitializer implements ProblemReporter { } else if (itemType == ChangeItemType.SHARDING_NODE) { ShardingNode shardingNode = (ShardingNode) item; dbGroup = shardingNode.getDbGroup(); - dbGroupName = dbGroup.getGroupName(); - // sharding group - List> schemaList = checkDbGroupMaxConn(hostSchemaMap, dbGroup); - for (PhysicalDbInstance ds : dbGroup.getDbInstances(true)) { - //test dbInstance - boolean testResult = checkAndTestDbInstance(ds, dbGroupName, schemaList); - if (!testResult) { - isAllDbInstanceConnected = false; - errDbInstanceNames.add("dbInstance[" + dbGroupName + "." + ds.getName() + "]"); - } + boolean isDbInstanceConnected = testDbGroup(dbGroup, hostSchemaMap, dbGroupTested, errDbInstanceNames); + if (!isDbInstanceConnected) { + isAllDbInstanceConnected = false; } } break; @@ -312,17 +299,10 @@ public class ConfigInitializer implements ProblemReporter { } else if (itemType == ChangeItemType.SHARDING_NODE) { ShardingNode shardingNode = (ShardingNode) item; dbGroup = shardingNode.getDbGroup(); - dbGroupName = dbGroup.getGroupName(); - // sharding group - List> schemaList = checkDbGroupMaxConn(hostSchemaMap, dbGroup); - for (PhysicalDbInstance ds : dbGroup.getDbInstances(true)) { - //test dbInstance - boolean testResult = checkAndTestDbInstance(ds, dbGroupName, schemaList); - if (!testResult) { - isAllDbInstanceConnected = false; - errDbInstanceNames.add("dbInstance[" + dbGroupName + "." + ds.getName() + "]"); - } + boolean isDbInstanceConnected = testDbGroup(dbGroup, hostSchemaMap, dbGroupTested, errDbInstanceNames); + if (!isDbInstanceConnected) { + isAllDbInstanceConnected = false; } } break; @@ -401,6 +381,25 @@ public class ConfigInitializer implements ProblemReporter { } } + private boolean testDbGroup(PhysicalDbGroup dbGroup, Map>> hostSchemaMap, Set dbGroupTested, Set errDbInstanceNames) { + String dbGroupName = dbGroup.getGroupName(); + boolean isAllDbInstanceConnected = true; + if (dbGroupTested.add(dbGroupName)) { + // sharding group + List> schemaList = checkDbGroupMaxConn(hostSchemaMap, dbGroup); + + for (PhysicalDbInstance ds : dbGroup.getDbInstances(true)) { + //test dbInstance + boolean testResult = checkAndTestDbInstance(ds, dbGroupName, schemaList); + if (!testResult) { + isAllDbInstanceConnected = false; + errDbInstanceNames.add("dbInstance[" + dbGroupName + "." + ds.getName() + "]"); + } + } + } + return isAllDbInstanceConnected; + } + private List> checkDbInstanceMaxConn(Map>> hostSchemaMap, PhysicalDbInstance ds) { List> schemaList = null; if (hostSchemaMap.containsKey(ds.getDbGroupConfig().getName())) { diff --git a/src/main/java/com/actiontech/dble/config/ServerConfig.java b/src/main/java/com/actiontech/dble/config/ServerConfig.java index 349f4425f..396d5a2ae 100644 --- a/src/main/java/com/actiontech/dble/config/ServerConfig.java +++ b/src/main/java/com/actiontech/dble/config/ServerConfig.java @@ -523,9 +523,10 @@ public class ServerConfig { PhysicalDbInstance physicalDbInstance = (PhysicalDbInstance) item; //delete slave instance PhysicalDbGroup physicalDbGroup = oldDbGroupMap.get(physicalDbInstance.getDbGroupConfig().getName()); - PhysicalDbInstance oldDbInstance = removeDbInstance(physicalDbGroup, physicalDbInstance.getName()); + PhysicalDbInstance oldDbInstance = physicalDbGroup.getAllDbInstanceMap().get(physicalDbInstance.getName()); oldDbInstance.stop("reload config, recycle old instance", ((loadAllMode & ManagerParseConfig.OPTF_MODE) != 0)); oldDbInstance = null; + removeDbInstance(physicalDbGroup, physicalDbInstance.getName()); } else if (itemType == ChangeItemType.SHARDING_NODE) { ShardingNode shardingNode = (ShardingNode) item; if (shardingNode.getDbGroup() != null) { @@ -566,9 +567,10 @@ public class ServerConfig { if (changeItem.isAffectHeartbeat() || changeItem.isAffectConnectionPool()) { PhysicalDbInstance physicalDbInstance = (PhysicalDbInstance) item; PhysicalDbGroup physicalDbGroup = oldDbGroupMap.get(physicalDbInstance.getDbGroupConfig().getName()); - PhysicalDbInstance oldDbInstance = removeDbInstance(physicalDbGroup, physicalDbInstance.getName()); + PhysicalDbInstance oldDbInstance = physicalDbGroup.getAllDbInstanceMap().get(physicalDbInstance.getName()); oldDbInstance.stop("reload config, recycle old instance", ((loadAllMode & ManagerParseConfig.OPTF_MODE) != 0)); oldDbInstance = null; + removeDbInstance(physicalDbGroup, physicalDbInstance.getName()); physicalDbInstance.init("reload config", true); physicalDbGroup.setDbInstance(physicalDbInstance); } else {