Skip to content

Commit 9940967

Browse files
committed
Refactoring. Get rid of global variable backup_instance_path
1 parent b956feb commit 9940967

File tree

8 files changed

+71
-87
lines changed

8 files changed

+71
-87
lines changed

src/backup.c

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ static void *backup_files(void *arg);
5050
static void do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
5151
PGNodeInfo *nodeInfo, bool no_sync, bool backup_logs);
5252

53-
static void pg_start_backup(const char *label, bool smooth, pgBackup *backup,
53+
static void pg_start_backup(InstanceState *instanceState, const char *label, bool smooth, pgBackup *backup,
5454
PGNodeInfo *nodeInfo, PGconn *conn);
5555
static void pg_switch_wal(PGconn *conn);
56-
static void pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn, PGNodeInfo *nodeInfo);
56+
static void pg_stop_backup(InstanceState *instanceState, pgBackup *backup, PGconn *pg_startbackup_conn, PGNodeInfo *nodeInfo);
5757

58-
static XLogRecPtr wait_wal_lsn(XLogRecPtr lsn, bool is_start_lsn, TimeLineID tli,
58+
static XLogRecPtr wait_wal_lsn(InstanceState *instanceState, XLogRecPtr lsn, bool is_start_lsn, TimeLineID tli,
5959
bool in_prev_segment, bool segment_only,
6060
int timeout_elevel, bool in_stream_dir);
6161

@@ -77,14 +77,15 @@ static void set_cfs_datafiles(parray *files, const char *root, char *relative, s
7777
static void
7878
backup_stopbackup_callback(bool fatal, void *userdata)
7979
{
80-
PGconn *pg_startbackup_conn = (PGconn *) userdata;
80+
InstanceState *instanceState = (InstanceState *) userdata;
81+
PGconn *pg_startbackup_conn = instanceState->conn;
8182
/*
8283
* If backup is in progress, notify stop of backup to PostgreSQL
8384
*/
8485
if (backup_in_progress)
8586
{
8687
elog(WARNING, "backup in progress, stop backup");
87-
pg_stop_backup(NULL, pg_startbackup_conn, NULL); /* don't care about stop_lsn in case of error */
88+
pg_stop_backup(instanceState, NULL, pg_startbackup_conn, NULL); /* don't care about stop_lsn in case of error */
8889
}
8990
}
9091

@@ -139,7 +140,7 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
139140
strlen(" with pg_probackup"));
140141

141142
/* Call pg_start_backup function in PostgreSQL connect */
142-
pg_start_backup(label, smooth_checkpoint, &current, nodeInfo, backup_conn);
143+
pg_start_backup(instanceState, label, smooth_checkpoint, &current, nodeInfo, backup_conn);
143144

144145
/* Obtain current timeline */
145146
#if PG_VERSION_NUM >= 90600
@@ -270,7 +271,7 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
270271
* Because WAL streaming will start after pg_start_backup() in stream
271272
* mode.
272273
*/
273-
wait_wal_lsn(current.start_lsn, true, current.tli, false, true, ERROR, false);
274+
wait_wal_lsn(instanceState, current.start_lsn, true, current.tli, false, true, ERROR, false);
274275
}
275276

276277
/* start stream replication */
@@ -527,7 +528,7 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
527528
}
528529

529530
/* Notify end of backup */
530-
pg_stop_backup(&current, backup_conn, nodeInfo);
531+
pg_stop_backup(instanceState, &current, backup_conn, nodeInfo);
531532

532533
/* In case of backup from replica >= 9.6 we must fix minRecPoint,
533534
* First we must find pg_control in backup_files_list.
@@ -1028,7 +1029,7 @@ confirm_block_size(PGconn *conn, const char *name, int blcksz)
10281029
* Notify start of backup to PostgreSQL server.
10291030
*/
10301031
static void
1031-
pg_start_backup(const char *label, bool smooth, pgBackup *backup,
1032+
pg_start_backup(InstanceState *instanceState, const char *label, bool smooth, pgBackup *backup,
10321033
PGNodeInfo *nodeInfo, PGconn *conn)
10331034
{
10341035
PGresult *res;
@@ -1056,7 +1057,8 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup,
10561057
* is necessary to call pg_stop_backup() in backup_cleanup().
10571058
*/
10581059
backup_in_progress = true;
1059-
pgut_atexit_push(backup_stopbackup_callback, conn);
1060+
instanceState->conn = conn;
1061+
pgut_atexit_push(backup_stopbackup_callback, instanceState);
10601062

10611063
/* Extract timeline and LSN from results of pg_start_backup() */
10621064
XLogDataFromLSN(PQgetvalue(res, 0, 0), &lsn_hi, &lsn_lo);
@@ -1262,7 +1264,7 @@ pg_is_superuser(PGconn *conn)
12621264
* Returns InvalidXLogRecPtr if 'segment_only' flag is used.
12631265
*/
12641266
static XLogRecPtr
1265-
wait_wal_lsn(XLogRecPtr target_lsn, bool is_start_lsn, TimeLineID tli,
1267+
wait_wal_lsn(InstanceState *instanceState, XLogRecPtr target_lsn, bool is_start_lsn, TimeLineID tli,
12661268
bool in_prev_segment, bool segment_only,
12671269
int timeout_elevel, bool in_stream_dir)
12681270
{
@@ -1296,8 +1298,9 @@ wait_wal_lsn(XLogRecPtr target_lsn, bool is_start_lsn, TimeLineID tli,
12961298
*/
12971299
if (in_stream_dir)
12981300
{
1299-
pgBackupGetPath2(&current, pg_wal_dir, lengthof(pg_wal_dir),
1300-
DATABASE_DIR, PG_XLOG_DIR);
1301+
snprintf(pg_wal_dir, lengthof(pg_wal_dir), "%s/%s/%s/%s",
1302+
instanceState->instance_backup_subdir_path, base36enc(current.start_time),
1303+
DATABASE_DIR, PG_XLOG_DIR);
13011304
join_path_components(wal_segment_path, pg_wal_dir, wal_segment);
13021305
wal_segment_dir = pg_wal_dir;
13031306
}
@@ -1438,7 +1441,7 @@ wait_wal_lsn(XLogRecPtr target_lsn, bool is_start_lsn, TimeLineID tli,
14381441
* Notify end of backup to PostgreSQL server.
14391442
*/
14401443
static void
1441-
pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
1444+
pg_stop_backup(InstanceState *instanceState, pgBackup *backup, PGconn *pg_startbackup_conn,
14421445
PGNodeInfo *nodeInfo)
14431446
{
14441447
PGconn *conn;
@@ -1566,7 +1569,8 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
15661569
}
15671570

15681571
/* After we have sent pg_stop_backup, we don't need this callback anymore */
1569-
pgut_atexit_pop(backup_stopbackup_callback, pg_startbackup_conn);
1572+
instanceState->conn = pg_startbackup_conn;
1573+
pgut_atexit_pop(backup_stopbackup_callback, instanceState);
15701574

15711575
/*
15721576
* Wait for the result of pg_stop_backup(), but no longer than
@@ -1671,9 +1675,10 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
16711675

16721676
if (stream_wal)
16731677
{
1674-
pgBackupGetPath2(backup, stream_xlog_path,
1675-
lengthof(stream_xlog_path),
1676-
DATABASE_DIR, PG_XLOG_DIR);
1678+
snprintf(stream_xlog_path, lengthof(stream_xlog_path),
1679+
"%s/%s/%s/%s", instanceState->instance_backup_subdir_path,
1680+
base36enc(backup->start_time),
1681+
DATABASE_DIR, PG_XLOG_DIR);
16771682
xlog_path = stream_xlog_path;
16781683
}
16791684
else
@@ -1702,7 +1707,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
17021707
if (stop_backup_lsn_tmp % instance_config.xlog_seg_size == 0)
17031708
{
17041709
/* Wait for segment with current stop_lsn, it is ok for it to never arrive */
1705-
wait_wal_lsn(stop_backup_lsn_tmp, false, backup->tli,
1710+
wait_wal_lsn(instanceState, stop_backup_lsn_tmp, false, backup->tli,
17061711
false, true, WARNING, stream_wal);
17071712

17081713
/* Get the first record in segment with current stop_lsn */
@@ -1730,7 +1735,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
17301735
/* Despite looking for previous record there is not guarantee of success
17311736
* because previous record can be the contrecord.
17321737
*/
1733-
lsn_tmp = wait_wal_lsn(stop_backup_lsn_tmp, false, backup->tli,
1738+
lsn_tmp = wait_wal_lsn(instanceState, stop_backup_lsn_tmp, false, backup->tli,
17341739
true, false, ERROR, stream_wal);
17351740

17361741
/* sanity */
@@ -1744,7 +1749,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
17441749
else if (stop_backup_lsn_tmp % XLOG_BLCKSZ == 0)
17451750
{
17461751
/* Wait for segment with current stop_lsn */
1747-
wait_wal_lsn(stop_backup_lsn_tmp, false, backup->tli,
1752+
wait_wal_lsn(instanceState, stop_backup_lsn_tmp, false, backup->tli,
17481753
false, true, ERROR, stream_wal);
17491754

17501755
/* Get the next closest record in segment with current stop_lsn */
@@ -1776,7 +1781,8 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
17761781
if (!exclusive_backup)
17771782
{
17781783
Assert(PQnfields(res) >= 4);
1779-
pgBackupGetPath2(backup, path, lengthof(path), DATABASE_DIR, NULL);
1784+
snprintf(path, lengthof(path), "%s/%s/%s", instanceState->instance_backup_subdir_path,
1785+
base36enc(backup->start_time), DATABASE_DIR);
17801786

17811787
/* Write backup_label */
17821788
join_path_components(backup_label, path, PG_BACKUP_LABEL_FILE);
@@ -1873,7 +1879,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
18731879
* look for previous record with endpoint >= STOP_LSN.
18741880
*/
18751881
if (!stop_lsn_exists)
1876-
stop_backup_lsn = wait_wal_lsn(stop_backup_lsn_tmp, false, backup->tli,
1882+
stop_backup_lsn = wait_wal_lsn(instanceState, stop_backup_lsn_tmp, false, backup->tli,
18771883
false, false, ERROR, stream_wal);
18781884

18791885
if (stream_wal)
@@ -1883,9 +1889,10 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
18831889
if(wait_WAL_streaming_end(backup_files_list))
18841890
elog(ERROR, "WAL streaming failed");
18851891

1886-
pgBackupGetPath2(backup, stream_xlog_path,
1887-
lengthof(stream_xlog_path),
1888-
DATABASE_DIR, PG_XLOG_DIR);
1892+
snprintf(stream_xlog_path, lengthof(stream_xlog_path), "%s/%s/%s/%s",
1893+
instanceState->instance_backup_subdir_path, base36enc(backup->start_time),
1894+
DATABASE_DIR, PG_XLOG_DIR);
1895+
18891896
xlog_path = stream_xlog_path;
18901897
}
18911898
else

src/catalog.c

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,8 @@ catalog_get_instance_list(CatalogState *catalogState)
733733
catalogState->backup_subdir_path, instanceState->instance_name);
734734
join_path_components(instanceState->instance_wal_subdir_path,
735735
catalogState->wal_subdir_path, instanceState->instance_name);
736-
736+
join_path_components(instanceState->instance_config_path,
737+
instanceState->instance_backup_subdir_path, BACKUP_CATALOG_CONF_FILE);
737738
parray_append(instances, instanceState);
738739
}
739740

@@ -2808,27 +2809,6 @@ pgBackupCompareIdDesc(const void *l, const void *r)
28082809
return -pgBackupCompareId(l, r);
28092810
}
28102811

2811-
/*
2812-
* Construct absolute path of the backup directory.
2813-
* Append "subdir1" and "subdir2" to the backup directory.
2814-
*/
2815-
void
2816-
pgBackupGetPath2(const pgBackup *backup, char *path, size_t len,
2817-
const char *subdir1, const char *subdir2)
2818-
{
2819-
/* If "subdir1" is NULL do not check "subdir2" */
2820-
if (!subdir1)
2821-
snprintf(path, len, "%s/%s", backup_instance_path,
2822-
base36enc(backup->start_time));
2823-
else if (!subdir2)
2824-
snprintf(path, len, "%s/%s/%s", backup_instance_path,
2825-
base36enc(backup->start_time), subdir1);
2826-
/* "subdir1" and "subdir2" is not NULL */
2827-
else
2828-
snprintf(path, len, "%s/%s/%s/%s", backup_instance_path,
2829-
base36enc(backup->start_time), subdir1, subdir2);
2830-
}
2831-
28322812
/*
28332813
* Check if multiple backups consider target backup to be their direct parent
28342814
*/

src/configure.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,16 @@ do_show_config(void)
277277
* values into the file.
278278
*/
279279
void
280-
do_set_config(bool missing_ok)
280+
do_set_config(InstanceState *instanceState, bool missing_ok)
281281
{
282-
char path[MAXPGPATH];
283282
char path_temp[MAXPGPATH];
284283
FILE *fp;
285284
int i;
286285

287-
join_path_components(path, backup_instance_path, BACKUP_CATALOG_CONF_FILE);
288-
snprintf(path_temp, sizeof(path_temp), "%s.tmp", path);
286+
snprintf(path_temp, sizeof(path_temp), "%s.tmp", instanceState->instance_config_path);
289287

290-
if (!missing_ok && !fileExists(path, FIO_LOCAL_HOST))
291-
elog(ERROR, "Configuration file \"%s\" doesn't exist", path);
288+
if (!missing_ok && !fileExists(instanceState->instance_config_path, FIO_LOCAL_HOST))
289+
elog(ERROR, "Configuration file \"%s\" doesn't exist", instanceState->instance_config_path);
292290

293291
fp = fopen(path_temp, "wt");
294292
if (fp == NULL)
@@ -327,12 +325,12 @@ do_set_config(bool missing_ok)
327325

328326
fclose(fp);
329327

330-
if (rename(path_temp, path) < 0)
328+
if (rename(path_temp, instanceState->instance_config_path) < 0)
331329
{
332330
int errno_temp = errno;
333331
unlink(path_temp);
334332
elog(ERROR, "Cannot rename configuration file \"%s\" to \"%s\": %s",
335-
path_temp, path, strerror(errno_temp));
333+
path_temp, instanceState->instance_config_path, strerror(errno_temp));
336334
}
337335
}
338336

@@ -376,7 +374,6 @@ init_config(InstanceConfig *config, const char *instance_name)
376374
InstanceConfig *
377375
readInstanceConfigFile(InstanceState *instanceState)
378376
{
379-
char path[MAXPGPATH];
380377
InstanceConfig *instance = pgut_new(InstanceConfig);
381378
char *log_level_console = NULL;
382379
char *log_level_file = NULL;
@@ -602,21 +599,19 @@ readInstanceConfigFile(InstanceState *instanceState)
602599
instanceState->catalog_state->catalog_path, "wal", instanceState->instance_name);
603600
canonicalize_path(instance->arclog_path);
604601

605-
join_path_components(path, instance->backup_instance_path,
606-
BACKUP_CATALOG_CONF_FILE);
607-
608-
if (fio_access(path, F_OK, FIO_BACKUP_HOST) != 0)
602+
if (fio_access(instanceState->instance_config_path, F_OK, FIO_BACKUP_HOST) != 0)
609603
{
610-
elog(WARNING, "Control file \"%s\" doesn't exist", path);
604+
elog(WARNING, "Control file \"%s\" doesn't exist", instanceState->instance_config_path);
611605
pfree(instance);
612606
return NULL;
613607
}
614608

615-
parsed_options = config_read_opt(path, instance_options, WARNING, true, true);
609+
parsed_options = config_read_opt(instanceState->instance_config_path,
610+
instance_options, WARNING, true, true);
616611

617612
if (parsed_options == 0)
618613
{
619-
elog(WARNING, "Control file \"%s\" is empty", path);
614+
elog(WARNING, "Control file \"%s\" is empty", instanceState->instance_config_path);
620615
pfree(instance);
621616
return NULL;
622617
}

src/delete.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,6 @@ do_delete_instance(InstanceState *instanceState)
972972
{
973973
parray *backup_list;
974974
int i;
975-
char instance_config_path[MAXPGPATH];
976-
977975

978976
/* Delete all backups. */
979977
backup_list = catalog_get_backup_list(instanceState, INVALID_BACKUP_ID);
@@ -994,16 +992,15 @@ do_delete_instance(InstanceState *instanceState)
994992
pgut_rmtree(arclog_path, false, true);
995993

996994
/* Delete backup instance config file */
997-
join_path_components(instance_config_path, backup_instance_path, BACKUP_CATALOG_CONF_FILE);
998-
if (remove(instance_config_path))
995+
if (remove(instanceState->instance_config_path))
999996
{
1000-
elog(ERROR, "Can't remove \"%s\": %s", instance_config_path,
997+
elog(ERROR, "Can't remove \"%s\": %s", instanceState->instance_config_path,
1001998
strerror(errno));
1002999
}
10031000

10041001
/* Delete instance root directories */
1005-
if (rmdir(backup_instance_path) != 0)
1006-
elog(ERROR, "Can't remove \"%s\": %s", backup_instance_path,
1002+
if (rmdir(instanceState->instance_backup_subdir_path) != 0)
1003+
elog(ERROR, "Can't remove \"%s\": %s", instanceState->instance_backup_subdir_path,
10071004
strerror(errno));
10081005

10091006
if (rmdir(arclog_path) != 0)

src/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ do_add_instance(InstanceState *instanceState, InstanceConfig *instance)
119119
SOURCE_DEFAULT);
120120

121121
/* pgdata was set through command line */
122-
do_set_config(true);
122+
do_set_config(instanceState, true);
123123

124124
elog(INFO, "Instance '%s' successfully inited", instanceState->instance_name);
125125
return 0;

src/pg_probackup.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static char *backup_path = NULL;
6868
* path or to the data files in the backup catalog
6969
* $BACKUP_PATH/backups/instance_name
7070
*/
71-
char backup_instance_path[MAXPGPATH];
71+
static char backup_instance_path[MAXPGPATH];
7272
/*
7373
* path or to the wal files in the backup catalog
7474
* $BACKUP_PATH/wal/instance_name
@@ -491,6 +491,9 @@ main(int argc, char *argv[])
491491
catalogState->backup_subdir_path, instanceState->instance_name);
492492
join_path_components(instanceState->instance_wal_subdir_path,
493493
catalogState->wal_subdir_path, instanceState->instance_name);
494+
join_path_components(instanceState->instance_config_path,
495+
instanceState->instance_backup_subdir_path, BACKUP_CATALOG_CONF_FILE);
496+
494497
}
495498
/* ===== instanceState (END) ======*/
496499

@@ -872,7 +875,7 @@ main(int argc, char *argv[])
872875
do_show_config();
873876
break;
874877
case SET_CONFIG_CMD:
875-
do_set_config(false);
878+
do_set_config(instanceState, false);
876879
break;
877880
case SET_BACKUP_CMD:
878881
if (!backup_id_string)

0 commit comments

Comments
 (0)