Search in sources :

Example 51 with InterpreterGroup

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

the class NotebookServer method angularObjectClientBind.

/**
 * 1. Push the given Angular variable to the target interpreter angular
 * registry given a noteId and a paragraph id.
 * 2. Save AngularObject to note.
 */
protected void angularObjectClientBind(NotebookSocket conn, Message fromMessage) throws Exception {
    String noteId = fromMessage.getType("noteId");
    String varName = fromMessage.getType("name");
    Object varValue = fromMessage.get("value");
    String paragraphId = fromMessage.getType("paragraphId");
    if (paragraphId == null) {
        throw new IllegalArgumentException("target paragraph not specified for " + "angular value bind");
    }
    getNotebook().processNote(noteId, note -> {
        if (note != null) {
            InterpreterGroup interpreterGroup;
            try {
                interpreterGroup = findInterpreterGroupForParagraph(note, paragraphId);
            } catch (Exception e) {
                LOG.error("No interpreter group found for noteId {} and paragraphId {}", noteId, paragraphId, e);
                return null;
            }
            final RemoteAngularObjectRegistry registry = (RemoteAngularObjectRegistry) interpreterGroup.getAngularObjectRegistry();
            AngularObject ao = pushAngularObjectToRemoteRegistry(noteId, paragraphId, varName, varValue, registry, interpreterGroup.getId(), conn);
            note.addOrUpdateAngularObject(interpreterGroup.getId(), ao);
        }
        return null;
    });
}
Also used : InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) AngularObject(org.apache.zeppelin.display.AngularObject) AngularObject(org.apache.zeppelin.display.AngularObject) URISyntaxException(java.net.URISyntaxException) ForbiddenException(org.apache.zeppelin.rest.exception.ForbiddenException) TException(org.apache.thrift.TException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ServiceException(org.apache.zeppelin.interpreter.thrift.ServiceException)

Example 52 with InterpreterGroup

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

the class NotebookServer method updateAngularObjectRegistry.

/**
 * Update the AngularObject object in the note to InterpreterGroup and AngularObjectRegistry.
 */
private void updateAngularObjectRegistry(NotebookSocket conn, Note note) {
    for (Paragraph paragraph : note.getParagraphs()) {
        InterpreterGroup interpreterGroup = null;
        try {
            interpreterGroup = findInterpreterGroupForParagraph(note, paragraph.getId());
        } catch (Exception e) {
            LOG.warn(e.getMessage(), e);
        }
        if (null == interpreterGroup) {
            return;
        }
        RemoteAngularObjectRegistry registry = (RemoteAngularObjectRegistry) interpreterGroup.getAngularObjectRegistry();
        List<AngularObject> angularObjects = note.getAngularObjects(interpreterGroup.getId());
        for (AngularObject ao : angularObjects) {
            if (StringUtils.equals(ao.getNoteId(), note.getId()) && StringUtils.equals(ao.getParagraphId(), paragraph.getId())) {
                pushAngularObjectToRemoteRegistry(ao.getNoteId(), ao.getParagraphId(), ao.getName(), ao.get(), registry, interpreterGroup.getId(), conn);
            }
        }
    }
}
Also used : InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry) AngularObject(org.apache.zeppelin.display.AngularObject) URISyntaxException(java.net.URISyntaxException) ForbiddenException(org.apache.zeppelin.rest.exception.ForbiddenException) TException(org.apache.thrift.TException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ServiceException(org.apache.zeppelin.interpreter.thrift.ServiceException) Paragraph(org.apache.zeppelin.notebook.Paragraph)

Example 53 with InterpreterGroup

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

the class NotebookServerTest method testMakeSureNoAngularObjectBroadcastToWebsocketWhoFireTheEvent.

@Test
public void testMakeSureNoAngularObjectBroadcastToWebsocketWhoFireTheEvent() throws IOException, InterruptedException {
    String note1Id = null;
    try {
        // create a notebook
        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("md")) {
                interpreterGroup = setting.getOrCreateInterpreterGroup("anonymous", note1Id);
                break;
            }
        }
        notebook.processNote(note1Id, note1 -> {
            // start interpreter process
            Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p1.setText("%md start remote interpreter process");
            p1.setAuthenticationInfo(anonymous);
            note1.run(p1.getId());
            return null;
        });
        Status status = notebook.processNote(note1Id, note1 -> note1.getParagraph(0).getStatus());
        // wait for paragraph finished
        while (true) {
            if (status == Job.Status.FINISHED) {
                break;
            }
            Thread.sleep(100);
            status = notebook.processNote(note1Id, note1 -> note1.getParagraph(0).getStatus());
        }
        // sleep for 1 second to make sure job running thread finish to fire event. See ZEPPELIN-3277
        Thread.sleep(1000);
        // add angularObject
        interpreterGroup.getAngularObjectRegistry().add("object1", "value1", note1Id, 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, new Message(OP.GET_NOTE).put("id", note1Id).toJson());
        notebookServer.onMessage(sock2, new Message(OP.GET_NOTE).put("id", note1Id).toJson());
        reset(sock1);
        reset(sock2);
        // update object from sock1
        notebookServer.onMessage(sock1, new Message(OP.ANGULAR_OBJECT_UPDATED).put("noteId", note1Id).put("name", "object1").put("value", "value1").put("interpreterGroupId", interpreterGroup.getId()).toJson());
        // expect object is broadcasted except for where the update is created
        verify(sock1, times(0)).send(anyString());
        verify(sock2, times(1)).send(anyString());
    } 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) Mockito.anyString(org.mockito.Mockito.anyString) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 54 with InterpreterGroup

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

the class NotebookServerTest method unbindAngularObjectFromRemoteForParagraphs.

@Test
public void unbindAngularObjectFromRemoteForParagraphs() throws Exception {
    // Given
    final String varName = "name";
    final String value = "val";
    final Message messageReceived = new Message(OP.ANGULAR_OBJECT_CLIENT_UNBIND).put("noteId", "noteId").put("name", varName).put("paragraphId", "paragraphId");
    try {
        final Notebook notebook = mock(Notebook.class);
        notebookServer.setNotebook(() -> notebook);
        notebookServer.setNotebookService(() -> notebookService);
        final Note note = mock(Note.class, RETURNS_DEEP_STUBS);
        when(notebook.processNote(eq("noteId"), Mockito.any())).then(e -> e.getArgumentAt(1, NoteProcessor.class).process(note));
        final Paragraph paragraph = mock(Paragraph.class, RETURNS_DEEP_STUBS);
        when(note.getParagraph("paragraphId")).thenReturn(paragraph);
        final RemoteAngularObjectRegistry mdRegistry = mock(RemoteAngularObjectRegistry.class);
        final InterpreterGroup mdGroup = new InterpreterGroup("mdGroup");
        mdGroup.setAngularObjectRegistry(mdRegistry);
        when(paragraph.getBindedInterpreter().getInterpreterGroup()).thenReturn(mdGroup);
        final AngularObject<String> ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");
        when(mdRegistry.removeAndNotifyRemoteProcess(varName, "noteId", "paragraphId")).thenReturn(ao1);
        NotebookSocket conn = mock(NotebookSocket.class);
        NotebookSocket otherConn = mock(NotebookSocket.class);
        final String mdMsg1 = notebookServer.serializeMessage(new Message(OP.ANGULAR_OBJECT_REMOVE).put("angularObject", ao1).put("interpreterGroupId", "mdGroup").put("noteId", "noteId").put("paragraphId", "paragraphId"));
        notebookServer.getConnectionManager().noteSocketMap.put("noteId", asList(conn, otherConn));
        // When
        notebookServer.angularObjectClientUnbind(conn, messageReceived);
        // Then
        verify(mdRegistry, never()).removeAndNotifyRemoteProcess(varName, "noteId", null);
        verify(otherConn).send(mdMsg1);
    } finally {
        // reset these so that it won't affect other tests
        notebookServer.setNotebook(() -> NotebookServerTest.notebook);
        notebookServer.setNotebookService(() -> NotebookServerTest.notebookService);
    }
}
Also used : Message(org.apache.zeppelin.common.Message) Notebook(org.apache.zeppelin.notebook.Notebook) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) Note(org.apache.zeppelin.notebook.Note) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry) Mockito.anyString(org.mockito.Mockito.anyString) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 55 with InterpreterGroup

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

the class Note method snapshotAngularObjectRegistry.

// TODO(zjffdu) how does this used ?
private void snapshotAngularObjectRegistry(String user) {
    angularObjects = new HashMap<>();
    List<InterpreterSetting> settings = getBindedInterpreterSettings(Arrays.asList(user));
    if (settings == null || settings.isEmpty()) {
        return;
    }
    for (InterpreterSetting setting : settings) {
        InterpreterGroup intpGroup = setting.getInterpreterGroup(getExecutionContext());
        if (intpGroup != null) {
            AngularObjectRegistry registry = intpGroup.getAngularObjectRegistry();
            angularObjects.put(intpGroup.getId(), registry.getAllWithGlobal(id));
        }
    }
}
Also used : InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)

Aggregations

InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)74 Properties (java.util.Properties)30 Interpreter (org.apache.zeppelin.interpreter.Interpreter)27 Test (org.junit.Test)26 LazyOpenInterpreter (org.apache.zeppelin.interpreter.LazyOpenInterpreter)23 Before (org.junit.Before)19 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)18 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)17 RemoteAngularObjectRegistry (org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)14 IOException (java.io.IOException)12 TException (org.apache.thrift.TException)12 AngularObject (org.apache.zeppelin.display.AngularObject)12 List (java.util.List)10 InterpreterOutput (org.apache.zeppelin.interpreter.InterpreterOutput)10 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)10 Note (org.apache.zeppelin.notebook.Note)9 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)8 Paragraph (org.apache.zeppelin.notebook.Paragraph)8 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)7 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)7