You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2020/12/22 09:08:15 UTC
[shardingsphere] branch master updated: #7318,
refactor MGRHAType (#8714)
This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 e70fb35 #7318, refactor MGRHAType (#8714)
e70fb35 is described below
commit e70fb353b4645efb4df9c791e55c96c6ca510cf6
Author: Zhang Yonglun <zh...@apache.org>
AuthorDate: Tue Dec 22 17:07:57 2020 +0800
#7318, refactor MGRHAType (#8714)
* #7318, move MGRHAType out of the common module
* #7318, add SQLException log
* #7318, remove primaryDataSource
---
shardingsphere-features/shardingsphere-ha/pom.xml | 1 +
.../{ => shardingsphere-ha-mgr}/pom.xml | 27 +++++++++++++-------
.../apache/shardingsphere/ha/mgr}/MGRHAType.java | 29 +++++++++-------------
.../org.apache.shardingsphere.ha.spi.HAType | 2 +-
4 files changed, 32 insertions(+), 27 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-ha/pom.xml b/shardingsphere-features/shardingsphere-ha/pom.xml
index 2bd77b7..af9fb36 100644
--- a/shardingsphere-features/shardingsphere-ha/pom.xml
+++ b/shardingsphere-features/shardingsphere-ha/pom.xml
@@ -32,6 +32,7 @@
<modules>
<module>shardingsphere-ha-api</module>
<module>shardingsphere-ha-common</module>
+ <module>shardingsphere-ha-mgr</module>
<module>shardingsphere-ha-route</module>
<module>shardingsphere-ha-spring</module>
</modules>
diff --git a/shardingsphere-features/shardingsphere-ha/pom.xml b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-mgr/pom.xml
similarity index 67%
copy from shardingsphere-features/shardingsphere-ha/pom.xml
copy to shardingsphere-features/shardingsphere-ha/shardingsphere-ha-mgr/pom.xml
index 2bd77b7..c2dc753 100644
--- a/shardingsphere-features/shardingsphere-ha/pom.xml
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-mgr/pom.xml
@@ -22,17 +22,26 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-features</artifactId>
+ <artifactId>shardingsphere-ha</artifactId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
- <artifactId>shardingsphere-ha</artifactId>
+ <artifactId>shardingsphere-ha-mgr</artifactId>
<name>${project.artifactId}</name>
- <packaging>pom</packaging>
- <modules>
- <module>shardingsphere-ha-api</module>
- <module>shardingsphere-ha-common</module>
- <module>shardingsphere-ha-route</module>
- <module>shardingsphere-ha-spring</module>
- </modules>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-ha-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.zaxxer</groupId>
+ <artifactId>HikariCP</artifactId>
+ </dependency>
+ </dependencies>
</project>
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/algorithm/MGRHAType.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java
similarity index 89%
rename from shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/algorithm/MGRHAType.java
rename to shardingsphere-features/shardingsphere-ha/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java
index 5378de1..5e57117 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/algorithm/MGRHAType.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java
@@ -15,10 +15,11 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.ha.algorithm;
+package org.apache.shardingsphere.ha.mgr;
import lombok.Getter;
import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.ha.spi.HAType;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
@@ -39,6 +40,7 @@ import java.util.concurrent.TimeUnit;
/**
* MGR HA type.
*/
+@Slf4j
public final class MGRHAType implements HAType {
private static final String PLUGIN_STATUS = "SELECT * FROM information_schema.PLUGINS WHERE PLUGIN_NAME='group_replication'";
@@ -51,8 +53,6 @@ public final class MGRHAType implements HAType {
private static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE = Executors.newSingleThreadScheduledExecutor();
- private String primaryDataSource;
-
private String oldPrimaryDataSource;
@Getter
@@ -61,7 +61,7 @@ public final class MGRHAType implements HAType {
@Override
public void checkHAConfig(final Map<String, DataSource> dataSourceMap, final String schemaName) throws SQLException {
- try (Connection connection = dataSourceMap.get(primaryDataSource).getConnection();
+ try (Connection connection = dataSourceMap.get(oldPrimaryDataSource).getConnection();
Statement statement = connection.createStatement()) {
checkPluginIsActive(statement);
checkReplicaMemberCount(statement);
@@ -118,15 +118,12 @@ public final class MGRHAType implements HAType {
if (newPrimaryDataSource.isEmpty()) {
return;
}
- if (null == oldPrimaryDataSource && null == primaryDataSource) {
- oldPrimaryDataSource = newPrimaryDataSource;
- primaryDataSource = newPrimaryDataSource;
- ShardingSphereEventBus.getInstance().post(new PrimaryDataSourceUpdateEvent(schemaName, primaryDataSource, oldPrimaryDataSource));
+ if (null == oldPrimaryDataSource) {
+ ShardingSphereEventBus.getInstance().post(new PrimaryDataSourceUpdateEvent(schemaName, newPrimaryDataSource, newPrimaryDataSource));
} else if (!newPrimaryDataSource.equals(oldPrimaryDataSource)) {
- oldPrimaryDataSource = primaryDataSource;
- primaryDataSource = newPrimaryDataSource;
- ShardingSphereEventBus.getInstance().post(new PrimaryDataSourceUpdateEvent(schemaName, primaryDataSource, oldPrimaryDataSource));
+ ShardingSphereEventBus.getInstance().post(new PrimaryDataSourceUpdateEvent(schemaName, newPrimaryDataSource, oldPrimaryDataSource));
}
+ oldPrimaryDataSource = newPrimaryDataSource;
}
private String determinePrimaryDataSource(final Map<String, DataSource> dataSourceMap) {
@@ -145,9 +142,8 @@ public final class MGRHAType implements HAType {
if (resultSet.next()) {
return String.format("%s:%s", resultSet.getString("MEMBER_HOST"), resultSet.getString("MEMBER_PORT"));
}
- // CHECKSTYLE:OFF
- } catch (final Exception ex) {
- // CHECKSTYLE:ON
+ } catch (final SQLException ex) {
+ log.error("An exception occurred while find primary data source url", ex);
}
}
return result;
@@ -161,9 +157,8 @@ public final class MGRHAType implements HAType {
if (connection.getMetaData().getURL().contains(primaryDataSourceURL)) {
return entry.getKey();
}
- // CHECKSTYLE:OFF
- } catch (final Exception ex) {
- // CHECKSTYLE:ON
+ } catch (final SQLException ex) {
+ log.error("An exception occurred while find primary data source name", ex);
}
}
return result;
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/resources/META-INF/services/org.apache.shardingsphere.ha.spi.HAType b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-mgr/src/main/resources/META-INF/services/org.apache.shardingsphere.ha.spi.HAType
similarity index 94%
rename from shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/resources/META-INF/services/org.apache.shardingsphere.ha.spi.HAType
rename to shardingsphere-features/shardingsphere-ha/shardingsphere-ha-mgr/src/main/resources/META-INF/services/org.apache.shardingsphere.ha.spi.HAType
index 5e204d4..9d6b034 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/resources/META-INF/services/org.apache.shardingsphere.ha.spi.HAType
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-mgr/src/main/resources/META-INF/services/org.apache.shardingsphere.ha.spi.HAType
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.ha.algorithm.MGRHAType
+org.apache.shardingsphere.ha.mgr.MGRHAType