Skip to content

Conversation

Sunrisea
Copy link
Contributor

@Sunrisea Sunrisea commented Jun 16, 2025

Please do not create a Pull Request without creating an issue first.

What is the purpose of the change

Fix #13514

Brief changelog

During horizontal notification configuration migration, an Md5 value was added as a judgment condition, and migration operations are also performed for configurations with the same timestamp.

Verifying this change

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean package apache-rat:check findbugs:findbugs -Dmaven.test.skip=true to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.

Copy link

Thanks for your this PR. 🙏
Please check again for your PR changes whether contains any usage/api/configuration change such as Add new API , Add new configuration, Change default value of configuration.
If so, please add or update documents(markdown type) in docs/next/ for repository nacos-group/nacos-group.github.io


感谢您提交的PR。 🙏
请再次查看您的PR内容,确认是否包含任何使用方式/API/配置参数的变更,如:新增API新增配置参数修改默认配置等操作。
如果是,请确保在提交之前,在仓库nacos-group/nacos-group.github.io中的docs/next/目录下添加或更新文档(markdown格式)。

Copy link

lingma-agents bot commented Jun 16, 2025

修复配置迁移服务中时间戳和MD5校验的逻辑问题

变更概述

问题修复

  • ConfigMigrateService.java
    • 修正了配置迁移过程中因时间戳比较不严格导致的遗漏或错误迁移问题。
      • checkChangedConfigGrayMigrateStatecheckDeletedConfigMigrateState方法中,将targetConfig.getLastModified() > sourceConfig.getLastModified()条件改为>=,确保时间戳相同时仍执行迁移操作。
      • 新增对srcUser字段的校验,仅在来源用户为NAMESPACE_MIGRATE_SRC_USER时执行删除或更新操作,避免误操作其他用户数据。
    • 修复了MD5校验逻辑冲突,新增对目标配置MD5值与源配置的对比,确保数据一致性。
      • 在更新配置时,若目标MD5与源MD5不一致且时间戳满足条件,强制覆盖目标配置(如targetConfigAllInfo.setTenant(tenant))。

数据库变更

  • Embedded/ExternalConfigMigratePersistServiceImpl.java
    • 调整SQL更新语句,新增对md5字段的条件约束(如gmt_modified, md5作为唯一性判断条件),避免无效的数据库写入操作。
    • updateConfigInfoAtomicupdateConfigInfo4GrayWithoutHistory方法中,新增targetMd5参数,确保更新操作时MD5值的准确性。

逻辑增强

  • 新增对源配置更新时间较新的场景处理,例如在checkChangedConfigGrayMigrateState中,若changedConfiglastModified更早,则回滚使用源配置(如changedConfigInfoGrayWrapper.setTenant(targetTenant))。
变更统计
  • 📁 修改文件: 3 个文件

  • ✅ 新增代码: +34 行

  • ❌ 删除代码: -28 行

变更文件
文件路径 变更说明
config/​src/​main/​java/​com/​alibaba/​nacos/​config/​server/​service/​ConfigMigrateService.​java 调整时间戳比较逻辑,新增MD5校验和用户来源判断,修复配置迁移遗漏问题。
config/​src/​main/​java/​com/​alibaba/​nacos/​config/​server/​service/​repository/​embedded/​EmbeddedConfigMigratePersistServiceImpl.​java 更新SQL语句的条件约束,新增MD5校验参数以确保数据库操作一致性。
config/​src/​main/​java/​com/​alibaba/​nacos/​config/​server/​service/​repository/​extrnal/​ExternalConfigMigratePersistServiceImpl.​java 同步更新外部存储的SQL条件和参数,确保与嵌入式存储逻辑一致。

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

Copy link

@lingma-agents lingma-agents bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔎 代码评审报告

🎯 评审意见概览
严重度 数量 说明
🔴 Blocker 0 阻断性问题,需立即修复。例如:系统崩溃、关键功能不可用或严重安全漏洞。
🟠 Critical 1 严重问题,高优先级修复。例如:核心功能异常或性能瓶颈影响用户体验。
🟡 Major 0 主要问题,建议修复。例如:非核心功能缺陷或代码维护性较差。
🟢 Minor 0 次要问题,酬情优化。例如:代码格式不规范或注释缺失。

总计: 1 个问题

📋 评审意见详情
💡 代码实现建议
以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
☕ config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigMigratePersistServiceImpl.java (1 💬)
🚀 架构设计建议
以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍1. 条件判断逻辑不一致可能导致数据竞争和覆盖问题

在多个方法中,关于时间戳(lastModified)和MD5的条件判断逻辑存在不一致。例如:

  1. ConfigMigrateService.checkChangedConfigGrayMigrateState中,338行使用>=比较lastModified,而364行使用>=但后续条件组合不同。
  2. checkDeletedConfigGrayMigrateState中469行将条件从<改为<=,可能导致数据删除策略变化。
  3. ExternalConfigMigratePersistServiceImpl.syncConfig中371行新增>=分支,但未统一其他类似场景的条件。

这种不一致可能导致不同分支下相同逻辑场景的处理结果不一致,引发数据覆盖或删除冲突。建议统一时间戳比较逻辑,并明确各条件组合的业务含义。

📌 关键代码

⚠️ 潜在风险

数据竞争导致配置信息意外覆盖或删除,迁移过程中数据不一致,影响系统稳定性。

🔍2. SQL参数与占位符不匹配问题可能未完全修复

虽然EmbeddedConfigMigratePersistServiceImpl的281行已指出参数不匹配,但其他SQL语句可能存在类似隐患。例如:

  1. updateConfigInfoAtomic方法(274行)新增的md5条件列未在参数列表中正确对应。
  2. updateConfigInfo4GrayWithoutHistory(361行)的SQL条件包含md5,但参数传递顺序可能未同步调整。

需要系统性检查所有SQL构造处的参数与占位符数量及顺序是否一致。

📌 关键代码

⚠️ 潜在风险

执行SQL时出现参数绑定错误,导致更新失败或数据污染。

🔍3. 跨存储实现的同步逻辑存在代码重复

EmbeddedExternal存储实现中的syncConfigsyncConfigGray方法存在大量重复逻辑,例如:

  1. 时间戳比较(lastModified)和MD5验证的分支判断。
  2. 租户设置(setTenant)和数据库更新操作的流程。

未将公共逻辑抽象为基类或工具方法,导致代码冗余,后续维护困难。

📌 关键代码

⚠️ 潜在风险

重复代码导致后续功能修改需同步更新多个地方,增加维护成本并引入不一致性风险。

🔍4. 新增复杂条件分支缺乏测试覆盖

新增的多个条件分支(如370行的else if(lastModified < ...)和参数组合(如targetMd5)未见对应的单元测试。特别是:

  1. 时间戳相等(==)场景的处理。
  2. targetMd5与源MD5不同时的更新逻辑。

可能存在未被测试的路径,导致线上环境出现未预期的行为。

📌 关键代码

⚠️ 潜在风险

新增逻辑未经过充分验证,存在潜在缺陷未被发现的风险。

审查详情
📒 文件清单 (3 个文件)
📝 变更: 3 个文件

📝 变更文件:

  • config/src/main/java/com/alibaba/nacos/config/server/service/ConfigMigrateService.java
  • config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigMigratePersistServiceImpl.java
  • config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigMigratePersistServiceImpl.java

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。


final Object[] args = new Object[] {configInfo.getContent(), md5Tmp, srcIp, srcUser, appNameTmp, desc, use,
effect, type, schema, encryptedDataKey, configInfo.getDataId(), configInfo.getGroup(), tenantTmp,
new Timestamp(lastModified)};
new Timestamp(lastModified), targetMd5};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL参数数量与占位符不匹配

🟠 Critical | 🐞 Bugs

📋 问题详情

在updateConfigInfoAtomic方法中,新增的targetMd5参数未被正确使用在SQL条件中。当前SQL的where条件仅包含data_id, group_id, tenant_id, gmt_modified, md5字段,但参数数组传递了额外的targetMd5值可能导致参数数量不匹配引发异常。

💡 解决方案

调整参数顺序并移除多余参数:

-new Timestamp(lastModified), targetMd5
+new Timestamp(lastModified)

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

@KomachiSion KomachiSion merged commit 46ca87c into alibaba:develop Jun 16, 2025
5 of 6 checks passed
@KomachiSion KomachiSion added this to the 3.0.2 milestone Jun 16, 2025
@KomachiSion KomachiSion added the kind/bug Category issues or prs related to bug. label Jun 16, 2025
@wuyfee
Copy link

wuyfee commented Jun 16, 2025

$\color{red}{FAILURE}$
DETAILS
✅ - docker: success
❌ - deploy (standalone & cluster & standalone_auth): failure
❌ - e2e-java-test (standalone & cluster & standalone_auth): skipped
❌ - e2e-go-test (standalone & cluster): skipped
❌ - e2e-cpp-test (standalone & cluster): skipped
❌ - e2e-csharp-test (standalone & cluster): skipped
❌ - e2e-nodejs-test (standalone & cluster): skipped
❌ - e2e-python-test (standalone & cluster): skipped
✅ - clean (standalone & cluster & standalone_auth): success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Category issues or prs related to bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

During Nacos 3.0 upgrade process, namespace errors may occur under scenarios with frequent configuration changes.
3 participants