Skip to content

Commit 41457fc

Browse files
committed
Minor cleanup of pg_rewind.
Update comments and function names to use the terms "source" and "target" consistently. Some places were calling them remote and local instead, which was confusing. Fix incorrect comment in extractPageInfo on database creation record - it was wrong on what happens for databases created in the target that don't exist in source.
1 parent 0d8a22a commit 41457fc

File tree

8 files changed

+58
-52
lines changed

8 files changed

+58
-52
lines changed

src/bin/pg_rewind/fetch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
#include "filemap.h"
2727

2828
void
29-
fetchRemoteFileList(void)
29+
fetchSourceFileList(void)
3030
{
3131
if (datadir_source)
32-
traverse_datadir(datadir_source, &process_remote_file);
32+
traverse_datadir(datadir_source, &process_source_file);
3333
else
3434
libpqProcessFileList();
3535
}

src/bin/pg_rewind/fetch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* Common interface. Calls the copy or libpq method depending on global
2626
* config options.
2727
*/
28-
extern void fetchRemoteFileList(void);
28+
extern void fetchSourceFileList(void);
2929
extern char *fetchFile(char *filename, size_t *filesize);
3030
extern void executeFileMap(void);
3131

src/bin/pg_rewind/filemap.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ filemap_create(void)
5151
}
5252

5353
/*
54-
* Callback for processing remote file list.
54+
* Callback for processing source file list.
5555
*
5656
* This is called once for every file in the source server. We decide what
5757
* action needs to be taken for the file, depending on whether the file
5858
* exists in the target and whether the size matches.
5959
*/
6060
void
61-
process_remote_file(const char *path, file_type_t type, size_t newsize,
61+
process_source_file(const char *path, file_type_t type, size_t newsize,
6262
const char *link_target)
6363
{
6464
bool exists;
@@ -97,7 +97,7 @@ process_remote_file(const char *path, file_type_t type, size_t newsize,
9797

9898
snprintf(localpath, sizeof(localpath), "%s/%s", datadir_target, path);
9999

100-
/* Does the corresponding local file exist? */
100+
/* Does the corresponding file exist in the target data dir? */
101101
if (lstat(localpath, &statbuf) < 0)
102102
{
103103
if (errno != ENOENT)
@@ -185,18 +185,19 @@ process_remote_file(const char *path, file_type_t type, size_t newsize,
185185
*
186186
* If it's smaller in the target, it means that it has been
187187
* truncated in the target, or enlarged in the source, or
188-
* both. If it was truncated locally, we need to copy the
189-
* missing tail from the remote system. If it was enlarged in
190-
* the remote system, there will be WAL records in the remote
188+
* both. If it was truncated in the target, we need to copy the
189+
* missing tail from the source system. If it was enlarged in
190+
* the source system, there will be WAL records in the source
191191
* system for the new blocks, so we wouldn't need to copy them
192192
* here. But we don't know which scenario we're dealing with,
193193
* and there's no harm in copying the missing blocks now, so
194194
* do it now.
195195
*
196-
* If it's the same size, do nothing here. Any locally
197-
* modified blocks will be copied based on parsing the local
198-
* WAL, and any remotely modified blocks will be updated after
199-
* rewinding, when the remote WAL is replayed.
196+
* If it's the same size, do nothing here. Any blocks modified
197+
* in the target will be copied based on parsing the target
198+
* system's WAL, and any blocks modified in the source will be
199+
* updated after rewinding, when the source system's WAL is
200+
* replayed.
200201
*/
201202
oldsize = statbuf.st_size;
202203
if (oldsize < newsize)
@@ -233,14 +234,15 @@ process_remote_file(const char *path, file_type_t type, size_t newsize,
233234
}
234235

235236
/*
236-
* Callback for processing local file list.
237+
* Callback for processing target file list.
237238
*
238-
* All remote files must be already processed before calling this. This only
239-
* marks local files that didn't exist in the remote system for deletion.
239+
* All source files must be already processed before calling this. This only
240+
* marks target data directory's files that didn't exist in the source for
241+
* deletion.
240242
*/
241243
void
242-
process_local_file(const char *path, file_type_t type, size_t oldsize,
243-
const char *link_target)
244+
process_target_file(const char *path, file_type_t type, size_t oldsize,
245+
const char *link_target)
244246
{
245247
bool exists;
246248
char localpath[MAXPGPATH];
@@ -266,7 +268,7 @@ process_local_file(const char *path, file_type_t type, size_t oldsize,
266268
if (map->nlist == 0)
267269
{
268270
/* should not happen */
269-
pg_fatal("remote file list is empty\n");
271+
pg_fatal("source file list is empty\n");
270272
}
271273

272274
filemap_list_to_array(map);
@@ -288,7 +290,7 @@ process_local_file(const char *path, file_type_t type, size_t oldsize,
288290
exists = (bsearch(&key_ptr, map->array, map->narray, sizeof(file_entry_t *),
289291
path_cmp) != NULL);
290292

291-
/* Remove any file or folder that doesn't exist in the remote system. */
293+
/* Remove any file or folder that doesn't exist in the source system. */
292294
if (!exists)
293295
{
294296
entry = pg_malloc(sizeof(file_entry_t));
@@ -313,16 +315,16 @@ process_local_file(const char *path, file_type_t type, size_t oldsize,
313315
else
314316
{
315317
/*
316-
* We already handled all files that exist in the remote system in
317-
* process_remote_file().
318+
* We already handled all files that exist in the source system in
319+
* process_source_file().
318320
*/
319321
}
320322
}
321323

322324
/*
323-
* This callback gets called while we read the old WAL, for every block that
324-
* have changed in the local system. It makes note of all the changed blocks
325-
* in the pagemap of the file.
325+
* This callback gets called while we read the WAL in the target, for every
326+
* block that have changed in the target system. It makes note of all the
327+
* changed blocks in the pagemap of the file.
326328
*/
327329
void
328330
process_block_change(ForkNumber forknum, RelFileNode rnode, BlockNumber blkno)
@@ -388,8 +390,8 @@ process_block_change(ForkNumber forknum, RelFileNode rnode, BlockNumber blkno)
388390
{
389391
/*
390392
* If we don't have any record of this file in the file map, it means
391-
* that it's a relation that doesn't exist in the remote system, and
392-
* it was subsequently removed in the local system, too. We can safely
393+
* that it's a relation that doesn't exist in the source system, and
394+
* it was subsequently removed in the target system, too. We can safely
393395
* ignore it.
394396
*/
395397
}

src/bin/pg_rewind/filemap.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ typedef struct file_entry_t
6262
typedef struct filemap_t
6363
{
6464
/*
65-
* New entries are accumulated to a linked list, in process_remote_file
66-
* and process_local_file.
65+
* New entries are accumulated to a linked list, in process_source_file
66+
* and process_target_file.
6767
*/
6868
file_entry_t *first;
6969
file_entry_t *last;
@@ -94,9 +94,12 @@ extern void calculate_totals(void);
9494
extern void print_filemap(void);
9595

9696
/* Functions for populating the filemap */
97-
extern void process_remote_file(const char *path, file_type_t type, size_t newsize, const char *link_target);
98-
extern void process_local_file(const char *path, file_type_t type, size_t newsize, const char *link_target);
99-
extern void process_block_change(ForkNumber forknum, RelFileNode rnode, BlockNumber blkno);
97+
extern void process_source_file(const char *path, file_type_t type,
98+
size_t newsize, const char *link_target);
99+
extern void process_target_file(const char *path, file_type_t type,
100+
size_t newsize, const char *link_target);
101+
extern void process_block_change(ForkNumber forknum, RelFileNode rnode,
102+
BlockNumber blkno);
100103
extern void filemap_finalize(void);
101104

102105
#endif /* FILEMAP_H */

src/bin/pg_rewind/libpq_fetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ libpqProcessFileList(void)
190190
else
191191
type = FILE_TYPE_REGULAR;
192192

193-
process_remote_file(path, type, filesize, link_target);
193+
process_source_file(path, type, filesize, link_target);
194194
}
195195
}
196196

src/bin/pg_rewind/logging.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ pg_log(eLogType type, const char *fmt,...)
7676
}
7777

7878

79+
/*
80+
* Print an error message, and exit.
81+
*/
7982
void
8083
pg_fatal(const char *fmt,...)
8184
{

src/bin/pg_rewind/parsexlog.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -314,39 +314,37 @@ extractPageInfo(XLogReaderState *record)
314314
{
315315
/*
316316
* New databases can be safely ignored. It won't be present in the
317-
* remote system, so it will be copied in toto. There's one
318-
* corner-case, though: if a new, different, database is also created
319-
* in the remote system, we'll see that the files already exist and
320-
* not copy them. That's OK, though; WAL replay of creating the new
321-
* database, from the remote WAL, will re-copy the new database,
322-
* overwriting the database created in the local system.
317+
* source system, so it will be deleted. There's one corner-case,
318+
* though: if a new, different, database is also created in the
319+
* source system, we'll see that the files already exist and not copy
320+
* them. That's OK, though; WAL replay of creating the new database,
321+
* from the source systems's WAL, will re-copy the new database,
322+
* overwriting the database created in the target system.
323323
*/
324324
}
325325
else if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_DROP)
326326
{
327327
/*
328328
* An existing database was dropped. We'll see that the files don't
329-
* exist in local system, and copy them in toto from the remote
329+
* exist in the target data dir, and copy them in toto from the source
330330
* system. No need to do anything special here.
331331
*/
332332
}
333333
else if (rmid == RM_SMGR_ID && rminfo == XLOG_SMGR_CREATE)
334334
{
335335
/*
336-
* We can safely ignore these. The local file will be removed, if it
337-
* doesn't exist in remote system. If a file with same name is created
338-
* in remote system, too, there will be WAL records for all the blocks
339-
* in it.
336+
* We can safely ignore these. The file will be removed from the
337+
* target, if it doesn't exist in source system. If a file with same
338+
* name is created in source system, too, there will be WAL records
339+
* for all the blocks in it.
340340
*/
341341
}
342342
else if (rmid == RM_SMGR_ID && rminfo == XLOG_SMGR_TRUNCATE)
343343
{
344344
/*
345-
* We can safely ignore these. If a file is truncated locally, we'll
346-
* notice that when we compare the sizes, and will copy the missing
347-
* tail from remote system.
348-
*
349-
* TODO: But it would be nice to do some sanity cross-checking here..
345+
* We can safely ignore these. When we compare the sizes later on,
346+
* we'll notice that they differ, and copy the missing tail from
347+
* source system.
350348
*/
351349
}
352350
else if (info & XLR_SPECIAL_REL_UPDATE)

src/bin/pg_rewind/pg_rewind.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,13 @@ main(int argc, char **argv)
263263
chkpttli);
264264

265265
/*
266-
* Build the filemap, by comparing the remote and local data directories.
266+
* Build the filemap, by comparing the source and target data directories.
267267
*/
268268
filemap_create();
269269
pg_log(PG_PROGRESS, "reading source file list\n");
270-
fetchRemoteFileList();
270+
fetchSourceFileList();
271271
pg_log(PG_PROGRESS, "reading target file list\n");
272-
traverse_datadir(datadir_target, &process_local_file);
272+
traverse_datadir(datadir_target, &process_target_file);
273273

274274
/*
275275
* Read the target WAL from last checkpoint before the point of fork, to

0 commit comments

Comments
 (0)