Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c79b6e0

Browse files
committed
Checking that shard already exists in create_replica
1 parent f01424c commit c79b6e0

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

bin/common.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ function run_demo()
6868
psql -c "select shardman.create_hash_partitions(2, 'pt', 'id', 2);"
6969

7070
psql -c "select shardman.add_node('port=5435');"
71-
psql -c "select shardman.move_primary('pt_0', 4);"
72-
psql -c "select shardman.create_replica('pt_0', 2);"
71+
# psql -c "select shardman.move_primary('pt_0', 4);"
72+
# psql -c "select shardman.create_replica('pt_0', 2);"
7373
}

init.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ BEGIN
124124
RETURN c_id;
125125
END $$ LANGUAGE plpgsql;
126126

127+
-- Move primary or replica partition to another node. Params:
128+
-- 'part_name' is name of the partition to move
129+
-- 'src' is id of the node with partition
130+
-- 'dest' is id of the destination node
131+
CREATE FUNCTION move_part(part_name text, src int, dest int) RETURNS int AS $$
132+
DECLARE
133+
c_id int;
134+
BEGIN
135+
INSERT INTO @extschema@.cmd_log VALUES (DEFAULT, 'move_primary')
136+
RETURNING id INTO c_id;
137+
INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, part_name);
138+
INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, dest);
139+
RETURN c_id;
140+
END $$ LANGUAGE plpgsql;
141+
127142
-- Internal functions
128143

129144
-- Called on shardmaster bgw start. Add itself to nodes table, set id, create

src/shard.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ move_primary(Cmd *cmd)
404404
int32 dst_node = atoi(cmd->opts[1]);
405405

406406
CopyPartState **tasks = palloc(sizeof(CopyPartState*));
407-
MovePrimaryState *mps = palloc(sizeof(MovePrimaryState));
407+
MovePrimaryState *mps = palloc0(sizeof(MovePrimaryState));
408408
tasks[0] = (CopyPartState *) mps;
409409
init_mp_state(mps, part_name, dst_node);
410410

@@ -450,7 +450,6 @@ init_mp_state(MovePrimaryState *mps, const char *part_name, int32 dst_node)
450450
*/
451451
mps->rep_connstr = get_worker_node_connstr(mps->rep_node);
452452
}
453-
mps->rep_conn = NULL;
454453

455454
mps->create_data_pub_sql = psprintf(
456455
"select shardman.primary_moved_create_data_pub('%s', %d, %d);",
@@ -470,7 +469,8 @@ create_replica(Cmd *cmd)
470469
int32 dst_node = atoi(cmd->opts[1]);
471470

472471
CopyPartState **tasks = palloc(sizeof(CopyPartState*));
473-
CreateReplicaState *crs = palloc(sizeof(CreateReplicaState));
472+
/* palloc0 is important to set ptrs to NULL */
473+
CreateReplicaState *crs = palloc0(sizeof(CreateReplicaState));
474474
tasks[0] = (CopyPartState *) crs;
475475
init_cr_state(crs, part_name, dst_node);
476476

@@ -484,6 +484,23 @@ create_replica(Cmd *cmd)
484484
void
485485
init_cr_state(CreateReplicaState *crs, const char *part_name, int32 dst_node)
486486
{
487+
char *sql;
488+
uint64 shard_exists;
489+
490+
/* Check that table with such name is not already exists on dst node */
491+
sql = psprintf(
492+
"select owner from shardman.partitions where part_name = '%s' and owner = %d",
493+
part_name, dst_node);
494+
shard_exists = void_spi(sql);
495+
if (shard_exists)
496+
{
497+
shmn_elog(WARNING,
498+
"Shard %s already exists on node %d, won't create replica on it.",
499+
part_name, dst_node);
500+
crs->cp.res = TASK_FAILED;
501+
return;
502+
}
503+
487504
/* Set up fields neccesary to call init_cp_state */
488505
crs->cp.dst_node = dst_node;
489506
crs->cp.part_name = part_name;
@@ -545,8 +562,6 @@ init_cp_state(CopyPartState *cps)
545562
cps->res = TASK_FAILED;
546563
return;
547564
}
548-
cps->src_conn = NULL;
549-
cps->dst_conn = NULL;
550565

551566
/* constant strings */
552567
cps->logname = psprintf("shardman_copy_%s_%d_%d",

0 commit comments

Comments
 (0)