77 "fmt"
88 "log"
99 "os"
10- "strconv"
11- "time"
1210
1311 "github.com/fly-apps/postgres-flex/internal/flypg"
1412 "github.com/jackc/pgx/v5"
@@ -23,7 +21,6 @@ func main() {
2321 // old primary. In the events that we subscribe to it's always either empty or the new primary.
2422 // In the future if we subscribe to repmgrd_failover_promote, then we would have to change this
2523 // name.
26- newPrimary := flag .String ("new-node-id" , "" , "the new primary node id" )
2724 success := flag .String ("success" , "" , "success (1) failure (0)" )
2825 details := flag .String ("details" , "" , "details" )
2926 flag .Parse ()
@@ -39,69 +36,14 @@ func main() {
3936 log .SetOutput (logFile )
4037 log .Printf ("event: %s, node: %d, success: %s, details: %s\n " , * event , * nodeID , * success , * details )
4138
42- switch * event {
43- case "repmgrd_failover_promote" , "standby_promote" :
44- // TODO - Need to figure out what to do when success == 0.
45- retry := 0
46- maxRetries := 5
47- success := false
48-
49- for retry < maxRetries {
50- if err := reconfigurePGBouncer (* nodeID ); err != nil {
51- log .Printf ("%s - failed to reconfigure pgbouncer: %s. (attempt: %d)\n " , * event , err , retry )
52- retry ++
53- time .Sleep (1 * time .Second )
54- continue
55- }
56- success = true
57- break
58- }
59-
60- if success {
61- log .Printf ("%s - successfully reconfigured pgbouncer to target: %d\n " , * event , * nodeID )
62- os .Exit (0 )
63- } else {
64- log .Printf ("%s - failed to reconfigured pgbouncer to target: %d\n " , * event , * nodeID )
65- os .Exit (1 )
66- }
67-
68- case "standby_follow" :
69- newMemberID , err := strconv .Atoi (* newPrimary )
70- if err != nil {
71- log .Printf ("failed to parse newMemberID %s: %s\n " , * newPrimary , err )
72- os .Exit (1 )
73- }
74-
75- retry := 0
76- maxRetries := 5
77- success := false
78-
79- for retry < maxRetries {
80- if err := reconfigurePGBouncer (newMemberID ); err != nil {
81- log .Printf ("%s - failed to reconfigure pgbouncer: %s. (attempt: %d)\n " , * event , err , retry )
82- retry ++
83- time .Sleep (1 * time .Second )
84- continue
85- }
86- success = true
87- break
88- }
89-
90- if success {
91- log .Printf ("%s - successfully reconfigured pgbouncer to target: %d\n " , * event , newMemberID )
92- os .Exit (0 )
93- } else {
94- log .Printf ("%s - failed to reconfigured pgbouncer to target: %d\n " , * event , newMemberID )
95- os .Exit (1 )
96- }
39+ node , err := flypg .NewNode ()
40+ if err != nil {
41+ log .Printf ("failed to initialize node: %s" , err )
42+ os .Exit (1 )
43+ }
9744
45+ switch * event {
9846 case "child_node_disconnect" , "child_node_reconnect" , "child_node_new_connect" :
99- node , err := flypg .NewNode ()
100- if err != nil {
101- log .Printf ("failed to initialize node: %s" , err )
102- os .Exit (1 )
103- }
104-
10547 conn , err := node .RepMgr .NewLocalConnection (ctx )
10648 if err != nil {
10749 log .Printf ("failed to open local connection: %s" , err )
@@ -132,29 +74,6 @@ func main() {
13274 }
13375}
13476
135- func reconfigurePGBouncer (id int ) error {
136- node , err := flypg .NewNode ()
137- if err != nil {
138- return fmt .Errorf ("failed to reference node: %s" , err )
139- }
140-
141- conn , err := node .RepMgr .NewLocalConnection (context .TODO ())
142- if err != nil {
143- return fmt .Errorf ("failed to establish connection with local pg: %s" , err )
144- }
145-
146- member , err := node .RepMgr .MemberByID (context .TODO (), conn , id )
147- if err != nil {
148- return err
149- }
150-
151- if err := node .PGBouncer .ConfigurePrimary (context .TODO (), member .Hostname , true ); err != nil {
152- return fmt .Errorf ("failed to reconfigure pgbouncer primary %s" , err )
153- }
154-
155- return nil
156- }
157-
15877func evaluateClusterState (ctx context.Context , conn * pgx.Conn , node * flypg.Node ) error {
15978 standbys , err := node .RepMgr .StandbyMembers (ctx , conn )
16079 if err != nil {
@@ -195,9 +114,5 @@ func evaluateClusterState(ctx context.Context, conn *pgx.Conn, node *flypg.Node)
195114 }
196115 }
197116
198- if err := node .PGBouncer .ConfigurePrimary (ctx , primary , true ); err != nil {
199- return fmt .Errorf ("failed to reconfigure pgbouncer primary %s" , err )
200- }
201-
202117 return nil
203118}
0 commit comments