Skip to content

Commit

Permalink
pghoard: add xlog filename check to CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
egor-voynov-aiven committed Apr 11, 2024
1 parent 7c466c3 commit bce9aa1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
20 changes: 12 additions & 8 deletions golang/pghoard_postgres_command_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/http"
"os"
"path"
"regexp"
"time"
)

Expand Down Expand Up @@ -120,15 +121,18 @@ func restore_command(url string, output string, xlog string) (int, error) {
}
output_path = path.Join(cwd, output)
}
// if file "<xlog>.pghoard.prefetch" exists, just move it to destination
xlogPrefetchPath := path.Join(path.Dir(output_path), xlog+".pghoard.prefetch")
_, err = os.Stat(xlogPrefetchPath)
if err == nil {
err := os.Rename(xlogPrefetchPath, output_path)
if err != nil {
return EXIT_ABORT, err
xlogNameRe := regexp.MustCompile(`^[A-F0-9]{24}$`)
if xlogNameRe.MatchString(xlog) {
// if file "<xlog>.pghoard.prefetch" exists, just move it to destination
xlogPrefetchPath := path.Join(path.Dir(output_path), xlog+".pghoard.prefetch")
_, err = os.Stat(xlogPrefetchPath)
if err == nil {
err := os.Rename(xlogPrefetchPath, output_path)
if err != nil {
return EXIT_ABORT, err
}
return EXIT_OK, nil
}
return EXIT_OK, nil
}
req, err = http.NewRequest("GET", url, nil)
req.Header.Set("x-pghoard-target-path", output_path)
Expand Down
13 changes: 8 additions & 5 deletions pghoard/postgres_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import argparse
import os
import re
import socket
import sys
import time
Expand All @@ -16,6 +17,7 @@

PGHOARD_HOST = "127.0.0.1"
PGHOARD_PORT = 16000
XLOG_NAME_RE = re.compile("^[A-F0-9]{24}$")

# When running restore_command PostgreSQL interprets exit codes 1..125 as "file not found errors" signalling
# that there's no such WAL file from which PostgreSQL assumes that we've completed recovery. We never want to
Expand Down Expand Up @@ -72,11 +74,12 @@ def restore_command(site, xlog, output, host=PGHOARD_HOST, port=PGHOARD_PORT, re
# directory. Note that os.path.join strips preceding components if a new components starts with a
# slash so it's still possible to use this with absolute paths.
output_path = os.path.join(os.getcwd(), output)
# if file "<xlog>.pghoard.prefetch" exists, just move it to destination
prefetch_path = os.path.join(os.path.dirname(output_path), xlog + ".pghoard.prefetch")
if os.path.exists(prefetch_path):
os.rename(prefetch_path, output_path)
return
if XLOG_NAME_RE.match(xlog):
# if file "<xlog>.pghoard.prefetch" exists, just move it to destination
prefetch_path = os.path.join(os.path.dirname(output_path), xlog + ".pghoard.prefetch")
if os.path.exists(prefetch_path):
os.rename(prefetch_path, output_path)
return
headers = {"x-pghoard-target-path": output_path}
method = "GET"
path = "/{}/archive/{}".format(site, xlog)
Expand Down

0 comments on commit bce9aa1

Please sign in to comment.