Skip to content

Commit 1e06acd

Browse files
author
Dov Alperin
committed
Add a way to tell what role repmgr thinks we are
1 parent d51e95c commit 1e06acd

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

pkg/flypg/node.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ func (n *Node) PostInit() error {
177177
return fmt.Errorf("no primary to follow and can't configure self as primary because primary region is '%s' and we are in '%s'", n.Region, os.Getenv("PRIMARY_REGION"))
178178
}
179179

180-
// Initialize ourselves as the primary.
181180
conn, err := n.NewLocalConnection(context.TODO())
182181
if err != nil {
183182
return err
184183
}
185184

185+
// Initialize ourselves as the primary.
186186
if err := n.createRequiredUsers(conn); err != nil {
187187
return fmt.Errorf("failed to create required users: %s", err)
188188
}
@@ -214,12 +214,22 @@ func (n *Node) PostInit() error {
214214
// If we are here, we are a standby or a demoted primary who needs
215215
// to be reconfigured as a standby.
216216

217-
// TODO - This should probably be a bit more calculated with this call.
218-
// We don't care if this fails against a standby, but do care if this
219-
// fails against a demoted primary.
220-
fmt.Println("Unregistering primary")
221-
if err := unregisterPrimary(*n); err != nil {
222-
fmt.Printf("failed to unregister primary ( ignore ): %s\n", err)
217+
conn, err := n.NewRepLocalConnection(context.TODO())
218+
if err != nil {
219+
return err
220+
}
221+
222+
role, err := n.currentRole(context.TODO(), conn)
223+
if err != nil {
224+
return err
225+
}
226+
fmt.Printf("Reconfiguring a %s node as healthy\n", role)
227+
228+
if role == "primary" {
229+
fmt.Println("Unregistering primary")
230+
if err := unregisterPrimary(*n); err != nil {
231+
fmt.Printf("failed to unregister primary: %s\n", err)
232+
}
223233
}
224234

225235
// TODO - Verify if there are any issues with attempting to re-register
@@ -248,7 +258,12 @@ func (n *Node) PostInit() error {
248258

249259
func (n *Node) NewLocalConnection(ctx context.Context) (*pgx.Conn, error) {
250260
host := net.JoinHostPort(n.PrivateIP, strconv.Itoa(n.PGPort))
251-
return openConnection(ctx, host, n.OperatorCredentials)
261+
return openConnection(ctx, host, "postgres", n.OperatorCredentials)
262+
}
263+
264+
func (n *Node) NewRepLocalConnection(ctx context.Context) (*pgx.Conn, error) {
265+
host := net.JoinHostPort(n.PrivateIP, strconv.Itoa(n.PGPort))
266+
return openConnection(ctx, host, "repmgr", n.ManagerCredentials)
252267
}
253268

254269
func (n *Node) createRequiredUsers(conn *pgx.Conn) error {
@@ -385,11 +400,11 @@ func (n *Node) setDefaultHBA() error {
385400
return nil
386401
}
387402

388-
func openConnection(ctx context.Context, host string, creds Credentials) (*pgx.Conn, error) {
403+
func openConnection(ctx context.Context, host string, database string, creds Credentials) (*pgx.Conn, error) {
389404
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
390405
defer cancel()
391406

392-
url := fmt.Sprintf("postgres://%s/postgres", host)
407+
url := fmt.Sprintf("postgres://%s/%s", host, database)
393408
conf, err := pgx.ParseConfig(url)
394409
if err != nil {
395410
return nil, err

pkg/flypg/repmgr.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package flypg
22

33
import (
4+
"context"
45
"fmt"
6+
"github.com/jackc/pgx/v4"
57
"os"
68
)
79

@@ -134,3 +136,13 @@ func writePasswdConf(node Node) error {
134136

135137
return nil
136138
}
139+
140+
func (n *Node) currentRole(ctx context.Context, pg *pgx.Conn) (string, error) {
141+
sql := fmt.Sprintf("select n.type from repmgr.nodes n LEFT JOIN repmgr.nodes un ON un.node_id = n.upstream_node_id WHERE n.node_id = '%d';", n.ID)
142+
var role string
143+
err := pg.QueryRow(ctx, sql).Scan(&role)
144+
if err != nil {
145+
return "", err
146+
}
147+
return role, nil
148+
}

0 commit comments

Comments
 (0)