summaryrefslogtreecommitdiff
path: root/src/gtm/proxy/proxy_main.c
blob: ba976eb938bec9b7bdac09b757d2b4b514f799eb (plain)
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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
/*-------------------------------------------------------------------------
 *
 * proxy_main.c
 *
 * 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 <time.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <getopt.h>

#include "gtm/gtm_c.h"
#include "gtm/path.h"
#include "gtm/gtm_proxy.h"
#include "gtm/register.h"
#include "gtm/elog.h"
#include "gtm/memutils.h"
#include "gtm/gtm_list.h"
#include "gtm/libpq.h"
#include "gtm/libpq-be.h"
#include "gtm/libpq-fe.h"
#include "gtm/pqsignal.h"
#include "gtm/pqformat.h"
#include "gtm/assert.h"
#include "gtm/gtm_txn.h"
#include "gtm/gtm_seq.h"
#include "gtm/gtm_msg.h"
#include "gtm/libpq-int.h"
#include "gtm/gtm_ip.h"
#include "gtm/gtm_standby.h"
/* For reconnect control lock */
#include "gtm/gtm_lock.h"
#include "gtm/gtm_opt.h"

extern int	optind;
extern char *optarg;

#define GTM_MAX_PATH			1024
#define GTM_PROXY_DEFAULT_HOSTNAME	"*"
#define GTM_PROXY_DEFAULT_PORT		6666
#define GTM_PROXY_DEFAULT_WORKERS	2
#define GTM_PID_FILE			"gtm_proxy.pid"
#define GTM_LOG_FILE			"gtm_proxy.log"
#ifndef PROXY_CLIENT_TIMEOUT
#ifdef  GTM_DEBUG
#define PROXY_CLIENT_TIMEOUT	3600
#else
#define PROXY_CLIENT_TIMEOUT	20
#endif
#endif

static int poll_timeout_ms = 10;
static char *progname = "gtm_proxy";
char	   *ListenAddresses;
int			GTMProxyPortNumber;
int			GTMProxyWorkerThreads;
char		*GTMProxyDataDir;
char		*GTMProxyConfigFileName;
char		*GTMConfigFileName;

char		*GTMServerHost;
int			GTMServerPortNumber;

int			GTMConnectRetryInterval = 60;

/*
 * Keepalives setup for the connection with GTM server
 */
int	tcp_keepalives_idle = 0;
int	tcp_keepalives_interval = 0;
int tcp_keepalives_count = 0;

char *GTMProxyNodeName = NULL;
GTM_ThreadID	TopMostThreadID;

/* Communication area with SIGUSR2 signal handler */
GTMProxy_ThreadInfo **Proxy_ThreadInfo;
short	ReadyToReconnect = FALSE;
char	*NewGTMServerHost;
int		NewGTMServerPortNumber;

/* Status reader/reporter */
char	*error_reporter;
char	*status_reader;

/* Mode */
bool	isStartUp = false;

/* Reconnect Control Lock */
GTM_RWLock 	ReconnectControlLock;
jmp_buf     mainThreadSIGUSR1_buf;
int			SIGUSR1Accepted = FALSE;

/* If this is GTM or not */
/*
 * Used to determine if given Port is in GTM or in GT_Proxy.
 * If it is in GTM, we should consider to flush GTM_Conn before
 * writing anything to Port.
 */
bool isGTM = false;

/* The socket(s) we're listening to. */
#define MAXLISTEN	64
static int	ListenSocket[MAXLISTEN];

pthread_key_t	threadinfo_key;
static bool		GTMProxyAbortPending = false;
static GTM_Conn *master_conn;


/*
 * External Routines
 */
extern void InitializeGTMOptions(void);


/*
 * Internal Routines
 */
static Port *ConnCreate(int serverFd);
static void ConnFree(Port *conn);
static int ServerLoop(void);
static int initMasks(fd_set *rmask);
void *GTMProxy_ThreadMain(void *argp);
static int GTMProxyAddConnection(Port *port);
static int ReadCommand(GTMProxy_ConnectionInfo *conninfo, StringInfo inBuf);
static void GTMProxy_HandshakeConnection(GTMProxy_ConnectionInfo *conninfo);
static void GTMProxy_HandleDisconnect(GTMProxy_ConnectionInfo *conninfo, GTM_Conn *gtm_conn);

static void GTMProxy_ProxyCommand(GTMProxy_ConnectionInfo *conninfo,
		GTM_Conn *gtm_conn, GTM_MessageType mtype, StringInfo message);

static void ProcessCommand(GTMProxy_ConnectionInfo *conninfo,
		GTM_Conn *gtm_conn, StringInfo input_message);
static GTM_Conn *HandleGTMError(GTM_Conn *gtm_conn);
static GTM_Conn *HandlePostCommand(GTMProxy_ConnectionInfo *conninfo, GTM_Conn *gtm_conn);
static void ProcessTransactionCommand(GTMProxy_ConnectionInfo *conninfo,
		GTM_Conn *gtm_conn, GTM_MessageType mtype, StringInfo message);
static void ProcessSnapshotCommand(GTMProxy_ConnectionInfo *conninfo,
		GTM_Conn *gtm_conn, GTM_MessageType mtype, StringInfo message);

static void GTMProxy_RegisterPGXCNode(GTMProxy_ConnectionInfo *conninfo,
									  char *node_name,
									  GTM_PGXCNodeType remote_type,
									  bool is_postmaster);

static void ProcessResponse(GTMProxy_ThreadInfo *thrinfo,
		GTMProxy_CommandInfo *cmdinfo, GTM_Result *res);

static void GTMProxy_ProcessPendingCommands(GTMProxy_ThreadInfo *thrinfo);
static void GTMProxy_CommandPending(GTMProxy_ConnectionInfo *conninfo,
		GTM_MessageType mtype, GTMProxy_CommandData cmd_data);

static bool CreateOptsFile(int argc, char *argv[]);
static void CreateDataDirLockFile(void);
static void CreateLockFile(const char *filename, const char *refName);
static void SetDataDir(void);
static void ChangeToDataDir(void);
static void checkDataDir(void);
static void DeleteLockFile(const char *filename);
static void RegisterProxy(bool is_reconnect);
static void UnregisterProxy(void);
static GTM_Conn *ConnectGTM(void);
static void ReleaseCmdBackup(GTMProxy_CommandInfo *cmdinfo);
static void workerThreadReconnectToGTM(void);
#ifdef USE_ASSERT_CHECKING
static bool IsProxiedMessage(GTM_MessageType mtype);
#endif

/*
 * One-time initialization. It's called immediately after the main process
 * starts
 */
static GTMProxy_ThreadInfo *
MainThreadInit()
{
	GTMProxy_ThreadInfo *thrinfo;

	pthread_key_create(&threadinfo_key, NULL);

	/*
	 * Initialize the lock protecting the global threads info
	 */
	GTM_RWLockInit(&GTMProxyThreads->gt_lock);

	/*
	 * We are called even before memory context management is setup. We must
	 * use malloc
	 */
	thrinfo = (GTMProxy_ThreadInfo *)malloc(sizeof (GTMProxy_ThreadInfo));

	if (thrinfo == NULL)
	{
		fprintf(stderr, "malloc failed: %d", errno);
		fflush(stdout);
		fflush(stderr);
		exit(1);
	}

	memset((char *)thrinfo, 0, sizeof(GTMProxy_ThreadInfo));

	if (SetMyThreadInfo(thrinfo))
	{
		fprintf(stderr, "SetMyThreadInfo failed: %d", errno);
		fflush(stdout);
		fflush(stderr);
		exit(1);
	}

	TopMostThreadID = pthread_self();

	return thrinfo;
}

/*
 * Bare minimum supporting infrastructure. Must be called at the very beginning
 * so that further initilization can have it ready
 */
static void
InitGTMProxyProcess()
{
	GTMProxy_ThreadInfo *thrinfo = MainThreadInit();
	MyThreadID = pthread_self();
	MemoryContextInit();

	/*
	 * The memory context is now set up.
	 * Add the thrinfo structure in the global array
	 */
	if (GTMProxy_ThreadAdd(thrinfo) == -1)
	{
		fprintf(stderr, "GTMProxy_ThreadAdd for main thread failed: %d", errno);
		fflush(stdout);
		fflush(stderr);
	}
}

static void
BaseInit()
{
	checkDataDir();
	SetDataDir();
	ChangeToDataDir();
	CreateDataDirLockFile();

	if (GTMLogFile == NULL)
	{
		GTMLogFile = (char *) malloc(GTM_MAX_PATH);
		sprintf(GTMLogFile, "%s/%s", GTMProxyDataDir, GTM_LOG_FILE);
	}

	/* Initialize reconnect control lock */

	GTM_RWLockInit(&ReconnectControlLock);

	/* Register Proxy on GTM */
	RegisterProxy(false);

	DebugFileOpen();
}

static char *
read_token(char *line, char **next)
{
	char *tok;
	char *next_token;

	if (line == NULL)
	{
		*next = NULL;
		return(NULL);
	}
	for (tok = line;; tok++)
	{
		if (*tok == 0 || *tok == '\n')
			return(NULL);
		if (*tok == ' ' || *tok == '\t')
			continue;
		else
			break;
	}
	for (next_token = tok;; next_token++)
	{
		if (*next_token == 0 || *next_token == '\n')
		{
			*next_token = 0;
			*next = NULL;
			return(tok);
		}
		if (*next_token == ' ' || *next_token == '\t')

		{
			*next_token = 0;
			*next = next_token + 1;
			return(tok);
		}
		else
			continue;
	}
	Assert(0);		/* Never comes here.  Keep compiler quiet. */
}

/*
 * Returns non-zero if failed.
 * We assume that current working directory is that specified by -D option.
 */
#define MAXLINE 1024
#define INVALID_RECONNECT_OPTION_MSG() \
	do{ \
		ereport(ERROR, (0, errmsg("Invalid Reconnect Option"))); \
	} while(0)

static int
GTMProxy_ReadReconnectInfo(void)
{

	char optstr[MAXLINE];
	char *line;
	FILE *optarg_file;
	char *optValue;
	char *option;
	char *next_token;

	optarg_file = fopen("newgtm", "r");
	if (optarg_file == NULL)
	{
		INVALID_RECONNECT_OPTION_MSG();
		return(-1);
	}
	line = fgets(optstr, MAXLINE, optarg_file);
	if (line == NULL)
	{
		INVALID_RECONNECT_OPTION_MSG();
		return(-1);
	}
	fclose(optarg_file);

	elog(LOG, "reconnect option = \"%s\"\n", optstr);

	next_token = optstr;
	while ((option = read_token(next_token, &next_token)))
	{
		if (strcmp(option, "-t") == 0)	/* New GTM port */
		{
			optValue = read_token(next_token, &next_token);
			if (optValue == NULL)
			{
				INVALID_RECONNECT_OPTION_MSG();
				return(-1);
			}
			NewGTMServerPortNumber = atoi(optValue);
			continue;
		}
		else if (strcmp(option, "-s") == 0)
		{
			optValue = read_token(next_token, &next_token);
			if (optValue == NULL)
			{
				INVALID_RECONNECT_OPTION_MSG();
				return(-1);
			}
			if (NewGTMServerHost)
				free(NewGTMServerHost);
			NewGTMServerHost = strdup(optValue);
			continue;
		}
		else
		{
			INVALID_RECONNECT_OPTION_MSG();
			return(-1);
		}
	}
	return(0);
}

static void
GTMProxy_SigleHandler(int signal)
{
	int ii;

	elog(DEBUG1, "Received signal %d\n", signal);

	switch (signal)
	{
		case SIGKILL:
		case SIGTERM:
		case SIGQUIT:
		case SIGINT:
		case SIGHUP:
			break;
		case SIGUSR1:	/* Reconnect from gtm_ctl */
			/*
			 * Only the main thread can distribute SIGUSR2 to avoid lock contention
			 * of the thread info. If an other thread receives SIGUSR1, it will proxy
			 * SIGUSR1 to the main thread.
			 *
			 * The mask is set to block signals.  They're blocked until all the
			 * threads reconnect to the new GTM.
			 */
			elog(DEBUG1, "Accepted SIGUSR1\n");
			if (MyThreadID != TopMostThreadID)
			{

				elog(DEBUG1, "Not on main thread, proxy the signal to the main thread.");

				pthread_kill(TopMostThreadID, SIGUSR1);
				return;
			}
			/*
			 * Then this is the main thread.
			 */
			PG_SETMASK(&BlockSig);

			elog(DEBUG1, "I'm the main thread. Accepted SIGUSR1.");

			/*
			 * Set Reconnect Info
			 */
			if (!ReadyToReconnect)
			{
				elog(DEBUG1, "SIGUSR1 detected, but not ready to handle this. Ignored");
				PG_SETMASK(&UnBlockSig);
				return;
			}
			elog(DEBUG1, "SIGUSR1 detected. Set reconnect info for each worker thread");
			if (GTMProxy_ReadReconnectInfo() != 0)
			{
				/* Failed to read reconnect information from reconnect data file */
				PG_SETMASK(&UnBlockSig);
				return;
			}
			/*
			 * Send SIGUSR2 to all worker threads.
			 * Check if all the worker threads can accept SIGUSR2
			 */
			for (ii = 0; ii < GTMProxyWorkerThreads; ii++)
			{
				if ((Proxy_ThreadInfo[ii] == NULL) ||
					(Proxy_ThreadInfo[ii]->can_accept_SIGUSR2 == FALSE))
				{
					elog(NOTICE, "Some worker thread is not ready to handle this. Retry reconnection later.\n");
					PG_SETMASK(&UnBlockSig);
					return;
				}
			}
			/*
			 * Before send SIGUSR2 to worker threads, acquire reconnect control lock in write mode
			 * so that worker threads wait until main thread reconnects to new GTM and register
			 * itself.
			 */
			GTM_RWLockAcquire(&ReconnectControlLock, GTM_LOCKMODE_WRITE);

			/* We cannot accept the next SIGUSR1 until all the reconnect is finished. */
			ReadyToReconnect = false;

			/*
			 * Issue SIGUSR2 to all the worker threads.
			 * It will not be issued to the main thread.
			 */
			for (ii = 0; ii < GTMProxyWorkerThreads; ii++)
				pthread_kill(Proxy_ThreadInfo[ii]->thr_id, SIGUSR2);

			elog(DEBUG1, "SIGUSR2 issued to all the worker threads.");
			PG_SETMASK(&UnBlockSig);

			/*
			 * Note that during connection handling with backends, signals are blocked
			 * so it is safe to longjump here.
			 */
			siglongjmp(mainThreadSIGUSR1_buf, 1);

		case SIGUSR2:  /* Reconnect from the main thread */
			/* Main thread has nothing to do twith this signal and should not receive this. */
			PG_SETMASK(&BlockSig);

			elog(DEBUG1, "Detected SIGUSR2, thread:%ld", (long) MyThreadID);

			if (MyThreadID == TopMostThreadID)
			{
				/* This should not be reached. Just in case. */

				elog(DEBUG1, "SIGUSR2 received by the main thread.  Ignoring.");

				PG_SETMASK(&UnBlockSig);
				return;
			}
			GetMyThreadInfo->reconnect_issued = TRUE;
			if (GetMyThreadInfo->can_longjmp)
			{
				siglongjmp(GetMyThreadInfo->longjmp_env, 1);
			}
			PG_SETMASK(&UnBlockSig);
			return;

		default:
			fprintf(stderr, "Unknown signal %d\n", signal);
			return;
	}

	/* Unregister Proxy on GTM */
	UnregisterProxy();

	/*
	 * XXX We should do a clean shutdown here.
	 */
	/* Delete pid file before shutting down */
	DeleteLockFile(GTM_PID_FILE);

	PG_SETMASK(&BlockSig);
	GTMProxyAbortPending = true;

	return;
}

/*
 * Help display should match
 */
static void
help(const char *progname)
{
	printf(_("This is the GTM proxy.\n\n"));
	printf(_("Usage:\n  %s [OPTION]...\n\n"), progname);
	printf(_("Options:\n"));
	printf(_("  -h hostname		GTM proxy hostname/IP\n"));
	printf(_("  -p port			GTM proxy port number\n"));
	printf(_("  -s hostname		GTM server hostname/IP \n"));
	printf(_("  -t port			GTM server port number\n"));
	printf(_("  -i nodename 	GTM proxy nodename\n"));
	printf(_("  -n count		Number of worker threads\n"));
	printf(_("  -D directory	GTM proxy working directory\n"));
	printf(_("  -l filename		GTM proxy log file name \n"));
	printf(_("  --help          show this help, then exit\n"));
}


int
main(int argc, char *argv[])
{
	int			opt;
	int			status;
	int			i;

	/*
	 * Variable to store option parameters
	 */
	char	*listen_addresses = NULL;
	char	*node_name = NULL;
	char   	*proxy_port_number = NULL;
	char	*proxy_worker_threads = NULL;
	char	*data_dir = NULL;
	char	*log_file = NULL;
	char	*gtm_host = NULL;
	char	*gtm_port = NULL;

	isStartUp = true;

	/*
	 * At first, initialize options.
	 */
	InitializeGTMOptions();

	/* 
	 * Also initialize bare minimum supporting infrastructure such as memory
	 * context and thread control structure
	 */
	InitGTMProxyProcess();

	/*
	 * Catch standard options before doing much else
	 */
	if (argc > 1)
	{
		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
		{
			help(argv[0]);
			exit(0);
		}
	}

	ListenAddresses = strdup(GTM_PROXY_DEFAULT_HOSTNAME);
	GTMProxyPortNumber = GTM_PROXY_DEFAULT_PORT;
	GTMProxyWorkerThreads = GTM_PROXY_DEFAULT_WORKERS;

	NewGTMServerHost = NULL;

	/*
	 * Parse the command like options and set variables
	 */
	while ((opt = getopt(argc, argv, "h:i:p:n:D:l:s:t:")) != -1)
	{
		switch (opt)
		{
			case 'h':
				/* Listen address of the proxy */
				if (listen_addresses)
					free(listen_addresses);
				listen_addresses = strdup(optarg);
				break;

			case 'i':
				/* GTM Proxy identification name */
				if (node_name)
					free(node_name);
				node_name = strdup(optarg);
				break;

			case 'p':
				/* Port number for the proxy to listen on */
				if (proxy_port_number)
					free(proxy_port_number);
				proxy_port_number = strdup(optarg);
				break;

			case 'n':
				/* Number of worker threads */
				if (proxy_worker_threads)
					free(proxy_worker_threads);
				proxy_worker_threads = strdup(optarg);
				break;

			case 'D':
				if (data_dir)
					free(data_dir);
				data_dir = strdup(optarg);
				canonicalize_path(data_dir);
				break;

			case 'l':
				/* The log file */
				if (log_file)
					free(log_file);
				log_file = strdup(optarg);
				break;

			case 's':
				/* GTM server host name */
				if (gtm_host)
					free(gtm_host);
				gtm_host = strdup(optarg);
				break;

			case 't':
				/* GTM server port number */
				if (gtm_port)
					free(gtm_port);
				gtm_port = strdup(optarg);
				break;

			default:
				write_stderr("Try \"%s --help\" for more information.\n",
							 progname);
		}
	}

	/*
	 * Setup working directory
	 */
	if (data_dir)
		SetConfigOption("data_dir", data_dir, GTMC_STARTUP, GTMC_S_OVERRIDE);

	/*
	 * Setup configuration file
	 */
	if (!SelectConfigFiles(data_dir, progname))
		exit(1);

	/*
	 * Parse config file
	 */
	ProcessConfigFile(GTMC_STARTUP);

	/*
	 * Override with command line options.   "data_dir" was handled in the privious line.
	 */
	if (listen_addresses)
	{
		SetConfigOption(GTM_OPTNAME_LISTEN_ADDRESSES, listen_addresses, GTMC_STARTUP, GTMC_S_OVERRIDE);
		free(listen_addresses);
		listen_addresses = NULL;
	}
	if (node_name)
	{
		SetConfigOption(GTM_OPTNAME_NODENAME, node_name, GTMC_STARTUP, GTMC_S_OVERRIDE);
		free(node_name);
		node_name = NULL;
	}
	if (proxy_port_number)
	{
		SetConfigOption(GTM_OPTNAME_PORT, proxy_port_number, GTMC_STARTUP, GTMC_S_OVERRIDE);
		free(proxy_port_number);
		proxy_port_number = NULL;
	}
	if (proxy_worker_threads)
	{
		SetConfigOption(GTM_OPTNAME_WORKER_THREADS, proxy_worker_threads, GTMC_STARTUP, GTMC_S_OVERRIDE);
		free(proxy_worker_threads);
		proxy_worker_threads = NULL;
	}
	if (log_file)
	{
		SetConfigOption(GTM_OPTNAME_LOG_FILE, log_file, GTMC_STARTUP, GTMC_S_OVERRIDE);
		free(log_file);
		log_file = NULL;
	}
	if (gtm_host)
	{
		SetConfigOption(GTM_OPTNAME_GTM_HOST, gtm_host, GTMC_STARTUP, GTMC_S_OVERRIDE);
		free(gtm_host);
		gtm_host = NULL;
	}
	if (gtm_port)
	{
		SetConfigOption(GTM_OPTNAME_GTM_PORT, gtm_port, GTMC_STARTUP, GTMC_S_OVERRIDE);
		free(gtm_port);
		gtm_port = NULL;
	}


	/*
	 * Check Options
	 */
	if (GTMProxyDataDir == NULL)
	{
		write_stderr("GTM Proxy data directory must be specified\n");
		write_stderr("Try \"%s --help\" for more information.\n",
					 progname);
		exit(1);
	}
	if (GTMProxyNodeName == NULL)
	{
		write_stderr("GTM Proxy Node name must be specified\n");
		write_stderr("Try \"%s --help\" for more information.\n",
					 progname);
		exit(1);
	}
	if (ListenAddresses == NULL || *ListenAddresses == '\0')
	{
		write_stderr("GTM Proxy listen addresses must be specified\n");
		write_stderr("Try \"%s --help\" for more information.\n",
					 progname);
		exit(1);
	}
	if (GTMProxyPortNumber == 0)
	{
		write_stderr("GTM Proxy port number must be specified\n");
		write_stderr("Try \"%s --help\" for more information.\n",
					 progname);
		exit(1);

	}
	if (GTMServerHost == NULL || *GTMServerHost == '\0')
	{
		write_stderr("GTM server listen address must be specified\n");
		write_stderr("Try \"%s --help\" for more information.\n",
					 progname);
		exit(1);

	}
	if (GTMServerPortNumber == 0)
	{
		write_stderr("GTM server port number must be specified\n");
		write_stderr("Try \"%s --help\" for more information.\n",
					 progname);
		exit(1);

	}

	/*
	 * GTM accepts no non-option switch arguments.
	 */
	if (optind < argc)
	{
		write_stderr("%s: invalid argument: \"%s\"\n",
					 progname, argv[optind]);
		write_stderr("Try \"%s --help\" for more information.\n",
					 progname);
		exit(1);
	}

	/*
	 * Some basic initialization must happen before we do anything
	 * useful
	 */
	BaseInit();

	elog(LOG, "Starting GTM proxy at (%s:%d)", ListenAddresses, GTMProxyPortNumber);

	/*
	 * Establish input sockets.
	 */
	for (i = 0; i < MAXLISTEN; i++)
		ListenSocket[i] = -1;

	if (ListenAddresses)
	{
		int			success = 0;

		if (strcmp(ListenAddresses, "*") == 0)
			status = StreamServerPort(AF_UNSPEC, NULL,
									  (unsigned short) GTMProxyPortNumber,
									  ListenSocket, MAXLISTEN);
		else
			status = StreamServerPort(AF_UNSPEC, ListenAddresses,
									  (unsigned short) GTMProxyPortNumber,
									  ListenSocket, MAXLISTEN);

		if (status == STATUS_OK)
			success++;
		else
			ereport(FATAL,
					(errmsg("could not create listen socket for \"%s\"",
							ListenAddresses)));
	}

	/*
	 * check that we have some socket to listen on
	 */
	if (ListenSocket[0] == -1)
		ereport(FATAL,
				(errmsg("no socket created for listening")));

	/*
	 * Record gtm proxy options.  We delay this till now to avoid recording
	 * bogus options
	 */
	if (!CreateOptsFile(argc, argv))
		exit(1);

	pqsignal(SIGHUP, GTMProxy_SigleHandler);
	pqsignal(SIGKILL, GTMProxy_SigleHandler);
	pqsignal(SIGQUIT, GTMProxy_SigleHandler);
	pqsignal(SIGTERM, GTMProxy_SigleHandler);
	pqsignal(SIGINT, GTMProxy_SigleHandler);
	pqsignal(SIGUSR1, GTMProxy_SigleHandler);
	pqsignal(SIGUSR2, GTMProxy_SigleHandler);
	pqsignal(SIGPIPE, SIG_IGN);

	pqinitmask();

	/*
	 * Initialize SIGUSR2 interface area (Thread info)
	 */
	Proxy_ThreadInfo = palloc0(sizeof(GTMProxy_ThreadInfo *) * GTMProxyWorkerThreads);

	/*
	 * Pre-fork so many worker threads
	 */
	for (i = 0; i < GTMProxyWorkerThreads; i++)
	{
		/*
		 * XXX Start the worker thread
		 */
		if (GTMProxy_ThreadCreate(GTMProxy_ThreadMain, i) == NULL)
		{
			elog(ERROR, "failed to create a new thread");
			return STATUS_ERROR;
		}
	}

	/*
	 * Accept any new connections. Add for each incoming connection to one of
	 * the pre-forked threads.
	 */
	status = ServerLoop();

	/*
	 * ServerLoop probably shouldn't ever return, but if it does, close down.
	 */
	exit(status != STATUS_OK);

	return 0;					/* not reached */
}

/*
 * ConnCreate -- create a local connection data structure
 */
static Port *
ConnCreate(int serverFd)
{
	Port	   *port;

	if (!(port = (Port *) calloc(1, sizeof(Port))))
	{
		ereport(LOG,
				(ENOMEM,
				 errmsg("out of memory")));
		return NULL;
	}

	if (StreamConnection(serverFd, port) != STATUS_OK)
	{
		if (port->sock >= 0)
			StreamClose(port->sock);
		ConnFree(port);

		return NULL;
	}

	port->conn_id = InvalidGTMProxyConnID;

	return port;
}

