From 1fc56f624b878fd646e48211bf0514a68a3dae15 Mon Sep 17 00:00:00 2001 From: Hongsheng Zhong Date: Fri, 17 Nov 2023 16:51:06 +0800 Subject: [PATCH] Improve integer matching on consistency check --- .../DataConsistencyCheckUtils.java | 24 ++++++++++++++++++- ...dSingleTableInventoryCalculatedResult.java | 5 ++-- .../DataConsistencyCheckUtilsTest.java | 11 +++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/DataConsistencyCheckUtils.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/DataConsistencyCheckUtils.java index adb79c3255405..0aa0844db660e 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/DataConsistencyCheckUtils.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/DataConsistencyCheckUtils.java @@ -65,9 +65,20 @@ public static boolean recordsEquals(final Map thisRecord, final return true; } + /** + * Whether column values are matched or not. + * + * @param equalsBuilder equals builder + * @param thisColumnValue this column value + * @param thatColumnValue that column value + * @return true if matched, otherwise false + */ @SneakyThrows(SQLException.class) - private static boolean isMatched(final EqualsBuilder equalsBuilder, final Object thisColumnValue, final Object thatColumnValue) { + public static boolean isMatched(final EqualsBuilder equalsBuilder, final Object thisColumnValue, final Object thatColumnValue) { equalsBuilder.reset(); + if (isInteger(thisColumnValue) && isInteger(thatColumnValue)) { + return isIntegerEquals((Number) thisColumnValue, (Number) thatColumnValue); + } if (thisColumnValue instanceof SQLXML && thatColumnValue instanceof SQLXML) { return ((SQLXML) thisColumnValue).getString().equals(((SQLXML) thatColumnValue).getString()); } @@ -80,6 +91,17 @@ private static boolean isMatched(final EqualsBuilder equalsBuilder, final Object return equalsBuilder.append(thisColumnValue, thatColumnValue).isEquals(); } + private static boolean isInteger(final Object value) { + if (!(value instanceof Number)) { + return false; + } + return value instanceof Long || value instanceof Integer || value instanceof Short || value instanceof Byte; + } + + private static boolean isIntegerEquals(final Number one, final Number another) { + return one.longValue() == another.longValue(); + } + /** * Check two BigDecimal whether equals or not. * diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/result/RecordSingleTableInventoryCalculatedResult.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/result/RecordSingleTableInventoryCalculatedResult.java index 1db10f4352b37..8bd104982f04d 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/result/RecordSingleTableInventoryCalculatedResult.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/result/RecordSingleTableInventoryCalculatedResult.java @@ -26,7 +26,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Optional; /** @@ -66,12 +65,12 @@ public boolean equals(final Object o) { return false; } final RecordSingleTableInventoryCalculatedResult that = (RecordSingleTableInventoryCalculatedResult) o; - if (recordsCount != that.recordsCount || !Objects.equals(maxUniqueKeyValue, that.maxUniqueKeyValue)) { + EqualsBuilder equalsBuilder = new EqualsBuilder(); + if (recordsCount != that.recordsCount || !DataConsistencyCheckUtils.isMatched(equalsBuilder, maxUniqueKeyValue, that.maxUniqueKeyValue)) { log.warn("Record count or max unique key value not match, recordCount1={}, recordCount2={}, maxUniqueKeyValue1={}, maxUniqueKeyValue2={}.", recordsCount, that.recordsCount, maxUniqueKeyValue, that.maxUniqueKeyValue); return false; } - EqualsBuilder equalsBuilder = new EqualsBuilder(); Iterator> thisRecordsIterator = records.iterator(); Iterator> thatRecordsIterator = that.records.iterator(); while (thisRecordsIterator.hasNext() && thatRecordsIterator.hasNext()) { diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/DataConsistencyCheckUtilsTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/DataConsistencyCheckUtilsTest.java index 13a4fbba35e92..01676117059a8 100644 --- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/DataConsistencyCheckUtilsTest.java +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/DataConsistencyCheckUtilsTest.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.data.pipeline.core.consistencycheck; +import org.apache.commons.lang3.builder.EqualsBuilder; import org.junit.jupiter.api.Test; import java.math.BigDecimal; @@ -25,6 +26,16 @@ class DataConsistencyCheckUtilsTest { + @Test + void assertIsIntegerEquals() { + EqualsBuilder equalsBuilder = new EqualsBuilder(); + String value = "123"; + Long longValue = Long.parseLong(value); + assertTrue(DataConsistencyCheckUtils.isMatched(equalsBuilder, longValue, Integer.parseInt(value))); + assertTrue(DataConsistencyCheckUtils.isMatched(equalsBuilder, longValue, Short.parseShort(value))); + assertTrue(DataConsistencyCheckUtils.isMatched(equalsBuilder, longValue, Byte.parseByte(value))); + } + @Test void assertIsBigDecimalEquals() { BigDecimal one = BigDecimal.valueOf(3322, 1);