Skip to content

Commit 80d566e

Browse files
committed
Ensure replset member on port 27017 is primary
1 parent 96f382d commit 80d566e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

.mci.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,48 @@ functions:
191191
echo "{ \"releases\": { \"default\": \"$MONGODB_BINARIES\" }}" > $MONGO_ORCHESTRATION_HOME/orchestration.config
192192
./.evergreen/run-orchestration.sh
193193
194+
# Ensure server on port 27017 is the primary server.
195+
if [[ "$TOPOLOGY" == replica_set ]]; then
196+
# Replset members should be on the following ports.
197+
declare hosts="localhost:27017,localhost:27018,localhost:27019"
198+
199+
# Authentication may be required.
200+
declare creds
201+
if [[ -n "$AUTH" ]]; then
202+
creds="bob:pwd123@"
203+
else
204+
creds=""
205+
fi
206+
207+
declare uri
208+
printf -v uri "mongodb://%s%s" "$creds" "$hosts"
209+
210+
# Raise the priority of the member on port 27017. Others should have priority 1.
211+
declare script
212+
printf -v script "%s\n%s\n%s\n" \
213+
"let c = rs.conf()" \
214+
"c.members.find((m) => m.host.includes('27017')).priority = 10" \
215+
"rs.reconfig(c)"
216+
217+
mongosh --quiet "$uri" --eval "$script"
218+
219+
# Wait up to a minute for member on port 27017 to become primary.
220+
wait_for_primary() {
221+
for _ in $(seq 60); do
222+
if mongosh --quiet "$uri" --eval "quit(rs.hello().primary.includes('27017') ? 0 : 1)"; then
223+
return 0
224+
else
225+
sleep 1
226+
fi
227+
done
228+
echo "Could not set replset member on port 27017 as primary"
229+
return 1
230+
}
231+
echo "Waiting for replset member 27017 to become primary..."
232+
wait_for_primary
233+
echo "Waiting for replset member 27017 to become primary... done."
234+
fi
235+
194236
# Copy mongocryptd up so other functions can find it later, since we can't share PATHs
195237
if [ -f $MONGODB_BINARIES/mongocryptd ]; then
196238
cp $MONGODB_BINARIES/mongocryptd ../mongocryptd

0 commit comments

Comments
 (0)