This is an automated email from the ASF dual-hosted git repository.

wuweijie 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 5d4f541  Performance minor enhance of scaling data source checker 
(#11516)
5d4f541 is described below

commit 5d4f541c71e14384e13c8b2fa7842286550b93f0
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Jul 26 23:03:17 2021 +0800

    Performance minor enhance of scaling data source checker (#11516)
---
 .../check/source/AbstractDataSourceChecker.java    | 12 +++----
 .../job/check/AbstractDataSourceCheckerTest.java   |  2 +-
 .../component/checker/MySQLDataSourceChecker.java  | 25 ++++++-------
 .../component/MySQLDataSourceCheckerTest.java      | 41 +++++++++-------------
 .../checker/PostgreSQLDataSourceChecker.java       | 29 +++++++++------
 5 files changed, 54 insertions(+), 55 deletions(-)

diff --git 
a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/job/check/source/AbstractDataSourceChecker.java
 
b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/job/check/source/AbstractDataSourceChecker.java
index 47d15a4..8b64263 100644
--- 
a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/job/check/source/AbstractDataSourceChecker.java
+++ 
b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/job/check/source/AbstractDataSourceChecker.java
@@ -38,31 +38,31 @@ public abstract class AbstractDataSourceChecker implements 
DataSourceChecker {
                 each.getConnection().close();
             }
         } catch (final SQLException ex) {
-            throw new PrepareFailedException("Data sources can't connected!", 
ex);
+            throw new PrepareFailedException("Data sources can not connect.", 
ex);
         }
     }
     
     @Override
-    public void checkTargetTable(final Collection<? extends DataSource> 
dataSources, final Collection<String> tableNames) {
+    public final void checkTargetTable(final Collection<? extends DataSource> 
dataSources, final Collection<String> tableNames) {
         try {
             for (DataSource each : dataSources) {
                 checkEmpty(each, tableNames);
             }
         } catch (final SQLException ex) {
-            throw new PrepareFailedException("Check target table failed!", ex);
+            throw new PrepareFailedException("Check target table failed.", ex);
         }
     }
     
     private void checkEmpty(final DataSource dataSource, final 
Collection<String> tableNames) throws SQLException {
         for (String each : tableNames) {
-            try (PreparedStatement preparedStatement = 
dataSource.getConnection().prepareStatement(getSqlBuilder().buildCheckEmptySQL(each));
+            try (PreparedStatement preparedStatement = 
dataSource.getConnection().prepareStatement(getSQLBuilder().buildCheckEmptySQL(each));
                  ResultSet resultSet = preparedStatement.executeQuery()) {
                 if (resultSet.next()) {
-                    throw new PrepareFailedException(String.format("Target 
table [%s] is not empty!", each));
+                    throw new PrepareFailedException(String.format("Target 
table `%s` is not empty.", each));
                 }
             }
         }
     }
     
-    protected abstract ScalingSQLBuilder getSqlBuilder();
+    protected abstract ScalingSQLBuilder getSQLBuilder();
 }
diff --git 
a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/job/check/AbstractDataSourceCheckerTest.java
 
b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/job/check/AbstractDataSourceCheckerTest.java
index 1325a81..7425e71 100644
--- 
a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/job/check/AbstractDataSourceCheckerTest.java
+++ 
b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/job/check/AbstractDataSourceCheckerTest.java
@@ -61,7 +61,7 @@ public final class AbstractDataSourceCheckerTest {
             }
             
             @Override
-            protected ScalingSQLBuilder getSqlBuilder() {
+            protected ScalingSQLBuilder getSQLBuilder() {
                 return null;
             }
         };
diff --git 
a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/component/checker/MySQLDataSourceChecker.java
 
b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/component/checker/MySQLDataSourceChecker.java
index fe19662..d95f70b 100644
--- 
a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/component/checker/MySQLDataSourceChecker.java
+++ 
b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/component/checker/MySQLDataSourceChecker.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.scaling.mysql.component.checker;
 
-import com.google.common.collect.Maps;
 import 
org.apache.shardingsphere.scaling.core.common.exception.PrepareFailedException;
 import 
org.apache.shardingsphere.scaling.core.job.check.source.AbstractDataSourceChecker;
 import 
org.apache.shardingsphere.scaling.mysql.component.MySQLScalingSQLBuilder;
@@ -42,7 +41,7 @@ public final class MySQLDataSourceChecker extends 
AbstractDataSourceChecker {
     
     private static final String[][] REQUIRED_PRIVILEGES = {{"ALL PRIVILEGES", 
"ON *.*"}, {"REPLICATION SLAVE", "REPLICATION CLIENT", "ON *.*"}};
     
-    private static final String SHOW_VARIABLES_SQL = "SHOW VARIABLES LIKE 
'%s'";
+    private static final String SHOW_VARIABLES_SQL = "SHOW VARIABLES LIKE ?";
     
     private static final Map<String, String> REQUIRED_VARIABLES = new 
HashMap<>(3, 1);
     
@@ -89,26 +88,28 @@ public final class MySQLDataSourceChecker extends 
AbstractDataSourceChecker {
     private void checkVariable(final DataSource dataSource) {
         try (Connection connection = dataSource.getConnection()) {
             for (Entry<String, String> entry : REQUIRED_VARIABLES.entrySet()) {
-                checkVariable(connection, entry);
+                checkVariable(connection, entry.getKey(), entry.getValue());
             }
         } catch (final SQLException ex) {
             throw new PrepareFailedException("Source data source check 
variables failed.", ex);
         }
     }
     
-    private void checkVariable(final Connection connection, final 
Entry<String, String> entry) throws SQLException {
-        try (PreparedStatement preparedStatement = 
connection.prepareStatement(String.format(SHOW_VARIABLES_SQL, entry.getKey()));
-             ResultSet resultSet = preparedStatement.executeQuery()) {
-            resultSet.next();
-            String value = resultSet.getString(2);
-            if (!entry.getValue().equalsIgnoreCase(value)) {
-                throw new PrepareFailedException(String.format("Source data 
source required %s = %s, now is %s", entry.getKey(), entry.getValue(), value));
+    private void checkVariable(final Connection connection, final String key, 
final String toBeCheckedValue) throws SQLException {
+        try (PreparedStatement preparedStatement = 
connection.prepareStatement(SHOW_VARIABLES_SQL)) {
+            preparedStatement.setString(1, "%" + key);
+            try (ResultSet resultSet = preparedStatement.executeQuery()) {
+                resultSet.next();
+                String actualValue = resultSet.getString(2);
+                if (!toBeCheckedValue.equalsIgnoreCase(actualValue)) {
+                    throw new PrepareFailedException(String.format("Source 
data source required `%s = %s`, now is `%s`", key, toBeCheckedValue, 
actualValue));
+                }
             }
         }
     }
     
     @Override
-    protected MySQLScalingSQLBuilder getSqlBuilder() {
-        return new MySQLScalingSQLBuilder(Maps.newHashMap());
+    protected MySQLScalingSQLBuilder getSQLBuilder() {
+        return new MySQLScalingSQLBuilder(new HashMap<>());
     }
 }
diff --git 
a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/component/MySQLDataSourceCheckerTest.java
 
b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/component/MySQLDataSourceCheckerTest.java
index 45d086b..6c282a1 100644
--- 
a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/component/MySQLDataSourceCheckerTest.java
+++ 
b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/component/MySQLDataSourceCheckerTest.java
@@ -23,19 +23,19 @@ import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.junit.MockitoJUnitRunner;
 
 import javax.sql.DataSource;
-import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collection;
-import java.util.LinkedList;
+import java.util.Collections;
 
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -43,9 +43,6 @@ import static org.mockito.Mockito.when;
 public final class MySQLDataSourceCheckerTest {
     
     @Mock
-    private Connection connection;
-    
-    @Mock
     private PreparedStatement preparedStatement;
     
     @Mock
@@ -53,16 +50,11 @@ public final class MySQLDataSourceCheckerTest {
     
     private Collection<DataSource> dataSources;
     
-    private MySQLDataSourceChecker dataSourceChecker;
-    
     @Before
     public void setUp() throws SQLException {
-        DataSource dataSource = mock(DataSource.class);
-        when(dataSource.getConnection()).thenReturn(connection);
-        dataSources = new LinkedList<>();
-        dataSources.add(dataSource);
-        dataSourceChecker = new MySQLDataSourceChecker();
-        
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
+        DataSource dataSource = mock(DataSource.class, RETURNS_DEEP_STUBS);
+        
when(dataSource.getConnection().prepareStatement(anyString())).thenReturn(preparedStatement);
+        dataSources = Collections.singleton(dataSource);
         when(preparedStatement.executeQuery()).thenReturn(resultSet);
     }
     
@@ -70,48 +62,47 @@ public final class MySQLDataSourceCheckerTest {
     public void assertCheckPrivilegeWithParticularSuccess() throws 
SQLException {
         when(resultSet.next()).thenReturn(true);
         when(resultSet.getString(1)).thenReturn("GRANT REPLICATION SLAVE, 
REPLICATION CLIENT ON *.* TO '%'@'%'");
-        dataSourceChecker.checkPrivilege(dataSources);
-        verify(preparedStatement, Mockito.times(1)).executeQuery();
+        new MySQLDataSourceChecker().checkPrivilege(dataSources);
+        verify(preparedStatement).executeQuery();
     }
     
     @Test
     public void assertCheckPrivilegeWithAllSuccess() throws SQLException {
         when(resultSet.next()).thenReturn(true);
         when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES CLIENT 
ON *.* TO '%'@'%'");
-        dataSourceChecker.checkPrivilege(dataSources);
-        verify(preparedStatement, Mockito.times(1)).executeQuery();
+        new MySQLDataSourceChecker().checkPrivilege(dataSources);
+        verify(preparedStatement).executeQuery();
     }
     
     @Test(expected = PrepareFailedException.class)
     public void assertCheckPrivilegeLackPrivileges() throws SQLException {
-        when(resultSet.next()).thenReturn(false);
-        dataSourceChecker.checkPrivilege(dataSources);
+        new MySQLDataSourceChecker().checkPrivilege(dataSources);
     }
     
     @Test(expected = PrepareFailedException.class)
     public void assertCheckPrivilegeFailure() throws SQLException {
         when(resultSet.next()).thenThrow(new SQLException(""));
-        dataSourceChecker.checkPrivilege(dataSources);
+        new MySQLDataSourceChecker().checkPrivilege(dataSources);
     }
     
     @Test
     public void assertCheckVariableSuccess() throws SQLException {
         when(resultSet.next()).thenReturn(true, true);
         when(resultSet.getString(2)).thenReturn("ON", "ROW", "FULL");
-        dataSourceChecker.checkVariable(dataSources);
-        verify(preparedStatement, Mockito.times(3)).executeQuery();
+        new MySQLDataSourceChecker().checkVariable(dataSources);
+        verify(preparedStatement, times(3)).executeQuery();
     }
     
     @Test(expected = PrepareFailedException.class)
     public void assertCheckVariableWithWrongVariable() throws SQLException {
         when(resultSet.next()).thenReturn(true, true);
         when(resultSet.getString(2)).thenReturn("OFF", "ROW");
-        dataSourceChecker.checkVariable(dataSources);
+        new MySQLDataSourceChecker().checkVariable(dataSources);
     }
     
     @Test(expected = PrepareFailedException.class)
     public void assertCheckVariableFailure() throws SQLException {
         when(resultSet.next()).thenThrow(new SQLException(""));
-        dataSourceChecker.checkVariable(dataSources);
+        new MySQLDataSourceChecker().checkVariable(dataSources);
     }
 }
diff --git 
a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-postgresql/src/main/java/org/apache/shardingsphere/scaling/postgresql/component/checker/PostgreSQLDataSourceChecker.java
 
b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-postgresql/src/main/java/org/apache/shardingsphere/scaling/postgresql/component/checker/PostgreSQLDataSourceChecker.java
index 00accd5..8427b22 100644
--- 
a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-postgresql/src/main/java/org/apache/shardingsphere/scaling/postgresql/component/checker/PostgreSQLDataSourceChecker.java
+++ 
b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-postgresql/src/main/java/org/apache/shardingsphere/scaling/postgresql/component/checker/PostgreSQLDataSourceChecker.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.scaling.postgresql.component.checker;
 
-import com.google.common.collect.Maps;
 import 
org.apache.shardingsphere.scaling.core.common.exception.PrepareFailedException;
 import 
org.apache.shardingsphere.scaling.core.common.sqlbuilder.ScalingSQLBuilder;
 import 
org.apache.shardingsphere.scaling.core.job.check.source.AbstractDataSourceChecker;
@@ -25,9 +24,11 @@ import 
org.apache.shardingsphere.scaling.postgresql.component.PostgreSQLScalingS
 
 import javax.sql.DataSource;
 import java.sql.Connection;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collection;
+import java.util.HashMap;
 
 /**
  * PostgreSQL Data source checker.
@@ -39,28 +40,34 @@ public final class PostgreSQLDataSourceChecker extends 
AbstractDataSourceChecker
         try {
             for (DataSource dataSource : dataSources) {
                 String tableName;
-                try (Connection connection = dataSource.getConnection()) {
-                    ResultSet tables = 
connection.getMetaData().getTables(connection.getCatalog(), null, "%", new 
String[]{"TABLE"});
-                    if (tables.next()) {
-                        tableName = tables.getString(3);
+                try (Connection connection = dataSource.getConnection();
+                     ResultSet resultSet = 
connection.getMetaData().getTables(connection.getCatalog(), null, "%", new 
String[]{"TABLE"})) {
+                    if (resultSet.next()) {
+                        tableName = resultSet.getString(3);
                     } else {
-                        throw new PrepareFailedException("No tables find in 
the source data source");
+                        throw new PrepareFailedException("No resultSet find in 
the source data source.");
                     }
-                    connection.prepareStatement(String.format("SELECT * FROM 
%s LIMIT 1", tableName)).executeQuery();
+                    checkTableExisted(tableName, connection);
                 }
             }
         } catch (final SQLException ex) {
-            throw new PrepareFailedException("Data sources privilege check 
failed!", ex);
+            throw new PrepareFailedException("Data sources privilege check 
failed.", ex);
+        }
+    }
+    
+    private void checkTableExisted(final String tableName, final Connection 
connection) throws SQLException {
+        String sql = "SELECT * FROM " + tableName + " LIMIT 1";
+        try (PreparedStatement preparedStatement = 
connection.prepareStatement(sql)) {
+            preparedStatement.executeQuery();
         }
     }
     
     @Override
     public void checkVariable(final Collection<? extends DataSource> 
dataSources) {
-    
     }
     
     @Override
-    protected ScalingSQLBuilder getSqlBuilder() {
-        return new PostgreSQLScalingSQLBuilder(Maps.newHashMap());
+    protected ScalingSQLBuilder getSQLBuilder() {
+        return new PostgreSQLScalingSQLBuilder(new HashMap<>());
     }
 }

Reply via email to