Skip to content

fix: implement LeaderElectionRunnable for CostAccrual - #121

Merged
InftyAI-Agent merged 1 commit into
InftyAI:mainfrom
Sarthak-Shreshtha01:fix/issue-95
Sep 26, 2026
Merged

InftyAI-Agent merged 1 commit into
InftyAI:mainfrom
Sarthak-Shreshtha01:fix/issue-95

Conversation

@Sarthak-Shreshtha01

@Sarthak-Shreshtha01 Sarthak-Shreshtha01 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it

CostAccrual ran only on the leader because that is the manager's default for a plain Runnable. Nothing in the code said so, so a change to that default could let every replica add up the same costs.

This PR adds NeedLeaderElection(), which returns true, and a compile-time check that CostAccrual implements manager.LeaderElectionRunnable. I also updated the comment in cmd/main.go, which said the loop relied on not having this method.

Which issue(s) this PR fixes

Fixes #95

Special notes for your reviewer

I added TestCostAccrualNeedsLeaderElection. The related tests pass.

Does this PR introduce a user-facing change?

NONE

Summary by CodeRabbit

  • Bug Fixes
    • Cost accrual now runs on the elected leader, preventing multiple replicas from accruing costs at the same time. This helps keep cost totals consistent across deployments.

Signed-off-by: Sarthak <sarthakshreshtha345@gmail.com>
Copilot AI lite review requested due to automatic review settings September 26, 2026 10:51

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@InftyAI-Agent InftyAI-Agent added needs-triage Indicates an issue or PR lacks a label and requires one. needs-priority Indicates a PR lacks a label and requires one. do-not-merge/needs-kind Indicates a PR lacks a label and requires one. labels Sep 26, 2026
@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: e37f4953-68cd-4f66-905b-7096423ff7e3

📥 Commits

Reviewing files that changed from the base of the PR and between 7bf4809 and e51a242.

📒 Files selected for processing (3)
  • cmd/main.go
  • internal/controller/cost_accrual.go
  • internal/controller/cost_accrual_test.go
 _____________________________________________________________
< Tabs vs spaces? You somehow chose violence *and* confusion. >
 -------------------------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 3c5f03e2-01ea-4f67-b52a-2b73759c57e9

📥 Commits

Reviewing files that changed from the base of the PR and between 7bf4809 and e51a242.

📒 Files selected for processing (3)
  • cmd/main.go
  • internal/controller/cost_accrual.go
  • internal/controller/cost_accrual_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.


📝 Walkthrough

Walkthrough

CostAccrual now implements manager.LeaderElectionRunnable and returns true from NeedLeaderElection. The change also updates comments about leader election and adds a test for the method.

Changes

Cost Accrual Leader Election

Layer / File(s) Summary
Leader-election contract and validation
internal/controller/cost_accrual.go, cmd/main.go, internal/controller/cost_accrual_test.go
CostAccrual now implements the leader-election runnable interface and requires leader election. The comments describe this requirement, and a test asserts that NeedLeaderElection() returns true.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: kerthcet

Merge Risk: ⚪ Minimal · up to e51a2

In the supported deployment, cost accrual runs only on the elected manager, preventing replicas from accruing concurrently. No actionable merge risk remains.

Security Architecture Review

Security architecture risk: 🔵 Low · up to e51a2

The change reinforces the intended single-leader behavior rather than expanding access. Its protection still depends on leader election being enabled wherever multiple manager replicas run.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — Leader-only execution is relevant to the fleet-wide cost ledger and per-replica metrics, but the reviewed change pins the existing execution intent rather than increasing the loop's authority or reachable assets.

Trust Boundaries and Controls

  • inferred — The method opts into the manager's leader-election control, conditional on that control being enabled. A false leader-election setting predates this PR and is not newly caused by the method.

Resilience and Maintainability Implications

  • observed — The unchanged write ordering limits duplicate durable charging after a conflict; context cancellation stops the ticker loop. An in-flight write during leader handoff is not covered by the reviewed test.

Hardening Proposals

  • proposed — Verify leader election is enabled in any multi-replica deployment and exercise leadership loss during an in-flight accrual write.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issue #95 requires CostAccrual to implement NeedLeaderElection() bool and return true. The PR adds this method, adds the manager.LeaderElectionRunnable compile-time assertion, and adds `TestCo…
Out of Scope Changes check ✅ Passed The changes stay within issue #95. The implementation, compile-time assertion, test, and comment update all support leader election for CostAccrual. No unrelated change is shown.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: implementing LeaderElectionRunnable for CostAccrual.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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

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

@Sarthak-Shreshtha01

Copy link
Copy Markdown
Contributor Author

/kind bug

@InftyAI-Agent InftyAI-Agent added bug Categorizes issue or PR as related to a bug. and removed do-not-merge/needs-kind Indicates a PR lacks a label and requires one. labels Sep 26, 2026
@kerthcet

Copy link
Copy Markdown
Member

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

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.

@kerthcet

Copy link
Copy Markdown
Member

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Pull request base or head changed.

@kerthcet

Copy link
Copy Markdown
Member

/lgtm
/approve

Thanks @Sarthak-Shreshtha01

@InftyAI-Agent InftyAI-Agent added lgtm Looks good to me, indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Sep 26, 2026

@InftyAI-Agent InftyAI-Agent left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approved: PR has both lgtm and approved labels

@InftyAI-Agent
InftyAI-Agent merged commit ef280f1 into InftyAI:main Sep 26, 2026
39 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. bug Categorizes issue or PR as related to a bug. lgtm Looks good to me, indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a label and requires one. needs-triage Indicates an issue or PR lacks a label and requires one.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implementing LeaderElectionRunnable for CostAccrual

4 participants