|
| 1 | +/* |
| 2 | +Copyright © contributors to CloudNativePG, established as |
| 3 | +CloudNativePG a Series of LF Projects, LLC. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +SPDX-License-Identifier: Apache-2.0 |
| 18 | +*/ |
| 19 | + |
| 20 | +package common |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "os/exec" |
| 26 | + "regexp" |
| 27 | + "strconv" |
| 28 | + "strings" |
| 29 | + |
| 30 | + "github.com/cloudnative-pg/machinery/pkg/log" |
| 31 | +) |
| 32 | + |
| 33 | +var timelineRe = regexp.MustCompile(`Latest checkpoint's TimeLineID:\s+(\d+)`) |
| 34 | + |
| 35 | +// currentTimeline returns the server's current PostgreSQL timeline by |
| 36 | +// parsing pg_controldata output. |
| 37 | +// |
| 38 | +// This is reliable for the promotion case because PostgreSQL performs a |
| 39 | +// synchronous end-of-recovery checkpoint (which updates the control file) |
| 40 | +// before the server starts accepting connections and before the WAL |
| 41 | +// archiver is signaled. By the time this function is called during the |
| 42 | +// first WAL archive attempt, the control file reflects the promoted |
| 43 | +// timeline. |
| 44 | +// |
| 45 | +// Returns an error if the timeline cannot be determined. Callers must NOT |
| 46 | +// silently fall back to omitting --timeline, as that reintroduces the |
| 47 | +// original "Expected empty archive" bug after failover. |
| 48 | +func currentTimeline(ctx context.Context, pgDataPath string) (int, error) { |
| 49 | + contextLogger := log.FromContext(ctx) |
| 50 | + |
| 51 | + cmd := exec.CommandContext(ctx, "pg_controldata", pgDataPath) // #nosec G204 |
| 52 | + cmd.Env = append(cmd.Environ(), "LC_ALL=C") |
| 53 | + out, err := cmd.Output() |
| 54 | + if err != nil { |
| 55 | + return 0, fmt.Errorf( |
| 56 | + "pg_controldata exec failed (PGDATA=%s): %w; "+ |
| 57 | + "WAL archive check cannot run safely without a timeline — "+ |
| 58 | + "set annotation cnpg.io/skipEmptyWalArchiveCheck=enabled "+ |
| 59 | + "as a manual workaround", |
| 60 | + pgDataPath, err) |
| 61 | + } |
| 62 | + |
| 63 | + tl, err := parseTimelineIDFromPgControldataOutput(string(out), pgDataPath) |
| 64 | + if err != nil { |
| 65 | + return 0, err |
| 66 | + } |
| 67 | + |
| 68 | + contextLogger.Info("Detected PostgreSQL timeline from pg_controldata", |
| 69 | + "timeline", tl) |
| 70 | + return tl, nil |
| 71 | +} |
| 72 | + |
| 73 | +// parseTimelineIDFromPgControldataOutput extracts Latest checkpoint's TimeLineID |
| 74 | +// from pg_controldata stdout. pgDataPath is used only in error messages. |
| 75 | +func parseTimelineIDFromPgControldataOutput(out string, pgDataPath string) (int, error) { |
| 76 | + matches := timelineRe.FindStringSubmatch(out) |
| 77 | + if len(matches) < 2 { |
| 78 | + return 0, fmt.Errorf( |
| 79 | + "could not parse TimeLineID from pg_controldata output "+ |
| 80 | + "(PGDATA=%s); set annotation "+ |
| 81 | + "cnpg.io/skipEmptyWalArchiveCheck=enabled as a manual "+ |
| 82 | + "workaround", |
| 83 | + pgDataPath) |
| 84 | + } |
| 85 | + |
| 86 | + tl, err := strconv.Atoi(strings.TrimSpace(matches[1])) |
| 87 | + if err != nil { |
| 88 | + return 0, fmt.Errorf("parse timeline %q: %w", matches[1], err) |
| 89 | + } |
| 90 | + |
| 91 | + return tl, nil |
| 92 | +} |
0 commit comments