Created
October 10, 2020 19:15
-
-
Save xcsrz/cfcd9c977ab9d0e4a2e645591c237009 to your computer and use it in GitHub Desktop.
This python script is used to increase the number of replicas in an ES cluster and force those new replicas to be allocated to a new host to enable a filesystem backup of the ES data directory.
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
from requests import get, post, put | |
from json import dumps, loads | |
from sys import argv | |
from math import ceil | |
commands = [] | |
def addShard(idx, shard): | |
commands.append({ | |
"allocate" : { | |
"index" : idx, | |
"shard" : int(shard), | |
"allow_primary" : False, | |
"node" : backup_node | |
} | |
}) | |
state = loads(get('http://localhost:9200/_cluster/state?filter_metadata=true').content) | |
data_nodes = [] | |
for node in state['nodes']: | |
if "data" in state['nodes'][node]['attributes'] and state['nodes'][node]['attributes']['data'] == "false": | |
continue | |
data_nodes.append({ 'node': node, 'address': state['nodes'][node]['transport_address']}) | |
replica_count = int(ceil(len(data_nodes)/2.0)) | |
if len(argv) != 2: | |
print "You must specify which node is the backup node or the 'reset' command. Available nodes right now are:" | |
for node in data_nodes: | |
print "\t- %s (%s)" % (node['node'], node['address']) | |
quit() | |
if argv[1] == "reset": | |
put('http://localhost:9200/_settings', data='{ "index.routing.allocation.disable_allocation": false }') | |
put('http://localhost:9200/_settings', data='{ "index" : { "number_of_replicas" : %d } }' % replica_count) | |
quit() | |
backup_node = argv[1] | |
if backup_node not in state['nodes']: | |
print "Invalid data node" | |
quit() | |
put('http://localhost:9200/_settings', data='{ "index.routing.allocation.disable_allocation": true }') | |
put('http://localhost:9200/_settings', data='{ "index" : { "number_of_replicas" : %d } }' % (replica_count+1)) | |
for idx in state['routing_table']['indices']: | |
for shard in state['routing_table']['indices'][idx]['shards']: | |
addShard(idx, shard) | |
# this is a lot of output, TODO: consolidate this into just what's useful | |
print dumps(loads(post('http://localhost:9200/_cluster/reroute?pretty', data=dumps({ "commands": commands })).content), indent=4) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment