This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 15880ec Add SHOW_SLAVE_STATUS check configuration. (#16263)
15880ec is described below
commit 15880ece98257a2019847c0ff0cd9dde3d8f721c
Author: zhaojinchao <[email protected]>
AuthorDate: Tue Mar 22 16:41:08 2022 +0800
Add SHOW_SLAVE_STATUS check configuration. (#16263)
* Add check show slave status configuration.
* Add show slave status check config unit test.
* Fixed.
* Adjust desc
---
.../type/ShowSlaveStatusDatabaseDiscoveryType.java | 29 +++++++++++++++++++---
.../ShowSlaveStatusDatabaseDiscoveryTypeTest.java | 8 ++++++
2 files changed, 34 insertions(+), 3 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryType.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryType.java
index a44b449..359731e 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryType.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryType.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.dbdiscovery.mysql.type;
+import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@@ -32,6 +33,8 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
@@ -49,15 +52,35 @@ public final class ShowSlaveStatusDatabaseDiscoveryType
extends AbstractDatabase
private Properties props = new Properties();
@Override
- public void checkDatabaseDiscoveryConfiguration(final String schemaName,
final Map<String, DataSource> dataSourceMap) {
- //TODO Check master-slave mode
+ public void checkDatabaseDiscoveryConfiguration(final String schemaName,
final Map<String, DataSource> dataSourceMap) throws SQLException {
+ Collection<String> result = getPrimaryDataSourceURLS(dataSourceMap);
+ Preconditions.checkState(!result.isEmpty(), "Not found primary data
source for schemaName `%s`", schemaName);
+ Preconditions.checkState(1 == result.size(), "More than one primary
data source for schemaName `%s`", schemaName);
+ }
+
+ private Collection<String> getPrimaryDataSourceURLS(final Map<String,
DataSource> dataSourceMap) throws SQLException {
+ Collection<String> result = new ArrayList<>();
+ for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+ try (Connection connection = entry.getValue().getConnection();
+ Statement statement = connection.createStatement()) {
+ String url = getPrimaryDataSourceURL(statement);
+ if (!url.isEmpty() && !result.contains(url)) {
+ result.add(url);
+ }
+ }
+ }
+ return result;
}
@Override
protected String getPrimaryDataSourceURL(final Statement statement) throws
SQLException {
try (ResultSet resultSet = statement.executeQuery(SHOW_SLAVE_STATUS)) {
if (resultSet.next()) {
- return String.format("%s:%s",
resultSet.getString("Master_Host"), resultSet.getString("Master_Port"));
+ String masterHost = resultSet.getString("Master_Host");
+ String masterPort = resultSet.getString("Master_Port");
+ if (null != masterHost && null != masterPort) {
+ return String.format("%s:%s", masterHost, masterPort);
+ }
}
return "";
}
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryTypeTest.java
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryTypeTest.java
index db7c517..e539d8d 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryTypeTest.java
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryTypeTest.java
@@ -39,6 +39,14 @@ public final class ShowSlaveStatusDatabaseDiscoveryTypeTest {
private final ShowSlaveStatusDatabaseDiscoveryType
showSlaveStatusDatabaseDiscoveryType = new
ShowSlaveStatusDatabaseDiscoveryType();
@Test
+ public void assertCheckShowSlaveStatusConfig() throws SQLException {
+ Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
+ dataSourceMap.put("ds_0", getDataSource(false, 3306));
+ dataSourceMap.put("ds_1", getDataSource(true, 3307));
+
showSlaveStatusDatabaseDiscoveryType.checkDatabaseDiscoveryConfiguration("discovery_db",
dataSourceMap);
+ }
+
+ @Test
public void assertUpdatePrimaryDataSource() throws SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
dataSourceMap.put("ds_0", getDataSource(false, 3306));