-
Notifications
You must be signed in to change notification settings - Fork 92
/
pool_stream.c
1404 lines (1222 loc) · 29.1 KB
/
pool_stream.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
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
/* -*-pgsql-c-*- */
/*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2023 PgPool Global Development Group
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that copyright notice and this permission
* notice appear in supporting documentation, and that the name of the
* author not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. The author makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* pool_stream.c: stream I/O modules
*
*/
#include "config.h"
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "pool.h"
#include "pool_config.h"
#include "utils/elog.h"
#include "utils/palloc.h"
#include "utils/memutils.h"
#include "utils/socket_stream.h"
#include "utils/pool_stream.h"
#include "utils/pool_ssl.h"
#include "main/pool_internal_comms.h"
static int mystrlen(char *str, int upper, int *flag);
static int mystrlinelen(char *str, int upper, int *flag);
static int save_pending_data(POOL_CONNECTION * cp, void *data, int len);
static int consume_pending_data(POOL_CONNECTION * cp, void *data, int len);
static MemoryContext SwitchToConnectionContext(bool backend_connection);
#ifdef DEBUG
static void dump_buffer(char *buf, int len);
#endif
static int pool_write_flush(POOL_CONNECTION * cp, void *buf, int len);
/* timeout sec for pool_check_fd */
static int timeoutsec = -1;
static MemoryContext
SwitchToConnectionContext(bool backend_connection)
{
/*
* backend connection can live as long as process life, while the frontend
* connection is only for one session life. So we create backend
* connection in long living memory context
*/
if (backend_connection)
return MemoryContextSwitchTo(TopMemoryContext);
else
return MemoryContextSwitchTo(ProcessLoopContext);
}
/*
* open read/write file descriptors.
* returns POOL_CONNECTION on success otherwise NULL.
*/
POOL_CONNECTION *
pool_open(int fd, bool backend_connection)
{
POOL_CONNECTION *cp;
MemoryContext oldContext = SwitchToConnectionContext(backend_connection);
cp = (POOL_CONNECTION *) palloc0(sizeof(POOL_CONNECTION));
/* initialize write buffer */
cp->wbuf = palloc(WRITEBUFSZ);
cp->wbufsz = WRITEBUFSZ;
cp->wbufpo = 0;
/* initialize pending data buffer */
cp->hp = palloc(READBUFSZ);
cp->bufsz = READBUFSZ;
cp->po = 0;
cp->len = 0;
cp->sbuf = NULL;
cp->sbufsz = 0;
cp->buf2 = NULL;
cp->bufsz2 = 0;
cp->buf3 = NULL;
cp->bufsz3 = 0;
cp->fd = fd;
cp->socket_state = POOL_SOCKET_VALID;
MemoryContextSwitchTo(oldContext);
return cp;
}
/*
* close read/write file descriptors.
*/
void
pool_close(POOL_CONNECTION * cp)
{
/*
* shutdown connection to the client so that pgpool is not blocked
*/
if (!cp->isbackend)
shutdown(cp->fd, 1);
close(cp->fd);
cp->socket_state = POOL_SOCKET_CLOSED;
pfree(cp->wbuf);
pfree(cp->hp);
if (cp->sbuf)
pfree(cp->sbuf);
if (cp->buf2)
pfree(cp->buf2);
if (cp->buf3)
pfree(cp->buf3);
pool_discard_params(&cp->params);
pool_ssl_close(cp);
pfree(cp);
}
void
pool_read_with_error(POOL_CONNECTION * cp, void *buf, int len,
const char *err_context)
{
if (pool_read(cp, buf, len) < 0)
{
ereport(ERROR,
(errmsg("failed to read data of length %d from DB node: %d", len, cp->db_node_id),
errdetail("error occurred when reading: %s", err_context ? err_context : "")));
}
}
/*
* read len bytes from cp
* returns 0 on success otherwise throws an ereport.
*/
int
pool_read(POOL_CONNECTION * cp, void *buf, int len)
{
static char readbuf[READBUFSZ];
int consume_size;
int readlen;
consume_size = consume_pending_data(cp, buf, len);
len -= consume_size;
buf += consume_size;
while (len > 0)
{
/*
* If select(2) timeout is disabled, there's no need to call
* pool_check_fd().
*/
if (pool_get_timeout() >= 0 && pool_check_fd(cp))
{
if (!IS_MAIN_NODE_ID(cp->db_node_id) && (getpid() != mypid))
{
ereport(FATAL,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("data is not ready in DB node")));
}
else
{
ereport(ERROR,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("pool_check_fd call failed with error \"%m\"")));
}
}
if (cp->ssl_active > 0)
{
readlen = pool_ssl_read(cp, readbuf, READBUFSZ);
}
else
{
readlen = read(cp->fd, readbuf, READBUFSZ);
if (cp->isbackend)
{
ereport(DEBUG5,
(errmsg("pool_read: read %d bytes from backend %d",
readlen, cp->db_node_id)));
#ifdef DEBUG
dump_buffer(readbuf, readlen);
#endif
}
}
if (readlen == -1)
{
if (processType == PT_HEALTH_CHECK && health_check_timer_expired && errno == EINTR)
{
ereport(ERROR,
(errmsg("health check timed out while waiting for reading data")));
return -1;
}
if (errno == EINTR || errno == EAGAIN)
{
ereport(DEBUG5,
(errmsg("read on socket failed with error :\"%m\""),
errdetail("retrying...")));
continue;
}
cp->socket_state = POOL_SOCKET_ERROR;
if (cp->isbackend)
{
if (cp->con_info && cp->con_info->swallow_termination == 1)
{
cp->con_info->swallow_termination = 0;
ereport(FATAL,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("pg_terminate_backend was called on the backend")));
}
/*
* if failover_on_backend_error is true, then trigger
* failover
*/
if (pool_config->failover_on_backend_error)
{
notice_backend_error(cp->db_node_id, REQ_DETAIL_SWITCHOVER);
/* If we are in the main process, we will not exit */
child_exit(POOL_EXIT_AND_RESTART);
ereport(ERROR,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("socket read failed with error \"%m\"")));
}
else
{
ereport(ERROR,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("socket read failed with error \"%m\"")));
}
}
else
{
ereport(FRONTEND_DEBUG,
(errmsg("unable to read data from frontend"),
errdetail("socket read failed with error \"%m\"")));
}
}
else if (readlen == 0)
{
cp->socket_state = POOL_SOCKET_EOF;
if (cp->isbackend)
{
if (processType == PT_MAIN || processType == PT_HEALTH_CHECK)
ereport(ERROR,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("EOF encountered with backend")));
ereport(FATAL,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("EOF encountered with backend")));
}
else
{
/*
* if backend offers authentication method, frontend could
* close connection
*/
ereport(FRONTEND_DEBUG,
(errmsg("unable to read data from frontend"),
errdetail("EOF encountered with frontend")));
}
}
if (len < readlen)
{
/* overrun. we need to save remaining data to pending buffer */
save_pending_data(cp, readbuf + len, readlen - len);
memmove(buf, readbuf, len);
break;
}
memmove(buf, readbuf, readlen);
buf += readlen;
len -= readlen;
}
return 0;
}
/*
* read exactly len bytes from cp
* returns buffer address on success otherwise NULL.
*/
char *
pool_read2(POOL_CONNECTION * cp, int len)
{
char *buf;
int req_size;
int alloc_size;
int consume_size;
int readlen;
MemoryContext oldContext = SwitchToConnectionContext(cp->isbackend);
req_size = cp->len + len;
if (req_size > cp->bufsz2)
{
alloc_size = ((req_size + 1) / READBUFSZ + 1) * READBUFSZ;
cp->buf2 = repalloc(cp->buf2, alloc_size);
cp->bufsz2 = alloc_size;
}
buf = cp->buf2;
consume_size = consume_pending_data(cp, buf, len);
len -= consume_size;
buf += consume_size;
while (len > 0)
{
/*
* If select(2) timeout is disabled, there's no need to call
* pool_check_fd().
*/
if (pool_get_timeout() >= 0 && pool_check_fd(cp))
{
if (!IS_MAIN_NODE_ID(cp->db_node_id))
{
ereport(FATAL,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("data is not ready in DB node")));
}
else
{
ereport(ERROR,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("pool_check_fd call failed with error \"%m\"")));
}
}
if (cp->ssl_active > 0)
{
readlen = pool_ssl_read(cp, buf, len);
}
else
{
readlen = read(cp->fd, buf, len);
if (cp->isbackend)
ereport(DEBUG5,
(errmsg("pool_read2: read %d bytes from backend %d",
readlen, cp->db_node_id)));
}
if (readlen == -1)
{
if (errno == EINTR || errno == EAGAIN)
{
ereport(DEBUG5,
(errmsg("read on socket failed with error :\"%m\""),
errdetail("retrying...")));
continue;
}
cp->socket_state = POOL_SOCKET_ERROR;
if (cp->isbackend)
{
if (cp->con_info && cp->con_info->swallow_termination == 1)
{
cp->con_info->swallow_termination = 0;
ereport(FATAL,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("pg_terminate_backend was called on the backend")));
}
/*
* if failover_on_backend_error is true, then trigger
* failover
*/
if (pool_config->failover_on_backend_error)
{
notice_backend_error(cp->db_node_id, REQ_DETAIL_SWITCHOVER);
child_exit(POOL_EXIT_AND_RESTART);
/* we are in main process */
ereport(ERROR,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("socket read failed with error \"%m\"")));
}
else
{
ereport(ERROR,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("do not failover because failover_on_backend_error is off")));
}
}
else
{
ereport(ERROR,
(errmsg("unable to read data from frontend"),
errdetail("socket read function returned -1")));
}
}
else if (readlen == 0)
{
cp->socket_state = POOL_SOCKET_EOF;
if (cp->isbackend)
{
ereport(ERROR,
(errmsg("unable to read data from backend"),
errdetail("EOF read on socket")));
}
else
{
/*
* if backend offers authentication method, frontend could
* close connection
*/
ereport(ERROR,
(errmsg("unable to read data from frontend"),
errdetail("EOF read on socket")));
}
}
buf += readlen;
len -= readlen;
}
MemoryContextSwitchTo(oldContext);
return cp->buf2;
}
/*
* write len bytes to cp the write buffer.
* returns 0 on success otherwise -1.
*/
int
pool_write_noerror(POOL_CONNECTION * cp, void *buf, int len)
{
if (len < 0)
return -1;
if (cp->no_forward)
return 0;
if (len == 1 && cp->isbackend)
{
char c;
c = ((char *) buf)[0];
ereport(DEBUG5,
(errmsg("pool_write: to backend: %d kind:%c", cp->db_node_id, c)));
}
if (!cp->isbackend)
{
char c;
c = ((char *) buf)[0];
if (len == 1)
ereport(DEBUG5,
(errmsg("pool_write: to frontend: kind:%c po:%d", c, cp->wbufpo)));
else
ereport(DEBUG5,
(errmsg("pool_write: to frontend: length:%d po:%d", len, cp->wbufpo)));
}
while (len > 0)
{
int remainder = WRITEBUFSZ - cp->wbufpo;
/*
* If requested data cannot be added to the write buffer, flush the
* buffer and directly write the requested data. This could avoid
* unwanted write in the middle of message boundary.
*/
if (remainder < len)
{
if (pool_flush_it(cp) == -1)
return -1;
if (pool_write_flush(cp, buf, len) < 0)
return -1;
return 0;
}
if (cp->wbufpo >= WRITEBUFSZ)
{
/*
* Write buffer is full. so flush buffer. wbufpo is reset in
* pool_flush_it().
*/
if (pool_flush_it(cp) == -1)
return -1;
remainder = WRITEBUFSZ;
}
/* check buffer size */
remainder = Min(remainder, len);
memcpy(cp->wbuf + cp->wbufpo, buf, remainder);
cp->wbufpo += remainder;
buf += remainder;
len -= remainder;
}
return 0;
}
/*
* write len bytes to cp the write buffer.
* returns 0 on success otherwise ereport.
*/
int
pool_write(POOL_CONNECTION * cp, void *buf, int len)
{
if (len < 0)
ereport(ERROR,
(errmsg("unable to write data to %s", cp->isbackend ? "backend" : "frontend"),
errdetail("invalid data size: %d", len)));
if (pool_write_noerror(cp, buf, len))
ereport(ERROR,
(errmsg("unable to write data to %s", cp->isbackend ? "backend" : "frontend"),
errdetail("pool_flush failed")));
return 0;
}
/*
* Direct write.
* This function does not throws an ereport in case of an error
*/
static int
pool_write_flush(POOL_CONNECTION * cp, void *buf, int len)
{
int sts;
int wlen;
int offset;
wlen = len;
ereport(DEBUG5,
(errmsg("pool_write_flush: write size: %d", wlen)));
if (wlen == 0)
{
return 0;
}
offset = 0;
for (;;)
{
errno = 0;
if (cp->ssl_active > 0)
{
sts = pool_ssl_write(cp, buf + offset, wlen);
}
else
{
sts = write(cp->fd, buf + offset, wlen);
}
if (sts >= 0)
{
wlen -= sts;
if (wlen == 0)
{
/* write completed */
break;
}
else if (wlen < 0)
{
ereport(WARNING,
(errmsg("pool_write_flush: invalid write size %d", sts)));
return -1;
}
else
{
/* need to write remaining data */
ereport(DEBUG5,
(errmsg("pool_write_flush: write retry: %d", wlen)));
offset += sts;
continue;
}
}
else if (errno == EAGAIN || errno == EINTR)
{
continue;
}
else
{
/*
* If this is the backend stream, report error. Otherwise just
* report debug message.
*/
if (cp->isbackend)
ereport(WARNING,
(errmsg("write on backend %d failed with error :\"%m\"", cp->db_node_id),
errdetail("while trying to write data from offset: %d wlen: %d", offset, wlen)));
else
ereport(DEBUG5,
(errmsg("write on frontend failed with error :\"%m\""),
errdetail("while trying to write data from offset: %d wlen: %d", offset, wlen)));
return -1;
}
}
return 0;
}
/*
* flush write buffer
* This function does not throws an ereport in case of an error
*/
int
pool_flush_it(POOL_CONNECTION * cp)
{
int sts;
int wlen;
int offset;
wlen = cp->wbufpo;
ereport(DEBUG5,
(errmsg("pool_flush_it: flush size: %d", wlen)));
if (wlen == 0)
{
return 0;
}
offset = 0;
for (;;)
{
errno = 0;
if (cp->ssl_active > 0)
{
sts = pool_ssl_write(cp, cp->wbuf + offset, wlen);
}
else
{
sts = write(cp->fd, cp->wbuf + offset, wlen);
}
if (sts >= 0)
{
wlen -= sts;
if (wlen == 0)
{
/* write completed */
break;
}
else if (wlen < 0)
{
ereport(WARNING,
(errmsg("pool_flush_it: invalid write size %d", sts)));
cp->wbufpo = 0;
return -1;
}
else
{
/* need to write remaining data */
ereport(DEBUG5,
(errmsg("pool_flush_it: write retry: %d", wlen)));
offset += sts;
continue;
}
}
else if (errno == EAGAIN || errno == EINTR)
{
continue;
}
else
{
/*
* If this is the backend stream, report error. Otherwise just
* report debug message.
*/
if (cp->isbackend)
ereport(WARNING,
(errmsg("write on backend %d failed with error :\"%m\"", cp->db_node_id),
errdetail("while trying to write data from offset: %d wlen: %d", offset, wlen)));
else
ereport(DEBUG5,
(errmsg("write on frontend failed with error :\"%m\""),
errdetail("while trying to write data from offset: %d wlen: %d", offset, wlen)));
cp->wbufpo = 0;
return -1;
}
}
cp->wbufpo = 0;
return 0;
}
/*
* flush write buffer and degenerate/failover if error occurs
*/
int
pool_flush(POOL_CONNECTION * cp)
{
if (pool_flush_it(cp) == -1)
{
if (cp->isbackend)
{
if (cp->con_info && cp->con_info->swallow_termination == 1)
{
cp->con_info->swallow_termination = 0;
ereport(FATAL,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("pg_terminate_backend was called on the backend")));
}
/* if failover_on_backend_error is true, then trigger failover */
if (pool_config->failover_on_backend_error)
{
notice_backend_error(cp->db_node_id, REQ_DETAIL_SWITCHOVER);
ereport(LOG,
(errmsg("unable to flush data to backend"),
errdetail("do not failover because I am the main process")));
child_exit(POOL_EXIT_AND_RESTART);
return -1;
}
else
{
ereport(ERROR,
(errmsg("unable to flush data to backend"),
errdetail("do not failover because failover_on_backend_error is off")));
}
}
else
{
/*
* If we are in replication mode, we need to continue the
* processing with backends to keep consistency among backends,
* thus ignore error.
*/
if (REPLICATION)
ereport(NOTICE,
(errmsg("unable to flush data to frontend"),
errdetail("pgpool is in replication mode, ignoring error to keep consistency among backends")));
else
ereport(FRONTEND_DEBUG,
(errmsg("unable to flush data to frontend")));
}
}
return 0;
}
/*
* same as pool_flush() but returns -ve value instead of ereport in case of failure
*/
int
pool_flush_noerror(POOL_CONNECTION * cp)
{
if (pool_flush_it(cp) == -1)
{
if (cp->isbackend)
{
if (cp->con_info && cp->con_info->swallow_termination == 1)
{
cp->con_info->swallow_termination = 0;
ereport(FATAL,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("pg_terminate_backend was called on the backend")));
}
/* if fail_over_on_backend_error is true, then trigger failover */
if (pool_config->failover_on_backend_error)
{
notice_backend_error(cp->db_node_id, REQ_DETAIL_SWITCHOVER);
child_exit(POOL_EXIT_AND_RESTART);
ereport(LOG,
(errmsg("unable to flush data to backend"),
errdetail("do not failover because I am the main process")));
return -1;
}
else
{
ereport(LOG,
(errmsg("unable to flush data to backend"),
errdetail("do not failover because failover_on_backend_error is off")));
return -1;
}
}
else
{
/*
* If we are in replication mode, we need to continue the
* processing with backends to keep consistency among backends,
* thus ignore error.
*/
if (REPLICATION)
return 0;
else
return -1;
}
}
return 0;
}
/*
* combo of pool_write and pool_flush
*/
void
pool_write_and_flush(POOL_CONNECTION * cp, void *buf, int len)
{
pool_write(cp, buf, len);
pool_flush(cp);
}
/*
* same as pool_write_and_flush() but does not throws ereport when error occurs
*/
int
pool_write_and_flush_noerror(POOL_CONNECTION * cp, void *buf, int len)
{
int ret;
ret = pool_write_noerror(cp, buf, len);
if (ret == 0)
return pool_flush_noerror(cp);
return ret;
}
/*
* read a string until EOF or NULL is encountered.
* if line is not 0, read until new line is encountered.
*/
char *
pool_read_string(POOL_CONNECTION * cp, int *len, int line)
{
int readp;
int readsize;
int readlen;
int strlength;
int flag;
int consume_size;
#ifdef DEBUG
static char pbuf[READBUFSZ];
#endif
*len = 0;
readp = 0;
/* initialize read buffer */
if (cp->sbufsz == 0)
{
MemoryContext oldContext = SwitchToConnectionContext(cp->isbackend);
cp->sbuf = palloc(READBUFSZ);
MemoryContextSwitchTo(oldContext);
cp->sbufsz = READBUFSZ;
*cp->sbuf = '\0';
}
/* any pending data? */
if (cp->len)
{
if (line)
strlength = mystrlinelen(cp->hp + cp->po, cp->len, &flag);
else
strlength = mystrlen(cp->hp + cp->po, cp->len, &flag);
/* buffer is too small? */
if ((strlength + 1) > cp->sbufsz)
{
MemoryContext oldContext = SwitchToConnectionContext(cp->isbackend);
cp->sbufsz = ((strlength + 1) / READBUFSZ + 1) * READBUFSZ;
cp->sbuf = repalloc(cp->sbuf, cp->sbufsz);
MemoryContextSwitchTo(oldContext);
}
/* consume pending and save to read string buffer */
consume_size = consume_pending_data(cp, cp->sbuf, strlength);
*len = strlength;
/* is the string null terminated? */
if (consume_size == strlength && !flag)
{
/*
* not null or line terminated. we need to read more since we have
* not encountered NULL or new line yet
*/
readsize = cp->sbufsz - strlength;
readp = strlength;
}
else
{
ereport(DEBUG5,
(errmsg("reading string data"),
errdetail("read all from pending data. po:%d len:%d",
cp->po, cp->len)));
return cp->sbuf;
}
}
else
{
readsize = cp->sbufsz;
}
for (;;)
{
if (pool_check_fd(cp))
{
if (!IS_MAIN_NODE_ID(cp->db_node_id))
{
ereport(FATAL,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("data is not ready in DB node")));
}
else
{
ereport(ERROR,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("pool_check_fd call failed with error \"%m\"")));
}
}
if (cp->ssl_active > 0)
{
readlen = pool_ssl_read(cp, cp->sbuf + readp, readsize);
}
else
{
readlen = read(cp->fd, cp->sbuf + readp, readsize);
}
if (readlen == -1)
{
cp->socket_state = POOL_SOCKET_ERROR;
if (cp->isbackend)
{
if (cp->con_info && cp->con_info->swallow_termination == 1)
{
cp->con_info->swallow_termination = 0;
ereport(FATAL,
(errmsg("unable to read data from DB node %d", cp->db_node_id),
errdetail("pg_terminate_backend was called on the backend")));
}
notice_backend_error(cp->db_node_id, REQ_DETAIL_SWITCHOVER);
child_exit(POOL_EXIT_AND_RESTART);
ereport(ERROR,
(errmsg("unable to read data from frontend"),
errdetail("socket read function returned -1")));
}
else
{
ereport(ERROR,
(errmsg("unable to read data from frontend"),
errdetail("socket read function returned -1")));
}
}
else if (readlen == 0) /* EOF detected */
{
/*