forked from vmware-archive/pg_rewind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_rewind.c
542 lines (467 loc) · 14.2 KB
/
pg_rewind.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
/*-------------------------------------------------------------------------
*
* pg_rewind.c
* Synchronizes an old master server to a new timeline
*
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
* Portions Copyright (c) 2013-2015 VMware, Inc. All Rights Reserved.
*
*-------------------------------------------------------------------------
*/
#include "postgres_fe.h"
#include "pg_rewind.h"
#include "fetch.h"
#include "filemap.h"
#include "access/timeline.h"
#include "access/xlog_internal.h"
#include "catalog/catversion.h"
#include "catalog/pg_control.h"
#include "storage/bufpage.h"
#include "getopt_long.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
static void usage(const char *progname);
static void createBackupLabel(XLogRecPtr startpoint, TimeLineID starttli,
XLogRecPtr checkpointloc);
static void digestControlFile(ControlFileData *ControlFile, char *source, size_t size);
static void updateControlFile(ControlFileData *ControlFile,
char *datadir);
static void sanityChecks(void);
static void findCommonAncestorTimeline(XLogRecPtr *recptr, TimeLineID *tli);
static ControlFileData ControlFile_target;
static ControlFileData ControlFile_source;
const char *progname;
char *datadir_target = NULL;
char *datadir_source = NULL;
char *connstr_source = NULL;
int verbose;
int dry_run;
static void
usage(const char *progname)
{
printf("%s resynchronizes a cluster with another copy of the cluster.\n\n", progname);
printf("Usage:\n %s [OPTION]...\n\n", progname);
printf("Options:\n");
printf(" -D, --target-pgdata=DIRECTORY\n");
printf(" existing data directory to modify\n");
printf(" --source-pgdata=DIRECTORY\n");
printf(" source data directory to sync with\n");
printf(" --source-server=CONNSTR\n");
printf(" source server to sync with\n");
printf(" -v write a lot of progress messages\n");
printf(" -n, --dry-run stop before modifying anything\n");
printf(" -V, --version output version information, then exit\n");
printf(" -?, --help show this help, then exit\n");
printf("\n");
printf("Report bugs to https://github.com/vmware/pg_rewind.\n");
}
int
main(int argc, char **argv)
{
static struct option long_options[] = {
{"help", no_argument, NULL, '?'},
{"target-pgdata", required_argument, NULL, 'D'},
{"source-pgdata", required_argument, NULL, 1},
{"source-server", required_argument, NULL, 2},
{"version", no_argument, NULL, 'V'},
{"dry-run", no_argument, NULL, 'n'},
{"verbose", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
int option_index;
int c;
XLogRecPtr divergerec;
TimeLineID lastcommontli;
XLogRecPtr chkptrec;
TimeLineID chkpttli;
XLogRecPtr chkptredo;
size_t size;
char *buffer;
bool rewind_needed;
ControlFileData ControlFile;
progname = get_progname(argv[0]);
/* Set default parameter values */
verbose = 0;
dry_run = 0;
/* Process command-line arguments */
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
usage(progname);
exit(0);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
puts("pg_rewind " PG_REWIND_VERSION);
exit(0);
}
}
while ((c = getopt_long(argc, argv, "D:vn", long_options, &option_index)) != -1)
{
switch (c)
{
case '?':
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
case ':':
exit(1);
case 'v':
verbose = 1;
break;
case 'n':
dry_run = 1;
break;
case 'D': /* -D or --target-pgdata */
datadir_target = pg_strdup(optarg);
break;
case 1: /* --source-pgdata */
datadir_source = pg_strdup(optarg);
break;
case 2: /* --source-server */
connstr_source = pg_strdup(optarg);
break;
}
}
/* No source given? Show usage */
if (datadir_source == NULL && connstr_source == NULL)
{
fprintf(stderr, "%s: no source specified\n", progname);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
if (datadir_target == NULL)
{
fprintf(stderr, "%s: no target data directory specified\n", progname);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
if (argc != optind)
{
fprintf(stderr, "%s: invalid arguments\n", progname);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
/*
* Connect to remote server
*/
if (connstr_source)
libpqConnect(connstr_source);
/*
* Ok, we have all the options and we're ready to start. Read in all the
* information we need from both clusters.
*/
buffer = slurpFile(datadir_target, "global/pg_control", &size);
digestControlFile(&ControlFile_target, buffer, size);
pg_free(buffer);
buffer = fetchFile("global/pg_control", &size);
digestControlFile(&ControlFile_source, buffer, size);
pg_free(buffer);
sanityChecks();
/*
* If both clusters are already on the same timeline, there's nothing
* to do.
*/
if (ControlFile_target.checkPointCopy.ThisTimeLineID == ControlFile_source.checkPointCopy.ThisTimeLineID)
{
fprintf(stderr, "source and target cluster are both on the same timeline.\n");
exit(1);
}
findCommonAncestorTimeline(&divergerec, &lastcommontli);
printf("The servers diverged at WAL position %X/%X on timeline %u.\n",
(uint32) (divergerec >> 32), (uint32) divergerec, lastcommontli);
/*
* Check for the possibility that the target is in fact a direct ancestor
* of the source. In that case, there is no divergent history in the
* target that needs rewinding.
*/
if (ControlFile_target.checkPoint >= divergerec)
{
rewind_needed = true;
}
else
{
XLogRecPtr chkptendrec;
/* Read the checkpoint record on the target to see where it ends. */
chkptendrec = readOneRecord(datadir_target,
ControlFile_target.checkPoint,
ControlFile_target.checkPointCopy.ThisTimeLineID);
/*
* If the histories diverged exactly at the end of the shutdown
* checkpoint record on the target, there are no WAL records in the
* target that don't belong in the source's history, and no rewind is
* needed.
*/
if (chkptendrec == divergerec)
rewind_needed = false;
else
rewind_needed = true;
}
if (!rewind_needed)
{
printf("No rewind required.\n");
exit(0);
}
findLastCheckpoint(datadir_target, divergerec, lastcommontli,
&chkptrec, &chkpttli, &chkptredo);
printf("Rewinding from Last common checkpoint at %X/%X on timeline %u\n",
(uint32) (chkptrec >> 32), (uint32) chkptrec,
chkpttli);
/*
* Build the filemap, by comparing the remote and local data directories
*/
(void) filemap_create();
fetchRemoteFileList();
traverse_datadir(datadir_target, &process_local_file);
/*
* Read the target WAL from last checkpoint before the point of fork,
* to extract all the pages that were modified on the target cluster
* after the fork.
*/
extractPageMap(datadir_target, chkptrec, lastcommontli);
filemap_finalize();
/* XXX: this is probably too verbose even in verbose mode */
if (verbose)
print_filemap();
/* Ok, we're ready to start copying things over. */
executeFileMap();
createBackupLabel(chkptredo, chkpttli, chkptrec);
/*
* Update control file of target file and make it ready to
* perform archive recovery when restarting.
*/
memcpy(&ControlFile, &ControlFile_source, sizeof(ControlFileData));
ControlFile.minRecoveryPoint = divergerec;
ControlFile.minRecoveryPointTLI = ControlFile_target.checkPointCopy.ThisTimeLineID;
ControlFile.state = DB_IN_ARCHIVE_RECOVERY;
updateControlFile(&ControlFile, datadir_target);
printf("Done!\n");
return 0;
}
static void
sanityChecks(void)
{
/* Check that there's no backup_label in either cluster */
/* Check system_id match */
if (ControlFile_target.system_identifier != ControlFile_source.system_identifier)
{
fprintf(stderr, "source and target clusters are from different systems\n");
exit(1);
}
/* check version */
if (ControlFile_target.pg_control_version != PG_CONTROL_VERSION ||
ControlFile_source.pg_control_version != PG_CONTROL_VERSION ||
ControlFile_target.catalog_version_no != CATALOG_VERSION_NO ||
ControlFile_source.catalog_version_no != CATALOG_VERSION_NO)
{
fprintf(stderr, "clusters are not compatible with this version of pg_rewind\n");
exit(1);
}
/*
* Target cluster need to use checksums or hint bit wal-logging, this to
* prevent from data corruption that could occur because of hint bits.
*/
if (ControlFile_target.data_checksum_version != PG_DATA_CHECKSUM_VERSION &&
!ControlFile_target.wal_log_hints)
{
fprintf(stderr, "target master need to use either data checksums or \"wal_log_hints = on\".\n");
exit(1);
}
/*
* Target cluster better not be running. This doesn't guard against someone
* starting the cluster concurrently. Also, this is probably more strict
* than necessary; it's OK if the master was not shut down cleanly, as
* long as it isn't running at the moment.
*/
if (ControlFile_target.state != DB_SHUTDOWNED)
{
fprintf(stderr, "target master must be shut down cleanly.\n");
exit(1);
}
}
/*
* Determine the TLI of the last common timeline in the histories of the two
* clusters. *tli is set to the last common timeline, and *recptr is set to
* the position where the histories diverged (ie. the first WAL record that's
* not the same in both clusters).
*
* Control files of both clusters must be read into ControlFile_target/source
* before calling this.
*/
static void
findCommonAncestorTimeline(XLogRecPtr *recptr, TimeLineID *tli)
{
TimeLineID targettli;
TimeLineHistoryEntry *sourceHistory;
int nentries;
int i;
TimeLineID sourcetli;
targettli = ControlFile_target.checkPointCopy.ThisTimeLineID;
sourcetli = ControlFile_source.checkPointCopy.ThisTimeLineID;
/* Timeline 1 does not have a history file, so no need to check */
if (sourcetli == 1)
{
sourceHistory = (TimeLineHistoryEntry *) pg_malloc(sizeof(TimeLineHistoryEntry));
sourceHistory->tli = sourcetli;
sourceHistory->begin = sourceHistory->end = InvalidXLogRecPtr;
nentries = 1;
}
else
{
char path[MAXPGPATH];
char *histfile;
TLHistoryFilePath(path, sourcetli);
histfile = fetchFile(path, NULL);
sourceHistory = rewind_parseTimeLineHistory(histfile,
ControlFile_source.checkPointCopy.ThisTimeLineID,
&nentries);
pg_free(histfile);
}
/*
* Trace the history backwards, until we hit the target timeline.
*
* TODO: This assumes that there are no timeline switches on the target
* cluster after the fork.
*/
for (i = nentries - 1; i >= 0; i--)
{
TimeLineHistoryEntry *entry = &sourceHistory[i];
if (entry->tli == targettli)
{
/* found it */
*recptr = entry->end;
*tli = entry->tli;
free(sourceHistory);
return;
}
}
fprintf(stderr, "could not find common ancestor of the source and target cluster's timelines\n");
exit(1);
}
/*
* Create a backup_label file that forces recovery to begin at the last common
* checkpoint.
*/
static void
createBackupLabel(XLogRecPtr startpoint, TimeLineID starttli, XLogRecPtr checkpointloc)
{
XLogSegNo startsegno;
char BackupLabelFilePath[MAXPGPATH];
FILE *fp;
time_t stamp_time;
char strfbuf[128];
char xlogfilename[MAXFNAMELEN];
struct tm *tmp;
if (dry_run)
return;
XLByteToSeg(startpoint, startsegno);
XLogFileName(xlogfilename, starttli, startsegno);
/*
* TODO: move old file out of the way, if any. And perhaps create the
* file with temporary name first and rename in place after it's done.
*/
snprintf(BackupLabelFilePath, MAXPGPATH,
"%s/backup_label" /* BACKUP_LABEL_FILE */, datadir_target);
/*
* Construct backup label file
*/
fp = fopen(BackupLabelFilePath, "wb");
stamp_time = time(NULL);
tmp = localtime(&stamp_time);
strftime(strfbuf, sizeof(strfbuf), "%Y-%m-%d %H:%M:%S %Z", tmp);
fprintf(fp, "START WAL LOCATION: %X/%X (file %s)\n",
(uint32) (startpoint >> 32), (uint32) startpoint, xlogfilename);
fprintf(fp, "CHECKPOINT LOCATION: %X/%X\n",
(uint32) (checkpointloc >> 32), (uint32) checkpointloc);
fprintf(fp, "BACKUP METHOD: pg_rewind\n");
fprintf(fp, "BACKUP FROM: standby\n");
fprintf(fp, "START TIME: %s\n", strfbuf);
/* omit LABEL: line */
if (fclose(fp) != 0)
{
fprintf(stderr, _("could not write backup label file \"%s\": %s\n"),
BackupLabelFilePath, strerror(errno));
exit(2);
}
}
/*
* Check CRC of control file
*/
static void
checkControlFile(ControlFileData *ControlFile)
{
pg_crc32 crc;
/* Calculate CRC */
INIT_CRC32C(crc);
COMP_CRC32C(crc,
(char *) ControlFile,
offsetof(ControlFileData, crc));
FIN_CRC32C(crc);
/* And simply compare it */
if (!EQ_CRC32C(crc, ControlFile->crc))
{
fprintf(stderr, "unexpected control file CRC\n");
exit(1);
}
}
/*
* Verify control file contents in the buffer src, and copy it to *ControlFile.
*/
static void
digestControlFile(ControlFileData *ControlFile, char *src, size_t size)
{
if (size != PG_CONTROL_SIZE)
{
fprintf(stderr, "unexpected control file size %d, expected %d\n",
(int) size, PG_CONTROL_SIZE);
exit(1);
}
memcpy(ControlFile, src, sizeof(ControlFileData));
/* Additional checks on control file */
checkControlFile(ControlFile);
}
/*
* Update a control file with fresh content
*/
static void
updateControlFile(ControlFileData *ControlFile, char *datadir)
{
char path[MAXPGPATH];
char buffer[PG_CONTROL_SIZE];
FILE *fp;
if (dry_run)
return;
/* Recalculate CRC of control file */
INIT_CRC32C(ControlFile->crc);
COMP_CRC32C(ControlFile->crc,
(char *) ControlFile,
offsetof(ControlFileData, crc));
FIN_CRC32C(ControlFile->crc);
/*
* Write out PG_CONTROL_SIZE bytes into pg_control by zero-padding
* the excess over sizeof(ControlFileData) to avoid premature EOF
* related errors when reading it.
*/
memset(buffer, 0, PG_CONTROL_SIZE);
memcpy(buffer, ControlFile, sizeof(ControlFileData));
snprintf(path, MAXPGPATH,
"%s/global/pg_control", datadir);
if ((fp = fopen(path, "wb")) == NULL)
{
fprintf(stderr,"Could not open the pg_control file\n");
exit(1);
}
if (fwrite(buffer, 1,
PG_CONTROL_SIZE, fp) != PG_CONTROL_SIZE)
{
fprintf(stderr,"Could not write the pg_control file\n");
exit(1);
}
if (fclose(fp))
{
fprintf(stderr,"Could not close the pg_control file\n");
exit(1);
}
}