@@ -34,7 +34,6 @@ type RepMgr struct {
3434 PrimaryRegion string
3535 Region string
3636 PrivateIP string
37- MachineID string
3837 DataDir string
3938 DatabaseName string
4039 Credentials admin.Credential
@@ -162,12 +161,10 @@ func (r *RepMgr) setDefaults() error {
162161 return err
163162 }
164163
165- hostname := r .machineIdToDNS (r .MachineID )
166-
167164 conf := ConfigMap {
168165 "node_id" : nodeID ,
169- "node_name" : fmt .Sprintf ("'%s'" , hostname ),
170- "conninfo" : fmt .Sprintf ("'host=%s port=%d user=%s dbname=%s connect_timeout=5'" , hostname , r .Port , r .Credentials .Username , r .DatabaseName ),
166+ "node_name" : fmt .Sprintf ("'%s'" , r . PrivateIP ),
167+ "conninfo" : fmt .Sprintf ("'host=%s port=%d user=%s dbname=%s connect_timeout=5'" , r . PrivateIP , r .Port , r .Credentials .Username , r .DatabaseName ),
171168 "data_directory" : fmt .Sprintf ("'%s'" , r .DataDir ),
172169 "failover" : "'automatic'" ,
173170 "use_replication_slots" : "yes" ,
@@ -279,7 +276,7 @@ func (*RepMgr) restartDaemon() error {
279276}
280277
281278func (r * RepMgr ) daemonRestartRequired (m * Member ) bool {
282- return m .Hostname != r .MachineID
279+ return m .Hostname != r .PrivateIP
283280}
284281
285282func (r * RepMgr ) unregisterWitness (id int ) error {
@@ -304,14 +301,14 @@ func (r *RepMgr) rejoinCluster(hostname string) error {
304301 return err
305302}
306303
307- func (r * RepMgr ) clonePrimary (hostname string ) error {
304+ func (r * RepMgr ) clonePrimary (ipStr string ) error {
308305 cmdStr := fmt .Sprintf ("mkdir -p %s" , r .DataDir )
309306 if _ , err := utils .RunCommand (cmdStr , "postgres" ); err != nil {
310307 return fmt .Errorf ("failed to create pg directory: %s" , err )
311308 }
312309
313310 cmdStr = fmt .Sprintf ("repmgr -h %s -p %d -d %s -U %s -f %s standby clone -c -F" ,
314- hostname ,
311+ ipStr ,
315312 r .Port ,
316313 r .DatabaseName ,
317314 r .Credentials .Username ,
@@ -325,21 +322,6 @@ func (r *RepMgr) clonePrimary(hostname string) error {
325322 return nil
326323}
327324
328- func (r * RepMgr ) regenReplicationConf (ctx context.Context ) error {
329- // TODO: do we need -c?
330- if _ , err := utils .RunCmd (ctx , "postgres" ,
331- "repmgr" , "--replication-conf-only" ,
332- "-h" , "" ,
333- "-p" , fmt .Sprint (r .Port ),
334- "-d" , r .DatabaseName ,
335- "-U" , r .Credentials .Username ,
336- "-f" , r .ConfigPath ,
337- "standby" , "clone" , "-F" ); err != nil {
338- return fmt .Errorf ("failed to regenerate replication conf: %s" , err )
339- }
340- return nil
341- }
342-
343325type Member struct {
344326 ID int
345327 Hostname string
@@ -449,56 +431,26 @@ func (*RepMgr) MemberByHostname(ctx context.Context, pg *pgx.Conn, hostname stri
449431 return & member , nil
450432}
451433
452- // MemberBy6PN returns a member by its 6PN address.
453- func (r * RepMgr ) MemberBy6PN (ctx context.Context , pg * pgx.Conn , ip string ) (* Member , error ) {
454- members , err := r .Members (ctx , pg )
455- if err != nil {
456- return nil , err
457- }
458-
459- resolver := privnet .GetResolver ()
460- var lastErr error
461- for _ , member := range members {
462- ips , err := resolver .LookupIPAddr (ctx , member .Hostname )
463- if err != nil {
464- lastErr = err
465- continue
466- }
467-
468- for _ , addr := range ips {
469- if addr .IP .String () == ip {
470- return & member , nil
471- }
472- }
473- }
474-
475- if lastErr != nil {
476- return nil , fmt .Errorf ("no matches found for %s, and error encountered: %s" , ip , lastErr )
477- }
478-
479- return nil , nil
480- }
481-
482434func (r * RepMgr ) ResolveMemberOverDNS (ctx context.Context ) (* Member , error ) {
483- machineIds , err := r .InRegionPeerMachines (ctx )
435+ ips , err := r .InRegionPeerIPs (ctx )
484436 if err != nil {
485437 return nil , err
486438 }
487439
488440 var target * Member
489441
490- for _ , machineId := range machineIds {
491- if machineId == r .MachineID {
442+ for _ , ip := range ips {
443+ if ip . String () == r .PrivateIP {
492444 continue
493445 }
494446
495- conn , err := r .NewRemoteConnection (ctx , r . machineIdToDNS ( machineId ))
447+ conn , err := r .NewRemoteConnection (ctx , ip . String ( ))
496448 if err != nil {
497449 continue
498450 }
499451 defer func () { _ = conn .Close (ctx ) }()
500452
501- member , err := r .MemberByHostname (ctx , conn , r . machineIdToDNS ( machineId ))
453+ member , err := r .MemberByHostname (ctx , conn , ip . String ( ))
502454 if err != nil {
503455 continue
504456 }
@@ -525,21 +477,6 @@ func (r *RepMgr) InRegionPeerIPs(ctx context.Context) ([]net.IPAddr, error) {
525477 return privnet .AllPeers (ctx , targets )
526478}
527479
528- func (r * RepMgr ) InRegionPeerMachines (ctx context.Context ) ([]string , error ) {
529- machines , err := privnet .AllMachines (ctx , r .AppName )
530- if err != nil {
531- return nil , err
532- }
533-
534- var machineIDs []string
535- for _ , machine := range machines {
536- if machine .Region == r .PrimaryRegion {
537- machineIDs = append (machineIDs , machine .Id )
538- }
539- }
540- return machineIDs , nil
541- }
542-
543480func (r * RepMgr ) HostInRegion (ctx context.Context , hostname string ) (bool , error ) {
544481 ips , err := r .InRegionPeerIPs (ctx )
545482 if err != nil {
@@ -577,11 +514,3 @@ func (r *RepMgr) UnregisterMember(member Member) error {
577514func (r * RepMgr ) eligiblePrimary () bool {
578515 return r .Region == r .PrimaryRegion
579516}
580-
581- func (r * RepMgr ) machineIdToDNS (nodeName string ) string {
582- if len (nodeName ) != 14 {
583- panic ("invalid machine id" )
584- }
585-
586- return fmt .Sprintf ("%s.vm.%s.internal" , nodeName , r .AppName )
587- }
0 commit comments