/*
 * ConnFree -- free a local connection data structure
 */
static void
ConnFree(Port *conn)
{
	free(conn);
}

/*
 * Main idle loop of postmaster
 */
static int
ServerLoop(void)
{
	fd_set		readmask;
	int			nSockets;

	nSockets = initMasks(&readmask);

	for (;;)
	{
		fd_set		rmask;
		int			selres;

		if (sigsetjmp(mainThreadSIGUSR1_buf, 1) != 0)
		{
			/*
			 * Reconnect!
			 * Use RegisterProxy() call. Before this, change connection information
			 * of GTM to the new one.
			 * Because this is done while ReconnectControlLock is acquired,
			 * worker threads can use this change and they don't have to worry about
			 * new connection point.
			 *
			 * Because we leave the old socket as is, there could be some waste of
			 * the resource but this may not happen so many times.
			 */

			elog(DEBUG1, "Main Thread reconnecting to new GTM.");
			RegisterProxy(TRUE);
			elog(DEBUG1, "Reconnected.");

			/* If it is done, then release the lock for worker threads. */
			GTM_RWLockRelease(&ReconnectControlLock);
		}
		/*
		 * Delay the point to accept reconnect until here because
		 * longjmp buffer has not been prepared.
		 */
		ReadyToReconnect = TRUE;

		/*
		 * Wait for a connection request to arrive.
		 *
		 * Wait at most one minute, to ensure that the other background
		 * tasks handled below get done even when no requests are arriving.
		 */
		memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));

		PG_SETMASK(&UnBlockSig);

		if (GTMProxyAbortPending)
		{
			/*
			 * Tell everybody that we are shutting down
			 *
			 * !! TODO
			 */
			exit(1);
		}

		{
			/* must set timeout each time; some OSes change it! */
			struct timeval timeout;

			timeout.tv_sec = 60;
			timeout.tv_usec = 0;

			selres = select(nSockets, &rmask, NULL, NULL, &timeout);
		}

		/*
		 * Block all signals until we wait again.  (This makes it safe for our
		 * signal handlers to do nontrivial work.)
		 */
		PG_SETMASK(&BlockSig);

		/* Now check the select() result */
		if (selres < 0)
		{
			if (errno != EINTR && errno != EWOULDBLOCK)
			{
				ereport(DEBUG1,
						(EACCES,
						 errmsg("select() failed in main thread: %m")));
				return STATUS_ERROR;
			}
		}

		/*
		 * New connection pending on any of our sockets? If so, accept the
		 * connection and add it to one of the worker threads.
		 */
		if (selres > 0)
		{
			int			i;

			for (i = 0; i < MAXLISTEN; i++)
			{
				if (ListenSocket[i] == -1)
					break;
				if (FD_ISSET(ListenSocket[i], &rmask))
				{
					Port	   *port;

					port = ConnCreate(ListenSocket[i]);
					if (port)
					{
						if (GTMProxyAddConnection(port) != STATUS_OK)
						{
							StreamClose(port->sock);
							ConnFree(port);
						}
					}
				}
			}
		}
	}
}

/*
 * Initialise the masks for select() for the ports we are listening on.
 * Return the number of sockets to listen on.
 */
static int
initMasks(fd_set *rmask)
{
	int			maxsock = -1;
	int			i;

	FD_ZERO(rmask);

	for (i = 0; i < MAXLISTEN; i++)
	{
		int			fd = ListenSocket[i];

		if (fd == -1)
			break;
		FD_SET(fd, rmask);
		if (fd > maxsock)
			maxsock = fd;
	}

	return maxsock + 1;
}

/*
 * The main worker thread routine
 */
void *
GTMProxy_ThreadMain(void *argp)
{
	GTMProxy_ThreadInfo *thrinfo = (GTMProxy_ThreadInfo *)argp;
	int qtype;
	StringInfoData input_message;
	sigjmp_buf  local_sigjmp_buf;
	int32 saved_seqno = -1;
	int ii, nrfds;
	char gtm_connect_string[1024];
	int	first_turn = TRUE;	/* Used only to set longjmp target at the first turn of thread loop */
	GTMProxy_CommandData cmd_data = {};

	elog(DEBUG3, "Starting the connection helper thread");

	/*
	 * Create the memory context we will use in the main loop.
	 *
	 * MessageContext is reset once per iteration of the main loop, ie, upon
	 * completion of processing of each command message from the client.
	 *
	 * This context is thread-specific
	 */
	MessageContext = AllocSetContextCreate(TopMemoryContext,
										   "MessageContext",
										   ALLOCSET_DEFAULT_MINSIZE,
										   ALLOCSET_DEFAULT_INITSIZE,
										   ALLOCSET_DEFAULT_MAXSIZE,
										   false);

	/*
	 * Set up connection with the GTM server
	 */
	sprintf(gtm_connect_string, "host=%s port=%d node_name=%s remote_type=%d",
			GTMServerHost, GTMServerPortNumber, GTMProxyNodeName, GTM_NODE_GTM_PROXY);

	thrinfo->thr_gtm_conn = PQconnectGTM(gtm_connect_string);

	if (thrinfo->thr_gtm_conn == NULL)
		elog(FATAL, "GTM connection failed");

	/*
	 * Get the input_message in the TopMemoryContext so that we don't need to
	 * free/palloc it for every incoming message. Unlike Postgres, we don't
	 * expect the incoming messages to be of arbitrary sizes
	 */

	initStringInfo(&input_message);

	thrinfo->reconnect_issued = FALSE;

	/*
	 * Initialize comand backup area
	 */
	for (ii = 0; ii < GTM_PROXY_MAX_CONNECTIONS; ii++)
	{
		thrinfo->thr_any_backup[ii] = FALSE;
		thrinfo->thr_qtype[ii] = 0;
		initStringInfo(&(thrinfo->thr_inBufData[ii]));
	}

	/*
	 * If an exception is encountered, processing resumes here so we abort the
	 * current transaction and start a new one.
	 *
	 * You might wonder why this isn't coded as an infinite loop around a
	 * PG_TRY construct.  The reason is that this is the bottom of the
	 * exception stack, and so with PG_TRY there would be no exception handler
	 * in force at all during the CATCH part.  By leaving the outermost setjmp
	 * always active, we have at least some chance of recovering from an error
	 * during error recovery.  (If we get into an infinite loop thereby, it
	 * will soon be stopped by overflow of elog.c's internal state stack.)
	 */

	if (sigsetjmp(local_sigjmp_buf, 1) != 0)
	{
		/* Release all mutex and rwlocks */
		GTM_MutexLockReleaseAll();
		GTM_RWLockReleaseAll();

		/*
		 * NOTE: if you are tempted to add more code in this if-block,
		 * consider the high probability that it should be in
		 * AbortTransaction() instead.	The only stuff done directly here
		 * should be stuff that is guaranteed to apply *only* for outer-level
		 * error recovery, such as adjusting the FE/BE protocol status.
		 */

		/* Report the error to the client and/or server log */
		if (thrinfo->thr_conn_count > 0)
		{
			for (ii = 0; ii < thrinfo->thr_conn_count; ii++)
			{
				int connIndx = thrinfo->thr_conn_map[ii];
				GTMProxy_ConnectionInfo *conninfo = thrinfo->thr_all_conns[connIndx];
				
				/*
				 * Now clean up disconnected connections
				 */
				if (conninfo->con_disconnected)
				{
					EmitErrorReport(conninfo->con_port);
					if (conninfo->con_port->sock > 0)
						StreamClose(conninfo->con_port->sock);
					GTMProxy_ThreadRemoveConnection(thrinfo, conninfo);
					pfree(conninfo);
					ii--;
				}
				else
				{
					/*
					 * Consume all the pending data on this connection and send
					 * error report
					 */
					if (conninfo->con_pending_msg != MSG_TYPE_INVALID)
					{
						conninfo->con_port->PqRecvPointer = conninfo->con_port->PqRecvLength = 0;
						conninfo->con_pending_msg = MSG_TYPE_INVALID;
						EmitErrorReport(conninfo->con_port);
					}
				}
			}
		}
		else
			EmitErrorReport(NULL);

		/*
		 * Now return to normal top-level context and clear ErrorContext for
		 * next time.
		 */
		MemoryContextSwitchTo(TopMemoryContext);
		FlushErrorState();
	}

	/* We can now handle ereport(ERROR) */
	PG_exception_stack = &local_sigjmp_buf;

	/*
	 * Now we're entering thread loop. The last work is to initialize SIGUSR2 control.
	 */
	Disable_Longjmp();
	GetMyThreadInfo->can_accept_SIGUSR2 = TRUE;
	GetMyThreadInfo->reconnect_issued = FALSE;
	GetMyThreadInfo->can_longjmp = FALSE;

	/*--------------------------------------------------------------
	 * Thread Loop
	 *-------------------------------------------------------------
	 */
	for (;;)
	{
		gtm_ListCell *elem = NULL;
		GTM_Result *res = NULL;

		/*
		 * Release storage left over from prior query cycle, and create a new
		 * query input buffer in the cleared MessageContext.
		 */
		MemoryContextSwitchTo(MessageContext);
		MemoryContextResetAndDeleteChildren(MessageContext);

		/*
		 * The following block should be skipped at the first turn.
		 */
		if (!first_turn)
		{
			GTMProxy_ConnectionInfo	*auth_required[GTM_PROXY_MAX_CONNECTIONS];
			int						auth_required_count = 0;

			/*
			 * Check if there are any changes to the connection array assigned to
			 * this thread. If so, we need to rebuild the fd array.
			 */
			GTM_MutexLockAcquire(&thrinfo->thr_lock);
			if (saved_seqno != thrinfo->thr_seqno)
			{
				saved_seqno = thrinfo->thr_seqno;

				while (thrinfo->thr_conn_count <= 0)
				{
					/*
					 * No connections assigned to the thread. Wait for at least one
					 * connection to be assigned to us
					 */
					if (sigsetjmp(GetMyThreadInfo->longjmp_env, 1) == 0)
					{
						Enable_Longjmp();
						GTM_CVWait(&thrinfo->thr_cv, &thrinfo->thr_lock);
						Disable_Longjmp();
					}
					else
					{
						/* Release all mutex and rwlocks */
						GTM_MutexLockReleaseAll();
						GTM_RWLockReleaseAll();

						/*
						 * and re-acquire the only mutex lock we are supposed
						 * to hold after the long jump.
						 */
						GTM_MutexLockAcquire(&thrinfo->thr_lock);

						/* SIGUSR2 here */
						workerThreadReconnectToGTM();
					}
				}

				memset(thrinfo->thr_poll_fds, 0, sizeof (thrinfo->thr_poll_fds));

				/*
				 * Now grab all the open connections. A lock is being hold so no
				 * new connections can be added.
				 */
				for (ii = 0; ii < thrinfo->thr_conn_count; ii++)
				{
					int connIndx = thrinfo->thr_conn_map[ii];
					GTMProxy_ConnectionInfo *conninfo = thrinfo->thr_all_conns[connIndx];

					/*
					 * Detect if the connection has been dropped to avoid
					 * a segmentation fault.
					 */
					if (conninfo->con_port == NULL)
					{
						conninfo->con_disconnected = true;
						continue;
					}

					/*
					 * If this is a newly added connection, complete the handshake
					 */
					if (!conninfo->con_authenticated)
					{
						auth_required[auth_required_count++] = conninfo;
					}

					thrinfo->thr_poll_fds[ii].fd = conninfo->con_port->sock;
					thrinfo->thr_poll_fds[ii].events = POLLIN;
					thrinfo->thr_poll_fds[ii].revents = 0;
				}
			}
			GTM_MutexLockRelease(&thrinfo->thr_lock);

			for (ii = 0; ii < auth_required_count; ii++)
			{
				/*
				 * Complete handshake with the remote node. In case we
				 * don't get the expected handshake message, the
				 * connection is dropped.
				 */
				GTMProxy_HandshakeConnection(auth_required[ii]);
			}

			while (true)
			{
				Enable_Longjmp();
				nrfds = poll(thrinfo->thr_poll_fds, thrinfo->thr_conn_count,
							poll_timeout_ms);
				Disable_Longjmp();

				if (nrfds < 0)
				{
					if (errno == EINTR)
						continue;
					elog(FATAL, "poll returned with error %d", nrfds);
				}
				else
					break;
			}

			if (nrfds == 0)
				continue;

			/*
			 * Initialize the lists
			 */
			thrinfo->thr_processed_commands = gtm_NIL;
			memset(thrinfo->thr_pending_commands, 0, sizeof (thrinfo->thr_pending_commands));
		}

		/*
		 * Each SIGUSR2 should return here and please note that from the beginning
		 * of the outer loop, longjmp is disabled and signal handler will simply return
		 * so that we don't have to be botherd with the memory context. We should be
		 * sure to be in MemoryContext where siglongjmp() is issued.
		 */
setjmp_again:
		if (sigsetjmp(thrinfo->longjmp_env, 1) == 0)
		{
			Disable_Longjmp();
		}
		else
		{
			/*
			 * SIGUSR2 is detected and jumped here
			 * Reconnection phase
			 */
			workerThreadReconnectToGTM();

			/*
			 * Correction of pending works.
			 */
			for (ii = 0; ii < MSG_TYPE_COUNT; ii++)
			{
				thrinfo->thr_pending_commands[ii] = gtm_NIL;
			}
			gtm_list_free_deep(thrinfo->thr_processed_commands);
			thrinfo->thr_processed_commands = gtm_NIL;
			goto setjmp_again;	/* Get ready for another SIGUSR2 */
		}
		if (first_turn)
		{
			first_turn = FALSE;
			continue;
		}

		/*
		 * Just reset the input buffer to avoid repeated palloc/pfrees
		 *
		 * XXX We should consider resetting the MessageContext periodically to
		 * handle any memory leaks
		 */
		resetStringInfo(&input_message);

		/*
		 * Now, read command from each of the connections that has some data to
		 * be read.
		 */
		for (ii = 0; ii < thrinfo->thr_conn_count; ii++)
		{
			int connIndx = thrinfo->thr_conn_map[ii];
			GTMProxy_ConnectionInfo *conninfo = thrinfo->thr_all_conns[connIndx];
			thrinfo->thr_conn = conninfo;

			if (thrinfo->thr_poll_fds[ii].revents & POLLHUP)
			{
				/*
				 * The fd has become invalid. The connection is broken. Add it
				 * to the remove_list and cleanup at the end of this round of
				 * cleanup.
				 */
				GTMProxy_CommandPending(thrinfo->thr_conn,
							MSG_BACKEND_DISCONNECT, cmd_data);
				continue;
			}

			if ((thrinfo->thr_any_backup[connIndx]) ||
				(thrinfo->thr_poll_fds[ii].revents & POLLIN))
			{
				/*
				 * (3) read a command (loop blocks here)
				 */
				qtype = ReadCommand(thrinfo->thr_conn, &input_message);

				thrinfo->thr_poll_fds[ii].revents = 0;

				switch(qtype)
				{
					case 'C':
						ProcessCommand(thrinfo->thr_conn, thrinfo->thr_gtm_conn,
								&input_message);
						HandlePostCommand(thrinfo->thr_conn, thrinfo->thr_gtm_conn);
						break;

					case 'X':
					case EOF:
						/*
						 * Connection termination request
						 *
						 * Close the socket and remember the connection
						 * as disconnected. All such connections will be
						 * removed after the command processing is over. We
						 * can't remove it just yet because we pass the slot id
						 * to the server to quickly find the backend connection
						 * while processing proxied messages.
						 */
						GTMProxy_CommandPending(thrinfo->thr_conn,
												MSG_BACKEND_DISCONNECT, cmd_data);
						break;
					default:
						/*
						 * Also disconnect if protocol error
						 */
						GTMProxy_HandleDisconnect(thrinfo->thr_conn, thrinfo->thr_gtm_conn);
						elog(ERROR, "Unexpected message, or client disconnected abruptly.");
						break;
				}

			}
		}

		/*
		 * Ok. All the commands are processed. Commands which can be proxied
		 * directly have been already sent to the GTM server. Now, group the
		 * remaining commands, send them to the server and flush the data.
		 */
		GTMProxy_ProcessPendingCommands(thrinfo);

		/*
		 * Add a special marker to tell the GTM server that we are done with
		 * one round of messages and the GTM server should flush all the
		 * pending responses after seeing this message.
		 */
		if (gtmpqPutMsgStart('F', true, thrinfo->thr_gtm_conn) ||
			gtmpqPutInt(MSG_DATA_FLUSH, sizeof (GTM_MessageType), thrinfo->thr_gtm_conn) ||
			gtmpqPutMsgEnd(thrinfo->thr_gtm_conn))
			elog(ERROR, "Error sending flush message");

		/*
		 * Make sure everything is on wire now
		 */
		Enable_Longjmp();
		gtmpqFlush(thrinfo->thr_gtm_conn);
		Disable_Longjmp();

		/*
		 * Read back the responses and put them on to the right backend
		 * connection.
		 */
		gtm_foreach(elem, thrinfo->thr_processed_commands)
		{
			GTMProxy_CommandInfo *cmdinfo = (GTMProxy_CommandInfo *)gtm_lfirst(elem);

			/*
			 * If this is a continuation of a multi-part command response, we
			 * don't need to read another result from the stream. The previous
			 * result contains our response and we should just read from it.
			 */
			if (cmdinfo->ci_res_index == 0)
			{
				Enable_Longjmp();
				if ((res = GTMPQgetResult(thrinfo->thr_gtm_conn)) == NULL)
				{
					/*
					 * Here's another place to check GTM communication error.
					 * In this case, backup of each command will be taken care of
					 * by ProcessResponse() so if socket read/write error is recorded,
					 * disconnect GTM connection, retry connection and then if it faile,
					 * wait for reconnect from gtm_ctl.
					 */
					if ((thrinfo->thr_gtm_conn->last_errno != 0) || (thrinfo->thr_gtm_conn->status == CONNECTION_BAD))
					{
						/*
						 * Please note that error handling can end up with longjmp() and
						 * may not return here.
						 */
						HandleGTMError(thrinfo->thr_gtm_conn);
					}
					elog(ERROR, "GTMPQgetResult failed");
				}
				Disable_Longjmp();
			}

			ProcessResponse(thrinfo, cmdinfo, res);
		}

		gtm_list_free_deep(thrinfo->thr_processed_commands);
		thrinfo->thr_processed_commands = gtm_NIL;

		/*
		 * Now clean up disconnected connections
		 */
		for (ii = 0; ii < thrinfo->thr_conn_count; ii++)
		{
			int connIndx = thrinfo->thr_conn_map[ii];
			GTMProxy_ConnectionInfo *conninfo = thrinfo->thr_all_conns[connIndx];
			if (conninfo->con_disconnected)
			{
				GTMProxy_ThreadRemoveConnection(thrinfo, conninfo);
				pfree(conninfo);
				ii--;
			}
		}
	}

	/* can't get here because the above loop never exits */
	Assert(false);

	return thrinfo;
}

