Skip to content

Instantly share code, notes, and snippets.

@tekton
Created February 25, 2016 17:56
Show Gist options
  • Save tekton/3d8250cfcf4c8bc43e26 to your computer and use it in GitHub Desktop.
Save tekton/3d8250cfcf4c8bc43e26 to your computer and use it in GitHub Desktop.
Way to help elastichsearch assign primaries
import urllib2
import random
import json
import argparse
def get_primary(rotation):
return rotation[random.randint(0, len(rotation)-1)]
def send_data(index, shard, url, rotation):
data = {"commands": [{"allocate": {"index": index,
"shard": shard,
"node": get_primary(rotation),
"allow_primary": "true"}}]}
req = urllib2.Request("{}/{}".format(url, "_cluster/reroute"), json.dumps(data))
try:
response = urllib2.urlopen(req)
except urllib2.URLError as e:
if hasattr(e, "reason"):
# print "We failed to reach a server."
print "send_data :: Reason: {}".format(e.reason)
print(e)
if hasattr(e, "code"):
# print "The server couldn't fulfill the request."
print "send_data :: fulfill :: Error code: {}".format(e.code)
print("{}/{}".format(url, "_cluster/reroute"))
print(json.dumps(data))
except Exception as e:
print("send_data :: {}".format(e))
def get_unassigned(url, servers):
print(url, servers)
rotation = servers.split(",")
print(rotation)
req = urllib2.Request("{}/{}".format(url, "_cat/shards"))
try:
response = urllib2.urlopen(req)
except urllib2.URLError as e:
if hasattr(e, "reason"):
# print "We failed to reach a server."
print "get_unassigned :: reach :: Reason: {}".format(e.reason)
elif hasattr(e, "code"):
# print "The server couldn't fulfill the request."
print "get_unassigned :: fulfill :: Error code: {}".format(e.code)
except Exception as e:
print("get_unassigned :: {}".format(e))
shards = response.read().split("\n")
for line in shards:
x = line.split()
if len(x) >= 3:
if x[3] == "UNASSIGNED":
send_data(x[0], x[1], url, rotation)
print(x[0], x[1], x[3])
if __name__ == "__main__":
parser = argparse.ArgumentParser('lap', description='Logstash Assign Primary')
parser.add_argument('-u', '--url',
metavar='URL',
help='elasticsearch URL to connet to connect to ')
parser.add_argument('-s', '--servers',
metavar='SERVERS',
help='comma separated list of servers to assign shards to')
args = parser.parse_args()
print(args)
get_unassigned(args.url, args.servers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment