Error when shard can't be safely moved

Fixes #7426
pull/7467/head
Filip Sedlák 2024-01-28 00:09:14 +01:00
parent 9c243d4477
commit fb23e04574
3 changed files with 69 additions and 0 deletions

View File

@ -294,6 +294,17 @@ citus_move_shard_placement(PG_FUNCTION_ARGS)
CheckCitusVersion(ERROR);
EnsureCoordinator();
List *referenceTableIdList = NIL;
if (HasNodesWithMissingReferenceTables(&referenceTableIdList))
{
ereport(ERROR, (errmsg("there are missing reference tables on some nodes"),
errhint("Copy reference tables first with "
"replicate_reference_tables() or use "
"citus_rebalance_start() that will do it automatically."
)));
}
int64 shardId = PG_GETARG_INT64(0);
char *sourceNodeName = text_to_cstring(PG_GETARG_TEXT_P(1));
int32 sourceNodePort = PG_GETARG_INT32(2);

View File

@ -2416,6 +2416,13 @@ SELECT create_reference_table('r1');
(1 row)
CREATE TABLE d1 (a int PRIMARY KEY, b int);
SELECT create_distributed_table('d1', 'a');
create_distributed_table
---------------------------------------------------------------------
(1 row)
ALTER SEQUENCE pg_dist_groupid_seq RESTART WITH 15;
SELECT 1 from master_add_node('localhost', :worker_2_port);
?column?
@ -2434,6 +2441,35 @@ WHERE logicalrelid = 'r1'::regclass;
1
(1 row)
-- #7426 We can't move shards to the fresh node before we copy reference tables there.
-- rebalance_table_shards() will do the copy, but the low-level
-- citus_move_shard_placement() should raise an error
SELECT citus_move_shard_placement(pg_dist_shard.shardid, nodename, nodeport, 'localhost', :worker_2_port)
FROM pg_dist_shard JOIN pg_dist_shard_placement USING (shardid)
WHERE logicalrelid = 'd1'::regclass AND nodename = 'localhost' AND nodeport = :worker_1_port LIMIT 1;
ERROR: there are missing reference tables on some nodes
SELECT replicate_reference_tables();
replicate_reference_tables
---------------------------------------------------------------------
(1 row)
-- After replication, the move should succeed.
SELECT citus_move_shard_placement(pg_dist_shard.shardid, nodename, nodeport, 'localhost', :worker_2_port)
FROM pg_dist_shard JOIN pg_dist_shard_placement USING (shardid)
WHERE logicalrelid = 'd1'::regclass AND nodename = 'localhost' AND nodeport = :worker_1_port LIMIT 1;
citus_move_shard_placement
---------------------------------------------------------------------
(1 row)
-- And a JOIN with the reference table should work.
SELECT 1 as dummy FROM d1 JOIN r1 USING (a);
dummy
---------------------------------------------------------------------
(0 rows)
DROP TABLE d1;
-- rebalance with _only_ a reference table, this should trigger the copy
SELECT rebalance_table_shards();
rebalance_table_shards

View File

@ -1349,6 +1349,9 @@ SELECT public.wait_until_metadata_sync(30000);
CREATE TABLE r1 (a int PRIMARY KEY, b int);
SELECT create_reference_table('r1');
CREATE TABLE d1 (a int PRIMARY KEY, b int);
SELECT create_distributed_table('d1', 'a');
ALTER SEQUENCE pg_dist_groupid_seq RESTART WITH 15;
SELECT 1 from master_add_node('localhost', :worker_2_port);
@ -1359,6 +1362,25 @@ FROM pg_dist_shard
JOIN pg_dist_shard_placement USING (shardid)
WHERE logicalrelid = 'r1'::regclass;
-- #7426 We can't move shards to the fresh node before we copy reference tables there.
-- rebalance_table_shards() will do the copy, but the low-level
-- citus_move_shard_placement() should raise an error
SELECT citus_move_shard_placement(pg_dist_shard.shardid, nodename, nodeport, 'localhost', :worker_2_port)
FROM pg_dist_shard JOIN pg_dist_shard_placement USING (shardid)
WHERE logicalrelid = 'd1'::regclass AND nodename = 'localhost' AND nodeport = :worker_1_port LIMIT 1;
SELECT replicate_reference_tables();
-- After replication, the move should succeed.
SELECT citus_move_shard_placement(pg_dist_shard.shardid, nodename, nodeport, 'localhost', :worker_2_port)
FROM pg_dist_shard JOIN pg_dist_shard_placement USING (shardid)
WHERE logicalrelid = 'd1'::regclass AND nodename = 'localhost' AND nodeport = :worker_1_port LIMIT 1;
-- And a JOIN with the reference table should work.
SELECT 1 as dummy FROM d1 JOIN r1 USING (a);
DROP TABLE d1;
-- rebalance with _only_ a reference table, this should trigger the copy
SELECT rebalance_table_shards();
CALL citus_cleanup_orphaned_resources();