11#! /usr/bin/env bash
2+ # Start MongoDB replica sets for source and target
3+ #
4+ # Usage:
5+ # ./run.sh # Default: start both rs0 and rs1
6+ # RS_COUNT=1 ./run.sh # Start only rs0
7+ # RS_COUNT=2 ./run.sh # Start both rs0 and rs1
8+ #
9+ # Limits: Max 2 replica sets (rs0 and rs1)
10+ # Each replica set has 3 members (rs{N}0, rs{N}1, rs{N}2)
11+ # Ports: rs0 uses 30000-30002, rs1 uses 30100-30102
12+
13+ export COMPOSE_PROJECT_NAME=r1
214
315BASE=$( dirname " $( dirname " $0 " ) " )
16+ echo " BASE=$BASE "
417
518# shellcheck source=/dev/null
619source " $BASE /util"
@@ -9,16 +22,55 @@ RDIR="$BASE/rs"
922
1023export compose=" $RDIR /compose.yml"
1124
12- dcf up -d rs00 rs01 rs02 rs10 rs11 rs12
25+ # Port mappings for each replica set (must match compose.yml)
26+ # rs0: 30000, 30001, 30002
27+ # rs1: 30100, 30101, 30102
28+ RS_PORTS=(
29+ " 30000 30001 30002"
30+ " 30100 30101 30102"
31+ )
32+
33+ # Dynamically defined by the length of sets of ports
34+ MAX_RS_COUNT=${# RS_PORTS[@]}
35+
36+ # Configuration: Number of replica sets to start
37+ # Can be overridden with environment variable: RS_COUNT=1 ./run.sh
38+ RS_COUNT=" ${RS_COUNT:- $MAX_RS_COUNT } "
39+
40+ if [ " $RS_COUNT " -gt " $MAX_RS_COUNT " ]; then
41+ echo " ERROR: RS_COUNT=$RS_COUNT exceeds maximum $MAX_RS_COUNT "
42+ echo " Available init scripts: $RDIR /mongo/scripts/rs{0..$(( MAX_RS_COUNT- 1 )) }.js"
43+ exit 1
44+ fi
45+
46+ if [ " $RS_COUNT " -lt 1 ]; then
47+ echo " ERROR: RS_COUNT=$RS_COUNT must be at least 1"
48+ exit 1
49+ fi
50+
51+ echo " Starting $RS_COUNT replica set(s) (max: $MAX_RS_COUNT )"
52+
53+ # Build list of services to start and initialize each replica set
54+ SERVICES=" "
55+ for rs_idx in $( seq 0 $(( RS_COUNT - 1 )) ) ; do
56+ echo " Starting replica set rs${rs_idx} "
57+ SERVICES=" $SERVICES rs${rs_idx} 0 rs${rs_idx} 1 rs${rs_idx} 2"
58+ done
1359
14- mwait " rs00:30000"
15- mwait " rs01:30001"
16- mwait " rs02:30002"
60+ # Start the selected services
61+ dcf up -d $SERVICES
1762
18- rsinit " scripts/rs0" " rs00:30000"
63+ # Initialize each replica set
64+ for rs_idx in $( seq 0 $(( RS_COUNT - 1 )) ) ; do
65+ # Get ports for this replica set
66+ read -r -a PORTS <<< " ${RS_PORTS[$rs_idx]}"
1967
20- mwait " rs10:30100"
21- mwait " rs11:30101"
22- mwait " rs12:30102"
68+ # Wait for all members
69+ for member_idx in 0 1 2; do
70+ PORT=${PORTS[$member_idx]}
71+ mwait " rs${rs_idx}${member_idx} :${PORT} "
72+ done
2373
24- rsinit " scripts/rs1" " rs10:30100"
74+ # Initialize replica set
75+ rsinit " scripts/rs${rs_idx} " " rs${rs_idx} 0:${PORTS[0]} "
76+ done
0 commit comments