Skip to content

perf(api): バッチ化・並列化で逐次クエリ/逐次 await を解消 - #1005

Merged
otomatty merged 2 commits into
developfrom
claude/github-issue-1001-mBbii
Jun 7, 2026
Merged

perf(api): バッチ化・並列化で逐次クエリ/逐次 await を解消#1005
otomatty merged 2 commits into
developfrom
claude/github-issue-1001-mBbii

Conversation

@otomatty

@otomatty otomatty commented Jun 5, 2026

Copy link
Copy Markdown
Owner

タイトルリネーム伝播 (propagateTitleRename) の参照元ページ書き換えを、
最大 8 件 (SOURCE_REWRITE_CONCURRENCY) の Promise.allSettled バッチで
並列化する。各 source は独立した page_contents 行をロックするため順序
非依存で、ロック競合・デッドロックは発生しない。失敗は従来どおりベスト
エフォートでカウント・ログし、後続 source / ghost 昇格を止めない。

AI モデル同期 (syncOneProvider) の新規モデル投入を、行ごとの逐次 INSERT
から単一マルチバリュー INSERT に変更 (N+1 → 1 クエリ)。sortOrder は max+1
から index で事前採番する。表示順 (orderBy) にしか使わないため、稀な
onConflict スキップで生じるギャップは無害。

並列パスの回帰防止テストを追加 (順序非依存の意味ベース DB モック):
ベストエフォート集計・同時実行数 8 のバウンド検証。

Refs #1001

Summary by CodeRabbit

  • Tests

    • Added unit tests verifying concurrent title-rename behavior and enforcement of parallelism limits.
    • Improved concurrency test utilities to make task-ordering and microtask boundaries deterministic.
  • Performance

    • Optimized AI model sync with batched inserts to reduce per-item work.
    • Title-rename processing now uses bounded parallelism with improved success/failure accounting and resilient error handling.

タイトルリネーム伝播 (`propagateTitleRename`) の参照元ページ書き換えを、
最大 8 件 (`SOURCE_REWRITE_CONCURRENCY`) の `Promise.allSettled` バッチで
並列化する。各 source は独立した `page_contents` 行をロックするため順序
非依存で、ロック競合・デッドロックは発生しない。失敗は従来どおりベスト
エフォートでカウント・ログし、後続 source / ghost 昇格を止めない。

AI モデル同期 (`syncOneProvider`) の新規モデル投入を、行ごとの逐次 INSERT
から単一マルチバリュー INSERT に変更 (N+1 → 1 クエリ)。sortOrder は max+1
から index で事前採番する。表示順 (orderBy) にしか使わないため、稀な
onConflict スキップで生じるギャップは無害。

並列パスの回帰防止テストを追加 (順序非依存の意味ベース DB モック):
ベストエフォート集計・同時実行数 8 のバウンド検証。

Refs #1001
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cf73ae5a-0bc2-4274-8199-016e1221c5e1

📥 Commits

Reviewing files that changed from the base of the PR and between d7ed16b and b222dfd.

📒 Files selected for processing (2)
  • server/api/src/__tests__/services/titleRenamePropagationService.test.ts
  • server/api/src/services/titleRenamePropagationService.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • server/api/src/tests/services/titleRenamePropagationService.test.ts
  • server/api/src/services/titleRenamePropagationService.ts

📝 Walkthrough

Walkthrough

This PR implements two independent performance optimizations: source-page rewrites in title-rename propagation now execute with bounded parallelism via chunked batching, and AI model insertion switches from per-row loops to single multi-value statements. Tests verify parallel failure tolerance and concurrency limits.

Changes

Title Rename Propagation with Bounded Parallelism

Layer / File(s) Summary
Concurrency constant and source-page wrapper
server/api/src/services/titleRenamePropagationService.ts
SOURCE_REWRITE_CONCURRENCY caps concurrent source-page rewrites, and rewriteAndInvalidateSourcePage wraps per-page rewrite with optional Hocuspocus cache invalidation that logs but suppresses invalidation errors.
Batched parallel propagateTitleRename loop
server/api/src/services/titleRenamePropagationService.ts
propagateTitleRename replaces sequential per-source rewrites with Promise.allSettled over SOURCE_REWRITE_CONCURRENCY-sized batches, aggregating success/failure counters and continuing to ghost-link promotion phase.
Test utilities and parallel concurrency tests
server/api/src/__tests__/services/titleRenamePropagationService.test.ts
Order-independent DB mock (createParallelRenameDb) and microtask-flush utility enable two new tests: one verifies best-effort parallel rewriting tolerates per-source FOR UPDATE failures while aggregating counts; the other asserts concurrency is bounded by SOURCE_REWRITE_CONCURRENCY.

AI Model Batch Insert Optimization

Layer / File(s) Summary
Batched multi-value insert in syncOneProvider
server/api/src/services/syncAiModels.ts
syncOneProvider now computes nextSortOrder once, filters existing IDs upfront, and inserts all new rows in a single batch with per-row sortOrder offsets, using onConflictDoNothing with returning to set upserted count.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related issues

Possibly related PRs

  • otomatty/zedi#736: The batched parallel rewrite flow in titleRenamePropagationService builds on the propagateTitleRename implementation introduced in this PR.

Poem

🐰 I hop in chunks and split my load,

rewrites race but stay within the road.
Batch the seeds, one planted row—
fewer trips, more carrots grow.
Hooray for rabbits, fast and bold!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 55.56% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main performance improvements: eliminating sequential queries and awaits through batching and parallelization.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/github-issue-1001-mBbii

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes AI model synchronization by batching inserts and parallelizes source page rewriting during title rename propagation in bounded batches. The feedback recommends reducing the concurrency limit from 8 to 4 to avoid database connection pool exhaustion and transaction propagation issues, along with updating the corresponding test suite to reflect this change.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread server/api/src/services/titleRenamePropagationService.ts Outdated
Comment thread server/api/src/__tests__/services/titleRenamePropagationService.test.ts Outdated
fire-and-forget な伝播では同時リネームで使用接続数が重畳する
(N 件同時 → 最大 4N 接続)。DB プール上限 max:20 を枯渇させて
無関係な API を巻き込まないよう、保守的に 4 へ。バウンド検証テストも
4 ベース(チャンク 4/4/2)に更新。

レビュー指摘 (gemini-code-assist) への対応。

Refs #1001
@otomatty
otomatty merged commit d475df5 into develop Jun 7, 2026
17 of 18 checks passed
@otomatty
otomatty deleted the claude/github-issue-1001-mBbii branch June 7, 2026 02:27
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.

2 participants