Skip to content

Commit 093572f

Browse files
authored
Desultory refactoring of the code. (#745)
1 parent cf91b7a commit 093572f

17 files changed

+303
-112
lines changed

src/bin/pg_autoctl/cli_common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern KeeperConfig keeperOptions;
2323
extern bool createAndRun;
2424
extern bool outputJSON;
2525
extern bool openAppHBAonLAN;
26+
extern bool dropAndDestroy;
2627

2728
#define SSL_CA_FILE_FLAG 1 /* root public certificate */
2829
#define SSL_CRL_FILE_FLAG 2 /* certificates revocation list */
@@ -172,8 +173,15 @@ void cli_set_groupId(Monitor *monitor, KeeperConfig *kconfig);
172173
bool cli_create_config(Keeper *keeper);
173174
void cli_create_pg(Keeper *keeper);
174175
bool check_or_discover_hostname(KeeperConfig *config);
176+
177+
int cli_drop_node_getopts(int argc, char **argv);
178+
void cli_drop_node(int argc, char **argv);
175179
void keeper_cli_destroy_node(int argc, char **argv);
176180

181+
void cli_drop_node_from_monitor(KeeperConfig *config,
182+
int64_t *nodeId, int *groupId);
183+
void cli_drop_local_node(KeeperConfig *config, bool dropAndDestroy);
184+
177185
bool cli_getopt_ssl_flags(int ssl_flag, char *optarg, PostgresSetup *pgSetup);
178186
bool cli_getopt_accept_ssl_options(SSLCommandLineOptions newSSLOption,
179187
SSLCommandLineOptions currentSSLOptions);

src/bin/pg_autoctl/cli_do_monitor.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ cli_do_monitor_get_coordinator(int argc, char **argv)
352352
{
353353
KeeperConfig config = keeperOptions;
354354
Monitor monitor = { 0 };
355-
NodeAddress coordinatorNode = { 0 };
355+
CoordinatorNodeAddress coordinatorNode = { 0 };
356356

357357
bool missingPgdataIsOk = true;
358358
bool pgIsNotRunningIsOk = true;
@@ -381,7 +381,7 @@ cli_do_monitor_get_coordinator(int argc, char **argv)
381381
exit(EXIT_CODE_MONITOR);
382382
}
383383

384-
if (IS_EMPTY_STRING_BUFFER(coordinatorNode.host))
384+
if (IS_EMPTY_STRING_BUFFER(coordinatorNode.node.host))
385385
{
386386
fformat(stdout, "%s has no coordinator ready yet\n", config.formation);
387387
exit(EXIT_CODE_QUIT);
@@ -395,8 +395,8 @@ cli_do_monitor_get_coordinator(int argc, char **argv)
395395

396396
json_object_set_string(root, "formation", config.formation);
397397
json_object_set_number(root, "groupId", (double) config.groupId);
398-
json_object_set_string(root, "host", coordinatorNode.host);
399-
json_object_set_number(root, "port", (double) coordinatorNode.port);
398+
json_object_set_string(root, "host", coordinatorNode.node.host);
399+
json_object_set_number(root, "port", (double) coordinatorNode.node.port);
400400

401401
(void) cli_pprint_json(js);
402402
}
@@ -405,8 +405,8 @@ cli_do_monitor_get_coordinator(int argc, char **argv)
405405
fformat(stdout,
406406
"%s %s:%d\n",
407407
config.formation,
408-
coordinatorNode.host,
409-
coordinatorNode.port);
408+
coordinatorNode.node.host,
409+
coordinatorNode.node.port);
410410
}
411411
}
412412

src/bin/pg_autoctl/cli_drop_node.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,11 @@
4545
* Global variables that we're going to use to "communicate" in between getopts
4646
* functions and their command implementation. We can't pass parameters around.
4747
*/
48-
static bool dropAndDestroy = false;
48+
bool dropAndDestroy = false;
4949
static bool dropForce = false;
5050

51-
static int cli_drop_node_getopts(int argc, char **argv);
52-
static void cli_drop_node(int argc, char **argv);
5351
static void cli_drop_monitor(int argc, char **argv);
5452

55-
static void cli_drop_node_from_monitor(KeeperConfig *config,
56-
int64_t *nodeId,
57-
int *groupId);
58-
59-
static void cli_drop_local_node(KeeperConfig *config, bool dropAndDestroy);
6053
static void cli_drop_local_monitor(MonitorConfig *mconfig, bool dropAndDestroy);
6154

6255
static void cli_drop_node_with_monitor_disabled(KeeperConfig *config,
@@ -98,7 +91,7 @@ CommandLine drop_node_command =
9891
* cli_drop_node_getopts parses the command line options necessary to drop or
9992
* destroy a local pg_autoctl node.
10093
*/
101-
static int
94+
int
10295
cli_drop_node_getopts(int argc, char **argv)
10396
{
10497
KeeperConfig options = { 0 };
@@ -337,7 +330,7 @@ cli_drop_node_getopts(int argc, char **argv)
337330
* cli_drop_node removes the local PostgreSQL node from the pg_auto_failover
338331
* monitor, and when it's a worker, from the Citus coordinator too.
339332
*/
340-
static void
333+
void
341334
cli_drop_node(int argc, char **argv)
342335
{
343336
KeeperConfig config = keeperOptions;
@@ -504,7 +497,7 @@ cli_drop_monitor(int argc, char **argv)
504497
* for the given --hostname and --pgport, or from the given --formation and
505498
* --name.
506499
*/
507-
static void
500+
void
508501
cli_drop_node_from_monitor(KeeperConfig *config, int64_t *nodeId, int *groupId)
509502
{
510503
Monitor monitor = { 0 };
@@ -563,7 +556,7 @@ cli_drop_node_from_monitor(KeeperConfig *config, int64_t *nodeId, int *groupId)
563556
* cli_drop_local_node drops the local node files, maybe including the PGDATA
564557
* directory (when --destroy has been used).
565558
*/
566-
static void
559+
void
567560
cli_drop_local_node(KeeperConfig *config, bool dropAndDestroy)
568561
{
569562
Keeper keeper = { 0 };

src/bin/pg_autoctl/fsm.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@
175175
#define COMMENT_INIT_TO_REPORT_LSN \
176176
"Creating a new node from a standby node that is not a candidate."
177177

178+
#define COMMENT_DROPPED_TO_REPORT_LSN \
179+
"This node is being reinitialized after having been dropped"
180+
178181
#define COMMENT_ANY_TO_DROPPED \
179182
"This node is being dropped from the monitor"
180183

@@ -200,7 +203,7 @@ KeeperFSMTransition KeeperFSM[] = {
200203
*/
201204
{ INIT_STATE, SINGLE_STATE, COMMENT_INIT_TO_SINGLE, &fsm_init_primary },
202205
{ DROPPED_STATE, SINGLE_STATE, COMMENT_INIT_TO_SINGLE, &fsm_init_primary },
203-
206+
{ DROPPED_STATE, REPORT_LSN_STATE, COMMENT_DROPPED_TO_REPORT_LSN, &fsm_init_from_standby },
204207

205208
/*
206209
* The previous implementation has a transition from any state to the INIT
@@ -362,7 +365,7 @@ KeeperFSMTransition KeeperFSM[] = {
362365
{ REPORT_LSN_STATE, PREP_PROMOTION_STATE, COMMENT_REPORT_LSN_TO_PREP_PROMOTION, &fsm_prepare_standby_for_promotion },
363366

364367
{ REPORT_LSN_STATE, FAST_FORWARD_STATE, COMMENT_REPORT_LSN_TO_FAST_FORWARD, &fsm_fast_forward },
365-
{ FAST_FORWARD_STATE, PREP_PROMOTION_STATE, COMMENT_FAST_FORWARD_TO_PREP_PROMOTION, &fsm_cleanup_and_resume_as_primary },
368+
{ FAST_FORWARD_STATE, PREP_PROMOTION_STATE, COMMENT_FAST_FORWARD_TO_PREP_PROMOTION, &fsm_cleanup_as_primary },
366369

367370
{ REPORT_LSN_STATE, JOIN_SECONDARY_STATE, COMMENT_REPORT_LSN_TO_JOIN_SECONDARY, &fsm_checkpoint_and_stop_postgres },
368371
{ REPORT_LSN_STATE, SECONDARY_STATE, COMMENT_REPORT_LSN_TO_JOIN_SECONDARY, &fsm_follow_new_primary },
@@ -513,6 +516,7 @@ keeper_fsm_reach_assigned_state(Keeper *keeper)
513516
{
514517
bool ret = false;
515518

519+
/* avoid logging "#any state#" to the user */
516520
if (transition.current != ANY_STATE)
517521
{
518522
log_info("FSM transition from \"%s\" to \"%s\"%s%s",
@@ -551,10 +555,19 @@ keeper_fsm_reach_assigned_state(Keeper *keeper)
551555
}
552556
else
553557
{
554-
log_error("Failed to transition from state \"%s\" "
555-
"to state \"%s\", see above.",
556-
NodeStateToString(transition.current),
557-
NodeStateToString(transition.assigned));
558+
/* avoid logging "#any state#" to the user */
559+
if (transition.current != ANY_STATE)
560+
{
561+
log_error("Failed to transition from state \"%s\" "
562+
"to state \"%s\", see above.",
563+
NodeStateToString(transition.current),
564+
NodeStateToString(transition.assigned));
565+
}
566+
else
567+
{
568+
log_error("Failed to transition to state \"%s\", see above.",
569+
NodeStateToString(transition.assigned));
570+
}
558571
}
559572

560573
return ret;

src/bin/pg_autoctl/fsm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool fsm_report_lsn(Keeper *keeper);
6767
bool fsm_fast_forward(Keeper *keeper);
6868
bool fsm_prepare_cascade(Keeper *keeper);
6969
bool fsm_follow_new_primary(Keeper *keeper);
70-
bool fsm_cleanup_and_resume_as_primary(Keeper *keeper);
70+
bool fsm_cleanup_as_primary(Keeper *keeper);
7171

7272
bool fsm_init_from_standby(Keeper *keeper);
7373

src/bin/pg_autoctl/fsm_transition.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,11 +1325,11 @@ fsm_fast_forward(Keeper *keeper)
13251325

13261326

13271327
/*
1328-
* fsm_cleanup_and_resume_as_primary cleans-up the replication setting and
1329-
* start the local node as primary. It's called after a fast-forward operation.
1328+
* fsm_cleanup_as_primary cleans-up the replication setting. It's called after
1329+
* a fast-forward operation.
13301330
*/
13311331
bool
1332-
fsm_cleanup_and_resume_as_primary(Keeper *keeper)
1332+
fsm_cleanup_as_primary(Keeper *keeper)
13331333
{
13341334
LocalPostgresServer *postgres = &(keeper->postgres);
13351335

@@ -1340,13 +1340,6 @@ fsm_cleanup_and_resume_as_primary(Keeper *keeper)
13401340
return false;
13411341
}
13421342

1343-
if (!keeper_restart_postgres(keeper))
1344-
{
1345-
log_error("Failed to restart Postgres after changing its "
1346-
"primary conninfo, see above for details");
1347-
return false;
1348-
}
1349-
13501343
return true;
13511344
}
13521345

0 commit comments

Comments
 (0)