Skip to content

refactor: decompose the root package (design + M1 admin bridges) - #1432

Merged
bootjp merged 2 commits into
mainfrom
design/root-package-decomposition
Nov 12, 2026
Merged

bootjp merged 2 commits into
mainfrom
design/root-package-decomposition

Conversation

@bootjp

@bootjp bootjp commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Design: docs/design/2026_11_12_proposed_root_package_decomposition.md

Stacked on #1431 (base is its branch; retarget to main after it merges).

Summary

#1431 split main.go into per-area files, but they all still share package main (~25k lines of non-test code, ~28k lines of tests). This PR addresses the structural causes.

  1. Design doc (first commit, reviewable on its own). The dependency survey found three root causes: 92 flag globals dereferenced deep inside helpers; the shared runtime types (raftGroupRuntime, groupSpec, raftBootstrapConfig, runtimeConfig) live in main, so no area can leave it; and even leaf code has never been extracted. The doc proposes six behavior-neutral milestones: M1 admin bridges → M2 typed flag config → M3 internal/noderuntime → M4 mid-sized areas → M5 encryption wiring → M6 bootstrap CLI.
  2. M1: move the SQS/S3/DynamoDB admin bridges, convertAdminPrincipal / isLeaderChurnError, and the keyviz source/fan-out builders into the new internal/adminbridge package. Only constructors are exported; the bridge types and error translators stay unexported. Their tests move with them. Comments that pointed at main_admin.go for these bridges now point at the new package.

Behavior change

None. Function bodies are unchanged; only identifiers were exported and call sites qualified.

Risk

Low. M1 is leaf code (it depends only on adapter, internal/admin, keyviz) and introduces no import cycle. The design doc stays proposed and will be renamed to partial in a follow-up once this merges.

