You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/04/26 09:57:33 UTC

[GitHub] [shardingsphere] terrymanu commented on a diff in pull request #17049: optimization check highly available status

terrymanu commented on code in PR #17049:
URL: https://github.com/apache/shardingsphere/pull/17049#discussion_r858521990


##########
shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java:
##########
@@ -43,31 +49,41 @@
 @RequiredArgsConstructor
 @Slf4j
 public final class DatabaseDiscoveryEngine {
+
+    private static final int CPU_CORES = Runtime.getRuntime().availableProcessors();
     
     private final DatabaseDiscoveryProviderAlgorithm databaseDiscoveryProviderAlgorithm;
-    
+
     /**
      * Check environment of database cluster.
      *
-     * @param databaseName database name
+     * @param databaseName  database name
      * @param dataSourceMap data source map
      * @throws SQLException SQL exception
      */
     public void checkEnvironment(final String databaseName, final Map<String, DataSource> dataSourceMap) throws SQLException {
+        ExecutorEngine executorEngine = new ExecutorEngine(Math.min(CPU_CORES * 2, dataSourceMap.isEmpty() ? 1 : dataSourceMap.entrySet().size()));
+        ExecutorDataMap.getValue().put(DatabaseDiscoveryExecutorCallback.DATABASE_NAME, databaseName);
+        executorEngine.execute(createExecutionGroupContext(dataSourceMap), new DatabaseDiscoveryExecutorCallback(databaseDiscoveryProviderAlgorithm));
+    }
+
+    private ExecutionGroupContext<DataSource> createExecutionGroupContext(final Map<String, DataSource> dataSourceMap) {
+        Collection<ExecutionGroup<DataSource>> inputGroups = new ArrayList<>();
         for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
-            // TODO query with multiple threads
-            databaseDiscoveryProviderAlgorithm.checkEnvironment(databaseName, entry.getValue());
+            ExecutionGroup<DataSource> executionGroup = new ExecutionGroup<>(Lists.newArrayList(entry.getValue()));
+            inputGroups.add(executionGroup);
         }
+        return new ExecutionGroupContext<>(inputGroups);
     }
-    
+
     /**
      * Change primary data source.
      *
-     * @param databaseName database name
-     * @param groupName group name
+     * @param databaseName                  database name
+     * @param groupName                     group name
      * @param originalPrimaryDataSourceName original primary data source name
-     * @param dataSourceMap data source map
-     * @param disabledDataSourceNames disabled data source names
+     * @param dataSourceMap                 data source map
+     * @param disabledDataSourceNames       disabled data source names

Review Comment:
   Please do not change original indent



##########
shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java:
##########
@@ -80,7 +96,7 @@ public String changePrimaryDataSource(final String databaseName, final String gr
         postReplicaDataSourceDisabledEvent(databaseName, groupName, result, dataSourceMap);
         return result;
     }
-    
+

Review Comment:
   Please do not change original indent



##########
shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java:
##########
@@ -93,27 +109,27 @@ private Optional<String> findPrimaryDataSourceName(final Map<String, DataSource>
         }
         return Optional.empty();
     }
-    
+
     private Map<String, DataSource> getActiveDataSourceMap(final Map<String, DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames) {
         Map<String, DataSource> result = new HashMap<>(dataSourceMap);
         if (!disabledDataSourceNames.isEmpty()) {
             result.entrySet().removeIf(each -> disabledDataSourceNames.contains(each.getKey()));
         }
         return result;
     }
-    
+

Review Comment:
   Please do not change original indent



##########
shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java:
##########
@@ -43,31 +49,41 @@
 @RequiredArgsConstructor
 @Slf4j
 public final class DatabaseDiscoveryEngine {
+
+    private static final int CPU_CORES = Runtime.getRuntime().availableProcessors();
     
     private final DatabaseDiscoveryProviderAlgorithm databaseDiscoveryProviderAlgorithm;
-    
+
     /**
      * Check environment of database cluster.
      *
-     * @param databaseName database name
+     * @param databaseName  database name

Review Comment:
   Please do not change original indent



##########
shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java:
##########
@@ -93,27 +109,27 @@ private Optional<String> findPrimaryDataSourceName(final Map<String, DataSource>
         }
         return Optional.empty();
     }
-    
+
     private Map<String, DataSource> getActiveDataSourceMap(final Map<String, DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames) {
         Map<String, DataSource> result = new HashMap<>(dataSourceMap);
         if (!disabledDataSourceNames.isEmpty()) {
             result.entrySet().removeIf(each -> disabledDataSourceNames.contains(each.getKey()));
         }
         return result;
     }
-    
+
     private void postReplicaDataSourceDisabledEvent(final String databaseName, final String groupName, final String primaryDataSourceName, final Map<String, DataSource> dataSourceMap) {
         for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
             if (!entry.getKey().equals(primaryDataSourceName)) {
                 ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(databaseName, groupName, entry.getKey(), createStorageNodeDataSource(loadReplicaStatus(entry.getValue()))));
             }
         }
     }
-    
+
     private StorageNodeDataSource createStorageNodeDataSource(final ReplicaDataSourceStatus replicaStatus) {
         return new StorageNodeDataSource(StorageNodeRole.MEMBER, replicaStatus.isOnline() ? StorageNodeStatus.ENABLED : StorageNodeStatus.DISABLED, replicaStatus.getReplicationDelayMilliseconds());
     }
-    
+

Review Comment:
   Please do not change original indent



##########
shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java:
##########
@@ -93,27 +109,27 @@ private Optional<String> findPrimaryDataSourceName(final Map<String, DataSource>
         }
         return Optional.empty();
     }
-    
+
     private Map<String, DataSource> getActiveDataSourceMap(final Map<String, DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames) {
         Map<String, DataSource> result = new HashMap<>(dataSourceMap);
         if (!disabledDataSourceNames.isEmpty()) {
             result.entrySet().removeIf(each -> disabledDataSourceNames.contains(each.getKey()));
         }
         return result;
     }
-    
+
     private void postReplicaDataSourceDisabledEvent(final String databaseName, final String groupName, final String primaryDataSourceName, final Map<String, DataSource> dataSourceMap) {
         for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
             if (!entry.getKey().equals(primaryDataSourceName)) {
                 ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(databaseName, groupName, entry.getKey(), createStorageNodeDataSource(loadReplicaStatus(entry.getValue()))));
             }
         }
     }
-    
+

Review Comment:
   Please do not change original indent



##########
shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java:
##########
@@ -93,27 +109,27 @@ private Optional<String> findPrimaryDataSourceName(final Map<String, DataSource>
         }
         return Optional.empty();
     }
-    
+

Review Comment:
   Please do not change original indent



-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org