@@ -114,12 +114,12 @@ func (n *Node) Init(ctx context.Context) error {
114
114
return err
115
115
}
116
116
117
- consul , err := state .NewConsulClient ()
117
+ cs , err := state .NewClusterState ()
118
118
if err != nil {
119
- return fmt .Errorf ("failed to establish connection with consul: %s " , err )
119
+ return fmt .Errorf ("failed initialize cluster state store. %v " , err )
120
120
}
121
121
122
- primaryIP , err := consul . CurrentPrimary ()
122
+ primary , err := cs . PrimaryMember ()
123
123
if err != nil {
124
124
return fmt .Errorf ("failed to query current primary: %s" , err )
125
125
}
@@ -133,7 +133,7 @@ func (n *Node) Init(ctx context.Context) error {
133
133
fmt .Printf ("Failed to initialize repmgr: %s\n " , err .Error ())
134
134
}
135
135
136
- err = SyncUserConfig (& repmgr , consul )
136
+ err = SyncUserConfig (& repmgr , cs . Store )
137
137
if err != nil {
138
138
fmt .Printf ("Failed to sync user config from consul for repmgr: %s\n " , err .Error ())
139
139
}
@@ -148,7 +148,7 @@ func (n *Node) Init(ctx context.Context) error {
148
148
return err
149
149
}
150
150
151
- err = SyncUserConfig (& pgbouncer , consul )
151
+ err = SyncUserConfig (& pgbouncer , cs . Store )
152
152
if err != nil {
153
153
fmt .Printf ("Failed to sync user config from consul for pgbouncer: %s\n " , err .Error ())
154
154
}
@@ -158,10 +158,8 @@ func (n *Node) Init(ctx context.Context) error {
158
158
fmt .Printf ("Failed to write config files for pgbouncer: %s\n " , err .Error ())
159
159
}
160
160
161
- switch primaryIP {
162
- case n .PrivateIP :
163
- // noop
164
- case "" :
161
+ switch {
162
+ case primary == nil :
165
163
// Initialize ourselves as the primary.
166
164
fmt .Println ("Initializing postgres" )
167
165
if err := n .initialize (); err != nil {
@@ -172,20 +170,22 @@ func (n *Node) Init(ctx context.Context) error {
172
170
if err := n .setDefaultHBA (); err != nil {
173
171
return fmt .Errorf ("failed updating pg_hba.conf: %s" , err )
174
172
}
173
+ case primary .Hostname == n .PrivateIP :
174
+ // noop
175
175
default :
176
176
// If we are here we are either a standby, new node or primary coming back from the dead.
177
177
clonePrimary := true
178
178
if n .isInitialized () {
179
179
// Attempt to resolve our role by querying the primary.
180
- remoteConn , err := repmgr .NewRemoteConnection (ctx , primaryIP )
180
+ remoteConn , err := repmgr .NewRemoteConnection (ctx , primary . Hostname )
181
181
if err != nil {
182
182
return fmt .Errorf ("failed to resolve my role according to the primary: %s" , err )
183
183
}
184
184
defer remoteConn .Close (ctx )
185
185
186
186
role , err := repmgr .memberRoleByHostname (ctx , remoteConn , n .PrivateIP )
187
187
if err != nil {
188
- return fmt .Errorf ("failed to resolve role for %s: %s" , primaryIP , err )
188
+ return fmt .Errorf ("failed to resolve role for %s: %s" , primary . Hostname , err )
189
189
}
190
190
191
191
fmt .Printf ("My role is: %s\n " , role )
@@ -196,7 +196,7 @@ func (n *Node) Init(ctx context.Context) error {
196
196
197
197
if clonePrimary {
198
198
fmt .Println ("Cloning from primary" )
199
- if err := repmgr .clonePrimary (primaryIP ); err != nil {
199
+ if err := repmgr .clonePrimary (primary . Hostname ); err != nil {
200
200
return fmt .Errorf ("failed to clone primary: %s" , err )
201
201
}
202
202
}
@@ -205,7 +205,7 @@ func (n *Node) Init(ctx context.Context) error {
205
205
fmt .Println ("Resolving PG configuration settings." )
206
206
PGConfig .Setup ()
207
207
208
- err = SyncUserConfig (PGConfig , consul )
208
+ err = SyncUserConfig (PGConfig , cs . Store )
209
209
if err != nil {
210
210
fmt .Printf ("Failed to sync user config from consul for pgbouncer: %s\n " , err .Error ())
211
211
}
@@ -226,27 +226,21 @@ func (n *Node) PostInit(ctx context.Context) error {
226
226
}
227
227
defer conn .Close (ctx )
228
228
229
- consul , err := state .NewConsulClient ()
229
+ cs , err := state .NewClusterState ()
230
230
if err != nil {
231
- return fmt .Errorf ("failed to establish connection with consul: %s " , err )
231
+ return fmt .Errorf ("failed initialize cluster state store. %v " , err )
232
232
}
233
233
234
- primaryIP , err := consul . CurrentPrimary ()
234
+ primary , err := cs . PrimaryMember ()
235
235
if err != nil {
236
236
return fmt .Errorf ("failed to query current primary: %s" , err )
237
237
}
238
238
239
239
repmgr := n .RepMgr
240
240
pgbouncer := n .PGBouncer
241
241
242
- switch primaryIP {
243
- case n .PrivateIP :
244
- // Re-register the primary in order to pick up any changes made to the configuration file.
245
- fmt .Println ("Updating primary record" )
246
- if err := repmgr .registerPrimary (); err != nil {
247
- fmt .Printf ("failed to register primary with repmgr: %s" , err )
248
- }
249
- case "" :
242
+ switch {
243
+ case primary == nil :
250
244
// Check if we can be a primary
251
245
if ! repmgr .eligiblePrimary () {
252
246
return fmt .Errorf ("no primary to follow and can't configure self as primary because primary region is '%s' and we are in '%s'" , os .Getenv ("PRIMARY_REGION" ), repmgr .Region )
@@ -258,17 +252,22 @@ func (n *Node) PostInit(ctx context.Context) error {
258
252
}
259
253
260
254
// Setup repmgr database, extension, and register ourselves as the primary
261
- fmt .Println ("Perform Repmgr setup" )
255
+ fmt .Println ("Performing Repmgr setup" )
262
256
if err := repmgr .setup (ctx , conn ); err != nil {
263
257
fmt .Printf ("failed to setup repmgr: %s\n " , err )
264
258
}
265
259
266
- if err := consul .RegisterPrimary (n .PrivateIP ); err != nil {
267
- return fmt .Errorf ("failed to register primary with consul: %s" , err )
260
+ // Register primary member with consul
261
+ fmt .Println ("Registering member" )
262
+ if err := cs .RegisterMember (repmgr .ID , n .PrivateIP , repmgr .Region , true ); err != nil {
263
+ return fmt .Errorf ("failed to register member with consul: %s" , err )
268
264
}
269
265
270
- if err := consul .RegisterNode (repmgr .ID , n .PrivateIP ); err != nil {
271
- return fmt .Errorf ("failed to register member with consul: %s" , err )
266
+ case primary .Hostname == n .PrivateIP :
267
+ // Re-register the primary in order to pick up any changes made to the configuration file.
268
+ fmt .Println ("Updating primary record" )
269
+ if err := repmgr .registerPrimary (); err != nil {
270
+ fmt .Printf ("failed to register primary with repmgr: %s" , err )
272
271
}
273
272
default :
274
273
// If we are here we are a new node, standby or a demoted primary who needs to be reconfigured as a standby.
@@ -301,19 +300,19 @@ func (n *Node) PostInit(ctx context.Context) error {
301
300
fmt .Printf ("failed to register standby: %s\n " , err )
302
301
}
303
302
304
- fmt . Println ( "Registering Node with Consul" )
305
- if err := consul . RegisterNode (repmgr .ID , n .PrivateIP ); err != nil {
303
+ // Register member with consul if it hasn't been already
304
+ if err := cs . RegisterMember (repmgr .ID , n .PrivateIP , repmgr . Region , false ); err != nil {
306
305
return fmt .Errorf ("failed to register member with consul: %s" , err )
307
306
}
308
307
}
309
308
310
- // Requery the primaryIP in case a new primary was assigned above.
311
- primaryIP , err = consul . CurrentPrimary ()
309
+ // Requery the primaryIP from consul in case the primary was assigned above.
310
+ primary , err = cs . PrimaryMember ()
312
311
if err != nil {
313
312
return fmt .Errorf ("failed to query current primary: %s" , err )
314
313
}
315
314
316
- if err := pgbouncer .ConfigurePrimary (ctx , primaryIP , true ); err != nil {
315
+ if err := pgbouncer .ConfigurePrimary (ctx , primary . Hostname , true ); err != nil {
317
316
return fmt .Errorf ("failed to configure pgbouncer's primary: %s" , err )
318
317
}
319
318
0 commit comments