Skip to content

Commit 5121a16

Browse files
authored
Add support to make cluster for valgrind (#689)
You can run cluster using valgrind with: ``` make cluster VALGRIND=1 ```
1 parent a795dee commit 5121a16

7 files changed

Lines changed: 50 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ docs/tikz/*.png
5050

5151
# Exclude our demo/test tmux directory
5252
tmux/
53+
valgrind/

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ RUN apt-get update \
3131
watch \
3232
lsof \
3333
psutils \
34+
valgrind \
3435
postgresql-common \
3536
postgresql-server-dev-${PGVERSION} \
3637
&& rm -rf /var/lib/apt/lists/*

Makefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ FSM = docs/fsm.png
5555
PDF = ./docs/_build/latex/pg_auto_failover.pdf
5656

5757
# Command line with DEBUG facilities
58-
PG_AUTOCTL = PG_AUTOCTL_DEBUG=1 ./src/bin/pg_autoctl/pg_autoctl
58+
VALGRIND ?=
59+
ifeq ($(VALGRIND),)
60+
BINPATH = ./src/bin/pg_autoctl/pg_autoctl
61+
PG_AUTOCTL = PG_AUTOCTL_DEBUG=1 ./src/bin/pg_autoctl/pg_autoctl
62+
else
63+
BINPATH = ./src/tools/pg_autoctl.valgrind
64+
PG_AUTOCTL = PG_AUTOCTL_DEBUG=1 ./src/tools/pg_autoctl.valgrind
65+
endif
66+
5967

6068
NODES ?= 2 # total count of Postgres nodes
6169
NODES_ASYNC ?= 0 # count of replication-quorum false nodes
@@ -181,6 +189,7 @@ $(TMUX_SCRIPT): bin
181189
--node-priorities $(NODES_PRIOS) \
182190
--sync-standbys $(NODES_SYNC_SB) \
183191
$(CLUSTER_OPTS) \
192+
--binpath $(BINPATH) \
184193
--layout $(TMUX_LAYOUT) > $@
185194

186195
tmux-script: $(TMUX_SCRIPT) ;
@@ -200,6 +209,7 @@ cluster: install tmux-clean
200209
--node-priorities $(NODES_PRIOS) \
201210
--sync-standbys $(NODES_SYNC_SB) \
202211
$(CLUSTER_OPTS) \
212+
--binpath $(BINPATH) \
203213
--layout $(TMUX_LAYOUT)
204214

205215

src/bin/pg_autoctl/cli_do_root.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ CommandLine do_tmux_script =
223223
" --node-priorities list of nodes priorities (50)\n"
224224
" --sync-standbys number-sync-standbys to set (0 or 1)\n"
225225
" --skip-pg-hba use --skip-pg-hba when creating nodes\n"
226-
" --layout tmux layout to use (even-vertical)",
226+
" --layout tmux layout to use (even-vertical)\n"
227+
" --binpath path to the pg_autoctl binary (current binary path)",
227228
cli_do_tmux_script_getopts,
228229
cli_do_tmux_script);
229230

@@ -238,7 +239,8 @@ CommandLine do_tmux_session =
238239
" --node-priorities list of nodes priorities (50)\n"
239240
" --sync-standbys number-sync-standbys to set (0 or 1)\n"
240241
" --skip-pg-hba use --skip-pg-hba when creating nodes\n"
241-
" --layout tmux layout to use (even-vertical)",
242+
" --layout tmux layout to use (even-vertical)\n"
243+
" --binpath path to the pg_autoctl binary (current binary path)",
242244
cli_do_tmux_script_getopts,
243245
cli_do_tmux_session);
244246

src/bin/pg_autoctl/cli_do_tmux.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ cli_do_tmux_script_getopts(int argc, char **argv)
212212
{ "sync-standbys", required_argument, NULL, 's' },
213213
{ "skip-pg-hba", required_argument, NULL, 'S' },
214214
{ "layout", required_argument, NULL, 'l' },
215+
{ "binpath", required_argument, NULL, 'b' },
215216
{ "version", no_argument, NULL, 'V' },
216217
{ "verbose", no_argument, NULL, 'v' },
217218
{ "quiet", no_argument, NULL, 'q' },
@@ -229,6 +230,7 @@ cli_do_tmux_script_getopts(int argc, char **argv)
229230
options.skipHBA = false;
230231
strlcpy(options.root, "/tmp/pgaf/tmux", sizeof(options.root));
231232
strlcpy(options.layout, "even-vertical", sizeof(options.layout));
233+
strlcpy(options.binpath, pg_autoctl_argv0, sizeof(options.binpath));
232234

233235
if (!parseCandidatePriorities("", options.priorities))
234236
{
@@ -345,6 +347,13 @@ cli_do_tmux_script_getopts(int argc, char **argv)
345347
break;
346348
}
347349

350+
case 'b':
351+
{
352+
strlcpy(options.binpath, optarg, MAXPGPATH);
353+
log_trace("--binpath %s", options.binpath);
354+
break;
355+
}
356+
348357
case 'h':
349358
{
350359
commandline_help(stderr);
@@ -406,6 +415,7 @@ cli_do_tmux_script_getopts(int argc, char **argv)
406415
exit(EXIT_CODE_BAD_ARGS);
407416
}
408417

418+
409419
if (errors > 0)
410420
{
411421
commandline_help(stderr);
@@ -622,6 +632,7 @@ tmux_add_new_session(PQExpBuffer script, const char *root, int pgport)
622632
void
623633
tmux_pg_autoctl_create_monitor(PQExpBuffer script,
624634
const char *root,
635+
const char *binpath,
625636
int pgport,
626637
bool skipHBA)
627638
{
@@ -637,7 +648,7 @@ tmux_pg_autoctl_create_monitor(PQExpBuffer script,
637648

638649
tmux_add_send_keys_command(script,
639650
"%s create monitor %s --run",
640-
pg_autoctl_argv0,
651+
binpath,
641652
pg_ctl_opts);
642653
}
643654

@@ -649,6 +660,7 @@ tmux_pg_autoctl_create_monitor(PQExpBuffer script,
649660
void
650661
tmux_pg_autoctl_create_postgres(PQExpBuffer script,
651662
const char *root,
663+
const char *binpath,
652664
int pgport,
653665
const char *name,
654666
bool replicationQuorum,
@@ -665,7 +677,7 @@ tmux_pg_autoctl_create_postgres(PQExpBuffer script,
665677

666678
sformat(monitor, sizeof(monitor),
667679
"$(%s show uri --pgdata %s/monitor --formation monitor)",
668-
pg_autoctl_argv0,
680+
binpath,
669681
root);
670682

671683
tmux_add_send_keys_command(script,
@@ -681,7 +693,7 @@ tmux_pg_autoctl_create_postgres(PQExpBuffer script,
681693
"--replication-quorum %s "
682694
"--candidate-priority %d "
683695
"--run",
684-
pg_autoctl_argv0,
696+
binpath,
685697
pg_ctl_opts,
686698
monitor,
687699
name,
@@ -712,7 +724,8 @@ prepare_tmux_script(TmuxOptions *options, PQExpBuffer script)
712724

713725
/* start a monitor */
714726
(void) tmux_add_xdg_environment(script);
715-
tmux_pg_autoctl_create_monitor(script, root, pgport++, options->skipHBA);
727+
tmux_pg_autoctl_create_monitor(script, root, options->binpath, pgport++,
728+
options->skipHBA);
716729

717730
/* start the Postgres nodes, using the monitor URI */
718731
sformat(previousName, sizeof(previousName), "monitor");
@@ -733,12 +746,13 @@ prepare_tmux_script(TmuxOptions *options, PQExpBuffer script)
733746
tmux_add_send_keys_command(script,
734747
"PG_AUTOCTL_DEBUG=1 "
735748
"%s do tmux wait --root %s %s",
736-
pg_autoctl_argv0,
749+
options->binpath,
737750
options->root,
738751
previousName);
739752

740753
tmux_pg_autoctl_create_postgres(script,
741754
root,
755+
options->binpath,
742756
node->pgport,
743757
node->name,
744758
node->replicationQuorum,
@@ -757,12 +771,12 @@ prepare_tmux_script(TmuxOptions *options, PQExpBuffer script)
757771
tmux_add_send_keys_command(script,
758772
"PG_AUTOCTL_DEBUG=1 "
759773
"%s do tmux wait --root %s %s",
760-
pg_autoctl_argv0,
774+
options->binpath,
761775
options->root,
762776
"monitor");
763777
tmux_add_send_keys_command(script,
764778
"watch -n 0.2 %s show state",
765-
pg_autoctl_argv0);
779+
options->binpath);
766780

767781
/* add a window for interactive pg_autoctl commands */
768782
tmux_add_command(script, "split-window -v");
@@ -785,15 +799,15 @@ prepare_tmux_script(TmuxOptions *options, PQExpBuffer script)
785799
tmux_add_send_keys_command(script,
786800
"PG_AUTOCTL_DEBUG=1 "
787801
"%s do tmux wait --root %s %s %s",
788-
pg_autoctl_argv0,
802+
options->binpath,
789803
options->root,
790804
firstNode,
791805
NodeStateToString(targetPrimaryState));
792806

793807
/* PGDATA has just been exported, rely on it */
794808
tmux_add_send_keys_command(script,
795809
"%s set formation number-sync-standbys %d",
796-
pg_autoctl_argv0,
810+
options->binpath,
797811
options->numSync);
798812
}
799813

src/bin/pg_autoctl/cli_do_tmux.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef struct TmuxOptions
3838
int numSync; /* number-sync-standbys */
3939
bool skipHBA; /* do we want to use --skip-pg-hba? */
4040
char layout[BUFSIZE];
41+
char binpath[MAXPGPATH];
4142
} TmuxOptions;
4243

4344
typedef struct TmuxNode
@@ -78,11 +79,13 @@ bool tmux_prepare_XDG_environment(const char *root,
7879

7980
void tmux_pg_autoctl_create_monitor(PQExpBuffer script,
8081
const char *root,
82+
const char *binpath,
8183
int pgport,
8284
bool skipHBA);
8385

8486
void tmux_pg_autoctl_create_postgres(PQExpBuffer script,
8587
const char *root,
88+
const char *binpath,
8689
int pgport,
8790
const char *name,
8891
bool replicationQuorum,

src/tools/pg_autoctl.valgrind

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
script_dir="${0%/*}"
4+
pid=$$
5+
mkdir -p valgrind
6+
exec /usr/bin/valgrind --quiet --leak-check=yes --error-markers=VALGRINDERROR-BEGIN,VALGRINDERROR-END --max-stackframe=16000000 --log-file=./valgrind/log.${pid} "${script_dir}/../bin/pg_autoctl/pg_autoctl" "$@"
7+

0 commit comments

Comments
 (0)