Merge pull request #2904 from citusdata/sort_load_shard_placement_array

Sort load_shard_placement_array by worker name/port
pull/2864/head
Hadi Moshayedi 2019-08-21 14:44:37 -07:00 committed by GitHub
commit 3de851d3c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 3 deletions

View File

@ -40,6 +40,11 @@
#include "utils/palloc.h"
/* forward declaration of local functions */
static int CompareShardPlacementsByWorker(const void *leftElement,
const void *rightElement);
/* declarations for dynamic loading */
PG_FUNCTION_INFO_V1(load_shard_id_array);
PG_FUNCTION_INFO_V1(load_shard_interval_array);
@ -143,7 +148,7 @@ load_shard_placement_array(PG_FUNCTION_ARGS)
placementList = ShardPlacementList(shardId);
}
placementList = SortList(placementList, CompareShardPlacements);
placementList = SortList(placementList, CompareShardPlacementsByWorker);
placementCount = list_length(placementList);
placementDatumArray = palloc0(placementCount * sizeof(Datum));
@ -166,6 +171,35 @@ load_shard_placement_array(PG_FUNCTION_ARGS)
}
/*
* CompareShardPlacementsByWorker compares two shard placements by their
* worker node name and port.
*/
static int
CompareShardPlacementsByWorker(const void *leftElement, const void *rightElement)
{
const ShardPlacement *leftPlacement = *((const ShardPlacement **) leftElement);
const ShardPlacement *rightPlacement = *((const ShardPlacement **) rightElement);
int nodeNameCmp = strncmp(leftPlacement->nodeName, rightPlacement->nodeName,
WORKER_LENGTH);
if (nodeNameCmp != 0)
{
return nodeNameCmp;
}
else if (leftPlacement->nodePort > rightPlacement->nodePort)
{
return 1;
}
else if (leftPlacement->nodePort < rightPlacement->nodePort)
{
return -1;
}
return 0;
}
/*
* partition_column_id simply finds a distributed table using the provided Oid
* and returns the column_id of its partition column. If the specified table is

View File

@ -109,7 +109,7 @@ ERROR: could not find valid entry for shard 540005
SELECT load_shard_placement_array(540001, false);
load_shard_placement_array
-----------------------------------
{localhost:57638,localhost:57637}
{localhost:57637,localhost:57638}
(1 row)
-- only one of which is finalized
@ -123,7 +123,7 @@ SELECT load_shard_placement_array(540001, true);
SELECT load_shard_placement_array(540001, false);
load_shard_placement_array
-----------------------------------
{localhost:57638,localhost:57637}
{localhost:57637,localhost:57638}
(1 row)
-- should see column id of 'name'