Created on 01-26-2017 10:39 AM - edited 09-16-2022 03:58 AM
I am wondering if there is a way to restart Solr from the command line? I would like to script a restart of solr every night so that I dont have to do it manually.
Thank You
Jamie
Created 01-26-2017 02:05 PM
If you are using Cloudera Manager, you can use the API present to do that.
The end point you will like to use will be : /cm/service/roleCommands/restart
I would not recommend to do restart outside of the CM cause the management will be difficult.
Created 01-26-2017 02:05 PM
If you are using Cloudera Manager, you can use the API present to do that.
The end point you will like to use will be : /cm/service/roleCommands/restart
I would not recommend to do restart outside of the CM cause the management will be difficult.
Created 01-26-2017 02:11 PM
Thank you that gives me exactly what I needed. Why would you say that management would be difficult?
Jamie
Created 01-26-2017 03:35 PM
Created 03-20-2018 02:45 PM
#!/usr/bin/env python
import ssl,sys,time
from cm_api.api_client import ApiResource
from cm_api.endpoints.types import ApiClusterTemplate
from cm_api.endpoints.cms import ClouderaManager
from cm_api.endpoints import clusters, events, hosts, external_accounts, tools
from cm_api.endpoints import types, users, timeseries, roles, services
ssl._create_default_https_context = ssl._create_unverified_context
try:
  cm = ApiResource("CM_SERVER","7183","admin","CM_PASS","true","15")
  cluster = cm.get_cluster("CLUSTER_NAME")
except:
  print "Failed log into cluster %s" % ("CLUSTER_NAME")
  sys.exit(0)
servers = [
"server1.company.com",
"server2.company.com",
"server3.company.com"]
s = cluster.get_service("solr")
ra = []
for r in s.get_roles_by_type("SOLR_SERVER"):
  hostname = cm.get_host(r.hostRef.hostId).hostname
  if hostname in servers:
    ra.append([hostname,r])
ra.sort()
print "\nWill restart %s SOLR instances" % len(ra)
for hostname,r in ra:
  print "\nRestarting SOLR on %s" % (hostname)
  s.restart_roles(r.name)
  r = s.get_role(r.name)
  wait = time.time() + 180 # three minutes
  while r.roleState != "STARTED":
    print "Role State = %s" % (r.roleState)
    print "Waiting for role state to be STARTED"
    print time.strftime("%H:%M:%S")
    if time.time() > wait:
      print "SOLR failed to restart on %s" % (hostname)
      sys.exit(1)
    time.sleep(10)
    r = s.get_role(r.name)
  print "SOLR restarted on %s" % (hostname)
print "\nAll SOLR roles restarted"
sys.exit(0)