Created
July 25, 2020 00:02
-
-
Save viggy28/954ff01cd3c29d317834a5c25951a1cd to your computer and use it in GitHub Desktop.
This file contains 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
[17:00:07] visi:stolon git:(lawrence-checkpoint-before-rewind*) $ cat patch.txt | |
diff --git a/cmd/keeper/cmd/keeper.go b/cmd/keeper/cmd/keeper.go | |
index 047a2e7..919a5b4 100644 | |
--- a/cmd/keeper/cmd/keeper.go | |
+++ b/cmd/keeper/cmd/keeper.go | |
@@ -849,10 +849,11 @@ func (p *PostgresKeeper) resync(db, masterDB, followedDB *cluster.DB, tryPgrewin | |
// rewind that it targets the current primary, rather than whatever database we | |
// follow. | |
connParams := p.getSUConnParams(db, masterDB) | |
+ sslmode := masterDB.Spec.PGParameters["ssl"] | |
checkpointBeforePgrewind := db.Spec.CheckpointBeforePgrewind | |
log.Infow("syncing using pg_rewind", "masterDB", masterDB.UID, | |
"keeper", followedDB.Spec.KeeperUID, "forcingCheckpoint", checkpointBeforePgrewind) | |
- if err := pgm.SyncFromFollowedPGRewind(connParams, p.pgSUPassword, checkpointBeforePgrewind); err != nil { | |
+ if err := pgm.SyncFromFollowedPGRewind(connParams, sslmode, p.pgSUPassword, checkpointBeforePgrewind); err != nil { | |
// log pg_rewind error and fallback to pg_basebackup | |
log.Errorw("error syncing with pg_rewind", zap.Error(err)) | |
} else { | |
diff --git a/internal/postgresql/postgresql.go b/internal/postgresql/postgresql.go | |
index c858b60..7fc7982 100644 | |
--- a/internal/postgresql/postgresql.go | |
+++ b/internal/postgresql/postgresql.go | |
@@ -17,6 +17,7 @@ package postgresql | |
import ( | |
"bufio" | |
"context" | |
+ "database/sql" | |
"errors" | |
"fmt" | |
"io" | |
@@ -890,7 +891,7 @@ func (p *Manager) createPostgresqlAutoConf() error { | |
return nil | |
} | |
-func (p *Manager) SyncFromFollowedPGRewind(followedConnParams ConnParams, password string, forceCheckpoint bool) error { | |
+func (p *Manager) SyncFromFollowedPGRewind(followedConnParams ConnParams, sslmode, password string, forceCheckpoint bool) error { | |
// Remove postgresql.auto.conf since pg_rewind will error if it's a symlink to /dev/null | |
pgAutoConfPath := filepath.Join(p.dataDir, postgresAutoConf) | |
if err := os.Remove(pgAutoConfPath); err != nil && !os.IsNotExist(err) { | |
@@ -916,8 +917,15 @@ func (p *Manager) SyncFromFollowedPGRewind(followedConnParams ConnParams, passwo | |
// temporary table on the master but if synchronous replication is | |
// enabled and there're no active standbys it will hang. | |
followedConnParams.Set("options", "-c synchronous_commit=off") | |
- followedConnString := followedConnParams.ConnString() | |
- | |
+ var followedConnStringWithSSLmode string | |
+ switch sslmode { | |
+ case "on": | |
+ followedConnStringWithSSLmode = strings.Replace(followedConnParams.ConnString(), "sslmode=prefer", "sslmode=require", -1) | |
+ case "": | |
+ followedConnStringWithSSLmode = strings.Replace(followedConnParams.ConnString(), "sslmode=prefer", "sslmode=disable", -1) | |
+ default: | |
+ followedConnStringWithSSLmode = strings.Replace(followedConnParams.ConnString(), "sslmode=prefer", "sslmode=disable", -1) | |
+ } | |
// We need to issue a checkpoint on the source before pg_rewind'ing as until the primary | |
// checkpoints the global/pg_control file won't contain up-to-date information about | |
// what timeline the primary exists in. | |
@@ -934,19 +942,20 @@ func (p *Manager) SyncFromFollowedPGRewind(followedConnParams ConnParams, passwo | |
// of downtime. | |
if forceCheckpoint { | |
log.Infow("issuing checkpoint on primary") | |
- psqlName := filepath.Join(p.pgBinPath, "psql") | |
- cmd := exec.Command(psqlName, followedConnString, "-c", "CHECKPOINT;") | |
- cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSFILE=%s", pgpass.Name())) | |
- cmd.Stdout = os.Stdout | |
- cmd.Stderr = os.Stderr | |
- if err := cmd.Run(); err != nil { | |
- return fmt.Errorf("error: %v", err) | |
+ db, err := sql.Open("postgres", followedConnStringWithSSLmode) | |
+ if err != nil { | |
+ return err | |
} | |
+ defer db.Close() | |
+ _, err = dbExec(context.Background(), db, "CHECKPOINT") | |
+ if err != nil { | |
+ return err | |
+ } | |
+ log.Infow("issued checkpoint on primary") | |
} | |
- | |
log.Infow("running pg_rewind") | |
name := filepath.Join(p.pgBinPath, "pg_rewind") | |
- cmd := exec.Command(name, "--debug", "-D", p.dataDir, "--source-server="+followedConnString) | |
+ cmd := exec.Command(name, "--debug", "-D", p.dataDir, "--source-server="+followedConnParams.ConnString()) | |
cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSFILE=%s", pgpass.Name())) | |
log.Debugw("execing cmd", "cmd", cmd) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment