@@ -31,7 +31,12 @@ wait_for_mongos() {
3131check_shard_exists () {
3232 # check if the shard exists in the config database
3333 local shard_exists
34- shard_exists=$( $CLUSTER_MONGO " db.getSiblingDB(\" config\" ).shards.find({ _id: \" $MONGODB_REPLICA_SET_NAME \" })" )
34+ shard_exists=$( $CLUSTER_MONGO " db.getSiblingDB(\" config\" ).shards.find({ _id: \" $MONGODB_REPLICA_SET_NAME \" })" 2> /dev/null)
35+ if [ $? -ne 0 ]; then
36+ echo " ERROR: Failed to check if shard $MONGODB_REPLICA_SET_NAME exists." >&2
37+ exit 1
38+ fi
39+ echo " INFO: Check if shard $MONGODB_REPLICA_SET_NAME exists: $shard_exists "
3540 if [ -n " $shard_exists " ]; then
3641 return 0 # true
3742 else
@@ -63,7 +68,11 @@ initialize_or_scale_out_mongodb_shard() {
6368get_remove_shard_status () {
6469 # Execute the removeShard command and capture its JSON output
6570 local result
66- result=$( $CLUSTER_MONGO " EJSON.stringify(db.adminCommand( { removeShard: \" $MONGODB_REPLICA_SET_NAME \" } ))" )
71+ if [ " $CLIENT " = " mongosh" ]; then
72+ result=$( $CLUSTER_MONGO " EJSON.stringify(db.adminCommand( { removeShard: \" $MONGODB_REPLICA_SET_NAME \" } ))" )
73+ else
74+ result=$( $CLUSTER_MONGO " JSON.stringify(db.adminCommand( { removeShard: \" $MONGODB_REPLICA_SET_NAME \" } ))" )
75+ fi
6776 echo " $result "
6877}
6978
@@ -76,6 +85,32 @@ get_remove_shard_state() {
7685 echo " $state "
7786}
7887
88+ get_remaining_jumbo_chunks () {
89+ local result=$1
90+ # Parse and log the jumboChunks count using jq
91+ local jumbo_chunks
92+ if [ " $CLIENT " = " mongosh" ]; then
93+ jumbo_chunks=$( echo " $result " | jq -r ' .remaining.jumboChunks // 0' )
94+ else
95+ jumbo_chunks=$( echo " $result " | jq -r ' .remaining.jumboChunks.numberLong // 0' )
96+ fi
97+ # Return the jumboChunks count as the function output
98+ echo " $jumbo_chunks "
99+ }
100+
101+ get_remaining_chunks () {
102+ local result=$1
103+ # Parse and log the chunks count using jq
104+ local chunks
105+ if [ " $CLIENT " = " mongosh" ]; then
106+ chunks=$( echo " $result " | jq -r ' .remaining.chunks // 0' )
107+ else
108+ chunks=$( echo " $result " | jq -r ' .remaining.chunks.numberLong // 0' )
109+ fi
110+ # Return the chunks count as the function output
111+ echo " $chunks "
112+ }
113+
79114delete_or_scale_in_mongodb_shard () {
80115 # Check if the shard is scaling in
81116 if [[ $KB_CLUSTER_COMPONENT_IS_SCALING_IN != " true" ]]; then
@@ -111,13 +146,13 @@ delete_or_scale_in_mongodb_shard() {
111146 if [ " $state " = " completed" ]; then
112147 break
113148 elif [ " $state " = " ongoing" ]; then
114- remaining_jumboChunks=$( echo " $status_json " | jq -r ' .remaining.jumboChunks ' )
149+ remaining_jumboChunks=$( get_remaining_jumbo_chunks " $status_json " )
115150 if [ " $remaining_jumboChunks " -gt 0 ]; then
116151 echo " INFO: $remaining_jumboChunks jumbo chunks remaining, please clear jumbo chunks before removing the shard."
117152 exit 1
118153 fi
119154
120- remaining_chunks=$( echo " $status_json " | jq -r ' .remaining.chunks ' )
155+ remaining_chunks=$( get_remaining_chunks " $status_json " )
121156 echo " INFO: $remaining_chunks chunks remaining."
122157 if [ " $remaining_chunks " -eq 0 ]; then
123158 dbs_to_move=$( echo " $status_json " | jq -r ' .dbsToMove[]' )
0 commit comments