-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmerge.c
1452 lines (1237 loc) · 43.6 KB
/
merge.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* merge.c: merge FULL and incremental backups
*
* Copyright (c) 2018-2022, Postgres Professional
*
*-------------------------------------------------------------------------
*/
#include "pg_probackup.h"
#include <sys/stat.h>
#include <unistd.h>
#include "utils/thread.h"
typedef struct
{
parray *merge_filelist;
parray *parent_chain;
pgBackup *dest_backup;
pgBackup *full_backup;
const char *full_database_dir;
const char *full_external_prefix;
// size_t in_place_merge_bytes;
bool compression_match;
bool program_version_match;
bool use_bitmap;
bool is_retry;
bool no_sync;
/*
* Return value from the thread.
* 0 means there is no error, 1 - there is an error.
*/
int ret;
} merge_files_arg;
static void *merge_files(void *arg);
static void
reorder_external_dirs(pgBackup *to_backup, parray *to_external,
parray *from_external);
static int
get_external_index(const char *key, const parray *list);
static void
merge_data_file(parray *parent_chain, pgBackup *full_backup,
pgBackup *dest_backup, pgFile *dest_file,
pgFile *tmp_file, const char *to_root, bool use_bitmap,
bool is_retry, bool no_sync);
static void
merge_non_data_file(parray *parent_chain, pgBackup *full_backup,
pgBackup *dest_backup, pgFile *dest_file,
pgFile *tmp_file, const char *full_database_dir,
const char *full_external_prefix, bool no_sync);
static bool is_forward_compatible(parray *parent_chain);
/*
* Implementation of MERGE command.
*
* - Find target and its parent full backup
* - Merge data files of target, parent and and intermediate backups
* - Remove unnecessary files, which doesn't exist in the target backup anymore
*/
void
do_merge(InstanceState *instanceState, time_t backup_id, bool no_validate, bool no_sync)
{
parray *backups;
parray *merge_list = parray_new();
pgBackup *dest_backup = NULL;
pgBackup *dest_backup_tmp = NULL;
pgBackup *full_backup = NULL;
int i;
if (backup_id == INVALID_BACKUP_ID)
elog(ERROR, "required parameter is not specified: --backup-id");
if (instanceState == NULL)
elog(ERROR, "required parameter is not specified: --instance");
elog(INFO, "Merge started");
/* Get list of all backups sorted in order of descending start time */
backups = catalog_get_backup_list(instanceState, INVALID_BACKUP_ID);
/* Find destination backup first */
for (i = 0; i < parray_num(backups); i++)
{
pgBackup *backup = (pgBackup *) parray_get(backups, i);
/* found target */
if (backup->start_time == backup_id)
{
/* sanity */
if (backup->status != BACKUP_STATUS_OK &&
backup->status != BACKUP_STATUS_DONE &&
/* It is possible that previous merging was interrupted */
backup->status != BACKUP_STATUS_MERGING &&
backup->status != BACKUP_STATUS_MERGED &&
backup->status != BACKUP_STATUS_DELETING)
elog(ERROR, "Backup %s has status: %s",
base36enc(backup->start_time), status2str(backup->status));
dest_backup = backup;
break;
}
}
/*
* Handle the case of crash right after deletion of the target
* incremental backup. We still can recover from this.
* Iterate over backups and look for the FULL backup with
* MERGED status, that has merge-target-id eqial to backup_id.
*/
if (dest_backup == NULL)
{
for (i = 0; i < parray_num(backups); i++)
{
pgBackup *backup = (pgBackup *) parray_get(backups, i);
if (backup->status == BACKUP_STATUS_MERGED &&
backup->merge_dest_backup == backup_id)
{
dest_backup = backup;
break;
}
}
}
if (dest_backup == NULL)
elog(ERROR, "Target backup %s was not found", base36enc(backup_id));
/* It is possible to use FULL backup as target backup for merge.
* There are two possible cases:
* 1. The user want to merge FULL backup with closest incremental backup.
* In this case we must find suitable destination backup and merge them.
*
* 2. Previous merge has failed after destination backup was deleted,
* but before FULL backup was renamed:
* Example A:
* PAGE2_1 OK
* FULL2 OK
* PAGE1_1 MISSING/DELETING <-
* FULL1 MERGED/MERGING
*/
if (dest_backup->backup_mode == BACKUP_MODE_FULL)
{
full_backup = dest_backup;
dest_backup = NULL;
elog(INFO, "Merge target backup %s is full backup",
base36enc(full_backup->start_time));
/* sanity */
if (full_backup->status == BACKUP_STATUS_DELETING)
elog(ERROR, "Backup %s has status: %s",
base36enc(full_backup->start_time),
status2str(full_backup->status));
/* Case #1 */
if (full_backup->status == BACKUP_STATUS_OK ||
full_backup->status == BACKUP_STATUS_DONE)
{
/* Check the case of FULL backup having more than one direct children */
if (is_prolific(backups, full_backup))
elog(ERROR, "Merge target is full backup and has multiple direct children, "
"you must specify child backup id you want to merge with");
elog(INFO, "Looking for closest incremental backup to merge with");
/* Look for closest child backup */
for (i = 0; i < parray_num(backups); i++)
{
pgBackup *backup = (pgBackup *) parray_get(backups, i);
/* skip unsuitable candidates */
if (backup->status != BACKUP_STATUS_OK &&
backup->status != BACKUP_STATUS_DONE)
continue;
if (backup->parent_backup == full_backup->start_time)
{
dest_backup = backup;
break;
}
}
/* sanity */
if (dest_backup == NULL)
elog(ERROR, "Failed to find merge candidate, "
"backup %s has no valid children",
base36enc(full_backup->start_time));
}
/* Case #2 */
else if (full_backup->status == BACKUP_STATUS_MERGING)
{
/*
* MERGING - merge was ongoing at the moment of crash.
* We must find destination backup and rerun merge.
* If destination backup is missing, then merge must be aborted,
* there is no recovery from this situation.
*/
if (full_backup->merge_dest_backup == INVALID_BACKUP_ID)
elog(ERROR, "Failed to determine merge destination backup");
/* look up destination backup */
for (i = 0; i < parray_num(backups); i++)
{
pgBackup *backup = (pgBackup *) parray_get(backups, i);
if (backup->start_time == full_backup->merge_dest_backup)
{
dest_backup = backup;
break;
}
}
if (!dest_backup)
{
char *tmp_backup_id = base36enc_dup(full_backup->start_time);
elog(ERROR, "Full backup %s has unfinished merge with missing backup %s",
tmp_backup_id,
base36enc(full_backup->merge_dest_backup));
pg_free(tmp_backup_id);
}
}
else if (full_backup->status == BACKUP_STATUS_MERGED)
{
/*
* MERGED - merge crashed after files were transfered, but
* before rename could take place.
* If destination backup is missing, this is ok.
* If destination backup is present, then it should be deleted.
* After that FULL backup must acquire destination backup ID.
*/
/* destination backup may or may not exists */
for (i = 0; i < parray_num(backups); i++)
{
pgBackup *backup = (pgBackup *) parray_get(backups, i);
if (backup->start_time == full_backup->merge_dest_backup)
{
dest_backup = backup;
break;
}
}
if (!dest_backup)
{
char *tmp_backup_id = base36enc_dup(full_backup->start_time);
elog(WARNING, "Full backup %s has unfinished merge with missing backup %s",
tmp_backup_id,
base36enc(full_backup->merge_dest_backup));
pg_free(tmp_backup_id);
}
}
else
elog(ERROR, "Backup %s has status: %s",
base36enc(full_backup->start_time),
status2str(full_backup->status));
}
else
{
/*
* Legal Case #1:
* PAGE2 OK <- target
* PAGE1 OK
* FULL OK
* Legal Case #2:
* PAGE2 MERGING <- target
* PAGE1 MERGING
* FULL MERGING
* Legal Case #3:
* PAGE2 MERGING <- target
* PAGE1 DELETING
* FULL MERGED
* Legal Case #4:
* PAGE2 MERGING <- target
* PAGE1 missing
* FULL MERGED
* Legal Case #5:
* PAGE2 DELETING <- target
* FULL MERGED
* Legal Case #6:
* PAGE2 MERGING <- target
* PAGE1 missing
* FULL MERGED
* Illegal Case #7:
* PAGE2 MERGING <- target
* PAGE1 missing
* FULL MERGING
*/
if (dest_backup->status == BACKUP_STATUS_MERGING ||
dest_backup->status == BACKUP_STATUS_DELETING)
elog(WARNING, "Rerun unfinished merge for backup %s",
base36enc(dest_backup->start_time));
/* First we should try to find parent FULL backup */
full_backup = find_parent_full_backup(dest_backup);
/* Chain is broken, one or more member of parent chain is missing */
if (full_backup == NULL)
{
/* It is the legal state of affairs in Case #4, but
* only for MERGING incremental target backup and only
* if FULL backup has MERGED status.
*/
if (dest_backup->status != BACKUP_STATUS_MERGING)
elog(ERROR, "Failed to find parent full backup for %s",
base36enc(dest_backup->start_time));
/* Find FULL backup that has unfinished merge with dest backup */
for (i = 0; i < parray_num(backups); i++)
{
pgBackup *backup = (pgBackup *) parray_get(backups, i);
if (backup->merge_dest_backup == dest_backup->start_time)
{
full_backup = backup;
break;
}
}
if (!full_backup)
elog(ERROR, "Failed to find full backup that has unfinished merge"
"with backup %s, cannot rerun merge",
base36enc(dest_backup->start_time));
if (full_backup->status == BACKUP_STATUS_MERGED)
elog(WARNING, "Incremental chain is broken, try to recover unfinished merge");
else
elog(ERROR, "Incremental chain is broken, merge is impossible to finish");
}
else
{
if ((full_backup->status == BACKUP_STATUS_MERGED ||
full_backup->status == BACKUP_STATUS_MERGED) &&
dest_backup->start_time != full_backup->merge_dest_backup)
{
char *tmp_backup_id = base36enc_dup(full_backup->start_time);
elog(ERROR, "Full backup %s has unfinished merge with backup %s",
tmp_backup_id, base36enc(full_backup->merge_dest_backup));
pg_free(tmp_backup_id);
}
}
}
/* sanity */
if (full_backup == NULL)
elog(ERROR, "Parent full backup for the given backup %s was not found",
base36enc(backup_id));
/* At this point NULL as dest_backup is allowed only in case of full backup
* having status MERGED */
if (dest_backup == NULL && full_backup->status != BACKUP_STATUS_MERGED)
elog(ERROR, "Cannot run merge for full backup %s",
base36enc(full_backup->start_time));
/* sanity */
if (full_backup->status != BACKUP_STATUS_OK &&
full_backup->status != BACKUP_STATUS_DONE &&
/* It is possible that previous merging was interrupted */
full_backup->status != BACKUP_STATUS_MERGED &&
full_backup->status != BACKUP_STATUS_MERGING)
elog(ERROR, "Backup %s has status: %s",
base36enc(full_backup->start_time), status2str(full_backup->status));
/* Form merge list */
dest_backup_tmp = dest_backup;
/* While loop below may looks strange, it is done so on purpose
* to handle both whole and broken incremental chains.
*/
while (dest_backup_tmp)
{
/* sanity */
if (dest_backup_tmp->status != BACKUP_STATUS_OK &&
dest_backup_tmp->status != BACKUP_STATUS_DONE &&
/* It is possible that previous merging was interrupted */
dest_backup_tmp->status != BACKUP_STATUS_MERGING &&
dest_backup_tmp->status != BACKUP_STATUS_MERGED &&
dest_backup_tmp->status != BACKUP_STATUS_DELETING)
elog(ERROR, "Backup %s has status: %s",
base36enc(dest_backup_tmp->start_time),
status2str(dest_backup_tmp->status));
if (dest_backup_tmp->backup_mode == BACKUP_MODE_FULL)
break;
parray_append(merge_list, dest_backup_tmp);
dest_backup_tmp = dest_backup_tmp->parent_backup_link;
}
/* Add FULL backup */
parray_append(merge_list, full_backup);
/* Lock merge chain */
catalog_lock_backup_list(merge_list, parray_num(merge_list) - 1, 0, true, true);
/* do actual merge */
merge_chain(instanceState, merge_list, full_backup, dest_backup, no_validate, no_sync);
if (!no_validate)
pgBackupValidate(full_backup, NULL);
if (full_backup->status == BACKUP_STATUS_CORRUPT)
elog(ERROR, "Merging of backup %s failed", base36enc(backup_id));
/* cleanup */
parray_walk(backups, pgBackupFree);
parray_free(backups);
parray_free(merge_list);
elog(INFO, "Merge of backup %s completed", base36enc(backup_id));
}
/*
* Merge backup chain.
* dest_backup - incremental backup.
* parent_chain - array of backups starting with dest_backup and
* ending with full_backup.
*
* Copy backup files from incremental backups from parent_chain into
* full backup directory.
* Remove unnecessary directories and files from full backup directory.
* Update metadata of full backup to represent destination backup.
*
* TODO: stop relying on caller to provide valid parent_chain, make sure
* that chain is ok.
*/
void
merge_chain(InstanceState *instanceState,
parray *parent_chain, pgBackup *full_backup, pgBackup *dest_backup,
bool no_validate, bool no_sync)
{
int i;
char *dest_backup_id;
char full_external_prefix[MAXPGPATH];
char full_database_dir[MAXPGPATH];
parray *full_externals = NULL,
*dest_externals = NULL;
parray *result_filelist = NULL;
bool use_bitmap = true;
bool is_retry = false;
// size_t total_in_place_merge_bytes = 0;
pthread_t *threads = NULL;
merge_files_arg *threads_args = NULL;
time_t merge_time;
bool merge_isok = true;
/* for fancy reporting */
time_t end_time;
char pretty_time[20];
/* in-place merge flags */
bool compression_match = false;
bool program_version_match = false;
/* It's redundant to check block checksumms during merge */
skip_block_validation = true;
/* Handle corner cases of missing destination backup */
if (dest_backup == NULL &&
full_backup->status == BACKUP_STATUS_MERGED)
goto merge_rename;
if (!dest_backup)
elog(ERROR, "Destination backup is missing, cannot continue merge");
if (dest_backup->status == BACKUP_STATUS_MERGING ||
full_backup->status == BACKUP_STATUS_MERGING ||
full_backup->status == BACKUP_STATUS_MERGED)
{
is_retry = true;
elog(INFO, "Retry failed merge of backup %s with parent chain", base36enc(dest_backup->start_time));
}
else
elog(INFO, "Merging backup %s with parent chain", base36enc(dest_backup->start_time));
/* sanity */
if (full_backup->merge_dest_backup != INVALID_BACKUP_ID &&
full_backup->merge_dest_backup != dest_backup->start_time)
{
char *merge_dest_backup_current = base36enc_dup(dest_backup->start_time);
char *merge_dest_backup = base36enc_dup(full_backup->merge_dest_backup);
elog(ERROR, "Cannot run merge for %s, because full backup %s has "
"unfinished merge with backup %s",
merge_dest_backup_current,
base36enc(full_backup->start_time),
merge_dest_backup);
pg_free(merge_dest_backup_current);
pg_free(merge_dest_backup);
}
/*
* Previous merging was interrupted during deleting source backup. It is
* safe just to delete it again.
*/
if (full_backup->status == BACKUP_STATUS_MERGED)
goto merge_delete;
/* Forward compatibility is not supported */
for (i = parray_num(parent_chain) - 1; i >= 0; i--)
{
pgBackup *backup = (pgBackup *) parray_get(parent_chain, i);
if (parse_program_version(backup->program_version) >
parse_program_version(PROGRAM_VERSION))
{
elog(ERROR, "Backup %s has been produced by pg_probackup version %s, "
"but current program version is %s. Forward compatibility "
"is not supported.",
base36enc(backup->start_time),
backup->program_version,
PROGRAM_VERSION);
}
}
/* If destination backup compression algorithm differs from
* full backup compression algorithm, then in-place merge is
* not possible.
*/
if (full_backup->compress_alg == dest_backup->compress_alg)
compression_match = true;
else
elog(WARNING, "In-place merge is disabled because of compression "
"algorithms mismatch");
/*
* If current program version differs from destination backup version,
* then in-place merge is not possible.
*/
program_version_match = is_forward_compatible(parent_chain);
/* Forbid merge retry for failed merges between 2.4.0 and any
* older version. Several format changes makes it impossible
* to determine the exact format any speific file is got.
*/
if (is_retry &&
parse_program_version(dest_backup->program_version) >= 20400 &&
parse_program_version(full_backup->program_version) < 20400)
{
elog(ERROR, "Retry of failed merge for backups with different between minor "
"versions is forbidden to avoid data corruption because of storage format "
"changes introduced in 2.4.0 version, please take a new full backup");
}
/*
* Validate or revalidate all members of parent chain
* with sole exception of FULL backup. If it has MERGING status
* then it isn't valid backup until merging is finished.
*/
if (!no_validate)
{
elog(INFO, "Validate parent chain for backup %s",
base36enc(dest_backup->start_time));
for (i = parray_num(parent_chain) - 1; i >= 0; i--)
{
pgBackup *backup = (pgBackup *) parray_get(parent_chain, i);
/* FULL backup is not to be validated if its status is MERGING */
if (backup->backup_mode == BACKUP_MODE_FULL &&
backup->status == BACKUP_STATUS_MERGING)
{
continue;
}
pgBackupValidate(backup, NULL);
if (backup->status != BACKUP_STATUS_OK)
elog(ERROR, "Backup %s has status %s, merge is aborted",
base36enc(backup->start_time), status2str(backup->status));
}
}
/*
* Get backup files.
*/
for (i = parray_num(parent_chain) - 1; i >= 0; i--)
{
pgBackup *backup = (pgBackup *) parray_get(parent_chain, i);
backup->files = get_backup_filelist(backup, true);
parray_qsort(backup->files, pgFileCompareRelPathWithExternal);
/* Set MERGING status for every member of the chain */
if (backup->backup_mode == BACKUP_MODE_FULL)
{
/* In case of FULL backup also remember backup_id of
* of destination backup we are merging with, so
* we can safely allow rerun merge in case of failure.
*/
backup->merge_dest_backup = dest_backup->start_time;
backup->status = BACKUP_STATUS_MERGING;
write_backup(backup, true);
}
else
write_backup_status(backup, BACKUP_STATUS_MERGING, true);
}
/* Construct path to database dir: /backup_dir/instance_name/FULL/database */
join_path_components(full_database_dir, full_backup->root_dir, DATABASE_DIR);
/* Construct path to external dir: /backup_dir/instance_name/FULL/external */
join_path_components(full_external_prefix, full_backup->root_dir, EXTERNAL_DIR);
/* Create directories */
create_data_directories(dest_backup->files, full_database_dir,
dest_backup->root_dir, false, false, FIO_BACKUP_HOST, NULL);
/* External directories stuff */
if (dest_backup->external_dir_str)
dest_externals = make_external_directory_list(dest_backup->external_dir_str, false);
if (full_backup->external_dir_str)
full_externals = make_external_directory_list(full_backup->external_dir_str, false);
/*
* Rename external directories in FULL backup (if exists)
* according to numeration of external dirs in destionation backup.
*/
if (full_externals && dest_externals)
reorder_external_dirs(full_backup, full_externals, dest_externals);
/* bitmap optimization rely on n_blocks, which is generally available since 2.3.0 */
if (parse_program_version(dest_backup->program_version) < 20300)
use_bitmap = false;
/* Setup threads */
for (i = 0; i < parray_num(dest_backup->files); i++)
{
pgFile *file = (pgFile *) parray_get(dest_backup->files, i);
/* if the entry was an external directory, create it in the backup */
if (file->external_dir_num && S_ISDIR(file->mode))
{
char dirpath[MAXPGPATH];
char new_container[MAXPGPATH];
makeExternalDirPathByNum(new_container, full_external_prefix,
file->external_dir_num);
join_path_components(dirpath, new_container, file->rel_path);
dir_create_dir(dirpath, DIR_PERMISSION, false);
}
pg_atomic_init_flag(&file->lock);
}
threads = (pthread_t *) palloc(sizeof(pthread_t) * num_threads);
threads_args = (merge_files_arg *) palloc(sizeof(merge_files_arg) * num_threads);
thread_interrupted = false;
merge_time = time(NULL);
elog(INFO, "Start merging backup files");
for (i = 0; i < num_threads; i++)
{
merge_files_arg *arg = &(threads_args[i]);
arg->merge_filelist = parray_new();
arg->parent_chain = parent_chain;
arg->dest_backup = dest_backup;
arg->full_backup = full_backup;
arg->full_database_dir = full_database_dir;
arg->full_external_prefix = full_external_prefix;
arg->compression_match = compression_match;
arg->program_version_match = program_version_match;
arg->use_bitmap = use_bitmap;
arg->is_retry = is_retry;
arg->no_sync = no_sync;
/* By default there are some error */
arg->ret = 1;
elog(VERBOSE, "Start thread: %d", i);
pthread_create(&threads[i], NULL, merge_files, arg);
}
/* Wait threads */
result_filelist = parray_new();
for (i = 0; i < num_threads; i++)
{
pthread_join(threads[i], NULL);
if (threads_args[i].ret == 1)
merge_isok = false;
/* Compile final filelist */
parray_concat(result_filelist, threads_args[i].merge_filelist);
/* cleanup */
parray_free(threads_args[i].merge_filelist);
//total_in_place_merge_bytes += threads_args[i].in_place_merge_bytes;
}
time(&end_time);
pretty_time_interval(difftime(end_time, merge_time),
pretty_time, lengthof(pretty_time));
if (merge_isok)
elog(INFO, "Backup files are successfully merged, time elapsed: %s",
pretty_time);
else
elog(ERROR, "Backup files merging failed, time elapsed: %s",
pretty_time);
/* If temp header map is open, then close it and make rename */
if (full_backup->hdr_map.fp)
{
cleanup_header_map(&(full_backup->hdr_map));
/* sync new header map to disk */
if (fio_sync(full_backup->hdr_map.path_tmp, FIO_BACKUP_HOST) != 0)
elog(ERROR, "Cannot sync temp header map \"%s\": %s",
full_backup->hdr_map.path_tmp, strerror(errno));
/* Replace old header map with new one */
if (rename(full_backup->hdr_map.path_tmp, full_backup->hdr_map.path))
elog(ERROR, "Could not rename file \"%s\" to \"%s\": %s",
full_backup->hdr_map.path_tmp, full_backup->hdr_map.path, strerror(errno));
}
/* Close page header maps */
for (i = parray_num(parent_chain) - 1; i >= 0; i--)
{
pgBackup *backup = (pgBackup *) parray_get(parent_chain, i);
cleanup_header_map(&(backup->hdr_map));
}
/*
* Update FULL backup metadata.
* We cannot set backup status to OK just yet,
* because it still has old start_time.
*/
strlcpy(full_backup->program_version, PROGRAM_VERSION,
sizeof(full_backup->program_version));
full_backup->parent_backup = INVALID_BACKUP_ID;
full_backup->start_lsn = dest_backup->start_lsn;
full_backup->stop_lsn = dest_backup->stop_lsn;
full_backup->recovery_time = dest_backup->recovery_time;
full_backup->recovery_xid = dest_backup->recovery_xid;
full_backup->tli = dest_backup->tli;
full_backup->from_replica = dest_backup->from_replica;
pfree(full_backup->external_dir_str);
full_backup->external_dir_str = pgut_strdup(dest_backup->external_dir_str);
pfree(full_backup->primary_conninfo);
full_backup->primary_conninfo = pgut_strdup(dest_backup->primary_conninfo);
full_backup->merge_time = merge_time;
full_backup->end_time = time(NULL);
full_backup->compress_alg = dest_backup->compress_alg;
full_backup->compress_level = dest_backup->compress_level;
/* If incremental backup is pinned,
* then result FULL backup must also be pinned.
* And reverse, if FULL backup was pinned and dest was not,
* then pinning is no more.
*/
full_backup->expire_time = dest_backup->expire_time;
pg_free(full_backup->note);
full_backup->note = NULL;
if (dest_backup->note)
full_backup->note = pgut_strdup(dest_backup->note);
/* FULL backup must inherit wal mode. */
full_backup->stream = dest_backup->stream;
/* ARCHIVE backup must inherit wal_bytes too.
* STREAM backup will have its wal_bytes calculated by
* write_backup_filelist().
*/
if (!dest_backup->stream)
full_backup->wal_bytes = dest_backup->wal_bytes;
parray_qsort(result_filelist, pgFileCompareRelPathWithExternal);
write_backup_filelist(full_backup, result_filelist, full_database_dir, NULL, true);
write_backup(full_backup, true);
/* Delete FULL backup files, that do not exists in destination backup
* Both arrays must be sorted in in reversed order to delete from leaf
*/
parray_qsort(dest_backup->files, pgFileCompareRelPathWithExternalDesc);
parray_qsort(full_backup->files, pgFileCompareRelPathWithExternalDesc);
for (i = 0; i < parray_num(full_backup->files); i++)
{
pgFile *full_file = (pgFile *) parray_get(full_backup->files, i);
if (full_file->external_dir_num && full_externals)
{
char *dir_name = parray_get(full_externals, full_file->external_dir_num - 1);
if (backup_contains_external(dir_name, full_externals))
/* Dir already removed*/
continue;
}
if (parray_bsearch(dest_backup->files, full_file, pgFileCompareRelPathWithExternalDesc) == NULL)
{
char full_file_path[MAXPGPATH];
/* We need full path, file object has relative path */
join_path_components(full_file_path, full_database_dir, full_file->rel_path);
pgFileDelete(full_file->mode, full_file_path);
elog(LOG, "Deleted \"%s\"", full_file_path);
}
}
/* Critical section starts.
* Change status of FULL backup.
* Files are merged into FULL backup. It is time to remove incremental chain.
*/
full_backup->status = BACKUP_STATUS_MERGED;
write_backup(full_backup, true);
merge_delete:
for (i = parray_num(parent_chain) - 2; i >= 0; i--)
{
pgBackup *backup = (pgBackup *) parray_get(parent_chain, i);
delete_backup_files(backup);
}
/*
* PAGE2 DELETED
* PAGE1 DELETED
* FULL MERGED
* If we crash now, automatic rerun of failed merge is still possible:
* The user should start merge with full backup ID as an argument to option '-i'.
*/
merge_rename:
/*
* Rename FULL backup directory to destination backup directory.
*/
if (dest_backup)
{
elog(LOG, "Rename %s to %s", full_backup->root_dir, dest_backup->root_dir);
if (rename(full_backup->root_dir, dest_backup->root_dir) == -1)
elog(ERROR, "Could not rename directory \"%s\" to \"%s\": %s",
full_backup->root_dir, dest_backup->root_dir, strerror(errno));
/* update root_dir after rename */
pg_free(full_backup->root_dir);
full_backup->root_dir = pgut_strdup(dest_backup->root_dir);
}
else
{
/* Ugly */
char destination_path[MAXPGPATH];
join_path_components(destination_path, instanceState->instance_backup_subdir_path,
base36enc(full_backup->merge_dest_backup));
elog(LOG, "Rename %s to %s", full_backup->root_dir, destination_path);
if (rename(full_backup->root_dir, destination_path) == -1)
elog(ERROR, "Could not rename directory \"%s\" to \"%s\": %s",
full_backup->root_dir, destination_path, strerror(errno));
/* update root_dir after rename */
pg_free(full_backup->root_dir);
full_backup->root_dir = pgut_strdup(destination_path);
}
/* Reinit path to database_dir */
join_path_components(full_backup->database_dir, full_backup->root_dir, DATABASE_DIR);
/* If we crash here, it will produce full backup in MERGED
* status, located in directory with wrong backup id.
* It should not be a problem.
*/
/*
* Merging finished, now we can safely update ID of the FULL backup
*/
dest_backup_id = base36enc_dup(full_backup->merge_dest_backup);
elog(INFO, "Rename merged full backup %s to %s",
base36enc(full_backup->start_time), dest_backup_id);
full_backup->status = BACKUP_STATUS_OK;
full_backup->start_time = full_backup->merge_dest_backup;
full_backup->merge_dest_backup = INVALID_BACKUP_ID;
write_backup(full_backup, true);
/* Critical section end */
/* Cleanup */
pg_free(dest_backup_id);
if (threads)
{
pfree(threads_args);
pfree(threads);
}
if (result_filelist && parray_num(result_filelist) > 0)
{
parray_walk(result_filelist, pgFileFree);
parray_free(result_filelist);
}
if (dest_externals != NULL)
free_dir_list(dest_externals);
if (full_externals != NULL)
free_dir_list(full_externals);
for (i = parray_num(parent_chain) - 1; i >= 0; i--)
{
pgBackup *backup = (pgBackup *) parray_get(parent_chain, i);
if (backup->files)
{
parray_walk(backup->files, pgFileFree);
parray_free(backup->files);
}
}
}
/*
* Thread worker of merge_chain().
*/
static void *
merge_files(void *arg)
{
int i;
merge_files_arg *arguments = (merge_files_arg *) arg;
size_t n_files = parray_num(arguments->dest_backup->files);
for (i = 0; i < n_files; i++)
{
pgFile *dest_file = (pgFile *) parray_get(arguments->dest_backup->files, i);
pgFile *tmp_file;
bool in_place = false; /* keep file as it is */
/* check for interrupt */
if (interrupted || thread_interrupted)
elog(ERROR, "Interrupted during merge");
if (!pg_atomic_test_set_flag(&dest_file->lock))
continue;
tmp_file = pgFileInit(dest_file->rel_path);
tmp_file->mode = dest_file->mode;
tmp_file->is_datafile = dest_file->is_datafile;
tmp_file->is_cfs = dest_file->is_cfs;
tmp_file->external_dir_num = dest_file->external_dir_num;
tmp_file->dbOid = dest_file->dbOid;
/* Directories were created before */
if (S_ISDIR(dest_file->mode))
goto done;
elog(progress ? INFO : LOG, "Progress: (%d/%lu). Merging file \"%s\"",
i + 1, n_files, dest_file->rel_path);
if (dest_file->is_datafile && !dest_file->is_cfs)
tmp_file->segno = dest_file->segno;
// If destination file is 0 sized, then go for the next
if (dest_file->write_size == 0)
{
if (!dest_file->is_datafile || dest_file->is_cfs)
tmp_file->crc = dest_file->crc;
tmp_file->write_size = 0;
goto done;
}
/*
* If file didn`t changed over the course of all incremental chain,
* then do in-place merge, unless destination backup has
* different compression algorithm.
* In-place merge is also impossible, if program version of destination
* backup differs from PROGRAM_VERSION
*/
if (arguments->program_version_match && arguments->compression_match &&
!arguments->is_retry)
{
/*
* Case 1:
* in this case in place merge is possible:
* 0 PAGE; file, size BYTES_INVALID
* 1 PAGE; file, size BYTES_INVALID
* 2 FULL; file, size 100500
*
* Case 2:
* in this case in place merge is possible:
* 0 PAGE; file, size 0
* 1 PAGE; file, size 0
* 2 FULL; file, size 100500
*
* Case 3:
* in this case in place merge is impossible:
* 0 PAGE; file, size BYTES_INVALID