Randomize the choice of the initial ROUNDROBIN node
authorTomas Vondra <tomas@2ndquadrant.com>
Sun, 11 Jun 2017 13:30:59 +0000 (15:30 +0200)
committerPavan Deolasee <pavan.deolasee@gmail.com>
Wed, 21 Jun 2017 05:52:55 +0000 (11:22 +0530)
With roundrobin node, the initial node was always set to the first node
in the list. That works fine when inserting many rows at once (e.g. with
INSERT SELECT), but with single-row inserts this puts all data on the
first node, resulting in unbalanced distribution.

This randomizes the choice of the initial node, so that with single-row
inserts the ROUNDROBIN behaves a bit like RANDOM distribution.

This also removes unnecessary srand() call from RelationBuildLocator(),
located after a call to rand().

src/backend/pgxc/locator/locator.c

index 2a87f40c4214cce118448d59eb56db87dbcebfa2..9c8e0507f301f9b93326d1863deccd4380b55cdd 100644 (file)
@@ -680,7 +680,6 @@ RelationBuildLocator(Relation rel)
                 */
                offset = compute_modulo(abs(rand()), list_length(relationLocInfo->rl_nodeList));
 
-               srand(time(NULL));
                relationLocInfo->roundRobinNode = relationLocInfo->rl_nodeList->head; /* initialize */
                for (j = 0; j < offset && relationLocInfo->roundRobinNode->next != NULL; j++)
                        relationLocInfo->roundRobinNode = relationLocInfo->roundRobinNode->next;
@@ -1008,7 +1007,8 @@ createLocator(char locatorType, RelationAccessType accessType,
                                                Assert(false);
                                                break;
                                }
-                               locator->roundRobinNode = -1;
+                               /* randomize choice of the initial node */
+                               locator->roundRobinNode = (abs(rand()) % locator->nodeCount) - 1;
                        }
                        else
                        {