diff --git a/pom.xml b/pom.xml index 3f36d517..6743a5ca 100644 --- a/pom.xml +++ b/pom.xml @@ -204,25 +204,11 @@ ZT 中铜 ZStack 私服 - http://172.16.46.63:30708/repository/test/ + http://172.16.46.63:30708/repository/zt-cloud/ always warn - - false - always - - - - ZT-snap - 中铜 ZStack 私服 - http://172.16.46.63:30708/repository/test-snap/ - - false - always - warn - true always diff --git a/zt-framework/pom.xml b/zt-framework/pom.xml index 241a211e..d34ccec7 100644 --- a/zt-framework/pom.xml +++ b/zt-framework/pom.xml @@ -25,7 +25,7 @@ zt-spring-boot-starter-job zt-spring-boot-starter-mq zt-spring-boot-starter-rpc - + zt-spring-boot-starter-seata-dm zt-spring-boot-starter-excel zt-spring-boot-starter-test diff --git a/zt-framework/zt-spring-boot-starter-seata-dm/pom.xml b/zt-framework/zt-spring-boot-starter-seata-dm/pom.xml new file mode 100644 index 00000000..49d7b1e2 --- /dev/null +++ b/zt-framework/zt-spring-boot-starter-seata-dm/pom.xml @@ -0,0 +1,32 @@ + + + + zt-framework + com.zt.plat + ${revision} + + 4.0.0 + jar + + zt-spring-boot-starter-seata-dm + + ${project.artifactId} + + Seata 达梦数据库补丁模块 + 解决 DmdbTimestamp 时区格式不一致导致的 dirty undo log 回滚失败问题 + 补丁来源: https://github.com/apache/incubator-seata/pull/7538 + Seata 2.6.0 发布后可移除此模块 + + + + + + org.apache.seata + seata-spring-boot-starter + provided + + + + diff --git a/zt-framework/zt-spring-boot-starter-seata-dm/src/main/java/org/apache/seata/rm/datasource/DataCompareUtils.java b/zt-framework/zt-spring-boot-starter-seata-dm/src/main/java/org/apache/seata/rm/datasource/DataCompareUtils.java new file mode 100644 index 00000000..24c15d5a --- /dev/null +++ b/zt-framework/zt-spring-boot-starter-seata-dm/src/main/java/org/apache/seata/rm/datasource/DataCompareUtils.java @@ -0,0 +1,326 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.rm.datasource; + +import org.apache.seata.common.util.CollectionUtils; +import org.apache.seata.common.util.StringUtils; +import org.apache.seata.core.model.Result; +import org.apache.seata.rm.datasource.sql.struct.Field; +import org.apache.seata.rm.datasource.sql.struct.Row; +import org.apache.seata.rm.datasource.sql.struct.TableRecords; +import org.apache.seata.rm.datasource.undo.AbstractUndoLogManager; +import org.apache.seata.rm.datasource.undo.parser.FastjsonUndoLogParser; +import org.apache.seata.rm.datasource.undo.parser.JacksonUndoLogParser; +import org.apache.seata.sqlparser.struct.TableMeta; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Method; +import java.math.BigDecimal; +import java.sql.Date; +import java.sql.Time; +import java.sql.Timestamp; +import java.sql.Types; +import java.time.Instant; +import java.time.LocalDateTime; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * DataCompareUtils - 包含达梦数据库 DmdbTimestamp 时区问题的补丁 + *

+ * 此类覆盖 Seata 原有的 DataCompareUtils,添加了对达梦数据库 DmdbTimestamp 类型的特殊处理。 + * 通过将 DmdbTimestamp 转换为 UTC Instant 进行比较,解决时区格式不一致导致的 dirty undo log 问题。 + *

+ * 问题背景: + * - 达梦数据库的 DmdbTimestamp 类型在序列化/反序列化后时区格式不一致 + * - 例如:beforeImage 为 "2025-12-25 09:38:54.077811 +08:00" + * afterImage 为 "2025-12-25 09:38:54.077811" + * - 导致 Seata AT 模式回滚时 dirty undo log 检查失败 + *

+ * 解决方案: + * - 当检测到 DmdbTimestamp 类型时,将两个值都转换为 UTC Instant 进行比较 + * - 这样可以忽略时区格式差异,只比较实际的时间点 + *

