IllegalStateException: Session is in state starting'

1.3k views Asked by At

I installed apache spark and apache Livy in my system. When I am running a python code, it's giving error

'u'java.lang.IllegalStateException: Session is in state starting''

By default Apchy Livy run on port number 8998. My python code is

import json, pprint, requests, textwrap

host = 'http://localhost:8998'
data = {'kind': 'pyspark'}
headers = {'Content-Type': 'application/json'}
r = requests.post(host + '/sessions', data=json.dumps(data),   
headers=headers)

session_url = host + r.headers['location']
statements_url = session_url + '/statements'


data = {
  'code': textwrap.dedent("""
          import random
          NUM_SAMPLES = 100000
          def sample(p):
          x, y = random.random(), random.random()
          return 1 if x*x + y*y < 1 else 0

          count = sc.parallelize(xrange(0, 
                    NUM_SAMPLES)).map(sample).reduce(lambda a, b: a +b)
          print "Pi is roughly %f" % (4.0 * count / NUM_SAMPLES)
          """)
      }

r = requests.post(statements_url, data=json.dumps(data),   
    headers=headers)
pprint.pprint(r.json())

{u'id': 12,
 u'output': {u'data': {u'text/plain': u'Pi is roughly 3.136000'},
            u'execution_count': 12,
            u'status': u'ok'},
 u'state': u'running'}

How I can resolve this error?

2

There are 2 answers

0
Mahnaz On

After creating a session, you need to check the returned state. submit statements, only after the state of your session turned into idle. The bellow code worked for me.

You can print the returned value to debug your setup. If the state continued to be starting or dead there is a problem in your setup. Maybe some permission issue or any other issue regarding initiating spark context by livy. Check that out in livy UI stderr.

    uri = "/sessions"
    data = {'kind': 'pyspark'
            }
    headers = {'Content-Type': 'application/json'}
    r = requests.post(url=self.host + uri, data=json.dumps(data),
                      headers=headers)

    response = r.json()
    sessionId = response['id']
    
    while response['state'] != "idle":
        r = requests.get(self.host + "/sessions/" + str(sessionId), headers=headers)
        response = r.json()
        # print(r.json())

    return response['id']

This return sessionId is now ready to be used for submitting statements. You can simply call POST /sessions/{sessionId}/statements to submit your statements.

0
Sachin Arora On

Whenever you create a new spark session in Livy it takes time for it to get into Idle state. As after creating the session you are directly posting up your code to that particular spark session which is still in starting state due to which exception in thrown.

Try doing something like this -:

import json, pprint, requests, textwrap
host = 'http://192.168.0.56:8998'
data = {'kind': 'spark'}
headers = {'Content-Type': 'application/json'}
r = requests.post(host + '/sessions', data=json.dumps(data), headers=headers)
print(headers['location'])

once you get the url location from headers follow this

session_url = "http://localhost/"+paste headers value
r = requests.get(session_url, headers=headers)
statements_url = session_url + '/statements'
data = {'code': '1 + 1'}
r = requests.post(statements_url, data=json.dumps(data), headers=headers)
print(r.json())
statement_url = session_url
r = requests.get(statement_url, headers=headers)
pprint.pprint(r.json())