Skip to content

Commit 48959dc

Browse files
committed
Merge branch 'backup_content_control_updates'
2 parents 6e0c213 + d28ea72 commit 48959dc

File tree

5 files changed

+120
-75
lines changed

5 files changed

+120
-75
lines changed

src/backup.c

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ do_backup_instance(PGconn *backup_conn)
182182
check_external_for_tablespaces(external_dirs, backup_conn);
183183
}
184184

185-
/* Initialize size summary */
186-
current.data_bytes = 0;
187-
188185
/* Obtain current timeline */
189186
current.tli = get_current_timeline(false);
190187

@@ -445,6 +442,11 @@ do_backup_instance(PGconn *backup_conn)
445442
if (prev_backup_filelist)
446443
parray_qsort(prev_backup_filelist, pgFileComparePathWithExternal);
447444

445+
/* write initial backup_content.control file and update backup.control */
446+
write_backup_filelist(&current, backup_files_list,
447+
instance_config.pgdata, external_dirs);
448+
write_backup(&current);
449+
448450
/* init thread args with own file lists */
449451
threads = (pthread_t *) palloc(sizeof(pthread_t) * num_threads);
450452
threads_args = (backup_files_arg *) palloc(sizeof(backup_files_arg)*num_threads);
@@ -462,6 +464,7 @@ do_backup_instance(PGconn *backup_conn)
462464
arg->prev_start_lsn = prev_backup_start_lsn;
463465
arg->conn_arg.conn = NULL;
464466
arg->conn_arg.cancel_conn = NULL;
467+
arg->thread_num = i+1;
465468
/* By default there are some error */
466469
arg->ret = 1;
467470
}
@@ -574,25 +577,14 @@ do_backup_instance(PGconn *backup_conn)
574577

575578
/* Print the list of files to backup catalog */
576579
write_backup_filelist(&current, backup_files_list, instance_config.pgdata,
577-
NULL, external_dirs);
580+
external_dirs);
581+
/* update backup control file to update size info */
582+
write_backup(&current);
578583

579584
/* clean external directories list */
580585
if (external_dirs)
581586
free_dir_list(external_dirs);
582587

583-
/* Compute summary of size of regular files in the backup */
584-
for (i = 0; i < parray_num(backup_files_list); i++)
585-
{
586-
pgFile *file = (pgFile *) parray_get(backup_files_list, i);
587-
588-
if (S_ISDIR(file->mode))
589-
current.data_bytes += 4096;
590-
591-
/* Count the amount of the data actually copied */
592-
if (S_ISREG(file->mode))
593-
current.data_bytes += file->write_size;
594-
}
595-
596588
/* Cleanup */
597589
if (backup_list)
598590
{
@@ -1804,7 +1796,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn)
18041796
*/
18051797
if (backup_files_list)
18061798
{
1807-
file = pgFileNew(backup_label, backup_label, true, 0,
1799+
file = pgFileNew(backup_label, PG_BACKUP_LABEL_FILE, true, 0,
18081800
FIO_BACKUP_HOST);
18091801
file->crc = pgFileGetCRC(file->path, true, false,
18101802
&file->read_size, FIO_BACKUP_HOST);
@@ -1850,7 +1842,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn)
18501842

18511843
if (backup_files_list)
18521844
{
1853-
file = pgFileNew(tablespace_map, tablespace_map, true, 0,
1845+
file = pgFileNew(tablespace_map, PG_TABLESPACE_MAP_FILE, true, 0,
18541846
FIO_BACKUP_HOST);
18551847
if (S_ISREG(file->mode))
18561848
{
@@ -1987,6 +1979,9 @@ backup_files(void *arg)
19871979
int i;
19881980
backup_files_arg *arguments = (backup_files_arg *) arg;
19891981
int n_backup_files_list = parray_num(arguments->files_list);
1982+
static time_t prev_time;
1983+
1984+
prev_time = current.start_time;
19901985

19911986
/* backup a file */
19921987
for (i = 0; i < n_backup_files_list; i++)
@@ -1995,6 +1990,20 @@ backup_files(void *arg)
19951990
struct stat buf;
19961991
pgFile *file = (pgFile *) parray_get(arguments->files_list, i);
19971992

1993+
if (arguments->thread_num == 1)
1994+
{
1995+
/* update backup_content.control every 10 seconds */
1996+
if ((difftime(time(NULL), prev_time)) > 10)
1997+
{
1998+
prev_time = time(NULL);
1999+
2000+
write_backup_filelist(&current, arguments->files_list, arguments->from_root,
2001+
arguments->external_dirs);
2002+
/* update backup control file to update size info */
2003+
write_backup(&current);
2004+
}
2005+
}
2006+
19982007
if (!pg_atomic_test_set_flag(&file->lock))
19992008
continue;
20002009
elog(VERBOSE, "Copying file: \"%s\" ", file->path);

src/catalog.c

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -630,24 +630,104 @@ write_backup(pgBackup *backup)
630630
*/
631631
void
632632
write_backup_filelist(pgBackup *backup, parray *files, const char *root,
633-
const char *external_prefix, parray *external_list)
633+
parray *external_list)
634634
{
635-
FILE *fp;
635+
FILE *out;
636636
char path[MAXPGPATH];
637637
char path_temp[MAXPGPATH];
638638
int errno_temp;
639+
size_t i = 0;
640+
#define BUFFERSZ BLCKSZ*500
641+
char buf[BUFFERSZ];
642+
size_t write_len = 0;
643+
int64 backup_size_on_disk = 0;
639644

640645
pgBackupGetPath(backup, path, lengthof(path), DATABASE_FILE_LIST);
641646
snprintf(path_temp, sizeof(path_temp), "%s.tmp", path);
642647

643-
fp = fio_fopen(path_temp, PG_BINARY_W, FIO_BACKUP_HOST);
644-
if (fp == NULL)
648+
out = fio_fopen(path_temp, PG_BINARY_W, FIO_BACKUP_HOST);
649+
if (out == NULL)
645650
elog(ERROR, "Cannot open file list \"%s\": %s", path_temp,
646651
strerror(errno));
647652

648-
print_file_list(fp, files, root, external_prefix, external_list);
653+
/* print each file in the list */
654+
while(i < parray_num(files))
655+
{
656+
pgFile *file = (pgFile *) parray_get(files, i);
657+
char *path = file->path; /* for streamed WAL files */
658+
char line[BLCKSZ];
659+
int len = 0;
649660

650-
if (fio_fflush(fp) || fio_fclose(fp))
661+
i++;
662+
663+
if (S_ISDIR(file->mode))
664+
backup_size_on_disk += 4096;
665+
666+
/* Count the amount of the data actually copied */
667+
if (S_ISREG(file->mode) && file->write_size > 0)
668+
backup_size_on_disk += file->write_size;
669+
670+
/* for files from PGDATA and external files use rel_path
671+
* streamed WAL files has rel_path relative not to "database/"
672+
* but to "database/pg_wal", so for them use path.
673+
*/
674+
if ((root && strstr(path, root) == path) ||
675+
(file->external_dir_num && external_list))
676+
path = file->rel_path;
677+
678+
len = sprintf(line, "{\"path\":\"%s\", \"size\":\"" INT64_FORMAT "\", "
679+
"\"mode\":\"%u\", \"is_datafile\":\"%u\", "
680+
"\"is_cfs\":\"%u\", \"crc\":\"%u\", "
681+
"\"compress_alg\":\"%s\", \"external_dir_num\":\"%d\"",
682+
path, file->write_size, file->mode,
683+
file->is_datafile ? 1 : 0,
684+
file->is_cfs ? 1 : 0,
685+
file->crc,
686+
deparse_compress_alg(file->compress_alg),
687+
file->external_dir_num);
688+
689+
if (file->is_datafile)
690+
len += sprintf(line+len, ",\"segno\":\"%d\"", file->segno);
691+
692+
if (file->linked)
693+
len += sprintf(line+len, ",\"linked\":\"%s\"", file->linked);
694+
695+
if (file->n_blocks != BLOCKNUM_INVALID)
696+
len += sprintf(line+len, ",\"n_blocks\":\"%i\"", file->n_blocks);
697+
698+
len += sprintf(line+len, "}\n");
699+
700+
if (write_len + len <= BUFFERSZ)
701+
{
702+
memcpy(buf+write_len, line, len);
703+
write_len += len;
704+
}
705+
else
706+
{
707+
/* write buffer to file */
708+
if (fio_fwrite(out, buf, write_len) != write_len)
709+
{
710+
errno_temp = errno;
711+
fio_unlink(path_temp, FIO_BACKUP_HOST);
712+
elog(ERROR, "Cannot write file list \"%s\": %s",
713+
path_temp, strerror(errno));
714+
}
715+
/* reset write_len */
716+
write_len = 0;
717+
}
718+
}
719+
720+
/* write what is left in the buffer to file */
721+
if (write_len > 0)
722+
if (fio_fwrite(out, buf, write_len) != write_len)
723+
{
724+
errno_temp = errno;
725+
fio_unlink(path_temp, FIO_BACKUP_HOST);
726+
elog(ERROR, "Cannot write file list \"%s\": %s",
727+
path_temp, strerror(errno));
728+
}
729+
730+
if (fio_fflush(out) || fio_fclose(out))
651731
{
652732
errno_temp = errno;
653733
fio_unlink(path_temp, FIO_BACKUP_HOST);
@@ -662,6 +742,9 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
662742
elog(ERROR, "Cannot rename configuration file \"%s\" to \"%s\": %s",
663743
path_temp, path, strerror(errno_temp));
664744
}
745+
746+
/* use extra variable to avoid reset of previous data_bytes value in case of error */
747+
backup->data_bytes = backup_size_on_disk;
665748
}
666749

667750
/*

src/dir.c

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,52 +1271,6 @@ get_external_remap(char *current_dir)
12711271
return current_dir;
12721272
}
12731273

1274-
/*
1275-
* Print backup content list.
1276-
*/
1277-
void
1278-
print_file_list(FILE *out, const parray *files, const char *root,
1279-
const char *external_prefix, parray *external_list)
1280-
{
1281-
size_t i;
1282-
1283-
/* print each file in the list */
1284-
for (i = 0; i < parray_num(files); i++)
1285-
{
1286-
pgFile *file = (pgFile *) parray_get(files, i);
1287-
char *path = file->path;
1288-
1289-
/* omit root directory portion */
1290-
if (root && strstr(path, root) == path)
1291-
path = GetRelativePath(path, root);
1292-
else if (file->external_dir_num && !external_prefix)
1293-
{
1294-
Assert(external_list);
1295-
path = GetRelativePath(path, parray_get(external_list,
1296-
file->external_dir_num - 1));
1297-
}
1298-
1299-
fio_fprintf(out, "{\"path\":\"%s\", \"size\":\"" INT64_FORMAT "\", "
1300-
"\"mode\":\"%u\", \"is_datafile\":\"%u\", "
1301-
"\"is_cfs\":\"%u\", \"crc\":\"%u\", "
1302-
"\"compress_alg\":\"%s\", \"external_dir_num\":\"%d\"",
1303-
path, file->write_size, file->mode,
1304-
file->is_datafile ? 1 : 0, file->is_cfs ? 1 : 0, file->crc,
1305-
deparse_compress_alg(file->compress_alg), file->external_dir_num);
1306-
1307-
if (file->is_datafile)
1308-
fio_fprintf(out, ",\"segno\":\"%d\"", file->segno);
1309-
1310-
if (file->linked)
1311-
fio_fprintf(out, ",\"linked\":\"%s\"", file->linked);
1312-
1313-
if (file->n_blocks != BLOCKNUM_INVALID)
1314-
fio_fprintf(out, ",\"n_blocks\":\"%i\"", file->n_blocks);
1315-
1316-
fio_fprintf(out, "}\n");
1317-
}
1318-
}
1319-
13201274
/* Parsing states for get_control_value() */
13211275
#define CONTROL_WAIT_NAME 1
13221276
#define CONTROL_INNAME 2

src/merge.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ merge_backups(pgBackup *to_backup, pgBackup *from_backup)
368368
else
369369
to_backup->wal_bytes = BYTES_INVALID;
370370

371-
write_backup_filelist(to_backup, files, from_database_path,
372-
from_external_prefix, NULL);
371+
write_backup_filelist(to_backup, files, from_database_path, NULL);
373372
write_backup(to_backup);
374373

375374
delete_source_backup:

src/pg_probackup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ extern int do_validate_all(void);
544544
extern pgBackup *read_backup(time_t timestamp);
545545
extern void write_backup(pgBackup *backup);
546546
extern void write_backup_status(pgBackup *backup, BackupStatus status);
547+
extern void write_backup_data_bytes(pgBackup *backup);
547548
extern bool lock_backup(pgBackup *backup);
548549

549550
extern const char *pgBackupGetBackupMode(pgBackup *backup);
@@ -555,8 +556,7 @@ extern pgBackup *catalog_get_last_data_backup(parray *backup_list,
555556
TimeLineID tli);
556557
extern void pgBackupWriteControl(FILE *out, pgBackup *backup);
557558
extern void write_backup_filelist(pgBackup *backup, parray *files,
558-
const char *root, const char *external_prefix,
559-
parray *external_list);
559+
const char *root, parray *external_list);
560560

561561
extern void pgBackupGetPath(const pgBackup *backup, char *path, size_t len,
562562
const char *subdir);

0 commit comments

Comments
 (0)