Search in sources :

Example 11 with InterpreterSetting

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

the class InterpreterRestApi method newSettings.

/**
   * Add new interpreter setting
   *
   * @param message NewInterpreterSettingRequest
   */
@POST
@Path("setting")
@ZeppelinApi
public Response newSettings(String message) {
    try {
        NewInterpreterSettingRequest request = gson.fromJson(message, NewInterpreterSettingRequest.class);
        if (request == null) {
            return new JsonResponse<>(Status.BAD_REQUEST).build();
        }
        Properties p = new Properties();
        p.putAll(request.getProperties());
        InterpreterSetting interpreterSetting = interpreterSettingManager.createNewSetting(request.getName(), request.getGroup(), request.getDependencies(), request.getOption(), p);
        logger.info("new setting created with {}", interpreterSetting.getId());
        return new JsonResponse<>(Status.OK, "", interpreterSetting).build();
    } catch (InterpreterException | IOException e) {
        logger.error("Exception in InterpreterRestApi while creating ", e);
        return new JsonResponse<>(Status.NOT_FOUND, e.getMessage(), ExceptionUtils.getStackTrace(e)).build();
    }
}
Also used : InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) NewInterpreterSettingRequest(org.apache.zeppelin.rest.message.NewInterpreterSettingRequest) IOException(java.io.IOException) Properties(java.util.Properties) JsonResponse(org.apache.zeppelin.server.JsonResponse) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 12 with InterpreterSetting

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

the class InterpreterRestApi method restartSetting.

/**
   * Restart interpreter setting
   */
@PUT
@Path("setting/restart/{settingId}")
@ZeppelinApi
public Response restartSetting(String message, @PathParam("settingId") String settingId) {
    logger.info("Restart interpreterSetting {}, msg={}", settingId, message);
    InterpreterSetting setting = interpreterSettingManager.get(settingId);
    try {
        RestartInterpreterRequest request = gson.fromJson(message, RestartInterpreterRequest.class);
        String noteId = request == null ? null : request.getNoteId();
        if (null == noteId) {
            interpreterSettingManager.close(setting);
        } else {
            interpreterSettingManager.restart(settingId, noteId, SecurityUtils.getPrincipal());
        }
        notebookServer.clearParagraphRuntimeInfo(setting);
    } catch (InterpreterException e) {
        logger.error("Exception in InterpreterRestApi while restartSetting ", e);
        return new JsonResponse<>(Status.NOT_FOUND, e.getMessage(), ExceptionUtils.getStackTrace(e)).build();
    }
    if (setting == null) {
        return new JsonResponse<>(Status.NOT_FOUND, "", settingId).build();
    }
    return new JsonResponse<>(Status.OK, "", setting).build();
}
Also used : RestartInterpreterRequest(org.apache.zeppelin.rest.message.RestartInterpreterRequest) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) JsonResponse(org.apache.zeppelin.server.JsonResponse) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) PUT(javax.ws.rs.PUT)

Example 13 with InterpreterSetting

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

the class NotebookServerTest method testMakeSureNoAngularObjectBroadcastToWebsocketWhoFireTheEvent.

@Test
public void testMakeSureNoAngularObjectBroadcastToWebsocketWhoFireTheEvent() throws IOException {
    // create a notebook
    Note note1 = notebook.createNote(anonymous);
    // get reference to interpreterGroup
    InterpreterGroup interpreterGroup = null;
    List<InterpreterSetting> settings = notebook.getInterpreterSettingManager().getInterpreterSettings(note1.getId());
    for (InterpreterSetting setting : settings) {
        if (setting.getName().equals("md")) {
            interpreterGroup = setting.getInterpreterGroup("anonymous", "sharedProcess");
            break;
        }
    }
    // start interpreter process
    Paragraph p1 = note1.addParagraph(AuthenticationInfo.ANONYMOUS);
    p1.setText("%md start remote interpreter process");
    p1.setAuthenticationInfo(anonymous);
    note1.run(p1.getId());
    // add angularObject
    interpreterGroup.getAngularObjectRegistry().add("object1", "value1", note1.getId(), null);
    // create two sockets and open it
    NotebookSocket sock1 = createWebSocket();
    NotebookSocket sock2 = createWebSocket();
    assertEquals(sock1, sock1);
    assertNotEquals(sock1, sock2);
    notebookServer.onOpen(sock1);
    notebookServer.onOpen(sock2);
    // getNote, getAngularObject
    verify(sock1, times(0)).send(anyString());
    // open the same notebook from sockets
    notebookServer.onMessage(sock1, gson.toJson(new Message(OP.GET_NOTE).put("id", note1.getId())));
    notebookServer.onMessage(sock2, gson.toJson(new Message(OP.GET_NOTE).put("id", note1.getId())));
    reset(sock1);
    reset(sock2);
    // update object from sock1
    notebookServer.onMessage(sock1, gson.toJson(new Message(OP.ANGULAR_OBJECT_UPDATED).put("noteId", note1.getId()).put("name", "object1").put("value", "value1").put("interpreterGroupId", interpreterGroup.getId())));
    // expect object is broadcasted except for where the update is created
    verify(sock1, times(0)).send(anyString());
    verify(sock2, times(1)).send(anyString());
    notebook.removeNote(note1.getId(), anonymous);
}
Also used : Message(org.apache.zeppelin.notebook.socket.Message) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) Note(org.apache.zeppelin.notebook.Note) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 14 with InterpreterSetting

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

the class ZeppelinSparkClusterTest method pySparkDepLoaderTest.

@Test
public void pySparkDepLoaderTest() throws IOException {
    // create new note
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    int sparkVersionNumber = getSparkVersionNumber(note);
    if (isPyspark() && sparkVersionNumber >= 14) {
        // restart spark interpreter
        List<InterpreterSetting> settings = ZeppelinServer.notebook.getBindedInterpreterSettings(note.getId());
        for (InterpreterSetting setting : settings) {
            if (setting.getName().equals("spark")) {
                ZeppelinServer.notebook.getInterpreterSettingManager().restart(setting.getId());
                break;
            }
        }
        // load dep
        Paragraph p0 = note.addParagraph(AuthenticationInfo.ANONYMOUS);
        Map config = p0.getConfig();
        config.put("enabled", true);
        p0.setConfig(config);
        p0.setText("%dep z.load(\"com.databricks:spark-csv_2.11:1.2.0\")");
        p0.setAuthenticationInfo(anonymous);
        note.run(p0.getId());
        waitForFinish(p0);
        assertEquals(Status.FINISHED, p0.getStatus());
        // write test csv file
        File tmpFile = File.createTempFile("test", "csv");
        FileUtils.write(tmpFile, "a,b\n1,2");
        // load data using libraries from dep loader
        Paragraph p1 = note.addParagraph(AuthenticationInfo.ANONYMOUS);
        p1.setConfig(config);
        String sqlContextName = "sqlContext";
        if (sparkVersionNumber >= 20) {
            sqlContextName = "spark";
        }
        p1.setText("%pyspark\n" + "from pyspark.sql import SQLContext\n" + "print(" + sqlContextName + ".read.format('com.databricks.spark.csv')" + ".load('" + tmpFile.getAbsolutePath() + "').count())");
        p1.setAuthenticationInfo(anonymous);
        note.run(p1.getId());
        waitForFinish(p1);
        assertEquals(Status.FINISHED, p1.getStatus());
        assertEquals("2\n", p1.getResult().message().get(0).getData());
    }
    ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
Also used : Note(org.apache.zeppelin.notebook.Note) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) Map(java.util.Map) File(java.io.File) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 15 with InterpreterSetting

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

the class ParagraphTest method returnUnchangedResultsWithDifferentUser.

@Test
public void returnUnchangedResultsWithDifferentUser() throws Throwable {
    InterpreterSettingManager mockInterpreterSettingManager = mock(InterpreterSettingManager.class);
    Note mockNote = mock(Note.class);
    when(mockNote.getCredentials()).thenReturn(mock(Credentials.class));
    Paragraph spyParagraph = spy(new Paragraph("para_1", mockNote, null, null, mockInterpreterSettingManager));
    doReturn("spy").when(spyParagraph).getRequiredReplName();
    Interpreter mockInterpreter = mock(Interpreter.class);
    doReturn(mockInterpreter).when(spyParagraph).getRepl(anyString());
    InterpreterGroup mockInterpreterGroup = mock(InterpreterGroup.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(Lists.<InterpreterSetting>newArrayList());
    InterpreterSetting mockInterpreterSetting = mock(InterpreterSetting.class);
    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.getInterpreterGroup(anyString(), anyString())).thenReturn(mockInterpreterGroup);
    spyInterpreterSettingList.add(mockInterpreterSetting);
    when(mockNote.getId()).thenReturn("any_id");
    when(mockInterpreterSettingManager.getInterpreterSettings(anyString())).thenReturn(spyInterpreterSettingList);
    doReturn("spy script body").when(spyParagraph).getScriptBody();
    when(mockInterpreter.getFormType()).thenReturn(FormType.NONE);
    ParagraphJobListener mockJobListener = mock(ParagraphJobListener.class);
    doReturn(mockJobListener).when(spyParagraph).getListener();
    doNothing().when(mockJobListener).onOutputUpdateAll(Mockito.<Paragraph>any(), Mockito.anyList());
    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 = Lists.newArrayList();
    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());
    List<InterpreterResultMessage> result2 = Lists.newArrayList();
    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) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ResourcePool(org.apache.zeppelin.resource.ResourcePool) InterpreterSettingManager(org.apache.zeppelin.interpreter.InterpreterSettingManager) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) InterpreterOption(org.apache.zeppelin.interpreter.InterpreterOption) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) Credentials(org.apache.zeppelin.user.Credentials) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) Test(org.junit.Test)

Aggregations

InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)21 Note (org.apache.zeppelin.notebook.Note)9 Test (org.junit.Test)9 Message (org.apache.zeppelin.notebook.socket.Message)7 InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)6 Paragraph (org.apache.zeppelin.notebook.Paragraph)6 WatcherMessage (org.apache.zeppelin.notebook.socket.WatcherMessage)5 Map (java.util.Map)4 Path (javax.ws.rs.Path)4 JsonObject (com.google.gson.JsonObject)3 PutMethod (org.apache.commons.httpclient.methods.PutMethod)3 ZeppelinApi (org.apache.zeppelin.annotation.ZeppelinApi)3 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)3 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)3 JsonResponse (org.apache.zeppelin.server.JsonResponse)3 JsonElement (com.google.gson.JsonElement)2 File (java.io.File)2 IOException (java.io.IOException)2 Properties (java.util.Properties)2 PUT (javax.ws.rs.PUT)2