Skip to content

fix: update global patroni params for systemd#398

Open
jason-lynch wants to merge 1 commit into
mainfrom
fix/PLAT-610/update-global-patroni-params
Open

fix: update global patroni params for systemd#398
jason-lynch wants to merge 1 commit into
mainfrom
fix/PLAT-610/update-global-patroni-params

Conversation

@jason-lynch
Copy link
Copy Markdown
Member

@jason-lynch jason-lynch commented Jun 3, 2026

Summary

Patroni disallows setting some parameters through the Patroni config file after the cluster is created. Instead, they must be set via the dynamic config API.

This commit adds functionality to the systemd Patroni config implementation to patch the dynamic config if the Patroni API is up and reports that it is the primary instance.

This commit only affects systemd clusters. I will implement this for Swarm clusters in a subsequent commit by migrating Swarm to use the common Patroni config resource.

Testing

# this only works with systemd so far
# start the dev-lima environment
make dev-lima-run

# in a new terminal, switch to the dev-lima environment
use-dev-lima

# ensure that the cluster is initialized
cp-init

# create a database with the default max_connections
cp1-req create-database <<EOF | cp-follow-task
{                  
  "id": "storefront",
  "spec": {
    "database_name": "storefront",
    "database_users": [
      {
        "username": "admin",
        "password": "password",
        "db_owner": true,
        "attributes": ["SUPERUSER", "LOGIN"]
      }
    ],
    "port": 0,
    "patroni_port": 0,
    "nodes": [
      { "name": "n1", "host_ids": ["host-1"] }
    ]
  }
}
EOF

# query the current max_connections
cp-psql -i storefront-n1-689qacsi -U admin -- -c 'SHOW max_connections'

# update the database with a different max_connections, e.g.
cp1-req update-database storefront <<EOF | cp-follow-task
{                  
  "id": "storefront",
  "spec": {
    "database_name": "storefront",
    "database_users": [
      {
        "username": "admin",
        "db_owner": true,
        "attributes": ["SUPERUSER", "LOGIN"]
      }
    ],
    "port": 0,
    "patroni_port": 0,
    "nodes": [
      { "name": "n1", "host_ids": ["host-1"] }
    ],
    "postgresql_conf": {
      "max_connections": 900
    }
  }
}
EOF

# query the updated max_connections
cp-psql -i storefront-n1-689qacsi -U admin -- -c 'SHOW max_connections'

PLAT-610

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

Warning

Review limit reached

@jason-lynch, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 31 minutes and 56 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fff9329a-0d8a-40ba-b680-66c30aaee7a6

📥 Commits

Reviewing files that changed from the base of the PR and between d2fa9fa and 775fef7.

📒 Files selected for processing (13)
  • e2e/patroni_global_params_test.go
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/enable_fast_basebackup.yaml
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/in-place_restore.yaml
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/minimal_swarm.yaml
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/minimal_systemd.yaml
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/with_backup_config.yaml
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/with_restore_config.yaml
  • server/internal/orchestrator/common/patroni_config.go
  • server/internal/orchestrator/common/patroni_config_generator.go
  • server/internal/orchestrator/common/patroni_config_generator_test.go
  • server/internal/orchestrator/systemd/patroni_config.go
  • server/internal/patroni/client.go
  • server/internal/patroni/config.go
📝 Walkthrough

Walkthrough

This PR extends Patroni orchestration to support dynamic updates of PostgreSQL global configuration on primary nodes. It introduces DCS-to-dynamic-config conversion, adds failsafe mode control based on cluster size, implements the Update method with wait-time computation, and validates the entire flow through e2e tests.

Changes

Patroni Dynamic Config Update Feature

Layer / File(s) Summary
Patroni Dynamic Config Schema
server/internal/patroni/client.go
DynamicConfig.Pause becomes optional pointer, DynamicStandbyClusterConfig.CreateReplicaMethods becomes list of strings, and RecoveryMinApplyDelay becomes integer for proper JSON marshaling.
DCS-to-Dynamic Config Conversion
server/internal/patroni/config.go
New ToDynamicConfig() methods on DCSPostgreSQL, DCSStandbyCluster, and DCS map static DCS configuration into dynamic runtime config; DCS struct fields updated to SynchronousNodeCount and MemberSlotsTtl as integer.
Failsafe Mode Configuration in Generator
server/internal/orchestrator/common/patroni_config_generator.go
NodeSize field added to generator and sourced from instance spec; FailsafeMode automatically set to true for single-node clusters, false otherwise.
PatroniConfig Update Method and Helpers
server/internal/orchestrator/common/patroni_config.go
Update method generates Patroni config, queries dynamic config and instance status to compute reload wait duration, patches DCS when primary, and invokes caller-provided reload callback. New client and getStatusInfo helpers construct Patroni client and compute effective wait from dynamic config and instance state.
Systemd Orchestrator Integration
server/internal/orchestrator/systemd/patroni_config.go
Refactored Create and Update to use shared getAddresses helper for host peer addresses; Update delegates to base class with signalReload as callback; signalReload accepts dynamic wait duration instead of fixed default.
Golden Test Output Validation
server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/*.yaml
Golden test YAML files updated to include failsafe_mode field (true for single-node, false for multi-node/restore scenarios).
Config Generator Test Updates
server/internal/orchestrator/common/patroni_config_generator_test.go
Test expectations updated to include NodeSize field in InstanceSpec across all six test scenarios.
E2E Test for Global Params Update
e2e/patroni_global_params_test.go
New systemd-only e2e test reads PostgreSQL max_connections from primary, updates via PostgresqlConf, triggers orchestrator update, and verifies the setting persists in the database.

🐰 A config update hops through the meadow,
Dynamic patches drift like morning's shadow,
Failsafe modes bloom—one node or many—
Patroni reloads, swift as a bunny!
Test it end-to-end, the flow is merry,
Settings change graceful, never scary.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The PR description adequately explains the purpose, scope, testing approach, and issue reference, but lacks several checklist items. Complete the PR checklist by confirming test additions, documentation updates, changelog entry, and checking for breaking changes before merging.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: enabling updates to global Patroni parameters for systemd-managed clusters, which is the core objective of this PR.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/PLAT-610/update-global-patroni-params

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 and usage tips.

@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented Jun 3, 2026

Up to standards ✅

🟢 Issues 2 medium

Results:
2 new issues

Category Results
Complexity 2 medium

View in Codacy

🟢 Metrics 6 complexity · -4 duplication

Metric Results
Complexity 6
Duplication -4

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
server/internal/orchestrator/common/patroni_config.go (1)

252-261: 💤 Low value

Computed wait can be negative when DCSLastSeen is stale but within bounds.

When lastSeen is between loopWait and 2*loopWait in the past (e.g., 15s ago with a 10s loop), time.Until(lastSeen.Add(loopWait)) returns a negative duration. While utils.SleepContext likely treats negative durations as zero, clamping explicitly would clarify intent.

Suggested fix
 		if lastSeen.After(lowerBound) && lastSeen.Before(upperBound) {
 			// Compute the time until the next run cycle
 			wait = time.Until(lastSeen.Add(loopWait))
+			if wait < 0 {
+				wait = 0
+			}
 		}
🤖 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 `@server/internal/orchestrator/common/patroni_config.go` around lines 252 -
261, The computed wait based on DCSLastSeen can be negative; in the block that
computes wait using time.Until(lastSeen.Add(loopWait)) (involving
status.DCSLastSeen, lastSeen, loopWait and wait), clamp the computed duration to
a non-negative value before assigning to wait (e.g., compute d :=
time.Until(lastSeen.Add(loopWait)); if d < 0 { d = 0 }; wait = d) so the code
explicitly prevents negative sleeps (which utils.SleepContext may otherwise
treat as zero).
e2e/patroni_global_params_test.go (1)

13-50: 💤 Low value

Minor: DatabaseName doesn't reflect what the test validates.

The test validates max_connections changes, but DatabaseName is "test_port_change". Consider renaming to "test_max_connections" or similar for clarity.

Suggested fix
 	db := fixture.NewDatabaseFixture(ctx, t, &api.CreateDatabaseRequest{
 		Spec: &api.DatabaseSpec{
-			DatabaseName: "test_port_change",
+			DatabaseName: "test_max_connections",
🤖 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 `@e2e/patroni_global_params_test.go` around lines 13 - 50, Rename the test
database name to reflect what the test asserts: update the DatabaseName field in
the CreateDatabaseRequest's Spec (inside TestUpdatePatroniGlobalParams) from
"test_port_change" to a clearer name like "test_max_connections" so the
identifier matches the test intent; update any local references/comments that
mention the old name to keep wording consistent.
🤖 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.

Nitpick comments:
In `@e2e/patroni_global_params_test.go`:
- Around line 13-50: Rename the test database name to reflect what the test
asserts: update the DatabaseName field in the CreateDatabaseRequest's Spec
(inside TestUpdatePatroniGlobalParams) from "test_port_change" to a clearer name
like "test_max_connections" so the identifier matches the test intent; update
any local references/comments that mention the old name to keep wording
consistent.

In `@server/internal/orchestrator/common/patroni_config.go`:
- Around line 252-261: The computed wait based on DCSLastSeen can be negative;
in the block that computes wait using time.Until(lastSeen.Add(loopWait))
(involving status.DCSLastSeen, lastSeen, loopWait and wait), clamp the computed
duration to a non-negative value before assigning to wait (e.g., compute d :=
time.Until(lastSeen.Add(loopWait)); if d < 0 { d = 0 }; wait = d) so the code
explicitly prevents negative sleeps (which utils.SleepContext may otherwise
treat as zero).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 282e42d3-0776-46a4-9147-ea9e7f712be1

📥 Commits

Reviewing files that changed from the base of the PR and between 3fb1e15 and d2fa9fa.

📒 Files selected for processing (13)
  • e2e/patroni_global_params_test.go
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/enable_fast_basebackup.yaml
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/in-place_restore.yaml
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/minimal_swarm.yaml
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/minimal_systemd.yaml
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/with_backup_config.yaml
  • server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/with_restore_config.yaml
  • server/internal/orchestrator/common/patroni_config.go
  • server/internal/orchestrator/common/patroni_config_generator.go
  • server/internal/orchestrator/common/patroni_config_generator_test.go
  • server/internal/orchestrator/systemd/patroni_config.go
  • server/internal/patroni/client.go
  • server/internal/patroni/config.go

Patroni disallows setting some parameters through the Patroni config
file after the cluster is created. Instead, they must be set via the
dynamic config API.

This commit adds functionality to the systemd Patroni config
implementation to patch the dynamic config if the Patroni API is up and
if the API reports that it is the primary instance.

This commit only affects systemd clusters. I will implement this for
Swarm clusters in a subsequent commit by migrating Swarm to use the
common Patroni config resource.

PLAT-610
@jason-lynch jason-lynch force-pushed the fix/PLAT-610/update-global-patroni-params branch from d2fa9fa to 775fef7 Compare June 3, 2026 20:36
@jason-lynch jason-lynch marked this pull request as ready for review June 3, 2026 20:52
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