Skip to content

Commit

Permalink
fix: mongodb pod terminating takes too long time (#3971)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuriwuyun authored Jun 27, 2023
1 parent de9b295 commit 2f78161
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions deploy/mongodb/scripts/replicaset-post-start.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ 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('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,16 +52,11 @@ done
CONFIGSVR=""
if [ ""$IS_CONFIGSVR = "true" ]; then CONFIGSVR="configsvr: true,"; fi

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 [ -z $is_inited ]; then
echo "initiate replset"
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 &

0 comments on commit 2f78161

Please sign in to comment.