Skip to content

Commit 6fe8440

Browse files
committed
Improvements for plain show, honest wal_bytes for STREAM backups and merged backups
1 parent cb64313 commit 6fe8440

File tree

5 files changed

+94
-41
lines changed

5 files changed

+94
-41
lines changed

src/backup.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ do_backup_instance(PGconn *backup_conn, PGNodeInfo *nodeInfo)
553553
dir_list_file(xlog_files_list, pg_xlog_path, false, true, false, 0,
554554
FIO_BACKUP_HOST);
555555

556+
/* TODO: Drop streamed WAL segments greater than stop_lsn */
556557
for (i = 0; i < parray_num(xlog_files_list); i++)
557558
{
558559
pgFile *file = (pgFile *) parray_get(xlog_files_list, i);
@@ -702,7 +703,7 @@ do_backup(time_t start_time, bool no_validate)
702703
}
703704

704705
elog(INFO, "Backup start, pg_probackup version: %s, instance: %s, backup ID: %s, backup mode: %s, "
705-
"wal-method: %s, remote: %s, compress-algorithm: %s, compress-level: %i",
706+
"wal mode: %s, remote: %s, compress-algorithm: %s, compress-level: %i",
706707
PROGRAM_VERSION, instance_name, base36enc(start_time), pgBackupGetBackupMode(&current),
707708
current.stream ? "STREAM" : "ARCHIVE", IsSshProtocol() ? "true" : "false",
708709
deparse_compress_alg(current.compress_alg), current.compress_level);

src/catalog.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,7 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
11211121
char buf[BUFFERSZ];
11221122
size_t write_len = 0;
11231123
int64 backup_size_on_disk = 0;
1124+
int64 wal_size_on_disk = 0;
11241125

11251126
pgBackupGetPath(backup, path, lengthof(path), DATABASE_FILE_LIST);
11261127
snprintf(path_temp, sizeof(path_temp), "%s.tmp", path);
@@ -1145,7 +1146,13 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
11451146

11461147
/* Count the amount of the data actually copied */
11471148
if (S_ISREG(file->mode) && file->write_size > 0)
1148-
backup_size_on_disk += file->write_size;
1149+
{
1150+
/* TODO: in 3.0 add attribute is_walfile */
1151+
if (IsXLogFileName(file->name) && (file->external_dir_num == 0))
1152+
wal_size_on_disk += file->write_size;
1153+
else
1154+
backup_size_on_disk += file->write_size;
1155+
}
11491156

11501157
/* for files from PGDATA and external files use rel_path
11511158
* streamed WAL files has rel_path relative not to "database/"
@@ -1227,6 +1234,7 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
12271234

12281235
/* use extra variable to avoid reset of previous data_bytes value in case of error */
12291236
backup->data_bytes = backup_size_on_disk;
1237+
backup->wal_bytes = wal_size_on_disk;
12301238
}
12311239

12321240
/*

src/merge.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -345,32 +345,12 @@ merge_backups(pgBackup *to_backup, pgBackup *from_backup)
345345
to_backup->merge_time = merge_time;
346346
to_backup->end_time = time(NULL);
347347

348-
/*
349-
* Target backup must inherit wal mode too.
350-
*/
348+
/* Target backup must inherit wal mode too. */
351349
to_backup->stream = from_backup->stream;
352-
/* Compute summary of size of regular files in the backup */
353-
to_backup->data_bytes = 0;
354-
for (i = 0; i < parray_num(files); i++)
355-
{
356-
pgFile *file = (pgFile *) parray_get(files, i);
357350

358-
if (S_ISDIR(file->mode))
359-
{
360-
to_backup->data_bytes += 4096;
361-
continue;
362-
}
363-
/* Count the amount of the data actually copied */
364-
if (file->write_size > 0)
365-
to_backup->data_bytes += file->write_size;
366-
}
367-
/* compute size of wal files of this backup stored in the archive */
351+
/* ARCHIVE backup must inherit wal_bytes. */
368352
if (!to_backup->stream)
369-
to_backup->wal_bytes = instance_config.xlog_seg_size *
370-
(to_backup->stop_lsn / instance_config.xlog_seg_size -
371-
to_backup->start_lsn / instance_config.xlog_seg_size + 1);
372-
else
373-
to_backup->wal_bytes = BYTES_INVALID;
353+
to_backup->wal_bytes = from_backup->wal_bytes;
374354

