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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
|
/*-------------------------------------------------------------------------
*
* fe-protocol3.c
* functions that are specific to frontend/backend protocol version 3
*
* Portions Copyright (c) 2012-2014, TransLattice, Inc.
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
* Portions Copyright (c) 2010-2012 Postgres-XC Development Group
*
*
* IDENTIFICATION
* $PostgreSQL$
*
*-------------------------------------------------------------------------
*/
#include "gtm/gtm_c.h"
#include <ctype.h>
#include <fcntl.h>
#include "gtm/libpq-fe.h"
#include "gtm/libpq-int.h"
#include "gtm/gtm_seq.h"
#include "gtm/gtm_client.h"
#include "gtm/gtm_serialize.h"
#include "gtm/register.h"
#include <unistd.h>
#include <netinet/in.h>
/*
* This macro lists the backend message types that could be "long" (more
* than a couple of kilobytes).
*/
#define VALID_LONG_MESSAGE_TYPE(id) \
((id) == 'S' || (id) == 'E')
static void handleSyncLoss(GTM_Conn *conn, char id, int msgLength);
static GTM_Result *pqParseInput(GTM_Conn *conn);
static int gtmpqParseSuccess(GTM_Conn *conn, GTM_Result *result);
static int gtmpqReadSeqKey(GTM_SequenceKey seqkey, GTM_Conn *conn);
/*
* parseInput: if appropriate, parse input data from backend
* until input is exhausted or a stopping state is reached.
* Note that this function will NOT attempt to read more data from the backend.
*/
static GTM_Result *
pqParseInput(GTM_Conn *conn)
{
char id;
int msgLength;
int avail;
GTM_Result *result = NULL;
if (conn->result == NULL)
{
conn->result = (GTM_Result *) malloc(sizeof (GTM_Result));
memset(conn->result, 0, sizeof (GTM_Result));
}
else
gtmpqFreeResultData(conn->result, conn->remote_type);
result = conn->result;
/*
* Try to read a message. First get the type code and length. Return
* if not enough data.
*/
conn->inCursor = conn->inStart;
if (gtmpqGetc(&id, conn))
return NULL;
if (gtmpqGetInt(&msgLength, 4, conn))
return NULL;
/*
* Try to validate message type/length here. A length less than 4 is
* definitely broken. Large lengths should only be believed for a few
* message types.
*/
if (msgLength < 4)
{
handleSyncLoss(conn, id, msgLength);
return NULL;
}
if (msgLength > 30000 && !VALID_LONG_MESSAGE_TYPE(id))
{
handleSyncLoss(conn, id, msgLength);
return NULL;
}
/*
* Can't process if message body isn't all here yet.
*/
conn->result->gr_msglen = msgLength -= 4;
avail = conn->inEnd - conn->inCursor;
if (avail < msgLength)
{
/*
* Before returning, enlarge the input buffer if needed to hold
* the whole message. This is better than leaving it to
* gtmpqReadData because we can avoid multiple cycles of realloc()
* when the message is large; also, we can implement a reasonable
* recovery strategy if we are unable to make the buffer big
* enough.
*/
if (gtmpqCheckInBufferSpace(conn->inCursor + (size_t) msgLength,
conn))
{
/*
* XXX add some better recovery code... plan is to skip over
* the message using its length, then report an error. For the
* moment, just treat this like loss of sync (which indeed it
* might be!)
*/
handleSyncLoss(conn, id, msgLength);
}
return NULL;
}
switch (id)
{
case 'S': /* command complete */
if (gtmpqParseSuccess(conn, result))
return NULL;
break;
case 'E': /* error return */
if (gtmpqGetError(conn, result))
return NULL;
result->gr_status = GTM_RESULT_ERROR;
break;
default:
printfGTMPQExpBuffer(&conn->errorMessage,
"unexpected response from server; first received character was \"%c\"\n",
id);
conn->inCursor += msgLength;
break;
} /* switch on protocol character */
/* Successfully consumed this message */
if (conn->inCursor == conn->inStart + 5 + msgLength)
{
/* Normal case: parsing agrees with specified length */
conn->inStart = conn->inCursor;
}
else
{
/* Trouble --- report it */
printfGTMPQExpBuffer(&conn->errorMessage,
"message contents do not agree with length in message type \"%c\"\n",
id);
/* trust the specified message length as what to skip */
conn->inStart += 5 + msgLength;
}
return result;
}
/*
* handleSyncLoss: clean up after loss of message-boundary sync
*
* There isn't really a lot we can do here except abandon the connection.
*/
static void
handleSyncLoss(GTM_Conn *conn, char id, int msgLength)
{
printfGTMPQExpBuffer(&conn->errorMessage,
"lost synchronization with server: got message type \"%c\", length %d\n",
id, msgLength);
close(conn->sock);
conn->sock = -1;
conn->status = CONNECTION_BAD; /* No more connection to backend */
}
/*
* Attempt to read an Error or Notice response message.
* This is possible in several places, so we break it out as a subroutine.
* Entry: 'E' message type and length have already been consumed.
* Exit: returns 0 if successfully consumed message.
* returns EOF if not enough data.
*/
int
gtmpqGetError(GTM_Conn *conn, GTM_Result *result)
{
char id;
/*
* If we are a GTM proxy, expect an additional proxy header in the incoming
* message.
*/
if (conn->remote_type == GTM_NODE_GTM_PROXY)
{
if (gtmpqGetnchar((char *)&result->gr_proxyhdr,
sizeof (GTM_ProxyMsgHeader), conn))
return 1;
result->gr_msglen -= sizeof (GTM_ProxyMsgHeader);
/*
* If the allocated buffer is not large enough to hold the proxied
* data, realloc the buffer.
*
* Since the client side code is shared between the proxy and the
* backend, we don't want any memory context management etc here. So
* just use plain realloc. Anyways, we don't indent to free the memory.
*/
if (result->gr_proxy_datalen < result->gr_msglen)
{
result->gr_proxy_data = (char *)realloc(
result->gr_proxy_data, result->gr_msglen);
result->gr_proxy_datalen = result->gr_msglen;
}
if (gtmpqGetnchar((char *)result->gr_proxy_data,
result->gr_msglen, conn))
{
result->gr_status = GTM_RESULT_UNKNOWN;
return 1;
}
return 0;
}
else
result->gr_proxyhdr.ph_conid = InvalidGTMProxyConnID;
/*
* Read the fields and save into res.
*/
for (;;)
{
if (gtmpqGetc(&id, conn))
goto fail;
if (id == '\0')
break;
if (gtmpqGets(&conn->errorMessage, conn))
goto fail;
}
return 0;
fail:
return EOF;
}
/*
* GTMPQgetResult
* Get the next GTM_Result produced. Returns NULL if no
* query work remains or an error has occurred (e.g. out of
* memory).
*/
GTM_Result *
GTMPQgetResult(GTM_Conn *conn)
{
GTM_Result *res;
if (!conn)
return NULL;
/* Parse any available data, if our state permits. */
while ((res = pqParseInput(conn)) == NULL)
{
int flushResult;
/*
* If data remains unsent, send it. Else we might be waiting for the
* result of a command the backend hasn't even got yet.
*/
while ((flushResult = gtmpqFlush(conn)) > 0)
{
if (gtmpqWait(false, true, conn))
{
flushResult = -1;
break;
}
}
/* Wait for some more data, and load it. */
if (flushResult ||
gtmpqWait(true, false, conn) ||
gtmpqReadData(conn) < 0)
{
/*
* conn->errorMessage has been set by gtmpqWait or gtmpqReadData.
*/
return NULL;
}
}
return res;
}
/*
* return 0 if parsing command is totally completed.
* return 1 if it needs to be read continuously.
*/
static int
gtmpqParseSuccess(GTM_Conn *conn, GTM_Result *result)
{
int xcnt, xsize;
int i;
GlobalTransactionId *xip = NULL;
result->gr_status = GTM_RESULT_OK;
if (gtmpqGetInt((int *)&result->gr_type, 4, conn))
return 1;
result->gr_msglen -= 4;
if (conn->remote_type == GTM_NODE_GTM_PROXY)
{
if (gtmpqGetnchar((char *)&result->gr_proxyhdr,
sizeof (GTM_ProxyMsgHeader), conn))
return 1;
result->gr_msglen -= sizeof (GTM_ProxyMsgHeader);
}
else
result->gr_proxyhdr.ph_conid = InvalidGTMProxyConnID;
/*
* If we are dealing with a proxied message, just read the remaining binary
* data which can then be forwarded to the right backend.
*/
if (result->gr_proxyhdr.ph_conid != InvalidGTMProxyConnID)
{
/*
* If the allocated buffer is not large enough to hold the proxied
* data, realloc the buffer.
*
* Since the client side code is shared between the proxy and the
* backend, we don't want any memory context management etc here. So
* just use plain realloc. Anyways, we don't indent to free the memory.
*/
if (result->gr_proxy_datalen < result->gr_msglen)
{
result->gr_proxy_data = (char *)realloc(
result->gr_proxy_data, result->gr_msglen);
result->gr_proxy_datalen = result->gr_msglen;
}
if (gtmpqGetnchar((char *)result->gr_proxy_data,
result->gr_msglen, conn))
{
result->gr_status = GTM_RESULT_UNKNOWN;
return 1;
}
return result->gr_status;
}
result->gr_status = GTM_RESULT_OK;
switch (result->gr_type)
{
case SYNC_STANDBY_RESULT:
break;
case NODE_BEGIN_REPLICATION_INIT_RESULT:
break;
case NODE_END_REPLICATION_INIT_RESULT:
break;
case BEGIN_BACKUP_RESULT:
break;
case END_BACKUP_RESULT:
break;
#ifdef XCP
case REGISTER_SESSION_RESULT:
break;
#endif
case TXN_BEGIN_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txnhandle,
sizeof (GTM_TransactionHandle), conn))
result->gr_status = GTM_RESULT_ERROR;
break;
case TXN_BEGIN_GETGXID_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_gxid_tp.gxid,
sizeof (GlobalTransactionId), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_gxid_tp.timestamp,
sizeof (GTM_Timestamp), conn))
result->gr_status = GTM_RESULT_ERROR;
break;
case TXN_BEGIN_GETGXID_AUTOVACUUM_RESULT:
case TXN_PREPARE_RESULT:
case TXN_START_PREPARED_RESULT:
case TXN_ROLLBACK_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_gxid,
sizeof (GlobalTransactionId), conn))
result->gr_status = GTM_RESULT_ERROR;
break;
case TXN_COMMIT_RESULT:
case TXN_COMMIT_PREPARED_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_eof_txn.gxid,
sizeof (GlobalTransactionId), conn))
result->gr_status = GTM_RESULT_ERROR;
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_eof_txn.status,
sizeof (int), conn))
result->gr_status = GTM_RESULT_ERROR;
break;
case TXN_GET_GXID_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txn.txnhandle,
sizeof (GTM_TransactionHandle), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txn.gxid,
sizeof (GlobalTransactionId), conn))
result->gr_status = GTM_RESULT_ERROR;
break;
case TXN_GET_NEXT_GXID_RESULT:
if (gtmpqGetInt((int *)&result->gr_resdata.grd_next_gxid,
sizeof (int32), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
break;
case TXN_BEGIN_GETGXID_MULTI_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txn_get_multi.txn_count,
sizeof (int), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)result->gr_resdata.grd_txn_get_multi.txn_gxid,
sizeof (GlobalTransactionId) * result->gr_resdata.grd_txn_get_multi.txn_count,
conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txn_get_multi.timestamp,
sizeof (GTM_Timestamp), conn))
result->gr_status = GTM_RESULT_ERROR;
break;
case TXN_COMMIT_MULTI_RESULT:
case TXN_ROLLBACK_MULTI_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txn_rc_multi.txn_count,
sizeof (int), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)result->gr_resdata.grd_txn_rc_multi.status,
sizeof (int) * result->gr_resdata.grd_txn_rc_multi.txn_count, conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
break;
case SNAPSHOT_GXID_GET_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txn_snap_multi.txnhandle,
sizeof (GTM_TransactionHandle), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
/* Fall through */
case SNAPSHOT_GET_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txn_snap_multi.gxid,
sizeof (GlobalTransactionId), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
/* Fall through */
case SNAPSHOT_GET_MULTI_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txn_snap_multi.txn_count,
sizeof (int), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)result->gr_resdata.grd_txn_snap_multi.status,
sizeof (int) * result->gr_resdata.grd_txn_snap_multi.txn_count, conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)&result->gr_snapshot.sn_xmin,
sizeof (GlobalTransactionId), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)&result->gr_snapshot.sn_xmax,
sizeof (GlobalTransactionId), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetInt((int *)&result->gr_snapshot.sn_xcnt,
sizeof (int32), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
xsize = result->gr_xip_size;
xcnt = result->gr_snapshot.sn_xcnt;
xip = result->gr_snapshot.sn_xip;
if (!xip || xcnt > xsize)
{
if (!xip)
xip = (GlobalTransactionId *) malloc(sizeof(GlobalTransactionId) *
GTM_MAX_GLOBAL_TRANSACTIONS);
else
xip = (GlobalTransactionId *) realloc(xip,
sizeof(GlobalTransactionId) * xcnt);
result->gr_snapshot.sn_xip = xip;
result->gr_xip_size = xcnt;
}
if (gtmpqGetnchar((char *)xip, sizeof(GlobalTransactionId) * xcnt, conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
break;
case SEQUENCE_INIT_RESULT:
case SEQUENCE_RESET_RESULT:
case SEQUENCE_CLOSE_RESULT:
case SEQUENCE_RENAME_RESULT:
case SEQUENCE_ALTER_RESULT:
case SEQUENCE_SET_VAL_RESULT:
if (gtmpqReadSeqKey(&result->gr_resdata.grd_seqkey, conn))
result->gr_status = GTM_RESULT_ERROR;
break;
case SEQUENCE_GET_CURRENT_RESULT:
case SEQUENCE_GET_NEXT_RESULT:
case SEQUENCE_GET_LAST_RESULT:
if (gtmpqReadSeqKey(&result->gr_resdata.grd_seq.seqkey, conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_seq.seqval,
sizeof (GTM_Sequence), conn))
result->gr_status = GTM_RESULT_ERROR;
#ifdef XCP
if (result->gr_type == SEQUENCE_GET_NEXT_RESULT &&
gtmpqGetnchar((char *)&result->gr_resdata.grd_seq.rangemax,
sizeof (GTM_Sequence), conn))
result->gr_status = GTM_RESULT_ERROR;
#endif
break;
case SEQUENCE_LIST_RESULT:
if (gtmpqGetInt(&result->gr_resdata.grd_seq_list.seq_count,
sizeof (int32), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
result->gr_resdata.grd_seq_list.seq =
(GTM_SeqInfo *)malloc(sizeof(GTM_SeqInfo) *
result->gr_resdata.grd_seq_list.seq_count);
for (i = 0 ; i < result->gr_resdata.grd_seq_list.seq_count; i++)
{
int buflen;
char *buf;
/* a length of the next serialized sequence */
if (gtmpqGetInt(&buflen, sizeof (int32), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
/* a data body of the serialized sequence */
buf = (char *)malloc(buflen);
if (gtmpqGetnchar(buf, buflen, conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
gtm_deserialize_sequence(result->gr_resdata.grd_seq_list.seq+i,
buf, buflen);
free(buf);
}
break;
case TXN_GET_STATUS_RESULT:
break;
case TXN_GET_ALL_PREPARED_RESULT:
break;
case TXN_GET_GID_DATA_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txn_get_gid_data.gxid,
sizeof (GlobalTransactionId), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_txn_get_gid_data.prepared_gxid,
sizeof (GlobalTransactionId), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetInt(&result->gr_resdata.grd_txn_get_gid_data.nodelen,
sizeof (int32), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (result->gr_resdata.grd_txn_get_gid_data.nodelen != 0)
{
/* Do necessary allocation */
result->gr_resdata.grd_txn_get_gid_data.nodestring =
(char *)malloc(sizeof(char *) * result->gr_resdata.grd_txn_get_gid_data.nodelen + 1);
if (result->gr_resdata.grd_txn_get_gid_data.nodestring == NULL)
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
/* get the string itself */
if (gtmpqGetnchar(result->gr_resdata.grd_txn_get_gid_data.nodestring,
result->gr_resdata.grd_txn_get_gid_data.nodelen, conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
/* null terminate the name*/
result->gr_resdata.grd_txn_get_gid_data.nodestring[result->gr_resdata.grd_txn_get_gid_data.nodelen] = '\0';
}
else
result->gr_resdata.grd_txn_get_gid_data.nodestring = NULL;
break;
case TXN_GXID_LIST_RESULT:
result->gr_resdata.grd_txn_gid_list.len = 0;
result->gr_resdata.grd_txn_gid_list.ptr = NULL;
if (gtmpqGetInt((int *)&result->gr_resdata.grd_txn_gid_list.len,
sizeof(int32), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
/*
* I don't understand why malloc() here? Should be palloc()?
*/
result->gr_resdata.grd_txn_gid_list.ptr =
(char *)malloc(result->gr_resdata.grd_txn_gid_list.len);
if (result->gr_resdata.grd_txn_gid_list.ptr==NULL)
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar(result->gr_resdata.grd_txn_gid_list.ptr,
result->gr_resdata.grd_txn_gid_list.len,
conn)) /* serialized GTM_Transactions */
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
break;
case NODE_UNREGISTER_RESULT:
case NODE_REGISTER_RESULT:
result->gr_resdata.grd_node.len = 0;
result->gr_resdata.grd_node.node_name = NULL;
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_node.type,
sizeof (GTM_PGXCNodeType), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetInt((int *)&result->gr_resdata.grd_node.len,
sizeof(int32), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
result->gr_resdata.grd_node.node_name =
(char *)malloc(result->gr_resdata.grd_node.len+1);
if (result->gr_resdata.grd_node.node_name==NULL)
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar(result->gr_resdata.grd_node.node_name,
result->gr_resdata.grd_node.len,
conn)) /* serialized GTM_Transactions */
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
result->gr_resdata.grd_node.node_name[result->gr_resdata.grd_node.len] = '\0';
break;
case NODE_LIST_RESULT:
{
int i;
if (gtmpqGetInt(&result->gr_resdata.grd_node_list.num_node, sizeof(int32), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
for (i = 0 ; i < result->gr_resdata.grd_node_list.num_node; i++)
{
int size;
char *buf;
GTM_PGXCNodeInfo *data = (GTM_PGXCNodeInfo *)malloc(sizeof(GTM_PGXCNodeInfo));
if (data == NULL)
{
result->gr_status = GTM_RESULT_ERROR;
printfGTMPQExpBuffer(&conn->errorMessage, "Out of memory");
break;
}
if (gtmpqGetInt(&size, sizeof(int32), conn))
{
result->gr_status = GTM_RESULT_ERROR;
free(data);
break;
}
buf = (char *) malloc(size);
if (buf == NULL)
{
result->gr_status = GTM_RESULT_ERROR;
printfGTMPQExpBuffer(&conn->errorMessage, "Out of memory");
free(data);
break;
}
if (gtmpqGetnchar(buf, size, conn))
{
result->gr_status = GTM_RESULT_ERROR;
free(buf);
free(data);
break;
}
if (!gtm_deserialize_pgxcnodeinfo(data, buf, size, &conn->errorMessage))
{
result->gr_status = GTM_RESULT_ERROR;
free(buf);
free(data);
break;
}
else
{
result->gr_resdata.grd_node_list.nodeinfo[i] = data;
free(buf);
}
}
break;
}
case BARRIER_RESULT:
break;
case REPORT_XMIN_RESULT:
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_report_xmin.latest_completed_xid,
sizeof (GlobalTransactionId), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_report_xmin.global_xmin,
sizeof (GlobalTransactionId), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
if (gtmpqGetnchar((char *)&result->gr_resdata.grd_report_xmin.errcode,
sizeof (int), conn))
{
result->gr_status = GTM_RESULT_ERROR;
break;
}
break;
default:
printfGTMPQExpBuffer(&conn->errorMessage,
"unexpected result type from server; result typr was \"%d\"\n",
result->gr_type);
result->gr_status = GTM_RESULT_ERROR;
break;
}
return (result->gr_status);
}
static int
gtmpqReadSeqKey(GTM_SequenceKey seqkey, GTM_Conn *conn)
{
/*
* Read keylength
*/
if (gtmpqGetInt((int *)&seqkey->gsk_keylen, 4, conn))
return EINVAL;
/*
* Do some sanity checks on the keylength
*/
if (seqkey->gsk_keylen <= 0 || seqkey->gsk_keylen > GTM_MAX_SEQKEY_LENGTH)
return EINVAL;
if ((seqkey->gsk_key = (char *) malloc(seqkey->gsk_keylen)) == NULL)
return ENOMEM;
if (gtmpqGetnchar(seqkey->gsk_key, seqkey->gsk_keylen, conn))
return EINVAL;
return 0;
}
void
gtmpqFreeResultData(GTM_Result *result, GTM_PGXCNodeType remote_type)
{
/*
* If we are running as a GTM proxy, we don't have anything to do. This may
* change though as we add more message types below and some of them may
* need cleanup even at the proxy level
*/
if (remote_type == GTM_NODE_GTM_PROXY)
return;
switch (result->gr_type)
{
case SEQUENCE_INIT_RESULT:
case SEQUENCE_RESET_RESULT:
case SEQUENCE_CLOSE_RESULT:
case SEQUENCE_RENAME_RESULT:
case SEQUENCE_ALTER_RESULT:
case SEQUENCE_SET_VAL_RESULT:
if (result->gr_resdata.grd_seqkey.gsk_key != NULL)
free(result->gr_resdata.grd_seqkey.gsk_key);
result->gr_resdata.grd_seqkey.gsk_key = NULL;
break;
case SEQUENCE_GET_CURRENT_RESULT:
case SEQUENCE_GET_NEXT_RESULT:
case SEQUENCE_GET_LAST_RESULT:
if (result->gr_resdata.grd_seq.seqkey.gsk_key != NULL)
free(result->gr_resdata.grd_seq.seqkey.gsk_key);
result->gr_resdata.grd_seqkey.gsk_key = NULL;
break;
case TXN_GET_STATUS_RESULT:
break;
case TXN_GET_ALL_PREPARED_RESULT:
break;
case BARRIER_RESULT:
break;
case SNAPSHOT_GET_RESULT:
case SNAPSHOT_GXID_GET_RESULT:
/*
* Lets not free the xip array in the snapshot since we may need it
* again shortly
*/
break;
default:
break;
}
}
|