You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2023/05/13 08:42:48 UTC

[shardingsphere] branch master updated: Fix sonar issue of AbstractDataConsistencyCalculateAlgorithm (#25639)

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

panjuan 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 ead9b72550a Fix sonar issue of AbstractDataConsistencyCalculateAlgorithm (#25639)
ead9b72550a is described below

commit ead9b72550a5729da479c118c02e5a3b6e53bafd
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat May 13 16:42:38 2023 +0800

    Fix sonar issue of AbstractDataConsistencyCalculateAlgorithm (#25639)
---
 .../AbstractDataConsistencyCalculateAlgorithm.java | 25 +++++++++++++++-------
 ...DataMatchDataConsistencyCalculateAlgorithm.java |  5 ++---
 2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/algorithm/AbstractDataConsistencyCalculateAlgorithm.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/algorithm/AbstractDataConsistencyCalculateAlgorithm.java
index d416b133bf6..bfaed7d3a6f 100644
--- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/algorithm/AbstractDataConsistencyCalculateAlgorithm.java
+++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/algorithm/AbstractDataConsistencyCalculateAlgorithm.java
@@ -17,13 +17,14 @@
 
 package org.apache.shardingsphere.data.pipeline.core.check.consistency.algorithm;
 
-import lombok.Getter;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.data.pipeline.spi.check.consistency.DataConsistencyCalculateAlgorithm;
 
 import java.sql.SQLException;
 import java.sql.SQLFeatureNotSupportedException;
 import java.sql.Statement;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * Abstract data consistency calculate algorithm.
@@ -31,19 +32,18 @@ import java.sql.Statement;
 @Slf4j
 public abstract class AbstractDataConsistencyCalculateAlgorithm implements DataConsistencyCalculateAlgorithm {
     
-    @Getter
-    private volatile boolean canceling;
+    private final AtomicBoolean canceling = new AtomicBoolean(false);
     
-    private volatile Statement currentStatement;
+    private final AtomicReference<Statement> currentStatement = new AtomicReference<>();
     
-    protected void setCurrentStatement(final Statement statement) {
-        this.currentStatement = statement;
+    protected final void setCurrentStatement(final Statement statement) {
+        currentStatement.set(statement);
     }
     
     @Override
     public void cancel() throws SQLException {
-        canceling = true;
-        Statement statement = currentStatement;
+        canceling.set(true);
+        Statement statement = currentStatement.get();
         if (null == statement || statement.isClosed()) {
             log.info("cancel, statement is null or closed");
             return;
@@ -60,4 +60,13 @@ public abstract class AbstractDataConsistencyCalculateAlgorithm implements DataC
         }
         log.info("cancel cost {} ms", System.currentTimeMillis() - startTimeMillis);
     }
+    
+    /**
+     * Is canceling.
+     * 
+     * @return is canceling or not
+     */
+    public final boolean isCanceling() {
+        return canceling.get();
+    }
 }
diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/algorithm/DataMatchDataConsistencyCalculateAlgorithm.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/algorithm/DataMatchDataConsistencyCalculateAlgorithm.java
index f87374fcf22..e720a9a3fd9 100644
--- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/algorithm/DataMatchDataConsistencyCalculateAlgorithm.java
+++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/algorithm/DataMatchDataConsistencyCalculateAlgorithm.java
@@ -37,6 +37,7 @@ import org.apache.shardingsphere.data.pipeline.spi.sqlbuilder.PipelineSQLBuilder
 import org.apache.shardingsphere.data.pipeline.util.spi.PipelineTypedSPILoader;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.util.spi.annotation.SPIDescription;
 import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
@@ -101,9 +102,7 @@ public final class DataMatchDataConsistencyCalculateAlgorithm extends AbstractSt
             ColumnValueReader columnValueReader = PipelineTypedSPILoader.getDatabaseTypedService(ColumnValueReader.class, param.getDatabaseType());
             ResultSet resultSet = calculationContext.getResultSet();
             while (resultSet.next()) {
-                if (isCanceling()) {
-                    throw new PipelineTableDataConsistencyCheckLoadingFailedException(param.getSchemaName(), param.getLogicTableName());
-                }
+                ShardingSpherePreconditions.checkState(!isCanceling(), () -> new PipelineTableDataConsistencyCheckLoadingFailedException(param.getSchemaName(), param.getLogicTableName()));
                 ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
                 int columnCount = resultSetMetaData.getColumnCount();
                 Collection<Object> record = new LinkedList<>();