Skip to content

Commit a37fdc3

Browse files
author
Dov Alperin
committed
Add some debug logging
1 parent 85be5cf commit a37fdc3

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

pkg/flypg/node.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,18 @@ func (n *Node) PostInit() error {
170170
return fmt.Errorf("failed to query current primary: %s", err)
171171
}
172172

173-
conn, err := n.NewLocalConnection(context.TODO())
174-
if err != nil {
175-
return err
176-
}
177-
178173
switch primaryIP {
179174
case "":
180175
// Check if we can be a primary
181176
if !n.ValidPrimary() {
182177
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"))
183178
}
184179

180+
conn, err := n.NewLocalConnection(context.TODO())
181+
if err != nil {
182+
return err
183+
}
184+
185185
// Initialize ourselves as the primary.
186186
if err := n.createRequiredUsers(conn); err != nil {
187187
return fmt.Errorf("failed to create required users: %s", err)
@@ -214,10 +214,16 @@ 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+
conn, err := n.NewRepLocalConnection(context.TODO())
218+
if err != nil {
219+
return err
220+
}
221+
217222
role, err := n.currentRole(context.TODO(), conn)
218223
if err != nil {
219224
return err
220225
}
226+
fmt.Printf("Reconfiguring a %s node as healthy\n", role)
221227

222228
if role == "primary" {
223229
fmt.Println("Unregistering primary")
@@ -252,7 +258,12 @@ func (n *Node) PostInit() error {
252258

253259
func (n *Node) NewLocalConnection(ctx context.Context) (*pgx.Conn, error) {
254260
host := net.JoinHostPort(n.PrivateIP, strconv.Itoa(n.PGPort))
255-
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)
256267
}
257268

258269
func (n *Node) createRequiredUsers(conn *pgx.Conn) error {
@@ -389,11 +400,11 @@ func (n *Node) setDefaultHBA() error {
389400
return nil
390401
}
391402

392-
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) {
393404
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
394405
defer cancel()
395406

396-
url := fmt.Sprintf("postgres://%s/postgres", host)
407+
url := fmt.Sprintf("postgres://%s/%s", host, database)
397408
conf, err := pgx.ParseConfig(url)
398409
if err != nil {
399410
return nil, err

0 commit comments

Comments
 (0)