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 2e8d80e #7318, code refactor (#8923)
2e8d80e is described below
commit 2e8d80e44a34be869572e7c50cac3b0c26af73f2
Author: Zhang Yonglun <[email protected]>
AuthorDate: Thu Jan 7 10:23:21 2021 +0800
#7318, code refactor (#8923)
---
.../org/apache/shardingsphere/ha/spi/HAType.java | 12 +++++-----
.../ha/fixture/TestHATypeFixture.java | 4 ++--
.../apache/shardingsphere/ha/mgr/MGRHAType.java | 26 ++++++++++++----------
.../shardingsphere/ha/mgr/MGRPeriodicalJob.java | 8 +++----
.../ha/route/fixture/TestRouteHATypeFixture.java | 4 ++--
5 files changed, 28 insertions(+), 26 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-api/src/main/java/org/apache/shardingsphere/ha/spi/HAType.java
b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-api/src/main/java/org/apache/shardingsphere/ha/spi/HAType.java
index c5f6c61..9ebf43c 100644
---
a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-api/src/main/java/org/apache/shardingsphere/ha/spi/HAType.java
+++
b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-api/src/main/java/org/apache/shardingsphere/ha/spi/HAType.java
@@ -41,29 +41,29 @@ public interface HAType extends TypedSPI {
/**
* Update primary data source.
*
- * @param originalDataSourceMap original data source map
+ * @param dataSourceMap data source map
* @param schemaName schema name
* @param disabledDataSourceNames disabled data source names
*/
- void updatePrimaryDataSource(Map<String, DataSource>
originalDataSourceMap, String schemaName, Collection<String>
disabledDataSourceNames);
+ void updatePrimaryDataSource(Map<String, DataSource> dataSourceMap, String
schemaName, Collection<String> disabledDataSourceNames);
/**
* Update member state.
*
- * @param originalDataSourceMap original data source map
+ * @param dataSourceMap data source map
* @param schemaName schema name
* @param disabledDataSourceNames disabled data source names
*/
- void updateMemberState(Map<String, DataSource> originalDataSourceMap,
String schemaName, Collection<String> disabledDataSourceNames);
+ void updateMemberState(Map<String, DataSource> dataSourceMap, String
schemaName, Collection<String> disabledDataSourceNames);
/**
* Start periodical update.
*
- * @param originalDataSourceMap original data source map
+ * @param dataSourceMap data source map
* @param schemaName schema name
* @param disabledDataSourceNames disabled data source names
*/
- void startPeriodicalUpdate(Map<String, DataSource> originalDataSourceMap,
String schemaName, Collection<String> disabledDataSourceNames);
+ void startPeriodicalUpdate(Map<String, DataSource> dataSourceMap, String
schemaName, Collection<String> disabledDataSourceNames);
/**
* Stop periodical update.
diff --git
a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/fixture/TestHATypeFixture.java
b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/fixture/TestHATypeFixture.java
index 1641fe8..d545494 100644
---
a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/fixture/TestHATypeFixture.java
+++
b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/fixture/TestHATypeFixture.java
@@ -37,11 +37,11 @@ public final class TestHATypeFixture implements HAType {
}
@Override
- public void updateMemberState(final Map<String, DataSource>
originalDataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
+ public void updateMemberState(final Map<String, DataSource> dataSourceMap,
final String schemaName, final Collection<String> disabledDataSourceNames) {
}
@Override
- public void startPeriodicalUpdate(final Map<String, DataSource>
originalDataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
+ public void startPeriodicalUpdate(final Map<String, DataSource>
dataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
}
@Override
diff --git
a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java
b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java
index b1a2aeb..fdb5bdc 100644
---
a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java
+++
b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java
@@ -123,8 +123,8 @@ public final class MGRHAType implements HAType {
}
@Override
- public void updatePrimaryDataSource(final Map<String, DataSource>
originalDataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
- Map<String, DataSource> activeDataSourceMap = new
HashMap<>(originalDataSourceMap);
+ public void updatePrimaryDataSource(final Map<String, DataSource>
dataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
+ Map<String, DataSource> activeDataSourceMap = new
HashMap<>(dataSourceMap);
if (!disabledDataSourceNames.isEmpty()) {
activeDataSourceMap.entrySet().removeIf(each ->
disabledDataSourceNames.contains(each.getKey()));
}
@@ -165,8 +165,10 @@ public final class MGRHAType implements HAType {
private String findPrimaryDataSourceName(final String
primaryDataSourceURL, final Map<String, DataSource> dataSourceMap) {
String result = "";
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+ String url;
try (Connection connection = entry.getValue().getConnection()) {
- if
(connection.getMetaData().getURL().contains(primaryDataSourceURL)) {
+ url = connection.getMetaData().getURL();
+ if (null != url && url.contains(primaryDataSourceURL)) {
return entry.getKey();
}
} catch (final SQLException ex) {
@@ -177,8 +179,8 @@ public final class MGRHAType implements HAType {
}
@Override
- public void updateMemberState(final Map<String, DataSource>
originalDataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
- Map<String, DataSource> activeDataSourceMap = new
HashMap<>(originalDataSourceMap);
+ public void updateMemberState(final Map<String, DataSource> dataSourceMap,
final String schemaName, final Collection<String> disabledDataSourceNames) {
+ Map<String, DataSource> activeDataSourceMap = new
HashMap<>(dataSourceMap);
if (!disabledDataSourceNames.isEmpty()) {
activeDataSourceMap.entrySet().removeIf(each ->
disabledDataSourceNames.contains(each.getKey()));
}
@@ -188,7 +190,7 @@ public final class MGRHAType implements HAType {
}
Map<String, String> dataSourceURLs = new HashMap<>(16, 1);
determineDisabledDataSource(schemaName, activeDataSourceMap,
memberDataSourceURLs, dataSourceURLs);
- determineEnabledDataSource(originalDataSourceMap, schemaName,
memberDataSourceURLs, dataSourceURLs);
+ determineEnabledDataSource(dataSourceMap, schemaName,
memberDataSourceURLs, dataSourceURLs);
}
private List<String> findMemberDataSourceURLs(final Map<String,
DataSource> activeDataSourceMap) {
@@ -212,11 +214,11 @@ public final class MGRHAType implements HAType {
final List<String>
memberDataSourceURLs, final Map<String, String> dataSourceURLs) {
for (Entry<String, DataSource> entry : activeDataSourceMap.entrySet())
{
boolean disable = true;
- String url = "";
+ String url = null;
try (Connection connection = entry.getValue().getConnection()) {
url = connection.getMetaData().getURL();
for (String each : memberDataSourceURLs) {
- if (url.contains(each)) {
+ if (null != url && url.contains(each)) {
disable = false;
break;
}
@@ -232,7 +234,7 @@ public final class MGRHAType implements HAType {
}
}
- private void determineEnabledDataSource(final Map<String, DataSource>
originalDataSourceMap, final String schemaName,
+ private void determineEnabledDataSource(final Map<String, DataSource>
dataSourceMap, final String schemaName,
final List<String>
memberDataSourceURLs, final Map<String, String> dataSourceURLs) {
for (String each : memberDataSourceURLs) {
boolean enable = true;
@@ -245,7 +247,7 @@ public final class MGRHAType implements HAType {
if (!enable) {
continue;
}
- for (Entry<String, DataSource> entry :
originalDataSourceMap.entrySet()) {
+ for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
String url;
try (Connection connection = entry.getValue().getConnection())
{
url = connection.getMetaData().getURL();
@@ -261,13 +263,13 @@ public final class MGRHAType implements HAType {
}
@Override
- public void startPeriodicalUpdate(final Map<String, DataSource>
originalDataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
+ public void startPeriodicalUpdate(final Map<String, DataSource>
dataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
if (null == coordinatorRegistryCenter) {
ZookeeperConfiguration zkConfig = new
ZookeeperConfiguration(props.getProperty("zkServerLists"), "mgr-elasticjob");
coordinatorRegistryCenter = new ZookeeperRegistryCenter(zkConfig);
coordinatorRegistryCenter.init();
}
- scheduleJobBootstrap = new
ScheduleJobBootstrap(coordinatorRegistryCenter, new MGRPeriodicalJob(this,
originalDataSourceMap, schemaName, disabledDataSourceNames),
+ scheduleJobBootstrap = new
ScheduleJobBootstrap(coordinatorRegistryCenter, new MGRPeriodicalJob(this,
dataSourceMap, schemaName, disabledDataSourceNames),
JobConfiguration.newBuilder("MGRPeriodicalJob",
1).cron(props.getProperty("keepAliveCron")).build());
scheduleJobBootstrap.schedule();
}
diff --git
a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRPeriodicalJob.java
b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRPeriodicalJob.java
index f3cee18..701e4b4 100644
---
a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRPeriodicalJob.java
+++
b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRPeriodicalJob.java
@@ -34,7 +34,7 @@ public final class MGRPeriodicalJob implements SimpleJob {
private final HAType haType;
- private final Map<String, DataSource> originalDataSourceMap;
+ private final Map<String, DataSource> dataSourceMap;
private final String schemaName;
@@ -42,12 +42,12 @@ public final class MGRPeriodicalJob implements SimpleJob {
@Override
public void execute(final ShardingContext shardingContext) {
- Map<String, DataSource> activeDataSourceMap = new
HashMap<>(originalDataSourceMap);
+ Map<String, DataSource> activeDataSourceMap = new
HashMap<>(dataSourceMap);
if (!disabledDataSourceNames.isEmpty()) {
activeDataSourceMap.entrySet().removeIf(each ->
disabledDataSourceNames.contains(each.getKey()));
}
log.info(" +++ " + activeDataSourceMap.toString());
- haType.updatePrimaryDataSource(originalDataSourceMap, schemaName,
disabledDataSourceNames);
- haType.updateMemberState(originalDataSourceMap, schemaName,
disabledDataSourceNames);
+ haType.updatePrimaryDataSource(dataSourceMap, schemaName,
disabledDataSourceNames);
+ haType.updateMemberState(dataSourceMap, schemaName,
disabledDataSourceNames);
}
}
diff --git
a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/test/java/org/apache/shardingsphere/ha/route/fixture/TestRouteHATypeFixture.java
b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/test/java/org/apache/shardingsphere/ha/route/fixture/TestRouteHATypeFixture.java
index b69a196..7a28f50 100644
---
a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/test/java/org/apache/shardingsphere/ha/route/fixture/TestRouteHATypeFixture.java
+++
b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/test/java/org/apache/shardingsphere/ha/route/fixture/TestRouteHATypeFixture.java
@@ -37,11 +37,11 @@ public final class TestRouteHATypeFixture implements HAType
{
}
@Override
- public void updateMemberState(final Map<String, DataSource>
originalDataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
+ public void updateMemberState(final Map<String, DataSource> dataSourceMap,
final String schemaName, final Collection<String> disabledDataSourceNames) {
}
@Override
- public void startPeriodicalUpdate(final Map<String, DataSource>
originalDataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
+ public void startPeriodicalUpdate(final Map<String, DataSource>
dataSourceMap, final String schemaName, final Collection<String>
disabledDataSourceNames) {
}
@Override