+ * 补丁来源: https://github.com/apache/incubator-seata/pull/7538 + * 相关 Issue: https://github.com/apache/incubator-seata/issues/7453 + * 该修复已合并到 Seata 2.x 分支,将在 Seata 2.6.0 正式发布,届时可删除此模块。 + * + * @author Seata Community (PR #7538) + */ +public class DataCompareUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(DataCompareUtils.class); + + /** + * 标识补丁类是否已加载 + */ + private static final boolean PATCHED; + + static { + PATCHED = true; + LOGGER.info("[zt-spring-boot-starter-seata-dm] DataCompareUtils 补丁类已加载,用于解决达梦数据库 DmdbTimestamp 时区问题"); + } + + private DataCompareUtils() {} + + /** + * Is field equals result. + * + * @param f0 the f 0 + * @param f1 the f 1 + * @return the result + */ + public static Result isFieldEquals(Field f0, Field f1) { + if (f0 == null) { + return Result.build(f1 == null); + } else { + if (f1 == null) { + return Result.build(false); + } else { + if (StringUtils.equalsIgnoreCase(f0.getName(), f1.getName()) && f0.getType() == f1.getType()) { + if (f0.getValue() == null) { + return Result.build(f1.getValue() == null); + } else { + if (f1.getValue() == null) { + return Result.buildWithParams( + false, "Field not equals, name {}, new value is null", f0.getName()); + } else { + String currentSerializer = AbstractUndoLogManager.getCurrentSerializer(); + if (StringUtils.equals(currentSerializer, FastjsonUndoLogParser.NAME)) { + convertType(f0, f1); + } + // 达梦数据库 DmdbTimestamp 时区补丁 (PR #7538) + if (StringUtils.equals(currentSerializer, JacksonUndoLogParser.NAME)) { + Object v0 = f0.getValue(); + Object v1 = f1.getValue(); + if (isDmdbTimestamp(v0) && isDmdbTimestamp(v1)) { + Instant i0 = toInstant(v0); + Instant i1 = toInstant(v1); + boolean equals = Objects.equals(i0, i1); + LOGGER.info("[zt-seata-dm-patch] DmdbTimestamp 字段比较: field={}, equals={}", f0.getName(), equals); + return equals + ? Result.ok() + : Result.buildWithParams( + false, + "Field not equals (DmdbTimestamp), name {}, old value {}, new value {}", + f0.getName(), + v0, + v1); + } + } + boolean result = Objects.deepEquals(f0.getValue(), f1.getValue()); + if (result) { + return Result.ok(); + } else { + return Result.buildWithParams( + false, + "Field not equals, name {}, old value {}, new value {}", + f0.getName(), + f0.getValue(), + f1.getValue()); + } + } + } + } else { + return Result.buildWithParams( + false, + "Field not equals, old name {} type {}, new name {} type {}", + f0.getName(), + f0.getType(), + f1.getName(), + f1.getType()); + } + } + } + } + + private static void convertType(Field f0, Field f1) { + int f0Type = f0.getType(); + int f1Type = f1.getType(); + if (f0Type == Types.DATE && f0.getValue().getClass().equals(String.class)) { + String[] strings = f0.getValue().toString().split(" "); + f0.setValue(Date.valueOf(strings[0])); + } + if (f1Type == Types.DATE && f1.getValue().getClass().equals(String.class)) { + String[] strings = f1.getValue().toString().split(" "); + f1.setValue(Date.valueOf(strings[0])); + } + if (f0Type == Types.TIME && f0.getValue().getClass().equals(String.class)) { + f0.setValue(Time.valueOf(f0.getValue().toString())); + } + if (f1Type == Types.TIME && f1.getValue().getClass().equals(String.class)) { + f1.setValue(Time.valueOf(f1.getValue().toString())); + } + if (f0Type == Types.TIMESTAMP && f0.getValue().getClass().equals(String.class)) { + if (f1.getValue().getClass().equals(LocalDateTime.class)) { + f0.setValue(LocalDateTime.parse(f0.getValue().toString())); + } else { + f0.setValue(Timestamp.valueOf(f0.getValue().toString())); + } + } + if (f1Type == Types.TIMESTAMP && f1.getValue().getClass().equals(String.class)) { + f1.setValue(Timestamp.valueOf(f1.getValue().toString())); + } + if (f0Type == Types.DECIMAL && f0.getValue().getClass().equals(Integer.class)) { + f0.setValue(new BigDecimal(f0.getValue().toString())); + } + if (f1Type == Types.DECIMAL && f1.getValue().getClass().equals(Integer.class)) { + f1.setValue(new BigDecimal(f1.getValue().toString())); + } + if (f0Type == Types.BIGINT && f0.getValue().getClass().equals(Integer.class)) { + f0.setValue(Long.parseLong(f0.getValue().toString())); + } + if (f1Type == Types.BIGINT && f1.getValue().getClass().equals(Integer.class)) { + f1.setValue(Long.parseLong(f1.getValue().toString())); + } + } + + /** + * Is records equals result. + * + * @param beforeImage the before image + * @param afterImage the after image + * @return the result + */ + public static Result isRecordsEquals(TableRecords beforeImage, TableRecords afterImage) { + if (beforeImage == null) { + return Result.build(afterImage == null, null); + } else { + if (afterImage == null) { + return Result.build(false, null); + } + if (beforeImage.getTableName().equalsIgnoreCase(afterImage.getTableName()) + && CollectionUtils.isSizeEquals(beforeImage.getRows(), afterImage.getRows())) { + // when image is EmptyTableRecords, getTableMeta will throw an exception + if (CollectionUtils.isEmpty(beforeImage.getRows())) { + return Result.ok(); + } + return compareRows(beforeImage.getTableMeta(), beforeImage.getRows(), afterImage.getRows()); + } else { + return Result.build(false, null); + } + } + } + + /** + * Is rows equals result. + * + * @param tableMetaData the table meta data + * @param oldRows the old rows + * @param newRows the new rows + * @return the result + */ + public static Result isRowsEquals(TableMeta tableMetaData, List oldRows, List newRows) { + if (!CollectionUtils.isSizeEquals(oldRows, newRows)) { + return Result.build(false, null); + } + return compareRows(tableMetaData, oldRows, newRows); + } + + private static Result compareRows(TableMeta tableMetaData, List oldRows, List newRows) { + // old row to map + Map> oldRowsMap = rowListToMap(oldRows, tableMetaData.getPrimaryKeyOnlyName()); + // new row to map + Map> newRowsMap = rowListToMap(newRows, tableMetaData.getPrimaryKeyOnlyName()); + // compare data + for (Map.Entry> oldEntry : oldRowsMap.entrySet()) { + String key = oldEntry.getKey(); + Map oldRow = oldEntry.getValue(); + Map newRow = newRowsMap.get(key); + if (newRow == null) { + return Result.buildWithParams(false, "compare row failed, rowKey {}, reason [newRow is null]", key); + } + for (Map.Entry oldRowEntry : oldRow.entrySet()) { + String fieldName = oldRowEntry.getKey(); + Field oldField = oldRowEntry.getValue(); + Field newField = newRow.get(fieldName); + if (newField == null) { + return Result.buildWithParams( + false, + "compare row failed, rowKey {}, fieldName {}, reason [newField is null]", + key, + fieldName); + } + Result oldEqualsNewFieldResult = isFieldEquals(oldField, newField); + if (!oldEqualsNewFieldResult.getResult()) { + return oldEqualsNewFieldResult; + } + } + } + return Result.ok(); + } + + /** + * Row list to map map. + * + * @param rowList the row list + * @param primaryKeyList the primary key list + * @return the map + */ + public static Map> rowListToMap(List rowList, List primaryKeyList) { + // {value of primaryKey, value of all columns} + Map> rowMap = new HashMap<>(); + for (Row row : rowList) { + // ensure the order of column + List rowFieldList = row.getFields().stream() + .sorted(Comparator.comparing(Field::getName)) + .collect(Collectors.toList()); + // {uppercase fieldName : field} + Map colsMap = new HashMap<>(); + StringBuilder rowKey = new StringBuilder(); + boolean firstUnderline = false; + for (int j = 0; j < rowFieldList.size(); j++) { + Field field = rowFieldList.get(j); + if (primaryKeyList.stream().anyMatch(e -> field.getName().equals(e))) { + if (firstUnderline && j > 0) { + rowKey.append("_"); + } + rowKey.append(String.valueOf(field.getValue())); + firstUnderline = true; + } + colsMap.put(field.getName().trim().toUpperCase(), field); + } + rowMap.put(rowKey.toString(), colsMap); + } + return rowMap; + } + + /** + * 判断是否为达梦数据库的 DmdbTimestamp 类型 + */ + private static boolean isDmdbTimestamp(Object obj) { + return obj != null + && "dm.jdbc.driver.DmdbTimestamp".equals(obj.getClass().getName()); + } + + /** + * 将 DmdbTimestamp 转换为 Instant + */ + private static Instant toInstant(Object dmdbTimestamp) { + try { + Method toInstantMethod = dmdbTimestamp.getClass().getMethod("toInstant"); + return (Instant) toInstantMethod.invoke(dmdbTimestamp); + } catch (Exception e) { + throw new RuntimeException("Failed to convert DmdbTimestamp to Instant", e); + } + } +}