/*
 * Add the accepted connection to the pool
 */
static int
GTMProxyAddConnection(Port *port)
{
	GTMProxy_ConnectionInfo *conninfo = NULL;

	conninfo = (GTMProxy_ConnectionInfo *)palloc0(sizeof (GTMProxy_ConnectionInfo));

	if (conninfo == NULL)
	{
		ereport(LOG,
				(ENOMEM,
					errmsg("Out of memory")));
		return STATUS_ERROR;
	}

	elog(DEBUG3, "Started new connection");
	conninfo->con_port = port;

	/*
	 * Add the conninfo struct to the next worker thread in round-robin manner
	 */
	if (!GTMProxy_ThreadAddConnection(conninfo))
		return STATUS_ERROR;

	return STATUS_OK;
}

void
ProcessCommand(GTMProxy_ConnectionInfo *conninfo, GTM_Conn *gtm_conn,
		StringInfo input_message)
{
	GTM_MessageType mtype;

	mtype = pq_getmsgint(input_message, sizeof (GTM_MessageType));

	switch (mtype)
	{
		case MSG_TXN_BEGIN_GETGXID_AUTOVACUUM:
		case MSG_TXN_PREPARE:
		case MSG_TXN_START_PREPARED:
		case MSG_TXN_GET_GID_DATA:
		case MSG_TXN_COMMIT_PREPARED:
		case MSG_SNAPSHOT_GET:
		case MSG_SEQUENCE_INIT:
		case MSG_SEQUENCE_GET_CURRENT:
		case MSG_SEQUENCE_GET_NEXT:
		case MSG_SEQUENCE_GET_LAST:
		case MSG_SEQUENCE_SET_VAL:
		case MSG_SEQUENCE_RESET:
		case MSG_SEQUENCE_CLOSE:
		case MSG_SEQUENCE_RENAME:
		case MSG_SEQUENCE_ALTER:
		case MSG_BARRIER:
		case MSG_TXN_COMMIT:
		case MSG_REGISTER_SESSION:
		case MSG_REPORT_XMIN:
		case MSG_NODE_REGISTER:
		case MSG_NODE_UNREGISTER:
			GTMProxy_ProxyCommand(conninfo, gtm_conn, mtype, input_message);
			break;

		case MSG_TXN_BEGIN:
		case MSG_TXN_BEGIN_GETGXID:
		case MSG_TXN_COMMIT_MULTI:
		case MSG_TXN_ROLLBACK:
		case MSG_TXN_GET_GXID:
			ProcessTransactionCommand(conninfo, gtm_conn, mtype, input_message);
			break;

		case MSG_SNAPSHOT_GET_MULTI:
		case MSG_SNAPSHOT_GXID_GET:
			ProcessSnapshotCommand(conninfo, gtm_conn, mtype, input_message);
			break;

		default:
			ereport(FATAL,
					(EPROTO,
					 errmsg("invalid frontend message type %d",
							mtype)));
	}

	conninfo->con_pending_msg = mtype;

}

/*
 * This funciton mainly takes care of GTM communcation error.
 *
 * Communication error was stored in last_errno chaned from GTMProxy_ConnectionInfo.
 * Note that it is set to zero if the last send/receive/read/write succeeds.
 *
 * If error is detected, then it tries to connect to GTM again, if it is
 * specified by configuration parameters.
 *
 * Relevant configuration parameters are: gtm_connect_retry_idle, gtm_connect_retry_count
 * and gtm_connect_retry_interval.
 *
 * If it is not successfull or configuration parameter does not specify it,
 * then, according to another confugration parameters, it waits "reconnect"
 * command from gtm_proxy.
 *
 * Relevant configuration parameters are: err_wait_idle, err_wait_count, and
 * err_wait_interval.
 */

static GTM_Conn *
HandleGTMError(GTM_Conn *gtm_conn)
{

	elog(NOTICE,
		 "GTM communication error was detected.  Retrying connection, interval = %d.",
		 GTMConnectRetryInterval);
	for (;;)
	{
		char gtm_connect_string[1024];

		/* Wait and retry reconnect */
		elog(DEBUG1, "Waiting %d secs.", GTMConnectRetryInterval);
		pg_usleep((long)GTMConnectRetryInterval * 1000000L);

		/*
		 * Connect retry
		 * Because this proxy has been registered to current
		 * GTM, we don't re-register it.
		 *
		 * Please note that GTM-Proxy accepts "reconnect" from gtm_ctl
		 * even while it is retrying to connect to GTM.
		 */
		elog(DEBUG1, "Try to reconnect to GTM");
		/* Make sure RECONNECT command would not come while we reconnecting */
		Disable_Longjmp();
		/* Close and free previous connection object if still active */
		GTMPQfinish(gtm_conn);
		/* Reconnect */
		sprintf(gtm_connect_string, "host=%s port=%d node_name=%s remote_type=%d",
				GTMServerHost, GTMServerPortNumber, GTMProxyNodeName, GTM_NODE_GTM_PROXY);
		gtm_conn = PQconnectGTM(gtm_connect_string);
		/*
		 * If reconnect succeeded the connection will be ready to use out of
		 * there, otherwise thr_gtm_conn will be set to NULL preventing double
		 * free.
		 */
		GetMyThreadInfo->thr_gtm_conn = gtm_conn;
		Enable_Longjmp();
		if (gtm_conn)
		{
			/* Success, update thread info and return new connection */
			elog(NOTICE, "GTM connection retry succeeded.");
			return gtm_conn;
		}
		elog(NOTICE, "GTM connection retry failed.");
	}
}

static GTM_Conn *
HandlePostCommand(GTMProxy_ConnectionInfo *conninfo, GTM_Conn *gtm_conn)
{
	int		connIdx = conninfo->con_id;

	Assert(conninfo && gtm_conn);
	/*
	 * Check if the response was handled without error.
	 * In this case, use last_errno to detect the error
	 * because system call error is only one case to detect GTM error.
	 */
	if (gtm_conn->last_errno != 0)
	{
		return(HandleGTMError(gtm_conn));
	}
	else
	{
		/*
		 * Command handled without error.  Clear the backup.
		 */
		resetStringInfo(&GetMyThreadInfo->thr_inBufData[connIdx]);
		GetMyThreadInfo->thr_any_backup[connIdx] = FALSE;
		return(gtm_conn);
	}

}

#ifdef USE_ASSERT_CHECKING
static bool
IsProxiedMessage(GTM_MessageType mtype)
{
	switch (mtype)
	{
		case MSG_TXN_BEGIN:
		case MSG_TXN_BEGIN_GETGXID_AUTOVACUUM:
		case MSG_TXN_PREPARE:
		case MSG_TXN_START_PREPARED:
		/* There are not so many 2PC from application messages, so just proxy it. */
		case MSG_TXN_COMMIT_PREPARED:
		case MSG_TXN_GET_GXID:
		case MSG_TXN_GET_GID_DATA:
		case MSG_NODE_REGISTER:
		case MSG_NODE_UNREGISTER:
		case MSG_REGISTER_SESSION:
		case MSG_REPORT_XMIN:
		case MSG_SNAPSHOT_GXID_GET:
		case MSG_SEQUENCE_INIT:
		case MSG_SEQUENCE_GET_CURRENT:
		case MSG_SEQUENCE_GET_NEXT:
		case MSG_SEQUENCE_GET_LAST:
		case MSG_SEQUENCE_SET_VAL:
		case MSG_SEQUENCE_RESET:
		case MSG_SEQUENCE_CLOSE:
		case MSG_SEQUENCE_RENAME:
		case MSG_SEQUENCE_ALTER:
		case MSG_SNAPSHOT_GET:
		case MSG_TXN_COMMIT:
		case MSG_BARRIER:
			return true;

		default:
			return false;
	}
}
#endif

