github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/deploy/qdrant/scripts/qdrant-pre-stop.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -x
     4  set -o errexit
     5  set -o errtrace
     6  set -o nounset
     7  set -o pipefail
     8  
     9  curl=/qdrant/tools/curl
    10  jq=/qdrant/tools/jq
    11  
    12  idx=${KB_POD_NAME##*-}
    13  current_component_replicas=$(cat /etc/annotations/component-replicas)
    14  local_uri=http://localhost:6333
    15  
    16  cluster_info=`$curl -s ${local_uri}/cluster`
    17  local_peer_id=`echo "${cluster_info}"| $jq -r .result.peer_id`
    18  leader_peer_id=`echo "${cluster_info}" | $jq -r .result.raft_info.leader`
    19  
    20  move_shards() {
    21      cols=`$curl -s ${local_uri}/collections`
    22      col_count=`echo ${cols} | $jq -r '.result.collections | length'`
    23      if [[ ${col_count} -eq 0 ]]; then
    24          echo "no collections found in the cluster"
    25          return
    26      fi
    27      col_names=`echo ${cols} | $jq -r '.result.collections[].name'`
    28      for col_name in ${col_names}; do
    29          col_cluster_info=`$curl -s ${local_uri}/collections/${col_name}/cluster`
    30          col_shard_count=`echo ${col_cluster_info} | $jq -r '.result.local_shards[] | length'`
    31          if [[ ${col_shard_count} -eq 0 ]]; then
    32              echo "no shards found in collection ${col_name}"
    33              continue
    34          fi
    35  
    36          local_shard_ids=`echo ${col_cluster_info} | $jq -r '.result.local_shards[].shard_id'`
    37          for shard_id in ${local_shard_ids}; do
    38              echo "move shard ${shard_id} in col_name ${col_name} from ${local_peer_id} to ${leader_peer_id}"
    39              $curl -s -X POST -H "Content-Type: application/json" \
    40                  -d '{"move_shard":{"shard_id": '${shard_id}',"to_peer_id": '${leader_peer_id}',"from_peer_id": '${local_peer_id}}}'' \
    41                  ${local_uri}/collections/${col_name}/cluster
    42          done
    43  
    44          while true; do
    45              col_cluster_info=`$curl -s ${local_uri}/collections/${col_name}/cluster`
    46              local_shard_ids=`echo ${col_cluster_info} | $jq -r '.result.local_shards[].shard_id'`
    47              if [ -z "${local_shard_ids}" ]; then
    48                  echo "all shards in collection ${col_name} has been moved"
    49                  break
    50              fi
    51              sleep 1
    52          done
    53      done
    54  }
    55  
    56  remove_peer() {
    57  #    declare -A peer_to_uri=()
    58  #    peer_ids="`echo ${cluster_info} | jq -r '.result.peers | keys'`"
    59  #    for peer_id in "${peer_ids[@]}"; do
    60  #        peer_uri=`echo ${cluster_info} | jq -r ".result.peers.${peer_id}.uri"`
    61  #        peer_to_uri[peer_id]=peer_uri
    62  #    done
    63  
    64      echo "remove local peer ${local_peer_id} from cluster"
    65      $curl -v -XDELETE ${local_uri}/cluster/peer/${local_peer_id}
    66  }
    67  
    68  if [ ! "$idx" -lt "$current_component_replicas" ] && [ "$current_component_replicas" -ne 0 ]; then
    69      echo "scaling in, we need to move local shards to other peers and remove local peer from the cluster"
    70  
    71      echo "cluster info: ${cluster_info}"
    72  
    73      move_shards
    74  
    75      remove_peer
    76  else
    77      # stop, do nothing.
    78      echo "stop, do nothing"
    79  fi