Skip to content

importguard: enforce one-way dependencies between components - #1433

Merged
bootjp merged 2 commits into
mainfrom
feature/import-layering-guard
Nov 12, 2026
Merged

bootjp merged 2 commits into
mainfrom
feature/import-layering-guard

Conversation

@bootjp

@bootjp bootjp commented Nov 12, 2026

Copy link
Copy Markdown
Owner

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.

adapter imported internal/raftengine/etcd only for DeriveNodeID, which
made the protocol layer depend on the concrete Raft backend. The
derivation (FNV-64a of the raft ID) is engine-neutral cluster identity,
so it now lives in internal/raftengine; etcd.DeriveNodeID delegates to
it so the backend and the wiring code keep their call sites.

Golden values pin the derivation, and an etcd test checks both names
agree.

Signed-off-by: bootjp <contact@bootjp.me>
Extend the production import guard (scripts/check-production-imports.sh,
run in go-test.yml) with layer rules in layers.go:

- leaf packages (proto, keyviz, proxy, key codecs, utility and wire
  packages, raftbootstrap, kek) import no module package;
- store, distribution, internal/raftengine, internal/encryption, kv and
  internal/filesystem never import the layers above them (adapter,
  monitoring, proxy, autosplit, admin, adminbridge, serverwiring,
  snapshotoffload), and each also stays below its own consumers;
- monitoring and internal/admin stay off the data plane and adapters;
- internal/raftengine/etcd, internal/serverwiring and
  internal/adminbridge may only be imported at the binary wiring edge.

Every rule holds on main after the previous commit. Rules use
module-relative paths, so they do not depend on where the repository is
checked out, and they cover files excluded by build tags. The guard now
reports every violation in one run instead of stopping at the first.
CLAUDE.md records the convention.

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

coderabbitai Bot commented Nov 12, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 50 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

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: c86284d1-418e-4da4-8828-267bf9f48449
Commits

Reviewing files that changed from the base of the PR and between aa92256 and 805dcfb.

Files selected for processing: 9

  • CLAUDE.md
  • adapter/migration_namespace_activation.go
  • internal/raftengine/etcd/node_id_test.go
  • internal/raftengine/etcd/peers.go
  • internal/raftengine/node_id.go
  • internal/raftengine/node_id_test.go
  • internal/testutil/cmd/importguard/layers.go
  • internal/testutil/cmd/importguard/layers_test.go
  • internal/testutil/cmd/importguard/main.go
  • 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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 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-12T01:58:03.849506Z 805dcfb PR opened
🔒 Security Review ✅ Completed 2026-11-12T01:59:40.086462Z 805dcfb 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 merged commit 15afc79 into main Nov 12, 2026
18 checks passed
@bootjp
bootjp deleted the feature/import-layering-guard branch November 12, 2026 04:20
bootjp added a commit that referenced this pull request Nov 12, 2026
…M4 part 1) (#1436)

Design: docs/design/2026_11_12_partial_root_package_decomposition.md

## Summary

This is milestone M4, part 1, of the root package decomposition. Two
areas leave `package main`:

| From (root) | To | API |
|---|---|---|
| `main_leader_balance.go` (652 lines) + tests |
`internal/leaderbalance` | `Start(ctx, eg, []Group, Options,
newConnCache, done)`, `Group` interface (`GroupID`, `Engine`),
`Options`, `Metrics`/`NewMetrics`, `ParsePinnedGroups`, `Default*` |
| `main_filesystem.go` (494 lines) + tests | `internal/fsmount` |
`Start(ctx, eg, cleanup, shardStore, coordinate, observer, Config)`,
`Config`, `MountService`, `InstallLifecycle`, `MountServer`,
`OverrideServeStopTimeout` |

Neither package reads flags or knows about `*raftGroupRuntime`:
- **leaderbalance** sees groups through a two-method `Group` interface.
`main` adapts its runtimes and drops nil entries, so no typed-nil
interface reaches the scheduler. The gRPC connection cache comes from a
factory, and as before it is created only when the scheduler runs.
- **fsmount** takes a resolved `Config`. Capacity, max files and the
collector intervals were previously read from flags inside the start
function. They are now `Config` fields that `main` fills in.
`filesystemStartConfigFromFlags` stays in `main` and returns the same
values as before.

The design doc gains a Progress section and one decision: extracted
areas declare narrow inputs instead of waiting for `raftGroupRuntime` to
move. `raftGroupRuntime` embeds bootstrap types, so moving it first
would drag the bootstrap area along with it. Snapshot-offload wiring is
deferred until after M2, because it reads about 20 flags directly.

## Behavior change

None. Function bodies are unchanged apart from two things: flag
dereferences became config fields, and some identifiers were renamed so
they could be exported.

## Risk

Low to medium. The leader-balance and FUSE start-up paths are rewired,
but their logic is identical. The two signal-shutdown kernel-mount tests
stay in `main` and now drive the exported `fsmount` API. The serve-stop
timeout knob is now `OverrideServeStopTimeout`, which returns a restore
func; before, another package wrote the package variable directly.

## Test evidence

- `go build ./...`, `go vet . ./internal/leaderbalance
./internal/fsmount`
- `golangci-lint run . ./internal/leaderbalance/ ./internal/fsmount/` →
0 issues
- `go test -race -count=1 ./internal/leaderbalance/ ./internal/fsmount/
./internal/testutil/...` → ok
- `go test -race -count=1 .` → ok, after rebasing onto `main`
- `importguard` (the layer rules from #1433) → passes
- The set of `func Test*` names is unchanged: tests moved, none were
lost.
- Jepsen was not run: no Raft, MVCC, OCC or HLC path changed. Leader
balancing uses the same gated transfer RPCs.

## Self-review

1. Data loss: no storage or apply path was touched. The filesystem
unmount/cleanup ordering is unchanged, and its lifecycle tests moved
intact.
2. Concurrency / distributed failures: leaderbalance reads engines
through `Engine()`, which takes the same `snapshotEngine()` lock as
before. Nil runtimes are filtered out before the interface conversion.
The shutdown join and cancellation tests moved and pass.
3. Performance: no hot path changed. The only addition is one interface
call per group per balance tick.
4. Data consistency: no change.
5. Test coverage: every test of the moved code moved with it. The root
keeps the flag-parsing tests and the signal-shutdown integration tests.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **改善**
  * ファイルシステムの起動時に設定を検証し、不正な設定をエラーとして通知するようになりました。
  * 起動時にリーダーを検出できない場合や期限切れの場合、一定時間再試行するようになりました。
* **ドキュメント**
  * ファイルシステム関連機能の移行状況と設計方針を更新しました。

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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