Skip to content

Commit 2c1c19a

Browse files
committed
[Issue #146] Refactoring
1 parent 30fbea7 commit 2c1c19a

File tree

4 files changed

+73
-29
lines changed

4 files changed

+73
-29
lines changed

src/backup.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ do_backup_instance(PGconn *backup_conn, PGNodeInfo *nodeInfo, bool no_sync)
156156
/* used for multitimeline incremental backup */
157157
parray *tli_list = NULL;
158158

159-
160159
/* for fancy reporting */
161160
time_t start_time, end_time;
162161
char pretty_time[20];
@@ -820,9 +819,7 @@ do_backup(time_t start_time, bool no_validate,
820819
/* Update backup status and other metainfo. */
821820
current.status = BACKUP_STATUS_RUNNING;
822821
current.start_time = start_time;
823-
824-
current.note = set_backup_params ? set_backup_params->note : NULL;
825-
822+
826823
StrNCpy(current.program_version, PROGRAM_VERSION,
827824
sizeof(current.program_version));
828825

@@ -905,6 +902,10 @@ do_backup(time_t start_time, bool no_validate,
905902
if (instance_config.master_conn_opt.pghost == NULL)
906903
elog(ERROR, "Options for connection to master must be provided to perform backup from replica");
907904

905+
/* add note to backup if requested */
906+
if (set_backup_params && set_backup_params->note)
907+
add_note(&current, set_backup_params->note);
908+
908909
/* backup data */
909910
do_backup_instance(backup_conn, &nodeInfo, no_sync);
910911
pgut_atexit_pop(backup_cleanup, NULL);
@@ -937,8 +938,7 @@ do_backup(time_t start_time, bool no_validate,
937938
(set_backup_params->ttl > 0 ||
938939
set_backup_params->expire_time > 0))
939940
{
940-
if (!pin_backup(&current, set_backup_params))
941-
elog(ERROR, "Failed to pin the backup %s", base36enc(current.backup_id));
941+
pin_backup(&current, set_backup_params);
942942
}
943943

944944
if (!no_validate)

src/catalog.c

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,18 +1534,23 @@ do_set_backup(const char *instance_name, time_t backup_id,
15341534

15351535
target_backup = (pgBackup *) parray_get(backup_list, 0);
15361536

1537-
if (!pin_backup(target_backup, set_backup_params))
1538-
elog(ERROR, "Failed to pin the backup %s", base36enc(backup_id));
1537+
/* Pin or unpin backup if requested */
1538+
if (set_backup_params->ttl >= 0 || set_backup_params->expire_time > 0)
1539+
pin_backup(target_backup, set_backup_params);
1540+
1541+
if (set_backup_params->note)
1542+
add_note(target_backup, set_backup_params->note);
15391543
}
15401544

15411545
/*
15421546
* Set 'expire-time' attribute based on set_backup_params, or unpin backup
15431547
* if ttl is equal to zero.
15441548
*/
1545-
bool
1549+
void
15461550
pin_backup(pgBackup *target_backup, pgSetBackupParams *set_backup_params)
15471551
{
15481552

1553+
/* sanity, backup must have positive recovery-time */
15491554
if (target_backup->recovery_time <= 0)
15501555
elog(ERROR, "Failed to set 'expire-time' for backup %s: invalid 'recovery-time'",
15511556
base36enc(target_backup->backup_id));
@@ -1563,17 +1568,16 @@ pin_backup(pgBackup *target_backup, pgSetBackupParams *set_backup_params)
15631568
{
15641569
elog(WARNING, "Backup %s is not pinned, nothing to unpin",
15651570
base36enc(target_backup->start_time));
1566-
return false;
1571+
return;
15671572
}
15681573
target_backup->expire_time = 0;
15691574
}
15701575
/* Pin comes from expire-time */
15711576
else if (set_backup_params->expire_time > 0)
15721577
target_backup->expire_time = set_backup_params->expire_time;
1573-
else if (!set_backup_params->note)
1574-
return false;
1575-
1576-
if (set_backup_params->note) target_backup->note = set_backup_params->note;
1578+
else
1579+
/* nothing to do */
1580+
return;
15771581

15781582
/* Update backup.control */
15791583
write_backup(target_backup);
@@ -1586,12 +1590,47 @@ pin_backup(pgBackup *target_backup, pgSetBackupParams *set_backup_params)
15861590
elog(INFO, "Backup %s is pinned until '%s'", base36enc(target_backup->start_time),
15871591
expire_timestamp);
15881592
}
1589-
else if (set_backup_params->ttl == 0)
1593+
else
15901594
elog(INFO, "Backup %s is unpinned", base36enc(target_backup->start_time));
15911595

1592-
if (set_backup_params->note)
1593-
elog(INFO, "Saved note for backup %s", base36enc(target_backup->start_time));
1594-
return true;
1596+
return;
1597+
}
1598+
1599+
/*
1600+
* Add note to backup metadata or unset already existing note.
1601+
* It is a job of the caller to make sure that note is not NULL.
1602+
*/
1603+
void
1604+
add_note(pgBackup *target_backup, char *note)
1605+
{
1606+
1607+
char *note_string;
1608+
1609+
/* unset note */
1610+
if (pg_strcasecmp(note, "none") == 0)
1611+
{
1612+
target_backup->note = NULL;
1613+
elog(INFO, "Removing note from backup %s",
1614+
base36enc(target_backup->start_time));
1615+
}
1616+
else
1617+
{
1618+
/* Currently we do not allow string with newlines as note,
1619+
* because it will break parsing of backup.control.
1620+
* So if user provides string like this "aaa\nbbbbb",
1621+
* we save only "aaa"
1622+
* Example: tests.set_backup.SetBackupTest.test_add_note_newlines
1623+
*/
1624+
note_string = pgut_malloc(MAX_NOTE_SIZE);
1625+
sscanf(note, "%[^\n]", note_string);
1626+
1627+
target_backup->note = note_string;
1628+
elog(INFO, "Adding note to backup %s: '%s'",
1629+
base36enc(target_backup->start_time), target_backup->note);
1630+
}
1631+
1632+
/* Update backup.control */
1633+
write_backup(target_backup);
15951634
}
15961635

15971636
/*
@@ -1688,7 +1727,7 @@ pgBackupWriteControl(FILE *out, pgBackup *backup)
16881727
fio_fprintf(out, "external-dirs = '%s'\n", backup->external_dir_str);
16891728

16901729
if (backup->note)
1691-
fio_fprintf(out, "note = %s\n", backup->note);
1730+
fio_fprintf(out, "note = '%s'\n", backup->note);
16921731

16931732
}
16941733

src/pg_probackup.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ char *replication_slot = NULL;
7575
bool temp_slot = false;
7676

7777
/* backup options */
78-
bool backup_logs = false;
79-
bool smooth_checkpoint;
80-
char *remote_agent;
81-
static char *backup_note = NULL;
78+
bool backup_logs = false;
79+
bool smooth_checkpoint;
80+
char *remote_agent;
81+
static char *backup_note = NULL;
8282
/* restore options */
8383
static char *target_time = NULL;
8484
static char *target_xid = NULL;
@@ -183,7 +183,7 @@ static ConfigOption cmd_options[] =
183183
{ 'b', 183, "delete-expired", &delete_expired, SOURCE_CMD_STRICT },
184184
{ 'b', 184, "merge-expired", &merge_expired, SOURCE_CMD_STRICT },
185185
{ 'b', 185, "dry-run", &dry_run, SOURCE_CMD_STRICT },
186-
{ 's', 238, "note", &backup_note, SOURCE_CMD_STRICT },
186+
{ 's', 238, "note", &backup_note, SOURCE_CMD_STRICT },
187187
/* restore options */
188188
{ 's', 136, "recovery-target-time", &target_time, SOURCE_CMD_STRICT },
189189
{ 's', 137, "recovery-target-xid", &target_xid, SOURCE_CMD_STRICT },
@@ -753,6 +753,9 @@ main(int argc, char *argv[])
753753
set_backup_params->ttl = ttl;
754754
set_backup_params->expire_time = expire_time;
755755
set_backup_params->note = backup_note;
756+
757+
if (backup_note && strlen(backup_note) > MAX_NOTE_SIZE)
758+
elog(ERROR, "Backup note cannot exceed %u bytes", MAX_NOTE_SIZE);
756759
}
757760
}
758761

