8000 Update pgcopydb sentinel's replay_lsn asynchronously. by dimitri · Pull Request #267 · dimitri/pgcopydb · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Update pgcopydb sentinel's replay_lsn asynchronously. #267

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

Merged
merged 2 commits into from
Apr 28, 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
77 changes: 77 additions & 0 deletions src/bin/pgcopydb/ld_apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,83 @@ stream_apply_sync_sentinel(StreamApplyContext *context)
}


/*
* stream_apply_send_sync_sentinel sends a query to sync with the pgcopydb
* sentinel table using the libpq async API. Use the associated function
* stream_apply_fetch_sync_sentinel to check for availability of the result and
* fetch it when it's available.
*/
bool
stream_apply_send_sync_sentinel(StreamApplyContext *context)
{
PGSQL *src = &(context->src);

if (src->connectionStatementType != PGSQL_CONNECTION_MULTI_STATEMENT)
{
log_error("BUG: stream_apply_send_sync_sentinel called "
"in SINGLE statement mode");
return false;
}

if (context->sentinelQueryInProgress)
{
log_error("BUG: stream_apply_send_sync_sentinel already in progress");
return false;
}

if (!pgsql_init(src, context->source_pguri, PGSQL_CONN_SOURCE))
{
/* errors have already been logged */
return false;
}

/* limit the amount of logging of the apply process */
src->logSQL = true;

if (!pgsql_send_sync_sentinel_apply(src, context->previousLSN))
{
log_error("Failed to sync progress with the pgcopydb sentinel");
return false;
}

context->sentinelSyncTime = time(NULL);
context->sentinelQueryInProgress = true;

return true;
}


/*
* stream_apply_fetch_sync_sentinel checks to see if the result of the
* pgcopydb sentinel sync query is available and fetches it then.
*/
bool
stream_apply_fetch_sync_sentinel(StreamApplyContext *context)
{
PGSQL *src = &(context->src);

bool retry;
CopyDBSentinel sentinel = { 0 };

if (!pgsql_fetch_sync_sentinel_apply(src, &retry, &sentinel))
{
log_error("Failed to fetch sentinel update query results");
return false;
}

if (!retry)
{
context->sentinelQueryInProgress = false;

context->apply = sentinel.apply;
context->endpos = sentinel.endpos;
context->startpos = sentinel.startpos;
}

return true;
}


/*
* stream_apply_file connects to the target database system and applies the
* given SQL file as prepared by the stream_transform_file function.
Expand Down
37 changes: 35 additions & 2 deletions src/bin/pgcopydb/ld_replay.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,17 @@ stream_apply_replay(StreamSpecs *specs)
/* we might still have to disconnect now */
(void) pgsql_finish(&(context->pgsql));

log_notice("Replayed %lld messages", (long long) readerContext.lineno);
/* make sure to send a last round of sentinel update before exit */
if (!stream_apply_sync_sentinel(context))
{
log_error("Failed to update pgcopydb.sentinel replay_lsn to %X/%X",
LSN_FORMAT_ARGS(context->replay_lsn));
return false;
}

log_notice("Replayed %lld messages up to replay_lsn %X/%X",
(long long) readerContext.lineno,
LSN_FORMAT_ARGS(context->replay_lsn));

return true;
}
Expand Down Expand Up @@ -156,7 +166,30 @@ stream_replay_line(void *ctx, const char *line, bool *stop)
case STREAM_ACTION_COMMIT:
case STREAM_ACTION_KEEPALIVE:
{
(void) stream_apply_sync_sentinel(context);
uint64_t now = time(NULL);

if (context->sentinelQueryInProgress)
{
if (!stream_apply_fetch_sync_sentinel(context))
{
/* errors have already been logged */
return false;
}
}

/* rate limit to 1 update per second */
else if (1 < (now - context->sentinelSyncTime))
{
/* we're going to keep the connection around */
context->src.connectionStatementType =
PGSQL_CONNECTION_MULTI_STATEMENT;

if (!stream_apply_send_sync_sentinel(context))
{
/* errors have already been logged */
return false;
}
}
break;
}

Expand Down
9 changes: 9 additions & 0 deletions src/bin/pgcopydb/ld_stream.h
888E
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ typedef struct StreamApplyContext
{
CDCPaths paths;

/* target connection */
PGSQL pgsql;

/* source connection to publish sentinel updates */
PGSQL src;
bool sentinelQueryInProgress;
uint64_t sentinelSyncTime;

char source_pguri[MAXCONNINFO];
char target_pguri[MAXCONNINFO];
char origin[BUFSIZE];
Expand Down Expand Up @@ -531,6 +538,8 @@ bool stream_apply_wait_for_sentinel(StreamSpecs *specs,
StreamApplyContext *context);

bool stream_apply_sync_sentinel(StreamApplyContext *context);
bool stream_apply_send_sync_sentinel(StreamApplyContext *context);
bool stream_apply_fetch_sync_sentinel(StreamApplyContext *context);

bool stream_apply_file(StreamApplyContext *context);

Expand Down
Loading
0