Skip to content

Instantly share code, notes, and snippets.

@chengdol
Last active June 19, 2022 18:40
Show Gist options
  • Save chengdol/be9e90efa69fe075d1da6d5c90307178 to your computer and use it in GitHub Desktop.
Save chengdol/be9e90efa69fe075d1da6d5c90307178 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
# Display data stream based hot shard distribution.
# Run this script in any ES node.
#
# Author: chengdol
#################################################################
out_file=$(mktemp /tmp/statistics.XXXX)
trap "rm -f $out_file" EXIT
declare -A write_shard_2_node_map
node_list=$(curl -s "http://localhost:9200/_cat/nodes?h=ip")
for ip in ${node_list[@]}
do
write_shard_2_node_map[$ip]=0
done
echo "Total node number(includes master): ${#write_shard_2_node_map[@]}"
declare -A shard_2_node_map
# all data stream shard list
shard_list=$(curl -s "http://localhost:9200/_cat/shards/.ds-*?h=index,shard,prirep,state,node&format=json" | jq -r '.[] | [.index,.shard,.prirep,.node] | @csv' | sed 's/\"//g')
for item in ${shard_list[@]}
do
# must use string expansion for high efficiency
# cut is too slow
shard_2_node_map[${item%\,*}]=${item##*\,}
done
echo "Total data stream shard number: ${#shard_2_node_map[@]}"
# get write index of each ds
declare -A write_index_set
write_index_list=$(curl -s "http://localhost:9200/_data_stream/*?format=json" | jq -r '.data_streams[].indices[-1].index_name')
for write_index_name in ${write_index_list[@]}
do
write_index_set[$write_index_name]=0
done
echo "Total write index number(ds number): ${#write_index_set[@]}"
# aggregating write shard per node
for key in "${!shard_2_node_map[@]}"
do
extract_index_name=${key%%\,*}
if [ ! -z "${write_index_set[${extract_index_name}]}" ]; then
node_ip=${shard_2_node_map[$key]}
let write_shard_2_node_map[$node_ip]+=1
fi
done
# sort by number and display statistics
for key in "${!write_shard_2_node_map[@]}"
do
echo "${write_shard_2_node_map[$key]} $key" >> $out_file
done
echo; echo
echo "hot shard count: node ip"
cat $out_file | sort -k1 -n -r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment