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

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

Copy link
Copy Markdown
Member

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

coderabbitai Bot commented Jun 3, 2026

Copy link
Copy Markdown

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 42 minutes and 32 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: acec50ed-5a73-49e5-8cce-ba234f069dc9

📥 Commits

Reviewing files that changed from the base of the PR and between 8cc83a1 and 8da1336.

📒 Files selected for processing (14)
  • 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/user_pg_hba_pg_ident_and_scram.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

Adds Patroni DCS-to-dynamic-config conversion and schema updates, wires NodeSize-driven failsafe_mode in the config generator, implements PatroniConfig create/update/delete with dynamic wait computation and optional dynamic-config patching, updates systemd reload wiring, updates golden tests and generator tests, and adds an e2e verifying max_connections update.

Changes

Patroni Dynamic Config Update Feature

Layer / File(s) Summary
Patroni client schema
server/internal/patroni/client.go
DynamicConfig.Pause becomes *bool; DynamicStandbyClusterConfig.CreateReplicaMethods becomes *[]string; RecoveryMinApplyDelay becomes *int.
DCS-to-Dynamic conversion
server/internal/patroni/config.go
Added ToDynamicConfig() for DCSPostgreSQL, DCSStandbyCluster, and DCS; renamed SynchronousModeCountSynchronousNodeCount and MemberSlotsTtl*int.
Failsafe Mode in generator
server/internal/orchestrator/common/patroni_config_generator.go
Added NodeSize to PatroniConfigGenerator, wired from instance spec; generator sets FailsafeMode true when NodeSize==1, else false.
PatroniConfig create/update/delete & helpers
server/internal/orchestrator/common/patroni_config.go
Create delegates to internal helper that returns generated config; Update computes reload wait from dynamic config and instance status, attempts dynamic-config patch when primary (logs on failure), and invokes a reload callback with computed wait; added client and getStatusInfo helpers; Delete tolerates missing file.
systemd orchestrator integration
server/internal/orchestrator/systemd/patroni_config.go
Extracted getAddresses helper; Create/Update reuse it and delegate to base with signalReload callback; signalReload accepts dynamic wait duration and sleeps that duration post-reload.
Golden test outputs
server/internal/orchestrator/common/golden_test/TestPatroniConfigGenerator/*.yaml
Inserted failsafe_mode in bootstrap DCS for generator golden outputs (true for single-node swarm, false otherwise).
Config generator tests
server/internal/orchestrator/common/patroni_config_generator_test.go
Updated expected InstanceSpec entries to include NodeSize across generator test cases.
E2E test for global params
e2e/patroni_global_params_test.go
New build-tagged e2e test reads pg_settings.max_connections, updates PostgresqlConf["max_connections"] to +1 via fixture update, and verifies the database reflects the incremented value.

🐰 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 | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

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.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: enabling global Patroni parameter updates for systemd clusters by adding dynamic config patching support.
Description check ✅ Passed The description includes all required sections: a clear summary explaining the problem and solution, a detailed list of changes, comprehensive testing instructions with manual steps, and a filled-out checklist with issue reference (PLAT-610).
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

codacy-production Bot commented Jun 3, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 2 medium

Results:
2 new issues

Category Results
Complexity 2 medium

View in Codacy

🟢 Metrics -4 duplication

Metric Results
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.

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

🧹 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

@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
@jason-lynch jason-lynch force-pushed the fix/PLAT-610/update-global-patroni-params branch from 775fef7 to 8cc83a1 Compare June 9, 2026 19:55
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 8cc83a1 to 8da1336 Compare June 9, 2026 20:13
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