375355
write_backup_filelist(to_backup, files, from_database_path, NULL);
376356
write_backup(to_backup);

src/pg_probackup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ struct pgBackup
314314
* BYTES_INVALID means nothing was backed up.
315315
*/
316316
int64 data_bytes;
317-
/* Size of WAL files in archive needed to restore this backup */
317+
/* Size of WAL files needed to restore this backup */
318318
int64 wal_bytes;
319319

320320
CompressAlg compress_alg;
@@ -812,7 +812,7 @@ int32 do_compress(void* dst, size_t dst_size, void const* src, size_t src_size,
812812
CompressAlg alg, int level, const char **errormsg);
813813

814814
extern void pretty_size(int64 size, char *buf, size_t len);
815-
815+
extern void pretty_time_interval(int64 num_seconds, char *buf, size_t len);
816816

817817
extern PGconn *pgdata_basic_setup(ConnectionOptions conn_opt, PGNodeInfo *nodeInfo);
818818
extern void check_system_identifiers(PGconn *conn, char *pgdata);

src/show.c

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef struct ShowBackendRow
3030
char tli[20];
3131
char duration[20];
3232
char data_bytes[20];
33+
char wal_bytes[20];
3334
char start_lsn[20];
3435
char stop_lsn[20];
3536
const char *status;
@@ -147,9 +148,15 @@ pretty_size(int64 size, char *buf, size_t len)
147148
int64 limit2 = limit * 2 - 1;
148149

149150
/* minus means the size is invalid */
150-
if (size < 0)
151+
// if (size < 0)
152+
// {
153+
// strncpy(buf, "----", len);
154+
// return;
155+
// }
156+
157+
if (size <= 0)
151158
{
152-
strncpy(buf, "----", len);
159+
strncpy(buf, "0", len);
153160
return;
154161
}
155162

@@ -180,6 +187,53 @@ pretty_size(int64 size, char *buf, size_t len)
180187
}
181188
}
182189

190+
void
191+
pretty_time_interval(int64 num_seconds, char *buf, size_t len)
192+
{
193+
int seconds = 0;
194+
int minutes = 0;
195+
int hours = 0;
196+
int days = 0;
197+
198+
if (num_seconds <= 0)
199+
{
200+
strncpy(buf, "0", len);
201+
return;
202+
}
203+
204+
days = num_seconds / (24 * 3600);
205+
num_seconds %= (24 * 3600);
206+
207+
hours = num_seconds / 3600;
208+
num_seconds %= 3600;
209+
210+
minutes = num_seconds / 60;
211+
num_seconds %= 60;
212+
213+
seconds = num_seconds;
214+
215+
if (days > 0)
216+
{
217+
snprintf(buf, len, "%dd:%dh", days, hours);
218+
return;
219+
}
220+
221+
if (hours > 0)
222+
{
223+
snprintf(buf, len, "%dh:%dm", hours, minutes);
224+
return;
225+
}
226+
227+
if (minutes > 0)
228+
{
229+
snprintf(buf, len, "%dm:%ds", minutes, seconds);
230+
return;
231+
}
232+
233+
snprintf(buf, len, "%ds", seconds);
234+
return;
235+
}
236+
183237
/*
184238
* Initialize instance visualization.
185239
*/
@@ -381,16 +435,16 @@ show_backup(const char *instance_name, time_t requested_backup_id)
381435
static void
382436
show_instance_plain(const char *instance_name, parray *backup_list, bool show_name)
383437
{
384-
#define SHOW_FIELDS_COUNT 12
438+
#define SHOW_FIELDS_COUNT 13
385439
int i;
386440
const char *names[SHOW_FIELDS_COUNT] =
387441
{ "Instance", "Version", "ID", "Recovery Time",
388-
"Mode", "WAL", "Current/Parent TLI", "Time", "Data",
442+
"Mode", "WAL Mode", "TLI", "Time", "Data", "WAL",
389443
"Start LSN", "Stop LSN", "Status" };
390444
const char *field_formats[SHOW_FIELDS_COUNT] =
391445
{ " %-*s ", " %-*s ", " %-*s ", " %-*s ",
392-
" %-*s ", " %-*s ", " %-*s ", " %*s ", " %*s ",
393-
" %*s ", " %*s ", " %-*s "};
446+
" %-*s ", " %-*s ", " %-*s ", " %*s ", " %-*s ", " %-*s ",
447+
" %-*s ", " %-*s ", " %-*s "};
394448
uint32 widths[SHOW_FIELDS_COUNT];
395449
uint32 widths_sum = 0;
396450
ShowBackendRow *rows;
@@ -443,7 +497,7 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
443497
widths[cur] = Max(widths[cur], strlen(row->mode));
444498
cur++;
445499

