forked from mysql/mysql-docker
-
Notifications
You must be signed in to change notification settings - Fork 7
/
build_run_docker.sh
executable file
·961 lines (815 loc) · 33.9 KB
/
build_run_docker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
#!/bin/bash
# Generating RonDB clusters of variable sizes with docker compose
# Copyright (c) 2022, 2023 Hopsworks AB and/or its affiliates.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
## This file does the following
## i. Builds the Docker image of RonDB
## i. Generates a config.ini & my.cnf file
## i. Creates docker-compose file
## i. Runs docker-compose
## i. Optionally runs a benchmark
# shellcheck disable=SC2059
set -e
# https://stackoverflow.com/a/246128/9068781
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
# Repo version
VERSION="$(< "$SCRIPT_DIR/VERSION" sed -e 's/^[[:space:]]*//' -e '/-SNAPSHOT$/s/.*/dev/' ./VERSION)"
################
### Defaults ###
################
NUM_MGM_NODES=1
NUM_MYSQLD_NODES=0
NUM_REST_API_NODES=0
NUM_BENCH_NODES=0
REPLICATION_FACTOR=1
NODE_GROUPS=1
RUN_BENCHMARK=
VOLUME_TYPE=docker
SAVE_SAMPLE_FILES=
DETACHED=
RONDB_SIZE=small
SQL_INIT_SCRIPT_CLI_DIR=$SCRIPT_DIR/resources/sql_init_scripts
RONDB_IMAGE_NAME=rondb
RONDB_VERSION=22.10.5
function print_usage() {
cat <<EOF
RonDB-Docker version: $VERSION
Usage: $0
[-h --help ]
[-v --rondb-version <string>
Default: $RONDB_VERSION ]
[-in --rondb-image-name <string>
Default: $RONDB_IMAGE_NAME ]
[-tp --rondb-tarball-path <string>
Build Dockerfile with a local tarball
Default: pull image from Dockerhub ]
[-tu --rondb-tarball-url <string>
Build Dockerfile with a remote tarball
Default: pull image from Dockerhub ]
[-m --num-mgm-nodes <int> ]
[-g --node-groups <int> ]
[-r --replication-factor <int> ]
[-my --num-mysql-nodes <int> ]
[-ra --num-rest-api-nodes <int> ]
[-bn --num-benchmarking-nodes <int> ]
[-b --run-benchmark <string>
Options: <sysbench_single, sysbench_multi,
dbt2_single> ]
[-lv --volumes-in-local-dir ]
[-sf --save-sample-files ]
[-d --detached ]
[-s --size <string>
Options: <mini, small, medium, large, xlarge>
Default: $RONDB_SIZE
The size of the machine that you are running
this script from.
This parameter is only intended for the
wrapper script run.sh in order to easily create
clusters that consume varying resources. ]
[-sql --sql-init-scripts-dir <string>
Directory with SQL scripts that will be read
in entrypoint with root privileges.
Default: $SQL_INIT_SCRIPT_CLI_DIR
Set this to an empty string if not wanted. ]
[-su --suffix
The suffix to add to the project name. Add a
suffix if you want to run several clusters in
parallel. ]
EOF
}
if [ -z "$1" ]; then
print_usage
exit 1
fi
#######################
#### CLI Arguments ####
#######################
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h | --help)
print_usage
exit 0
;;
-v | --rondb-version)
RONDB_VERSION="$2"
shift # past argument
shift # past value
;;
-in | --rondb-image-name)
RONDB_IMAGE_NAME="$2"
shift # past argument
shift # past value
;;
-tp | --rondb-tarball-path)
RONDB_TARBALL_PATH="$2"
shift # past argument
shift # past value
;;
-tu | --rondb-tarball-url)
RONDB_TARBALL_URL="$2"
shift # past argument
shift # past value
;;
-m | --num-mgm-nodes)
NUM_MGM_NODES="$2"
shift # past argument
shift # past value
;;
-g | --node-groups)
NODE_GROUPS="$2"
shift # past argument
shift # past value
;;
-r | --replication-factor)
REPLICATION_FACTOR="$2"
shift # past argument
shift # past value
;;
-my | --num-mysql-nodes)
NUM_MYSQLD_NODES="$2"
shift # past argument
shift # past value
;;
-ra | --num-rest-api-nodes)
NUM_REST_API_NODES="$2"
shift # past argument
shift # past value
;;
-bn | --num-benchmarking-nodes)
NUM_BENCH_NODES="$2"
shift # past argument
shift # past value
;;
-b | --run-benchmark)
RUN_BENCHMARK="$2"
shift # past argument
shift # past value
;;
-s | --size)
RONDB_SIZE="$2"
shift # past argument
shift # past value
;;
-sql | --sql-init-scripts-dir)
SQL_INIT_SCRIPT_CLI_DIR="$2"
shift # past argument
shift # past value
;;
-d | --detached)
DETACHED="-d"
shift # past argument
;;
-lv | --volumes-in-local-dir)
VOLUME_TYPE=local
shift # past argument
;;
-sf | --save-sample-files)
SAVE_SAMPLE_FILES=1
shift # past argument
;;
-su | --suffix)
USER_SUFFIX="_$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
print-parsed-arguments() {
echo "RonDB-Docker version: $VERSION"
echo
echo "#################"
echo "Parsed arguments:"
echo "#################"
echo
echo "RonDB version = ${RONDB_VERSION}"
echo "RonDB image name = ${RONDB_IMAGE_NAME}"
echo "RonDB tarball path = ${RONDB_TARBALL_PATH}"
echo "RonDB tarball url = ${RONDB_TARBALL_URL}"
echo "Number of management nodes = ${NUM_MGM_NODES}"
echo "Node groups = ${NODE_GROUPS}"
echo "Replication factor = ${REPLICATION_FACTOR}"
echo "Number of MySQLd nodes = ${NUM_MYSQLD_NODES}"
echo "Number of REST API nodes = ${NUM_REST_API_NODES}"
echo "Number of benchmarking nodes = ${NUM_BENCH_NODES}"
echo "Run benchmark = ${RUN_BENCHMARK}"
echo "Volume type docker/local = ${VOLUME_TYPE}"
echo "SQL Init scripts directory = ${SQL_INIT_SCRIPT_CLI_DIR}"
echo "Save sample files = ${SAVE_SAMPLE_FILES}"
echo "Run detached = ${DETACHED}"
echo "Run with RonDB size = ${RONDB_SIZE}"
echo "User suffix = ${USER_SUFFIX}"
}
print-parsed-arguments
set -- "${POSITIONAL[@]}" # restore unknown options
if [[ -n $1 ]]; then
echo "##################" >&2
echo "Illegal arguments: $*" >&2
echo "##################" >&2
echo
print_usage
exit 1
fi
if [ -n "$RONDB_TARBALL_PATH" ] && [ -n "$RONDB_TARBALL_URL" ]; then
echo "Cannot specify both a RonDB tarball path and url" >&2
print_usage
exit 1
fi
if [ "$NUM_MGM_NODES" -lt 1 ]; then
echo "At least 1 mgmd is required" >&2
exit 1
elif [ "$REPLICATION_FACTOR" -lt 1 ] || [ "$REPLICATION_FACTOR" -gt 3 ]; then
echo "The replication factor has to be >=1 and <4; It is currently $REPLICATION_FACTOR" >&2
exit 1
elif [ "$NODE_GROUPS" -lt 1 ]; then
echo "At least 1 node group is required" >&2
exit 1
fi
if [ "$RONDB_SIZE" != "small" ] && \
[ "$RONDB_SIZE" != "mini" ] && \
[ "$RONDB_SIZE" != "medium" ] && \
[ "$RONDB_SIZE" != "large" ] && \
[ "$RONDB_SIZE" != "xlarge" ]; then
echo "size has to be one of <mini, small, medium, large, xlarge>" >&2
exit 1
fi
if [ -n "$RUN_BENCHMARK" ]; then
if [ "$RUN_BENCHMARK" != "sysbench_single" ] && \
[ "$RUN_BENCHMARK" != "sysbench_multi" ] && \
[ "$RUN_BENCHMARK" != "dbt2_single" ]; then
echo "Benchmark has to be one of <sysbench_single, sysbench_multi, dbt2_single>" >&2
exit 1
elif [ $NUM_BENCH_NODES -lt 1 ]; then
echo "At least one bench node is required to run benchmarks" >&2
exit 1
elif [ $NUM_MYSQLD_NODES -lt 1 ]; then
echo "At least one MySQLd is required to run benchmarks" >&2
exit 1
fi
# This is not a hard requirement, but is better for benchmarking
# One benchmarking container can however also run multiple Sysbench instances against multiple MySQLd containers
if [ "$RUN_BENCHMARK" == "sysbench_multi" ]; then
if [ "$NUM_MYSQLD_NODES" -lt "$NUM_BENCH_NODES" ]; then
echo "For sysbench_multi, there should be at least as many MySQLd as benchmarking nodes" >&2
exit 1
fi
fi
if [ "$RUN_BENCHMARK" == "sysbench_multi" ] || [ "$RUN_BENCHMARK" == "dbt2_multi" ]; then
if [ "$NUM_MYSQLD_NODES" -lt 2 ]; then
echo "At least two MySQLds are required to run the multi-benchmarks" >&2
exit 1
fi
fi
if [ "$RUN_BENCHMARK" == "dbt2_single" ] || [ "$RUN_BENCHMARK" == "dbt2_multi" ]; then
if [ "$NUM_BENCH_NODES" -gt 1 ]; then
echo "Can only run dbt2 benchmarks with one api container" >&2
exit 1
fi
fi
# TODO: Make this work with BENCHMARK_SERVERS in sysbench_multi; This requires some
# care in synchronizing the benchmarking nodes when executing the benchmark.
if [ "$NUM_BENCH_NODES" -gt 1 ]; then
echo "Running more than one benchmarking container for Sysbench benchmarks is currently not supported" >&2
exit 1
fi
fi
source "$SCRIPT_DIR/environments/machine_sizes/$RONDB_SIZE.env"
# shellcheck source=./environments/common.env
source "$SCRIPT_DIR/environments/common.env"
# We use this for the docker-compose project name, which will not allow "."
RONDB_VERSION_NO_DOT=$(echo "$RONDB_VERSION" | tr -d '.')
## Uncomment this for quicker testing
# yes | docker container prune
# yes | docker volume prune
FILE_SUFFIX="v${RONDB_VERSION_NO_DOT}_m${NUM_MGM_NODES}_g${NODE_GROUPS}_r${REPLICATION_FACTOR}_my${NUM_MYSQLD_NODES}_ra${NUM_REST_API_NODES}_bn${NUM_BENCH_NODES}${USER_SUFFIX}"
AUTOGENERATED_FILES_DIR="$SCRIPT_DIR/autogenerated_files/$FILE_SUFFIX"
rm -rf $AUTOGENERATED_FILES_DIR
mkdir -p "$AUTOGENERATED_FILES_DIR"
PARSED_ARGUMENTS_FILEPATH="$AUTOGENERATED_FILES_DIR/parsed_arguments.txt"
print-parsed-arguments > "$PARSED_ARGUMENTS_FILEPATH"
DOCKER_COMPOSE_FILEPATH="$AUTOGENERATED_FILES_DIR/docker_compose.yml"
CONFIG_INI_FILEPATH="$AUTOGENERATED_FILES_DIR/config.ini"
MY_CNF_FILEPATH="$AUTOGENERATED_FILES_DIR/my.cnf"
REST_API_JSON_FILEPATH="$AUTOGENERATED_FILES_DIR/rest_api.json"
LOCAL_VOLUMES_DIR="$AUTOGENERATED_FILES_DIR/volumes"
SQL_INIT_SCRIPTS_DIR="$LOCAL_VOLUMES_DIR/sql_init_scripts"
mkdir -p "$SQL_INIT_SCRIPTS_DIR"
if [ "$SQL_INIT_SCRIPT_CLI_DIR" != "" ]; then
cp $SQL_INIT_SCRIPT_CLI_DIR/* "$SQL_INIT_SCRIPTS_DIR"
fi
# These directories will be mounted into the api containers
SYSBENCH_SINGLE_DIR="$LOCAL_VOLUMES_DIR/sysbench_single"
SYSBENCH_MULTI_DIR="$LOCAL_VOLUMES_DIR/sysbench_multi"
DBT2_SINGLE_DIR="$LOCAL_VOLUMES_DIR/dbt2_single"
DBT2_MULTI_DIR="$LOCAL_VOLUMES_DIR/dbt2_multi"
mkdir -p "$SYSBENCH_SINGLE_DIR" "$DBT2_SINGLE_DIR"
if [ "$NUM_MYSQLD_NODES" -gt 1 ]; then
mkdir -p "$SYSBENCH_MULTI_DIR" "$DBT2_MULTI_DIR"
fi
AUTOBENCH_SYS_SINGLE_FILEPATH="$SYSBENCH_SINGLE_DIR/autobench.conf"
AUTOBENCH_SYS_MULTI_FILEPATH="$SYSBENCH_MULTI_DIR/autobench.conf"
AUTOBENCH_DBT2_SINGLE_FILEPATH="$DBT2_SINGLE_DIR/autobench.conf"
AUTOBENCH_DBT2_MULTI_FILEPATH="$DBT2_MULTI_DIR/autobench.conf"
# Since we are mounting the entire benchmarking directories, these files would be
# overwritten if they are added via the Dockerfile.
DBT2_CONF_SINGLE_FILEPATH="$DBT2_SINGLE_DIR/dbt2_run_1.conf"
DBT2_CONF_MULTI_FILEPATH="$DBT2_MULTI_DIR/dbt2_run_1.conf"
if [ "$NUM_MYSQLD_NODES" -gt 0 ]; then
echo "$DBT2_RUN_SINGLE" > "$DBT2_CONF_SINGLE_FILEPATH"
if [ "$NUM_MYSQLD_NODES" -gt 1 ]; then
echo "$DBT2_RUN_MULTI" > "$DBT2_CONF_MULTI_FILEPATH"
fi
fi
DATA_DIR="/srv/hops/mysql-cluster"
BENCH_DIR="/srv/hops/benchmarks"
#######################
#######################
#######################
RONDB_IMAGE_ID="$RONDB_IMAGE_NAME:$RONDB_VERSION-$VERSION"
if [ ! -n "$RONDB_TARBALL_PATH" ] && [ ! -n "$RONDB_TARBALL_URL" ]; then
RONDB_IMAGE_ID="hopsworks/$RONDB_IMAGE_ID"
docker pull $RONDB_IMAGE_ID
else
echo "Building RonDB Docker image for local platform"
RONDB_TARBALL_URI=$RONDB_TARBALL_URL
RONDB_TARBALL_LOCAL_REMOTE=remote
if [ -n "$RONDB_TARBALL_PATH" ]; then
RONDB_TARBALL_URI=$RONDB_TARBALL_PATH
RONDB_TARBALL_LOCAL_REMOTE=local
fi
# We're not using this for cross-platform builds, so can use same argument twice
docker buildx build . \
--tag $RONDB_IMAGE_ID \
--build-arg RONDB_VERSION=$RONDB_VERSION \
--build-arg RONDB_TARBALL_LOCAL_REMOTE=$RONDB_TARBALL_LOCAL_REMOTE \
--build-arg RONDB_X86_TARBALL_URI=$RONDB_TARBALL_URI \
--build-arg RONDB_ARM_TARBALL_URI=$RONDB_TARBALL_URI
fi
#######################
#######################
#######################
echo "Loading templates"
CONFIG_INI_TEMPLATE=$(cat ./resources/config_templates/config.ini)
CONFIG_INI_MGMD_TEMPLATE=$(cat ./resources/config_templates/config_mgmd.ini)
CONFIG_INI_NDBD_TEMPLATE=$(cat ./resources/config_templates/config_ndbd.ini)
CONFIG_INI_MYSQLD_TEMPLATE=$(cat ./resources/config_templates/config_mysqld.ini)
CONFIG_INI_API_TEMPLATE=$(cat ./resources/config_templates/config_api.ini)
MY_CNF_TEMPLATE=$(cat ./resources/config_templates/my.cnf)
REST_API_CONFIG_TEMPLATE=$(cat ./resources/config_templates/rest_api.json)
AUTOBENCH_DBT2_TEMPLATE=$(cat ./resources/config_templates/autobench_dbt2.conf)
AUTOBENCH_SYSBENCH_TEMPLATE=$(cat ./resources/config_templates/autobench_sysbench.conf)
# Doing restart on-failure for the agent upgrade; we return a failure there
service-template() {
printf "
%s:
image: %s
container_name: %s
user: $(id -u):$(id -g)
" "$SERVICE_NAME" "$RONDB_IMAGE_ID" "$SERVICE_NAME";
}
DEPENDS_ON_FIELD="
depends_on:"
DEPENDS_ON_TEMPLATE="
%s:
condition: service_healthy"
VOLUMES_FIELD="
volumes:"
VOLUME_TEMPLATE="
- type: %s
source: %s
target: %s"
PORTS_FIELD="
ports:"
# HOST:CONTAINER
PORTS_TEMPLATE="
- %s:%s"
ENV_FIELD="
environment:
- HOST_GROUP_ID=$(id -g)"
ENV_VAR_TEMPLATE="
- %s=%s"
HEALTHCHECK_TEMPLATE="
healthcheck:
test: %s
interval: %ss
timeout: %ss
retries: %s
start_period: %ss"
COMMAND_TEMPLATE="
command: %s"
#######################
#######################
#######################
echo "Filling out templates"
CONFIG_INI=$(printf "$CONFIG_INI_TEMPLATE" \
"$CONFIG_INI_NumCPUs" \
"$CONFIG_INI_TotalMemoryConfig" \
"$CONFIG_INI_MaxNoOfTables" \
"$CONFIG_INI_MaxNoOfAttributes" \
"$CONFIG_INI_MaxNoOfTriggers" \
"$CONFIG_INI_TransactionMemory" \
"$CONFIG_INI_SharedGlobalMemory" \
"$CONFIG_INI_ReservedConcurrentOperations" \
"$CONFIG_INI_FragmentLogFileSize" \
"$REPLICATION_FACTOR" \
"$CONFIG_INI_MaxNoOfConcurrentOperations"
)
SINGLE_MGMD_IP=''
MGM_CONNECTION_STRING=''
MGMD_IPS=''
NDBD_IPS=()
SINGLE_MYSQLD_IP=''
MULTI_MYSQLD_IPS=''
# TODO: Use this for BENCHMARK_SERVERS in Sysbench
SINGLE_API_IP=''
MULTI_API_IPS=''
VOLUMES=()
# Add templated volume to `template` variable. Will create & mount docker
# volumes or local dirs depending on whether CLI argument `-lv` was provided.
add_volume_to_template() {
local VOLUME_NAME="$1"
local TARGET_DIR_PATH="$2"
if [ "$VOLUME_TYPE" == local ]; then
local VOLUME_DIR="$LOCAL_VOLUMES_DIR/$VOLUME_NAME"
mkdir -p "$VOLUME_DIR"
template+="$(printf "$VOLUME_TEMPLATE" bind "$VOLUME_DIR" "$TARGET_DIR_PATH")"
else
VOLUMES+=("$VOLUME_NAME")
template+="$(printf "$VOLUME_TEMPLATE" volume "$VOLUME_NAME" "$TARGET_DIR_PATH")"
fi
}
# Add templated volume to `template` variable. Will always bind local file
# irrespective of CLI argument `-lv`.
add_file_to_template() {
template+="$(printf "$VOLUME_TEMPLATE" bind "$1" "$2")"
}
# Adding the repo VERSION for easier reference in the documentation
BASE_DOCKER_COMPOSE_FILE="version: '3.8'
# RonDB-Docker version: $VERSION
services:"
for CONTAINER_NUM in $(seq "$NUM_MGM_NODES"); do
NODE_ID=$((65 + $((CONTAINER_NUM - 1))))
SERVICE_NAME="mgmd_${CONTAINER_NUM}${USER_SUFFIX}"
template="$(service-template)"
command=$(printf "$COMMAND_TEMPLATE" "[\"ndb_mgmd\", \"--ndb-nodeid=$NODE_ID\", \"--initial\"]")
template+="$command"
template+="
deploy:
resources:
limits:
cpus: '$MGMD_CPU_LIMIT'
memory: $MGMD_MEMORY_LIMIT
reservations:
memory: $MGMD_MEMORY_RESERVATION"
template+="$VOLUMES_FIELD"
add_file_to_template "$CONFIG_INI_FILEPATH" "$DATA_DIR/config.ini"
add_volume_to_template "dataDir_$SERVICE_NAME" "$DATA_DIR/mgmd"
add_volume_to_template "logDir_$SERVICE_NAME" "$DATA_DIR/log"
template+="$ENV_FIELD"
BASE_DOCKER_COMPOSE_FILE+="$template"
# NodeId, HostName, PortNumber, NodeActive, ArbitrationRank
SLOT=$(printf "$CONFIG_INI_MGMD_TEMPLATE" "$NODE_ID" "$SERVICE_NAME" "1186" "1" "2")
CONFIG_INI=$(printf "%s\n\n%s" "$CONFIG_INI" "$SLOT")
MGM_CONNECTION_STRING+="$SERVICE_NAME:1186,"
MGMD_IPS+="$SERVICE_NAME,"
if [ "$CONTAINER_NUM" -eq 1 ]; then
SINGLE_MGMD_IP+="$SERVICE_NAME"
fi
done
# Remove last comma from MGM_CONNECTION_STRING
MGM_CONNECTION_STRING=${MGM_CONNECTION_STRING%?}
MGMD_IPS=${MGMD_IPS%?}
# We're not bothering with inactive ndbds here
NUM_DATA_NODES=$((NODE_GROUPS * REPLICATION_FACTOR))
for CONTAINER_NUM in $(seq $NUM_DATA_NODES); do
NODE_ID=$CONTAINER_NUM
SERVICE_NAME="ndbd_${CONTAINER_NUM}${USER_SUFFIX}"
NDBD_IPS+=("$SERVICE_NAME")
template="$(service-template)"
command=$(printf "$COMMAND_TEMPLATE" "[\"ndbmtd\", \"--ndb-nodeid=$NODE_ID\", \"--initial\", \"--ndb-connectstring=$MGM_CONNECTION_STRING\"]")
template+="$command"
# interval, timeout, retries, start_period
healthcheck_command="./docker/rondb_standalone/healthcheck.sh $MGM_CONNECTION_STRING $NODE_ID"
healthcheck=$(printf "$HEALTHCHECK_TEMPLATE" "$healthcheck_command" "15" "15" "3" "20")
template+="$healthcheck"
template+="
deploy:
resources:
limits:
cpus: '$NDBD_CPU_LIMIT'
memory: $NDBD_MEMORY_LIMIT
reservations:
memory: $NDBD_MEMORY_RESERVATION"
template+="$VOLUMES_FIELD"
add_volume_to_template "dataDir_$SERVICE_NAME" "$DATA_DIR/ndb_data"
add_volume_to_template "logDir_$SERVICE_NAME" "$DATA_DIR/log"
template+="$ENV_FIELD"
BASE_DOCKER_COMPOSE_FILE+="$template"
NODE_GROUP=$((CONTAINER_NUM % NODE_GROUPS))
# NodeId, NodeGroup, NodeActive, HostName, ServerPort
SLOT=$(printf "$CONFIG_INI_NDBD_TEMPLATE" "$NODE_ID" "$NODE_GROUP" "1" "$SERVICE_NAME" "11860")
CONFIG_INI=$(printf "%s\n\n%s" "$CONFIG_INI" "$SLOT")
done
if [ "$NUM_MYSQLD_NODES" -gt 0 ]; then
for CONTAINER_NUM in $(seq "$NUM_MYSQLD_NODES"); do
SERVICE_NAME="mysqld_${CONTAINER_NUM}${USER_SUFFIX}"
template="$(service-template)"
command=$(printf "$COMMAND_TEMPLATE" "[\"mysqld\"]")
template+="$command"
# MySQLd needs this, or will otherwise complain "mbind: Operation not permitted".
template+="
cap_add:
- SYS_NICE"
# interval, timeout, retries, start_period
healthcheck=$(printf "$HEALTHCHECK_TEMPLATE" "mysqladmin ping -uroot" "10" "2" "6" "25")
template+="$healthcheck"
# Make sure these memory boundaries are allowed in Docker settings!
# To check whether they are being used use `docker stats`
template+="
deploy:
resources:
limits:
cpus: '$MYSQLD_CPU_LIMIT'
memory: $MYSQLD_MEMORY_LIMIT
reservations:
memory: $MYSQLD_MEMORY_RESERVATION"
template+="$VOLUMES_FIELD"
add_file_to_template "$MY_CNF_FILEPATH" "$DATA_DIR/my.cnf"
add_volume_to_template "dataDir_$SERVICE_NAME" "$DATA_DIR/mysql"
add_volume_to_template "mysqlFilesDir_$SERVICE_NAME" "$DATA_DIR/mysql-files"
if [ "$CONTAINER_NUM" -eq 1 ]; then
# Only need to run the files on one MySQLd
add_file_to_template "$SQL_INIT_SCRIPTS_DIR" "/srv/hops/docker/rondb_standalone/sql_init_scripts"
fi
template+="$PORTS_FIELD"
ports=$(printf "$PORTS_TEMPLATE" "$EXPOSE_MYSQLD_PORTS_STARTING_AT" "3306")
template+="$ports"
EXPOSE_MYSQLD_PORTS_STARTING_AT=$((EXPOSE_MYSQLD_PORTS_STARTING_AT + 1))
# Can add the following env vars to the MySQLd containers:
# MYSQL_DATABASE
template+="$ENV_FIELD"
template+="$(printf "$ENV_VAR_TEMPLATE" "MYSQL_ALLOW_EMPTY_PASSWORD" "true")"
template+="$(printf "$ENV_VAR_TEMPLATE" "MYSQL_ROOT_PASSWORD" "$MYSQL_ROOT_PASSWORD")"
template+="$(printf "$ENV_VAR_TEMPLATE" "MYSQL_BENCH_USER" "$MYSQL_BENCH_USER")"
template+="$(printf "$ENV_VAR_TEMPLATE" "MYSQL_BENCH_PASSWORD" "$MYSQL_BENCH_PASSWORD")"
if [ "$CONTAINER_NUM" -eq 1 ]; then
# Only need one mysqld to setup databases, users, etc.
template+="$(printf "$ENV_VAR_TEMPLATE" "MYSQL_INITIALIZE_DB" "1")"
fi
BASE_DOCKER_COMPOSE_FILE+="$template"
NODE_ID_OFFSET=$(($((CONTAINER_NUM - 1)) * MYSQLD_SLOTS_PER_CONTAINER))
for SLOT_NUM in $(seq "$MYSQLD_SLOTS_PER_CONTAINER"); do
NODE_ID=$((67 + NODE_ID_OFFSET + $((SLOT_NUM - 1))))
# NodeId, NodeActive, ArbitrationRank, HostName
SLOT=$(printf "$CONFIG_INI_MYSQLD_TEMPLATE" "$NODE_ID" "1" "1" "$SERVICE_NAME")
CONFIG_INI=$(printf "%s\n\n%s" "$CONFIG_INI" "$SLOT")
done
MULTI_MYSQLD_IPS+="$SERVICE_NAME;"
if [ "$CONTAINER_NUM" -eq 1 ]; then
SINGLE_MYSQLD_IP+="$SERVICE_NAME"
fi
done
fi
# Remove last semi-colon from MULTI_MYSQLD_IPS
MULTI_MYSQLD_IPS=${MULTI_MYSQLD_IPS%?}
FIRST_USEABLE_API_NODE_ID=195
if [ "$NUM_REST_API_NODES" -gt 0 ]; then
for CONTAINER_NUM in $(seq $NUM_REST_API_NODES); do
SERVICE_NAME="rest_${CONTAINER_NUM}${USER_SUFFIX}"
template="$(service-template)"
command=$(printf "$COMMAND_TEMPLATE" "[\"rdrs\", \"-config=$DATA_DIR/rest_api.json\"]")
template+="$command"
# Make sure these memory boundaries are allowed in Docker settings!
# To check whether they are being used use `docker stats`
template+="
deploy:
resources:
limits:
cpus: '$REST_API_CPU_LIMIT'
memory: $REST_API_MEMORY_LIMIT
reservations:
memory: $REST_API_MEMORY_RESERVATION"
template+="$VOLUMES_FIELD"
add_file_to_template "$REST_API_JSON_FILEPATH" "$DATA_DIR/rest_api.json"
# Open ports for REST API server
template+="$PORTS_FIELD"
ports=$(printf "$PORTS_TEMPLATE" "$EXPOSE_RDRS_REST_PORTS_STARTING_AT" "4406")
template+="$ports"
ports=$(printf "$PORTS_TEMPLATE" "$EXPOSE_RDRS_gRPC_PORTS_STARTING_AT" "5406")
template+="$ports"
EXPOSE_RDRS_REST_PORTS_STARTING_AT=$((EXPOSE_RDRS_REST_PORTS_STARTING_AT + 1))
EXPOSE_RDRS_gRPC_PORTS_STARTING_AT=$((EXPOSE_RDRS_gRPC_PORTS_STARTING_AT + 1))
template+="$ENV_FIELD"
# There are cases where the MySQLd is up, but the cluster is not.
# Also, we may not have MySQLds configured at all.
template+="$DEPENDS_ON_FIELD"
for NDBD_IP in "${NDBD_IPS[@]}"; do
depends_on=$(printf "$DEPENDS_ON_TEMPLATE" "$NDBD_IP")
template+="$depends_on"
done
if [ "$NUM_MYSQLD_NODES" -gt 0 ]; then
depends_on=$(printf "$DEPENDS_ON_TEMPLATE" "mysqld_1${USER_SUFFIX}")
template+="$depends_on"
fi
BASE_DOCKER_COMPOSE_FILE+="$template"
NODE_ID_OFFSET=$(($((CONTAINER_NUM - 1)) * API_SLOTS_PER_CONTAINER))
for SLOT_NUM in $(seq $API_SLOTS_PER_CONTAINER); do
NODE_ID=$((FIRST_USEABLE_API_NODE_ID + NODE_ID_OFFSET + $((SLOT_NUM - 1))))
# NodeId, NodeActive, ArbitrationRank, HostName
SLOT=$(printf "$CONFIG_INI_API_TEMPLATE" "$NODE_ID" "1" "1" "$SERVICE_NAME")
CONFIG_INI=$(printf "%s\n\n%s" "$CONFIG_INI" "$SLOT")
done
FIRST_USEABLE_API_NODE_ID=$(($NODE_ID + 1))
done
fi
if [ $NUM_BENCH_NODES -gt 0 ]; then
for CONTAINER_NUM in $(seq $NUM_BENCH_NODES); do
SERVICE_NAME="bench_${CONTAINER_NUM}${USER_SUFFIX}"
template="$(service-template)"
if [ -z "$RUN_BENCHMARK" ]; then
# Simply keep the API container running, so we can run benchmarks manually
command=$(printf "$COMMAND_TEMPLATE" "bash -c \"tail -F anything\"")
else
GENERATE_DBT2_DATA_FLAG=""
if [ "$RUN_BENCHMARK" == "dbt2_single" ] || [ "$RUN_BENCHMARK" == "dbt2_multi" ]; then
GENERATE_DBT2_DATA_FLAG="--generate-dbt2-data"
fi
# MySQLd might be up, but still not entirely ready yet..
command=$(printf "$COMMAND_TEMPLATE" ">
bash -c \"sleep 5 && bench_run.sh --verbose --default-directory $BENCH_DIR/$RUN_BENCHMARK $GENERATE_DBT2_DATA_FLAG\"")
fi
template+="$command"
# Make sure these memory boundaries are allowed in Docker settings!
# To check whether they are being used use `docker stats`
template+="
deploy:
resources:
limits:
cpus: '$BENCH_CPU_LIMIT'
memory: $BENCH_MEMORY_LIMIT
reservations:
memory: $BENCH_MEMORY_RESERVATION"
template+="$VOLUMES_FIELD"
if [ "$NUM_MYSQLD_NODES" -gt 0 ]; then
add_volume_to_template "sysbench_single" "$BENCH_DIR/sysbench_single"
add_volume_to_template "dbt2_single" "$BENCH_DIR/dbt2_single"
if [ "$NUM_MYSQLD_NODES" -gt 1 ]; then
add_volume_to_template "sysbench_multi" "$BENCH_DIR/sysbench_multi"
add_volume_to_template "dbt2_multi" "$BENCH_DIR/dbt2_multi"
fi
fi
# If we are using volumes for the benchmarking directories, we have to mount these files single-handedly.
# They will then also be available inside the volumes.
if [ "$VOLUME_TYPE" == "docker" ]; then
if [ "$NUM_MYSQLD_NODES" -gt 0 ]; then
add_file_to_template "$DBT2_CONF_SINGLE_FILEPATH" "$BENCH_DIR/dbt2_single/dbt2_run_1.conf"
add_file_to_template "$AUTOBENCH_SYS_SINGLE_FILEPATH" "$BENCH_DIR/sysbench_single/autobench.conf"
add_file_to_template "$AUTOBENCH_DBT2_SINGLE_FILEPATH" "$BENCH_DIR/dbt2_single/autobench.conf"
fi
if [ "$NUM_MYSQLD_NODES" -gt 1 ]; then
add_file_to_template "$DBT2_CONF_MULTI_FILEPATH" "$BENCH_DIR/dbt2_multi/dbt2_run_1.conf"
add_file_to_template "$AUTOBENCH_SYS_MULTI_FILEPATH" "$BENCH_DIR/sysbench_multi/autobench.conf"
add_file_to_template "$AUTOBENCH_DBT2_MULTI_FILEPATH" "$BENCH_DIR/dbt2_multi/autobench.conf"
fi
fi
template+="$ENV_FIELD"
template+="$(printf "$ENV_VAR_TEMPLATE" "MYSQL_BENCH_PASSWORD" "$MYSQL_BENCH_PASSWORD")"
# There are cases where the MySQLd is up, but the cluster is not.
# Also, we may not have MySQLds configured at all.
template+="$DEPENDS_ON_FIELD"
for NDBD_IP in "${NDBD_IPS[@]}"; do
depends_on=$(printf "$DEPENDS_ON_TEMPLATE" "$NDBD_IP")
template+="$depends_on"
done
if [ "$NUM_MYSQLD_NODES" -gt 0 ]; then
depends_on=$(printf "$DEPENDS_ON_TEMPLATE" "mysqld_1${USER_SUFFIX}")
template+="$depends_on"
fi
BASE_DOCKER_COMPOSE_FILE+="$template"
NODE_ID_OFFSET=$(($((CONTAINER_NUM - 1)) * API_SLOTS_PER_CONTAINER))
for SLOT_NUM in $(seq "$API_SLOTS_PER_CONTAINER"); do
API_NODE_ID=$((FIRST_USEABLE_API_NODE_ID + NODE_ID_OFFSET + $((SLOT_NUM - 1))))
# NodeId, NodeActive, ArbitrationRank, HostName
SLOT=$(printf "$CONFIG_INI_API_TEMPLATE" "$API_NODE_ID" "1" "1" "$SERVICE_NAME")
CONFIG_INI=$(printf "%s\n\n%s" "$CONFIG_INI" "$SLOT")
done
MULTI_API_IPS+="$SERVICE_NAME;"
if [ "$CONTAINER_NUM" -eq 1 ]; then
SINGLE_API_IP+="$SERVICE_NAME"
fi
done
fi
# Remove last semi-colon from MULTI_API_IPS
MULTI_API_IPS=${MULTI_API_IPS%?}
# Create empty API slots without HostNames to connect other
# services in outside containers; e.g. apps using ClusterJ or
# the NDB API. Important: these containers need to first be
# connected to the cluster's Docker Compose network.
for EMPTY_API_SLOT in $(seq "$EMPTY_API_SLOTS"); do
if [ ! -n "$API_NODE_ID" ]; then
API_NODE_ID=$FIRST_USEABLE_API_NODE_ID
else
API_NODE_ID=$((API_NODE_ID + 1))
fi
# NodeId, NodeActive, ArbitrationRank, HostName
SLOT=$(printf "$CONFIG_INI_API_TEMPLATE" "$API_NODE_ID" "1" "0" "") # Empty HostName
CONFIG_INI=$(printf "%s\n\n%s" "$CONFIG_INI" "$SLOT")
done
# Append volumes to end of file if docker volumes are used
if [ "$VOLUME_TYPE" == docker ]; then
BASE_DOCKER_COMPOSE_FILE+="
volumes:"
for VOLUME in "${VOLUMES[@]}"; do
BASE_DOCKER_COMPOSE_FILE+="
$VOLUME:"
done
BASE_DOCKER_COMPOSE_FILE+="
"
fi
#######################
#######################
#######################
echo "Writing data to files"
if [ "$NUM_MYSQLD_NODES" -gt 0 ]; then
echo "Writing my.cnf"
MY_CNF=$(printf "$MY_CNF_TEMPLATE" "$MYSQLD_SLOTS_PER_CONTAINER" "$MGM_CONNECTION_STRING")
echo "$MY_CNF" > "$MY_CNF_FILEPATH"
if [ "$NUM_BENCH_NODES" -gt 0 ]; then
echo "Writing benchmarking files for single MySQLds"
# This will always have 1 benchmarking and 1 MySQLd container, and 1 Sysbench instance
AUTOBENCH_SYSBENCH_SINGLE=$(printf "$AUTOBENCH_SYSBENCH_TEMPLATE" \
"$SINGLE_MYSQLD_IP" "$MYSQL_BENCH_USER" "$MYSQL_BENCH_PASSWORD" \
"$MYSQLD_SLOTS_PER_CONTAINER" "$MGMD_IPS" \
"$AUTO_SYS_THREAD_COUNTS_TO_RUN" "$AUTO_SYS_SYSBENCH_ROWS" \
"1")
echo "$AUTOBENCH_SYSBENCH_SINGLE" > "$AUTOBENCH_SYS_SINGLE_FILEPATH"
AUTOBENCH_DBT2_SINGLE=$(printf "$AUTOBENCH_DBT2_TEMPLATE" \
"$SINGLE_MYSQLD_IP" "$MYSQL_BENCH_USER" "$MYSQL_BENCH_PASSWORD" \
"$MYSQLD_SLOTS_PER_CONTAINER" "$MGMD_IPS" \
"$AUTO_DBT2_DBT2_WAREHOUSES")
echo "$AUTOBENCH_DBT2_SINGLE" > "$AUTOBENCH_DBT2_SINGLE_FILEPATH"
if [ "$NUM_MYSQLD_NODES" -gt 1 ]; then
echo "Writing benchmarking files for multiple MySQLds"
AUTOBENCH_SYSBENCH_MULTI=$(printf "$AUTOBENCH_SYSBENCH_TEMPLATE" \
"$MULTI_MYSQLD_IPS" "$MYSQL_BENCH_USER" "$MYSQL_BENCH_PASSWORD" \
"$MYSQLD_SLOTS_PER_CONTAINER" "$MGMD_IPS" \
"$AUTO_SYS_THREAD_COUNTS_TO_RUN" "$AUTO_SYS_SYSBENCH_ROWS" \
"$NUM_MYSQLD_NODES")
echo "$AUTOBENCH_SYSBENCH_MULTI" > "$AUTOBENCH_SYS_MULTI_FILEPATH"
AUTOBENCH_DBT2_MULTI=$(printf "$AUTOBENCH_DBT2_TEMPLATE" \
"$MULTI_MYSQLD_IPS" "$MYSQL_BENCH_USER" "$MYSQL_BENCH_PASSWORD" \
"$MYSQLD_SLOTS_PER_CONTAINER" "$MGMD_IPS" \
"$AUTO_DBT2_DBT2_WAREHOUSES")
echo "$AUTOBENCH_DBT2_MULTI" > "$AUTOBENCH_DBT2_MULTI_FILEPATH"
fi
fi
fi
if [ "$NUM_REST_API_NODES" -gt 0 ]; then
echo "Writing configuration file for the REST API server"
# Could also add more mgmds here
REST_API_CONFIG=$(printf "$REST_API_CONFIG_TEMPLATE" "$SINGLE_MGMD_IP")
echo "$REST_API_CONFIG" >$REST_API_JSON_FILEPATH
fi
echo "$BASE_DOCKER_COMPOSE_FILE" > "$DOCKER_COMPOSE_FILEPATH"
echo "$CONFIG_INI" > "$CONFIG_INI_FILEPATH"
# Save files for documentation
if [ -n "$SAVE_SAMPLE_FILES" ]; then
cp "$PARSED_ARGUMENTS_FILEPATH" "$SCRIPT_DIR/sample_files/parsed_arguments.txt"
cp "$CONFIG_INI_FILEPATH" "$SCRIPT_DIR/sample_files/config.ini"
cp "$DOCKER_COMPOSE_FILEPATH" "$SCRIPT_DIR/sample_files/docker_compose.yml"
if [ "$NUM_MYSQLD_NODES" -gt 0 ]; then
echo "$MY_CNF" > "$MY_CNF_FILEPATH"
cp "$MY_CNF_FILEPATH" "$SCRIPT_DIR/sample_files/my.cnf"
fi
fi
if which docker-compose >/dev/null 2>&1; then
DOCKER_COMPOSE=docker-compose
elif docker compose >/dev/null 2>&1; then
DOCKER_COMPOSE="docker compose"
else
echo "docker compose not installed."
exit 1
fi
# Make directories accessible by group members. This is used as a workaround
# since the local (host) user likely has a different UID from the user in the
# docker container.
chmod -R g=u "$AUTOGENERATED_FILES_DIR"
# Remove previous volumes
$DOCKER_COMPOSE -f "$DOCKER_COMPOSE_FILEPATH" -p "rondb_$FILE_SUFFIX" down -v
# Run fresh setup
$DOCKER_COMPOSE -f "$DOCKER_COMPOSE_FILEPATH" -p "rondb_$FILE_SUFFIX" up $DETACHED