Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kd3EH-0003mr-DD for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Nov 2020 03:24:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kd3EG-0001mC-1X for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Nov 2020 03:24:56 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kd3EF-0001m4-QX for pgsql-hackers@lists.postgresql.org; Thu, 12 Nov 2020 03:24:55 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kd3EC-0001Zo-3n for pgsql-hackers@postgresql.org; Thu, 12 Nov 2020 03:24:54 +0000 Received: from hnk.local (p1295030-ipngn4902funabasi.chiba.ocn.ne.jp [153.166.85.30]) by oss.nttdata.com (Postfix) with ESMTPSA id C254F60342; Thu, 12 Nov 2020 12:24:48 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.3 at oss.nttdata.com Subject: Re: Delay of standby shutdown To: Soumyadeep Chakraborty Cc: pgsql-hackers References: <9d7e6ab0-8a53-ddb9-63cd-289bcb25fe0e@oss.nttdata.com> From: Fujii Masao Message-ID: <87b9d893-101e-664f-270e-6029b1fe7a0c@oss.nttdata.com> Date: Thu, 12 Nov 2020 12:24:48 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.4.3 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------A3F158877009229BD693AA8C" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------A3F158877009229BD693AA8C Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 2020/11/04 9:35, Soumyadeep Chakraborty wrote: > Hello Fujii, > > On Thu, Sep 17, 2020 at 6:49 AM Fujii Masao wrote: > > As far as I understand after debugging, the problem is as follows: Yes. > > 1. After the primary is stopped, and the standby startup process is > waiting inside: > > (void) WaitLatch(&XLogCtl->recoveryWakeupLatch, > WL_LATCH_SET | WL_TIMEOUT | > WL_EXIT_ON_PM_DEATH, > wait_time, > WAIT_EVENT_RECOVERY_RETRIEVE_RETRY_INTERVAL); > > it receives SIGTERM on account of stopping the standby and it leads to > the WaitLatch call returning with WL_LATCH_SET. > > 2. WaitForWALToBecomeAvailable() then will return true after calling > XLogFileReadAnyTLI() and eventually, XLogReadRecord() will return NULL > since there is no new WAL to read, which means ReadRecord() will loop > back and perform another XLogReadRecord(). > > 3. The additional XLogReadRecord() will lead to a 5s wait inside > WaitForWALToBecomeAvailable() - a different WaitLatch() call this time: > > /* > * Wait for more WAL to arrive. Time out after 5 seconds > * to react to a trigger file promptly and to check if the > * WAL receiver is still active. > */ > (void) WaitLatch(&XLogCtl->recoveryWakeupLatch, > WL_LATCH_SET | WL_TIMEOUT | > WL_EXIT_ON_PM_DEATH, > 5000L, WAIT_EVENT_RECOVERY_WAL_STREAM); > > 4. And then eventually, the code will handle interrupts here inside > WaitForWALToBecomeAvailable() after the 5s wait: > > /* > * This possibly-long loop needs to handle interrupts of startup > * process. > */ > HandleStartupProcInterrupts(); > >> To avoid this issue, I think that ReadRecord() should call >> HandleStartupProcInterrupts() whenever looping back to retry. > > Alternatively, perhaps we can place it inside > WaitForWALToBecomeAvailable() (which already has a similar call), > since it is more semantically aligned to checking for interrupts, rather > than ReadRecord()? Like this: Yes. Your approach looks better to me. Attached is the updated version of the patch implementing that. Regards, -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION --------------A3F158877009229BD693AA8C Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="shutdown_in_standby_v2.patch" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="shutdown_in_standby_v2.patch" ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2FjY2Vzcy90cmFuc2FtL3hsb2cuYyBiL3NyYy9i YWNrZW5kL2FjY2Vzcy90cmFuc2FtL3hsb2cuYwppbmRleCBhYTYzZjM3NjE1Li43ZDk3Yjk2 ZTcyIDEwMDY0NAotLS0gYS9zcmMvYmFja2VuZC9hY2Nlc3MvdHJhbnNhbS94bG9nLmMKKysr IGIvc3JjL2JhY2tlbmQvYWNjZXNzL3RyYW5zYW0veGxvZy5jCkBAIC0xMjIyNSw2ICsxMjIy NSw5IEBAIFdhaXRGb3JXQUxUb0JlY29tZUF2YWlsYWJsZShYTG9nUmVjUHRyIFJlY1B0ciwg Ym9vbCByYW5kQWNjZXNzLAogCQkJCQkJCQkJCSBXQUlUX0VWRU5UX1JFQ09WRVJZX1JFVFJJ RVZFX1JFVFJZX0lOVEVSVkFMKTsKIAkJCQkJCVJlc2V0TGF0Y2goTXlMYXRjaCk7CiAJCQkJ CQlub3cgPSBHZXRDdXJyZW50VGltZXN0YW1wKCk7CisKKwkJCQkJCS8qIEhhbmRsZSBpbnRl cnJ1cHQgc2lnbmFscyBvZiBzdGFydHVwIHByb2Nlc3MgKi8KKwkJCQkJCUhhbmRsZVN0YXJ0 dXBQcm9jSW50ZXJydXB0cygpOwogCQkJCQl9CiAJCQkJCWxhc3RfZmFpbF90aW1lID0gbm93 OwogCQkJCQljdXJyZW50U291cmNlID0gWExPR19GUk9NX0FSQ0hJVkU7Cg== --------------A3F158877009229BD693AA8C--