Skip to content

fix(repository): reduce scheduler cache LastUsedAt write amplification#1735

Open
atoz03 wants to merge 2 commits intoWei-Shaw:mainfrom
atoz03:codex/issue-1723
Open

fix(repository): reduce scheduler cache LastUsedAt write amplification#1735
atoz03 wants to merge 2 commits intoWei-Shaw:mainfrom
atoz03:codex/issue-1723

Conversation

@atoz03
Copy link
Copy Markdown

@atoz03 atoz03 commented Apr 18, 2026

关联问题

背景

scheduler_cache.UpdateLastUsed 在热路径会反序列化并重写整块账号 JSON(sched:acc:*sched:meta:*),导致明显写放大。

变更内容

  • LastUsedAt 拆为独立 side key:sched:acc:last_used:<id>
  • UpdateLastUsed 仅更新 side key,不再重写账号/元数据 JSON。
  • 在读取路径补齐覆盖逻辑:
    • GetAccount 读取主账号后,用 side key 覆盖 LastUsedAt
    • GetSnapshot 读取 metadata 后,用 side key 覆盖 LastUsedAt
  • 在写入/删除路径保持一致性:
    • SetAccount / SetSnapshot(经 writeAccounts)同步 side key。
    • DeleteAccount 同时删除 side key。
  • 修复 CI lint 报错:将仅 integration 测试使用的 ptrTime helper 下沉到 idempotency_repo_integration_test.go,避免生产代码 unused

验证

  • cd backend && go test ./internal/repository -count=1
  • cd backend && golangci-lint run ./internal/repository/...
  • cd backend && go test -tags=integration ./internal/repository -run "TestSchedulerCacheSnapshotUsesSlimMetadataButKeepsFullAccount|TestSchedulerCacheUpdateLastUsedUsesSideKeyAndKeepsAccountJSON" -count=1

上述命令本地已通过。

atoz03 added 2 commits April 18, 2026 14:34
Split LastUsedAt into a dedicated Redis side key (sched:acc:last_used:<id>) to avoid rewriting full account JSON on every hot-path touch.

What changed:

- Update UpdateLastUsed to write only the side key instead of MGET/decode/re-marshal/full SET.

- Merge side-key value back into accounts in GetAccount and GetSnapshot.

- Keep side key in sync in SetAccount/SetSnapshot and remove it in DeleteAccount.

- Add integration coverage proving account JSON payload remains unchanged while LastUsedAt updates still take effect.
Move ptrTime out of production scheduler cache code into integration test scope where it is actually used.

This keeps issue-1723 behavior unchanged while eliminating the CI lint failure: func ptrTime is unused.

Validation performed:

- golangci-lint v2.9.0 run ./internal/repository/... -> 0 issues

- go test ./internal/repository -count=1 -> ok
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.

[Performance] UpdateLastUsed 写放大导致 Redis 大量不必要流量(单部署实测 1.85 TB / 6.5 天)

1 participant