You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/02/23 07:28:06 UTC

[shardingsphere] branch master updated: Update MySQL checksum SQL for scaling (#15452)

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

zhangliang 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 af1c0fc  Update MySQL checksum SQL for scaling (#15452)
af1c0fc is described below

commit af1c0fc750a8a1ef57d843d0bfbed5dde673bba7
Author: Hongsheng Zhong <sa...@126.com>
AuthorDate: Wed Feb 23 15:27:09 2022 +0800

    Update MySQL checksum SQL for scaling (#15452)
    
    * Update MySQL checksum SQL
    
    * Rename method name to buildCrc32SQL
    
    * Rename to CRC32
---
 .../consistency/CRC32MatchMySQLSingleTableDataCalculator.java     | 6 +++---
 .../data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilder.java   | 8 ++++----
 .../pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilderTest.java    | 4 ++--
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/check/consistency/CRC32MatchMySQLSingleTableDataCalculator.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/check/consistency/CRC32MatchMySQLSingleTableData [...]
index eaad0c9..565bac7 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/check/consistency/CRC32MatchMySQLSingleTableDataCalculator.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/check/consistency/CRC32MatchMySQLSingleTableDataCalculator.java
@@ -58,9 +58,9 @@ public final class CRC32MatchMySQLSingleTableDataCalculator extends AbstractSing
     public Iterable<Object> calculate(final DataCalculateParameter dataCalculateParameter) {
         String logicTableName = dataCalculateParameter.getLogicTableName();
         List<Long> result = dataCalculateParameter.getColumnNames().stream().map(each -> {
-            String sql = SQL_BUILDER.buildSumCrc32SQL(logicTableName, each);
+            String sql = SQL_BUILDER.buildCRC32SQL(logicTableName, each);
             try {
-                return sumCrc32(dataCalculateParameter.getDataSource(), sql);
+                return calculateCRC32(dataCalculateParameter.getDataSource(), sql);
             } catch (final SQLException ex) {
                 throw new PipelineDataConsistencyCheckFailedException(String.format("table %s data check failed.", logicTableName), ex);
             }
@@ -68,7 +68,7 @@ public final class CRC32MatchMySQLSingleTableDataCalculator extends AbstractSing
         return Collections.unmodifiableList(result);
     }
     
-    private long sumCrc32(final DataSource dataSource, final String sql) throws SQLException {
+    private long calculateCRC32(final DataSource dataSource, final String sql) throws SQLException {
         try (Connection connection = dataSource.getConnection();
              PreparedStatement preparedStatement = connection.prepareStatement(sql);
              ResultSet resultSet = preparedStatement.executeQuery()) {
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilder.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilder.java
index 80d2272..cf8409c 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilder.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilder.java
@@ -71,14 +71,14 @@ public final class MySQLPipelineSQLBuilder extends AbstractPipelineSQLBuilder {
     }
     
     /**
-     * Build select sum crc32 SQL.
+     * Build CRC32 SQL.
      *
      * @param tableName table Name
      * @param column column
-     * @return select sum crc32 SQL
+     * @return select CRC32 SQL
      */
-    public String buildSumCrc32SQL(final String tableName, final String column) {
-        return String.format("SELECT SUM(CRC32(%s)) AS checksum FROM %s", quote(column), quote(tableName));
+    public String buildCRC32SQL(final String tableName, final String column) {
+        return String.format("SELECT BIT_XOR(CAST(CRC32(%s) AS UNSIGNED)) AS checksum FROM %s", quote(column), quote(tableName));
     }
     
     @Override
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilderTest.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilderTest.java
index 002d54a..d111f4b 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilderTest.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/sqlbuilder/MySQLPipelineSQLBuilderTest.java
@@ -47,8 +47,8 @@ public final class MySQLPipelineSQLBuilderTest {
     
     @Test
     public void assertBuildSumCrc32SQL() {
-        String actual = sqlBuilder.buildSumCrc32SQL("t2", "id");
-        assertThat(actual, is("SELECT SUM(CRC32(`id`)) AS checksum FROM `t2`"));
+        String actual = sqlBuilder.buildCRC32SQL("t2", "id");
+        assertThat(actual, is("SELECT BIT_XOR(CAST(CRC32(`id`) AS UNSIGNED)) AS checksum FROM `t2`"));
     }
     
     private DataRecord mockDataRecord(final String tableName) {