static void
ProcessResponse(GTMProxy_ThreadInfo *thrinfo, GTMProxy_CommandInfo *cmdinfo,
		GTM_Result *res)
{
	StringInfoData buf;
	GlobalTransactionId gxid;
	GTM_Timestamp timestamp;
	int	status;

	switch (cmdinfo->ci_mtype)
	{
		case MSG_TXN_BEGIN_GETGXID:
			/*
			 * This is a grouped command. We send just the transaction count to
			 * the GTM server which responds back with the start GXID. We
			 * derive our GXID from the start GXID and the our position in the
			 * command queue
			 */
			if (res->gr_status == GTM_RESULT_OK)
			{
				if (res->gr_type != TXN_BEGIN_GETGXID_MULTI_RESULT)
				{
					ReleaseCmdBackup(cmdinfo);
					elog(ERROR, "Wrong result");
				}
				if (cmdinfo->ci_res_index >= res->gr_resdata.grd_txn_get_multi.txn_count)
				{
					ReleaseCmdBackup(cmdinfo);
					elog(ERROR, "Too few GXIDs");
				}

				gxid = res->gr_resdata.grd_txn_get_multi.txn_gxid[cmdinfo->ci_res_index];

				/* Send back to each client the same timestamp value asked in this message */
				timestamp = res->gr_resdata.grd_txn_get_multi.timestamp;

				pq_beginmessage(&buf, 'S');
				pq_sendint(&buf, TXN_BEGIN_GETGXID_RESULT, 4);
				pq_sendbytes(&buf, (char *)&gxid, sizeof (GlobalTransactionId));
				pq_sendbytes(&buf, (char *)&timestamp, sizeof (GTM_Timestamp));
				pq_endmessage(cmdinfo->ci_conn->con_port, &buf);
				pq_flush(cmdinfo->ci_conn->con_port);
			}
			else
			{
				pq_beginmessage(&buf, 'E');
				pq_sendbytes(&buf, res->gr_proxy_data, res->gr_msglen);
				pq_endmessage(cmdinfo->ci_conn->con_port, &buf);
				pq_flush(cmdinfo->ci_conn->con_port);
			}
			cmdinfo->ci_conn->con_pending_msg = MSG_TYPE_INVALID;
			ReleaseCmdBackup(cmdinfo);
			break;

		case MSG_TXN_COMMIT_MULTI:
			if (res->gr_type != TXN_COMMIT_MULTI_RESULT)
			{
				ReleaseCmdBackup(cmdinfo);
				elog(ERROR, "Wrong result");
			}
			/*
			 * These are grouped messages. We send an array of GXIDs to commit
			 * or rollback and the server sends us back an array of status
			 * codes.
			 */
			if (cmdinfo->ci_res_index >= res->gr_resdata.grd_txn_rc_multi.txn_count)
			{
				ReleaseCmdBackup(cmdinfo);
				elog(ERROR, "Too few GXIDs");
			}

			if (res->gr_resdata.grd_txn_rc_multi.status[cmdinfo->ci_res_index] == STATUS_OK)
			{
				int txn_count = 1;
				int status = STATUS_OK;

				pq_beginmessage(&buf, 'S');
				pq_sendint(&buf, TXN_COMMIT_MULTI_RESULT, 4);
				pq_sendbytes(&buf, (const char *)&txn_count, sizeof (int));
				pq_sendbytes(&buf, (const char *)&status, sizeof (int));
				pq_endmessage(cmdinfo->ci_conn->con_port, &buf);
				pq_flush(cmdinfo->ci_conn->con_port);
			}
			else
			{
				ReleaseCmdBackup(cmdinfo);
				ereport(ERROR2, (EINVAL, errmsg("Transaction commit failed")));
			}
			cmdinfo->ci_conn->con_pending_msg = MSG_TYPE_INVALID;
			ReleaseCmdBackup(cmdinfo);
			break;

		case MSG_TXN_ROLLBACK:
			if (res->gr_type != TXN_ROLLBACK_MULTI_RESULT)
			{
				ReleaseCmdBackup(cmdinfo);
				elog(ERROR, "Wrong result");
			}
			/*
			 * These are grouped messages. We send an array of GXIDs to commit
			 * or rollback and the server sends us back an array of status
			 * codes.
			 */
			if (cmdinfo->ci_res_index >= res->gr_resdata.grd_txn_rc_multi.txn_count)
			{
				ReleaseCmdBackup(cmdinfo);
				elog(ERROR, "Too few GXIDs");
			}

			if (res->gr_resdata.grd_txn_rc_multi.status[cmdinfo->ci_res_index] == STATUS_OK)
			{
				pq_beginmessage(&buf, 'S');
				pq_sendint(&buf, TXN_ROLLBACK_RESULT, 4);
				pq_sendbytes(&buf, (char *)&cmdinfo->ci_data.cd_rc.gxid, sizeof (GlobalTransactionId));
				pq_endmessage(cmdinfo->ci_conn->con_port, &buf);
				pq_flush(cmdinfo->ci_conn->con_port);
			}
			else
			{
				ReleaseCmdBackup(cmdinfo);
				ereport(ERROR2, (EINVAL, errmsg("Transaction commit failed")));
			}
			cmdinfo->ci_conn->con_pending_msg = MSG_TYPE_INVALID;
			ReleaseCmdBackup(cmdinfo);
			break;

		case MSG_SNAPSHOT_GET_MULTI:
			if ((res->gr_type != SNAPSHOT_GET_RESULT) &&
				(res->gr_type != SNAPSHOT_GET_MULTI_RESULT))
			{
				ReleaseCmdBackup(cmdinfo);
				elog(ERROR, "Wrong result");
			}

			if (cmdinfo->ci_res_index >= res->gr_resdata.grd_txn_snap_multi.txn_count)
			{
				ReleaseCmdBackup(cmdinfo);
				elog(ERROR, "Too few GXIDs - %d:%d", cmdinfo->ci_res_index,
						res->gr_resdata.grd_txn_snap_multi.txn_count);
			}

			status = res->gr_resdata.grd_txn_snap_multi.status[cmdinfo->ci_res_index];
		   	if ((status == STATUS_OK) || (status == STATUS_NOT_FOUND))
			{
				int txn_count = 1;
				int status = STATUS_OK;

				pq_beginmessage(&buf, 'S');
				pq_sendint(&buf, SNAPSHOT_GET_MULTI_RESULT, 4);
				pq_sendbytes(&buf, (char *)&txn_count, sizeof (txn_count));
				pq_sendbytes(&buf, (char *)&status, sizeof (status));
				pq_sendbytes(&buf, (char *)&res->gr_snapshot.sn_xmin, sizeof (GlobalTransactionId));
				pq_sendbytes(&buf, (char *)&res->gr_snapshot.sn_xmax, sizeof (GlobalTransactionId));
				pq_sendint(&buf, res->gr_snapshot.sn_xcnt, sizeof (int));
				pq_sendbytes(&buf, (char *)res->gr_snapshot.sn_xip,
							 sizeof(GlobalTransactionId) * res->gr_snapshot.sn_xcnt);
				pq_endmessage(cmdinfo->ci_conn->con_port, &buf);
				pq_flush(cmdinfo->ci_conn->con_port);
			}
			else
			{
				ReleaseCmdBackup(cmdinfo);
				ereport(ERROR2, (EINVAL, errmsg("snapshot request failed")));
			}
			cmdinfo->ci_conn->con_pending_msg = MSG_TYPE_INVALID;
			ReleaseCmdBackup(cmdinfo);
			break;

		case MSG_TXN_BEGIN:
		case MSG_TXN_BEGIN_GETGXID_AUTOVACUUM:
		case MSG_TXN_PREPARE:
		case MSG_TXN_START_PREPARED:
		/* There are not so many 2PC from application messages, so just proxy it. */
		case MSG_TXN_COMMIT_PREPARED:
		case MSG_TXN_GET_GXID:
		case MSG_TXN_GET_GID_DATA:
		case MSG_NODE_REGISTER:
		case MSG_NODE_UNREGISTER:
		case MSG_REGISTER_SESSION:
		case MSG_REPORT_XMIN:
		case MSG_SNAPSHOT_GXID_GET:
		case MSG_SEQUENCE_INIT:
		case MSG_SEQUENCE_GET_CURRENT:
		case MSG_SEQUENCE_GET_NEXT:
		case MSG_SEQUENCE_GET_LAST:
		case MSG_SEQUENCE_SET_VAL:
		case MSG_SEQUENCE_RESET:
		case MSG_SEQUENCE_CLOSE:
		case MSG_SEQUENCE_RENAME:
		case MSG_SEQUENCE_ALTER:
		case MSG_SNAPSHOT_GET:
		case MSG_TXN_COMMIT:
			Assert(IsProxiedMessage(cmdinfo->ci_mtype));
			if ((res->gr_proxyhdr.ph_conid == InvalidGTMProxyConnID) ||
				(res->gr_proxyhdr.ph_conid >= GTM_PROXY_MAX_CONNECTIONS) ||
				(thrinfo->thr_all_conns[res->gr_proxyhdr.ph_conid] != cmdinfo->ci_conn))
			{
				ReleaseCmdBackup(cmdinfo);
				elog(PANIC, "Invalid response or synchronization loss");
			}

			/*
			 * These are just proxied messages.. so just forward the response
			 * back after stripping the conid part.
			 *
			 * !!TODO As we start adding support for message grouping for
			 * messages, those message types would be removed from the above
			 * and handled separately.
			 */
			switch (res->gr_status)
			{
				case GTM_RESULT_OK:
					pq_beginmessage(&buf, 'S');
					pq_sendint(&buf, res->gr_type, 4);
					pq_sendbytes(&buf, res->gr_proxy_data, res->gr_msglen);
					pq_endmessage(cmdinfo->ci_conn->con_port, &buf);
					pq_flush(cmdinfo->ci_conn->con_port);
					break;

				default:
					pq_beginmessage(&buf, 'E');
					pq_sendbytes(&buf, res->gr_proxy_data, res->gr_msglen);
					pq_endmessage(cmdinfo->ci_conn->con_port, &buf);
					pq_flush(cmdinfo->ci_conn->con_port);
					break;
			}
			cmdinfo->ci_conn->con_pending_msg = MSG_TYPE_INVALID;
			ReleaseCmdBackup(cmdinfo);
			break;

		case MSG_BARRIER:
			switch (res->gr_status)
			{
				case GTM_RESULT_OK:
					pq_beginmessage(&buf, 'S');
					pq_sendint(&buf, res->gr_type, 4);
					pq_sendbytes(&buf, res->gr_proxy_data, res->gr_msglen);
					pq_endmessage(cmdinfo->ci_conn->con_port, &buf);
					pq_flush(cmdinfo->ci_conn->con_port);
					break;

				default:
					pq_beginmessage(&buf, 'E');
					pq_sendbytes(&buf, res->gr_proxy_data, res->gr_msglen);
					pq_endmessage(cmdinfo->ci_conn->con_port, &buf);
					pq_flush(cmdinfo->ci_conn->con_port);
					break;
			}
			cmdinfo->ci_conn->con_pending_msg = MSG_TYPE_INVALID;
			ReleaseCmdBackup(cmdinfo);
			break;
			
		default:
			ReleaseCmdBackup(cmdinfo);
			ereport(FATAL,
					(EPROTO,
					 errmsg("invalid frontend message type %d",
							cmdinfo->ci_mtype)));
	}
}

/* ----------------
 *		ReadCommand reads a command from either the frontend or
 *		standard input, places it in inBuf, and returns the
 *		message type code (first byte of the message).
 *		EOF is returned if end of file.
 * ----------------
 */
static int
ReadCommand(GTMProxy_ConnectionInfo *conninfo, StringInfo inBuf)
{
	int 			qtype;
	int				connIdx = conninfo->con_id;
	int				anyBackup;

	anyBackup = (GetMyThreadInfo->thr_any_backup[connIdx] ? TRUE : FALSE);

	/*
	 * Get message type code from the frontend.
	 */
	if (!anyBackup)
	{
		qtype = pq_getbyte(conninfo->con_port);
		GetMyThreadInfo->thr_qtype[connIdx] = qtype;
		/*
		 * We should not update thr_any_backup here.  This should be
		 * updated when the backup is consumed or command processing
		 * is done.
		 */
	}
	else
	{
		qtype = GetMyThreadInfo->thr_qtype[connIdx];
	}

	if (qtype == EOF)			/* frontend disconnected */
	{
		/* don't fill up the proxy log with client disconnect messages */
		ereport(DEBUG1,
				(EPROTO,
				 errmsg("unexpected EOF on client connection")));
		return qtype;
	}

	/*
	 * Validate message type code before trying to read body; if we have lost
	 * sync, better to say "command unknown" than to run out of memory because
	 * we used garbage as a length word.
	 *
	 * This also gives us a place to set the doing_extended_query_message flag
	 * as soon as possible.
	 */
	switch (qtype)
	{
		case 'C':
			break;

		case 'X':
			break;

		default:

			/*
			 * Otherwise we got garbage from the frontend.	We treat this as
			 * fatal because we have probably lost message boundary sync, and
			 * there's no good way to recover.
			 */
			ereport(ERROR,
					(EPROTO,
					 errmsg("invalid frontend message type %d", qtype)));

			break;
	}

	/*
	 * In protocol version 3, all frontend messages have a length word next
	 * after the type code; we can read the message contents independently of
	 * the type.
	 */
	if (!anyBackup)
	{
		if (pq_getmessage(conninfo->con_port, inBuf, 0))
			return EOF;			/* suitable message already logged */

		copyStringInfo(&(GetMyThreadInfo->thr_inBufData[connIdx]), inBuf);

		/*
		 * The next line is added because we added the code to clear backup
		 * when the response is processed.
		 */
		GetMyThreadInfo->thr_any_backup[connIdx] = TRUE;
	}
	else
	{
		copyStringInfo(inBuf, &(GetMyThreadInfo->thr_inBufData[connIdx]));
	}
	return qtype;
}

static void
ProcessTransactionCommand(GTMProxy_ConnectionInfo *conninfo, GTM_Conn *gtm_conn,
		GTM_MessageType mtype, StringInfo message)
{
	GTMProxy_CommandData cmd_data;
	uint32 global_sessionid_len;

	switch (mtype)
	{
		case MSG_TXN_BEGIN_GETGXID:
			cmd_data.cd_beg.iso_level = pq_getmsgint(message, sizeof (GTM_IsolationLevel));
			cmd_data.cd_beg.rdonly = pq_getmsgbyte(message);
			global_sessionid_len = pq_getmsgint(message, sizeof (uint32));
			memcpy(cmd_data.cd_beg.global_sessionid, pq_getmsgbytes(message,
					global_sessionid_len), global_sessionid_len);
			GTMProxy_CommandPending(conninfo, mtype, cmd_data);
			break;

		case MSG_TXN_COMMIT_MULTI:
			{
				(void) pq_getmsgint(message, sizeof (int));
			}
			/* fall through */
		case MSG_TXN_ROLLBACK:
			{
				const char *data = pq_getmsgbytes(message,
						sizeof (GlobalTransactionId));
				if (data == NULL)
					ereport(ERROR,
							(EPROTO,
							 errmsg("Message does not contain valid GXID")));
				memcpy(&cmd_data.cd_rc.gxid, data, sizeof (GlobalTransactionId));
			}
			pq_getmsgend(message);
			GTMProxy_CommandPending(conninfo, mtype, cmd_data);
			break;

		case MSG_TXN_BEGIN:
		case MSG_TXN_GET_GXID:
			elog(FATAL, "Support not yet added for these message types");
			break;

		default:
			Assert(0);			/* Shouldn't come here.. keep compiler quiet */
	}
}

static void
ProcessSnapshotCommand(GTMProxy_ConnectionInfo *conninfo, GTM_Conn *gtm_conn,
		GTM_MessageType mtype, StringInfo message)
{
	GTMProxy_CommandData cmd_data;

	switch (mtype)
	{
		case MSG_SNAPSHOT_GET_MULTI:
			{
				{
					const char *data;

					(void) pq_getmsgint(message, sizeof (int));
					data = pq_getmsgbytes(message,
							sizeof (GlobalTransactionId));
					if (data == NULL)
						ereport(ERROR,
								(EPROTO,
								 errmsg("Message does not contain valid GXID")));
					memcpy(&cmd_data.cd_snap.gxid, data, sizeof (GlobalTransactionId));
				}
				pq_getmsgend(message);
				GTMProxy_CommandPending(conninfo, mtype, cmd_data);
			}
			break;

		case MSG_SNAPSHOT_GXID_GET:
			elog(ERROR, "Message not yet support");
			break;

		default:
			Assert(0);			/* Shouldn't come here.. keep compiler quiet */
	}

}

/*
 * Proxy the incoming message to the GTM server after adding our own identifier
 * to it. The rest of the message is forwarded as it is without even reading
 * its contents.
 */
static void
GTMProxy_ProxyCommand(GTMProxy_ConnectionInfo *conninfo, GTM_Conn *gtm_conn,
		GTM_MessageType mtype, StringInfo message)
{
	GTMProxy_CommandInfo *cmdinfo;
	GTMProxy_ThreadInfo *thrinfo = GetMyThreadInfo;
	GTM_ProxyMsgHeader proxyhdr;
	const char *unreadmsg;
	int unreadmsglen;

	Assert(IsProxiedMessage(mtype));

	proxyhdr.ph_conid = conninfo->con_id;

	unreadmsglen = pq_getmsgunreadlen(message);
	unreadmsg = pq_getmsgbytes(message, unreadmsglen);

	 /* Start the message. */
	if (gtmpqPutMsgStart('C', true, gtm_conn) ||
		gtmpqPutnchar((char *)&proxyhdr, sizeof (GTM_ProxyMsgHeader), gtm_conn) ||
		gtmpqPutInt(mtype, sizeof (GTM_MessageType), gtm_conn) ||
		gtmpqPutnchar(unreadmsg, unreadmsglen, gtm_conn))
		elog(ERROR, "Error sending proxied message");

	/*
	 * Add the message to the pending command list
	 */
	cmdinfo = palloc0(sizeof (GTMProxy_CommandInfo));
	cmdinfo->ci_mtype = mtype;
	cmdinfo->ci_conn = conninfo;
	cmdinfo->ci_res_index = 0;
	thrinfo->thr_processed_commands = gtm_lappend(thrinfo->thr_processed_commands, cmdinfo);

	/* Finish the message. */
	Enable_Longjmp();
	if (gtmpqPutMsgEnd(gtm_conn))
		elog(ERROR, "Error finishing the message");
	Disable_Longjmp();

	return;
}


/*
 * Record the incoming message as per its type. After all messages of this type
 * are collected, they will be sent in a single message to the GTM server.
 */
static void
GTMProxy_CommandPending(GTMProxy_ConnectionInfo *conninfo, GTM_MessageType mtype,
		GTMProxy_CommandData cmd_data)
{
	GTMProxy_CommandInfo *cmdinfo;
	GTMProxy_ThreadInfo *thrinfo = GetMyThreadInfo;

	MemoryContext oldContext = MemoryContextSwitchTo(TopMemoryContext);

	/*
	 * Add the message to the pending command list
	 */
	cmdinfo = palloc0(sizeof (GTMProxy_CommandInfo));
	cmdinfo->ci_mtype = mtype;
	cmdinfo->ci_conn = conninfo;
	cmdinfo->ci_res_index = 0;
	cmdinfo->ci_data = cmd_data;
	thrinfo->thr_pending_commands[mtype] = gtm_lappend(thrinfo->thr_pending_commands[mtype], cmdinfo);

	MemoryContextSwitchTo(oldContext);

	return;
}

/*
 * Register PGXC Node Connection in Proxy
 * Registery on GTM is made with MSG_NODE_REGISTER message type when node is launched.
 */
static void
GTMProxy_RegisterPGXCNode(GTMProxy_ConnectionInfo *conninfo,
						  char *node_name,
						  GTM_PGXCNodeType remote_type,
						  bool is_postmaster)
{
	elog(DEBUG3, "Registering PGXC Node with name %s", node_name);
	conninfo->con_port->node_name = strdup(node_name);
	conninfo->con_port->remote_type = remote_type;
	conninfo->con_port->is_postmaster = is_postmaster;
}

static void
GTMProxy_HandshakeConnection(GTMProxy_ConnectionInfo *conninfo)
{
	/*
	 * We expect a startup message at the very start. The message type is
	 * REGISTER_COORD, followed by the 4 byte PGXC node ID
	 */
	char startup_type;
	GTM_StartupPacket sp;
	StringInfoData inBuf;
	StringInfoData buf;

	startup_type = pq_getbyte(conninfo->con_port);

	if (startup_type != 'A')
	{
		conninfo->con_disconnected = true;
		ereport(ERROR,
				(EPROTO,
				 errmsg("Expecting a startup message, but received %c",
					 startup_type)));
	}

	initStringInfo(&inBuf);

	/*
	 * All frontend messages have a length word next
	 * after the type code; we can read the message contents independently of
	 * the type.
	 */
	if (pq_getmessage(conninfo->con_port, &inBuf, 0))
	{
		conninfo->con_disconnected = true;
		ereport(ERROR,
				(EPROTO,
				 errmsg("Expecting PGXC Node ID, but received EOF")));
	}

	memcpy(&sp,
		   pq_getmsgbytes(&inBuf, sizeof (GTM_StartupPacket)),
		   sizeof (GTM_StartupPacket));
	pq_getmsgend(&inBuf);

	GTMProxy_RegisterPGXCNode(conninfo, sp.sp_node_name, sp.sp_remotetype, sp.sp_ispostmaster);

	/*
	 * Send a dummy authentication request message 'R' as the client
	 * expects that in the current protocol
	 */
	pq_beginmessage(&buf, 'R');
	pq_sendint(&buf, 0, 4);
	pq_endmessage(conninfo->con_port, &buf);
	pq_flush(conninfo->con_port);

	conninfo->con_authenticated = true;

	elog(DEBUG3, "Sent connection authentication message to the client");
}

static void
GTMProxy_HandleDisconnect(GTMProxy_ConnectionInfo *conninfo, GTM_Conn *gtm_conn)
{
	GTM_ProxyMsgHeader proxyhdr;
	int namelen;

	proxyhdr.ph_conid = conninfo->con_id;
	/* Start the message. */
	if (gtmpqPutMsgStart('C', true, gtm_conn) ||
		gtmpqPutnchar((char *)&proxyhdr, sizeof (GTM_ProxyMsgHeader), gtm_conn) ||
		gtmpqPutInt(MSG_BACKEND_DISCONNECT, sizeof (GTM_MessageType), gtm_conn) ||
		gtmpqPutc(conninfo->con_port->is_postmaster, gtm_conn))
		elog(ERROR, "Error proxing data");

	/*
	 * Then send node type and node number if backend is a postmaster to
	 * disconnect the correct node.
	 */
	if (conninfo->con_port->is_postmaster)
	{
		namelen = strlen(conninfo->con_port->node_name);
		if (gtmpqPutnchar((char *)&conninfo->con_port->remote_type, sizeof(GTM_PGXCNodeType), gtm_conn) ||
		    gtmpqPutInt(namelen, sizeof (int), gtm_conn) ||
		    gtmpqPutnchar(conninfo->con_port->node_name, namelen, gtm_conn) )
			elog(ERROR, "Error proxing data");
	}

	/* Finish the message. */
	if (gtmpqPutMsgEnd(gtm_conn))
		elog(ERROR, "Error finishing the message");

	conninfo->con_disconnected = true;
	if (conninfo->con_port->sock > 0)
		StreamClose(conninfo->con_port->sock);
	ConnFree(conninfo->con_port);
	conninfo->con_port = NULL;

	return;
}

/*
 * Process all the pending messages now.
 */
