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

Commit 5ce4eb8

Browse files
committed
Logging node id, adding and moving replicas in progress.
1 parent 45b8468 commit 5ce4eb8

13 files changed

+235
-55
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ DATA_built = $(EXTENSION)--$(EXTVERSION).sql
66

77

88
MODULE_big = pg_shardman
9-
OBJS = src/pg_shardman.o src/udf.o src/shard.o src/timeutils.o
9+
OBJS = src/pg_shardman.o src/udf.o src/shard.o src/timeutils.o \
10+
src/shardman_hooks.o
1011

1112
PG_CPPFLAGS += -Isrc/include
1213

@@ -21,7 +22,6 @@ PG_CPPFLAGS += -I$(INCLUDEDIR) # add server's include directory for libpq-fe.h
2122
SHLIB_LINK += -lpq # add libpq
2223

2324
EXTRA_CLEAN = $(EXTENSION)--$(EXTVERSION).sql
24-
$(info $$var is [${EXTRA_CLEAN}])
2525

2626
include $(PGXS)
2727

bin/common.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set -e
77
pgpath=~/postgres/install/vanilla/
88
pathmanpath=~/postgres/pg_pathman
99
install_pathman=false
10+
logfile=$HOME/tmp/tmp/tmp.log
1011

1112
master_datadir=~/postgres/data1
1213
master_port=5432
@@ -31,9 +32,9 @@ function start_nodes()
3132
for ((i=0; i<${#worker_datadirs[@]}; ++i)); do
3233
datadir="${worker_datadirs[i]}"
3334
port="${worker_ports[i]}"
34-
pg_ctl -o "-p $port" -D $datadir start
35+
pg_ctl -o "-p $port" -D $datadir -l $logfile start
3536
done
36-
pg_ctl -o "-p $master_port" -D $master_datadir start
37+
pg_ctl -o "-p $master_port" -D $master_datadir -l $logfile start
3738
}
3839

3940
function stop_nodes()
@@ -50,9 +51,9 @@ function restart_nodes()
5051
for ((i=0; i<${#worker_datadirs[@]}; ++i)); do
5152
datadir="${worker_datadirs[i]}"
5253
port="${worker_ports[i]}"
53-
pg_ctl -o "-p $port" -D $datadir restart
54+
pg_ctl -o "-p $port" -D $datadir -l $logfile restart
5455
done
55-
pg_ctl -o "-p $master_port" -D $master_datadir restart
56+
pg_ctl -o "-p $master_port" -D $master_datadir -l $logfile restart
5657
}
5758

5859
function run_demo()

bin/shardman_init.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ cd "${script_dir}/.."
1313
make clean
1414
make install
1515

16+
> $logfile
17+
1618
stop_nodes
1719
for datadir in $master_datadir "${worker_datadirs[@]}"; do
1820
rm -rf "$datadir"
@@ -37,4 +39,4 @@ restart_nodes
3739

3840
run_demo
3941

40-
psql
42+
# psql

bin/shardman_start.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ source "${script_dir}/common.sh"
66
cd "${script_dir}/.."
77
make clean
88

9+
> $logfile
10+
911
restart_nodes # make sure nodes run
1012
# first workers, then master
1113
for port in "${worker_ports[@]}" $master_port; do
@@ -23,4 +25,4 @@ restart_nodes
2325

2426
run_demo
2527

26-
psql
28+
# psql

membership.sql

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,30 @@ CREATE TABLE local_meta (
4646
k text NOT NULL, -- key
4747
v text -- value
4848
);
49-
INSERT INTO @extschema@.local_meta VALUES ('node_id', NULL);
49+
INSERT INTO shardman.local_meta VALUES ('node_id', NULL);
5050

5151
-- Get local node id. NULL means node is not in the cluster yet.
5252
CREATE FUNCTION get_node_id() RETURNS int AS $$
53-
SELECT v::int FROM @extschema@.local_meta WHERE k = 'node_id';
53+
SELECT v::int FROM shardman.local_meta WHERE k = 'node_id';
5454
$$ LANGUAGE sql;
5555

5656
-- Exclude node from the cluster
5757
CREATE FUNCTION reset_node_id() RETURNS void AS $$
58-
UPDATE @extschema@.local_meta SET v = NULL WHERE k = 'node_id';
59-
$$ LANGUAGE sql;
58+
BEGIN
59+
UPDATE shardman.local_meta SET v = NULL WHERE k = 'node_id';
60+
PERFORM shardman.reset_node_id_c();
61+
END $$ LANGUAGE plpgsql STRICT;
62+
CREATE FUNCTION reset_node_id_c() RETURNS void
63+
AS 'pg_shardman' LANGUAGE C;
6064

6165
-- Set local node id.
6266
CREATE FUNCTION set_node_id(node_id int) RETURNS void AS $$
63-
UPDATE @extschema@.local_meta SET v = node_id WHERE k = 'node_id';
64-
$$ LANGUAGE sql;
67+
BEGIN
68+
UPDATE shardman.local_meta SET v = node_id WHERE k = 'node_id';
69+
PERFORM shardman.set_node_id_c(node_id);
70+
END $$ LANGUAGE plpgsql STRICT;
71+
CREATE FUNCTION set_node_id_c(node_id int) RETURNS void
72+
AS 'pg_shardman' LANGUAGE C;
6573

6674
-- If for cmd cmd_id we haven't yet inserted new node, do that; mark it as
6775
-- passive for now, we still need to setup lr and set its id on the node itself.

postgresql.conf.common.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
shared_preload_libraries = 'pg_pathman, pg_shardman'
22

3+
# print node ids in log messages
4+
log_line_prefix = '%m %z'
5+
36
# just to suppress logging
47
autovacuum = off
58

shard.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ CREATE TRIGGER new_table_master_side AFTER INSERT ON shardman.tables
6565
CREATE TABLE partitions (
6666
part_name text,
6767
-- Shard number. 0 means primary shard.
68-
num int NOT NULL,
68+
num serial,
6969
nxt int,
7070
prev int,
7171
relation text NOT NULL REFERENCES tables(relation),

src/include/pg_shardman.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ extern char *shardman_master_connstring;
2828
extern int shardman_cmd_retry_naptime;
2929
extern int shardman_poll_interval;
3030

31+
extern int32 shardman_my_node_id;
32+
#define SHMN_INVALID_NODE_ID -1
33+
3134
typedef struct Cmd
3235
{
3336
int64 id;

src/include/shardman_hooks.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef SHARDMAN_HOOKS_H
2+
#define SHARDMAN_HOOKS_H
3+
4+
extern emit_log_hook_type log_hook_next;
5+
6+
extern void shardman_log_hook(ErrorData *edata);
7+
8+
#endif /* SHARDMAN_HOOKS_H */

src/pg_shardman.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "pg_shardman.h"
3131
#include "shard.h"
32+
#include "shardman_hooks.h"
3233

3334

3435
/* ensure that extension won't load against incompatible version of Postgres */
@@ -63,6 +64,8 @@ int shardman_poll_interval;
6364
* cleanup after receiving SIGTERM.
6465
*/
6566
static PGconn *conn = NULL;
67+
/* This node id. */
68+
int32 shardman_my_node_id = -1;
6669

6770
/*
6871
* Entrypoint of the module. Define variables and register background worker.
@@ -79,6 +82,10 @@ _PG_init()
7982
errhint("Add pg_shardman to shared_preload_libraries.")));
8083
}
8184

85+
/* remember & set hooks */
86+
log_hook_next = emit_log_hook;
87+
emit_log_hook = shardman_log_hook;
88+
8289
DefineCustomBoolVariable("shardman.master",
8390
"This node is the master?",
8491
NULL,
@@ -184,10 +191,10 @@ shardmaster_main(Datum main_arg)
184191
while ((cmd = next_cmd()) != NULL)
185192
{
186193
update_cmd_status(cmd->id, "in progress");
187-
shmn_elog(LOG, "Working on command %ld, %s, opts are",
194+
shmn_elog(DEBUG1, "Working on command %ld, %s, opts are",
188195
cmd->id, cmd->cmd_type);
189196
for (char **opts = cmd->opts; *opts; opts++)
190-
shmn_elog(LOG, "%s", *opts);
197+
shmn_elog(DEBUG1, "%s", *opts);
191198
if (strcmp(cmd->cmd_type, "add_node") == 0)
192199
add_node(cmd);
193200
else if (strcmp(cmd->cmd_type, "rm_node") == 0)
@@ -577,7 +584,7 @@ add_node(Cmd *cmd)
577584
pfree(sql);
578585

579586
/* done */
580-
elog(INFO, "Node %s successfully added, it is assigned id %d",
587+
shmn_elog(INFO, "Node %s successfully added, it is assigned id %d",
581588
connstr, node_id);
582589
return;
583590

0 commit comments

Comments
 (0)