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
|
/*-------------------------------------------------------------------------
*
* barrier.c
*
* Barrier handling for PITR
*
*
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 2010-2012 Postgres-XC Development Group
*
* IDENTIFICATION
* $$
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/gtm.h"
#include "access/xlog_internal.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "pgxc/barrier.h"
#include "pgxc/execRemote.h"
#include "pgxc/locator.h"
#include "pgxc/pgxc.h"
#include "nodes/nodes.h"
#include "pgxc/pgxcnode.h"
#include "storage/lwlock.h"
#include "tcop/dest.h"
static const char *generate_barrier_id(const char *id);
static PGXCNodeAllHandles *PrepareBarrier(const char *id);
static void ExecuteBarrier(const char *id);
static void EndBarrier(PGXCNodeAllHandles *handles, const char *id);
/*
* Prepare ourselves for an incoming BARRIER. We must disable all new 2PC
* commits and let the ongoing commits to finish. We then remember the
* barrier id (so that it can be matched with the final END message) and
* tell the driving Coordinator to proceed with the next step.
*
* A simple way to implement this is to grab a lock in an exclusive mode
* while all other backend starting a 2PC will grab the lock in shared
* mode. So as long as we hold the exclusive lock, no other backend start a
* new 2PC and there can not be any 2PC in-progress. This technique would
* rely on assumption that an exclusive lock requester is not starved by
* share lock requesters.
*
* Note: To ensure that the 2PC are not blocked for a long time, we should
* set a timeout. The lock should be release after the timeout and the
* barrier should be canceled.
*/
void
ProcessCreateBarrierPrepare(const char *id)
{
StringInfoData buf;
if (!IS_PGXC_REMOTE_COORDINATOR)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("The CREATE BARRIER PREPARE message is expected to "
"arrive at a Coordinator from another Coordinator")));
LWLockAcquire(BarrierLock, LW_EXCLUSIVE);
pq_beginmessage(&buf, 'b');
pq_sendstring(&buf, id);
pq_endmessage(&buf);
pq_flush();
/*
* TODO Start a timer to terminate the pending barrier after a specified
* timeout
*/
}
/*
* Mark the completion of an on-going barrier. We must have remembered the
* barrier ID when we received the CREATE BARRIER PREPARE command
*/
void
ProcessCreateBarrierEnd(const char *id)
{
StringInfoData buf;
if (!IS_PGXC_REMOTE_COORDINATOR)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("The CREATE BARRIER END message is expected to "
"arrive at a Coordinator from another Coordinator")));
LWLockRelease(BarrierLock);
pq_beginmessage(&buf, 'b');
pq_sendstring(&buf, id);
pq_endmessage(&buf);
pq_flush();
/*
* TODO Stop the timer
*/
}
/*
* Execute the CREATE BARRIER command. Write a BARRIER WAL record and flush the
* WAL buffers to disk before returning to the caller. Writing the WAL record
* does not guarantee successful completion of the barrier command.
*/
void
ProcessCreateBarrierExecute(const char *id)
{
StringInfoData buf;
if (!IsConnFromCoord())
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("The CREATE BARRIER EXECUTE message is expected to "
"arrive from a Coordinator")));
{
XLogRecPtr recptr;
XLogBeginInsert();
XLogRegisterData((char *) id, strlen(id) + 1);
recptr = XLogInsert(RM_BARRIER_ID, XLOG_BARRIER_CREATE);
XLogFlush(recptr);
}
pq_beginmessage(&buf, 'b');
pq_sendstring(&buf, id);
pq_endmessage(&buf);
pq_flush();
}
static const char *
generate_barrier_id(const char *id)
{
char genid[1024];
TimestampTz ts;
/*
* If the caller can passed a NULL value, generate an id which is
* guaranteed to be unique across the cluster. We use a combination of
* the Coordinator node id and current timestamp.
*/
if (id)
return id;
ts = GetCurrentTimestamp();
#ifdef HAVE_INT64_TIMESTAMP
sprintf(genid, "%s_"INT64_FORMAT, PGXCNodeName, ts);
#else
sprintf(genid, "%s_%.0f", PGXCNodeName, ts);
#endif
return pstrdup(genid);
}
static PGXCNodeAllHandles *
SendBarrierPrepareRequest(List *coords, const char *id)
{
PGXCNodeAllHandles *coord_handles;
int conn;
int msglen;
int barrier_idlen;
coord_handles = get_handles(NIL, coords, true, true);
for (conn = 0; conn < coord_handles->co_conn_count; conn++)
{
PGXCNodeHandle *handle = coord_handles->coord_handles[conn];
/* Invalid connection state, return error */
if (handle->state != DN_CONNECTION_STATE_IDLE)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("Failed to send CREATE BARRIER PREPARE request "
"to the node")));
barrier_idlen = strlen(id) + 1;
msglen = 4; /* for the length itself */
msglen += barrier_idlen;
msglen += 1; /* for barrier command itself */
/* msgType + msgLen */
if (ensure_out_buffer_capacity(handle->outEnd + 1 + msglen, handle) != 0)
{
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("Out of memory")));
}
handle->outBuffer[handle->outEnd++] = 'b';
msglen = htonl(msglen);
memcpy(handle->outBuffer + handle->outEnd, &msglen, 4);
handle->outEnd += 4;
handle->outBuffer[handle->outEnd++] = CREATE_BARRIER_PREPARE;
memcpy(handle->outBuffer + handle->outEnd, id, barrier_idlen);
handle->outEnd += barrier_idlen;
PGXCNodeSetConnectionState(handle, DN_CONNECTION_STATE_QUERY);
pgxc_node_flush(handle);
}
return coord_handles;
}
static void
CheckBarrierCommandStatus(PGXCNodeAllHandles *conn_handles, const char *id,
const char *command)
{
int conn;
int count = conn_handles->co_conn_count + conn_handles->dn_conn_count;
elog(DEBUG2, "Check CREATE BARRIER <%s> %s command status", id, command);
for (conn = 0; conn < count; conn++)
{
PGXCNodeHandle *handle;
if (conn < conn_handles->co_conn_count)
handle = conn_handles->coord_handles[conn];
else
handle = conn_handles->datanode_handles[conn - conn_handles->co_conn_count];
if (pgxc_node_receive(1, &handle, NULL))
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("Failed to receive response from the remote side")));
if (handle_response(handle, NULL) != RESPONSE_BARRIER_OK)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("CREATE BARRIER PREPARE command failed "
"with error %s", handle->error)));
}
elog(DEBUG2, "Successfully completed CREATE BARRIER <%s> %s command on "
"all nodes", id, command);
}
static void
SendBarrierEndRequest(PGXCNodeAllHandles *coord_handles, const char *id)
{
int conn;
int msglen;
int barrier_idlen;
elog(DEBUG2, "Sending CREATE BARRIER <%s> END command to all Coordinators", id);
for (conn = 0; conn < coord_handles->co_conn_count; conn++)
{
PGXCNodeHandle *handle = coord_handles->coord_handles[conn];
/* Invalid connection state, return error */
if (handle->state != DN_CONNECTION_STATE_IDLE)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("Failed to send CREATE BARRIER PREPARE request "
"to the node")));
barrier_idlen = strlen(id) + 1;
msglen = 4; /* for the length itself */
msglen += barrier_idlen;
msglen += 1; /* for barrier command itself */
/* msgType + msgLen */
if (ensure_out_buffer_capacity(handle->outEnd + 1 + msglen, handle) != 0)
{
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("Out of memory")));
}
handle->outBuffer[handle->outEnd++] = 'b';
msglen = htonl(msglen);
memcpy(handle->outBuffer + handle->outEnd, &msglen, 4);
handle->outEnd += 4;
handle->outBuffer[handle->outEnd++] = CREATE_BARRIER_END;
memcpy(handle->outBuffer + handle->outEnd, id, barrier_idlen);
handle->outEnd += barrier_idlen;
PGXCNodeSetConnectionState(handle, DN_CONNECTION_STATE_QUERY);
pgxc_node_flush(handle);
}
}
/*
* Prepare all Coordinators for barrier. During this step all the Coordinators
* are informed to suspend any new 2PC transactions. The Coordinators should
* disable new 2PC transactions and then wait for the existing transactions to
* complete. Once all "in-flight" 2PC transactions are over, the Coordinators
* respond back.
*
* That completes the first step in barrier generation
*
* Any errors will be reported via ereport.
*/
static PGXCNodeAllHandles *
PrepareBarrier(const char *id)
{
PGXCNodeAllHandles *coord_handles;
elog(DEBUG2, "Preparing Coordinators for BARRIER");
/*
* Send a CREATE BARRIER PREPARE message to all the Coordinators. We should
* send an asynchronous request so that we can disable local commits and
* then wait for the remote Coordinators to finish the work
*/
coord_handles = SendBarrierPrepareRequest(GetAllCoordNodes(), id);
/*
* Disable local commits
*/
LWLockAcquire(BarrierLock, LW_EXCLUSIVE);
elog(DEBUG2, "Disabled 2PC commits originating at the driving Coordinator");
/*
* TODO Start a timer to cancel the barrier request in case of a timeout
*/
/*
* Local in-flight commits are now over. Check status of the remote
* Coordinators
*/
CheckBarrierCommandStatus(coord_handles, id, "PREPARE");
return coord_handles;
}
/*
* Execute the barrier command on all the components, including Datanodes and
* Coordinators.
*/
static void
ExecuteBarrier(const char *id)
{
List *barrierDataNodeList = GetAllDataNodes();
List *barrierCoordList = GetAllCoordNodes();
PGXCNodeAllHandles *conn_handles;
int conn;
int msglen;
int barrier_idlen;
conn_handles = get_handles(barrierDataNodeList, barrierCoordList, false, true);
elog(DEBUG2, "Sending CREATE BARRIER <%s> EXECUTE message to "
"Datanodes and Coordinator", id);
/*
* Send a CREATE BARRIER request to all the Datanodes and the Coordinators
*/
for (conn = 0; conn < conn_handles->co_conn_count + conn_handles->dn_conn_count; conn++)
{
PGXCNodeHandle *handle;
if (conn < conn_handles->co_conn_count)
handle = conn_handles->coord_handles[conn];
else
handle = conn_handles->datanode_handles[conn - conn_handles->co_conn_count];
/* Invalid connection state, return error */
if (handle->state != DN_CONNECTION_STATE_IDLE)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("Failed to send CREATE BARRIER EXECUTE request "
"to the node")));
barrier_idlen = strlen(id) + 1;
msglen = 4; /* for the length itself */
msglen += barrier_idlen;
msglen += 1; /* for barrier command itself */
/* msgType + msgLen */
if (ensure_out_buffer_capacity(handle->outEnd + 1 + msglen, handle) != 0)
{
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("Out of memory")));
}
handle->outBuffer[handle->outEnd++] = 'b';
msglen = htonl(msglen);
memcpy(handle->outBuffer + handle->outEnd, &msglen, 4);
handle->outEnd += 4;
handle->outBuffer[handle->outEnd++] = CREATE_BARRIER_EXECUTE;
memcpy(handle->outBuffer + handle->outEnd, id, barrier_idlen);
handle->outEnd += barrier_idlen;
PGXCNodeSetConnectionState(handle, DN_CONNECTION_STATE_QUERY);
pgxc_node_flush(handle);
}
CheckBarrierCommandStatus(conn_handles, id, "EXECUTE");
pfree_pgxc_all_handles(conn_handles);
/*
* Also WAL log the BARRIER locally and flush the WAL buffers to disk
*/
{
XLogRecPtr recptr;
XLogBeginInsert();
XLogRegisterData((char *) id, strlen(id) + 1);
recptr = XLogInsert(RM_BARRIER_ID, XLOG_BARRIER_CREATE);
XLogFlush(recptr);
}
}
/*
* Resume 2PC commits on the local as well as remote Coordinators.
*/
static void
EndBarrier(PGXCNodeAllHandles *prepared_handles, const char *id)
{
/* Resume 2PC locally */
LWLockRelease(BarrierLock);
SendBarrierEndRequest(prepared_handles, id);
CheckBarrierCommandStatus(prepared_handles, id, "END");
}
void
RequestBarrier(const char *id, char *completionTag)
{
PGXCNodeAllHandles *prepared_handles;
const char *barrier_id;
elog(DEBUG2, "CREATE BARRIER request received");
/*
* Ensure that we are a Coordinator and the request is not from another
* coordinator
*/
if (!IS_PGXC_COORDINATOR)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("CREATE BARRIER command must be sent to a Coordinator")));
if (IsConnFromCoord())
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("CREATE BARRIER command is not expected from another Coordinator")));
/*
* Get a barrier id if the user has not supplied it
*/
barrier_id = generate_barrier_id(id);
elog(DEBUG2, "CREATE BARRIER <%s>", barrier_id);
/*
* Step One. Prepare all Coordinators for upcoming barrier request
*/
prepared_handles = PrepareBarrier(barrier_id);
/*
* Step two. Issue BARRIER command to all involved components, including
* Coordinators and Datanodes
*/
ExecuteBarrier(barrier_id);
/*
* Step three. Inform Coordinators about a successfully completed barrier
*/
EndBarrier(prepared_handles, barrier_id);
/* Finally report the barrier to GTM to backup its restart point */
ReportBarrierGTM(barrier_id);
/* Free the handles */
pfree_pgxc_all_handles(prepared_handles);
if (completionTag)
snprintf(completionTag, COMPLETION_TAG_BUFSIZE, "BARRIER %s", barrier_id);
}
void
barrier_redo(XLogReaderState *record)
{
/* Nothing to do */
return;
}
|