Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assorted fixes from review, including stream_read_context retry loop. #423

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bin/pgcopydb/file_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ read_from_stream(FILE *stream, ReadFromStreamContext *context)
if (asked_to_stop || asked_to_stop_fast || asked_to_quit)
{
doneReading = true;
log_info("read_from_stream was asked to stop or quit");
log_notice("read_from_stream was asked to stop or quit");
}

continue;
Expand Down
72 changes: 50 additions & 22 deletions src/bin/pgcopydb/ld_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,9 @@ stream_read_context(CDCPaths *paths,
uint32_t *WalSegSz)
{
char *wal_segment_size = NULL;
char *tli = NULL;
char *history = NULL;

long size = 0L;

/*
Expand All @@ -2600,7 +2603,7 @@ stream_read_context(CDCPaths *paths,
ConnectionRetryPolicy retryPolicy = { 0 };

int maxT = 10; /* 10s */
int maxSleepTime = 1000; /* 1s */
int maxSleepTime = 1500; /* 1.5s */
int baseSleepTime = 100; /* 100ms */

(void) pgsql_set_retry_policy(&retryPolicy,
Expand All @@ -2611,12 +2614,35 @@ stream_read_context(CDCPaths *paths,

while (!pgsql_retry_policy_expired(&retryPolicy))
{
if (asked_to_stop || asked_to_stop_fast || asked_to_quit)
{
return false;
}

if (file_exists(paths->walsegsizefile) &&
file_exists(paths->tlifile) &&
file_exists(paths->tlihistfile))
{
/* success: break out of the retry loop */
break;
/*
* File did exist, but might have been deleted now (race condition
* at prefetch and transform processes start-up).
*/
bool success = true;

success = success &&
read_file(paths->walsegsizefile, &wal_segment_size, &size);

success = success &&
read_file(paths->tlifile, &tli, &size);

success = success &&
read_file(paths->tlihistfile, &history, &size);

if (success)
{
/* success: break out of the retry loop */
break;
}
}

int sleepTimeMs =
Expand All @@ -2630,46 +2656,48 @@ stream_read_context(CDCPaths *paths,
(void) pg_usleep(sleepTimeMs * 1000);
}

/* we don't want to retry anymore, error out if files still don't exist */
if (!read_file(paths->walsegsizefile, &wal_segment_size, &size))
/* did retry policy expire before the files are created? */
if (!(file_exists(paths->walsegsizefile) &&
file_exists(paths->tlifile) &&
file_exists(paths->tlihistfile)))
{
/* errors have already been logged */
log_error("Failed to read stream context file: retry policy expired");
return false;
}

/*
* Now that we could read the file contents, parse it.
*/
if (!stringToUInt(wal_segment_size, WalSegSz))
{
/* errors have already been logged */
return false;
}

char *tli;

if (!read_file(paths->tlifile, &tli, &size))
{
/* errors have already been logged */
free(wal_segment_size);
free(tli);
free(history);
return false;
}

if (!stringToUInt(tli, &(system->timeline)))
{
/* errors have already been logged */
return false;
}

char *history = NULL;

if (!read_file(paths->tlihistfile, &history, &size))
{
/* errors have already been logged */
free(wal_segment_size);
free(tli);
free(history);
return false;
}

if (!parseTimeLineHistory(paths->tlihistfile, history, system))
{
/* errors have already been logged */
free(wal_segment_size);
free(tli);
free(history);
return false;
}

free(wal_segment_size);
free(tli);
free(history);

return true;
}
8 changes: 6 additions & 2 deletions src/bin/pgcopydb/ld_transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,6 @@ stream_transform_rotate(StreamContext *privateContext)
bool
stream_transform_worker(StreamSpecs *specs)
{
log_notice("Started Stream Transform worker %d [%d]", getpid(), getppid());

/*
* The timeline and wal segment size are determined when connecting to the
* source database, and stored to local files at that time. When the Stream
Expand All @@ -488,6 +486,12 @@ stream_transform_worker(StreamSpecs *specs)
*/
if (!stream_read_context(&(specs->paths), &(specs->system), &(specs->WalSegSz)))
{
if (asked_to_stop || asked_to_stop_fast || asked_to_quit)
{
log_debug("Stream Transform Worker startup was interrupted");
return true;
}

log_error("Failed to read the streaming context information "
"from the source database, see above for details");
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/bin/pgcopydb/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,8 @@ pgsql_execute_with_params(PGSQL *pgsql, const char *sql, int paramCount,

if (pgsql->logSQL)
{
log_sql("[%s %d] %s;", endpoint, PQbackendPID(connection), sql);

debugParameters = createPQExpBuffer();

if (!build_parameters_list(debugParameters, paramCount, paramValues))
Expand All @@ -1492,8 +1494,6 @@ pgsql_execute_with_params(PGSQL *pgsql, const char *sql, int paramCount,
return false;
}

log_sql("[%s %d] %s;", endpoint, PQbackendPID(connection), sql);

if (paramCount > 0)
{
log_sql("[%s %d] %s",
Expand Down
Loading