Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bug that reconnectsparkmagic cause 'Error Starting Kernel' in jupyterlab #690

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions sparkmagic/sparkmagic/serverextension/handlers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
from notebook.utils import url_path_join
from notebook.base.handlers import IPythonHandler
from tornado import web
Expand Down Expand Up @@ -99,20 +100,25 @@ def _get_kernel_manager(self, path, kernel_name):
sessions = self.session_manager.list_sessions()

kernel_id = None
name = None
for session in sessions:
if session['notebook']['path'] == path:
session_id = session['id']
kernel_id = session['kernel']['id']
existing_kernel_name = session['kernel']['name']
if 'name' in session:
name = session['name']

break

if kernel_id is None:
self.logger.debug(u"Kernel not found. Starting a new kernel.")
k_m = yield self._get_kernel_manager_new_session(path, kernel_name)
name = os.path.basename(path)
k_m = yield self._get_kernel_manager_new_session(path, kernel_name, name)
elif existing_kernel_name != kernel_name:
self.logger.debug(u"Existing kernel name '{}' does not match requested '{}'. Starting a new kernel.".format(existing_kernel_name, kernel_name))
self._delete_session(session_id)
k_m = yield self._get_kernel_manager_new_session(path, kernel_name)
k_m = yield self._get_kernel_manager_new_session(path, kernel_name, name)
else:
self.logger.debug(u"Kernel found. Restarting kernel.")
k_m = self.kernel_manager.get_kernel(kernel_id)
Expand All @@ -121,8 +127,11 @@ def _get_kernel_manager(self, path, kernel_name):
raise gen.Return(k_m)

@gen.coroutine
def _get_kernel_manager_new_session(self, path, kernel_name):
model_future = self.session_manager.create_session(kernel_name=kernel_name, path=path, type="notebook")
def _get_kernel_manager_new_session(self, path, kernel_name, name=None):
if name is None:
name = os.path.basename(path)

model_future = self.session_manager.create_session(kernel_name=kernel_name, path=path, name=name, type="notebook")
model = yield model_future
kernel_id = model["kernel"]["id"]
self.logger.debug("Kernel created with id {}".format(str(kernel_id)))
Expand Down
13 changes: 7 additions & 6 deletions sparkmagic/sparkmagic/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TestSparkMagicHandler(AsyncTestCase):
kernel_id = '1'
kernel_name = 'pysparkkernel'
session_id = '1'
session_name = 'some_path.ipynb'
username = 'username'
password = 'password'
endpoint = 'http://endpoint.com'
Expand All @@ -36,8 +37,8 @@ class TestSparkMagicHandler(AsyncTestCase):
bad_msg = dict(content=dict(status='error', ename='SyntaxError', evalue='oh no!'))
request = None

def create_session_dict(self, path, kernel_id):
return dict(notebook=dict(path=path), kernel=dict(id=kernel_id, name=self.kernel_name), id=self.session_id)
def create_session_dict(self, path, kernel_id, name):
return dict(notebook=dict(path=path), kernel=dict(id=kernel_id, name=self.kernel_name), id=self.session_id, name=name)

def get_argument(self, key):
return dict(username=self.username, password=self.password, endpoint=self.endpoint, path=self.path)[key]
Expand All @@ -53,10 +54,10 @@ def setUp(self):
self.kernel_manager.get_kernel = MagicMock(return_value=self.individual_kernel_manager)

# Mock session manager
self.session_list = [self.create_session_dict(self.path, self.kernel_id)]
self.session_list = [self.create_session_dict(self.path, self.kernel_id, self.session_name)]
self.session_manager = MagicMock()
self.session_manager.list_sessions = MagicMock(return_value=self.session_list)
self.session_manager.create_session = MagicMock(return_value=self.create_session_dict(self.path, self.kernel_id))
self.session_manager.create_session = MagicMock(return_value=self.create_session_dict(self.path, self.kernel_id, self.session_name))

# Mock spark events
self.spark_events = MagicMock()
Expand Down Expand Up @@ -195,7 +196,7 @@ def test_get_kernel_manager_no_existing_kernel(self, _get_kernel_manager_new_ses
assert_equals(self.individual_kernel_manager, km)
self.individual_kernel_manager.restart_kernel.assert_not_called()
self.kernel_manager.get_kernel.assert_not_called()
_get_kernel_manager_new_session.assert_called_once_with(different_path, self.kernel_name)
_get_kernel_manager_new_session.assert_called_once_with(different_path, self.kernel_name, different_path)

@patch('sparkmagic.serverextension.handlers.ReconnectHandler._get_kernel_manager_new_session')
@gen_test
Expand All @@ -219,5 +220,5 @@ def test_get_kernel_manager_different_kernel_type(self, _get_kernel_manager_new_
assert_equals(self.individual_kernel_manager, km)
self.individual_kernel_manager.restart_kernel.assert_not_called()
self.kernel_manager.get_kernel.assert_not_called()
_get_kernel_manager_new_session.assert_called_once_with(self.path, different_kernel)
_get_kernel_manager_new_session.assert_called_once_with(self.path, different_kernel, self.session_name)
self.session_manager.delete_session.assert_called_once_with(self.session_id)