Skip to content

Commit ee36dd9

Browse files
committed
[Issue #169] Calculate appoximate efficiency ratio when restoring backup
1 parent c0541a6 commit ee36dd9

File tree

3 files changed

+54
-31
lines changed

3 files changed

+54
-31
lines changed

src/data.c

+15-16
Original file line numberDiff line numberDiff line change
@@ -966,10 +966,11 @@ restore_data_file(const char *to_path, pgFile *file, bool allow_truncate,
966966
* filelist of every chain member starting with FULL backup.
967967
* Apply changed blocks to destination file from every backup in parent chain.
968968
*/
969-
void
969+
size_t
970970
restore_data_file_new(parray *parent_chain, pgFile *dest_file, FILE *out, const char *to_fullpath)
971971
{
972972
int i;
973+
size_t total_write_len = 0;
973974

974975
for (i = parray_num(parent_chain) - 1; i >= 0; i--)
975976
{
@@ -1013,29 +1014,27 @@ restore_data_file_new(parray *parent_chain, pgFile *dest_file, FILE *out, const
10131014

10141015
in = fopen(from_fullpath, PG_BINARY_R);
10151016
if (in == NULL)
1016-
{
10171017
elog(INFO, "Cannot open backup file \"%s\": %s", from_fullpath,
10181018
strerror(errno));
1019-
Assert(0);
1020-
}
10211019

10221020
/*
1023-
* restore the file.
1021+
* Restore the file.
10241022
* Datafiles are backed up block by block and every block
10251023
* have BackupPageHeader with meta information, so we cannot just
10261024
* copy the file from backup.
10271025
*/
1028-
restore_data_file_internal(in, out, tmp_file,
1026+
total_write_len += restore_data_file_internal(in, out, tmp_file,
10291027
parse_program_version(backup->program_version),
10301028
from_fullpath, to_fullpath, dest_file->n_blocks);
10311029

1032-
if (fio_fclose(in) != 0)
1030+
if (fclose(in) != 0)
10331031
elog(ERROR, "Cannot close file \"%s\": %s", from_fullpath,
10341032
strerror(errno));
10351033
}
1034+
return total_write_len;
10361035
}
10371036

1038-
void
1037+
size_t
10391038
restore_data_file_internal(FILE *in, FILE *out, pgFile *file, uint32 backup_version,
10401039
const char *from_fullpath, const char *to_fullpath, int nblocks)
10411040
{
@@ -1067,7 +1066,7 @@ restore_data_file_internal(FILE *in, FILE *out, pgFile *file, uint32 backup_vers
10671066
blknum, from_fullpath, strerror(errno_tmp));
10681067
}
10691068

1070-
/* Consider empty block */
1069+
/* Consider empty blockm. wtf empty block ? */
10711070
if (header.block == 0 && header.compressed_size == 0)
10721071
{
10731072
elog(VERBOSE, "Skip empty block of \"%s\"", from_fullpath);
@@ -1187,6 +1186,7 @@ restore_data_file_internal(FILE *in, FILE *out, pgFile *file, uint32 backup_vers
11871186
}
11881187

11891188
elog(VERBOSE, "Copied file \"%s\": %lu bytes", from_fullpath, write_len);
1189+
return write_len;
11901190
}
11911191

11921192
/*
@@ -1247,7 +1247,7 @@ restore_non_data_file_internal(FILE *in, FILE *out, pgFile *file,
12471247
elog(VERBOSE, "Copied file \"%s\": %lu bytes", from_fullpath, file->write_size);
12481248
}
12491249

1250-
void
1250+
size_t
12511251
restore_non_data_file(parray *parent_chain, pgBackup *dest_backup,
12521252
pgFile *dest_file, FILE *out, const char *to_fullpath)
12531253
{
@@ -1297,7 +1297,7 @@ restore_non_data_file(parray *parent_chain, pgBackup *dest_backup,
12971297

12981298
/* Full copy is found and it is null sized, nothing to do here */
12991299
if (tmp_file->write_size == 0)
1300-
return;
1300+
return 0;
13011301

13021302
/* Full copy is found */
13031303
if (tmp_file->write_size > 0)
@@ -1314,11 +1314,9 @@ restore_non_data_file(parray *parent_chain, pgBackup *dest_backup,
13141314
elog(ERROR, "Failed to locate a full copy of non-data file \"%s\"", to_fullpath);
13151315

13161316
if (tmp_file->external_dir_num == 0)
1317-
// pgBackupGetPath(tmp_backup, from_root, lengthof(from_root), DATABASE_DIR);
13181317
join_path_components(from_root, tmp_backup->root_dir, DATABASE_DIR);
13191318
else
13201319
{
1321-
// get external prefix for tmp_backup
13221320
char external_prefix[MAXPGPATH];
13231321

13241322
join_path_components(external_prefix, tmp_backup->root_dir, EXTERNAL_DIR);
@@ -1329,16 +1327,17 @@ restore_non_data_file(parray *parent_chain, pgBackup *dest_backup,
13291327

13301328
in = fopen(from_fullpath, PG_BINARY_R);
13311329
if (in == NULL)
1332-
{
13331330
elog(ERROR, "Cannot open backup file \"%s\": %s", from_fullpath,
13341331
strerror(errno));
1335-
}
13361332

1333+
/* do actual work */
13371334
restore_non_data_file_internal(in, out, tmp_file, from_fullpath, to_fullpath);
13381335

1339-
if (fio_fclose(in) != 0)
1336+
if (fclose(in) != 0)
13401337
elog(ERROR, "Cannot close file \"%s\": %s", from_fullpath,
13411338
strerror(errno));
1339+
1340+
return tmp_file->write_size;
13421341
}
13431342

13441343
/*

src/pg_probackup.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -840,11 +840,11 @@ extern void restore_data_file(const char *to_path,
840840
pgFile *file, bool allow_truncate,
841841
bool write_header,
842842
uint32 backup_version);
843-
extern void restore_data_file_new(parray *parent_chain, pgFile *dest_file,
843+
extern size_t restore_data_file_new(parray *parent_chain, pgFile *dest_file,
844844
FILE *out, const char *to_fullpath);
845-
extern void restore_data_file_internal(FILE *in, FILE *out, pgFile *file, uint32 backup_version,
845+
extern size_t restore_data_file_internal(FILE *in, FILE *out, pgFile *file, uint32 backup_version,
846846
const char *from_fullpath, const char *to_fullpath, int nblocks);
847-
extern void restore_non_data_file(parray *parent_chain, pgBackup *dest_backup,
847+
extern size_t restore_non_data_file(parray *parent_chain, pgBackup *dest_backup,
848848
pgFile *dest_file, FILE *out, const char *to_fullpath);
849849
extern void restore_non_data_file_internal(FILE *in, FILE *out, pgFile *file,
850850
const char *from_fullpath, const char *to_fullpath);

src/restore.c

+36-12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ typedef struct
4444
parray *dbOid_exclude_list;
4545
bool skip_external_dirs;
4646
const char *to_root;
47+
size_t restored_bytes;
4748

4849
/*
4950
* Return value from the thread.
@@ -524,6 +525,11 @@ restore_chain(pgBackup *dest_backup, parray *parent_chain,
524525
restore_files_arg_new *threads_args;
525526
bool restore_isok = true;
526527

528+
/* fancy reporting */
529+
char pretty_dest_bytes[20];
530+
char pretty_total_bytes[20];
531+
size_t dest_bytes = 0;
532+
size_t total_bytes = 0;
527533
char pretty_time[20];
528534
time_t start_time, end_time;
529535

@@ -608,6 +614,9 @@ restore_chain(pgBackup *dest_backup, parray *parent_chain,
608614
{
609615
pgFile *file = (pgFile *) parray_get(dest_files, i);
610616

617+
if (S_ISDIR(file->mode))
618+
total_bytes += 4096;
619+
611620
if (!params->skip_external_dirs &&
612621
file->external_dir_num && S_ISDIR(file->mode))
613622
{
@@ -639,9 +648,15 @@ restore_chain(pgBackup *dest_backup, parray *parent_chain,
639648
num_threads);
640649

641650
/* Restore files into target directory */
642-
thread_interrupted = false;
643-
elog(INFO, "Start restoring backup files");
651+
if (dest_backup->stream)
652+
dest_bytes = dest_backup->pgdata_bytes + dest_backup->wal_bytes;
653+
else
654+
dest_bytes = dest_backup->pgdata_bytes;
655+
656+
pretty_size(dest_bytes, pretty_dest_bytes, lengthof(pretty_dest_bytes));
657+
elog(INFO, "Start restoring backup files. PGDATA size: %s", pretty_dest_bytes);
644658
time(&start_time);
659+
thread_interrupted = false;
645660
for (i = 0; i < num_threads; i++)
646661
{
647662
restore_files_arg_new *arg = &(threads_args[i]);
@@ -653,6 +668,7 @@ restore_chain(pgBackup *dest_backup, parray *parent_chain,
653668
arg->dbOid_exclude_list = dbOid_exclude_list;
654669
arg->skip_external_dirs = params->skip_external_dirs;
655670
arg->to_root = pgdata_path;
671+
threads_args[i].restored_bytes = 0;
656672
/* By default there are some error */
657673
threads_args[i].ret = 1;
658674

@@ -668,15 +684,27 @@ restore_chain(pgBackup *dest_backup, parray *parent_chain,
668684
pthread_join(threads[i], NULL);
669685
if (threads_args[i].ret == 1)
670686
restore_isok = false;
687+
688+
total_bytes += threads_args[i].restored_bytes;
671689
}
672690

673691
time(&end_time);
674692
pretty_time_interval(difftime(end_time, start_time),
675693
pretty_time, lengthof(pretty_time));
694+
pretty_size(total_bytes, pretty_total_bytes, lengthof(pretty_total_bytes));
695+
676696
if (restore_isok)
677-
elog(INFO, "Backup files are restored, time elapsed: %s", pretty_time);
697+
{
698+
elog(INFO, "Backup files are restored. Transfered bytes: %s, time elapsed: %s",
699+
pretty_total_bytes, pretty_time);
700+
701+
elog(INFO, "Approximate restore efficiency ratio: %.f%% (%s/%s)",
702+
((float) dest_bytes / total_bytes) * 100,
703+
pretty_dest_bytes, pretty_total_bytes);
704+
}
678705
else
679-
elog(ERROR, "Backup files restoring failed, time elapsed: %s", pretty_time);
706+
elog(ERROR, "Backup files restoring failed. Transfered bytes: %s, time elapsed: %s",
707+
pretty_total_bytes, pretty_time);
680708

681709
if (no_sync)
682710
elog(WARNING, "Restored files are not synced to disk");
@@ -841,11 +869,12 @@ restore_files_new(void *arg)
841869
/* Restore destination file */
842870
if (dest_file->is_datafile && !dest_file->is_cfs)
843871
/* Destination file is data file */
844-
restore_data_file_new(arguments->parent_chain, dest_file, out, to_fullpath);
872+
arguments->restored_bytes += restore_data_file_new(arguments->parent_chain,
873+
dest_file, out, to_fullpath);
845874
else
846875
/* Destination file is non-data file */
847-
restore_non_data_file(arguments->parent_chain, arguments->dest_backup,
848-
dest_file, out, to_fullpath);
876+
arguments->restored_bytes += restore_non_data_file(arguments->parent_chain,
877+
arguments->dest_backup, dest_file, out, to_fullpath);
849878

850879
/*
851880
* Destination file is data file.
@@ -875,11 +904,6 @@ restore_files_new(void *arg)
875904
if (fio_fclose(out) != 0)
876905
elog(ERROR, "Cannot close file \"%s\": %s", to_fullpath,
877906
strerror(errno));
878-
879-
/* print size of restored file */
880-
// if (file->write_size != BYTES_INVALID)
881-
// elog(VERBOSE, "Restored file %s : " INT64_FORMAT " bytes",
882-
// file->path, file->write_size);
883907
}
884908

885909
/* Data files restoring is successful */

0 commit comments

Comments
 (0)