static void
GTMProxy_ProcessPendingCommands(GTMProxy_ThreadInfo *thrinfo)
{
	int ii;
	GTMProxy_CommandInfo *cmdinfo = NULL;
	GTM_ProxyMsgHeader proxyhdr;
	GTM_Conn *gtm_conn = thrinfo->thr_gtm_conn;
	gtm_ListCell *elem = NULL;

	for (ii = 0; ii < MSG_TYPE_COUNT; ii++)
	{
		int res_index = 0;

		/* We process backend disconnects last! */
		if (ii == MSG_BACKEND_DISCONNECT ||
				gtm_list_length(thrinfo->thr_pending_commands[ii]) == 0)
			continue;

		/*
		 * Start a new group message and fill in the headers
		 */
		proxyhdr.ph_conid = InvalidGTMProxyConnID;

		if (gtmpqPutMsgStart('C', true, gtm_conn) ||
			gtmpqPutnchar((char *)&proxyhdr, sizeof (GTM_ProxyMsgHeader), gtm_conn))
			elog(ERROR, "Error proxing data");

		switch (ii)
		{
			case MSG_TXN_BEGIN_GETGXID:
				if (gtm_list_length(thrinfo->thr_pending_commands[ii]) <=0 )
					elog(PANIC, "No pending commands of type %d", ii);

				if (gtmpqPutInt(MSG_TXN_BEGIN_GETGXID_MULTI, sizeof (GTM_MessageType), gtm_conn) ||
					gtmpqPutInt(gtm_list_length(thrinfo->thr_pending_commands[ii]), sizeof(int), gtm_conn))
					elog(ERROR, "Error sending data");
				gtm_foreach (elem, thrinfo->thr_pending_commands[ii])
				{
					cmdinfo = (GTMProxy_CommandInfo *)gtm_lfirst(elem);
					Assert(cmdinfo->ci_mtype == ii);
					cmdinfo->ci_res_index = res_index++;
					if (gtmpqPutInt(cmdinfo->ci_data.cd_beg.iso_level,
								sizeof (GTM_IsolationLevel), gtm_conn) ||
						gtmpqPutc(cmdinfo->ci_data.cd_beg.rdonly, gtm_conn) ||
						gtmpqPutInt(strlen(cmdinfo->ci_data.cd_beg.global_sessionid) + 1,
							sizeof (uint32), gtm_conn) ||
						gtmpqPutnchar(cmdinfo->ci_data.cd_beg.global_sessionid,
							strlen(cmdinfo->ci_data.cd_beg.global_sessionid) + 1, gtm_conn) ||
						gtmpqPutInt(cmdinfo->ci_conn->con_id, sizeof (GTMProxy_ConnID), gtm_conn))
						elog(ERROR, "Error sending data");

				}

				/* Finish the message. */
				Enable_Longjmp();
				if (gtmpqPutMsgEnd(gtm_conn))
					elog(ERROR, "Error finishing the message");
				Disable_Longjmp();

				/*
				 * Move the entire list to the processed command
				 */
				thrinfo->thr_processed_commands = gtm_list_concat(thrinfo->thr_processed_commands,
						thrinfo->thr_pending_commands[ii]);
				/*
				 * Free the list header of the second list, unless
				 * gtm_list_concat actually returned the second list as-is
				 * because the first list was empty
				 */
				if ((thrinfo->thr_processed_commands != thrinfo->thr_pending_commands[ii]) &&
					(thrinfo->thr_pending_commands[ii] != gtm_NIL))
					pfree(thrinfo->thr_pending_commands[ii]);
				thrinfo->thr_pending_commands[ii] = gtm_NIL;
				break;

			case MSG_TXN_COMMIT_MULTI:
				if (gtmpqPutInt(MSG_TXN_COMMIT_MULTI, sizeof (GTM_MessageType), gtm_conn) ||
					gtmpqPutInt(gtm_list_length(thrinfo->thr_pending_commands[ii]), sizeof(int), gtm_conn))
					elog(ERROR, "Error sending data");

				gtm_foreach (elem, thrinfo->thr_pending_commands[ii])
				{
					cmdinfo = (GTMProxy_CommandInfo *)gtm_lfirst(elem);
					Assert(cmdinfo->ci_mtype == ii);
					cmdinfo->ci_res_index = res_index++;
					{
						if (gtmpqPutnchar((char *)&cmdinfo->ci_data.cd_rc.gxid,
								sizeof (GlobalTransactionId), gtm_conn))
							elog(ERROR, "Error sending data");
					}
				}

				/* Finish the message. */
				Enable_Longjmp();
				if (gtmpqPutMsgEnd(gtm_conn))
					elog(ERROR, "Error finishing the message");
				Disable_Longjmp();

				/*
				 * Move the entire list to the processed command
				 */
				thrinfo->thr_processed_commands = gtm_list_concat(thrinfo->thr_processed_commands,
						thrinfo->thr_pending_commands[ii]);
				/*
				 * Free the list header of the second list, unless
				 * gtm_list_concat actually returned the second list as-is
				 * because the first list was empty
				 */
				if ((thrinfo->thr_processed_commands != thrinfo->thr_pending_commands[ii]) &&
					(thrinfo->thr_pending_commands[ii] != gtm_NIL))
					pfree(thrinfo->thr_pending_commands[ii]);
				thrinfo->thr_pending_commands[ii] = gtm_NIL;
				break;

				break;

			case MSG_TXN_ROLLBACK:
				if (gtmpqPutInt(MSG_TXN_ROLLBACK_MULTI, sizeof (GTM_MessageType), gtm_conn) ||
					gtmpqPutInt(gtm_list_length(thrinfo->thr_pending_commands[ii]), sizeof(int), gtm_conn))
					elog(ERROR, "Error sending data");

				gtm_foreach (elem, thrinfo->thr_pending_commands[ii])
				{
					cmdinfo = (GTMProxy_CommandInfo *)gtm_lfirst(elem);
					Assert(cmdinfo->ci_mtype == ii);
					cmdinfo->ci_res_index = res_index++;
					{
						if (gtmpqPutnchar((char *)&cmdinfo->ci_data.cd_rc.gxid,
								sizeof (GlobalTransactionId), gtm_conn))
							elog(ERROR, "Error sending data");
					}
				}

				/* Finish the message. */
				Enable_Longjmp();
				if (gtmpqPutMsgEnd(gtm_conn))
					elog(ERROR, "Error finishing the message");
				Disable_Longjmp();


				/*
				 * Move the entire list to the processed command
				 */
				thrinfo->thr_processed_commands = gtm_list_concat(thrinfo->thr_processed_commands,
						thrinfo->thr_pending_commands[ii]);
				/*
				 * Free the list header of the second list, unless
				 * gtm_list_concat actually returned the second list as-is
				 * because the first list was empty
				 */
				if ((thrinfo->thr_processed_commands != thrinfo->thr_pending_commands[ii]) &&
					(thrinfo->thr_pending_commands[ii] != gtm_NIL))
					pfree(thrinfo->thr_pending_commands[ii]);
				thrinfo->thr_pending_commands[ii] = gtm_NIL;
				break;

			case MSG_SNAPSHOT_GET_MULTI:
				if (gtmpqPutInt(MSG_SNAPSHOT_GET_MULTI, sizeof (GTM_MessageType), gtm_conn) ||
					gtmpqPutInt(gtm_list_length(thrinfo->thr_pending_commands[ii]), sizeof(int), gtm_conn))
					elog(ERROR, "Error sending data");

				gtm_foreach (elem, thrinfo->thr_pending_commands[ii])
				{
					cmdinfo = (GTMProxy_CommandInfo *)gtm_lfirst(elem);
					Assert(cmdinfo->ci_mtype == ii);
					cmdinfo->ci_res_index = res_index++;
					{
						if (gtmpqPutnchar((char *)&cmdinfo->ci_data.cd_rc.gxid,
								sizeof (GlobalTransactionId), gtm_conn))
							elog(ERROR, "Error sending data");
					}
				}

				/* Finish the message. */
				Enable_Longjmp();
				if (gtmpqPutMsgEnd(gtm_conn))
					elog(ERROR, "Error finishing the message");
				Disable_Longjmp();

				/*
				 * Move the entire list to the processed command
				 */
				thrinfo->thr_processed_commands = gtm_list_concat(thrinfo->thr_processed_commands,
						thrinfo->thr_pending_commands[ii]);
				/*
				 * Free the list header of the second list, unless
				 * gtm_list_concat actually returned the second list as-is
				 * because the first list was empty
				 */
				if ((thrinfo->thr_processed_commands != thrinfo->thr_pending_commands[ii]) &&
					(thrinfo->thr_pending_commands[ii] != gtm_NIL))
					pfree(thrinfo->thr_pending_commands[ii]);
				thrinfo->thr_pending_commands[ii] = gtm_NIL;
				break;


			default:
				elog(ERROR, "This message type (%d) can not be grouped together", ii);
		}
	}
	/* Process backend disconnect messages now */
	gtm_foreach (elem, thrinfo->thr_pending_commands[MSG_BACKEND_DISCONNECT])
	{
		ereport(DEBUG1,
				(EPROTO,
				 errmsg("cleaning up client disconnection")));
		cmdinfo = (GTMProxy_CommandInfo *)gtm_lfirst(elem);
		GTMProxy_HandleDisconnect(cmdinfo->ci_conn, gtm_conn);
	}
}

/*
 * Validate the proposed data directory
 */
static void
checkDataDir(void)
{
	struct stat stat_buf;

	Assert(GTMProxyDataDir);

retry:
	if (stat(GTMProxyDataDir, &stat_buf) != 0)
	{
		if (errno == ENOENT)
		{
			if (mkdir(GTMProxyDataDir, 0700) != 0)
			{
				ereport(FATAL,
						(errno,
						 errmsg("failed to create the directory \"%s\"",
							 GTMProxyDataDir)));
			}
			goto retry;
		}
		else
			ereport(FATAL,
					(EPERM,
				 errmsg("could not read permissions of directory \"%s\": %m",
						GTMProxyDataDir)));
	}

	/* eventual chdir would fail anyway, but let's test ... */
	if (!S_ISDIR(stat_buf.st_mode))
		ereport(FATAL,
				(EINVAL,
				 errmsg("specified data directory \"%s\" is not a directory",
						GTMProxyDataDir)));

	/*
	 * Check that the directory belongs to my userid; if not, reject.
	 *
	 * This check is an essential part of the interlock that prevents two
	 * postmasters from starting in the same directory (see CreateLockFile()).
	 * Do not remove or weaken it.
	 *
	 * XXX can we safely enable this check on Windows?
	 */
#if !defined(WIN32) && !defined(__CYGWIN__)
	if (stat_buf.st_uid != geteuid())
		ereport(FATAL,
				(EINVAL,
				 errmsg("data directory \"%s\" has wrong ownership",
						GTMProxyDataDir),
				 errhint("The server must be started by the user that owns the data directory.")));
#endif
}

/*
 * Set data directory, but make sure it's an absolute path.  Use this,
 * never set DataDir directly.
 */
void
SetDataDir(void)
{
	char   *new;

	/* If presented path is relative, convert to absolute */
	new = make_absolute_path(GTMProxyDataDir);
	if (!new)
		ereport(FATAL,
				(errno,
				 errmsg("failed to set the data directory \"%s\"",
						GTMProxyDataDir)));

	if (GTMProxyDataDir)
		free(GTMProxyDataDir);

	GTMProxyDataDir = new;
}

/*
 * Change working directory to DataDir.  Most of the postmaster and backend
 * code assumes that we are in DataDir so it can use relative paths to access
 * stuff in and under the data directory.  For convenience during path
 * setup, however, we don't force the chdir to occur during SetDataDir.
 */
static void
ChangeToDataDir(void)
{
	if (chdir(GTMProxyDataDir) < 0)
		ereport(FATAL,
				(EINVAL,
				 errmsg("could not change directory to \"%s\": %m",
						GTMProxyDataDir)));
}

/*
 * Create the data directory lockfile.
 *
 * When this is called, we must have already switched the working
 * directory to DataDir, so we can just use a relative path.  This
 * helps ensure that we are locking the directory we should be.
 */
static void
CreateDataDirLockFile()
{
	CreateLockFile(GTM_PID_FILE, GTMProxyDataDir);
}

/*
 * Create a lockfile.
 *
 * filename is the name of the lockfile to create.
 * amPostmaster is used to determine how to encode the output PID.
 * isDDLock and refName are used to determine what error message to produce.
 */
