<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 9, 2017 at 5:53 PM, Tatsuo Ishii <span dir="ltr">&lt;<a href="mailto:ishii@sraoss.co.jp" target="_blank">ishii@sraoss.co.jp</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">&gt; On Tue, May 9, 2017 at 6:16 AM, Tatsuo Ishii &lt;<a href="mailto:ishii@sraoss.co.jp">ishii@sraoss.co.jp</a>&gt; wrote:<br>
&gt;<br>
&gt;&gt; Usama,<br>
&gt;&gt;<br>
&gt;&gt; While adding a new test case to 003.failover regression test, I found<br>
&gt;&gt; a corner case bug in primary failover.<br>
&gt;&gt;<br>
&gt;&gt; Suppose Pgpool-II starts but is yet finding primary node. If primary<br>
&gt;&gt; failover happens, it skips finding primary node and let the initial<br>
&gt;&gt; value of it (Req_info-&gt;primary_node_id == -1) to be used as the new<br>
&gt;&gt; primary node id. As a result, no primary node id exists until next<br>
&gt;&gt; failover happens.<br>
&gt;&gt;<br>
&gt;&gt; Initialy I thought The problem is in the code of<br>
&gt;&gt; pgpool_main.c:failover() which tries to optimize finding primary node<br>
&gt;&gt; process.<br>
&gt;&gt;<br>
&gt;&gt;                 /*<br>
&gt;&gt;                  * If the down node was a standby node in streaming<br>
&gt;&gt; replication<br>
&gt;&gt;                  * mode, we can avoid calling<br>
&gt;&gt; find_primary_node_repeatedly() and<br>
&gt;&gt;                  * recognize the former primary as the new primary node,<br>
&gt;&gt; which<br>
&gt;&gt;                  * will reduce the time to process standby down.<br>
&gt;&gt;                  */<br>
&gt;&gt;                 else if (MASTER_SLAVE &amp;&amp; pool_config-&gt;master_slave_sub_<wbr>mode<br>
&gt;&gt; == STREAM_MODE &amp;&amp;<br>
&gt;&gt;                                  reqkind == NODE_DOWN_REQUEST)<br>
&gt;&gt;                 {<br>
&gt;&gt;                         if (Req_info-&gt;primary_node_id != node_id)<br>
&gt;&gt;                                 new_primary = Req_info-&gt;primary_node_id;<br>
&gt;&gt;                         else<br>
&gt;&gt;                                 new_primary =<br>
&gt;&gt; find_primary_node_repeatedly()<wbr>;<br>
&gt;&gt;<br>
&gt;&gt; I was attempting to fix it by checking Req_info-&gt;primary_node_id to<br>
&gt;&gt; see if it&#39;s initial value (-1) or not. If it&#39;s -1,<br>
&gt;&gt; find_primary_node_repeatedly() need to be called.<br>
&gt;&gt;<br>
&gt;&gt; But looking into pgpool_main() closely, I suspect there&#39;s a<br>
&gt;&gt; fundamental problem:<br>
&gt;&gt;<br>
&gt;&gt; 1) It processes failover in CHECK_REQUEST *before* setting<br>
&gt;&gt;    Req_info-&gt;primary_node_id.<br>
&gt;&gt;<br>
&gt;&gt;         /*<br>
&gt;&gt;          * check for child signals to ensure child startup before<br>
&gt;&gt; reporting successfull start<br>
&gt;&gt;          */<br>
&gt;&gt;         CHECK_REQUEST;<br>
&gt;&gt;<br>
&gt;&gt;         ereport(LOG,<br>
&gt;&gt;                         (errmsg(&quot;%s successfully started. version %s<br>
&gt;&gt; (%s)&quot;, PACKAGE, VERSION, PGPOOLVERSION)));<br>
&gt;&gt;<br>
&gt;&gt;         /*<br>
&gt;&gt;          * if the primary node id is not loaded by watchdog, search for it<br>
&gt;&gt;          */<br>
&gt;&gt;         if (Req_info-&gt;primary_node_id &lt; 0)<br>
&gt;&gt;         {<br>
&gt;&gt;                 /* Save primary node id */<br>
&gt;&gt;                 Req_info-&gt;primary_node_id = find_primary_node();<br>
&gt;&gt;         }<br>
&gt;&gt;<br>
&gt;&gt; 2) It uses find_primary_node(), rather than<br>
&gt;&gt;    find_primary_node_repeatedly()<wbr>. So if by some reasons (for example<br>
&gt;&gt;    the backend does not come up yet), find_primary_node() will fail<br>
&gt;&gt;    and Req_info-&gt;primary_node_id is set to -1.<br>
&gt;&gt;<br>
&gt;&gt; I think proper fix will be moving the CHECK_REQUEST call above inside<br>
&gt;&gt; main loop, and change the find_primary_node() call to<br>
&gt;&gt; find_primary_node_repeatedly()<wbr>.<br>
&gt;&gt;<br>
&gt;&gt; Attached is the patch to do that (plus change the<br>
&gt;&gt; search_primary_node_timeout to smaller value in 055.backend_all_down<br>
&gt;&gt; test. Otherwise, regression timeout is triggered) against master<br>
&gt;&gt; branch.<br>
&gt;&gt;<br>
&gt;&gt; What do you think?<br>
&gt;&gt;<br>
&gt;<br>
&gt;<br>
&gt; Waoo thanks for catching this, it is a really annoying issue, I think your<br>
&gt; patch does solve the problem and is the right approach,<br>
&gt; But I was thinking what if we move search for the primary node before<br>
&gt; starting the child processes. So that we spawn the child processes after<br>
&gt; finishing all the<br>
&gt; startup rituals?<br>
<br>
</div></div>Oh I think that will make even things safer.<br>
<span class=""><br>
&gt; Do you think it will cause some issues?<br>
<br>
</span>So far there&#39;s nothing I can think of. Let me try it. I will report<br>
back tomorrow.<br></blockquote><div><br></div><div>Many thanks</div><div><br></div><div>Best regards</div><div>Muhammad Usama </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Best regards,<br>
<div class="HOEnZb"><div class="h5">--<br>
Tatsuo Ishii<br>
SRA OSS, Inc. Japan<br>
English: <a href="http://www.sraoss.co.jp/index_en.php" rel="noreferrer" target="_blank">http://www.sraoss.co.jp/index_<wbr>en.php</a><br>
Japanese:<a href="http://www.sraoss.co.jp" rel="noreferrer" target="_blank">http://www.sraoss.co.<wbr>jp</a><br>
</div></div></blockquote></div><br></div></div>