src/pg_probackup.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ extern const char *PROGRAM_EMAIL;
9090
/* retry attempts */
9191
#define PAGE_READ_ATTEMPTS 100
9292

93+
#define MAX_NOTE_SIZE 1024
94+
9395
/* Check if an XLogRecPtr value is pointed to 0 offset */
9496
#define XRecOffIsNull(xlrp) \
9597
((xlrp) % XLOG_BLCKSZ == 0)
@@ -391,7 +393,6 @@ struct pgBackup
391393
parray *files; /* list of files belonging to this backup
392394
* must be populated explicitly */
393395
char *note;
394-
395396
};
396397

397398
/* Recovery target for restore and validate subcommands */
@@ -435,14 +436,14 @@ typedef struct pgRestoreParams
435436
/* Options needed for set-backup command */
436437
typedef struct pgSetBackupParams
437438
{
438-
int64 ttl; /* amount of time backup must be pinned
439+
int64 ttl; /* amount of time backup must be pinned
439440
* -1 - do nothing
440441
* 0 - disable pinning
441442
*/
442-
time_t expire_time; /* Point in time before which backup
443+
time_t expire_time; /* Point in time until backup
443444
* must be pinned.
444445
*/
445-
char *note;
446+
char *note;
446447
} pgSetBackupParams;
447448

448449
typedef struct
@@ -781,8 +782,9 @@ extern void timelineInfoFree(void *tliInfo);
781782
extern parray *catalog_get_timelines(InstanceConfig *instance);
782783
extern void do_set_backup(const char *instance_name, time_t backup_id,
783784
pgSetBackupParams *set_backup_params);
784-
extern bool pin_backup(pgBackup *target_backup,
785+
extern void pin_backup(pgBackup *target_backup,
785786
pgSetBackupParams *set_backup_params);
787+
extern void add_note(pgBackup *target_backup, char *note);
786788
extern void pgBackupWriteControl(FILE *out, pgBackup *backup);
787789
extern void write_backup_filelist(pgBackup *backup, parray *files,
788790
const char *root, parray *external_list);

0 commit comments

Comments
 (0)