Deal with the case when the primary is not node 0 in streaming replication mode.
authorTatsuo Ishii <[email protected]>
Wed, 25 May 2016 01:57:42 +0000 (10:57 +0900)
committerTatsuo Ishii <[email protected]>
Wed, 25 May 2016 01:58:44 +0000 (10:58 +0900)
http://www.pgpool.net/mantisbt/view.php?id=194#c837 reported that if
primary is not node 0, then statement timeout could occur even after
bug194-3.3.diff was applied. After some investigation, it appeared
that MASTER macro could return other than primary or load balance
node, which was not supposed to happen, thus do_query() sends queries
to wrong node (this is not clear from the report but I confirmed it in
my investigation).

pool_virtual_master_db_node_id(), which is called in MASTER macro
returns query_context->virtual_master_node_id if query context
exists. This could return wrong node if the variable has not been set
yet. To fix this, the function is modified: if the variable is not
either load balance node or primary node, the primary node id is
returned.

pool_query_context.c

index 3cb678447af9f0b4236ddfd77d39be7c62f14968..145124677a6e06e9407dda66ef7e76f8612b50e2 100644 (file)
@@ -296,7 +296,23 @@ int pool_virtual_master_db_node_id(void)
 
        if (sc->query_context)
        {
-               return sc->query_context->virtual_master_node_id;
+               int node_id = sc->query_context->virtual_master_node_id;
+
+               if (STREAM)
+               {
+                        /*
+                         * Make sure that virtual_master_node_id is either primary node
+                         * id or load balance node id.  If not, it is likely that
+                         * virtual_master_node_id is not set up yet. Let's use the
+                         * primary node id.
+                         */
+                       if (node_id != sc->load_balance_node_id && node_id != PRIMARY_NODE_ID)
+                       {
+                               node_id = PRIMARY_NODE_ID;
+                       }
+               }
+
+               return node_id;
        }
 
        /*