Skip to content

refactor: split daemon_ha_userspace.go by concern (#4659)#4685

Merged
psaab merged 1 commit into
masterfrom
refactor/4659-ha-userspace-split
Jul 8, 2026
Merged

refactor: split daemon_ha_userspace.go by concern (#4659)#4685
psaab merged 1 commit into
masterfrom
refactor/4659-ha-userspace-split

Conversation

@psaab

@psaab psaab commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Summary

Closes #4659. Splits pkg/daemon/daemon_ha_userspace.go (1123 LOC) by
concern into four sibling files in package daemon. codex-review-174 #17
flagged the file as a review hazard: ACK/withhold and demotion-barrier
logic (a class-B HA control path) was interleaved with endian/NAT
wire-conversion helpers.

PURE code motion — behavior-identical. Every top-level declaration is
moved verbatim; no logic changed.

New file layout

File Decls Concern
daemon_ha_userspace_convert.go 16 wire delta↔session conversion (buildZoneIDs, endian/NAT/MAC helpers, userspaceSessionFromDeltaV4/V6, forward-wire alias/key builders)
daemon_ha_userspace_stream.go 11 delta stream / drain / queue (drainer + event-stream interfaces, shouldSyncUserspaceDelta, the sync/event-stream loops, queueUserspaceSessionDeltas, drainUserspaceSessionDeltasWithConfig)
daemon_ha_userspace_export.go 4 bulk session export (exporter interfaces, primaryOwnerRGIDs, exportUserspaceOwnerRGSessionsWithConfig)
daemon_ha_userspace_readiness.go 18 takeover/demotion readiness + failover barriers (readiness/mismatch interfaces, demotion-prep suppression window, prepareUserspaceRGDemotionWithTimeout barrier, manual-failover wrappers, checkUserspaceTakeoverReadiness)
daemon_ha_userspace.go (shell) 3 shared: userspaceXSKBindingController, buildZoneRGMap, rgHasRETH

Load-bearing invariants preserved byte-identical

  • delta-slice behavior
  • handleEventStreamDelta ACK/withhold contract (returns false only for transient readiness gaps so the helper replays)
  • userspaceDeltaSyncMu (delta drain) vs userspaceDemotionPrepMu (demotion suppression window) lock separation — the split moves methods, not the lock discipline
  • failover peer-barrier logic

Same package, so all unexported symbols stay reachable (buildZoneIDs,
the exporter interfaces used by daemon_ha_sync.go, and
buildZoneRGMap/rgHasRETH used by daemon_apply.go/daemon_ha_vip.go
and tests).

Validation

  • go/parser decl-diff over the union of new files vs origin/master: 52 top-level declarations, identical=52, 0 bodydiff / 0 missing / 0 extra (import lists differ per file as expected, excluded from diff)
  • go build ./... clean
  • go vet ./pkg/daemon/... clean
  • go test ./pkg/daemon/... green — the userspace-sync + HA-sync semantic tests are the gate; all pass
  • gofmt -l clean

No design/doc change: pkg/daemon/README.md has no per-file layout entry
for this file (it documents behavior, not a file index) and the split is
behavior-identical.

🤖 Generated with Claude Code

https://claude.ai/code/session_015oARShYtiJJ2H4UB4nXGqi

pkg/daemon/daemon_ha_userspace.go had grown to 1123 LOC interleaving
four distinct concerns of the userspace-dataplane HA control path: wire
delta<->session conversion, the delta stream/drain/queue loop, bulk
session export, and takeover/demotion readiness plus failover barriers.
codex-review-174 #17 flagged the mix as a real review hazard — the
ACK/withhold and demotion-barrier logic sits next to endian/NAT
conversion helpers, so a reviewer scanning the barrier path has to wade
through unrelated byte-order code.

This is a class-B HA control path, so the split is PURE code motion:
every top-level declaration is moved verbatim, byte-for-byte, with no
behavior change. Concerns are grouped into four sibling files in package
daemon:

  - daemon_ha_userspace_convert.go (16 decls): buildZoneIDs, the
    endian/NAT-port/MAC helpers, userspaceSessionFromDeltaV4/V6, and the
    forward-wire alias/key builders.
  - daemon_ha_userspace_stream.go (11 decls): the drainer and
    event-stream provider interfaces, shouldSyncUserspaceDelta,
    syncUserspaceSessionDeltas, runUserspaceEventStream,
    wireUserspaceEventStreamCallbacks, handleEventStreamDelta,
    handleEventStreamFullResync, eventStreamFallbackLoop,
    queueUserspaceSessionDeltas, drainUserspaceSessionDeltasWithConfig.
  - daemon_ha_userspace_export.go (4 decls): the exporter interfaces,
    primaryOwnerRGIDs, exportUserspaceOwnerRGSessionsWithConfig.
  - daemon_ha_userspace_readiness.go (18 decls): the mode / takeover /
    transfer-readiness / HA-protocol-mismatch interfaces, the
    demotion-prep suppression-window methods, the
    prepareUserspaceRGDemotionWithTimeout peer barrier, the
    manual-failover wrappers, and userspaceDataplaneActive /
    userspaceRGConfigured / checkUserspaceTakeoverReadiness.

daemon_ha_userspace.go stays as the shared shell (3 decls:
userspaceXSKBindingController, buildZoneRGMap, rgHasRETH).

Load-bearing semantics preserved byte-identical: the delta-slice
behavior, the ACK/withhold return contract of handleEventStreamDelta
(returns false only for transient readiness gaps so the helper replays),
the userspaceDeltaSyncMu (delta drain) vs userspaceDemotionPrepMu
(demotion suppression window) lock separation, and the failover
peer-barrier logic. The split moves methods, not the lock discipline.
Same package, so every unexported symbol stays reachable —
buildZoneIDs (consumed by stream + export), the exporter interfaces
(used by daemon_ha_sync.go), and buildZoneRGMap / rgHasRETH (used by
daemon_apply.go, daemon_ha_vip.go, and package tests).

Validation: verified byte-for-byte against origin/master with a
go/parser decl-diff over the union of the new files — 52 top-level
declarations, identical=52, 0 bodydiff / 0 missing / 0 extra (import
lists differ per file as expected and are excluded from the diff).
go build ./... clean, go vet ./pkg/daemon/... clean, and
go test ./pkg/daemon/... green (the userspace-sync and HA-sync semantic
tests are the gate — all pass). No design/doc change: pkg/daemon/
README.md carries no per-file layout entry for this file and the split
is behavior-identical.

Closes #4659

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015oARShYtiJJ2H4UB4nXGqi
Copilot AI review requested due to automatic review settings July 8, 2026 16:17
@psaab
psaab merged commit 95b33d4 into master Jul 8, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Refactors the userspace-dataplane HA control path by splitting the former monolithic pkg/daemon/daemon_ha_userspace.go into smaller, concern-focused files within the same daemon package to reduce review hazard while keeping symbol visibility unchanged.

Changes:

  • Extracts wire/session conversion helpers into daemon_ha_userspace_convert.go.
  • Extracts event-stream + polling/drain/queue logic into daemon_ha_userspace_stream.go, and bulk export into daemon_ha_userspace_export.go.
  • Extracts takeover/demotion readiness + demotion barrier logic into daemon_ha_userspace_readiness.go, leaving daemon_ha_userspace.go as a small shared shell.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
pkg/daemon/daemon_ha_userspace.go Reduced to shared shell (XSK binding interface + zone/RG helpers).
pkg/daemon/daemon_ha_userspace_convert.go Houses zone ID derivation and delta→session conversion / endian / NAT / MAC helpers.
pkg/daemon/daemon_ha_userspace_stream.go Houses event stream wiring + fallback polling and delta drain/queue pipeline.
pkg/daemon/daemon_ha_userspace_export.go Houses bulk export interfaces and owner-RG session export helpers.
pkg/daemon/daemon_ha_userspace_readiness.go Houses takeover readiness + demotion suppression window + peer-barrier logic.
_Log.md Logs the refactor per repository logging rules.

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.

refactor: pkg/daemon/daemon_ha_userspace.go (1123 LOC) — split conversion/stream/drain/export/readiness (HA control)

2 participants