Created
November 24, 2017 15:35
-
-
Save kszarek/ea554395ff0afabb303f82086cccd81c to your computer and use it in GitHub Desktop.
ElasticSeach: force to allocate a primary shard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# script will force to allocate unassigned shards in the cluster | |
# based on: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/cluster-reroute.html | |
import urllib.request | |
import json | |
host = 'http://es-main-master2-gew2-staging' | |
destination_host = 'es-main-data2-gew2-staging' | |
shards_uri = ':9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason&format=json' | |
reroute_uri = ':9200/_cluster/reroute' | |
with urllib.request.urlopen(host + shards_uri) as url: | |
data = json.loads(url.read().decode()) | |
for shard in data: | |
if (shard['state'] != 'UNASSIGNED' and shard['unassigned.reason'] != 'CLUSTER_RECOVERED') or shard['prirep'] == 'r': | |
continue | |
print("Shard:", shard) | |
data = { | |
"commands": [ | |
{ | |
"allocate_stale_primary": { | |
"index": shard['index'], | |
"shard": int(shard['shard']), | |
"node": destination_host, | |
"accept_data_loss": True | |
} | |
} | |
] | |
} | |
print("Request:", data) | |
req = urllib.request.Request(host + reroute_uri) | |
req.add_header('Content-Type', 'application/json') | |
encoded_data = json.dumps(data).encode('utf8') | |
response = urllib.request.urlopen(req, encoded_data) | |
print("Response:", response.code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment