Search in sources :

Example 26 with Message

use of org.apache.zeppelin.common.Message in project zeppelin by apache.

the class NotebookServerTest method testLoadAngularObjectFromNote.

@Test
public void testLoadAngularObjectFromNote() throws IOException, InterruptedException {
    // create a notebook
    String note1Id = null;
    try {
        note1Id = notebook.createNote("note1", anonymous);
        // get reference to interpreterGroup
        InterpreterGroup interpreterGroup = null;
        List<InterpreterSetting> settings = notebook.getInterpreterSettingManager().get();
        for (InterpreterSetting setting : settings) {
            if (setting.getName().equals("angular")) {
                interpreterGroup = setting.getOrCreateInterpreterGroup("anonymous", note1Id);
                break;
            }
        }
        String p1Id = notebook.processNote(note1Id, note1 -> {
            // start interpreter process
            Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p1.setText("%angular <h2>Bind here : {{COMMAND_TYPE}}</h2>");
            p1.setAuthenticationInfo(anonymous);
            note1.run(p1.getId());
            return p1.getId();
        });
        // wait for paragraph finished
        Status status = notebook.processNote(note1Id, note1 -> note1.getParagraph(p1Id).getStatus());
        while (true) {
            System.out.println("loop");
            if (status == Job.Status.FINISHED) {
                break;
            }
            Thread.sleep(100);
            status = notebook.processNote(note1Id, note1 -> note1.getParagraph(p1Id).getStatus());
        }
        // sleep for 1 second to make sure job running thread finish to fire event. See ZEPPELIN-3277
        Thread.sleep(1000);
        // set note AngularObject
        AngularObject ao = new AngularObject("COMMAND_TYPE", "COMMAND_TYPE_VALUE", note1Id, p1Id, null);
        notebook.processNote(note1Id, note1 -> {
            note1.addOrUpdateAngularObject("angular-shared_process", ao);
            return null;
        });
        // create sockets and open it
        NotebookSocket sock1 = createWebSocket();
        notebookServer.onOpen(sock1);
        // Check the AngularObjectRegistry of the interpreterGroup before executing GET_NOTE
        Map<String, Map<String, AngularObject>> mapRegistry1 = interpreterGroup.getAngularObjectRegistry().getRegistry();
        assertEquals(0, mapRegistry1.size());
        // open the notebook from sockets, AngularObjectRegistry that triggers the update of the interpreterGroup
        notebookServer.onMessage(sock1, new Message(OP.GET_NOTE).put("id", note1Id).toJson());
        Thread.sleep(1000);
        // After executing GET_NOTE, check the AngularObjectRegistry of the interpreterGroup
        Map<String, Map<String, AngularObject>> mapRegistry2 = interpreterGroup.getAngularObjectRegistry().getRegistry();
        assertEquals(2, mapRegistry1.size());
        AngularObject ao1 = mapRegistry2.get(note1Id + "_" + p1Id).get("COMMAND_TYPE");
        assertEquals("COMMAND_TYPE", ao1.getName());
        assertEquals("COMMAND_TYPE_VALUE", ao1.get());
    } finally {
        if (note1Id != null) {
            notebook.removeNote(note1Id, anonymous);
        }
    }
}
Also used : Status(org.apache.zeppelin.scheduler.Job.Status) ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) TestUtils(org.apache.zeppelin.utils.TestUtils) Arrays(java.util.Arrays) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) InetAddress(java.net.InetAddress) ServiceContext(org.apache.zeppelin.service.ServiceContext) ParagraphInfo(org.apache.zeppelin.interpreter.thrift.ParagraphInfo) Arrays.asList(java.util.Arrays.asList) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Job(org.apache.zeppelin.scheduler.Job) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) AuthorizationService(org.apache.zeppelin.notebook.AuthorizationService) NotebookRepoWithVersionControl(org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl) AngularObjectBuilder(org.apache.zeppelin.display.AngularObjectBuilder) AngularObject(org.apache.zeppelin.display.AngularObject) AfterClass(org.junit.AfterClass) NoteProcessor(org.apache.zeppelin.notebook.Notebook.NoteProcessor) NoteInfo(org.apache.zeppelin.notebook.NoteInfo) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Status(org.apache.zeppelin.scheduler.Job.Status) Mockito.mock(org.mockito.Mockito.mock) BeforeClass(org.junit.BeforeClass) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HttpServletRequest(javax.servlet.http.HttpServletRequest) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) Message(org.apache.zeppelin.common.Message) Mockito.anyString(org.mockito.Mockito.anyString) OP(org.apache.zeppelin.common.Message.OP) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) Before(org.junit.Before) Paragraph(org.apache.zeppelin.notebook.Paragraph) NotebookService(org.apache.zeppelin.service.NotebookService) Assert.assertNotNull(org.junit.Assert.assertNotNull) Note(org.apache.zeppelin.notebook.Note) Assert.assertTrue(org.junit.Assert.assertTrue) TException(org.apache.thrift.TException) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UnknownHostException(java.net.UnknownHostException) Notebook(org.apache.zeppelin.notebook.Notebook) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ServiceException(org.apache.zeppelin.interpreter.thrift.ServiceException) AbstractTestRestApi(org.apache.zeppelin.rest.AbstractTestRestApi) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) Mockito.reset(org.mockito.Mockito.reset) Assert.assertEquals(org.junit.Assert.assertEquals) Message(org.apache.zeppelin.common.Message) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) AngularObject(org.apache.zeppelin.display.AngularObject) Mockito.anyString(org.mockito.Mockito.anyString) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 27 with Message

use of org.apache.zeppelin.common.Message in project zeppelin by apache.

the class NotebookServerTest method testCreateNoteWithDefaultInterpreterId.

@Test
public void testCreateNoteWithDefaultInterpreterId() throws IOException {
    // create two sockets and open it
    NotebookSocket sock1 = createWebSocket();
    NotebookSocket sock2 = createWebSocket();
    assertEquals(sock1, sock1);
    assertNotEquals(sock1, sock2);
    notebookServer.onOpen(sock1);
    notebookServer.onOpen(sock2);
    String noteName = "Note with millis " + System.currentTimeMillis();
    String defaultInterpreterId = "";
    List<InterpreterSetting> settings = notebook.getInterpreterSettingManager().get();
    if (settings.size() > 1) {
        defaultInterpreterId = settings.get(0).getId();
    }
    // create note from sock1
    notebookServer.onMessage(sock1, new Message(OP.NEW_NOTE).put("name", noteName).put("defaultInterpreterId", defaultInterpreterId).toJson());
    int sendCount = 2;
    if (ZeppelinConfiguration.create().isZeppelinNotebookCollaborativeModeEnable()) {
        sendCount++;
    }
    // expect the events are broadcasted properly
    verify(sock1, times(sendCount)).send(anyString());
    String createdNoteId = null;
    for (NoteInfo noteInfo : notebook.getNotesInfo()) {
        ;
        if (notebook.processNote(noteInfo.getId(), Note::getName).equals(noteName)) {
            createdNoteId = noteInfo.getId();
            break;
        }
    }
    if (settings.size() > 1) {
        assertEquals(notebook.getInterpreterSettingManager().getDefaultInterpreterSetting(createdNoteId).getId(), defaultInterpreterId);
    }
    notebook.removeNote(createdNoteId, anonymous);
}
Also used : NoteInfo(org.apache.zeppelin.notebook.NoteInfo) Message(org.apache.zeppelin.common.Message) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

Example 28 with Message

use of org.apache.zeppelin.common.Message in project zeppelin by apache.

the class NotebookServerTest method testImportJupyterNote.

@Test
public void testImportJupyterNote() throws IOException {
    String jupyterNoteJson = IOUtils.toString(getClass().getResourceAsStream("/Lecture-4.ipynb"), StandardCharsets.UTF_8);
    String msg = "{\"op\":\"IMPORT_NOTE\",\"data\":" + "{\"note\": " + jupyterNoteJson + "}}";
    Message messageReceived = notebookServer.deserializeMessage(msg);
    String noteId = null;
    ServiceContext context = new ServiceContext(AuthenticationInfo.ANONYMOUS, new HashSet<>());
    try {
        try {
            noteId = notebookServer.importNote(null, context, messageReceived);
        } catch (NullPointerException e) {
            // broadcastNoteList(); failed nothing to worry.
            LOG.error("Exception in NotebookServerTest while testImportJupyterNote, failed nothing to " + "worry ", e);
        }
        notebook.processNote(noteId, note -> {
            assertNotNull(note);
            assertTrue(note.getName(), note.getName().startsWith("Note converted from Jupyter_"));
            assertEquals("md", note.getParagraphs().get(0).getIntpText());
            assertEquals("\n# matplotlib - 2D and 3D plotting in Python", note.getParagraphs().get(0).getScriptText());
            return null;
        });
    } finally {
        if (noteId != null) {
            notebook.removeNote(noteId, anonymous);
        }
    }
}
Also used : Message(org.apache.zeppelin.common.Message) ServiceContext(org.apache.zeppelin.service.ServiceContext) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

Example 29 with Message

use of org.apache.zeppelin.common.Message in project zeppelin by apache.

the class ConnectionManager method checkCollaborativeStatus.

private void checkCollaborativeStatus(String noteId, List<NotebookSocket> socketList) {
    if (!collaborativeModeEnable.booleanValue()) {
        return;
    }
    boolean collaborativeStatusNew = socketList.size() > 1;
    if (collaborativeStatusNew) {
        collaborativeModeList.add(noteId);
    } else {
        collaborativeModeList.remove(noteId);
    }
    Message message = new Message(Message.OP.COLLABORATIVE_MODE_STATUS);
    message.put("status", collaborativeStatusNew);
    if (collaborativeStatusNew) {
        HashSet<String> userList = new HashSet<>();
        for (NotebookSocket noteSocket : socketList) {
            userList.add(noteSocket.getUser());
        }
        message.put("users", userList);
    }
    broadcast(noteId, message);
}
Also used : Message(org.apache.zeppelin.common.Message) WatcherMessage(org.apache.zeppelin.notebook.socket.WatcherMessage) HashSet(java.util.HashSet)

Example 30 with Message

use of org.apache.zeppelin.common.Message in project zeppelin by apache.

the class NotebookServer method patchParagraph.

private void patchParagraph(NotebookSocket conn, ServiceContext context, Message fromMessage) throws IOException {
    if (!collaborativeModeEnable) {
        return;
    }
    String paragraphId = fromMessage.getType("id", LOG);
    if (paragraphId == null) {
        return;
    }
    String noteId = connectionManager.getAssociatedNoteId(conn);
    if (noteId == null) {
        noteId = fromMessage.getType("noteId", LOG);
        if (noteId == null) {
            return;
        }
    }
    final String noteId2 = noteId;
    String patchText = fromMessage.getType("patch", LOG);
    if (patchText == null) {
        return;
    }
    getNotebookService().patchParagraph(noteId, paragraphId, patchText, context, new WebSocketServiceCallback<String>(conn) {

        @Override
        public void onSuccess(String result, ServiceContext context) throws IOException {
            super.onSuccess(result, context);
            Message message = new Message(OP.PATCH_PARAGRAPH).put("patch", result).put("paragraphId", paragraphId);
            connectionManager.broadcastExcept(noteId2, message, conn);
        }
    });
}
Also used : OnMessage(javax.websocket.OnMessage) ClusterMessage(org.apache.zeppelin.cluster.event.ClusterMessage) Message(org.apache.zeppelin.common.Message) ServiceContext(org.apache.zeppelin.service.ServiceContext) IOException(java.io.IOException)

Aggregations

Message (org.apache.zeppelin.common.Message)49 OnMessage (javax.websocket.OnMessage)31 ClusterMessage (org.apache.zeppelin.cluster.event.ClusterMessage)31 IOException (java.io.IOException)18 ServiceContext (org.apache.zeppelin.service.ServiceContext)18 Paragraph (org.apache.zeppelin.notebook.Paragraph)12 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 AngularObject (org.apache.zeppelin.display.AngularObject)10 Test (org.junit.Test)10 Mockito.anyString (org.mockito.Mockito.anyString)10 UnknownHostException (java.net.UnknownHostException)8 TException (org.apache.thrift.TException)8 ServiceException (org.apache.zeppelin.interpreter.thrift.ServiceException)8 Note (org.apache.zeppelin.notebook.Note)8 RemoteAngularObjectRegistry (org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)7 HashMap (java.util.HashMap)6 List (java.util.List)6 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)6