-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathcatchup.c
1110 lines (940 loc) · 36.5 KB
/
catchup.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
/*-------------------------------------------------------------------------
*
* catchup.c: sync DB cluster
*
* Copyright (c) 2021, Postgres Professional
*
*-------------------------------------------------------------------------
*/
#include "pg_probackup.h"
#if PG_VERSION_NUM < 110000
#include "catalog/catalog.h"
#endif
#include "catalog/pg_tablespace.h"
#include "access/timeline.h"
#include "pgtar.h"
#include "streamutil.h"
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "utils/thread.h"
#include "utils/file.h"
/*
* Catchup routines
*/
static PGconn *catchup_init_state(PGNodeInfo *source_node_info, const char *source_pgdata, const char *dest_pgdata);
static void catchup_preflight_checks(PGNodeInfo *source_node_info, PGconn *source_conn, const char *source_pgdata,
const char *dest_pgdata);
static void catchup_check_tablespaces_existance_in_tbsmapping(PGconn *conn);
static parray* catchup_get_tli_history(ConnectionOptions *conn_opt, TimeLineID tli);
//REVIEW I'd also suggest to wrap all these fields into some CatchupState, but it isn't urgent.
//REVIEW_ANSWER what for?
/*
* Prepare for work: fill some globals, open connection to source database
*/
static PGconn *
catchup_init_state(PGNodeInfo *source_node_info, const char *source_pgdata, const char *dest_pgdata)
{
PGconn *source_conn;
/* Initialize PGInfonode */
pgNodeInit(source_node_info);
/* Get WAL segments size and system ID of source PG instance */
instance_config.xlog_seg_size = get_xlog_seg_size(source_pgdata);
instance_config.system_identifier = get_system_identifier(source_pgdata, FIO_DB_HOST, false);
current.start_time = time(NULL);
strlcpy(current.program_version, PROGRAM_VERSION, sizeof(current.program_version));
/* Do some compatibility checks and fill basic info about PG instance */
source_conn = pgdata_basic_setup(instance_config.conn_opt, source_node_info);
#if PG_VERSION_NUM >= 110000
if (!RetrieveWalSegSize(source_conn))
elog(ERROR, "Failed to retrieve wal_segment_size");
#endif
get_ptrack_version(source_conn, source_node_info);
if (source_node_info->ptrack_version_num > 0)
source_node_info->is_ptrack_enabled = pg_is_ptrack_enabled(source_conn, source_node_info->ptrack_version_num);
/* Obtain current timeline */
#if PG_VERSION_NUM >= 90600
current.tli = get_current_timeline(source_conn);
#else
instance_config.pgdata = source_pgdata;
current.tli = get_current_timeline_from_control(source_pgdata, FIO_DB_HOST, false);
#endif
elog(INFO, "Catchup start, pg_probackup version: %s, "
"PostgreSQL version: %s, "
"remote: %s, source-pgdata: %s, destination-pgdata: %s",
PROGRAM_VERSION, source_node_info->server_version_str,
IsSshProtocol() ? "true" : "false",
source_pgdata, dest_pgdata);
if (current.from_replica)
elog(INFO, "Running catchup from standby");
return source_conn;
}
/*
* Check that catchup can be performed on source and dest
* this function is for checks, that can be performed without modification of data on disk
*/
static void
catchup_preflight_checks(PGNodeInfo *source_node_info, PGconn *source_conn,
const char *source_pgdata, const char *dest_pgdata)
{
/* TODO
* gsmol - fallback to FULL mode if dest PGDATA is empty
* kulaginm -- I think this is a harmful feature. If user requested an incremental catchup, then
* he expects that this will be done quickly and efficiently. If, for example, he made a mistake
* with dest_dir, then he will receive a second full copy instead of an error message, and I think
* that in some cases he would prefer the error.
* I propose in future versions to offer a backup_mode auto, in which we will look to the dest_dir
* and decide which of the modes will be the most effective.
* I.e.:
* if(requested_backup_mode == BACKUP_MODE_DIFF_AUTO)
* {
* if(dest_pgdata_is_empty)
* backup_mode = BACKUP_MODE_FULL;
* else
* if(ptrack supported and applicable)
* backup_mode = BACKUP_MODE_DIFF_PTRACK;
* else
* backup_mode = BACKUP_MODE_DIFF_DELTA;
* }
*/
if (dir_is_empty(dest_pgdata, FIO_LOCAL_HOST))
{
if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK ||
current.backup_mode == BACKUP_MODE_DIFF_DELTA)
elog(ERROR, "\"%s\" is empty, but incremental catchup mode requested.",
dest_pgdata);
}
else /* dest dir not empty */
{
if (current.backup_mode == BACKUP_MODE_FULL)
elog(ERROR, "Can't perform full catchup into non-empty directory \"%s\".",
dest_pgdata);
}
/* check that postmaster is not running in destination */
if (current.backup_mode != BACKUP_MODE_FULL)
{
pid_t pid;
pid = fio_check_postmaster(dest_pgdata, FIO_LOCAL_HOST);
if (pid == 1) /* postmaster.pid is mangled */
{
char pid_filename[MAXPGPATH];
join_path_components(pid_filename, dest_pgdata, "postmaster.pid");
elog(ERROR, "Pid file \"%s\" is mangled, cannot determine whether postmaster is running or not",
pid_filename);
}
else if (pid > 1) /* postmaster is up */
{
elog(ERROR, "Postmaster with pid %u is running in destination directory \"%s\"",
pid, dest_pgdata);
}
}
/* check backup_label absence in dest */
if (current.backup_mode != BACKUP_MODE_FULL)
{
char backup_label_filename[MAXPGPATH];
join_path_components(backup_label_filename, dest_pgdata, PG_BACKUP_LABEL_FILE);
if (fio_access(backup_label_filename, F_OK, FIO_LOCAL_HOST) == 0)
elog(ERROR, "Destination directory contains \"" PG_BACKUP_LABEL_FILE "\" file");
}
/* Check that connected PG instance, source and destination PGDATA are the same */
{
uint64 source_conn_id, source_id, dest_id;
source_conn_id = get_remote_system_identifier(source_conn);
source_id = get_system_identifier(source_pgdata, FIO_DB_HOST, false); /* same as instance_config.system_identifier */
if (source_conn_id != source_id)
elog(ERROR, "Database identifiers mismatch: we connected to DB id %lu, but in \"%s\" we found id %lu",
source_conn_id, source_pgdata, source_id);
if (current.backup_mode != BACKUP_MODE_FULL)
{
dest_id = get_system_identifier(dest_pgdata, FIO_LOCAL_HOST, false);
if (source_conn_id != dest_id)
elog(ERROR, "Database identifiers mismatch: we connected to DB id %lu, but in \"%s\" we found id %lu",
source_conn_id, dest_pgdata, dest_id);
}
}
/* check PTRACK version */
if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
{
if (source_node_info->ptrack_version_num == 0)
elog(ERROR, "This PostgreSQL instance does not support ptrack");
else if (source_node_info->ptrack_version_num < 200)
elog(ERROR, "ptrack extension is too old.\n"
"Upgrade ptrack to version >= 2");
else if (!source_node_info->is_ptrack_enabled)
elog(ERROR, "Ptrack is disabled");
}
if (current.from_replica && exclusive_backup)
elog(ERROR, "Catchup from standby is only available for PostgreSQL >= 9.6");
/* check that we don't overwrite tablespace in source pgdata */
catchup_check_tablespaces_existance_in_tbsmapping(source_conn);
/* check timelines */
if (current.backup_mode != BACKUP_MODE_FULL)
{
RedoParams dest_redo = { 0, InvalidXLogRecPtr, 0 };
/* fill dest_redo.lsn and dest_redo.tli */
get_redo(dest_pgdata, FIO_LOCAL_HOST, &dest_redo);
elog(VERBOSE, "source.tli = %X, dest_redo.lsn = %X/%X, dest_redo.tli = %X",
current.tli, (uint32) (dest_redo.lsn >> 32), (uint32) dest_redo.lsn, dest_redo.tli);
if (current.tli != 1)
{
parray *source_timelines; /* parray* of TimeLineHistoryEntry* */
source_timelines = catchup_get_tli_history(&instance_config.conn_opt, current.tli);
if (source_timelines == NULL)
elog(ERROR, "Cannot get source timeline history");
if (!satisfy_timeline(source_timelines, dest_redo.tli, dest_redo.lsn))
elog(ERROR, "Destination is not in source timeline history");
parray_walk(source_timelines, pfree);
parray_free(source_timelines);
}
else /* special case -- no history files in source */
{
if (dest_redo.tli != 1)
elog(ERROR, "Source is behind destination in timeline history");
}
}
}
/*
* Check that all tablespaces exists in tablespace mapping (--tablespace-mapping option)
* Check that all local mapped directories is empty if it is local FULL catchup
* Emit fatal error if that (not existent in map or not empty) tablespace found
*/
static void
catchup_check_tablespaces_existance_in_tbsmapping(PGconn *conn)
{
PGresult *res;
int i;
char *tablespace_path = NULL;
const char *linked_path = NULL;
char *query = "SELECT pg_catalog.pg_tablespace_location(oid) "
"FROM pg_catalog.pg_tablespace "
"WHERE pg_catalog.pg_tablespace_location(oid) <> '';";
res = pgut_execute(conn, query, 0, NULL);
if (!res)
elog(ERROR, "Failed to get list of tablespaces");
for (i = 0; i < res->ntups; i++)
{
tablespace_path = PQgetvalue(res, i, 0);
Assert (strlen(tablespace_path) > 0);
canonicalize_path(tablespace_path);
linked_path = get_tablespace_mapping(tablespace_path);
if (strcmp(tablespace_path, linked_path) == 0)
/* same result -> not found in mapping */
{
if (!fio_is_remote(FIO_DB_HOST))
elog(ERROR, "Local catchup executed, but source database contains "
"tablespace (\"%s\"), that is not listed in the map", tablespace_path);
else
elog(WARNING, "Remote catchup executed and source database contains "
"tablespace (\"%s\"), that is not listed in the map", tablespace_path);
}
if (!is_absolute_path(linked_path))
elog(ERROR, "Tablespace directory path must be an absolute path: \"%s\"",
linked_path);
if (current.backup_mode == BACKUP_MODE_FULL
&& !dir_is_empty(linked_path, FIO_LOCAL_HOST))
elog(ERROR, "Target mapped tablespace directory (\"%s\") is not empty in FULL catchup",
linked_path);
}
PQclear(res);
}
/*
* Get timeline history via replication connection
* returns parray* of TimeLineHistoryEntry*
*/
static parray*
catchup_get_tli_history(ConnectionOptions *conn_opt, TimeLineID tli)
{
PGresult *res;
PGconn *conn;
char *history;
char query[128];
parray *result = NULL;
TimeLineHistoryEntry *entry = NULL;
snprintf(query, sizeof(query), "TIMELINE_HISTORY %u", tli);
/*
* Connect in replication mode to the server.
*/
conn = pgut_connect_replication(conn_opt->pghost,
conn_opt->pgport,
conn_opt->pgdatabase,
conn_opt->pguser,
false);
if (!conn)
return NULL;
res = PQexec(conn, query);
PQfinish(conn);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
elog(WARNING, "Could not send replication command \"%s\": %s",
query, PQresultErrorMessage(res));
PQclear(res);
return NULL;
}
/*
* The response to TIMELINE_HISTORY is a single row result set
* with two fields: filename and content
*/
if (PQnfields(res) != 2 || PQntuples(res) != 1)
{
elog(ERROR, "Unexpected response to TIMELINE_HISTORY command: "
"got %d rows and %d fields, expected %d rows and %d fields",
PQntuples(res), PQnfields(res), 1, 2);
PQclear(res);
return NULL;
}
history = pgut_strdup(PQgetvalue(res, 0, 1));
result = parse_tli_history_buffer(history, tli);
/* some cleanup */
pg_free(history);
PQclear(res);
/* append last timeline entry (as read_timeline_history() do) */
entry = pgut_new(TimeLineHistoryEntry);
entry->tli = tli;
entry->end = InvalidXLogRecPtr;
parray_insert(result, 0, entry);
return result;
}
/*
* catchup multithreaded copy rountine and helper structure and function
*/
/* parameters for catchup_thread_runner() passed from catchup_multithreaded_copy() */
typedef struct
{
PGNodeInfo *nodeInfo;
const char *from_root;
const char *to_root;
parray *source_filelist;
parray *dest_filelist;
XLogRecPtr sync_lsn;
BackupMode backup_mode;
int thread_num;
size_t transfered_bytes;
bool completed;
} catchup_thread_runner_arg;
/* Catchup file copier executed in separate thread */
static void *
catchup_thread_runner(void *arg)
{
int i;
char from_fullpath[MAXPGPATH];
char to_fullpath[MAXPGPATH];
catchup_thread_runner_arg *arguments = (catchup_thread_runner_arg *) arg;
int n_files = parray_num(arguments->source_filelist);
/* catchup a file */
for (i = 0; i < n_files; i++)
{
pgFile *file = (pgFile *) parray_get(arguments->source_filelist, i);
pgFile *dest_file = NULL;
/* We have already copied all directories */
if (S_ISDIR(file->mode))
continue;
if (file->excluded)
continue;
if (!pg_atomic_test_set_flag(&file->lock))
continue;
/* check for interrupt */
if (interrupted || thread_interrupted)
elog(ERROR, "Interrupted during catchup");
if (progress)
elog(INFO, "Progress: (%d/%d). Process file \"%s\"",
i + 1, n_files, file->rel_path);
/* construct destination filepath */
Assert(file->external_dir_num == 0);
join_path_components(from_fullpath, arguments->from_root, file->rel_path);
join_path_components(to_fullpath, arguments->to_root, file->rel_path);
/* Encountered some strange beast */
if (!S_ISREG(file->mode))
elog(WARNING, "Unexpected type %d of file \"%s\", skipping",
file->mode, from_fullpath);
/* Check that file exist in dest pgdata */
if (arguments->backup_mode != BACKUP_MODE_FULL)
{
pgFile **dest_file_tmp = NULL;
dest_file_tmp = (pgFile **) parray_bsearch(arguments->dest_filelist,
file, pgFileCompareRelPathWithExternal);
if (dest_file_tmp)
{
/* File exists in destination PGDATA */
file->exists_in_prev = true;
dest_file = *dest_file_tmp;
}
}
/* Do actual work */
if (file->is_datafile && !file->is_cfs)
{
catchup_data_file(file, from_fullpath, to_fullpath,
arguments->sync_lsn,
arguments->backup_mode,
arguments->nodeInfo->checksum_version,
dest_file != NULL ? dest_file->size : 0);
}
else
{
backup_non_data_file(file, dest_file, from_fullpath, to_fullpath,
arguments->backup_mode, current.parent_backup, true);
}
/* file went missing during catchup */
if (file->write_size == FILE_NOT_FOUND)
continue;
if (file->write_size == BYTES_INVALID)
{
elog(VERBOSE, "Skipping the unchanged file: \"%s\", read %li bytes", from_fullpath, file->read_size);
continue;
}
arguments->transfered_bytes += file->write_size;
elog(VERBOSE, "File \"%s\". Copied "INT64_FORMAT " bytes",
from_fullpath, file->write_size);
}
/* ssh connection to longer needed */
fio_disconnect();
/* Data files transferring is successful */
arguments->completed = true;
return NULL;
}
/*
* main multithreaded copier
* returns size of transfered data file
* or -1 in case of error
*/
static ssize_t
catchup_multithreaded_copy(int num_threads,
PGNodeInfo *source_node_info,
const char *source_pgdata_path,
const char *dest_pgdata_path,
parray *source_filelist,
parray *dest_filelist,
XLogRecPtr sync_lsn,
BackupMode backup_mode)
{
/* arrays with meta info for multi threaded catchup */
catchup_thread_runner_arg *threads_args;
pthread_t *threads;
bool all_threads_successful = true;
ssize_t transfered_bytes_result = 0;
int i;
/* init thread args */
threads_args = (catchup_thread_runner_arg *) palloc(sizeof(catchup_thread_runner_arg) * num_threads);
for (i = 0; i < num_threads; i++)
threads_args[i] = (catchup_thread_runner_arg){
.nodeInfo = source_node_info,
.from_root = source_pgdata_path,
.to_root = dest_pgdata_path,
.source_filelist = source_filelist,
.dest_filelist = dest_filelist,
.sync_lsn = sync_lsn,
.backup_mode = backup_mode,
.thread_num = i + 1,
.transfered_bytes = 0,
.completed = false,
};
/* Run threads */
thread_interrupted = false;
threads = (pthread_t *) palloc(sizeof(pthread_t) * num_threads);
for (i = 0; i < num_threads; i++)
{
elog(VERBOSE, "Start thread num: %i", i);
pthread_create(&threads[i], NULL, &catchup_thread_runner, &(threads_args[i]));
}
/* Wait threads */
for (i = 0; i < num_threads; i++)
{
pthread_join(threads[i], NULL);
all_threads_successful &= threads_args[i].completed;
transfered_bytes_result += threads_args[i].transfered_bytes;
}
free(threads);
free(threads_args);
return all_threads_successful ? transfered_bytes_result : -1;
}
/*
* Sync every file in destination directory to disk
*/
static void
catchup_sync_destination_files(const char* pgdata_path, fio_location location, parray *filelist, pgFile *pg_control_file)
{
char fullpath[MAXPGPATH];
time_t start_time, end_time;
char pretty_time[20];
int i;
elog(INFO, "Syncing copied files to disk");
time(&start_time);
for (i = 0; i < parray_num(filelist); i++)
{
pgFile *file = (pgFile *) parray_get(filelist, i);
/* TODO: sync directory ?
* - at first glance we can rely on fs journaling,
* which is enabled by default on most platforms
* - but PG itself is not relying on fs, its durable_sync
* includes directory sync
*/
if (S_ISDIR(file->mode) || file->excluded)
continue;
Assert(file->external_dir_num == 0);
join_path_components(fullpath, pgdata_path, file->rel_path);
if (fio_sync(fullpath, location) != 0)
elog(ERROR, "Cannot sync file \"%s\": %s", fullpath, strerror(errno));
}
/*
* sync pg_control file
*/
join_path_components(fullpath, pgdata_path, pg_control_file->rel_path);
if (fio_sync(fullpath, location) != 0)
elog(ERROR, "Cannot sync file \"%s\": %s", fullpath, strerror(errno));
time(&end_time);
pretty_time_interval(difftime(end_time, start_time),
pretty_time, lengthof(pretty_time));
elog(INFO, "Files are synced, time elapsed: %s", pretty_time);
}
/*
* Filter filelist helper function (used to process --exclude-path's)
* filelist -- parray of pgFile *, can't be NULL
* exclude_absolute_paths_list -- sorted parray of char * (absolute paths, starting with '/'), can be NULL
* exclude_relative_paths_list -- sorted parray of char * (relative paths), can be NULL
* logging_string -- helper parameter, used for generating verbose log messages ("Source" or "Destination")
*/
static void
filter_filelist(parray *filelist, const char *pgdata,
parray *exclude_absolute_paths_list, parray *exclude_relative_paths_list,
const char *logging_string)
{
int i;
if (exclude_absolute_paths_list == NULL && exclude_relative_paths_list == NULL)
return;
for (i = 0; i < parray_num(filelist); ++i)
{
char full_path[MAXPGPATH];
pgFile *file = (pgFile *) parray_get(filelist, i);
join_path_components(full_path, pgdata, file->rel_path);
if (
(exclude_absolute_paths_list != NULL
&& parray_bsearch(exclude_absolute_paths_list, full_path, pgPrefixCompareString)!= NULL
) || (
exclude_relative_paths_list != NULL
&& parray_bsearch(exclude_relative_paths_list, file->rel_path, pgPrefixCompareString)!= NULL)
)
{
elog(LOG, "%s file \"%s\" excluded with --exclude-path option", logging_string, full_path);
file->excluded = true;
}
}
}
/*
* Entry point of pg_probackup CATCHUP subcommand.
* exclude_*_paths_list are parray's of char *
*/
int
do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads, bool sync_dest_files,
parray *exclude_absolute_paths_list, parray *exclude_relative_paths_list)
{
PGconn *source_conn = NULL;
PGNodeInfo source_node_info;
bool backup_logs = false;
parray *source_filelist = NULL;
pgFile *source_pg_control_file = NULL;
parray *dest_filelist = NULL;
char dest_xlog_path[MAXPGPATH];
RedoParams dest_redo = { 0, InvalidXLogRecPtr, 0 };
PGStopBackupResult stop_backup_result;
bool catchup_isok = true;
int i;
/* for fancy reporting */
time_t start_time, end_time;
ssize_t transfered_datafiles_bytes = 0;
ssize_t transfered_walfiles_bytes = 0;
char pretty_source_bytes[20];
source_conn = catchup_init_state(&source_node_info, source_pgdata, dest_pgdata);
catchup_preflight_checks(&source_node_info, source_conn, source_pgdata, dest_pgdata);
/* we need to sort --exclude_path's for future searching */
if (exclude_absolute_paths_list != NULL)
parray_qsort(exclude_absolute_paths_list, pgCompareString);
if (exclude_relative_paths_list != NULL)
parray_qsort(exclude_relative_paths_list, pgCompareString);
elog(LOG, "Database catchup start");
if (current.backup_mode != BACKUP_MODE_FULL)
{
dest_filelist = parray_new();
dir_list_file(dest_filelist, dest_pgdata,
true, true, false, backup_logs, true, 0, FIO_LOCAL_HOST);
filter_filelist(dest_filelist, dest_pgdata, exclude_absolute_paths_list, exclude_relative_paths_list, "Destination");
// fill dest_redo.lsn and dest_redo.tli
get_redo(dest_pgdata, FIO_LOCAL_HOST, &dest_redo);
elog(INFO, "syncLSN = %X/%X", (uint32) (dest_redo.lsn >> 32), (uint32) dest_redo.lsn);
/*
* Future improvement to catch partial catchup:
* 1. rename dest pg_control into something like pg_control.pbk
* (so user can't start partial catchup'ed instance from this point)
* 2. try to read by get_redo() pg_control and pg_control.pbk (to detect partial catchup)
* 3. at the end (after copy of correct pg_control), remove pg_control.pbk
*/
}
/*
* Make sure that sync point is withing ptrack tracking range
* TODO: move to separate function to use in both backup.c and catchup.c
*/
if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
{
XLogRecPtr ptrack_lsn = get_last_ptrack_lsn(source_conn, &source_node_info);
if (ptrack_lsn > dest_redo.lsn || ptrack_lsn == InvalidXLogRecPtr)
elog(ERROR, "LSN from ptrack_control in source %X/%X is greater than checkpoint LSN in destination %X/%X.\n"
"You can perform only FULL catchup.",
(uint32) (ptrack_lsn >> 32), (uint32) (ptrack_lsn),
(uint32) (dest_redo.lsn >> 32),
(uint32) (dest_redo.lsn));
}
{
char label[1024];
/* notify start of backup to PostgreSQL server */
time2iso(label, lengthof(label), current.start_time, false);
strncat(label, " with pg_probackup", lengthof(label) -
strlen(" with pg_probackup"));
/* Call pg_start_backup function in PostgreSQL connect */
pg_start_backup(label, smooth_checkpoint, ¤t, &source_node_info, source_conn);
elog(LOG, "pg_start_backup START LSN %X/%X", (uint32) (current.start_lsn >> 32), (uint32) (current.start_lsn));
}
/* Sanity: source cluster must be "in future" relatively to dest cluster */
if (current.backup_mode != BACKUP_MODE_FULL &&
dest_redo.lsn > current.start_lsn)
elog(ERROR, "Current START LSN %X/%X is lower than SYNC LSN %X/%X, "
"it may indicate that we are trying to catchup with PostgreSQL instance from the past",
(uint32) (current.start_lsn >> 32), (uint32) (current.start_lsn),
(uint32) (dest_redo.lsn >> 32), (uint32) (dest_redo.lsn));
/* Start stream replication */
join_path_components(dest_xlog_path, dest_pgdata, PG_XLOG_DIR);
fio_mkdir(dest_xlog_path, DIR_PERMISSION, FIO_LOCAL_HOST);
start_WAL_streaming(source_conn, dest_xlog_path, &instance_config.conn_opt,
current.start_lsn, current.tli, false);
source_filelist = parray_new();
/* list files with the logical path. omit $PGDATA */
if (fio_is_remote(FIO_DB_HOST))
fio_list_dir(source_filelist, source_pgdata,
true, true, false, backup_logs, true, 0);
else
dir_list_file(source_filelist, source_pgdata,
true, true, false, backup_logs, true, 0, FIO_LOCAL_HOST);
//REVIEW FIXME. Let's fix that before release.
// TODO what if wal is not a dir (symlink to a dir)?
// - Currently backup/restore transform pg_wal symlink to directory
// so the problem is not only with catchup.
// if we want to make it right - we must provide the way
// for symlink remapping during restore and catchup.
// By default everything must be left as it is.
/* close ssh session in main thread */
fio_disconnect();
/*
* Sort pathname ascending. It is necessary to create intermediate
* directories sequentially.
*
* For example:
* 1 - create 'base'
* 2 - create 'base/1'
*
* Sorted array is used at least in parse_filelist_filenames(),
* extractPageMap(), make_pagemap_from_ptrack().
*/
parray_qsort(source_filelist, pgFileCompareRelPathWithExternal);
//REVIEW Do we want to do similar calculation for dest?
//REVIEW_ANSWER what for?
{
ssize_t source_bytes = 0;
char pretty_bytes[20];
source_bytes += calculate_datasize_of_filelist(source_filelist);
/* Extract information about files in source_filelist parsing their names:*/
parse_filelist_filenames(source_filelist, source_pgdata);
filter_filelist(source_filelist, source_pgdata, exclude_absolute_paths_list, exclude_relative_paths_list, "Source");
current.pgdata_bytes += calculate_datasize_of_filelist(source_filelist);
pretty_size(current.pgdata_bytes, pretty_source_bytes, lengthof(pretty_source_bytes));
pretty_size(source_bytes - current.pgdata_bytes, pretty_bytes, lengthof(pretty_bytes));
elog(INFO, "Source PGDATA size: %s (excluded %s)", pretty_source_bytes, pretty_bytes);
}
elog(LOG, "Start LSN (source): %X/%X, TLI: %X",
(uint32) (current.start_lsn >> 32), (uint32) (current.start_lsn),
current.tli);
if (current.backup_mode != BACKUP_MODE_FULL)
elog(LOG, "LSN in destination: %X/%X, TLI: %X",
(uint32) (dest_redo.lsn >> 32), (uint32) (dest_redo.lsn),
dest_redo.tli);
/* Build page mapping in PTRACK mode */
if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
{
time(&start_time);
elog(INFO, "Extracting pagemap of changed blocks");
/* Build the page map from ptrack information */
make_pagemap_from_ptrack_2(source_filelist, source_conn,
source_node_info.ptrack_schema,
source_node_info.ptrack_version_num,
dest_redo.lsn);
time(&end_time);
elog(INFO, "Pagemap successfully extracted, time elapsed: %.0f sec",
difftime(end_time, start_time));
}
/*
* Make directories before catchup
*/
/*
* We iterate over source_filelist and for every directory with parent 'pg_tblspc'
* we must lookup this directory name in tablespace map.
* If we got a match, we treat this directory as tablespace.
* It means that we create directory specified in tablespace map and
* original directory created as symlink to it.
*/
for (i = 0; i < parray_num(source_filelist); i++)
{
pgFile *file = (pgFile *) parray_get(source_filelist, i);
char parent_dir[MAXPGPATH];
if (!S_ISDIR(file->mode) || file->excluded)
continue;
/*
* check if it is fake "directory" and is a tablespace link
* this is because we passed the follow_symlink when building the list
*/
/* get parent dir of rel_path */
strncpy(parent_dir, file->rel_path, MAXPGPATH);
get_parent_directory(parent_dir);
/* check if directory is actually link to tablespace */
if (strcmp(parent_dir, PG_TBLSPC_DIR) != 0)
{
/* if the entry is a regular directory, create it in the destination */
char dirpath[MAXPGPATH];
join_path_components(dirpath, dest_pgdata, file->rel_path);
elog(VERBOSE, "Create directory '%s'", dirpath);
fio_mkdir(dirpath, DIR_PERMISSION, FIO_LOCAL_HOST);
}
else
{
/* this directory located in pg_tblspc */
const char *linked_path = NULL;
char to_path[MAXPGPATH];
// TODO perform additional check that this is actually symlink?
{ /* get full symlink path and map this path to new location */
char source_full_path[MAXPGPATH];
char symlink_content[MAXPGPATH];
join_path_components(source_full_path, source_pgdata, file->rel_path);
fio_readlink(source_full_path, symlink_content, sizeof(symlink_content), FIO_DB_HOST);
/* we checked that mapping exists in preflight_checks for local catchup */
linked_path = get_tablespace_mapping(symlink_content);
elog(INFO, "Map tablespace full_path: \"%s\" old_symlink_content: \"%s\" new_symlink_content: \"%s\"\n",
source_full_path,
symlink_content,
linked_path);
}
if (!is_absolute_path(linked_path))
elog(ERROR, "Tablespace directory path must be an absolute path: %s\n",
linked_path);
join_path_components(to_path, dest_pgdata, file->rel_path);
elog(VERBOSE, "Create directory \"%s\" and symbolic link \"%s\"",
linked_path, to_path);
/* create tablespace directory */
if (fio_mkdir(linked_path, file->mode, FIO_LOCAL_HOST) != 0)
elog(ERROR, "Could not create tablespace directory \"%s\": %s",
linked_path, strerror(errno));
/* create link to linked_path */
if (fio_symlink(linked_path, to_path, true, FIO_LOCAL_HOST) < 0)
elog(ERROR, "Could not create symbolic link \"%s\" -> \"%s\": %s",
linked_path, to_path, strerror(errno));
}
}
/*
* find pg_control file (in already sorted source_filelist)
* and exclude it from list for future special processing
*/
{
int control_file_elem_index;
pgFile search_key;
MemSet(&search_key, 0, sizeof(pgFile));
/* pgFileCompareRelPathWithExternal uses only .rel_path and .external_dir_num for comparision */
search_key.rel_path = XLOG_CONTROL_FILE;
search_key.external_dir_num = 0;
control_file_elem_index = parray_bsearch_index(source_filelist, &search_key, pgFileCompareRelPathWithExternal);
if(control_file_elem_index < 0)
elog(ERROR, "\"%s\" not found in \"%s\"\n", XLOG_CONTROL_FILE, source_pgdata);
source_pg_control_file = parray_remove(source_filelist, control_file_elem_index);
}
/* TODO before public release: must be more careful with pg_control.
* when running catchup or incremental restore
* cluster is actually in two states
* simultaneously - old and new, so
* it must contain both pg_control files
* describing those states: global/pg_control_old, global/pg_control_new
* 1. This approach will provide us with means of
* robust detection of previos failures and thus correct operation retrying (or forbidding).
* 2. We will have the ability of preventing instance from starting
* in the middle of our operations.
*/
/*
* remove absent source files in dest (dropped tables, etc...)
* note: global/pg_control will also be deleted here
* mark dest files (that excluded with source --exclude-path) also for exclusion
*/
if (current.backup_mode != BACKUP_MODE_FULL)
{
elog(INFO, "Removing redundant files in destination directory");
parray_qsort(dest_filelist, pgFileCompareRelPathWithExternalDesc);
for (i = 0; i < parray_num(dest_filelist); i++)
{
bool redundant = true;
pgFile *file = (pgFile *) parray_get(dest_filelist, i);
pgFile **src_file = NULL;
//TODO optimize it and use some merge-like algorithm
//instead of bsearch for each file.
src_file = (pgFile **) parray_bsearch(source_filelist, file, pgFileCompareRelPathWithExternal);
if (src_file!= NULL && !(*src_file)->excluded && file->excluded)
(*src_file)->excluded = true;
if (src_file!= NULL || file->excluded)
redundant = false;
/* pg_filenode.map are always copied, because it's crc cannot be trusted */
Assert(file->external_dir_num == 0);
if (pg_strcasecmp(file->name, RELMAPPER_FILENAME) == 0)
redundant = true;
/* if file does not exists in destination list, then we can safely unlink it */
if (redundant)
{
char fullpath[MAXPGPATH];
join_path_components(fullpath, dest_pgdata, file->rel_path);
fio_delete(file->mode, fullpath, FIO_DB_HOST);
elog(VERBOSE, "Deleted file \"%s\"", fullpath);
/* shrink dest pgdata list */
pgFileFree(file);
parray_remove(dest_filelist, i);
i--;
}
}
}
/* clear file locks */
pfilearray_clear_locks(source_filelist);
/* Sort by size for load balancing */
parray_qsort(source_filelist, pgFileCompareSizeDesc);
/* Sort the array for binary search */
if (dest_filelist)
parray_qsort(dest_filelist, pgFileCompareRelPathWithExternal);
/* run copy threads */
elog(INFO, "Start transferring data files");
time(&start_time);
transfered_datafiles_bytes = catchup_multithreaded_copy(num_threads, &source_node_info,
source_pgdata, dest_pgdata,
source_filelist, dest_filelist,
dest_redo.lsn, current.backup_mode);
catchup_isok = transfered_datafiles_bytes != -1;
/* at last copy control file */
if (catchup_isok)
{
char from_fullpath[MAXPGPATH];
char to_fullpath[MAXPGPATH];
join_path_components(from_fullpath, source_pgdata, source_pg_control_file->rel_path);
join_path_components(to_fullpath, dest_pgdata, source_pg_control_file->rel_path);
copy_pgcontrol_file(from_fullpath, FIO_DB_HOST,
to_fullpath, FIO_LOCAL_HOST, source_pg_control_file);
transfered_datafiles_bytes += source_pg_control_file->size;
}
if (!catchup_isok)
{
char pretty_time[20];
char pretty_transfered_data_bytes[20];
time(&end_time);
pretty_time_interval(difftime(end_time, start_time),
pretty_time, lengthof(pretty_time));
pretty_size(transfered_datafiles_bytes, pretty_transfered_data_bytes, lengthof(pretty_transfered_data_bytes));
elog(ERROR, "Catchup failed. Transfered: %s, time elapsed: %s",
pretty_transfered_data_bytes, pretty_time);
}
/* Notify end of backup */
{
//REVIEW Is it relevant to catchup? I suppose it isn't, since catchup is a new code.
//If we do need it, please write a comment explaining that.
/* kludge against some old bug in archive_timeout. TODO: remove in 3.0.0 */
int timeout = (instance_config.archive_timeout > 0) ?
instance_config.archive_timeout : ARCHIVE_TIMEOUT_DEFAULT;
char *stop_backup_query_text = NULL;
pg_silent_client_messages(source_conn);
/* Execute pg_stop_backup using PostgreSQL connection */