446-
/* WAL */
500+
/* WAL mode*/
447501
row->wal_mode = backup->stream ? "STREAM": "ARCHIVE";
448502
widths[cur] = Max(widths[cur], strlen(row->wal_mode));
449503
cur++;
@@ -453,22 +507,22 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
453507
if (backup->parent_backup_link != NULL)
454508
parent_tli = backup->parent_backup_link->tli;
455509

456-
snprintf(row->tli, lengthof(row->tli), "%u / %u",
510+
snprintf(row->tli, lengthof(row->tli), "%u/%u",
457511
backup->tli,
458512
backup->backup_mode == BACKUP_MODE_FULL ? 0 : parent_tli);
459513
widths[cur] = Max(widths[cur], strlen(row->tli));
460514
cur++;
461515

462516
/* Time */
463517
if (backup->status == BACKUP_STATUS_RUNNING)
464-
snprintf(row->duration, lengthof(row->duration), "%.*lfs", 0,
465-
difftime(current_time, backup->start_time));
518+
pretty_time_interval(difftime(current_time, backup->start_time),
519+
row->duration, lengthof(row->duration));
466520
else if (backup->merge_time != (time_t) 0)
467-
snprintf(row->duration, lengthof(row->duration), "%.*lfs", 0,
468-
difftime(backup->end_time, backup->merge_time));
521+
pretty_time_interval(difftime(backup->end_time, backup->merge_time),
522+
row->duration, lengthof(row->duration));
469523
else if (backup->end_time != (time_t) 0)
470-
snprintf(row->duration, lengthof(row->duration), "%.*lfs", 0,
471-
difftime(backup->end_time, backup->start_time));
524+
pretty_time_interval(difftime(backup->end_time, backup->start_time),
525+
row->duration, lengthof(row->duration));
472526
else
473527
StrNCpy(row->duration, "----", sizeof(row->duration));
474528
widths[cur] = Max(widths[cur], strlen(row->duration));
@@ -480,6 +534,12 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
480534
widths[cur] = Max(widths[cur], strlen(row->data_bytes));
481535
cur++;
482536

537+
/* WAL */
538+
pretty_size(backup->wal_bytes, row->wal_bytes,
539+
lengthof(row->wal_bytes));
540+
widths[cur] = Max(widths[cur], strlen(row->wal_bytes));
541+
cur++;
542+
483543
/* Start LSN */
484544
snprintf(row->start_lsn, lengthof(row->start_lsn), "%X/%X",
485545
(uint32) (backup->start_lsn >> 32),
@@ -566,6 +626,10 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
566626
row->data_bytes);
567627
cur++;
568628

629+
appendPQExpBuffer(&show_buf, field_formats[cur], widths[cur],
630+
row->wal_bytes);
631+
cur++;
632+
569633
appendPQExpBuffer(&show_buf, field_formats[cur], widths[cur],
570634
row->start_lsn);
571635
cur++;

0 commit comments

Comments
 (0)