-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_follower_apply.c
568 lines (458 loc) · 13.7 KB
/
pg_follower_apply.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*-------------------------------------------------------------------------
*
* pg_follower_apply.c
*
* IDENTIFICATION
* pg_follower/pg_follower_apply.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "access/xlog.h"
#include "executor/spi.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "postmaster/bgworker.h"
#include "postmaster/interrupt.h"
#include "replication/walreceiver.h"
#include "storage/dsm_registry.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/lwlock.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
#include "utils/wait_event.h"
PG_FUNCTION_INFO_V1(start_follow);
PGDLLEXPORT void pg_follower_worker_main(Datum main_arg);
static bool start_bgworker(const char *connection_string);
static void pfw_init_shmem(void *ptr);
static void pfw_attach_shmem(bool require_found);
static void create_replication_slot(WalReceiverConn *conn);
static bool start_streaming(WalReceiverConn *conn);
static void apply_loop(WalReceiverConn *conn);
static void apply_message(StringInfo message);
static void send_feedback(WalReceiverConn *conn, XLogRecPtr recvpos, bool force,
bool requestReply);
static uint32 pg_follower_we_main = 0;
static MemoryContext message_context = NULL;
static MemoryContext pfw_worker_context = NULL;
/* Determine the max length for the connection string */
#define MAXCONNSTRING 1024
/* Determine name of used replication slot */
#define PFW_SLOT_NAME "pg_follower_tmp_slot"
/* Determine name of used plugin */
#define PFW_PLUGIN_NAME "pg_follower"
/* Shared state information for pg_follower bgworker. */
typedef struct
{
Oid local_database_oid;
char connection_string[MAXCONNSTRING];
} pg_follower_shared_state;
/* Pointer to shared-memory state. */
static pg_follower_shared_state *pfw_state;
/*
* Create a logical replication slot to the upstream node.
*
* walrcv_create_slot() macro cannot be used becasue it cannot create with an
* arbitrary logical decoding output plugin.
*/
static void
create_replication_slot(WalReceiverConn *conn)
{
#define CREATE_SLOT_OUTPUT_COL_COUNT 4
StringInfoData query;
Oid slot_row[CREATE_SLOT_OUTPUT_COL_COUNT] = {TEXTOID, TEXTOID,
TEXTOID, TEXTOID};
bool started_tx = false;
/* The syscache access in walrcv_exec() needs a transaction env. */
if (!IsTransactionState())
{
StartTransactionCommand();
started_tx = true;
}
/*
* Construct a query. Any options could not be accepted now. Also, only
* a temporary slot is supported.
*/
initStringInfo(&query);
appendStringInfo(&query, "CREATE_REPLICATION_SLOT %s TEMPORARY LOGICAL %s",
PFW_SLOT_NAME, PFW_PLUGIN_NAME);
/* Execute the query */
walrcv_exec(conn, query.data, CREATE_SLOT_OUTPUT_COL_COUNT, slot_row);
pfree(query.data);
if (started_tx)
CommitTransactionCommand();
}
/*
* Start streaming data from upstream.
*
* walrcv_startstreaming() macro cannot be used becasue it requires to specify
* the name of publications.
*/
static bool
start_streaming(WalReceiverConn *conn)
{
StringInfoData query;
bool started_tx;
/* The syscache access in walrcv_exec() needs a transaction env. */
if (!IsTransactionState())
{
StartTransactionCommand();
started_tx = true;
}
/*
* Construct a query. Any options could not be accepted now. Also, the
* startpoint is always set to 0/0.
*/
initStringInfo(&query);
appendStringInfo(&query, "START_REPLICATION SLOT %s LOGICAL 0/0 ;",
PFW_SLOT_NAME);
/*
* Execute the query. Since START_REPLICATION returns PGRES_COPY_BOTH
* response, no need to prepare nRetTypes and retTypes.
*/
walrcv_exec(conn, query.data, 0, NULL);
pfree(query.data);
if (started_tx)
CommitTransactionCommand();
return true;
}
/*
* Send a Standby Status Update message to server.
*
* 'recvpos' is the latest LSN we've received data to, force is set if we need
* to send a response to avoid timeouts.
*/
static void
send_feedback(WalReceiverConn *conn, XLogRecPtr recvpos, bool force,
bool requestReply)
{
static StringInfo reply_message = NULL;
static TimestampTz send_time = 0;
static XLogRecPtr last_recvpos = InvalidXLogRecPtr;
static XLogRecPtr last_writepos = InvalidXLogRecPtr;
static XLogRecPtr last_flushpos = InvalidXLogRecPtr;
/* XXX: Reply as all the WAL records has been flushed */
XLogRecPtr writepos = recvpos;
XLogRecPtr flushpos = recvpos;
TimestampTz now;
/*
* If the user doesn't want status to be reported to the publisher, be
* sure to exit before doing anything at all.
*/
if (!force && wal_receiver_status_interval <= 0)
return;
/* It's legal to not pass a recvpos */
if (recvpos < last_recvpos)
recvpos = last_recvpos;
if (writepos < last_writepos)
writepos = last_writepos;
if (flushpos < last_flushpos)
flushpos = last_flushpos;
now = GetCurrentTimestamp();
/* if we've already reported everything we're good */
if (!force &&
writepos == last_writepos &&
flushpos == last_flushpos &&
!TimestampDifferenceExceeds(send_time, now,
wal_receiver_status_interval * 1000))
return;
send_time = now;
if (!reply_message)
{
MemoryContext oldctx = MemoryContextSwitchTo(pfw_worker_context);
reply_message = makeStringInfo();
MemoryContextSwitchTo(oldctx);
}
else
resetStringInfo(reply_message);
pq_sendbyte(reply_message, 'r');
pq_sendint64(reply_message, recvpos); /* write */
pq_sendint64(reply_message, flushpos); /* flush */
pq_sendint64(reply_message, writepos); /* apply */
pq_sendint64(reply_message, now); /* sendTime */
pq_sendbyte(reply_message, requestReply); /* replyRequested */
elog(DEBUG2, "sending feedback (force %d) to recv %X/%X, write %X/%X, flush %X/%X",
force,
LSN_FORMAT_ARGS(recvpos),
LSN_FORMAT_ARGS(writepos),
LSN_FORMAT_ARGS(flushpos));
walrcv_send(conn,
reply_message->data, reply_message->len);
if (recvpos > last_recvpos)
last_recvpos = recvpos;
if (writepos > last_writepos)
last_writepos = writepos;
if (flushpos > last_flushpos)
last_flushpos = flushpos;
}
/*
* Read received message and apply via server programming interface
*/
static void
apply_message(StringInfo message)
{
const char *query = pq_getmsgbytes(message,
(message->len - message->cursor));
elog(DEBUG1, "received query: %s", query);
if (strncmp(query, "BEGIN", 5) == 0)
{
SetCurrentStatementStartTimestamp();
StartTransactionCommand();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
}
else if (strncmp(query, "CREATE", 5) == 0 ||
strncmp(query, "DROP", 4) == 0)
{
int ret;
ret = SPI_execute(query, false, 1);
if (ret != SPI_OK_UTILITY)
elog(ERROR, "failed to execute query content: \"%s\" length:%d", query, ret);
}
else if (strncmp(query, "COMMIT", 6) == 0)
{
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
}
/* Seems normal DML commands or TRUNCATE. Use the given string as-is. */
else
{
int ret;
ret = SPI_execute(query, false, 1);
if (ret < 0)
elog(ERROR, "failed to execute query :%s :%d", query, ret);
}
}
/*
* main loop for the pg_follower worker
*
* XXX: basically ported from LogicalRepApplyLoop()
*/
static void
apply_loop(WalReceiverConn *conn)
{
XLogRecPtr last_received = InvalidXLogRecPtr;
TimeLineID tli;
/* Init the message_context which we clean up after each message */
message_context = AllocSetContextCreate(pfw_worker_context,
"pfw_worker_context",
ALLOCSET_DEFAULT_SIZES);
for (;;)
{
pgsocket fd = PGINVALID_SOCKET;
int rc;
int len;
char *buf = NULL;
bool endofstream = false;
CHECK_FOR_INTERRUPTS();
MemoryContextSwitchTo(message_context);
len = walrcv_receive(conn, &buf, &fd);
if (len != 0)
{
/* Loop to process all available data (without blocking). */
for (;;)
{
CHECK_FOR_INTERRUPTS();
if (len == 0)
{
break;
}
else if (len < 0)
{
ereport(LOG,
(errmsg("data stream from publisher has ended")));
endofstream = true;
break;
}
else
{
int c;
StringInfoData s;
if (ConfigReloadPending)
{
ConfigReloadPending = false;
ProcessConfigFile(PGC_SIGHUP);
}
/* Ensure we are reading the data into our memory context. */
MemoryContextSwitchTo(message_context);
initReadOnlyStringInfo(&s, buf, len);
c = pq_getmsgbyte(&s);
if (c == 'w')
{
XLogRecPtr start_lsn;
XLogRecPtr end_lsn;
start_lsn = pq_getmsgint64(&s);
end_lsn = pq_getmsgint64(&s);
/* Timestamp is not used now */
(void) pq_getmsgint64(&s);
if (last_received < start_lsn)
last_received = start_lsn;
if (last_received < end_lsn)
last_received = end_lsn;
apply_message(&s);
}
else if (c == 'k')
{
XLogRecPtr end_lsn;
bool reply_requested;
end_lsn = pq_getmsgint64(&s);
/* Timestamp is not used now */
(void) pq_getmsgint64(&s);
reply_requested = pq_getmsgbyte(&s);
if (last_received < end_lsn)
last_received = end_lsn;
send_feedback(conn, last_received, reply_requested, false);
}
/* other message types are purposefully ignored */
MemoryContextReset(message_context);
}
len = walrcv_receive(conn, &buf, &fd);
}
}
send_feedback(conn, last_received, false, false);
/* Cleanup the memory. */
MemoryContextReset(message_context);
MemoryContextSwitchTo(pfw_worker_context);
/* Check if we need to exit the streaming loop. */
if (endofstream)
break;
/* Sleep 1s or until new message would come */
rc = WaitLatchOrSocket(MyLatch,
WL_SOCKET_READABLE | WL_LATCH_SET |
WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
fd, 1000L,
pg_follower_we_main);
if (rc & WL_LATCH_SET)
{
ResetLatch(MyLatch);
CHECK_FOR_INTERRUPTS();
}
if (ConfigReloadPending)
{
ConfigReloadPending = false;
ProcessConfigFile(PGC_SIGHUP);
}
/* We won't do timeout */
}
walrcv_endstreaming(conn, &tli);
}
/*
* Entrypoint for pg_follower worker
*/
void
pg_follower_worker_main(Datum main_arg)
{
Oid database_oid;
char *connection_string;
WalReceiverConn *pfw_walrcv_conn = NULL;
char *err;
/* Setup signal handlers */
pqsignal(SIGHUP, SignalHandlerForConfigReload);
pqsignal(SIGTERM, die);
BackgroundWorkerUnblockSignals();
/* Determine a memory context which is mainly used */
pfw_worker_context = AllocSetContextCreate(TopMemoryContext,
"pfw_worker_context",
ALLOCSET_DEFAULT_SIZES);
MemoryContextSwitchTo(pfw_worker_context);
/* Attach the shared memory */
pfw_attach_shmem(true);
/* And accept information */
database_oid = pfw_state->local_database_oid;
connection_string = pstrdup(pfw_state->connection_string);
/* Allocate or get the custom wait event */
if (pg_follower_we_main == 0)
pg_follower_we_main = WaitEventExtensionNew("PgFollowerWorkerMain");
/* Connect to a local database */
BackgroundWorkerInitializeConnectionByOid(database_oid, InvalidOid, 0);
/* Load the libpq-specific functions */
load_file("libpqwalreceiver", false);
/* Connect to the upstream */
pfw_walrcv_conn = walrcv_connect(connection_string, true, true, false,
"pg_follower worker", &err);
if (pfw_walrcv_conn == NULL)
elog(ERROR, "bad connection");
pfree(connection_string);
/* Create a replication slot */
create_replication_slot(pfw_walrcv_conn);
/* Start streaming */
start_streaming(pfw_walrcv_conn);
/* RUn main loop */
apply_loop(pfw_walrcv_conn);
walrcv_disconnect(pfw_walrcv_conn);
}
/*
* An implentation for init_callback callback
*/
static void
pfw_init_shmem(void *ptr)
{
pg_follower_shared_state *handler = (pg_follower_shared_state *) ptr;
handler->local_database_oid = InvalidOid;
memset(handler->connection_string, 0, MAXCONNSTRING);
}
/*
* Attach or initialize a shared memory segment
*/
static void
pfw_attach_shmem(bool require_found)
{
bool found = false;
pfw_state = GetNamedDSMSegment("pg_follower",
sizeof(pg_follower_shared_state),
pfw_init_shmem,
&found);
if (require_found && !found)
elog(ERROR, "caller requires to attach the allocated memory, but not found");
}
/*
* Kick a new background worker
*/
static bool
start_bgworker(const char *connection_string)
{
BackgroundWorker worker;
BackgroundWorkerHandle *handle;
BgwHandleStatus status;
pid_t pid;
/* Set worker-specific data */
MemSet(&worker, 0, sizeof(BackgroundWorker));
strcpy(worker.bgw_name, "pg_follower worker");
strcpy(worker.bgw_type, "pg_follower worker");
worker.bgw_flags = BGWORKER_SHMEM_ACCESS |
BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
worker.bgw_restart_time = BGW_NEVER_RESTART;
strcpy(worker.bgw_library_name, "pg_follower");
strcpy(worker.bgw_function_name, "pg_follower_worker_main");
worker.bgw_main_arg = (Datum) 0;
/* must set notify PID to wait for startup */
worker.bgw_notify_pid = MyProcPid;
/* Dinamically allocate a shared memory */
pfw_attach_shmem(false);
/* Fill shared-memory data structure for passing info to the worker */
pfw_state->local_database_oid = MyDatabaseId;
strncpy(pfw_state->connection_string, connection_string, MAXCONNSTRING);
if (!RegisterDynamicBackgroundWorker(&worker, &handle))
elog(ERROR, "could not register background process");
status = WaitForBackgroundWorkerStartup(handle, &pid);
if (status != BGWH_STARTED)
elog(ERROR, "could not start background process");
return true;
}
Datum
start_follow(PG_FUNCTION_ARGS)
{
char *connection_string;
if (RecoveryInProgress())
elog(ERROR, "recovery is in progress");
connection_string = text_to_cstring(PG_GETARG_TEXT_PP(0));
start_bgworker(connection_string);
PG_RETURN_VOID();
}