Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rFAhX-008dur-Lm for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Dec 2023 10:18:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rFAhW-008BQ8-Bq for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Dec 2023 10:18:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rFAhV-008BNw-Se for pgsql-hackers@lists.postgresql.org; Mon, 18 Dec 2023 10:18:18 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rFAhT-00Az1i-4O for pgsql-hackers@lists.postgresql.org; Mon, 18 Dec 2023 10:18:16 +0000 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-40d1bcd93b0so1339225e9.0 for ; Mon, 18 Dec 2023 02:18:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702894692; x=1703499492; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=nv31P5f+ada8yOb8qIkaIdoN7ODeTsU9B6tpxrZYeQE=; b=B9e8SclXpsbLTnIWUSxA1AH1J+h9tc8GLzRLcoE/U4FX5SsX9wku5r5VQaVyLfz18N /oq4DOk0ZswY67lBOCDs5C8yT2NbyuiOTkFfriwNuSkw1Er1r2h0fDMLJVezBZ/XLtNa ETtKEecG7bwIQe2aVAWTFBnIN4U935cRYFX5O6PwegN/dDNoJN9Nj2hWEbS28MUz3Gur RYYcyfN/AJsDJfITw1ehWhoGJsOrG4+4NGe/lkuLkH8StoN+9ICuPASb6o0pJPUJHUAJ AWf4zuFqLM7RpK67rOmZZG3j8XMr/TiSmwVnztORYME00J2q9Tnj5Xfaxpj2jR7MGrKN 9zjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702894692; x=1703499492; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nv31P5f+ada8yOb8qIkaIdoN7ODeTsU9B6tpxrZYeQE=; b=BHrdEiqQs6DaJ86Cu7cQ4sImEcaobVcRTrt0Ann+3c7nEVCvzF6pXwZBYhMMhUNWjk gR5f5aDXjy733zqR38QfwCOXAMwmHp0wQ0OpDLOBCRlAMLD2OUMvrVCYaTIXlMwN/dkp Cv+mWqBRyFEEBe9cC2W7HZbQwzC/v69C/Uhm2MdGWReKEyROsjHvdgF6vU2qSr1HFM+z s7CFgo476Orkyy+gqxPUsVto+bglN1vyB30PYuakYSvG3wXLhVXuAvQbBfNSMkU4hEA4 I5nPIdinoadqpHOF3OVYRIOkff5anIR+hiT08/S7Spmbwbg0EudyMJ7yIRwA7zOcUJ9i 4f+A== X-Gm-Message-State: AOJu0Yw+3dRt+GkgLbK4ghUHOVooO+cVJI8Vk/RizwfAVUzcJ9b6GSpa P/OKHs8RAjfkUVR8VjUOtKqo/+h9STU= X-Google-Smtp-Source: AGHT+IGsrZNtT8+H1Ittyt4Je2I5pqdQ/ztZSufLhAma3mgCRmeNhoaPSR51HW0io5nAGXHCnOsFvw== X-Received: by 2002:adf:e610:0:b0:336:5c03:c598 with SMTP id p16-20020adfe610000000b003365c03c598mr2087822wrm.58.1702894691937; Mon, 18 Dec 2023 02:18:11 -0800 (PST) Received: from ?IPV6:2a01:cb11:192:7000:18a7:2a4b:c6a8:b79a? ([2a01:cb11:192:7000:18a7:2a4b:c6a8:b79a]) by smtp.gmail.com with ESMTPSA id m1-20020adfa3c1000000b0033667f42ef8sm2574603wrb.63.2023.12.18.02.18.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Dec 2023 02:18:11 -0800 (PST) Message-ID: Date: Mon, 18 Dec 2023 11:18:10 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Move walreceiver state assignment (to WALRCV_STREAMING) in WalReceiverMain() Content-Language: en-US To: Euler Taveira , PostgreSQL Hackers References: <11ff040c-2eca-421a-b74a-0e99937c2d93@gmail.com> <3abb8c54-11de-4220-ad65-c2785df01124@app.fastmail.com> From: "Drouvot, Bertrand" In-Reply-To: <3abb8c54-11de-4220-ad65-c2785df01124@app.fastmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 12/12/23 8:54 PM, Euler Taveira wrote: > On Tue, Dec 12, 2023, at 12:58 PM, Drouvot, Bertrand wrote: >> Currently walrcv->walRcvState is set to WALRCV_STREAMING at the >> beginning of WalReceiverMain(). >> >> But it seems that after this assignment things could be wrong before the >> walreicever actually starts streaming (like not being able to connect >> to the primary). >> >> It looks to me that WALRCV_STREAMING should be set once walrcv_startstreaming() >> returns true: this is the proposal of this patch. > > Per the state name (streaming), it seems it should be set later as you > proposed, Thanks for looking at it! > however, I'm concerned about the previous state (WALRCV_STARTING). If > I'm reading the code correctly, WALRCV_STARTING is assigned at > RequestXLogStreaming(): > >                         SetInstallXLogFileSegmentActive(); >                         RequestXLogStreaming(tli, ptr, PrimaryConnInfo, >                                              PrimarySlotName, >                                              wal_receiver_create_temp_slot); >                         flushedUpto = 0; >                     } > >                     /* >                      * Check if WAL receiver is active or wait to start up. >                      */ >                     if (!WalRcvStreaming()) >                     { >                         lastSourceFailed = true; >                         break; >                     } > > After a few lines the function WalRcvStreaming() has: > >     SpinLockRelease(&walrcv->mutex); > >     /* >      * If it has taken too long for walreceiver to start up, give up. Setting >      * the state to STOPPED ensures that if walreceiver later does start up >      * after all, it will see that it's not supposed to be running and die >      * without doing anything. >      */ >     if (state == WALRCV_STARTING) >     { >         pg_time_t   now = (pg_time_t) time(NULL); > >         if ((now - startTime) > WALRCV_STARTUP_TIMEOUT) >         { >             bool        stopped = false; > >             SpinLockAcquire(&walrcv->mutex); >             if (walrcv->walRcvState == WALRCV_STARTING) >             { >                 state = walrcv->walRcvState = WALRCV_STOPPED; >                 stopped = true; >             } >             SpinLockRelease(&walrcv->mutex); > >             if (stopped) >                 ConditionVariableBroadcast(&walrcv->walRcvStoppedCV); >         } >     } > > Couldn't it give up before starting if you apply your patch? My main concern is > due to a slow system, the walrcv_connect() took to long in WalReceiverMain() > and the code above kills the walreceiver while in the process to start it. Yeah, so it looks to me that the sequence of events is: 1) The startup process sets walrcv->walRcvState = WALRCV_STARTING (in RequestXLogStreaming()) 2) The startup process sets the walrcv->startTime (in RequestXLogStreaming()) 3) The startup process asks then the postmaster to starts the walreceiver 4) Then The startup process checks if WalRcvStreaming() is true Note that 3) is not waiting for the walreceiver to actually start: it "just" sets a flag and kill (SIGUSR1) the postmaster (in SendPostmasterSignal()). It means that if the time between 1 and 4 is <= WALRCV_STARTUP_TIMEOUT (10 seconds) then WalRcvStreaming() returns true (even if the walreceiver is not streaming yet). So it looks to me that even if the walreceiver does take time to start streaming, as long as the time between 1 and 4 is <= 10 seconds we are fine. And I think it's fine because WalRcvStreaming() does not actually "only" check that the walreceiver is streaming but as its comment states: " /* * Is walreceiver running and streaming (or at least attempting to connect, * or starting up)? */ " Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com