Test evidence

  • go build ./..., go vet . ./internal/adminbridge
  • golangci-lint run . ./internal/adminbridge/... ./internal/admin/... ./adapter/... → 0 issues
  • go test -race -count=1 ./internal/adminbridge/ ./internal/admin/ → ok
  • go test -race -count=1 . → ok github.com/bootjp/elastickv 228.767s
  • The set of func Test* names across the repository is identical before and after (no test lost in the move).
  • Jepsen not run: no runtime path or start-up order changed (per the doc's verification section, Jepsen starts at M3).

Self-review

  1. Data loss: no logic changed.
  2. Concurrency / distributed failures: no change; the bridges are stateless wrappers.
  3. Performance: no change (same calls, one extra package boundary at construction time only).
  4. Data consistency: no change; error mapping to admin.Err*NotLeader is covered by the moved translate tests.
  5. Test coverage: every test covering the moved code moved with it and passes in the new package.

Summary by CodeRabbit

  • 改善
    • 管理画面のテーブル、キュー、オブジェクト操作で、リーダー変更に関連するエラーが適切に分類されるようになりました。
  • ドキュメント
    • サーバー構成の分割案と、管理機能の構成に関する説明を追加・更新しました。
  • テスト
    • 管理操作のエラー変換や、停止中のサービスへの要求に関する確認を追加しました。

Signed-off-by: bootjp <contact@bootjp.me>
Milestone 1 of the root package decomposition. The SQS, S3 and DynamoDB
bridges that convert adapter admin DTOs to internal/admin sources, the
shared principal / leader-churn helpers, and the keyviz source and
fan-out builders move out of package main into internal/adminbridge.

Constructors are exported (NewSQSQueuesSource, NewBucketsSource,
NewDynamoTablesSource, KeyVizSource, KeyVizHotKeysSource,
NewKeyVizFanout, NewKeyVizHotKeysFanout, KeyVizFanoutConfig); the
bridge types and error translators stay unexported. Function bodies are
unchanged. The tests that cover the bridges move with them, and
comments that pointed at main_admin.go for the bridges now point at the
new package. parseCSV stays in main_admin.go because the admin role
flags use it.

Signed-off-by: bootjp <contact@bootjp.me>
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ba8a4c45-ea0d-42ca-948e-a80bc322bdf2
Commits

Reviewing files that changed from the base of the PR and between 311f752 and 311f752.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

管理DTO変換、管理データソース生成、KeyVizソースとファンアウト生成をinternal/adminbridgeへ移しました。サーバー側の呼び出しとテストを更新し、ルートmainパッケージの分割計画を設計文書に追加しました。

Changes: 管理ブリッジの分離と配線

Layer / File(s) Summary
ブリッジAPIとエラー変換テスト
internal/adminbridge/*.go, internal/adminbridge/*_test.go
principal変換とleader churn判定をadminbridgeに追加しました。テーブル、バケット、キューのソース生成関数とKeyViz関連の型・関数を同パッケージから公開します。エラー変換などのテストを追加し、一部の既存テストを同パッケージへ移しました。
サーバー配線と分割計画の更新
main_admin.go, main_server_runner.go, main_admin*_test.go, adapter/*_admin.go, internal/admin/*.go, CLAUDE.md, docs/design/2026_11_12_proposed_root_package_decomposition.md
管理サーバーとAdminForwardのソース生成をadminbridgeのAPIに切り替えました。関連テストと参照コメントを更新しました。ルートmainパッケージの分割方針、目標レイアウト、移行段階、検証手順を設計文書に記載しました。

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Merge Risk: Minimal · up to 311f7

This is a behavior-neutral refactor that moves admin bridge code into its own package, with call sites and tests updated to match. No actionable merge-blocking issue remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed タイトル「refactor: decompose the root package (design + M1 admin bridges)」は、PR の主要な変更を具体的に示しています。変更内容は、ルートパッケージ分割の設計文書の追加と、M1 として SQS・S3・DynamoDB の管理ブリッジおよび keyviz のソース・ファンアウト生成を internal/adminbridge へ移…
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.
Full details: Title check

Explanation

タイトル「refactor: decompose the root package (design + M1 admin bridges)」は、PR の主要な変更を具体的に示しています。変更内容は、ルートパッケージ分割の設計文書の追加と、M1 として SQS・S3・DynamoDB の管理ブリッジおよび keyviz のソース・ファンアウト生成を internal/adminbridge へ移すことです。diff のサマリーでも、internal/adminbridge の新規追加、既存ファイルのパッケージ変更、参照コメントの更新が確認できます。タイトルは英語で 64 文字・10 語です。PR の目的を十分に表しています。

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e82d41f3-1212-4f29-b2a2-67cdb624d488
Commits

Reviewing files that changed from the base of the PR and between 0a2d5de and 311f752.

Files selected for processing: 27

  • CLAUDE.md
  • adapter/dynamodb_admin.go
  • adapter/s3_admin.go
  • docs/design/2026_11_12_proposed_root_package_decomposition.md
  • internal/admin/buckets_source.go
  • internal/admin/dynamo_handler.go
  • internal/admin/dynamo_items_handler.go
  • internal/admin/forward_server.go
  • internal/admin/keyviz_hotkeys_fanout.go
  • internal/admin/s3_handler.go
  • internal/admin/s3_objects_handler.go
  • internal/admin/sqs_handler.go
  • internal/admin/sqs_handler_metrics_test.go
  • internal/adminbridge/common.go
  • internal/adminbridge/doc.go
  • internal/adminbridge/dynamo.go
  • internal/adminbridge/dynamo_stopping_test.go
  • internal/adminbridge/keyviz.go
  • internal/adminbridge/s3.go
  • internal/adminbridge/s3_stopping_test.go
  • internal/adminbridge/sqs.go
  • internal/adminbridge/sqs_observer_test.go
  • internal/adminbridge/translate_test.go
  • main_admin.go
  • main_admin_lifecycle_test.go
  • main_admin_test.go
  • main_server_runner.go
Files with no reviewable changes: 1

  • main_admin_lifecycle_test.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T15:28:34.378792Z 311f752 PR opened
🔒 Security Review ✅ Completed 2026-11-11T15:30:27.190496Z 311f752 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@bootjp

bootjp commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Base automatically changed from feature/main-file-refactor-f424e9 to main November 12, 2026 04:17
@bootjp
bootjp merged commit 9d49d00 into main Nov 12, 2026
13 checks passed
@bootjp
bootjp deleted the design/root-package-decomposition branch November 12, 2026 04:19
bootjp added a commit that referenced this pull request Nov 12, 2026
## Summary

This PR makes the dependency direction between components a CI-enforced
rule.

1. **`raftengine: own DeriveNodeID`**: the protocol layer `adapter`
imported the concrete Raft backend `internal/raftengine/etcd` only for
`DeriveNodeID`. `DeriveNodeID` (FNV-64a of the raft ID) is
engine-neutral cluster identity, so it now lives in
`internal/raftengine`. `etcd.DeriveNodeID` delegates to it, so existing
callers are unchanged. `adapter` now depends only on
`internal/raftengine`.
2. **`importguard` layer rules**
(`internal/testutil/cmd/importguard/layers.go`), run by the existing
`scripts/check-production-imports.sh` in `go-test.yml`:
- Leaf packages (`proto`, `keyviz`, `proxy`, key codecs, utility and
wire packages, `raftbootstrap`, `kek`) import no module package.
- `store` → `distribution` / `internal/raftengine` /
`internal/encryption` → `kv` → `internal/filesystem`: none of these
imports the layers above it (`adapter`, `monitoring`, `proxy`,
`distribution/autosplit`, `internal/admin`, `internal/adminbridge`,
`internal/serverwiring`, `internal/snapshotoffload`), and each lower
layer stays below its consumers.
- `monitoring` and `internal/admin` do not reach into the data plane or
adapters. This matches the existing comments saying `internal/admin`
stays free of `adapter`.
- `internal/raftengine/etcd`, `internal/serverwiring` and
`internal/adminbridge` may only be imported from the root binary and
`cmd/`. The etcd backend is additionally importable from inside
`internal/raftengine` and from `internal/snapshotoffload`.
- The guard now reports every violation in one run instead of stopping
at the first.

The rules use module-relative paths, so they do not depend on where the
repo is checked out, and they also cover files excluded by build tags.
depguard was not used because its globs match absolute paths, so a rule
like `**/kv/**` would misfire whenever a parent directory is named `kv`.
CLAUDE.md now documents the convention.

## Behavior change

None. `DeriveNodeID` keeps the same implementation and results; only
where it is defined changed. The second commit is a CI/tooling change.

## Risk

Low. If someone later needs an upward dependency, CI fails. The intended
fix is to invert the dependency, not to relax the rule (documented in
CLAUDE.md and `layers.go`).

Note for #1432: its branch predates this fix, so after a rebase it will
pass. `internal/adminbridge` itself already satisfies the rules.

## Test evidence

- `bash scripts/check-production-imports.sh` → passes. Before the first
commit it fails with: `adapter/migration_namespace_activation.go:8:2:
layer violation: internal/raftengine/etcd may only be imported from .,
cmd, internal/raftengine, internal/snapshotoffload, not from adapter`
- `go test -race ./internal/testutil/...` (new `TestCheckLayering`
table, `TestModuleDir`, `TestCheckTreeReportsEveryLayerViolation`)
- `go test ./internal/raftengine/ ./internal/raftengine/etcd/ -run
DeriveNodeID` (golden values pinning the derivation, plus a check that
both names agree)
- `go test -race -run 'Namespace|Migration' ./adapter/` → ok
- `golangci-lint run` on the touched packages → 0 issues
- Jepsen not run: no runtime change.

## Self-review

1. Data loss: none. The node ID derivation is unchanged and pinned by
golden values (it is persisted identity).
2. Concurrency / distributed failures: no runtime change.
3. Performance: none (one extra function call, inlined).
4. Data consistency: node IDs in ConfState, peer files and nonce
prefixes are unchanged; the golden test guards against drift.
5. Test coverage: the new rules and the moved function both have tests,
and the guard's own tests cover allow and deny cases plus reporting of
multiple violations.
bootjp added a commit that referenced this pull request Nov 12, 2026
M1 (internal/adminbridge) shipped in #1432, so the design doc moves to the partial lifecycle state; CLAUDE.md follows the rename.

Signed-off-by: bootjp <contact@bootjp.me>
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.

1 participant