Search in sources :

Example 11 with ManagedInterpreterGroup

use of org.apache.zeppelin.interpreter.ManagedInterpreterGroup in project zeppelin by apache.

the class ParagraphTest method returnUnchangedResultsWithDifferentUser.

@Ignore
public void returnUnchangedResultsWithDifferentUser() throws Throwable {
    Note mockNote = mock(Note.class);
    when(mockNote.getCredentials()).thenReturn(mock(Credentials.class));
    Paragraph spyParagraph = spy(new Paragraph("para_1", mockNote, null));
    Interpreter mockInterpreter = mock(Interpreter.class);
    spyParagraph.setInterpreter(mockInterpreter);
    doReturn(mockInterpreter).when(spyParagraph).getBindedInterpreter();
    ManagedInterpreterGroup mockInterpreterGroup = mock(ManagedInterpreterGroup.class);
    when(mockInterpreter.getInterpreterGroup()).thenReturn(mockInterpreterGroup);
    when(mockInterpreterGroup.getId()).thenReturn("mock_id_1");
    when(mockInterpreterGroup.getAngularObjectRegistry()).thenReturn(mock(AngularObjectRegistry.class));
    when(mockInterpreterGroup.getResourcePool()).thenReturn(mock(ResourcePool.class));
    List<InterpreterSetting> spyInterpreterSettingList = spy(new ArrayList<>());
    InterpreterSetting mockInterpreterSetting = mock(InterpreterSetting.class);
    when(mockInterpreterGroup.getInterpreterSetting()).thenReturn(mockInterpreterSetting);
    InterpreterOption mockInterpreterOption = mock(InterpreterOption.class);
    when(mockInterpreterSetting.getOption()).thenReturn(mockInterpreterOption);
    when(mockInterpreterOption.permissionIsSet()).thenReturn(false);
    when(mockInterpreterSetting.getStatus()).thenReturn(Status.READY);
    when(mockInterpreterSetting.getId()).thenReturn("mock_id_1");
    when(mockInterpreterSetting.getOrCreateInterpreterGroup(anyString(), anyString())).thenReturn(mockInterpreterGroup);
    when(mockInterpreterSetting.isUserAuthorized(any(List.class))).thenReturn(true);
    spyInterpreterSettingList.add(mockInterpreterSetting);
    when(mockNote.getId()).thenReturn("any_id");
    when(mockInterpreter.getFormType()).thenReturn(FormType.NONE);
    ParagraphJobListener mockJobListener = mock(ParagraphJobListener.class);
    doReturn(mockJobListener).when(spyParagraph).getListener();
    InterpreterResult mockInterpreterResult = mock(InterpreterResult.class);
    when(mockInterpreter.interpret(anyString(), Mockito.<InterpreterContext>any())).thenReturn(mockInterpreterResult);
    when(mockInterpreterResult.code()).thenReturn(Code.SUCCESS);
    // Actual test
    List<InterpreterResultMessage> result1 = new ArrayList<>();
    result1.add(new InterpreterResultMessage(Type.TEXT, "result1"));
    when(mockInterpreterResult.message()).thenReturn(result1);
    AuthenticationInfo user1 = new AuthenticationInfo("user1");
    spyParagraph.setAuthenticationInfo(user1);
    spyParagraph.jobRun();
    Paragraph p1 = spyParagraph.getUserParagraph(user1.getUser());
    mockInterpreterResult = mock(InterpreterResult.class);
    when(mockInterpreter.interpret(anyString(), Mockito.<InterpreterContext>any())).thenReturn(mockInterpreterResult);
    when(mockInterpreterResult.code()).thenReturn(Code.SUCCESS);
    List<InterpreterResultMessage> result2 = new ArrayList<>();
    result2.add(new InterpreterResultMessage(Type.TEXT, "result2"));
    when(mockInterpreterResult.message()).thenReturn(result2);
    AuthenticationInfo user2 = new AuthenticationInfo("user2");
    spyParagraph.setAuthenticationInfo(user2);
    spyParagraph.jobRun();
    Paragraph p2 = spyParagraph.getUserParagraph(user2.getUser());
    assertNotEquals(p1.getReturn().toString(), p2.getReturn().toString());
    assertEquals(p1, spyParagraph.getUserParagraph(user1.getUser()));
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) ArrayList(java.util.ArrayList) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ResourcePool(org.apache.zeppelin.resource.ResourcePool) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) InterpreterOption(org.apache.zeppelin.interpreter.InterpreterOption) ArrayList(java.util.ArrayList) List(java.util.List) UserCredentials(org.apache.zeppelin.user.UserCredentials) Credentials(org.apache.zeppelin.user.Credentials) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) Ignore(org.junit.Ignore)

Example 12 with ManagedInterpreterGroup

use of org.apache.zeppelin.interpreter.ManagedInterpreterGroup in project zeppelin by apache.

the class RecoveryTest method testRecovery_2.

@Test
public void testRecovery_2() throws Exception {
    LOG.info("Test testRecovery_2");
    String note1Id = null;
    try {
        note1Id = notebook.createNote("note2", AuthenticationInfo.ANONYMOUS);
        // run python interpreter and create new variable `user`
        notebook.processNote(note1Id, note1 -> {
            Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p1.setText("%python user='abc'");
            return null;
        });
        CloseableHttpResponse post = httpPost("/notebook/job/" + note1Id + "?blocking=true", "");
        assertThat(post, isAllowed());
        Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        assertEquals("OK", resp.get("status"));
        post.close();
        notebook.processNote(note1Id, note1 -> {
            Paragraph p1 = note1.getParagraph(0);
            assertEquals(Job.Status.FINISHED, p1.getStatus());
            TestUtils.getInstance(Notebook.class).saveNote(note1, AuthenticationInfo.ANONYMOUS);
            // restart the python interpreter
            try {
                TestUtils.getInstance(Notebook.class).getInterpreterSettingManager().restart(((ManagedInterpreterGroup) p1.getBindedInterpreter().getInterpreterGroup()).getInterpreterSetting().getId());
            } catch (InterpreterException e) {
                fail();
            }
            return null;
        });
        // shutdown zeppelin and restart it
        shutDown();
        startUp(RecoveryTest.class.getSimpleName(), false);
        Thread.sleep(5 * 1000);
        // run the paragraph again, but change the text to print variable `user`.
        // can not recover the python interpreter, because it has been shutdown.
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            Paragraph p1 = note1.getParagraph(0);
            p1.setText("%python print(user)");
            return null;
        });
        post = httpPost("/notebook/job/" + note1Id + "?blocking=true", "");
        assertEquals("OK", resp.get("status"));
        post.close();
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            Paragraph p1 = note1.getParagraph(0);
            assertEquals(Job.Status.ERROR, p1.getStatus());
            return null;
        });
    } catch (Exception e) {
        LOG.error(e.toString(), e);
        throw e;
    } finally {
        if (null != note1Id) {
            TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) Paragraph(org.apache.zeppelin.notebook.Paragraph) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) Test(org.junit.Test)

Aggregations

ManagedInterpreterGroup (org.apache.zeppelin.interpreter.ManagedInterpreterGroup)12 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)7 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)6 Interpreter (org.apache.zeppelin.interpreter.Interpreter)5 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)4 InterpreterNotFoundException (org.apache.zeppelin.interpreter.InterpreterNotFoundException)4 Credentials (org.apache.zeppelin.user.Credentials)4 UserCredentials (org.apache.zeppelin.user.UserCredentials)4 IOException (java.io.IOException)3 ResourcePool (org.apache.zeppelin.resource.ResourcePool)3 ArrayList (java.util.ArrayList)2 SessionInfo (org.apache.zeppelin.common.SessionInfo)2 AngularObject (org.apache.zeppelin.display.AngularObject)2 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)2 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)2 Notebook (org.apache.zeppelin.notebook.Notebook)2 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 TypeToken (com.google.gson.reflect.TypeToken)1