You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/02/02 11:15:49 UTC
[shardingsphere] branch master updated: Refactor DatabaseDiscoveryProviderAlgorithm implement use Future replace CompletableFuture. (#23928)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new ed5a72e2efd Refactor DatabaseDiscoveryProviderAlgorithm implement use Future replace CompletableFuture. (#23928)
ed5a72e2efd is described below
commit ed5a72e2efd116252c5f459fabb2f54049567cb1
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Thu Feb 2 19:15:38 2023 +0800
Refactor DatabaseDiscoveryProviderAlgorithm implement use Future replace CompletableFuture. (#23928)
* Refactor DatabaseDiscoveryProviderAlgorithm implement use Future replace CompletableFuture.
* Refactor logic
---
...MGRMySQLDatabaseDiscoveryProviderAlgorithm.java | 34 ++++++++-----------
...licationDatabaseDiscoveryProviderAlgorithm.java | 39 +++++++++-------------
2 files changed, 28 insertions(+), 45 deletions(-)
diff --git a/features/db-discovery/provider/mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java b/features/db-discovery/provider/mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java
index 47a08e5a559..0517321ca3c 100644
--- a/features/db-discovery/provider/mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java
+++ b/features/db-discovery/provider/mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.dbdiscovery.mysql.type;
+import lombok.SneakyThrows;
import org.apache.shardingsphere.dbdiscovery.mysql.exception.mgr.InvalidMGRGroupNameConfigurationException;
import org.apache.shardingsphere.dbdiscovery.mysql.exception.mgr.InvalidMGRModeException;
import org.apache.shardingsphere.dbdiscovery.mysql.exception.mgr.InvalidMGRPluginException;
@@ -26,7 +27,6 @@ import org.apache.shardingsphere.dbdiscovery.spi.ReplicaDataSourceStatus;
import org.apache.shardingsphere.infra.database.metadata.dialect.MySQLDataSourceMetaData;
import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.util.exception.external.sql.type.wrapper.SQLWrapperException;
import javax.sql.DataSource;
import java.sql.Connection;
@@ -35,12 +35,12 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Properties;
-import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
/**
* MGR database discovery provider algorithm for MySQL.
@@ -70,31 +70,20 @@ public final class MGRMySQLDatabaseDiscoveryProviderAlgorithm implements Databas
groupName = props.getProperty("group-name", "");
}
+ @SneakyThrows({InterruptedException.class, ExecutionException.class})
@Override
public void checkEnvironment(final String databaseName, final Collection<DataSource> dataSources) {
ExecutorService executorService = ExecutorEngine.createExecutorEngineWithCPUAndResources(dataSources.size()).getExecutorServiceManager().getExecutorService();
- Collection<CompletableFuture<Void>> completableFutures = new LinkedList<>();
- for (DataSource dataSource : dataSources) {
- completableFutures.add(runAsyncCheckEnvironment(databaseName, dataSource, executorService));
+ Collection<Future<Boolean>> futures = new LinkedList<>();
+ for (DataSource each : dataSources) {
+ futures.add(executorService.submit(() -> checkDataSourceEnvironment(databaseName, each)));
}
- CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0]));
- Iterator<CompletableFuture<Void>> mgrInstancesFuture = completableFutures.stream().iterator();
- while (mgrInstancesFuture.hasNext()) {
- mgrInstancesFuture.next().join();
+ for (Future<Boolean> each : futures) {
+ each.get();
}
}
- private CompletableFuture<Void> runAsyncCheckEnvironment(final String databaseName, final DataSource dataSource, final ExecutorService executorService) {
- return CompletableFuture.runAsync(() -> {
- try {
- checkSingleDataSourceEnvironment(databaseName, dataSource);
- } catch (final SQLException ex) {
- throw new SQLWrapperException(ex);
- }
- }, executorService);
- }
-
- private void checkSingleDataSourceEnvironment(final String databaseName, final DataSource dataSource) throws SQLException {
+ private Boolean checkDataSourceEnvironment(final String databaseName, final DataSource dataSource) {
try (
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
@@ -102,6 +91,9 @@ public final class MGRMySQLDatabaseDiscoveryProviderAlgorithm implements Databas
checkSinglePrimaryMode(databaseName, statement);
checkGroupName(databaseName, statement);
checkMemberInstanceURL(databaseName, connection.getMetaData().getURL(), statement);
+ return Boolean.TRUE;
+ } catch (final SQLException ignored) {
+ return Boolean.FALSE;
}
}
diff --git a/features/db-discovery/provider/mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm.java b/features/db-discovery/provider/mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm.java
index 9f4541fd11f..3f340b03db4 100644
--- a/features/db-discovery/provider/mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm.java
+++ b/features/db-discovery/provider/mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm.java
@@ -17,12 +17,12 @@
package org.apache.shardingsphere.dbdiscovery.mysql.type;
+import lombok.SneakyThrows;
import org.apache.shardingsphere.dbdiscovery.mysql.exception.replica.DuplicatePrimaryDataSourceException;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
import org.apache.shardingsphere.dbdiscovery.spi.ReplicaDataSourceStatus;
import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.util.exception.external.sql.type.wrapper.SQLWrapperException;
import javax.sql.DataSource;
import java.sql.Connection;
@@ -30,13 +30,14 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Properties;
-import java.util.concurrent.CompletableFuture;
+import java.util.TreeSet;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
-import java.util.stream.Collectors;
+import java.util.concurrent.Future;
/**
* Normal replication database discovery provider algorithm for MySQL.
@@ -59,32 +60,22 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm impl
delayMillisecondsThreshold = Long.parseLong(props.getProperty("delay-milliseconds-threshold", "0"));
}
+ @SneakyThrows({InterruptedException.class, ExecutionException.class})
@Override
public void checkEnvironment(final String databaseName, final Collection<DataSource> dataSources) {
ExecutorService executorService = ExecutorEngine.createExecutorEngineWithCPUAndResources(dataSources.size()).getExecutorServiceManager().getExecutorService();
- checkPrimaryDataSource(databaseName, dataSources.stream().map(each -> asyncCheckEnvironment(executorService, each)).collect(Collectors.toList()));
- }
-
- private void checkPrimaryDataSource(final String databaseName, final Collection<CompletableFuture<Boolean>> completableFutures) {
- CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0]));
- Iterator<CompletableFuture<Boolean>> primaryInstancesFuture = completableFutures.stream().iterator();
- int primaryInstanceCount = 0;
- while (primaryInstancesFuture.hasNext()) {
- if (primaryInstancesFuture.next().join()) {
- primaryInstanceCount++;
- }
+ Collection<Future<Boolean>> futures = new LinkedList<>();
+ Set<Boolean> primaryInstances = new TreeSet<>();
+ for (DataSource each : dataSources) {
+ futures.add(executorService.submit(() -> queryPrimaryInstance(each, primaryInstances)));
+ }
+ for (Future<Boolean> each : futures) {
+ ShardingSpherePreconditions.checkState(each.get(), () -> new DuplicatePrimaryDataSourceException(databaseName));
}
- ShardingSpherePreconditions.checkState(1 == primaryInstanceCount, () -> new DuplicatePrimaryDataSourceException(databaseName));
}
- private CompletableFuture<Boolean> asyncCheckEnvironment(final ExecutorService executorService, final DataSource dataSource) {
- return CompletableFuture.supplyAsync(() -> {
- try {
- return isPrimaryInstance(dataSource);
- } catch (final SQLException ex) {
- throw new SQLWrapperException(ex);
- }
- }, executorService);
+ private synchronized boolean queryPrimaryInstance(final DataSource dataSource, final Collection<Boolean> primaryInstances) throws SQLException {
+ return isPrimaryInstance(dataSource) && primaryInstances.add(Boolean.TRUE);
}
@Override