static void
CreateLockFile(const char *filename, const char *refName)
{
	int			fd;
	char		buffer[MAXPGPATH + 100];
	int			ntries;
	int			len;
	int			encoded_pid;
	pid_t		other_pid;
	pid_t		my_pid = getpid();

	/*
	 * We need a loop here because of race conditions.	But don't loop forever
	 * (for example, a non-writable $PGDATA directory might cause a failure
	 * that won't go away).  100 tries seems like plenty.
	 */
	for (ntries = 0;; ntries++)
	{
		/*
		 * Try to create the lock file --- O_EXCL makes this atomic.
		 *
		 * Think not to make the file protection weaker than 0600.	See
		 * comments below.
		 */
		fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
		if (fd >= 0)
			break;				/* Success; exit the retry loop */

		/*
		 * Couldn't create the pid file. Probably it already exists.
		 */
		if ((errno != EEXIST && errno != EACCES) || ntries > 100)
			ereport(FATAL,
					(EINVAL,
					 errmsg("could not create lock file \"%s\": %m",
							filename)));

		/*
		 * Read the file to get the old owner's PID.  Note race condition
		 * here: file might have been deleted since we tried to create it.
		 */
		fd = open(filename, O_RDONLY, 0600);
		if (fd < 0)
		{
			if (errno == ENOENT)
				continue;		/* race condition; try again */
			ereport(FATAL,
					(EINVAL,
					 errmsg("could not open lock file \"%s\": %m",
							filename)));
		}
		if ((len = read(fd, buffer, sizeof(buffer) - 1)) < 0)
			ereport(FATAL,
					(EINVAL,
					 errmsg("could not read lock file \"%s\": %m",
							filename)));
		close(fd);

		buffer[len] = '\0';
		encoded_pid = atoi(buffer);
		other_pid = (pid_t) encoded_pid;

		if (other_pid <= 0)
			elog(FATAL, "bogus data in lock file \"%s\": \"%s\"",
				 filename, buffer);

		/*
		 * Check to see if the other process still exists
		 *
		 * If the PID in the lockfile is our own PID or our parent's PID, then
		 * the file must be stale (probably left over from a previous system
		 * boot cycle).  We need this test because of the likelihood that a
		 * reboot will assign exactly the same PID as we had in the previous
		 * reboot.	Also, if there is just one more process launch in this
		 * reboot than in the previous one, the lockfile might mention our
		 * parent's PID.  We can reject that since we'd never be launched
		 * directly by a competing postmaster.	We can't detect grandparent
		 * processes unfortunately, but if the init script is written
		 * carefully then all but the immediate parent shell will be
		 * root-owned processes and so the kill test will fail with EPERM.
		 *
		 * We can treat the EPERM-error case as okay because that error
		 * implies that the existing process has a different userid than we
		 * do, which means it cannot be a competing postmaster.  A postmaster
		 * cannot successfully attach to a data directory owned by a userid
		 * other than its own.	(This is now checked directly in
		 * checkDataDir(), but has been true for a long time because of the
		 * restriction that the data directory isn't group- or
		 * world-accessible.)  Also, since we create the lockfiles mode 600,
		 * we'd have failed above if the lockfile belonged to another userid
		 * --- which means that whatever process kill() is reporting about
		 * isn't the one that made the lockfile.  (NOTE: this last
		 * consideration is the only one that keeps us from blowing away a
		 * Unix socket file belonging to an instance of Postgres being run by
		 * someone else, at least on machines where /tmp hasn't got a
		 * stickybit.)
		 *
		 * Windows hasn't got getppid(), but doesn't need it since it's not
		 * using real kill() either...
		 *
		 * Normally kill() will fail with ESRCH if the given PID doesn't
		 * exist.
		 */
		if (other_pid != my_pid
#ifndef WIN32
			&& other_pid != getppid()
#endif
			)
		{
			if (kill(other_pid, 0) == 0 ||
				(errno != ESRCH && errno != EPERM))
			{
				/* lockfile belongs to a live process */
				ereport(FATAL,
						(EINVAL,
						 errmsg("lock file \"%s\" already exists",
								filename),
						  errhint("Is another GTM proxy (PID %d) running in data directory \"%s\"?",
								  (int) other_pid, refName)));
			}
		}

		/*
		 * Looks like nobody's home.  Unlink the file and try again to create
		 * it.	Need a loop because of possible race condition against other
		 * would-be creators.
		 */
		if (unlink(filename) < 0)
			ereport(FATAL,
					(EACCES,
					 errmsg("could not remove old lock file \"%s\": %m",
							filename),
					 errhint("The file seems accidentally left over, but "
						   "it could not be removed. Please remove the file "
							 "by hand and try again.")));
	}

	/*
	 * Successfully created the file, now fill it.
	 */
	snprintf(buffer, sizeof(buffer), "%d\n%s\n",
			 (int) my_pid, GTMProxyDataDir);
	errno = 0;
	if (write(fd, buffer, strlen(buffer)) != strlen(buffer))
	{
		int			save_errno = errno;

		close(fd);
		unlink(filename);
		/* if write didn't set errno, assume problem is no disk space */
		errno = save_errno ? save_errno : ENOSPC;
		ereport(FATAL,
				(EACCES,
				 errmsg("could not write lock file \"%s\": %m", filename)));
	}
	if (close(fd))
	{
		int			save_errno = errno;

		unlink(filename);
		errno = save_errno;
		ereport(FATAL,
				(EACCES,
				 errmsg("could not write lock file \"%s\": %m", filename)));
	}

}

/*
 * Create the opts file
 */
static bool
CreateOptsFile(int argc, char *argv[])
{
	FILE	   *fp;
	int			i;

#define OPTS_FILE	"gtm_proxy.opts"

	if ((fp = fopen(OPTS_FILE, "w")) == NULL)
	{
		elog(LOG, "could not create file \"%s\": %m", OPTS_FILE);
		return false;
	}

	for (i = 1; i < argc; i++)
		fprintf(fp, " \"%s\"", argv[i]);
	fputs("\n", fp);

	if (fclose(fp))
	{
		elog(LOG, "could not write file \"%s\": %m", OPTS_FILE);
		return false;
	}

	return true;
}

/* delete pid file */
static void
DeleteLockFile(const char *filename)
{
	if (unlink(filename) < 0)
		ereport(FATAL,
				(EACCES,
				 errmsg("could not remove old lock file \"%s\": %m",
						filename),
				 errhint("The file seems accidentally left over, but "
						 "it could not be removed. Please remove the file "
						 "by hand and try again.")));
}

/*
 * Unregister Proxy on GTM
 */
static void
UnregisterProxy(void)
{
	GTM_PGXCNodeType type = GTM_NODE_GTM_PROXY;
	GTM_Result *res = NULL;
	time_t finish_time;

	if (!master_conn || GTMPQstatus(master_conn) != CONNECTION_OK)
		master_conn = ConnectGTM();
	if (!master_conn || GTMProxyNodeName == NULL)
		goto failed;

	if (gtmpqPutMsgStart('C', true, master_conn) ||
		gtmpqPutInt(MSG_NODE_UNREGISTER, sizeof (GTM_MessageType), master_conn) ||
		gtmpqPutnchar((char *)&type, sizeof(GTM_PGXCNodeType), master_conn) ||
		/* Node name length */
		gtmpqPutInt(strlen(GTMProxyNodeName), sizeof (GTM_StrLen), master_conn) ||
		/* Node name (var-len) */
		gtmpqPutnchar(GTMProxyNodeName, strlen(GTMProxyNodeName), master_conn) )
		goto failed;

	/* Finish the message. */
	if (gtmpqPutMsgEnd(master_conn))
		goto failed;

	/* Flush to ensure backend gets it. */
	if (gtmpqFlush(master_conn))
		goto failed;

	finish_time = time(NULL) + PROXY_CLIENT_TIMEOUT;
	if (gtmpqWaitTimed(true, false, master_conn, finish_time) ||
		gtmpqReadData(master_conn) < 0)
		goto failed;

	if ((res = GTMPQgetResult(master_conn)) == NULL)
		goto failed;

	/* Check on node type and node name */
	if (res->gr_status == GTM_RESULT_OK)
	{
		Assert(res->gr_resdata.grd_node.type == type);
		Assert( strcmp(res->gr_resdata.grd_node.node_name, GTMProxyNodeName) == 0 );
	}

	/* Disconnect cleanly as Proxy is shutting down */
	GTMPQfinish(master_conn);

	return;

failed:
	/*
	 * We don't deliberately write an ERROR here to ensure that proxy shutdown
	 * proceeds to the end. Without that we have a danger of leaving behind a
	 * stale PID file, thus causing gtm_ctl stop to wait forever for the proxy
	 * to shutdown
	 *
	 * XXX This can happen when GTM restarts, clearing existing registration
	 * information. See if this needs to fixed
	 */
	return elog(LOG, "can not Unregister Proxy on GTM");
}

/*
 * Register Proxy on GTM
 *
 * If reconnect is specified, then existing connection is closed
 * and the target GTM is taken from NewGTMServerHost and
 * NewGTMServerPortNumber.
 */
static void
RegisterProxy(bool is_reconnect)
{
	GTM_PGXCNodeType type = GTM_NODE_GTM_PROXY;
	GTM_PGXCNodePort port = (GTM_PGXCNodePort) GTMProxyPortNumber;
	GTM_Result *res = NULL;
	char proxyname[] = "";
	time_t finish_time;
	MemoryContext old_mcxt = NULL;

	if (is_reconnect)
	{
		elog(NOTICE,
			 "Reconnect to new GTM, hostname=%s, port=%d",
			 NewGTMServerHost, NewGTMServerPortNumber);
		/*
		 * Now reconnect.   Close the exising connection
		 * and update the target host and port.
		 * First, change the memory context to TopMemoryContext
		 */
		old_mcxt = MemoryContextSwitchTo(TopMemoryContext);

		/* Change the target to new GTM */
		GTMPQfinish(master_conn);
		GTMServerHost = NewGTMServerHost;
		GTMServerPortNumber = NewGTMServerPortNumber;
	}

	master_conn = ConnectGTM();
	if (!master_conn || GTMProxyNodeName == NULL)
		goto failed;

	/*
	 * As this node is itself a Proxy it registers 0 as Proxy ID on GTM
	 * as it doesn't go through any other proxy.
	 */
	if (gtmpqPutMsgStart('C', true, master_conn) ||
		gtmpqPutInt(MSG_NODE_REGISTER, sizeof (GTM_MessageType), master_conn) ||
		gtmpqPutnchar((char *)&type, sizeof(GTM_PGXCNodeType), master_conn) ||
		gtmpqPutInt((int)strlen(GTMProxyNodeName), sizeof(int), master_conn) ||
		gtmpqPutnchar(GTMProxyNodeName, (int)strlen(GTMProxyNodeName), master_conn) ||
		gtmpqPutInt((int)strlen(ListenAddresses), sizeof(int), master_conn) ||
		gtmpqPutnchar(ListenAddresses, (int)strlen(ListenAddresses), master_conn) ||
		gtmpqPutnchar((char *)&port, sizeof(GTM_PGXCNodePort), master_conn) ||
		/* PGXCTODO : previously ZERO was used if the node was itself proxy, now its name is empty. */
		gtmpqPutInt((int)strlen(proxyname), sizeof(int), master_conn) ||
		gtmpqPutnchar(proxyname, (int)strlen(proxyname), master_conn) ||
		gtmpqPutInt((int)strlen(GTMProxyDataDir), 4, master_conn) ||
		gtmpqPutnchar(GTMProxyDataDir, strlen(GTMProxyDataDir), master_conn)||
		gtmpqPutInt(NODE_CONNECTED, sizeof(GTM_PGXCNodeStatus), master_conn))
		goto failed;

	/* Finish the message. */
	if (gtmpqPutMsgEnd(master_conn))
		goto failed;

	/* Flush to ensure backend gets it. */
	if (gtmpqFlush(master_conn))
		goto failed;

	finish_time = time(NULL) + PROXY_CLIENT_TIMEOUT;
	if (gtmpqWaitTimed(true, false, master_conn, finish_time) ||
		gtmpqReadData(master_conn) < 0)
	{
		elog(ERROR, "Cannot read data.");
		goto failed;
	}

	if ((res = GTMPQgetResult(master_conn)) == NULL)
	{
		elog(ERROR, "Cannot get result.");
		goto failed;
	}

	if (res->gr_status == GTM_RESULT_OK)
	{
		Assert(res->gr_resdata.grd_node.type == type);
		Assert( strcmp(res->gr_resdata.grd_node.node_name, GTMProxyNodeName) == 0 );
	}

	/* If reconnect, restore the old memory context */
	if (is_reconnect)
		MemoryContextSwitchTo(old_mcxt);
	return;

failed:
	elog(ERROR, "can not register Proxy on GTM");
}

static GTM_Conn*
ConnectGTM(void)
{
	char conn_str[256];
	GTM_Conn *conn;

	sprintf(conn_str, "host=%s port=%d node_name=%s remote_type=%d postmaster=1",
			GTMServerHost, GTMServerPortNumber, GTMProxyNodeName, GTM_NODE_GTM_PROXY_POSTMASTER);

	conn = PQconnectGTM(conn_str);
	if (GTMPQstatus(conn) != CONNECTION_OK)
	{
		int save_errno = errno;

		elog(ERROR, "can not connect to GTM");

		errno = save_errno;

		GTMPQfinish(conn);
		conn = NULL;
	}

	return conn;
}

/*
 * Release backup command data
 */
static void ReleaseCmdBackup(GTMProxy_CommandInfo *cmdinfo)
{
	GTMProxy_ConnID connIdx = cmdinfo->ci_conn->con_id;

	GetMyThreadInfo->thr_any_backup[connIdx] = FALSE;
	GetMyThreadInfo->thr_qtype[connIdx] = 0;
	resetStringInfo(&(GetMyThreadInfo->thr_inBufData[connIdx]));
}


static void
workerThreadReconnectToGTM(void)
{
	char gtm_connect_string[1024];
	MemoryContext   oldContext;
	uint32 saveMyClientId = 0;

	/*
	 * First of all, we should acquire reconnect control lock in READ mode
	 * to wait for the main thread to finish reconnect.
	 */
	GTM_RWLockAcquire(&ReconnectControlLock, GTM_LOCKMODE_READ);
	GTM_RWLockRelease(&ReconnectControlLock);	/* The lock not needed any longer */
	PG_SETMASK(&UnBlockSig);

	/* Disconnect the current connection and re-connect to the new GTM */
	oldContext = MemoryContextSwitchTo(TopMostMemoryContext);

	/*
	 * Before cleaning the old connection, remember the client identifier
	 * issued to us by the old GTM master. We send that identifier back to the
	 * new master so that it can re-establish our association with any
	 * transactions currently open by us
	 */
	if (GetMyThreadInfo->thr_gtm_conn)
	{
		saveMyClientId = GetMyThreadInfo->thr_gtm_conn->my_id;
		GTMPQfinish(GetMyThreadInfo->thr_gtm_conn);
	}

	sprintf(gtm_connect_string, "host=%s port=%d node_name=%s remote_type=%d client_id=%u",
			GTMServerHost, GTMServerPortNumber, GTMProxyNodeName,
			GTM_NODE_GTM_PROXY, saveMyClientId);
	elog(DEBUG1, "Worker thread connecting to %s", gtm_connect_string);
	GetMyThreadInfo->thr_gtm_conn = PQconnectGTM(gtm_connect_string);

	if (GetMyThreadInfo->thr_gtm_conn == NULL)
		elog(FATAL, "Worker thread GTM connection failed.");
	elog(DEBUG1, "Worker thread connection done.");

	MemoryContextSwitchTo(oldContext);

	/* Initialize the command processing */
	GetMyThreadInfo->reconnect_issued = FALSE;
}