Skip to content

Instantly share code, notes, and snippets.

@akatrevorjay
Last active December 6, 2017 05:12
Show Gist options
  • Save akatrevorjay/ad3ef876a5309446cd0b9fd2822c1533 to your computer and use it in GitHub Desktop.
Save akatrevorjay/ad3ef876a5309446cd0b9fd2822c1533 to your computer and use it in GitHub Desktop.
Kick elasticsearch until it finds your unassigned shards
#!/usr/bin/env zsh
##
## "You know, for sanity."
##
## ES clusters can determine that your shards are permanently missing at times, marking them forever unassigned.
## You go and look, but the shards are right there. What?
## What this script does is kick ES to make it actually take a look.
## For some reason telling ES to move the shard makes it realize it does in fact exist. Don't ask me, man.
##
set -eo pipefail
setopt nullglob
# set -xv
es_master=${1:?}
target_node=${2:?}
[[ $es_master = *://* ]] || es_master="http://$es_master"
[[ $es_master = *://*:* ]] || es_master="$es_master:9200"
printf '-- Fetching unassigned shards from es_master=%s\n' $es_master
raw_shards=$(curl -sSLf "$es_master/_cat/shards" | grep 'UNASSIGNED')
shards=(${(f)raw_shards})
printf '-- Got %d unassigned shards\n' $#shards
for line in $shards; do
[[ -n $line ]] || continue
set -- $=line
idx=$1
shard=$2
status=$3
printf '-- idx=%s shard=%s status=%s -> %s\n' $idx $shard $status $target_node
data=$(printf \
'{"commands": [{"allocate": {"index": "%s", "shard": "%s", "node": "%s", "allow_primary": "%s"}}]}' \
$idx $shard $target_node "true" \
)
# perform sacrifice
curl -sSLf -XPOST "$es_master/_cluster/reroute" -d "$data"
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment