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 f470bb1 Add check discovery rules and remove other heartbeat job.
(#14070)
f470bb1 is described below
commit f470bb1a8fd9f291fa7e85c3a86fd37dc39f8773
Author: zhaojinchao <[email protected]>
AuthorDate: Tue Dec 14 10:44:37 2021 +0800
Add check discovery rules and remove other heartbeat job. (#14070)
* add check discovery rules and remove other heartbeat job.
* update rename param
---
.../DatabaseDiscoveryHeartBeatConfiguration.java | 2 +-
.../dbdiscovery/spi/DatabaseDiscoveryType.java | 19 ------
.../rule/DatabaseDiscoveryDataSourceRule.java | 9 ---
.../dbdiscovery/rule/DatabaseDiscoveryRule.java | 20 +++---
.../fixture/TestDatabaseDiscoveryType.java | 10 ---
.../route/DatabaseDiscoverySQLRouterTest.java | 4 +-
.../rule/DatabaseDiscoveryDataSourceRuleTest.java | 13 ++--
.../rule/DatabaseDiscoveryRuleTest.java | 4 +-
.../builder/DatabaseDiscoveryRuleBuilderTest.java | 4 +-
...AlterDatabaseDiscoveryRuleStatementUpdater.java | 11 +---
.../dbdiscovery/mgr/MGRDatabaseDiscoveryType.java | 39 ------------
.../dbdiscovery/mgr/MGRHeartbeatJob.java | 50 ---------------
.../mgr/MGRDatabaseDiscoveryTypeTest.java | 72 ----------------------
.../opengauss/OpenGaussDatabaseDiscoveryType.java | 58 ++---------------
.../opengauss/OpenGaussHeartbeatJob.java | 49 ---------------
.../OpenGaussDatabaseDiscoveryTypeTest.java | 55 +++++++----------
.../cluster/ClusterContextManagerBuilder.java | 4 +-
.../distsql/fixture/TestDatabaseDiscoveryType.java | 10 ---
.../resources/conf/config-database-discovery.yaml | 18 ++++--
19 files changed, 70 insertions(+), 381 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/api/config/rule/DatabaseDiscoveryHeartBeatConfiguration.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/api/config/rule/DatabaseDiscoveryHeartBeatConfiguration.java
index 420c34a..11225b8 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/api/config/rule/DatabaseDiscoveryHeartBeatConfiguration.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/api/config/rule/DatabaseDiscoveryHeartBeatConfiguration.java
@@ -23,7 +23,7 @@ import lombok.RequiredArgsConstructor;
import java.util.Properties;
/**
- * Database discovery heartbeat configuration.
+ * Database discovery heartbeat config.
*/
@RequiredArgsConstructor
@Getter
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java
index 08b67af..4eb65d7 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java
@@ -23,7 +23,6 @@ import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;
-import java.util.Properties;
/**
* Database discovery type.
@@ -59,27 +58,9 @@ public interface DatabaseDiscoveryType extends
ShardingSphereAlgorithm {
void updateMemberState(String schemaName, Map<String, DataSource>
dataSourceMap, Collection<String> disabledDataSourceNames);
/**
- * Start periodical update.
- *
- * @param dataSourceMap data source map
- * @param schemaName schema name
- * @param disabledDataSourceNames disabled data source names
- * @param groupName group name
- */
- void startPeriodicalUpdate(String schemaName, Map<String, DataSource>
dataSourceMap, Collection<String> disabledDataSourceNames, String groupName);
-
- /**
* Get primary data source.
*
* @return primary data source
*/
String getPrimaryDataSource();
-
- /**
- * Update properties.
- *
- * @param groupName group name
- * @param props properties
- */
- void updateProperties(String groupName, Properties props);
}
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRule.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRule.java
index ef4eaed..340a4862 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRule.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRule.java
@@ -50,15 +50,6 @@ public final class DatabaseDiscoveryDataSourceRule {
private String primaryDataSourceName;
- //TODO remove this when job finished.
- public DatabaseDiscoveryDataSourceRule(final
DatabaseDiscoveryDataSourceRuleConfiguration config, final
DatabaseDiscoveryType databaseDiscoveryType) {
- checkConfiguration(config);
- name = config.getName();
- dataSourceNames = config.getDataSourceNames();
- this.heartbeatProps = new Properties();
- this.databaseDiscoveryType = databaseDiscoveryType;
- }
-
public DatabaseDiscoveryDataSourceRule(final
DatabaseDiscoveryDataSourceRuleConfiguration config, final Properties props,
final DatabaseDiscoveryType databaseDiscoveryType) {
checkConfiguration(config);
name = config.getName();
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
index 6585f77..1f3b1c3 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
@@ -50,7 +50,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;
-import java.util.Properties;
import java.util.Optional;
import java.util.Map.Entry;
@@ -83,7 +82,7 @@ public final class DatabaseDiscoveryRule implements
SchemaRule, DataSourceContai
checkDataSourcesArguments(dataSourceMap, dataSourceRuleConfigs);
this.discoveryTypes = discoveryTypes;
dataSourceRules = getDataSourceRules(dataSourceRuleConfigs,
heartBeatConfig);
- startMonitor(schemaName, dataSourceMap);
+ findMasterSlaveRelation(schemaName, dataSourceMap);
initAware();
}
@@ -101,24 +100,25 @@ public final class DatabaseDiscoveryRule implements
SchemaRule, DataSourceContai
}
private Map<String, DatabaseDiscoveryDataSourceRule>
getDataSourceRules(final
Collection<DatabaseDiscoveryDataSourceRuleConfiguration> dataSources,
-
final Map<String, DatabaseDiscoveryHeartBeatConfiguration> heartBeatConfig) {
+
final Map<String, DatabaseDiscoveryHeartBeatConfiguration> heartbeatConfig) {
Map<String, DatabaseDiscoveryDataSourceRule> result = new
HashMap<>(dataSources.size(), 1);
for (DatabaseDiscoveryDataSourceRuleConfiguration each : dataSources) {
- checkDatabaseDiscoveryDataSourceRuleConfigurationArguments(each);
- //TODO heartBeatConfig not empty when job finished.
- result.put(each.getName(), new
DatabaseDiscoveryDataSourceRule(each, null ==
heartBeatConfig.get(each.getDiscoveryHeartbeatName())
- ? new Properties() :
heartBeatConfig.get(each.getDiscoveryHeartbeatName()).getProps(),
discoveryTypes.get(each.getDiscoveryTypeName())));
+ checkDatabaseDiscoveryDataSourceRuleConfigurationArguments(each,
heartbeatConfig);
+ result.put(each.getName(), new
DatabaseDiscoveryDataSourceRule(each,
heartbeatConfig.get(each.getDiscoveryHeartbeatName()).getProps(),
discoveryTypes.get(each.getDiscoveryTypeName())));
}
return result;
}
- private void
checkDatabaseDiscoveryDataSourceRuleConfigurationArguments(final
DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig) {
+ private void
checkDatabaseDiscoveryDataSourceRuleConfigurationArguments(final
DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig,
+
final Map<String, DatabaseDiscoveryHeartBeatConfiguration> heartbeatConfig) {
+
Preconditions.checkNotNull(dataSourceRuleConfig.getDiscoveryHeartbeatName(),
"Discovery heartbeat cannot be null of rule name `%s`.",
dataSourceRuleConfig.getName());
+
Preconditions.checkArgument(heartbeatConfig.containsKey(dataSourceRuleConfig.getDiscoveryHeartbeatName()),
+ "Can not find discovery heartbeat of rule name `%s`.",
dataSourceRuleConfig.getName());
Preconditions.checkNotNull(dataSourceRuleConfig.getDiscoveryTypeName(),
"Discovery type cannot be null of rule name `%s`.",
dataSourceRuleConfig.getName());
Preconditions.checkArgument(discoveryTypes.containsKey(dataSourceRuleConfig.getDiscoveryTypeName()),
"Can not find discovery type of rule name `%s`.",
dataSourceRuleConfig.getName());
}
- //TODO remove startPeriodicalUpdate method and change startMonitor method
name when job finished.
- private void startMonitor(final String schemaName, final Map<String,
DataSource> dataSourceMap) {
+ private void findMasterSlaveRelation(final String schemaName, final
Map<String, DataSource> dataSourceMap) {
for (Entry<String, DatabaseDiscoveryDataSourceRule> entry :
dataSourceRules.entrySet()) {
String groupName = entry.getKey();
DatabaseDiscoveryDataSourceRule dataSourceRule = entry.getValue();
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/TestDatabaseDiscoveryType.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/TestDatabaseDiscoveryType.java
index b3dc6ed..7c8b997 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/TestDatabaseDiscoveryType.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/TestDatabaseDiscoveryType.java
@@ -22,7 +22,6 @@ import
org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
import javax.sql.DataSource;
import java.util.Collection;
import java.util.Map;
-import java.util.Properties;
public final class TestDatabaseDiscoveryType implements DatabaseDiscoveryType {
@@ -39,20 +38,11 @@ public final class TestDatabaseDiscoveryType implements
DatabaseDiscoveryType {
}
@Override
- public void startPeriodicalUpdate(final String schemaName, final
Map<String, DataSource> dataSourceMap, final Collection<String>
disabledDataSourceNames, final String groupName) {
- }
-
- @Override
public String getPrimaryDataSource() {
return "primary";
}
@Override
- public void updateProperties(final String groupName, final Properties
props) {
-
- }
-
- @Override
public String getType() {
return "TEST";
}
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java
index 492b7ee..6d95577 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.dbdiscovery.route;
import
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
+import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
import org.apache.shardingsphere.dbdiscovery.rule.DatabaseDiscoveryRule;
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
@@ -76,7 +77,8 @@ public final class DatabaseDiscoverySQLRouterTest {
DatabaseDiscoveryDataSourceRuleConfiguration dataSourceConfig
= new
DatabaseDiscoveryDataSourceRuleConfiguration(DATA_SOURCE_NAME,
Collections.singletonList(PRIMARY_DATA_SOURCE), "ha_heartbeat", "TEST");
ShardingSphereAlgorithmConfiguration algorithmConfig = new
ShardingSphereAlgorithmConfiguration("TEST", new Properties());
- DatabaseDiscoveryRuleConfiguration config = new
DatabaseDiscoveryRuleConfiguration(Collections.singleton(dataSourceConfig),
Collections.emptyMap(),
+ DatabaseDiscoveryRuleConfiguration config = new
DatabaseDiscoveryRuleConfiguration(Collections.singleton(dataSourceConfig),
+ Collections.singletonMap("ha_heartbeat", new
DatabaseDiscoveryHeartBeatConfiguration(new Properties())),
Collections.singletonMap("TEST", algorithmConfig));
rule = new DatabaseDiscoveryRule("TEST",
Collections.singletonMap("ds", mock(DataSource.class)), config);
sqlRouter = (DatabaseDiscoverySQLRouter)
OrderedSPIRegistry.getRegisteredServices(SQLRouter.class,
Collections.singleton(rule)).get(rule);
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRuleTest.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRuleTest.java
index 5407a76..7ff1576 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRuleTest.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRuleTest.java
@@ -26,6 +26,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
+import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -33,21 +34,25 @@ import static org.junit.Assert.assertThat;
public final class DatabaseDiscoveryDataSourceRuleTest {
private final DatabaseDiscoveryDataSourceRule
databaseDiscoveryDataSourceRule = new DatabaseDiscoveryDataSourceRule(
- new DatabaseDiscoveryDataSourceRuleConfiguration("test_pr",
Arrays.asList("ds_0", "ds_1"), "ha_heartbeat", "discoveryTypeName"), new
MGRDatabaseDiscoveryType());
+ new DatabaseDiscoveryDataSourceRuleConfiguration("test_pr",
Arrays.asList("ds_0", "ds_1"), "ha_heartbeat", "discoveryTypeName"), new
Properties(),
+ new MGRDatabaseDiscoveryType());
@Test(expected = IllegalArgumentException.class)
public void assertNewHADataSourceRuleWithoutName() {
- new DatabaseDiscoveryDataSourceRule(new
DatabaseDiscoveryDataSourceRuleConfiguration("", Arrays.asList("ds_0", "ds_1"),
"ha_heartbeat", "discoveryTypeName"), new MGRDatabaseDiscoveryType());
+ new DatabaseDiscoveryDataSourceRule(new
DatabaseDiscoveryDataSourceRuleConfiguration("", Arrays.asList("ds_0", "ds_1"),
"ha_heartbeat", "discoveryTypeName"),
+ new Properties(), new MGRDatabaseDiscoveryType());
}
@Test(expected = IllegalArgumentException.class)
public void assertNewHADataSourceRuleWithNullDataSourceName() {
- new DatabaseDiscoveryDataSourceRule(new
DatabaseDiscoveryDataSourceRuleConfiguration("ds", null, "ha_heartbeat",
"discoveryTypeName"), new MGRDatabaseDiscoveryType());
+ new DatabaseDiscoveryDataSourceRule(new
DatabaseDiscoveryDataSourceRuleConfiguration("ds", null, "ha_heartbeat",
"discoveryTypeName"),
+ new Properties(), new MGRDatabaseDiscoveryType());
}
@Test(expected = IllegalArgumentException.class)
public void assertNewHADataSourceRuleWithEmptyDataSourceName() {
- new DatabaseDiscoveryDataSourceRule(new
DatabaseDiscoveryDataSourceRuleConfiguration("ds", Collections.emptyList(),
"ha_heartbeat", "discoveryTypeName"), new MGRDatabaseDiscoveryType());
+ new DatabaseDiscoveryDataSourceRule(new
DatabaseDiscoveryDataSourceRuleConfiguration("ds", Collections.emptyList(),
"ha_heartbeat", "discoveryTypeName"),
+ new Properties(), new MGRDatabaseDiscoveryType());
}
@Test
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
index f712154..b6c4646 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.dbdiscovery.rule;
import com.google.common.collect.ImmutableMap;
import
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
+import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import
org.apache.shardingsphere.infra.rule.event.impl.DataSourceNameDisabledEvent;
import org.junit.Test;
@@ -103,6 +104,7 @@ public final class DatabaseDiscoveryRuleTest {
private DatabaseDiscoveryRule createRule() {
DatabaseDiscoveryDataSourceRuleConfiguration config = new
DatabaseDiscoveryDataSourceRuleConfiguration("test_pr", Arrays.asList("ds_0",
"ds_1"), "ha_heartbeat", "TEST");
return new DatabaseDiscoveryRule("ha_db", dataSourceMap, new
DatabaseDiscoveryRuleConfiguration(
- Collections.singleton(config), Collections.emptyMap(),
ImmutableMap.of("TEST", new ShardingSphereAlgorithmConfiguration("TEST", new
Properties()))));
+ Collections.singleton(config),
Collections.singletonMap("ha_heartbeat", new
DatabaseDiscoveryHeartBeatConfiguration(new Properties())),
+ ImmutableMap.of("TEST", new
ShardingSphereAlgorithmConfiguration("TEST", new Properties()))));
}
}
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilderTest.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilderTest.java
index cd0960c..117d12a 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilderTest.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilderTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.dbdiscovery.rule.builder;
import
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
+import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
import org.apache.shardingsphere.dbdiscovery.rule.DatabaseDiscoveryRule;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
@@ -51,7 +52,8 @@ public final class DatabaseDiscoveryRuleBuilderTest {
DatabaseDiscoveryDataSourceRuleConfiguration dataSourceConfig =
new DatabaseDiscoveryDataSourceRuleConfiguration("name",
Collections.singletonList("name"), "ha_heartbeat", "TEST");
DatabaseDiscoveryRuleConfiguration config = new
DatabaseDiscoveryRuleConfiguration(
- Collections.singleton(dataSourceConfig),
Collections.emptyMap(), Collections.singletonMap("TEST", new
ShardingSphereAlgorithmConfiguration("TEST", new Properties())));
+ Collections.singleton(dataSourceConfig),
Collections.singletonMap("ha_heartbeat", new
DatabaseDiscoveryHeartBeatConfiguration(new Properties())),
+ Collections.singletonMap("TEST", new
ShardingSphereAlgorithmConfiguration("TEST", new Properties())));
SchemaRuleBuilder builder =
OrderedSPIRegistry.getRegisteredServices(SchemaRuleBuilder.class,
Collections.singletonList(config)).get(config);
Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
dataSourceMap.put("primaryDataSourceName", mock(DataSource.class));
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/AlterDatabaseDiscoveryRuleStatementUpdater.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/AlterDatabaseDis
[...]
index 792d4fa..8a4255f 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/AlterDatabaseDiscoveryRuleStatementUpdater.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/AlterDatabaseDiscoveryRuleStatementUpdater.java
@@ -27,7 +27,6 @@ import
org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDisc
import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.AlterDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
-import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import
org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmConfigurationException;
@@ -121,7 +120,7 @@ public final class
AlterDatabaseDiscoveryRuleStatementUpdater implements RuleDef
public void updateCurrentRuleConfiguration(final
DatabaseDiscoveryRuleConfiguration currentRuleConfig, final
DatabaseDiscoveryRuleConfiguration toBeAlteredRuleConfig) {
dropRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig);
addRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig);
- updateProperties(toBeAlteredRuleConfig);
+ //TODO DistSQL update rule config need to sync scheduler module cron.
}
private void dropRuleConfiguration(final
DatabaseDiscoveryRuleConfiguration currentRuleConfig, final
DatabaseDiscoveryRuleConfiguration toBeAlteredRuleConfig) {
@@ -145,14 +144,6 @@ public final class
AlterDatabaseDiscoveryRuleStatementUpdater implements RuleDef
currentRuleConfig.getDiscoveryHeartbeats().putAll(toBeAlteredRuleConfig.getDiscoveryHeartbeats());
}
- private void updateProperties(final DatabaseDiscoveryRuleConfiguration
ruleConfiguration) {
- ruleConfiguration.getDataSources().forEach(each -> {
- ShardingSphereAlgorithmConfiguration configuration =
ruleConfiguration.getDiscoveryTypes().get(each.getDiscoveryTypeName());
-
TypedSPIRegistry.findRegisteredService(DatabaseDiscoveryType.class,
configuration.getType(),
- new Properties()).ifPresent(databaseDiscoveryType ->
databaseDiscoveryType.updateProperties(each.getName(),
configuration.getProps()));
- });
- }
-
@Override
public Class<DatabaseDiscoveryRuleConfiguration>
getRuleConfigurationClass() {
return DatabaseDiscoveryRuleConfiguration.class;
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryType.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryType.java
index 948a8d8..55d0948 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryType.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryType.java
@@ -21,13 +21,6 @@ import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
-import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
-import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
-import
org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
-import
org.apache.shardingsphere.elasticjob.lite.lifecycle.internal.settings.JobConfigurationAPIImpl;
-import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
-import
org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
-import
org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;
@@ -62,10 +55,6 @@ public final class MGRDatabaseDiscoveryType implements
DatabaseDiscoveryType {
private static final String MEMBER_LIST = "SELECT MEMBER_HOST,
MEMBER_PORT, MEMBER_STATE FROM performance_schema.replication_group_members";
- private static CoordinatorRegistryCenter coordinatorRegistryCenter;
-
- private static final Map<String, ScheduleJobBootstrap>
SCHEDULE_JOB_BOOTSTRAP_MAP = new HashMap<>(16, 1);
-
private String oldPrimaryDataSource;
@Getter
@@ -265,39 +254,11 @@ public final class MGRDatabaseDiscoveryType implements
DatabaseDiscoveryType {
}
@Override
- public void startPeriodicalUpdate(final String schemaName, final
Map<String, DataSource> dataSourceMap, final Collection<String>
disabledDataSourceNames, final String groupName) {
- if (null == coordinatorRegistryCenter) {
- ZookeeperConfiguration zkConfig = new
ZookeeperConfiguration(props.getProperty("zkServerLists"), "mgr-elasticjob");
- coordinatorRegistryCenter = new ZookeeperRegistryCenter(zkConfig);
- coordinatorRegistryCenter.init();
- }
- if (null != SCHEDULE_JOB_BOOTSTRAP_MAP.get(groupName)) {
- SCHEDULE_JOB_BOOTSTRAP_MAP.get(groupName).shutdown();
- }
- SCHEDULE_JOB_BOOTSTRAP_MAP.put(groupName, new
ScheduleJobBootstrap(coordinatorRegistryCenter, new MGRHeartbeatJob(this,
schemaName, dataSourceMap, disabledDataSourceNames,
- groupName), JobConfiguration.newBuilder("MGR-" + groupName,
1).cron(props.getProperty("keepAliveCron")).build()));
- SCHEDULE_JOB_BOOTSTRAP_MAP.get(groupName).schedule();
- }
-
- @Override
public String getPrimaryDataSource() {
return oldPrimaryDataSource;
}
@Override
- public void updateProperties(final String groupName, final Properties
props) {
- new
JobConfigurationAPIImpl(coordinatorRegistryCenter).updateJobConfiguration(createJobConfiguration("MGR-"
+ groupName, props.getProperty("keepAliveCron")));
- }
-
- private JobConfigurationPOJO createJobConfiguration(final String jobName,
final String cron) {
- JobConfigurationPOJO result = new JobConfigurationPOJO();
- result.setJobName(jobName);
- result.setCron(cron);
- result.setShardingTotalCount(1);
- return result;
- }
-
- @Override
public String getType() {
return "MGR";
}
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRHeartbeatJob.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRHeartbeatJob.java
deleted file mode 100644
index 6979764..0000000
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRHeartbeatJob.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.dbdiscovery.mgr;
-
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
-import org.apache.shardingsphere.elasticjob.api.ShardingContext;
-import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
-
-import javax.sql.DataSource;
-import java.util.Collection;
-import java.util.Map;
-
-/**
- * MGR heartbeat job.
- */
-@RequiredArgsConstructor
-public final class MGRHeartbeatJob implements SimpleJob {
-
- private final DatabaseDiscoveryType databaseDiscoveryType;
-
- private final String schemaName;
-
- private final Map<String, DataSource> dataSourceMap;
-
- private final Collection<String> disabledDataSourceNames;
-
- private final String groupName;
-
- @Override
- public void execute(final ShardingContext shardingContext) {
- databaseDiscoveryType.updatePrimaryDataSource(schemaName,
dataSourceMap, disabledDataSourceNames, groupName);
- databaseDiscoveryType.updateMemberState(schemaName, dataSourceMap,
disabledDataSourceNames);
- }
-}
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
index 802cf74..693c651 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
@@ -18,24 +18,13 @@
package org.apache.shardingsphere.dbdiscovery.mgr;
import com.google.common.eventbus.EventBus;
-import lombok.SneakyThrows;
-import org.apache.curator.framework.CuratorFramework;
-import
org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
-import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
-import
org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
-import
org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;
-import org.apache.zookeeper.CreateMode;
-import org.hamcrest.Matchers;
-import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import javax.sql.DataSource;
import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
@@ -47,14 +36,11 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -167,62 +153,4 @@ public final class MGRDatabaseDiscoveryTypeTest {
mgrHaType.updateMemberState("discovery_db", dataSourceMap,
disabledDataSourceNames);
verify(eventBus).post(Mockito.refEq(new
DataSourceDisabledEvent("discovery_db", "ds_2", true)));
}
-
- @Test
- @Ignore
- public void assertStartPeriodicalUpdate() throws NoSuchFieldException,
IllegalAccessException {
- Properties props = mock(Properties.class);
- when(props.getProperty("zkServerLists")).thenReturn("127.0.0.1:2181");
- when(props.getProperty("keepAliveCron")).thenReturn("0/5 * * * * ?");
- Field propsFiled =
MGRDatabaseDiscoveryType.class.getDeclaredField("props");
- propsFiled.setAccessible(true);
- propsFiled.set(mgrHaType, props);
- final Map<String, ScheduleJobBootstrap> scheduleJobHashMap =
spy(HashMap.class);
- Field field =
MGRDatabaseDiscoveryType.class.getDeclaredField("SCHEDULE_JOB_BOOTSTRAP_MAP");
- makeAccessible(field);
- field.set(mgrHaType, scheduleJobHashMap);
- Map<String, DataSource> originalDataSourceMap = new HashMap<>(3, 1);
- mgrHaType.startPeriodicalUpdate("discovery_db", originalDataSourceMap,
null, "group_name");
- verify(scheduleJobHashMap, times(2)).get("group_name");
- assertThat(scheduleJobHashMap.get("group_name").getClass(),
Matchers.equalTo(ScheduleJobBootstrap.class));
- scheduleJobHashMap.get("group_name").shutdown();
- }
-
- @Test
- @Ignore
- public void assertUpdateProperties() throws Exception {
- Properties props = mock(Properties.class);
- when(props.getProperty("zkServerLists")).thenReturn("127.0.0.1:2181");
- when(props.getProperty("keepAliveCron")).thenReturn("0/5 * * * * ?");
- ZookeeperConfiguration zkConfig = new
ZookeeperConfiguration(props.getProperty("zkServerLists"), "");
- CoordinatorRegistryCenter coordinatorRegistryCenter = new
ZookeeperRegistryCenter(zkConfig);
- coordinatorRegistryCenter.init();
- ((CuratorFramework)
coordinatorRegistryCenter.getRawClient()).create().withMode(CreateMode.PERSISTENT).forPath("/MGR-group_name",
"123".getBytes("utf-8"));
- ((CuratorFramework)
coordinatorRegistryCenter.getRawClient()).create().withMode(CreateMode.PERSISTENT).forPath("/MGR-group_name/config",
"123".getBytes("utf-8"));
- mgrHaType.updateProperties("group_name", props);
- assertThat(coordinatorRegistryCenter.get("/MGR-group_name/config"),
is("cron: 0/5 * * * * ?\n" + "disabled: false\n"
- + "failover: false\n" + "jobName: MGR-group_name\n" +
"maxTimeDiffSeconds: -1\n" + "misfire: false\n"
- + "monitorExecution: false\n" + "overwrite: false\n" +
"reconcileIntervalMinutes: 0\n" + "shardingTotalCount: 1\n" + "staticSharding:
false\n"));
- }
-
- @SneakyThrows
- private void makeAccessible(final Field field) {
- field.setAccessible(true);
- Field modifiersField = getModifiersField();
- modifiersField.setAccessible(true);
- modifiersField.set(field, field.getModifiers() & ~Modifier.FINAL);
- }
-
- @SneakyThrows
- private Field getModifiersField() {
- Method getDeclaredFields0 =
Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
- getDeclaredFields0.setAccessible(true);
- Field[] fields = (Field[]) getDeclaredFields0.invoke(Field.class,
false);
- for (Field each : fields) {
- if ("modifiers".equals(each.getName())) {
- return each;
- }
- }
- throw new UnsupportedOperationException();
- }
}
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
index 6210a57..a6b0390 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
@@ -21,17 +21,10 @@ import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
-import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
-import
org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
-import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
-import
org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
-import
org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
-import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;
import
org.apache.shardingsphere.infra.rule.event.impl.PrimaryDataSourceChangedEvent;
-import
org.apache.shardingsphere.elasticjob.lite.lifecycle.internal.settings.JobConfigurationAPIImpl;
import javax.sql.DataSource;
import java.sql.Connection;
@@ -52,10 +45,6 @@ public final class OpenGaussDatabaseDiscoveryType implements
DatabaseDiscoveryTy
private static final String DB_ROLE = "select local_role,db_state from
pg_stat_get_stream_replications()";
- private static CoordinatorRegistryCenter coordinatorRegistryCenter;
-
- private static final Map<String, ScheduleJobBootstrap>
SCHEDULE_JOB_BOOTSTRAP_MAP = new HashMap<>(16, 1);
-
private String oldPrimaryDataSource;
@Getter
@@ -63,8 +52,7 @@ public final class OpenGaussDatabaseDiscoveryType implements
DatabaseDiscoveryTy
private Properties props = new Properties();
@Override
- public void checkDatabaseDiscoveryConfiguration(final String schemaName,
- final Map<String, DataSource> dataSourceMap) throws SQLException {
+ public void checkDatabaseDiscoveryConfiguration(final String schemaName,
final Map<String, DataSource> dataSourceMap) throws SQLException {
try (Connection connection =
dataSourceMap.get(oldPrimaryDataSource).getConnection();
Statement statement = connection.createStatement()) {
checkRolePrimary(statement);
@@ -82,8 +70,7 @@ public final class OpenGaussDatabaseDiscoveryType implements
DatabaseDiscoveryTy
}
@Override
- public void updatePrimaryDataSource(final String schemaName, final
Map<String, DataSource> dataSourceMap,
- final Collection<String> disabledDataSourceNames, final String
groupName) {
+ public void updatePrimaryDataSource(final String schemaName, final
Map<String, DataSource> dataSourceMap, final Collection<String>
disabledDataSourceNames, final String groupName) {
Map<String, DataSource> activeDataSourceMap = new
HashMap<>(dataSourceMap);
if (!disabledDataSourceNames.isEmpty()) {
activeDataSourceMap.entrySet().removeIf(each ->
disabledDataSourceNames.contains(each.getKey()));
@@ -94,8 +81,7 @@ public final class OpenGaussDatabaseDiscoveryType implements
DatabaseDiscoveryTy
}
if (!newPrimaryDataSource.equals(oldPrimaryDataSource)) {
oldPrimaryDataSource = newPrimaryDataSource;
- ShardingSphereEventBus.getInstance()
- .post(new PrimaryDataSourceChangedEvent(schemaName,
groupName, newPrimaryDataSource));
+ ShardingSphereEventBus.getInstance().post(new
PrimaryDataSourceChangedEvent(schemaName, groupName, newPrimaryDataSource));
}
}
@@ -106,8 +92,7 @@ public final class OpenGaussDatabaseDiscoveryType implements
DatabaseDiscoveryTy
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(DB_ROLE)) {
if (resultSet.next()) {
- if (resultSet.getString("local_role").equals("Primary")
- &&
resultSet.getString("db_state").equals("Normal")) {
+ if (resultSet.getString("local_role").equals("Primary") &&
resultSet.getString("db_state").equals("Normal")) {
return entry.getKey();
}
}
@@ -119,8 +104,7 @@ public final class OpenGaussDatabaseDiscoveryType
implements DatabaseDiscoveryTy
}
@Override
- public void updateMemberState(final String schemaName, final Map<String,
DataSource> dataSourceMap,
- final Collection<String> disabledDataSourceNames) {
+ public void updateMemberState(final String schemaName, final Map<String,
DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames) {
Map<String, DataSource> activeDataSourceMap = new
HashMap<>(dataSourceMap);
determineDisabledDataSource(schemaName, activeDataSourceMap);
}
@@ -144,43 +128,11 @@ public final class OpenGaussDatabaseDiscoveryType
implements DatabaseDiscoveryTy
}
@Override
- public void startPeriodicalUpdate(final String schemaName, final
Map<String, DataSource> dataSourceMap,
- final Collection<String> disabledDataSourceNames, final String
groupName) {
- if (null == coordinatorRegistryCenter) {
- ZookeeperConfiguration zkConfig = new
ZookeeperConfiguration(props.getProperty("zkServerLists"),
- "opengauss-elasticjob");
- coordinatorRegistryCenter = new ZookeeperRegistryCenter(zkConfig);
- coordinatorRegistryCenter.init();
- }
- if (null != SCHEDULE_JOB_BOOTSTRAP_MAP.get(groupName)) {
- SCHEDULE_JOB_BOOTSTRAP_MAP.get(groupName).shutdown();
- }
- SCHEDULE_JOB_BOOTSTRAP_MAP.put(groupName, new
ScheduleJobBootstrap(coordinatorRegistryCenter,
- new OpenGaussHeartbeatJob(this, schemaName, dataSourceMap,
disabledDataSourceNames, groupName),
- JobConfiguration.newBuilder("opengauss-" + groupName,
1).cron(props.getProperty("keepAliveCron")).build()));
- SCHEDULE_JOB_BOOTSTRAP_MAP.get(groupName).schedule();
- }
-
- @Override
public String getPrimaryDataSource() {
return oldPrimaryDataSource;
}
@Override
- public void updateProperties(final String groupName, final Properties
props) {
- new JobConfigurationAPIImpl(coordinatorRegistryCenter)
- .updateJobConfiguration(createJobConfiguration("opengauss-" +
groupName, props.getProperty("keepAliveCron")));
- }
-
- private JobConfigurationPOJO createJobConfiguration(final String jobName,
final String cron) {
- JobConfigurationPOJO result = new JobConfigurationPOJO();
- result.setJobName(jobName);
- result.setCron(cron);
- result.setShardingTotalCount(1);
- return result;
- }
-
- @Override
public String getType() {
return "openGauss";
}
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussHeartbeatJob.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussHeartbeatJob.java
deleted file mode 100644
index 472ef8d..0000000
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussHeartbeatJob.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.dbdiscovery.opengauss;
-
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
-import org.apache.shardingsphere.elasticjob.api.ShardingContext;
-import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
-import javax.sql.DataSource;
-import java.util.Collection;
-import java.util.Map;
-
-/**
- * OpenGauss heartbeat job.
- */
-@RequiredArgsConstructor
-public final class OpenGaussHeartbeatJob implements SimpleJob {
-
- private final DatabaseDiscoveryType databaseDiscoveryType;
-
- private final String schemaName;
-
- private final Map<String, DataSource> dataSourceMap;
-
- private final Collection<String> disabledDataSourceNames;
-
- private final String groupName;
-
- @Override
- public void execute(final ShardingContext shardingContext) {
- databaseDiscoveryType.updatePrimaryDataSource(schemaName,
dataSourceMap, disabledDataSourceNames, groupName);
- databaseDiscoveryType.updateMemberState(schemaName, dataSourceMap,
disabledDataSourceNames);
- }
-}
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
index b0475b0..e08126a 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.dbdiscovery.opengauss;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.junit.Test;
import javax.sql.DataSource;
@@ -46,35 +45,27 @@ public final class OpenGaussDatabaseDiscoveryTypeTest {
private final OpenGaussDatabaseDiscoveryType ogHaType = new
OpenGaussDatabaseDiscoveryType();
@Test
- public void assertCheckHAConfig() {
+ public void assertCheckHAConfig() throws SQLException {
DataSource dataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
Statement statement = mock(Statement.class);
ResultSet resultSet = mock(ResultSet.class);
- try {
- when(dataSource.getConnection()).thenReturn(connection);
- when(connection.createStatement()).thenReturn(statement);
- when(statement.executeQuery(DB_ROLE)).thenReturn(resultSet);
- when(statement.executeQuery(STANDBYS)).thenReturn(resultSet);
- when(resultSet.next()).thenReturn(true, false, true, false, true,
false, true, false);
- when(resultSet.getString("local_role")).thenReturn("Primary");
- when(resultSet.getString("db_state")).thenReturn("Normal");
- when(resultSet.getString("db_state")).thenReturn("Sync");
- } catch (final SQLException ex) {
- throw new ShardingSphereException(ex);
- }
+ when(dataSource.getConnection()).thenReturn(connection);
+ when(connection.createStatement()).thenReturn(statement);
+ when(statement.executeQuery(DB_ROLE)).thenReturn(resultSet);
+ when(statement.executeQuery(STANDBYS)).thenReturn(resultSet);
+ when(resultSet.next()).thenReturn(true, false, true, false, true,
false, true, false);
+ when(resultSet.getString("local_role")).thenReturn("Primary");
+ when(resultSet.getString("db_state")).thenReturn("Normal");
+ when(resultSet.getString("db_state")).thenReturn("Sync");
Map<String, DataSource> dataSourceMap = mock(HashMap.class);
when(dataSourceMap.get(null)).thenReturn(dataSource);
- try {
- ogHaType.getProps().setProperty("groupName", "group_name");
- ogHaType.checkDatabaseDiscoveryConfiguration("discovery_db",
dataSourceMap);
- } catch (final SQLException ex) {
- throw new ShardingSphereException(ex);
- }
+ ogHaType.getProps().setProperty("groupName", "group_name");
+ ogHaType.checkDatabaseDiscoveryConfiguration("discovery_db",
dataSourceMap);
}
@Test
- public void assertUpdatePrimaryDataSource() {
+ public void assertUpdatePrimaryDataSource() throws SQLException {
List<DataSource> dataSources = new LinkedList<>();
List<Connection> connections = new LinkedList<>();
List<Statement> statements = new LinkedList<>();
@@ -87,19 +78,15 @@ public final class OpenGaussDatabaseDiscoveryTypeTest {
resultSets.add(mock(ResultSet.class));
databaseMetaData.add(mock(DatabaseMetaData.class));
}
- try {
- for (int i = 0; i < 3; i++) {
-
when(dataSources.get(i).getConnection()).thenReturn(connections.get(i));
-
when(connections.get(i).createStatement()).thenReturn(statements.get(i));
-
when(statements.get(i).executeQuery(DB_ROLE)).thenReturn(resultSets.get(i));
- when(resultSets.get(i).next()).thenReturn(true, false);
-
when(resultSets.get(i).getString("local_role")).thenReturn("Primary");
-
when(resultSets.get(i).getString("db_state")).thenReturn("Normal");
-
when(connections.get(i).getMetaData()).thenReturn(databaseMetaData.get(i));
-
when(databaseMetaData.get(i).getURL()).thenReturn("jdbc:postgres://127.0.0.1:"
+ (3306 + i) + "/ds_0");
- }
- } catch (final SQLException ex) {
- throw new ShardingSphereException(ex);
+ for (int i = 0; i < 3; i++) {
+
when(dataSources.get(i).getConnection()).thenReturn(connections.get(i));
+
when(connections.get(i).createStatement()).thenReturn(statements.get(i));
+
when(statements.get(i).executeQuery(DB_ROLE)).thenReturn(resultSets.get(i));
+ when(resultSets.get(i).next()).thenReturn(true, false);
+
when(resultSets.get(i).getString("local_role")).thenReturn("Primary");
+ when(resultSets.get(i).getString("db_state")).thenReturn("Normal");
+
when(connections.get(i).getMetaData()).thenReturn(databaseMetaData.get(i));
+
when(databaseMetaData.get(i).getURL()).thenReturn("jdbc:postgres://127.0.0.1:"
+ (3306 + i) + "/ds_0");
}
Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1);
for (int i = 0; i < 3; i++) {
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index 6b4760c..fd4f987 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -252,8 +252,8 @@ public final class ClusterContextManagerBuilder implements
ContextManagerBuilder
}
private void startMonitor() {
- metaDataContexts.getAllSchemaNames().forEach(each ->
metaDataContexts.getMetaData(each).getRuleMetaData().getRules().stream().filter(each1
-> each1 instanceof SchedulerRule)
- .forEach(each1 -> startSchedules(each,
metaDataContexts.getMetaData(each).getResource().getDataSources(),
(SchedulerRule) each1)));
+ metaDataContexts.getAllSchemaNames().forEach(each ->
metaDataContexts.getMetaData(each).getRuleMetaData().getRules().stream().filter(rule
-> rule instanceof SchedulerRule)
+ .forEach(rule -> startSchedules(each,
metaDataContexts.getMetaData(each).getResource().getDataSources(),
(SchedulerRule) rule)));
}
private void startSchedules(final String schemaName, final Map<String,
DataSource> dataSources, final SchedulerRule schedulerRule) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/TestDatabaseDiscoveryType.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/TestDatabaseDiscoveryType.java
index ee47046..91c80f2 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/TestDatabaseDiscoveryType.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/TestDatabaseDiscoveryType.java
@@ -22,7 +22,6 @@ import
org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
import javax.sql.DataSource;
import java.util.Collection;
import java.util.Map;
-import java.util.Properties;
public final class TestDatabaseDiscoveryType implements DatabaseDiscoveryType {
@@ -40,20 +39,11 @@ public final class TestDatabaseDiscoveryType implements
DatabaseDiscoveryType {
}
@Override
- public void startPeriodicalUpdate(final String schemaName, final
Map<String, DataSource> dataSourceMap, final Collection<String>
disabledDataSourceNames, final String groupName) {
- }
-
- @Override
public String getPrimaryDataSource() {
return null;
}
@Override
- public void updateProperties(final String groupName, final Properties
props) {
-
- }
-
- @Override
public String getType() {
return "TEST";
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-database-discovery.yaml
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-database-discovery.yaml
index 6ed7371..1672d8e 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-database-discovery.yaml
+++
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-database-discovery.yaml
@@ -56,19 +56,22 @@
#rules:
#- !DB_DISCOVERY
# dataSources:
-# pr_ds:
+# primary_replica_ds:
# dataSourceNames:
# - ds_0
# - ds_1
# - ds_2
+# discoveryHeartbeatName: mgr-heartbeat
# discoveryTypeName: mgr
+# discoveryHeartbeats:
+# mgr-heartbeat:
+# props:
+# keepAliveCron: '0/5 * * * * ?'
# discoveryTypes:
# mgr:
# type: MGR
# props:
# groupName: 92504d5b-6dec-11e8-91ea-246e9612aaf1
-# zkServerLists: 'localhost:2181'
-# keepAliveCron: '0/5 * * * * ?'
######################################################################################################
#
@@ -110,16 +113,19 @@
#rules:
#- !DB_DISCOVERY
# dataSources:
-# pr_ds:
+# primary_replica_ds:
# dataSourceNames:
# - ds_0
# - ds_1
# - ds_2
+# discoveryHeartbeatName: mgr-heartbeat
# discoveryTypeName: mgr
+# discoveryHeartbeats:
+# mgr-heartbeat:
+# props:
+# keepAliveCron: '0/5 * * * * ?'
# discoveryTypes:
# mgr:
# type: MGR
# props:
# groupName: 92504d5b-6dec-11e8-91ea-246e9612aaf1
-# zkServerLists: 'localhost:2181'
-# keepAliveCron: '0/5 * * * * ?'