Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mongodb pod terminating takes too long time #3971

Merged
merged 4 commits into from
Jun 27, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions deploy/mongodb/scripts/replicaset-post-start.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ PORT={{ $mongodb_port }}
MONGODB_ROOT={{ $mongodb_root }}
INDEX=$(echo $KB_POD_NAME | grep -o "\-[0-9]\+\$");
INDEX=${INDEX#-};
if [ $INDEX -ne 0 ]; then exit 0; fi
if [ $INDEX -ne 0 ]; then
exit 0
fi

until mongosh --quiet --port $PORT --eval "print('ready')"; do sleep 1; done
until mongosh --quiet --port $PORT --eval "print('I am ready')"; do sleep 1; done

until is_inited=$(mongosh --quiet --port $PORT --eval "rs.status().set" -u root --password $MONGODB_ROOT_PASSWORD || mongosh --quiet --port $PORT --eval "try { rs.status().set } catch (e) { '' }") ; do
sleep 1;
done

echo is_inited: $is_inited

if [ $is_inited ]; then
exit 0
fi

RPL_SET_NAME=$(echo $KB_POD_NAME | grep -o ".*-");
RPL_SET_NAME=${RPL_SET_NAME%-};
Expand All @@ -40,13 +52,11 @@ done
CONFIGSVR=""
if [ ""$IS_CONFIGSVR = "true" ]; then CONFIGSVR="configsvr: true,"; fi

until is_inited=$(mongosh --quiet --port $PORT --eval "rs.status().ok" -u root --password $MONGODB_ROOT_PASSWORD || mongosh --quiet --port $PORT --eval "try { rs.status().ok } catch (e) { 0 }") ; do sleep 1; done
if [ $is_inited -ne 1 ]; then
sleep 10
set -e
mongosh --quiet --port $PORT --eval "rs.initiate({_id: \"$RPL_SET_NAME\", $CONFIGSVR members: [$MEMBERS]})";
set +e
fi;
echo "initiate replset"
sleep 10
set -e
mongosh --quiet --port $PORT --eval "rs.initiate({_id: \"$RPL_SET_NAME\", $CONFIGSVR members: [$MEMBERS]})";
set +e

(until mongosh --quiet --port $PORT --eval "rs.isMaster().isWritablePrimary"|grep true; do sleep 1; done;
mongosh --quiet --port $PORT admin --eval "db.createUser({ user: '$MONGODB_ROOT_USER', pwd: '$MONGODB_ROOT_PASSWORD', roles: [{role: 'root', db: 'admin'}] })") </dev/null >/dev/null 2>&1 &