Skip to content

PMM-15227 Remove stale HA replicas from Inventory - #5738

Open
4nte wants to merge 8 commits into
mainfrom
PMM-15227-remove-stale-ha-nodes-from-inventory
Open

PMM-15227 Remove stale HA replicas from Inventory#5738
4nte wants to merge 8 commits into
mainfrom
PMM-15227-remove-stale-ha-nodes-from-inventory

Conversation

@4nte

@4nte 4nte commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

PMM-15227

Percona-Lab/pmm-submodules#4513

Problem

Every PMM Server replica in an HA deployment registers its own inventory Node at first boot,
and nothing ever removed it. After scaling the deployment down, the departed replica kept
appearing under Inventory > Nodes with status Unknown.

Scope

HA deployments only, and inventory rows only. No API or schema change; metrics already
written to VictoriaMetrics are left alone.

Solution

At startup, remove PMM Server Nodes whose name is absent from the configured PMM_HA_PEERS.
The peer list is the signal because the chart regenerates it from the replica count and
recreates every pod when it changes, whereas a member missing from the memberlist cluster may
simply be restarting.

Cleanup is skipped when the peer list cannot be trusted (an entry that carries no node name,
or a list that omits this replica), and a departing replica that still monitors Services is
kept with a warning instead of having those exporters cascade-deleted.

@catalinaadam
catalinaadam temporarily deployed to PMM-15227-remove-stale-ha-nodes-from-inventory - pmm-doc-3.9.0 PR #5738 August 6, 2026 08:03 — with Render Destroyed
@ademidoff
ademidoff temporarily deployed to PMM-15227-remove-stale-ha-nodes-from-inventory - pmm-doc-3 PR #5738 August 6, 2026 08:03 — with Render Destroyed
@github-actions github-actions Bot added the documentation Documentation changes label Aug 6, 2026
@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.57143% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 45.07%. Comparing base (31318c7) to head (b2cbb72).
⚠️ Report is 96 commits behind head on main.

Files with missing lines Patch % Lines
managed/models/node_helpers.go 82.08% 8 Missing and 4 partials ⚠️
managed/models/database.go 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5738      +/-   ##
==========================================
+ Coverage   43.59%   45.07%   +1.48%     
==========================================
  Files         415      218     -197     
  Lines       43134    27916   -15218     
==========================================
- Hits        18804    12584    -6220     
+ Misses      22454    13977    -8477     
+ Partials     1876     1355     -521     
Flag Coverage Δ
admin ?
agent ?
managed 45.07% <78.57%> (+2.09%) ⬆️
vmproxy ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@4nte
4nte marked this pull request as ready for review August 6, 2026 09:18
@4nte
4nte requested review from a team as code owners August 6, 2026 09:18
@4nte
4nte requested review from ademidoff and maxkondr and removed request for a team August 6, 2026 09:18
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d292d336-f461-405b-88d9-0d52987f82af

📥 Commits

Reviewing files that changed from the base of the PR and between d52d51a and c62e178.

📒 Files selected for processing (2)
  • managed/models/node_helpers.go
  • managed/models/node_helpers_test.go
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • percona/pmm-qa (manual)
  • percona/pmm (manual)
🚧 Files skipped from review as they are similar to previous changes (2)
  • managed/models/node_helpers_test.go
  • managed/models/node_helpers.go

Walkthrough

HA setup now removes eligible stale PMM Server nodes when trusted peer data identifies scaled-down replicas. It preserves active replicas and nodes with monitored services. Tests cover cleanup and invalid peer data. Documentation describes the resulting Inventory behavior.

Changes

HA stale-node cleanup

Layer / File(s) Summary
Stale-node detection and removal
managed/models/node_helpers.go
RemoveStaleHANodes validates HA peer data, identifies obsolete PMM Server nodes, preserves nodes with monitored services, and removes eligible nodes with their agents.
HA setup integration
managed/models/database.go, documentation/docs/install-pmm/install-HA-clustered.md
HA setup runs stale-node cleanup before existing-agent handling and returns cleanup errors. The documentation describes when removed replica nodes disappear from Inventory.
Cleanup validation
managed/models/node_helpers_test.go
Tests cover stale replica removal, active and monitored node preservation, trusted peer lists, and invalid or incomplete peer data.

Sequence Diagram(s)

sequenceDiagram
  participant HASetup as setupPMMServerHAAgents
  participant Cleanup as RemoveStaleHANodes
  participant Nodes as PMM node records
  participant Agents as PMM agent records
  HASetup->>Cleanup: Pass HA node ID and configured peers
  Cleanup->>Nodes: Find obsolete PMM Server nodes
  Cleanup->>Agents: Check monitored services
  Cleanup->>Nodes: Remove eligible stale nodes
  Cleanup-->>HASetup: Return cleanup result
Loading

Possibly related PRs

  • percona/pmm#5704: Handles related PMM HA node management through service-delegation logic.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the ticket and the primary change: removing stale HA replicas from Inventory.
Description check ✅ Passed The description explains the problem, scope, solution, safety conditions, related work, and that no API changes were made.
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.

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

@coderabbitai coderabbitai 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.

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@documentation/docs/install-pmm/install-HA-clustered.md`:
- Line 832: Revise the statement about removed replica Nodes in the HA cluster
cleanup documentation to say that only eligible stale Nodes disappear after
remaining pods restart. Mention that removal is skipped when peer data is
untrusted or the Node still monitors Services, and instruct operators to move
those Services to a running replica before removal.

In `@managed/models/node_helpers_test.go`:
- Around line 272-275: Replace the live PostgreSQL setup in the
RemoveStaleHANodes unit test with a go-sqlmock database, configuring only the
SQL expectations needed by RemoveStaleHANodes. Keep testdb.Open out of this unit
test; move the coverage to integration tests only if the behavior cannot be
validated with mocked database interactions.

In `@managed/models/node_helpers.go`:
- Around line 375-394: Exclude the legacy PMM Server node from stale-replica
removal by adding a check for node.NodeID == PMMServerNodeID in the loop before
removeNode is called. Preserve the existing handling for eligible HA replicas
and retain the legacy node without attempting removal.
- Around line 411-420: Update the peer parsing helper around the existing
strings.TrimSpace/strings.Cut logic to call net.ParseIP on the full trimmed peer
entry before splitting at “:”, rejecting unbracketed IPv6 addresses instead of
treating their first segment as a node label. Preserve the existing host and
label validation for non-IP entries, and add an unbracketed IPv6 case to the
untrusted-peer tests.
- Around line 359-399: Replace the package-level logrus.Warnf and logrus.Infof
calls in the stale HA cleanup flow with structured *logrus.Entry logging. Attach
relevant fields such as node_id, node_name, peer, and ha_node_id to each event
while preserving the existing messages and control flow.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f0769d83-7fa0-483d-b09c-be3789de1b70

📥 Commits

Reviewing files that changed from the base of the PR and between 13c3072 and b82c657.

📒 Files selected for processing (4)
  • documentation/docs/install-pmm/install-HA-clustered.md
  • managed/models/database.go
  • managed/models/node_helpers.go
  • managed/models/node_helpers_test.go
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • percona/pmm-qa (manual)
  • percona/pmm (manual)

- HAProxy continues routing to available pods during rollout
- No data loss (distributed storage)
- Rolling update strategy minimizes downtime
- The Nodes of removed replicas disappear from **Inventory > Nodes** once the remaining pods restart

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

State the retention condition.

This sentence guarantees Node removal after restart. Cleanup retains a stale Node when it still monitors Services. Cleanup also skips removal when peer data is not trusted.

State that only eligible stale replica Nodes disappear. Explain that operators must move monitored Services to a running replica before removal.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@documentation/docs/install-pmm/install-HA-clustered.md` at line 832, Revise
the statement about removed replica Nodes in the HA cluster cleanup
documentation to say that only eligible stale Nodes disappear after remaining
pods restart. Mention that removal is skipped when peer data is untrusted or the
Node still monitors Services, and instruct operators to move those Services to a
running replica before removal.

Comment thread managed/models/node_helpers_test.go
Comment thread managed/models/node_helpers.go Outdated
Comment thread managed/models/node_helpers.go
Comment thread managed/models/node_helpers.go Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Documentation changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants