Skip to content

Commit 66a1c64

Browse files
committed
update backup_content.control during backup;
implement buffered write of backup_content.control
1 parent e66d96a commit 66a1c64

File tree

3 files changed

+98
-51
lines changed

3 files changed

+98
-51
lines changed

src/backup.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ do_backup_instance(PGconn *backup_conn)
462462
arg->prev_start_lsn = prev_backup_start_lsn;
463463
arg->conn_arg.conn = NULL;
464464
arg->conn_arg.cancel_conn = NULL;
465+
arg->thread_num = i+1;
465466
/* By default there are some error */
466467
arg->ret = 1;
467468
}
@@ -1987,6 +1988,9 @@ backup_files(void *arg)
19871988
int i;
19881989
backup_files_arg *arguments = (backup_files_arg *) arg;
19891990
int n_backup_files_list = parray_num(arguments->files_list);
1991+
static time_t prev_time;
1992+
1993+
prev_time = current.start_time;
19901994

19911995
/* backup a file */
19921996
for (i = 0; i < n_backup_files_list; i++)
@@ -1995,6 +1999,21 @@ backup_files(void *arg)
19951999
struct stat buf;
19962000
pgFile *file = (pgFile *) parray_get(arguments->files_list, i);
19972001

2002+
2003+
if (arguments->thread_num == 1)
2004+
{
2005+
/* update every 10 seconds */
2006+
if ((difftime(time(NULL), prev_time)) > 10)
2007+
{
2008+
prev_time = time(NULL);
2009+
elog(INFO, "write_backup_filelist N=%ld, starttime %ld, time %ld",
2010+
parray_num(backup_files_list), current.start_time, prev_time);
2011+
2012+
write_backup_filelist(&current, backup_files_list, instance_config.pgdata,
2013+
NULL, arguments->external_dirs);
2014+
}
2015+
}
2016+
19982017
if (!pg_atomic_test_set_flag(&file->lock))
19992018
continue;
20002019
elog(VERBOSE, "Copying file: \"%s\" ", file->path);

src/catalog.c

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -632,22 +632,96 @@ void
632632
write_backup_filelist(pgBackup *backup, parray *files, const char *root,
633633
const char *external_prefix, 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;
639643

640644
pgBackupGetPath(backup, path, lengthof(path), DATABASE_FILE_LIST);
641645
snprintf(path_temp, sizeof(path_temp), "%s.tmp", path);
642646

643-
fp = fio_fopen(path_temp, PG_BINARY_W, FIO_BACKUP_HOST);
644-
if (fp == NULL)
647+
out = fio_fopen(path_temp, PG_BINARY_W, FIO_BACKUP_HOST);
648+
if (out == NULL)
645649
elog(ERROR, "Cannot open file list \"%s\": %s", path_temp,
646650
strerror(errno));
647651

648-
print_file_list(fp, files, root, external_prefix, external_list);
652+
/* print each file in the list */
653+
while(i < parray_num(files))
654+
{
655+
pgFile *file = (pgFile *) parray_get(files, i);
656+
char *path = file->path;
657+
char line[BLCKSZ];
658+
int len = 0;
659+
660+
/* omit root directory portion */
661+
if (root && strstr(path, root) == path)
662+
path = GetRelativePath(path, root);
663+
else if (file->external_dir_num && !external_prefix)
664+
{
665+
Assert(external_list);
666+
path = GetRelativePath(path, parray_get(external_list,
667+
file->external_dir_num - 1));
668+
}
649669

650-
if (fio_fflush(fp) || fio_fclose(fp))
670+
len = sprintf(line, "{\"path\":\"%s\", \"size\":\"" INT64_FORMAT "\", "
671+
"\"mode\":\"%u\", \"is_datafile\":\"%u\", "
672+
"\"is_cfs\":\"%u\", \"crc\":\"%u\", "
673+
"\"compress_alg\":\"%s\", \"external_dir_num\":\"%d\"",
674+
path, file->write_size, file->mode,
675+
file->is_datafile ? 1 : 0,
676+
file->is_cfs ? 1 : 0,
677+
file->crc,
678+
deparse_compress_alg(file->compress_alg),
679+
file->external_dir_num);
680+
681+
if (file->is_datafile)
682+
len += sprintf(line+len, ",\"segno\":\"%d\"", file->segno);
683+
684+
if (file->linked)
685+
len += sprintf(line+len, ",\"linked\":\"%s\"", file->linked);
686+
687+
if (file->n_blocks != BLOCKNUM_INVALID)
688+
len += sprintf(line+len, ",\"n_blocks\":\"%i\"", file->n_blocks);
689+
690+
len += sprintf(line+len, "}\n");
691+
692+
if (write_len + len <= BUFFERSZ)
693+
{
694+
memcpy(buf+write_len, line, len);
695+
write_len += len;
696+
}
697+
else
698+
{
699+
/* write buffer to file */
700+
if (fio_fwrite(out, buf, write_len) != write_len)
701+
{
702+
errno_temp = errno;
703+
fio_unlink(path_temp, FIO_BACKUP_HOST);
704+
elog(ERROR, "Cannot write file list \"%s\": %s",
705+
path_temp, strerror(errno));
706+
}
707+
/* reset write_len */
708+
write_len = 0;
709+
}
710+
711+
i++;
712+
}
713+
714+
/* write what is left in the buffer to file */
715+
if (write_len > 0)
716+
if (fio_fwrite(out, buf, write_len) != write_len)
717+
{
718+
errno_temp = errno;
719+
fio_unlink(path_temp, FIO_BACKUP_HOST);
720+
elog(ERROR, "Cannot write file list \"%s\": %s",
721+
path_temp, strerror(errno));
722+
}
723+
724+
if (fio_fflush(out) || fio_fclose(out))
651725
{
652726
errno_temp = errno;
653727
fio_unlink(path_temp, FIO_BACKUP_HOST);

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

0 commit comments

Comments
 (0)