Repository navigation
Check and log logical replication lag before upgrade for pg17+ #3214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ed6de2c
Check and log logical replication lag before upgrade for pg17+
hughcapet 9709165
Address review comments
hughcapet 4f3db68
Allow longer wait for major upgrade E2E
Copilot c03dfa2
Allow more time for taint test operator sync
Copilot 1bf359d
Keep operator alive during taint E2E test
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,10 +16,33 @@ import ( | |
| ) | ||
|
|
||
| const ( | ||
| majorVersionUpgradeSuccessAnnotation = "last-major-upgrade-success" | ||
| majorVersionUpgradeFailureAnnotation = "last-major-upgrade-failure" | ||
| majorVersionUpgradeSuccessAnnotation = "last-major-upgrade-success" | ||
| majorVersionUpgradeFailureAnnotation = "last-major-upgrade-failure" | ||
| logicalReplicationSlotLagThreshold = 2 * 16 * 1024 * 1024 | ||
| logicalReplicationSlotLagCheckMinVersion = 17 * 10000 | ||
| ) | ||
|
|
||
| func parseLaggingLogicalReplicationSlots(output string) ([]string, error) { | ||
| laggingSlots := []string{} | ||
| for _, line := range strings.Split(strings.TrimSpace(output), "\n") { | ||
| if line == "" { | ||
| continue | ||
| } | ||
| fields := strings.Split(line, "|") | ||
| if len(fields) != 2 { | ||
| return nil, fmt.Errorf("unexpected logical replication slot lag output: %q", line) | ||
| } | ||
| lagBytes, err := strconv.ParseInt(fields[1], 10, 64) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid logical replication slot lag for slot %q: %w", fields[0], err) | ||
| } | ||
| if lagBytes > logicalReplicationSlotLagThreshold { | ||
| laggingSlots = append(laggingSlots, line) | ||
| } | ||
| } | ||
| return laggingSlots, nil | ||
| } | ||
|
|
||
| // IsBiggerPostgresVersion Compare two Postgres version numbers | ||
| func IsBiggerPostgresVersion(old string, new string) bool { | ||
| oldN, _ := strconv.Atoi(old) | ||
|
|
@@ -230,6 +253,26 @@ func (c *Cluster) majorVersionUpgrade() error { | |
| isUpgradeSuccess := true | ||
| numberOfPods := len(pods) | ||
| if allRunning { | ||
| podName := &spec.NamespacedName{Namespace: masterPod.Namespace, Name: masterPod.Name} | ||
| if c.currentMajorVersion >= logicalReplicationSlotLagCheckMinVersion { | ||
| const logicalSlotLagQuery = `SELECT slot_name, pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)::bigint | ||
| FROM pg_replication_slots WHERE slot_type = 'logical'` | ||
| slotLagOutput, err := c.ExecCommand(podName, "psql", "-X", "-A", "-t", "-v", "ON_ERROR_STOP=1", "-U", c.OpConfig.SuperUsername, "-d", "postgres", "-c", logicalSlotLagQuery) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about we move this SQL part to a new function in database.go file? |
||
| if err != nil { | ||
| c.logger.Errorf("skipping major version upgrade, could not check logical replication slot lag: %v", err) | ||
| return fmt.Errorf("could not check logical replication slot lag: %w", err) | ||
| } | ||
| laggingSlots, err := parseLaggingLogicalReplicationSlots(slotLagOutput) | ||
| if err != nil { | ||
| c.logger.Errorf("skipping major version upgrade, could not parse logical replication slot lag: %v", err) | ||
| return fmt.Errorf("could not parse logical replication slot lag: %w", err) | ||
| } | ||
| if len(laggingSlots) > 0 { | ||
| c.logger.Errorf("skipping major version upgrade, logical replication slot lag exceeds %d bytes (slot_name|lag_bytes): %s", logicalReplicationSlotLagThreshold, strings.Join(laggingSlots, ", ")) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| c.logger.Infof("healthy cluster ready to upgrade, current: %d desired: %d", c.currentMajorVersion, desiredVersion) | ||
| if c.currentMajorVersion < desiredVersion { | ||
| defer func() error { | ||
|
|
@@ -243,7 +286,6 @@ func (c *Cluster) majorVersionUpgrade() error { | |
| return fmt.Errorf("failed to assign critical-operation label: %s", err) | ||
| } | ||
|
|
||
| podName := &spec.NamespacedName{Namespace: masterPod.Namespace, Name: masterPod.Name} | ||
| c.logger.Infof("triggering major version upgrade on pod %s of %d pods", masterPod.Name, numberOfPods) | ||
| c.eventRecorder.Eventf(c.GetReference(), v1.EventTypeNormal, "Major Version Upgrade", "starting major version upgrade on pod %s of %d pods", masterPod.Name, numberOfPods) | ||
| upgradeCommand := fmt.Sprintf("set -o pipefail && /usr/bin/python3 /scripts/inplace_upgrade.py %d 2>&1 | tee last_upgrade.log", numberOfPods) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package cluster | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseLaggingLogicalReplicationSlots(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| output string | ||
| wantSlots []string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "no slots", | ||
| wantSlots: []string{}, | ||
| }, | ||
| { | ||
| name: "slots at threshold are allowed", | ||
| output: "at_threshold|33554432\n", | ||
| wantSlots: []string{}, | ||
| }, | ||
| { | ||
| name: "lagging slot is returned", | ||
| output: "lagging_slot|33554433\n", | ||
| wantSlots: []string{"lagging_slot|33554433"}, | ||
| }, | ||
| { | ||
| name: "only lagging slots are returned", | ||
| output: "healthy_slot|100\nlagging_slot|33554433\n", | ||
| wantSlots: []string{"lagging_slot|33554433"}, | ||
| }, | ||
| { | ||
| name: "invalid output is rejected", | ||
| output: "unexpected output", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "invalid lag is rejected", | ||
| output: "slot|unknown", | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| gotSlots, err := parseLaggingLogicalReplicationSlots(tt.output) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("parseLaggingLogicalReplicationSlots() error = %v, wantErr %v", err, tt.wantErr) | ||
| } | ||
| if err != nil { | ||
| return | ||
| } | ||
| if !reflect.DeepEqual(gotSlots, tt.wantSlots) { | ||
| t.Errorf("parseLaggingLogicalReplicationSlots() = %v, want %v", gotSlots, tt.wantSlots) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could the query not contain more logic instead of the go code? We could pass the tolerance value to the query and only return lagging slots or empty result. When empty we're good to go, when query returns slots we delay the major version upgrade. Could still be nice to know, though, which slots are lagging behind (which this PR currently supports).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, i only did it for this extended testing:D, otherwise there is not much to test