Skip to content

perf: 空间配置恢复默认值修改为批量操作 --story=132989396#651

Open
guohelu wants to merge 47 commits intoTencentBlueKing:developfrom
guohelu:develop_0325
Open

perf: 空间配置恢复默认值修改为批量操作 --story=132989396#651
guohelu wants to merge 47 commits intoTencentBlueKing:developfrom
guohelu:develop_0325

Conversation

@guohelu
Copy link
Copy Markdown
Collaborator

@guohelu guohelu commented Mar 27, 2026

Reviewed, transaction id: 77017

Mianhuatang8 and others added 30 commits February 25, 2026 19:37
* feat: 节点配置添加凭证选择 --story=125449007
# Reviewed, transaction id: 66075

* fix: 关闭label提示下划线 --story=125449007
# Reviewed, transaction id: 66078

* feat: 节点配置添加凭证选择 --story=125449007
# Reviewed, transaction id: 66104

* fix: 凭证选择校验优化 --story=125449007
# Reviewed, transaction id: 66163
* fix: 任务和流程表格增加hover和无数据时填充--

* fix: 流程和任务列表新建标签后回填

* fix: 标签选择器样式调整

* fix: 新建标签增加校验条件

* fix: 国际化内容补全
新增 bkflow.statistics 模块,包含:
- 6 张统计数据模型(任务/节点/模板/每日汇总/插件汇总)
- 模板和任务采集器,通过 Django Signal 触发 Celery 异步写入
- 预聚合定时任务(每日汇总、插件汇总、过期清理)
- 系统级和空间级两套 REST API(概览/趋势/排行/失败分析)
- 独立数据库路由、回填管理命令、Django Admin 配置

Made-with: Cursor
新增 _CustomSpan、create_execution_span、_create_span_with_custom_id、
clean_plugin_span_outputs 等函数,修复 span 层级关系,支持预生成
plugin_span_id 用于 method span 正确嵌套,精简冗余属性,增加
propagate_attributes 幂等优化。

Made-with: Cursor
更新插件基类 BKFlowBaseService:span 名称增加 .plugin. 命名空间,
_get_trace_context 返回 plugin_span_id,_end_plugin_span 后调用
clean_plugin_span_outputs 清理内部属性,execute/schedule 传递
plugin_span_id 给 method span。

Made-with: Cursor
TaskOperation.start() 使用 create_execution_span 替代原有
get_current_trace_context 方式,创建独立的执行级根 span 并
注入 root_pipeline_data,不再依赖外部 trace context 传递。

Made-with: Cursor
为 collectors、conf、signals、tasks、views、models、serializers、
db_router、backfill command 等 12 个核心文件补充类/方法 docstring
和关键逻辑的行内注释,提升代码可读性。

Made-with: Cursor
1. 恢复 end_plugin_span/plugin_method_span 中的 plugin.success 属性
2. 对 error_message 增加 [:1000] 截断保护
3. 关键 span 创建失败日志从 debug 提升为 warning
4. _CustomSpan 补充 SDK 兼容性风险注释
5. 旧设计文档标注已被新方案取代

Made-with: Cursor
* feat: 新增支持简化协议JSON创建流程接口 --story=132674359

# Conflicts:
#	bkflow/apigw/docs/apigw-docs.zip

* fix: 重命名& 补充tests和docs --story=132674359

* fix: tests 修复 --story=132674359

* fix: 优化数据库查询 --story=132674359

* fix: tests 修复 & trigger_ids 防空值 --story=132674359

* fix: 作用域类型和作用域值 修复 --story=132674359
新增 _CustomSpan、create_execution_span、_create_span_with_custom_id、
clean_plugin_span_outputs 等函数,修复 span 层级关系,支持预生成
plugin_span_id 用于 method span 正确嵌套,精简冗余属性,增加
propagate_attributes 幂等优化。

Made-with: Cursor
更新插件基类 BKFlowBaseService:span 名称增加 .plugin. 命名空间,
_get_trace_context 返回 plugin_span_id,_end_plugin_span 后调用
clean_plugin_span_outputs 清理内部属性,execute/schedule 传递
plugin_span_id 给 method span。

Made-with: Cursor
dengyh and others added 15 commits March 18, 2026 12:10
TaskOperation.start() 使用 create_execution_span 替代原有
get_current_trace_context 方式,创建独立的执行级根 span 并
注入 root_pipeline_data,不再依赖外部 trace context 传递。

Made-with: Cursor
1. 恢复 end_plugin_span/plugin_method_span 中的 plugin.success 属性
2. 对 error_message 增加 [:1000] 截断保护
3. 关键 span 创建失败日志从 debug 提升为 warning
4. _CustomSpan 补充 SDK 兼容性风险注释
5. 旧设计文档标注已被新方案取代

Made-with: Cursor
审批插件的 plugin_schedule 在成功路径上未调用 finish_schedule(),
导致基类 BKFlowBaseService.schedule() 中 is_schedule_finished() 始终
为 False,_end_plugin_span() 从未被触发,bkflow.plugin.approve 父级
Span 缺失,approve.execute 和 approve.schedule 直接暴露在最外层。

Made-with: Cursor
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Code Review Summary

PR 新增了 SpaceConfigAdminViewSet.batch_delete 批量删除空间配置接口,以及对应的 SpaceConfigBatchDeleteSerializer

主要问题

  1. 🚨 关键数据完整性风险batch_delete 直接调用 SpaceConfig.objects.filter(id__in=ids).delete(),绕过了 SpaceConfigManager.delete_space_config() 中对 REF 类型配置的引擎同步逻辑(TaskComponentClient.delete_engine_config),可能导致引擎侧残留孤立配置。
  2. ⚠️ Serializer 缺少 allow_empty=False 校验,空列表会导致无意义的 delete 调用。
  3. ⚠️ 日志信息使用中文,项目规范要求批量/清理操作日志使用英文。

整体思路正确,建议复用 Manager 层的 delete_space_config 逻辑(或新增 batch_delete_space_config 方法)来保证 REF 类型配置的引擎同步一致性。

Comment thread bkflow/space/views.py Outdated
Comment thread bkflow/space/serializers.py Outdated
Comment thread bkflow/space/views.py
try:
SpaceConfig.objects.filter(id__in=ids).delete()
except Exception as e:
err_msg = f"批量删除空间配置失败: {str(e)}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ 项目规范要求清理/批量操作日志使用英文。建议改为 "batch delete space configs failed: {e}"

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Code Review: PR #651

🔒 Security Issue — Space ID Not Validated

bkflow/space/models.py:177 in batch_delete():

instances = self.filter(id__in=inst_ids)

The filter only checks ID but NOT space_id. This allows users to delete configs from other spaces if they know the IDs. Add constraint: .filter(id__in=inst_ids, space_id=space_id)


⚡ Logic Error — Redundant Delete

bkflow/space/models.py:187–190:

instances.delete()  # Line 187 deletes ALL instances
if ref_instances_ids:
    self.filter(id__in=ref_instances_ids).delete()  # Line 190 — already deleted!

Line 187 deletes all instances including REF types. Line 190's second delete is unreachable. Remove line 190 entirely since instances are already gone.


⚠️ Chinese Error Message — Use English

bkflow/space/views.py:453:

err_msg = f"批量删除空间配置失败: {str(e)}"

Project convention requires English log messages. Change to: f"Failed to batch delete space configs: {str(e)}"


🔐 Input Validation Missing

bkflow/space/serializers.py:86–88 SpaceConfigBatchDeleteSerializer:
No validation that IDs belong to the given space_id. Add a validate() method to verify ownership before the model layer processes deletion. This prevents unauthorized access if a user guesses valid config IDs.


✨ Test Coverage

No tests added for batch delete. Consider adding:

  • Test REF vs non-REF config deletion
  • Verify remote API call for REF types
  • Ensure atomic rollback on API failure

Summary: PR follows batch operation pattern correctly (POST method matches batch_delete_tasks), but has a permission gap (space_id not filtered) and logic redundancy (double delete). These should be fixed before merge. Test coverage would strengthen confidence in the refactoring.

# Reviewed, transaction id: 77026
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Code Review Summary

PR 将空间配置恢复默认值的操作改为批量删除,新增 SpaceConfigAdminViewSet.batch_delete 接口和 SpaceConfigBatchDeleteSerializer,并在 Manager 层实现了 batch_delete 方法。

先前评审问题追踪

  • ✅ REF 类型引擎同步:batch_delete 已在 Manager 层正确处理 REF 类型配置的引擎侧同步删除
  • ✅ 空列表校验:Serializer 已添加 min_length=1
  • ✅ 冗余二次删除:已修复,当前仅一次 instances.delete() 调用

当前仍存在的问题

  1. 🔒 space_id 未参与过滤(Critical):batch_deleteself.filter(id__in=inst_ids) 未限制 space_id,用户传入其他空间的配置 ID 可跨空间删除
  2. ⚠️ 中文日志消息(Minor):views.py:453 错误日志使用中文,项目规范要求批量操作日志统一使用英文

其他观察

  • 测试文件变更为标签模块的格式化修正和断言优化,与主功能无关,无问题
  • 使用 POST 方法进行批量删除与项目中 batch_delete_tasks 等现有模式一致
  • 建议为 batch_delete 补充单元测试(REF/非 REF 类型、事务回滚场景)

Comment thread bkflow/space/models.py Outdated
Comment thread bkflow/space/views.py
try:
SpaceConfig.objects.batch_delete(inst_ids=ids, space_id=space_id)
except Exception as e:
err_msg = f"批量删除空间配置失败: {str(e)}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ 项目规范要求批量/清理操作日志使用英文。建议改为 f"batch delete space configs failed: {e}"

# Reviewed, transaction id: 77030
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 23.33333% with 23 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@5351ad2). Learn more about missing BASE report.

Files with missing lines Patch % Lines
bkflow/space/models.py 7.69% 12 Missing ⚠️
bkflow/space/views.py 21.42% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop     #651   +/-   ##
==========================================
  Coverage           ?   82.99%           
==========================================
  Files              ?      272           
  Lines              ?    16112           
  Branches           ?        0           
==========================================
  Hits               ?    13372           
  Misses             ?     2740           
  Partials           ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Code Review Summary

PR 将空间配置恢复默认值的操作改为批量删除,新增 SpaceConfigAdminViewSet.batch_delete 接口、SpaceConfigBatchDeleteSerializer 和 Manager 层 batch_delete 方法。同时修复了测试中的时间竞争问题和标签引用计数断言的顺序依赖。

先前评审问题追踪

问题 状态
REF 类型引擎同步遗漏 ✅ 已解决 — 逻辑已移入 Manager batch_delete
ListField 缺少 min_length ✅ 已解决 — 已添加 min_length=1
space_id 过滤缺失(跨空间删除风险) ✅ 已解决 — 已添加 space_id=space_id
日志应使用英文 ❌ 未修复 — 仍为中文

本轮新发现

  1. models.py:180-184ref_instances_ids 计算在事务外,存在竞态风险
  2. ⚠️ views.py:453 — 日志仍使用中文,不符合项目规范

测试改动

  • test_legacy.py:将 VALID_DATETIME_TEST_CASE 改为动态生成时间,修复模块加载时的时间竞争 — 改动合理
  • test_label_viewset.py:用集合比较替代有序断言 — 改动合理

总体来看,先前的关键问题(REF 同步、跨空间删除)已修复,剩余 2 个 Minor/Important 级别建议。

Comment thread bkflow/space/models.py
inst.id
for inst in instances
if SpaceConfigHandler.get_config(inst.name).value_type == SpaceConfigValueType.REF.value
]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ref_instances_ids 的计算在 transaction.atomic() 之外,存在竞态窗口:在迭代 queryset 和进入事务之间,数据可能被并发请求修改。建议将此列表推导移到 with transaction.atomic(): 块内部,与 delete_space_config 保持一致的事务保护模式。

Comment thread bkflow/space/views.py
try:
SpaceConfig.objects.batch_delete(inst_ids=ids, space_id=space_id)
except Exception as e:
err_msg = f"批量删除空间配置失败: {str(e)}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ 项目规范要求批量/清理操作日志使用英文(此问题先前已反馈但未修复)。建议改为 f"batch delete space configs failed: {e}"

@tencentblueking-adm
Copy link
Copy Markdown

tencentblueking-adm commented Apr 16, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
6 out of 7 committers have signed the CLA.

✅ Mianhuatang8
✅ guohelu
✅ dengyh
✅ RGardenia
✅ Yuikill
✅ wyyalt
❌ noahchi


noahchi seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants