Skip to content

Commit 2cceb71

Browse files
committed
Review answer #2
1 parent 746a1a5 commit 2cceb71

File tree

2 files changed

+40
-65
lines changed

2 files changed

+40
-65
lines changed

src/catchup.c

+33-58
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads)
6161
//Besides, the name of this function looks strange to me.
6262
//Maybe catchup_init_state() or catchup_setup() will do better?
6363
//I'd also suggest to wrap all these fields into some CatchupState, but it isn't urgent.
64+
/*
65+
* Prepare for work: fill some globals, open connection to source database
66+
*/
6467
static PGconn *
6568
catchup_collect_info(PGNodeInfo *source_node_info, const char *source_pgdata, const char *dest_pgdata)
6669
{
@@ -186,7 +189,7 @@ catchup_preflight_checks(PGNodeInfo *source_node_info, PGconn *source_conn,
186189

187190
/*
188191
* Check that all tablespaces exists in tablespace mapping (--tablespace-mapping option)
189-
* Emit fatal error if that tablespace found
192+
* Emit fatal error if that (not existent in map) tablespace found
190193
*/
191194
static void
192195
check_tablespaces_existance_in_tbsmapping(PGconn *conn)
@@ -227,6 +230,7 @@ check_tablespaces_existance_in_tbsmapping(PGconn *conn)
227230
/*
228231
* TODO:
229232
* - add description
233+
* main worker function, to be moved into do_catchup() and then to be split into meaningful pieces
230234
*/
231235
static void
232236
do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *source_conn,
@@ -315,13 +319,10 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
315319
(uint32) (dest_redo.lsn >> 32), (uint32) (dest_redo.lsn));
316320

317321
/* Start stream replication */
318-
if (stream_wal)
319-
{
320-
join_path_components(dest_xlog_path, dest_pgdata, PG_XLOG_DIR);
321-
fio_mkdir(dest_xlog_path, DIR_PERMISSION, FIO_BACKUP_HOST);
322-
start_WAL_streaming(source_conn, dest_xlog_path, &instance_config.conn_opt,
323-
current.start_lsn, current.tli);
324-
}
322+
join_path_components(dest_xlog_path, dest_pgdata, PG_XLOG_DIR);
323+
fio_mkdir(dest_xlog_path, DIR_PERMISSION, FIO_BACKUP_HOST);
324+
start_WAL_streaming(source_conn, dest_xlog_path, &instance_config.conn_opt,
325+
current.start_lsn, current.tli);
325326

326327
source_filelist = parray_new();
327328

@@ -358,7 +359,6 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
358359
*/
359360
parray_qsort(source_filelist, pgFileCompareRelPathWithExternal);
360361

361-
//REVIEW Please adjust the comment.
362362
/* Extract information about files in source_filelist parsing their names:*/
363363
parse_filelist_filenames(source_filelist, source_pgdata);
364364

@@ -408,7 +408,7 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
408408

409409
/*
410410
* check if it is fake "directory" and is a tablespace link
411-
* это происходит потому что мы передали follow_symlink при построении списка
411+
* this is because we passed the follow_symlink when building the list
412412
*/
413413
/* get parent dir of rel_path */
414414
strncpy(parent_dir, file->rel_path, MAXPGPATH);
@@ -431,18 +431,20 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
431431
const char *linked_path = NULL;
432432
char to_path[MAXPGPATH];
433433

434-
// perform additional check that this is actually synlink?
434+
// perform additional check that this is actually symlink?
435435
//REVIEW Why is this code block separated?
436+
//REVIEW_ANSWER because i want to localize usage of source_full_path and symlink_content
436437
{ /* get full symlink path and map this path to new location */
437438
char source_full_path[MAXPGPATH];
438439
char symlink_content[MAXPGPATH];
439440
join_path_components(source_full_path, source_pgdata, file->rel_path);
440441
fio_readlink(source_full_path, symlink_content, sizeof(symlink_content), FIO_DB_HOST);
441442
//REVIEW What if we won't find mapping for this tablespace?
442443
//I'd expect a failure. Otherwise, we may spoil source database data.
444+
// REVIEW_ANSWER we checked that in preflight_checks for local catchup
445+
// and for remote catchup this may be correct behavior
443446
linked_path = leaked_abstraction_get_tablespace_mapping(symlink_content);
444-
// TODO: check that linked_path != symlink_content in case of local catchup?
445-
elog(WARNING, "Map tablespace full_path: \"%s\" old_symlink_content: \"%s\" old_symlink_content: \"%s\"\n",
447+
elog(INFO, "Map tablespace full_path: \"%s\" old_symlink_content: \"%s\" new_symlink_content: \"%s\"\n",
446448
source_full_path,
447449
symlink_content,
448450
linked_path);
@@ -457,15 +459,15 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
457459
elog(VERBOSE, "Create directory \"%s\" and symbolic link \"%s\"",
458460
linked_path, to_path);
459461

460-
//REVIEW Handle return value here.
461-
//We should not proceed if failed to create dir.
462462
/* create tablespace directory */
463-
fio_mkdir(linked_path, DIR_PERMISSION, FIO_BACKUP_HOST);
463+
if (fio_mkdir(linked_path, file->mode, FIO_BACKUP_HOST) != 0)
464+
elog(ERROR, "Could not create tablespace directory \"%s\": %s",
465+
linked_path, strerror(errno));
464466

465467
/* create link to linked_path */
466468
if (fio_symlink(linked_path, to_path, true, FIO_BACKUP_HOST) < 0)
467-
elog(ERROR, "Could not create symbolic link \"%s\": %s",
468-
to_path, strerror(errno));
469+
elog(ERROR, "Could not create symbolic link \"%s\" -> \"%s\": %s",
470+
linked_path, to_path, strerror(errno));
469471
}
470472
}
471473

@@ -475,7 +477,7 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
475477
*/
476478
{
477479
int control_file_elem_index;
478-
pgFile search_key ;
480+
pgFile search_key;
479481
MemSet(&search_key, 0, sizeof(pgFile));
480482
/* pgFileCompareRelPathWithExternal uses only .rel_path and .external_dir_num for comparision */
481483
search_key.rel_path = XLOG_CONTROL_FILE;
@@ -502,12 +504,13 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
502504

503505
//REVIEW Can we maybe optimize it and use some merge-like algorithm
504506
//instead of bsearch for each file? Of course it isn't an urgent fix.
507+
//REVIEW_ANSWER yes, merge will be better
505508
if (parray_bsearch(source_filelist, file, pgFileCompareRelPathWithExternal))
506509
redundant = false;
507510

508511
/* pg_filenode.map are always restored, because it's crc cannot be trusted */
509-
if (file->external_dir_num == 0 &&
510-
pg_strcasecmp(file->name, RELMAPPER_FILENAME) == 0)
512+
Assert(file->external_dir_num == 0);
513+
if (pg_strcasecmp(file->name, RELMAPPER_FILENAME) == 0)
511514
redundant = true;
512515

513516
//REVIEW This check seems unneded. Anyway we delete only redundant stuff below.
@@ -588,6 +591,7 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
588591
}
589592

590593
/* at last copy control file */
594+
if (catchup_isok)
591595
{
592596
char from_fullpath[MAXPGPATH];
593597
char to_fullpath[MAXPGPATH];
@@ -678,9 +682,8 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
678682
/* Cleanup */
679683
pg_free(query_text);
680684

681-
//REVIEW Please adjust the comment.
682-
/* In case of backup from replica >= 9.6 we must fix minRecPoint,
683-
* First we must find pg_control in source_filelist.
685+
/*
686+
* In case of backup from replica >= 9.6 we must fix minRecPoint
684687
*/
685688
if (current.from_replica && !exclusive_backup)
686689
{
@@ -712,19 +715,9 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
712715
continue;
713716

714717
/* construct fullpath */
715-
if (file->external_dir_num == 0)
716-
join_path_components(to_fullpath, dest_pgdata, file->rel_path);
717-
//REVIEW Let's clean this.
718-
/* TODO разобраться с external */
719-
/*else
720-
{
721-
char external_dst[MAXPGPATH];
718+
Assert(file->external_dir_num == 0);
719+
join_path_components(to_fullpath, dest_pgdata, file->rel_path);
722720

723-
makeExternalDirPathByNum(external_dst, external_prefix,
724-
file->external_dir_num);
725-
join_path_components(to_fullpath, external_dst, file->rel_path);
726-
}
727-
*/
728721
if (fio_sync(to_fullpath, FIO_BACKUP_HOST) != 0)
729722
elog(ERROR, "Cannot sync file \"%s\": %s", to_fullpath, strerror(errno));
730723
}
@@ -754,7 +747,7 @@ do_catchup_instance(const char *source_pgdata, const char *dest_pgdata, PGconn *
754747
}
755748

756749
/*
757-
* TODO: add description
750+
* Catchup file copier executed in separate threads
758751
*/
759752
static void *
760753
catchup_thread_runner(void *arg)
@@ -788,27 +781,9 @@ catchup_thread_runner(void *arg)
788781
i + 1, n_files, file->rel_path);
789782

790783
/* construct destination filepath */
791-
/* TODO разобраться нужен ли external */
792-
if (file->external_dir_num == 0)
793-
{
794-
join_path_components(from_fullpath, arguments->from_root, file->rel_path);
795-
join_path_components(to_fullpath, arguments->to_root, file->rel_path);
796-
}
797-
//REVIEW Let's clean this.
798-
/*else
799-
{
800-
char external_dst[MAXPGPATH];
801-
char *external_path = parray_get(arguments->external_dirs,
802-
file->external_dir_num - 1);
803-
804-
makeExternalDirPathByNum(external_dst,
805-
arguments->external_prefix,
806-
file->external_dir_num);
807-
808-
join_path_components(to_fullpath, external_dst, file->rel_path);
809-
join_path_components(from_fullpath, external_path, file->rel_path);
810-
}
811-
*/
784+
Assert(file->external_dir_num == 0);
785+
join_path_components(from_fullpath, arguments->from_root, file->rel_path);
786+
join_path_components(to_fullpath, arguments->to_root, file->rel_path);
812787

813788
/* Encountered some strange beast */
814789
if (!S_ISREG(file->mode))

src/stream.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,14 @@ stop_streaming(XLogRecPtr xlogpos, uint32 timeline, bool segment_finished)
308308

309309
/* we assume that we get called once at the end of each segment */
310310
if (segment_finished)
311-
{
312-
elog(VERBOSE, _("finished segment at %X/%X (timeline %u)"),
313-
(uint32) (xlogpos >> 32), (uint32) xlogpos, timeline);
311+
{
312+
elog(VERBOSE, _("finished segment at %X/%X (timeline %u)"),
313+
(uint32) (xlogpos >> 32), (uint32) xlogpos, timeline);
314314

315-
add_walsegment_to_filelist(xlog_files_list, timeline, xlogpos,
316-
(char*) stream_thread_arg.basedir,
317-
instance_config.xlog_seg_size);
318-
}
315+
add_walsegment_to_filelist(xlog_files_list, timeline, xlogpos,
316+
(char*) stream_thread_arg.basedir,
317+
instance_config.xlog_seg_size);
318+
}
319319

320320
/*
321321
* Note that we report the previous, not current, position here. After a

0 commit comments

Comments
 (0)