Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tr6ce-0005ec-8K for pgsql-hackers@arkaria.postgresql.org; Fri, 04 Jan 2013 12:43:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Tr6cd-0006ta-7T for pgsql-hackers@arkaria.postgresql.org; Fri, 04 Jan 2013 12:43:39 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tr6cb-0006tO-AH for pgsql-hackers@postgresql.org; Fri, 04 Jan 2013 12:43:37 +0000 Received: from szxga01-in.huawei.com ([119.145.14.64]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tr6cX-0001SO-QO for pgsql-hackers@postgresql.org; Fri, 04 Jan 2013 12:43:36 +0000 Received: from 172.24.2.119 (EHLO szxeml207-edg.china.huawei.com) ([172.24.2.119]) by szxrg01-dlp.huawei.com (MOS 4.3.4-GA FastPath queued) with ESMTP id AVK69694; Fri, 04 Jan 2013 20:43:24 +0800 (CST) Received: from SZXEML414-HUB.china.huawei.com (10.82.67.153) by szxeml207-edg.china.huawei.com (172.24.2.56) with Microsoft SMTP Server (TLS) id 14.1.323.3; Fri, 4 Jan 2013 20:43:21 +0800 Received: from blrprnc07ns (10.18.96.96) by SZXEML414-HUB.china.huawei.com (10.82.67.153) with Microsoft SMTP Server id 14.1.323.3; Fri, 4 Jan 2013 20:43:13 +0800 From: Hari Babu To: "'Boszormenyi Zoltan'" CC: "'Fujii Masao'" , "'Heikki Linnakangas'" , , "'Amit kapila'" References: <6C0B27F7206C9E4CA54AE035729E9C38285337FF@szxeml509-mbs> <506972B9.6060104@vmware.com> <6C0B27F7206C9E4CA54AE035729E9C38285358F7@szxeml509-mbs> <506AA530.30106@vmware.com> <6C0B27F7206C9E4CA54AE035729E9C382853645A@szxeml509-mbs> <507597E2.8090701@vmware.com> <5076DD38.8020804@vmware.com> <507BE1AD.2060709@vmware.com> <507e9adc.43db440a.5351.fffff4d0SMTPIN_ADDED@mx.google.com> <6C0B27F7206C9E4CA54AE035729E9C382853BBED@szxeml509-mbs> <509B6E66.9070408@vmware.com> <509c9cd1.494f420a.3fc7.4362SMTPIN_ADDED@mx.google.com>, <6C0B27F7206C9E4CA54AE035729E9C38285499FA@szxeml509-mbx> <50E31370.5030405@cybertec.at> In-Reply-To: Subject: Re: Review of "pg_basebackup and pg_receivexlog to use non-blocking socket communication", was: Re: Re: [BUGS] BUG #7534: walreceiver takes long time to detect n/w breakdown Date: Fri, 4 Jan 2013 18:13:08 +0530 Message-ID: <007701cdea79$0cf35b30$26da1190$@kommi@huawei.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0078_01CDEAA7.26AB9730" X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: Ac3oP/tIKVT2t1DVRTiqjbGzu7e6kAAaC4jwAHGy3wA= Content-Language: en-us X-Originating-IP: [10.18.96.96] X-CFilter-Loop: Reflected X-Pg-Spam-Score: -3.2 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------=_NextPart_000_0078_01CDEAA7.26AB9730 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable On January 02, 2013 12:41 PM Hari Babu wrote: >On January 01, 2013 10:19 PM Boszormenyi Zoltan wrote: >>I am reviewing your patch. >>=95 Is the patch in context diff format?=20 >>Yes. > >Thanks for reviewing the patch. > >>=95 Does it apply cleanly to the current git master? >>Not quite cleanly but it doesn't produce rejects or fuzz, only offset warnings: > >Will rebase the patch to head. > >>=95 Does it include reasonable tests, necessary doc patches, etc?=20 >>The test cases are not applicable. There is no test framework for >>testing network outage in "make check". >> >>There are no documentation patches for the new --recvtimeout=3DINTERVAL >>and --conntimeout=3DINTERVAL options for either pg_basebackup or >>pg_receivexlog. > >I will add the documentation for the same. > >>Per the previous comment, no. But those are for the backend >>to notice network breakdowns and as such, they need a >>separate patch.=20 > >I also think it is better to handle it as a separate patch for walsender. > >>=95 Are the comments sufficient and accurate? >>This chunk below removes a comment which seems obvious enough >>so it's not needed: >>*************** >>*** 518,524 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline, >>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 got= o error; >>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 } >>=A0=20 >>!=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 /* Check the message type. */ >>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 if (copybuf[0] =3D=3D 'k') >>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 { >>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 int= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 pos; >>--- 559,568 ---- >>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 got= o error; >>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 } >>=A0=20 >>!=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 /* Set the last reply timesta= mp */ >>!=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 last_recv_timestamp =3D local= GetCurrentTimestamp(); >>!=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 ping_sent =3D false; >>!=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=20 >>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 if (copybuf[0] =3D=3D 'k') >>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 { >>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 int= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 pos; >>*************** >> >>Other comments are sufficient and accurate. > >I will fix and update the patch. The attached V2 patch in the mail handles all the review comments identified above. Regards, Hari babu. ------=_NextPart_000_0078_01CDEAA7.26AB9730 Content-Type: application/octet-stream; name="pg_basebkup_recvxlog_noblock_comm_v2.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="pg_basebkup_recvxlog_noblock_comm_v2.patch" *** a/doc/src/sgml/ref/pg_basebackup.sgml=0A= --- b/doc/src/sgml/ref/pg_basebackup.sgml=0A= ***************=0A= *** 386,391 **** PostgreSQL documentation=0A= --- 386,411 ----=0A= =0A= =20=20=0A= =0A= + =0A= + =0A= + =0A= + =0A= + time that receiver waits for communication from server (in second= s).=0A= + =0A= + =0A= + =20=20=20=20=20=0A= +=20=0A= + =0A= + =0A= + =0A= + =0A= + =0A= + time that client wait for connection to establish with server (in= seconds).=0A= + =0A= + =0A= + =20=20=20=20=20=20=20=20=20=20=0A= +=20=0A= + =0A= = =0A= =0A= =0A= *** a/doc/src/sgml/ref/pg_receivexlog.sgml=0A= --- b/doc/src/sgml/ref/pg_receivexlog.sgml=0A= ***************=0A= *** 164,169 **** PostgreSQL documentation=0A= --- 164,189 ----=0A= =0A= =20=20=0A= =0A= + =0A= + =0A= + =0A= + =0A= + time that receiver waits for communication from server (in second= s).=0A= + =0A= + =0A= + =20=20=20=20=20=0A= +=20=0A= + =0A= + =0A= + =0A= + =0A= + =0A= + time that client wait for connection to establish with server (in= seconds).=0A= + =0A= + =0A= + =20=20=0A= +=20=20=20=20=20=20=0A= + =0A= = =0A= =0A= =0A= *** a/src/bin/pg_basebackup/pg_basebackup.c=0A= --- b/src/bin/pg_basebackup/pg_basebackup.c=0A= ***************=0A= *** 41,46 **** bool includewal =3D false;=0A= --- 41,50 ----=0A= bool streamwal =3D false;=0A= bool fastcheckpoint =3D false;=0A= int standby_message_timeout =3D 10 * 1000; /* 10 sec =3D default */=0A= + int standby_recv_timeout =3D 60*1000; /* 60 sec =3D default */=0A= + char *standby_connect_timeout =3D NULL;=09=09=0A= +=20=0A= + #define NAPTIME_PER_CYCLE 100 /* max sleep time between cycles (100ms) */= =0A= =20=20=0A= /* Progress counters */=0A= static uint64 totalsize;=0A= ***************=0A= *** 119,124 **** usage(void)=0A= --- 123,132 ----=0A= printf(_(" -p, --port=3DPORT database server port number\n"));= =0A= printf(_(" -s, --status-interval=3DINTERVAL\n"=0A= " time between status packets sent to server = (in seconds)\n"));=0A= + printf(_(" -r, --recvtimeout=3DINTERVAL time that receiver waits for co= mmunication from\n"=0A= + " server (in seconds)\n"));=0A= + printf(_(" -t, --conntimeout=3DINTERVAL time that client wait for conne= ction to establish\n"=0A= + " with server (in seconds)\n"));=09=0A= printf(_(" -U, --username=3DNAME connect as specified database user\= n"));=0A= printf(_(" -w, --no-password never prompt for password\n"));=0A= printf(_(" -W, --password force password prompt (should happen = automatically)\n"));=0A= ***************=0A= *** 231,238 **** LogStreamerMain(logstreamer_param *param)=0A= {=0A= if (!ReceiveXlogStream(param->bgconn, param->startptr, param->timeline,= =0A= param->sysidentifier, param->xlogdir,=0A= ! reached_end_position, standby_message_timeout,=0A= ! true))=0A= =20=20=0A= /*=0A= * Any errors will already have been reported in the function process,= =0A= --- 239,246 ----=0A= {=0A= if (!ReceiveXlogStream(param->bgconn, param->startptr, param->timeline,= =0A= param->sysidentifier, param->xlogdir,=0A= ! reached_end_position, standby_message_timeout,=20=0A= ! standby_recv_timeout, true))=0A= =20=20=0A= /*=0A= * Any errors will already have been reported in the function process,= =0A= ***************=0A= *** 284,291 **** StartLogStreamer(char *startpos, uint32 timeline, char *sy= sidentifier)=0A= }=0A= #endif=0A= =20=20=0A= ! /* Get a second connection */=0A= ! param->bgconn =3D GetConnection();=0A= if (!param->bgconn)=0A= /* Error message already written in GetConnection() */=0A= exit(1);=0A= --- 292,300 ----=0A= }=0A= #endif=0A= =20=20=0A= ! /* Get a second connection. Sending connect_timeout=0A= ! * as configured, there is no need for rw_timeout.*/=0A= ! param->bgconn =3D GetConnection(standby_connect_timeout);=0A= if (!param->bgconn)=0A= /* Error message already written in GetConnection() */=0A= exit(1);=0A= ***************=0A= *** 461,466 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)=0A= --- 470,476 ----=0A= char filename[MAXPGPATH];=0A= char *copybuf =3D NULL;=0A= FILE *tarfile =3D NULL;=0A= + int64 last_recv_timestamp;=0A= =20=20=0A= #ifdef HAVE_LIBZ=0A= gzFile ztarfile =3D NULL;=0A= ***************=0A= *** 578,586 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)=0A= --- 588,599 ----=0A= disconnect_and_exit(1);=0A= }=0A= =20=20=0A= + /* Set the last reply timestamp */=0A= + last_recv_timestamp =3D localGetCurrentTimestamp();=0A= while (1)=0A= {=0A= int r;=0A= + int64 now;=0A= =20=20=0A= if (copybuf !=3D NULL)=0A= {=0A= ***************=0A= *** 588,594 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)=0A= copybuf =3D NULL;=0A= }=0A= =20=20=0A= ! r =3D PQgetCopyData(conn, ©buf, 0);=0A= if (r =3D=3D -1)=0A= {=0A= /*=0A= --- 601,662 ----=0A= copybuf =3D NULL;=0A= }=0A= =20=20=0A= ! r =3D PQgetCopyData(conn, ©buf, 1);=0A= ! if (r =3D=3D 0)=0A= ! {=0A= ! /*=0A= ! * In async mode, and no data available. We block on reading but=0A= ! * not more than the specified timeout, so that we can send a=0A= ! * response back to the client.=0A= ! */=0A= ! fd_set input_mask;=0A= ! struct timeval timeout;=0A= !=20=0A= ! FD_ZERO(&input_mask);=0A= ! FD_SET(PQsocket(conn), &input_mask);=0A= ! timeout.tv_sec =3D 0;=20=0A= ! timeout.tv_usec =3D NAPTIME_PER_CYCLE*1000;=0A= !=20=0A= ! r =3D select(PQsocket(conn) + 1, &input_mask, NULL, NULL, &timeout);= =0A= ! if (r =3D=3D 0 || (r < 0 && errno =3D=3D EINTR))=0A= ! {=0A= ! /*=0A= ! * Got a timeout or signal. Before Continuing the loop, check for tim= eout.=0A= ! */=0A= ! if (standby_recv_timeout > 0)=0A= ! {=0A= ! now =3D localGetCurrentTimestamp();=0A= ! if (localTimestampDifferenceExceeds(last_recv_timestamp, now, standb= y_recv_timeout))=0A= ! {=0A= ! fprintf(stderr, _("%s: terminating DB File receive due to timeout\n= "),=0A= ! progname);=0A= ! disconnect_and_exit(1);=0A= ! }=0A= ! }=0A= !=20=09=09=09=09=0A= ! continue;=0A= ! }=0A= ! else if (r < 0)=0A= ! {=0A= ! fprintf(stderr, _("%s: select() failed: %s\n"),=0A= ! progname, strerror(errno));=0A= ! disconnect_and_exit(1);=0A= ! }=0A= ! /* Else there is actually data on the socket */=0A= ! if (PQconsumeInput(conn) =3D=3D 0)=0A= ! {=0A= ! fprintf(stderr,=0A= ! _("%s: could not receive data from WAL Sender: %s"),=0A= ! progname, PQerrorMessage(conn));=0A= ! disconnect_and_exit(1);=0A= ! }=0A= !=20=0A= ! /* Set the last reply timestamp */=0A= ! last_recv_timestamp =3D localGetCurrentTimestamp();=0A= !=20=0A= ! /* Some data is received, so go back read them in buffer*/=09=09=09=0A= ! continue;=0A= ! }=09=09=0A= if (r =3D=3D -1)=0A= {=0A= /*=0A= ***************=0A= *** 659,664 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)=0A= --- 727,735 ----=0A= disconnect_and_exit(1);=0A= }=0A= =20=20=0A= + /* Set the last reply timestamp */=0A= + last_recv_timestamp =3D localGetCurrentTimestamp();=09=0A= +=20=0A= #ifdef HAVE_LIBZ=0A= if (ztarfile !=3D NULL)=0A= {=0A= ***************=0A= *** 708,713 **** ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int r= ownum)=0A= --- 779,785 ----=0A= int current_padding =3D 0;=0A= char *copybuf =3D NULL;=0A= FILE *file =3D NULL;=0A= + int64 last_recv_timestamp;=0A= =20=20=0A= if (PQgetisnull(res, rownum, 0))=0A= strcpy(current_path, basedir);=0A= ***************=0A= *** 725,733 **** ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int r= ownum)=0A= --- 797,809 ----=0A= disconnect_and_exit(1);=0A= }=0A= =20=20=0A= + /* Set the last reply timestamp */=0A= + last_recv_timestamp =3D localGetCurrentTimestamp();=0A= while (1)=0A= {=0A= int r;=0A= + int64 now;=0A= +=20=0A= =20=20=0A= if (copybuf !=3D NULL)=0A= {=0A= ***************=0A= *** 735,742 **** ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int r= ownum)=0A= copybuf =3D NULL;=0A= }=0A= =20=20=0A= ! r =3D PQgetCopyData(conn, ©buf, 0);=0A= =20=20=0A= if (r =3D=3D -1)=0A= {=0A= /*=0A= --- 811,872 ----=0A= copybuf =3D NULL;=0A= }=0A= =20=20=0A= ! r =3D PQgetCopyData(conn, ©buf, 1);=0A= ! if (r =3D=3D 0)=0A= ! {=0A= ! /*=0A= ! * In async mode, and no data available. We block on reading but=0A= ! * not more than the specified timeout, so that we can send a=0A= ! * response back to the client.=0A= ! */=0A= ! fd_set input_mask;=0A= ! struct timeval timeout;=0A= !=20=0A= ! FD_ZERO(&input_mask);=0A= ! FD_SET(PQsocket(conn), &input_mask);=0A= ! timeout.tv_sec =3D 0;=20=0A= ! timeout.tv_usec =3D NAPTIME_PER_CYCLE*1000;=0A= =20=20=0A= + r =3D select(PQsocket(conn) + 1, &input_mask, NULL, NULL, &timeout);= =0A= + if (r =3D=3D 0 || (r < 0 && errno =3D=3D EINTR))=0A= + {=0A= + /*=0A= + * Got a timeout or signal. Before Continuing the loop, check for tim= eout.=0A= + */=0A= + if (standby_recv_timeout > 0)=0A= + {=0A= + now =3D localGetCurrentTimestamp();=0A= + if (localTimestampDifferenceExceeds(last_recv_timestamp, now, standb= y_recv_timeout))=0A= + {=0A= + fprintf(stderr, _("%s: terminating DB File receive due to timeout\n= "),=0A= + progname);=0A= + disconnect_and_exit(1);=0A= + }=0A= + }=0A= +=20=09=09=09=09=0A= + continue;=0A= + }=0A= + else if (r < 0)=0A= + {=0A= + fprintf(stderr, _("%s: select() failed: %s\n"),=0A= + progname, strerror(errno));=0A= + disconnect_and_exit(1);=0A= + }=0A= + /* Else there is actually data on the socket */=0A= + if (PQconsumeInput(conn) =3D=3D 0)=0A= + {=0A= + fprintf(stderr,=0A= + _("%s: could not receive data from WAL Sender: %s"),=0A= + progname, PQerrorMessage(conn));=0A= + disconnect_and_exit(1);=0A= + }=0A= +=20=0A= + /* Set the last reply timestamp */=0A= + last_recv_timestamp =3D localGetCurrentTimestamp();=0A= +=20=0A= + /* Some data is received, so go back read them in buffer*/=09=09=09=0A= + continue;=0A= + }=0A= if (r =3D=3D -1)=0A= {=0A= /*=0A= ***************=0A= *** 749,759 **** ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int r= ownum)=0A= }=0A= else if (r =3D=3D -2)=0A= {=0A= fprintf(stderr, _("%s: could not read COPY data: %s"),=0A= progname, PQerrorMessage(conn));=0A= disconnect_and_exit(1);=0A= }=0A= !=20=0A= if (file =3D=3D NULL)=0A= {=0A= int filemode;=0A= --- 879,892 ----=0A= }=0A= else if (r =3D=3D -2)=0A= {=0A= + fprintf(stderr, "\n");=0A= fprintf(stderr, _("%s: could not read COPY data: %s"),=0A= progname, PQerrorMessage(conn));=0A= disconnect_and_exit(1);=0A= }=0A= !=20=09=09=0A= ! /* Set the last reply timestamp */=0A= ! last_recv_timestamp =3D localGetCurrentTimestamp();=0A= if (file =3D=3D NULL)=0A= {=0A= int filemode;=0A= ***************=0A= *** 947,955 **** BaseBackup(void)=0A= char xlogend[64];=0A= =20=20=0A= /*=0A= ! * Connect in replication mode to the server=0A= */=0A= ! conn =3D GetConnection();=0A= if (!conn)=0A= /* Error message already written in GetConnection() */=0A= exit(1);=0A= --- 1080,1088 ----=0A= char xlogend[64];=0A= =20=20=0A= /*=0A= ! * Connect in replication mode to the server. Sending connect_timeout.= =0A= */=0A= ! conn =3D GetConnection(standby_connect_timeout);=0A= if (!conn)=0A= /* Error message already written in GetConnection() */=0A= exit(1);=0A= ***************=0A= *** 1248,1253 **** main(int argc, char **argv)=0A= --- 1381,1388 ----=0A= {"no-password", no_argument, NULL, 'w'},=0A= {"password", no_argument, NULL, 'W'},=0A= {"status-interval", required_argument, NULL, 's'},=0A= + {"recvtimeout", required_argument, NULL, 'r'},=09=09=0A= + {"conntimeout", required_argument, NULL, 't'},=09=0A= {"verbose", no_argument, NULL, 'v'},=0A= {"progress", no_argument, NULL, 'P'},=0A= {NULL, 0, NULL, 0}=0A= ***************=0A= *** 1274,1280 **** main(int argc, char **argv)=0A= }=0A= }=0A= =20=20=0A= ! while ((c =3D getopt_long(argc, argv, "D:F:xX:l:zZ:c:h:p:U:s:wWvP",=0A= long_options, &option_index)) !=3D -1)=0A= {=0A= switch (c)=0A= --- 1409,1415 ----=0A= }=0A= }=0A= =20=20=0A= ! while ((c =3D getopt_long(argc, argv, "D:F:xX:l:zZ:c:h:p:U:s:r:t:wWvP",= =0A= long_options, &option_index)) !=3D -1)=0A= {=0A= switch (c)=0A= ***************=0A= *** 1386,1391 **** main(int argc, char **argv)=0A= --- 1521,1546 ----=0A= exit(1);=0A= }=0A= break;=0A= + case 'r':=0A= + standby_recv_timeout =3D atoi(optarg)*1000;=0A= + if (standby_recv_timeout < 0)=0A= + {=0A= + fprintf(stderr, _("%s: invalid recv timeout \"%s\"\n"),=0A= + progname, optarg);=0A= + exit(1);=0A= + }=0A= +=20=0A= + break;=09=09=09=0A= + case 't':=0A= + if (atoi(optarg) < 0)=0A= + {=0A= + fprintf(stderr, _("%s: invalid connect timeout \"%s\"\n"),=0A= + progname, optarg);=0A= + exit(1);=0A= + }=0A= +=20=09=09=09=09=0A= + standby_connect_timeout =3D pg_strdup(optarg);=0A= + break;=09=09=09=09=09=09=09=09=0A= case 'v':=0A= verbose++;=0A= break;=0A= *** a/src/bin/pg_basebackup/pg_receivexlog.c=0A= --- b/src/bin/pg_basebackup/pg_receivexlog.c=0A= ***************=0A= *** 35,40 **** char *basedir =3D NULL;=0A= --- 35,42 ----=0A= int verbose =3D 0;=0A= int noloop =3D 0;=0A= int standby_message_timeout =3D 10 * 1000; /* 10 sec =3D default */=0A= + int standby_recv_timeout =3D 60*1000; /* 60 sec =3D default */=0A= + char *standby_connect_timeout =3D NULL;=09=09=0A= volatile bool time_to_abort =3D false;=0A= =20=20=0A= =20=20=0A= ***************=0A= *** 63,68 **** usage(void)=0A= --- 65,74 ----=0A= printf(_(" -p, --port=3DPORT database server port number\n"));= =0A= printf(_(" -s, --status-interval=3DINTERVAL\n"=0A= " time between status packets sent to server = (in seconds)\n"));=0A= + printf(_(" -r, --recvtimeout=3DINTERVAL time that receiver waits for co= mmunication from\n"=0A= + " server (in seconds)\n"));=0A= + printf(_(" -t, --conntimeout=3DINTERVAL time that client wait for conne= ction to establish\n"=0A= + " with server (in seconds)\n"));=09=09= =0A= printf(_(" -U, --username=3DNAME connect as specified database user\= n"));=0A= printf(_(" -w, --no-password never prompt for password\n"));=0A= printf(_(" -W, --password force password prompt (should happen = automatically)\n"));=0A= ***************=0A= *** 218,226 **** StreamLog(void)=0A= lo;=0A= =20=20=0A= /*=0A= ! * Connect in replication mode to the server=0A= */=0A= ! conn =3D GetConnection();=0A= if (!conn)=0A= /* Error message already written in GetConnection() */=0A= return;=0A= --- 224,233 ----=0A= lo;=0A= =20=20=0A= /*=0A= ! * Connect in replication mode to the server, Sending connect_timeout=0A= ! * as configured, there is no need for rw_timeout.=0A= */=0A= ! conn =3D GetConnection(standby_connect_timeout);=0A= if (!conn)=0A= /* Error message already written in GetConnection() */=0A= return;=0A= ***************=0A= *** 274,280 **** StreamLog(void)=0A= timeline);=0A= =20=20=0A= ReceiveXlogStream(conn, startpos, timeline, NULL, basedir,=0A= ! stop_streaming, standby_message_timeout, false);=0A= =20=20=0A= PQfinish(conn);=0A= }=0A= --- 281,288 ----=0A= timeline);=0A= =20=20=0A= ReceiveXlogStream(conn, startpos, timeline, NULL, basedir,=0A= ! stop_streaming, standby_message_timeout,=0A= ! standby_recv_timeout, false);=0A= =20=20=0A= PQfinish(conn);=0A= }=0A= ***************=0A= *** 306,311 **** main(int argc, char **argv)=0A= --- 314,321 ----=0A= {"no-password", no_argument, NULL, 'w'},=0A= {"password", no_argument, NULL, 'W'},=0A= {"status-interval", required_argument, NULL, 's'},=0A= + {"recvtimeout", required_argument, NULL, 'r'},=09=09=0A= + {"conntimeout", required_argument, NULL, 't'},=09=09=09=09=0A= {"verbose", no_argument, NULL, 'v'},=0A= {NULL, 0, NULL, 0}=0A= };=0A= ***************=0A= *** 331,337 **** main(int argc, char **argv)=0A= }=0A= }=0A= =20=20=0A= ! while ((c =3D getopt_long(argc, argv, "D:h:p:U:s:nwWv",=0A= long_options, &option_index)) !=3D -1)=0A= {=0A= switch (c)=0A= --- 341,347 ----=0A= }=0A= }=0A= =20=20=0A= ! while ((c =3D getopt_long(argc, argv, "D:h:p:U:s:r:t:nwWv",=0A= long_options, &option_index)) !=3D -1)=0A= {=0A= switch (c)=0A= ***************=0A= *** 369,374 **** main(int argc, char **argv)=0A= --- 379,404 ----=0A= exit(1);=0A= }=0A= break;=0A= + case 'r':=0A= + standby_recv_timeout =3D atoi(optarg)*1000;=0A= + if (standby_recv_timeout < 0)=0A= + {=0A= + fprintf(stderr, _("%s: invalid recv timeout \"%s\"\n"),=0A= + progname, optarg);=0A= + exit(1);=0A= + }=0A= + break;=0A= + case 't':=0A= + if (atoi(optarg) < 0)=0A= + {=0A= + fprintf(stderr, _("%s: invalid connect timeout \"%s\"\n"),=0A= + progname, optarg);=0A= + exit(1);=0A= + }=0A= +=20=09=09=09=09=0A= + standby_connect_timeout =3D pg_strdup(optarg);=0A= + break;=09=09=09=09=09=09=09=09=0A= +=20=09=09=09=09=0A= case 'n':=0A= noloop =3D 1;=0A= break;=0A= *** a/src/bin/pg_basebackup/receivelog.c=0A= --- b/src/bin/pg_basebackup/receivelog.c=0A= ***************=0A= *** 181,187 **** close_walfile(char *basedir, char *walname, bool segment_c= omplete)=0A= * backend code. The protocol always uses integer timestamps, regardless = of=0A= * server setting.=0A= */=0A= ! static int64=0A= localGetCurrentTimestamp(void)=0A= {=0A= int64 result;=0A= --- 181,187 ----=0A= * backend code. The protocol always uses integer timestamps, regardless = of=0A= * server setting.=0A= */=0A= ! int64=0A= localGetCurrentTimestamp(void)=0A= {=0A= int64 result;=0A= ***************=0A= *** 201,207 **** localGetCurrentTimestamp(void)=0A= * Local version of TimestampDifference(), since we are not linked with= =0A= * backend code.=0A= */=0A= ! static void=0A= localTimestampDifference(int64 start_time, int64 stop_time,=0A= long *secs, int *microsecs)=0A= {=0A= --- 201,207 ----=0A= * Local version of TimestampDifference(), since we are not linked with= =0A= * backend code.=0A= */=0A= ! void=0A= localTimestampDifference(int64 start_time, int64 stop_time,=0A= long *secs, int *microsecs)=0A= {=0A= ***************=0A= *** 223,229 **** localTimestampDifference(int64 start_time, int64 stop_time,= =0A= * Local version of TimestampDifferenceExceeds(), since we are not=0A= * linked with backend code.=0A= */=0A= ! static bool=0A= localTimestampDifferenceExceeds(int64 start_time,=0A= int64 stop_time,=0A= int msec)=0A= --- 223,229 ----=0A= * Local version of TimestampDifferenceExceeds(), since we are not=0A= * linked with backend code.=0A= */=0A= ! bool=0A= localTimestampDifferenceExceeds(int64 start_time,=0A= int64 stop_time,=0A= int msec)=0A= ***************=0A= *** 333,339 **** bool=0A= ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,=0A= char *sysidentifier, char *basedir,=0A= stream_stop_callback stream_stop,=0A= ! int standby_message_timeout, bool rename_partial)=0A= {=0A= char query[128];=0A= char current_walfile_name[MAXPGPATH];=0A= --- 333,340 ----=0A= ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,=0A= char *sysidentifier, char *basedir,=0A= stream_stop_callback stream_stop,=0A= ! int standby_message_timeout,=20=0A= ! int standby_recv_timeout, bool rename_partial)=0A= {=0A= char query[128];=0A= char current_walfile_name[MAXPGPATH];=0A= ***************=0A= *** 341,346 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint3= 2 timeline,=0A= --- 342,349 ----=0A= char *copybuf =3D NULL;=0A= int64 last_status =3D -1;=0A= XLogRecPtr blockpos =3D InvalidXLogRecPtr;=0A= + int64 last_recv_timestamp;=0A= + bool ping_sent =3D false;=0A= =20=20=0A= if (sysidentifier !=3D NULL)=0A= {=0A= ***************=0A= *** 394,399 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint3= 2 timeline,=0A= --- 397,406 ----=0A= }=0A= PQclear(res);=0A= =20=20=0A= + /* Set the last reply timestamp */=0A= + last_recv_timestamp =3D localGetCurrentTimestamp();=0A= + ping_sent =3D false;=0A= +=20=09=0A= /*=0A= * Receive the actual xlog data=0A= */=0A= ***************=0A= *** 477,486 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint3= 2 timeline,=0A= if (r =3D=3D 0 || (r < 0 && errno =3D=3D EINTR))=0A= {=0A= /*=0A= ! * Got a timeout or signal. Continue the loop and either=0A= ! * deliver a status packet to the server or just go back into=0A= * blocking.=0A= */=0A= continue;=0A= }=0A= else if (r < 0)=0A= --- 484,522 ----=0A= if (r =3D=3D 0 || (r < 0 && errno =3D=3D EINTR))=0A= {=0A= /*=0A= ! * Got a timeout or signal. Before Continuing the loop, check for tim= eout.=0A= ! * and then either deliver a status packet to the server or just go b= ack into=0A= * blocking.=0A= */=0A= + if (standby_recv_timeout > 0)=0A= + {=0A= + now =3D localGetCurrentTimestamp();=0A= + if (localTimestampDifferenceExceeds(last_recv_timestamp, now, standb= y_recv_timeout))=0A= + {=0A= + fprintf(stderr, _("%s: terminating XLogStream receiver due to timeo= ut\n"),=0A= + progname);=0A= + goto error;=0A= + }=0A= +=20=0A= + /*=0A= + * We didn't receive anything new, for half of receiver=0A= + * replication timeout. Ping the server, if not already done.=0A= + */=0A= + if (!ping_sent)=0A= + {=0A= + if (localTimestampDifferenceExceeds(last_recv_timestamp, now, (stan= dby_recv_timeout/2)))=0A= + {=0A= + if (!sendFeedback(conn, blockpos, now, true))=0A= + {=0A= + goto error;=0A= + }=0A= +=20=0A= + last_status =3D now;=0A= + ping_sent =3D true;=0A= + }=0A= + }=09=09=0A= + }=09=09=09=0A= +=20=09=09=09=09=0A= continue;=0A= }=0A= else if (r < 0)=0A= ***************=0A= *** 497,502 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint3= 2 timeline,=0A= --- 533,543 ----=0A= progname, PQerrorMessage(conn));=0A= goto error;=0A= }=0A= +=20=0A= + /* Set the last reply timestamp */=0A= + last_recv_timestamp =3D localGetCurrentTimestamp();=0A= + ping_sent =3D false;=0A= +=20=09=09=09=0A= continue;=0A= }=0A= if (r =3D=3D -1)=0A= ***************=0A= *** 509,514 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint3= 2 timeline,=0A= --- 550,559 ----=0A= goto error;=0A= }=0A= =20=20=0A= + /* Set the last reply timestamp */=0A= + last_recv_timestamp =3D localGetCurrentTimestamp();=0A= + ping_sent =3D false;=0A= +=20=0A= /* Check the message type. */=0A= if (copybuf[0] =3D=3D 'k')=0A= {=0A= *** a/src/bin/pg_basebackup/receivelog.h=0A= --- b/src/bin/pg_basebackup/receivelog.h=0A= ***************=0A= *** 7,16 ****=0A= typedef bool (*stream_stop_callback) (XLogRecPtr segendpos, uint32 timeli= ne, bool segment_finished);=0A= =20=20=0A= extern bool ReceiveXlogStream(PGconn *conn,=0A= ! XLogRecPtr startpos,=0A= ! uint32 timeline,=0A= ! char *sysidentifier,=0A= ! char *basedir,=0A= ! stream_stop_callback stream_stop,=0A= ! int standby_message_timeout,=0A= ! bool rename_partial);=0A= --- 7,24 ----=0A= typedef bool (*stream_stop_callback) (XLogRecPtr segendpos, uint32 timeli= ne, bool segment_finished);=0A= =20=20=0A= extern bool ReceiveXlogStream(PGconn *conn,=0A= ! XLogRecPtr startpos,=0A= ! uint32 timeline,=0A= ! char *sysidentifier,=0A= ! char *basedir,=0A= ! stream_stop_callback stream_stop,=0A= ! int standby_message_timeout,=0A= ! int standby_recv_timeout,=0A= ! bool rename_partial);=0A= !=20=0A= ! extern int64 localGetCurrentTimestamp(void);=0A= ! extern void localTimestampDifference(int64 start_time, int64 stop_time,= =0A= ! long *secs, int *microsecs);=0A= ! extern bool localTimestampDifferenceExceeds(int64 start_time,=0A= ! int64 stop_time,=0A= ! int msec);=0A= *** a/src/bin/pg_basebackup/streamutil.c=0A= --- b/src/bin/pg_basebackup/streamutil.c=0A= ***************=0A= *** 66,74 **** pg_malloc0(size_t size)=0A= * Connect to the server. Returns a valid PGconn pointer if connected,=0A= * or NULL on non-permanent error. On permanent error, the function will= =0A= * call exit(1) directly.=0A= */=0A= PGconn *=0A= ! GetConnection(void)=0A= {=0A= PGconn *tmpconn;=0A= int argcount =3D 4; /* dbname, replication, fallback_app_name,=0A= --- 66,76 ----=0A= * Connect to the server. Returns a valid PGconn pointer if connected,=0A= * or NULL on non-permanent error. On permanent error, the function will= =0A= * call exit(1) directly.=0A= + * Set conn_timeout to PGconn structure if their value=20=0A= + * is not NULL.=0A= */=0A= PGconn *=0A= ! GetConnection(char *conn_timeout)=0A= {=0A= PGconn *tmpconn;=0A= int argcount =3D 4; /* dbname, replication, fallback_app_name,=0A= ***************=0A= *** 85,90 **** GetConnection(void)=0A= --- 87,94 ----=0A= argcount++;=0A= if (dbport)=0A= argcount++;=0A= + if (conn_timeout)=0A= + argcount++;=0A= =20=20=0A= keywords =3D pg_malloc0((argcount + 1) * sizeof(*keywords));=0A= values =3D pg_malloc0((argcount + 1) * sizeof(*values));=0A= ***************=0A= *** 114,119 **** GetConnection(void)=0A= --- 118,129 ----=0A= values[i] =3D dbport;=0A= i++;=0A= }=0A= + if (conn_timeout !=3D NULL)=0A= + {=0A= + keywords[i] =3D "connect_timeout";=0A= + values[i] =3D conn_timeout;=0A= + i++;=0A= + }=0A= =20=20=0A= while (true)=0A= {=0A= *** a/src/bin/pg_basebackup/streamutil.h=0A= --- b/src/bin/pg_basebackup/streamutil.h=0A= ***************=0A= *** 19,22 **** extern PGconn *conn;=0A= extern char *pg_strdup(const char *s);=0A= extern void *pg_malloc0(size_t size);=0A= =20=20=0A= ! extern PGconn *GetConnection(void);=0A= --- 19,22 ----=0A= extern char *pg_strdup(const char *s);=0A= extern void *pg_malloc0(size_t size);=0A= =20=20=0A= ! PGconn *GetConnection(char *conn_timeout);=0A= ------=_NextPart_000_0078_01CDEAA7.26AB9730 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------=_NextPart_000_0078_01CDEAA7.26AB9730--