Skip to content

fix(watch): 避免资源移动期间持有全局锁 - #3833

Merged
qin-ctx merged 8 commits into
mainfrom
codex/fix-watch-move-global-lock
Aug 10, 2026
Merged

qin-ctx merged 8 commits into
mainfrom
codex/fix-watch-move-global-lock

Conversation

@KCHENPENGFEI

@KCHENPENGFEI KCHENPENGFEI commented Aug 6, 2026 •

Copy link
Copy Markdown
Collaborator

背景

WatchManager.sync_tasks_with_resource_move_internal() 原先在整个 viking_fs.mv 调用期间持有 WatchManager 全局锁。资源移动耗时较长时,与 source/target 完全无关的 watch 查询、创建和调度也会被串行阻塞。

同时,直接把 mv、回滚等业务语义继续下沉到 WatchManager,会让通用 watch 管理组件与文件系统操作耦合。

方案

新增进程内 UriMutationCoordinator,只负责按 account_id + URI 范围 协调并发,不感知 mv、watch 或 add-resource 等业务语义:

  • access:操作期间要求 URI 保持稳定;多个 access 可以并发。
  • mutation:正在修改 URI;与相交路径的 access/mutation 互斥。
  • 不同账号或不相交路径互不阻塞。
  • URI 重叠按路径 segment 判断,wiki 与 wiki/page 相交,和 wiki-old 不相交。

改动内容

FSService 负责资源移动事务

FSService.mv() 在 source + target 的 mutation lease 内执行:

  1. 调用 WatchManager 校验 watch target 前缀改写是否冲突。
  2. 不持有 WatchManager 全局锁执行 viking_fs.mv(source, target)。
  3. 调用 WatchManager 原子改写 watch target 并保存 .watch_tasks.json。
  4. watch 提交失败时执行 viking_fs.mv(target, source) 回滚。

正向移动失败时不会改写 watch;回滚再次失败时抛出 rollback error,并将原 watch commit error 保留为 cause。

事务运行在独立 task 中并使用 asyncio.shield。调用方取消时,会先等待事务完成提交或回滚、释放 mutation lease,再向调用方返回 CancelledError,避免留下“资源已移动但 watch 未更新”的中间状态。

WatchManager 只保留 watch 语义

删除 WatchManager 中的资源移动 callback、回滚 callback 和 move 状态,改为两个通用的 watch 原语:

  • validate_target_prefix_rewrite_internal():只校验改写冲突。
  • rewrite_target_prefix_internal():原子更新 task、URI 索引和持久化文件;保存失败时恢复内存状态。

watch CRUD/query 在访问具体 target URI 时使用 access lease。通过 task ID 操作时采用“读取 URI 快照 -> 获取 lease -> 重新校验”的方式,避免按旧 URI 获取 lease 后继续处理已迁移的 task。

WatchScheduler 在稳定 URI 上执行刷新

due task 在等待 semaphore 期间可能被移动,因此执行前会:

  1. 获取候选 target URI 的 access lease。
  2. 重新读取当前 task。
  3. URI 已变化时按新 URI 重试。
  4. URI 稳定后,在整个 refresh 期间持有 access lease。

这样,同路径 mv 与 watch refresh 继续串行,无关路径仍可并发。

Core 共享同一协调器

Core 创建唯一的 UriMutationCoordinator,注入 FSService、WatchScheduler 和 WatchManager,确保 mutation/access 位于同一个协调域。

行为边界

  • 不修改 resource import 或 watch 的外部协议。
  • 不修改 .watch_tasks.json 的单文件格式和串行保存逻辑。
  • 不修改 VikingFS 已有的 source/target 路径锁。
  • get_due_tasks() 不持有所有 URI 的 lease,执行前由 scheduler 重新校验。
  • get_all_tasks() 保持轻量查询;资源移动事务期间可能短暂返回旧 target URI,事务结束后恢复一致。
  • 协调器为进程内机制,与原 WatchManager 锁的作用域一致。

测试

相关测试共 96 个通过,覆盖:

  • source、target、父路径、子路径的相交判断和 segment 边界。
  • 不同 account 隔离及无关路径并发。
  • access/mutation 互斥、嵌套 access 和取消后的 lease 释放。
  • 正向 mv 失败、watch 提交失败、反向回滚及回滚再次失败。
  • 调用方在正向移动、watch 提交和回滚阶段取消。
  • scheduler 候选 URI 过期及 refresh/mv 协调。
  • WatchManager 前缀改写、URI 索引和持久化失败恢复。

格式、Ruff 和 Python compileall 检查通过。

完整 pytest tests 在收集阶段被仓库现有可选测试依赖阻断:当前环境缺少 pytest-html、psutil 和 google.genai;相关测试集没有失败。

chenpengfei added 6 commits August 10, 2026 15:56
Allow unrelated watch operations to continue while viking_fs.mv is running, while serializing overlapping resource paths with a move fence. Preserve transaction integrity across persistence failures, rollback, and caller cancellation.
@KCHENPENGFEI
KCHENPENGFEI force-pushed the codex/fix-watch-move-global-lock branch from 1553be9 to af7de88 Compare August 10, 2026 08:04
@qin-ctx
qin-ctx merged commit 8acaf7f into main Aug 10, 2026
6 checks passed
@qin-ctx
qin-ctx deleted the codex/fix-watch-move-global-lock branch August 10, 2026 08:39
@github-project-automation github-project-automation Bot moved this from Backlog to Done in OpenViking project Aug 10, 2026
skloxo pushed a commit to skloxo/OpenVikingStudio that referenced this pull request Aug 22, 2026
* fix(watch): avoid global lock during resource moves

Allow unrelated watch operations to continue while viking_fs.mv is running, while serializing overlapping resource paths with a move fence. Preserve transaction integrity across persistence failures, rollback, and caller cancellation.

* feat(resource): add URI mutation coordinator

* refactor(watch): separate target rewrites from resource moves

* refactor(fs): own resource move watch transaction

* feat(watch): coordinate refreshes with URI mutations

* refactor(core): share URI mutation coordinator

* test(watch): focus URI move coverage

* test(watch): reuse existing URI move coverage

---------

Co-authored-by: chenpengfei <chenpengfei@bytedance.com>
Co-authored-by: qin-ctx <qinhaojie.exe@bytedance.com>
(cherry picked from commit 8acaf7f)
frankyang2008-eng added a commit to frankyang2008-eng/OpenViking that referenced this pull request Oct 9, 2026
Sync upstream main (e546b79) - 19 commits, 315 files:

- refactor(client): remove Python embedded mode (volcengine#3712)
- perf(queue): stream semantic vectorization tasks (volcengine#3636)
- perf(queuefs): defer embedding content materialization (volcengine#3871)
- feat(memory): support event tag filtering (volcengine#3850)
- feat(pathlock): keep lease alive across handoff (volcengine#3903)
- feat(server): refresh read-replica API key index via store watcher (volcengine#3857)
- feat(agent-evolution): add trajectory date filters (volcengine#3915)
- feat(agent-evolution): add trajectory task query (volcengine#3856)
- feat(web-studio): add account deletion workflow (volcengine#3912)
- feat(web-studio): add scheduled resource sync management (volcengine#3909)
- fix(watch): avoid global lock during resource moves (volcengine#3833)
- fix(observer): ignore historical lock diagnostics in health (volcengine#3920)
- fix(session): validate archive names when loading history (volcengine#3922)
- fix(task-tracker): delete expired persisted tasks (volcengine#3913)
- fix(storage): exclude zero-byte files from index expectations (volcengine#3904)
- fix(semantic): reduce entity hallucinations in summaries (volcengine#3908)
- fix(retrieval): record first-turn session recalls (volcengine#3907)
- fix(opencode-plugin): serialize saveState to prevent ENOENT race (volcengine#3885)
- fix(examples): align quick start with server mode (volcengine#3923)

Reviewed: zero conflicts, all local dev-branch modifications preserved,
no references to deleted embedded client modules, config backward-compatible.
frankyang2008-eng pushed a commit to frankyang2008-eng/OpenViking that referenced this pull request Oct 9, 2026
* fix(watch): avoid global lock during resource moves

Allow unrelated watch operations to continue while viking_fs.mv is running, while serializing overlapping resource paths with a move fence. Preserve transaction integrity across persistence failures, rollback, and caller cancellation.

* feat(resource): add URI mutation coordinator

* refactor(watch): separate target rewrites from resource moves

* refactor(fs): own resource move watch transaction

* feat(watch): coordinate refreshes with URI mutations

* refactor(core): share URI mutation coordinator

* test(watch): focus URI move coverage

* test(watch): reuse existing URI move coverage

---------

Co-authored-by: chenpengfei <chenpengfei@bytedance.com>
Co-authored-by: qin-ctx <qinhaojie.exe@bytedance.com>
skloxo pushed a commit to skloxo/OpenVikingStudio that referenced this pull request Nov 9, 2026
* fix(watch): avoid global lock during resource moves

Allow unrelated watch operations to continue while viking_fs.mv is running, while serializing overlapping resource paths with a move fence. Preserve transaction integrity across persistence failures, rollback, and caller cancellation.

* feat(resource): add URI mutation coordinator

* refactor(watch): separate target rewrites from resource moves

* refactor(fs): own resource move watch transaction

* feat(watch): coordinate refreshes with URI mutations

* refactor(core): share URI mutation coordinator

* test(watch): focus URI move coverage

* test(watch): reuse existing URI move coverage

---------

Co-authored-by: chenpengfei <chenpengfei@bytedance.com>
Co-authored-by: qin-ctx <qinhaojie.exe@bytedance.com>
(cherry picked from commit 8acaf7f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants