Skip to content

Instantly share code, notes, and snippets.

@brentley
Forked from kintarowins/rebalance.sh
Last active February 21, 2017 16:56
Show Gist options
  • Save brentley/6a875889a4f9409203bb3300e3bc7560 to your computer and use it in GitHub Desktop.
Save brentley/6a875889a4f9409203bb3300e3bc7560 to your computer and use it in GitHub Desktop.
Cassandra node rebalancing script
#!/bin/bash
#set -eu
#this looks up the nodes from the ring and rebalances them automatically
# We expect exactly 3 racks (currently)
ip_16="$(hostname -i |cut -f1,2 -d.)"
racks=($(nodetool ring |grep $ip_16 |awk '{print $2}' |sort -u))
rack_0=($(/usr/bin/nodetool ring |grep ${racks[0]} |awk '{print $1}' | sort -n))
rack_1=($(/usr/bin/nodetool ring |grep ${racks[1]} |awk '{print $1}' | sort -n))
rack_2=($(/usr/bin/nodetool ring |grep ${racks[2]} |awk '{print $1}' | sort -n))
hosts_per_az=${#rack_0[*]}
declare -a hosts
declare -a exitcode
declare -a nodetoolstatus
stripe=0
while [[ $stripe -lt $hosts_per_az ]]; do
hosts+=(${rack_0[$stripe]})
hosts+=(${rack_1[$stripe]})
hosts+=(${rack_2[$stripe]})
stripe=$((stripe+1))
done
RING_SIZE=$(echo "2^127" | bc)
HOST_NUM=${#hosts[*]}
echo we have ${#hosts[*]} hosts
INDEX=0
echo ${hosts[*]}
for host in ${hosts[*]}
do
token=$(echo "$INDEX*$RING_SIZE/$HOST_NUM+1" | bc)
if [[ $token -ne 0 ]]; then
curl -f http://$host:8080/Priam/REST/v1/cassadmin/move?token=$token
exitcode+=($?)
else
curl -f http://$host:8080/Priam/REST/v1/cassadmin/move?token=$token
exitcode+=($?)
fi
INDEX=$((INDEX+1))
done
echo exitcode=${exitcode[*]}
echo exitcount=${#exitcode[*]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment