Search in sources :

Example 31 with AngularObject

use of org.apache.zeppelin.display.AngularObject 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 32 with AngularObject

use of org.apache.zeppelin.display.AngularObject in project zeppelin by apache.

the class ZeppelinSparkClusterTest method testAngularObjects.

@Test
public void testAngularObjects() throws IOException, InterpreterNotFoundException {
    assumeTrue("Hadoop version mismatch, skip test", isHadoopVersionMatch());
    String noteId = null;
    try {
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            Paragraph p1 = note.addNewParagraph(anonymous);
            // add local angular object
            p1.setText("%spark z.angularBind(\"name\", \"world\")");
            note.run(p1.getId(), true);
            assertEquals(Status.FINISHED, p1.getStatus());
            // angular object is saved to InterpreterGroup's AngularObjectRegistry
            List<AngularObject> angularObjects;
            try {
                angularObjects = p1.getBindedInterpreter().getInterpreterGroup().getAngularObjectRegistry().getAll(note.getId(), null);
                assertEquals(1, angularObjects.size());
                assertEquals("name", angularObjects.get(0).getName());
                assertEquals("world", angularObjects.get(0).get());
            } catch (InterpreterNotFoundException e) {
                fail();
            }
            // angular object is saved to note as well.
            try {
                angularObjects = note.getAngularObjects(p1.getBindedInterpreter().getInterpreterGroup().getId());
                assertEquals(1, angularObjects.size());
                assertEquals("name", angularObjects.get(0).getName());
                assertEquals("world", angularObjects.get(0).get());
            } catch (InterpreterNotFoundException e) {
                fail();
            }
            // remove local angular object
            Paragraph p2 = note.addNewParagraph(anonymous);
            p2.setText("%spark z.angularUnbind(\"name\")");
            note.run(p2.getId(), true);
            assertEquals(Status.FINISHED, p2.getStatus());
            try {
                angularObjects = p1.getBindedInterpreter().getInterpreterGroup().getAngularObjectRegistry().getAll(note.getId(), null);
                assertEquals(0, angularObjects.size());
            } catch (InterpreterNotFoundException e) {
                fail();
            }
            try {
                angularObjects = note.getAngularObjects(p1.getBindedInterpreter().getInterpreterGroup().getId());
                assertEquals(0, angularObjects.size());
            } catch (InterpreterNotFoundException e) {
                fail();
            }
            // add global angular object
            Paragraph p3 = note.addNewParagraph(anonymous);
            p3.setText("%spark z.angularBindGlobal(\"name2\", \"world2\")");
            note.run(p3.getId(), true);
            assertEquals(Status.FINISHED, p3.getStatus());
            List<AngularObject> globalAngularObjects;
            try {
                globalAngularObjects = p3.getBindedInterpreter().getInterpreterGroup().getAngularObjectRegistry().getAll(null, null);
                assertEquals(1, globalAngularObjects.size());
                assertEquals("name2", globalAngularObjects.get(0).getName());
                assertEquals("world2", globalAngularObjects.get(0).get());
            } catch (InterpreterNotFoundException e) {
                fail();
            }
            // global angular object is not saved to note
            try {
                angularObjects = note.getAngularObjects(p1.getBindedInterpreter().getInterpreterGroup().getId());
                assertEquals(0, angularObjects.size());
            } catch (InterpreterNotFoundException e) {
                fail();
            }
            // remove global angular object
            Paragraph p4 = note.addNewParagraph(anonymous);
            p4.setText("%spark z.angularUnbindGlobal(\"name2\")");
            note.run(p4.getId(), true);
            assertEquals(Status.FINISHED, p4.getStatus());
            try {
                globalAngularObjects = p4.getBindedInterpreter().getInterpreterGroup().getAngularObjectRegistry().getAll(note.getId(), null);
                assertEquals(0, globalAngularObjects.size());
            } catch (InterpreterNotFoundException e) {
                fail();
            }
            // global angular object is not saved to note
            try {
                angularObjects = note.getAngularObjects(p1.getBindedInterpreter().getInterpreterGroup().getId());
                assertEquals(0, angularObjects.size());
            } catch (InterpreterNotFoundException e) {
                fail();
            }
            return null;
        });
    } finally {
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) InterpreterNotFoundException(org.apache.zeppelin.interpreter.InterpreterNotFoundException) AngularObject(org.apache.zeppelin.display.AngularObject) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 33 with AngularObject

use of org.apache.zeppelin.display.AngularObject in project zeppelin by apache.

the class ZeppelinContext method angularGlobal.

/**
 * Get angular object. Look up global scope
 *
 * @param name variable name
 * @return value
 */
@Deprecated
public Object angularGlobal(String name) {
    AngularObjectRegistry registry = interpreterContext.getAngularObjectRegistry();
    AngularObject ao = registry.get(name, null, null);
    if (ao == null) {
        return null;
    } else {
        return ao.get();
    }
}
Also used : AngularObject(org.apache.zeppelin.display.AngularObject) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry)

Example 34 with AngularObject

use of org.apache.zeppelin.display.AngularObject in project zeppelin by apache.

the class NotebookServer method angularObjectUpdated.

/**
 * 1. When angular object updated from client.
 * 2. Save AngularObject to note.
 *
 * @param conn        the web socket.
 * @param fromMessage the message.
 */
private void angularObjectUpdated(NotebookSocket conn, ServiceContext context, Message fromMessage) throws IOException {
    String noteId = (String) fromMessage.get("noteId");
    String paragraphId = (String) fromMessage.get("paragraphId");
    String interpreterGroupId = (String) fromMessage.get("interpreterGroupId");
    String varName = (String) fromMessage.get("name");
    Object varValue = fromMessage.get("value");
    String user = fromMessage.principal;
    getNotebookService().updateAngularObject(noteId, paragraphId, interpreterGroupId, varName, varValue, context, new WebSocketServiceCallback<AngularObject>(conn) {

        @Override
        public void onSuccess(AngularObject ao, ServiceContext context) throws IOException {
            super.onSuccess(ao, context);
            connectionManager.broadcastExcept(noteId, new Message(OP.ANGULAR_OBJECT_UPDATE).put("angularObject", ao).put("interpreterGroupId", interpreterGroupId).put("noteId", noteId).put("paragraphId", ao.getParagraphId()), conn);
            getNotebook().processNote(noteId, note -> {
                note.addOrUpdateAngularObject(interpreterGroupId, ao);
                return null;
            });
        }
    });
}
Also used : ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) TestUtils(org.apache.zeppelin.utils.TestUtils) Arrays(java.util.Arrays) TypeToken(com.google.gson.reflect.TypeToken) Provider(javax.inject.Provider) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) OnMessage(javax.websocket.OnMessage) GUI(org.apache.zeppelin.display.GUI) LoggerFactory(org.slf4j.LoggerFactory) ManagedOperation(org.eclipse.jetty.util.annotation.ManagedOperation) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) StringUtils(org.apache.commons.lang3.StringUtils) TicketContainer(org.apache.zeppelin.ticket.TicketContainer) GsonBuilder(com.google.gson.GsonBuilder) ManagedAttribute(org.eclipse.jetty.util.annotation.ManagedAttribute) ServiceContext(org.apache.zeppelin.service.ServiceContext) HeliumPackage(org.apache.zeppelin.helium.HeliumPackage) ParagraphInfo(org.apache.zeppelin.interpreter.thrift.ParagraphInfo) Gson(com.google.gson.Gson) Map(java.util.Map) ClusterEvent(org.apache.zeppelin.cluster.event.ClusterEvent) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) AuthorizationService(org.apache.zeppelin.notebook.AuthorizationService) ParagraphJobListener(org.apache.zeppelin.notebook.ParagraphJobListener) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) AngularObject(org.apache.zeppelin.display.AngularObject) Revision(org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl.Revision) Session(javax.websocket.Session) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) NoteInfo(org.apache.zeppelin.notebook.NoteInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) JupyterUtil(org.apache.zeppelin.jupyter.JupyterUtil) ForbiddenException(org.apache.zeppelin.rest.exception.ForbiddenException) ServerEndpoint(javax.websocket.server.ServerEndpoint) Executors(java.util.concurrent.Executors) ClusterManagerServer(org.apache.zeppelin.cluster.ClusterManagerServer) JobManagerService(org.apache.zeppelin.service.JobManagerService) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry) List(java.util.List) Type(java.lang.reflect.Type) Status(org.apache.zeppelin.scheduler.Job.Status) NotebookImportDeserializer(org.apache.zeppelin.notebook.NotebookImportDeserializer) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) MSG_ID_NOT_DEFINED(org.apache.zeppelin.common.Message.MSG_ID_NOT_DEFINED) RemoteInterpreterProcessListener(org.apache.zeppelin.interpreter.remote.RemoteInterpreterProcessListener) CloseReason(javax.websocket.CloseReason) ClusterEventListener(org.apache.zeppelin.cluster.event.ClusterEventListener) Input(org.apache.zeppelin.display.Input) InterpreterSettingsList(org.apache.zeppelin.types.InterpreterSettingsList) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ClusterMessage(org.apache.zeppelin.cluster.event.ClusterMessage) OnOpen(javax.websocket.OnOpen) AngularObjectRegistryListener(org.apache.zeppelin.display.AngularObjectRegistryListener) IdHashes(org.apache.zeppelin.util.IdHashes) OnClose(javax.websocket.OnClose) Message(org.apache.zeppelin.common.Message) ExecutorService(java.util.concurrent.ExecutorService) OP(org.apache.zeppelin.common.Message.OP) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) NoteEventListener(org.apache.zeppelin.notebook.NoteEventListener) Paragraph(org.apache.zeppelin.notebook.Paragraph) CorsUtils(org.apache.zeppelin.utils.CorsUtils) NotebookService(org.apache.zeppelin.service.NotebookService) Logger(org.slf4j.Logger) ApplicationEventListener(org.apache.zeppelin.helium.ApplicationEventListener) SimpleServiceCallback(org.apache.zeppelin.service.SimpleServiceCallback) EndpointConfig(javax.websocket.EndpointConfig) Note(org.apache.zeppelin.notebook.Note) TException(org.apache.thrift.TException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Notebook(org.apache.zeppelin.notebook.Notebook) ServiceException(org.apache.zeppelin.interpreter.thrift.ServiceException) InterpreterCompletion(org.apache.zeppelin.interpreter.thrift.InterpreterCompletion) ZEPPELIN_ALLOWED_ORIGINS(org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars.ZEPPELIN_ALLOWED_ORIGINS) ConfigurationService(org.apache.zeppelin.service.ConfigurationService) OnMessage(javax.websocket.OnMessage) ClusterMessage(org.apache.zeppelin.cluster.event.ClusterMessage) Message(org.apache.zeppelin.common.Message) ServiceContext(org.apache.zeppelin.service.ServiceContext) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) AngularObject(org.apache.zeppelin.display.AngularObject) AngularObject(org.apache.zeppelin.display.AngularObject) IOException(java.io.IOException)

Example 35 with AngularObject

use of org.apache.zeppelin.display.AngularObject in project zeppelin by apache.

the class BaseInterpreterTest method getIntpContext.

protected InterpreterContext getIntpContext() {
    final AtomicInteger onAdd = new AtomicInteger(0);
    final AtomicInteger onUpdate = new AtomicInteger(0);
    final AtomicInteger onRemove = new AtomicInteger(0);
    AngularObjectRegistry registry = new AngularObjectRegistry("intpId", new AngularObjectRegistryListener() {

        @Override
        public void onAddAngularObject(String interpreterGroupId, AngularObject angularObject) {
            onAdd.incrementAndGet();
        }

        @Override
        public void onUpdateAngularObject(String interpreterGroupId, AngularObject angularObject) {
            onUpdate.incrementAndGet();
        }

        @Override
        public void onRemoveAngularObject(String interpreterGroupId, AngularObject angularObject) {
            onRemove.incrementAndGet();
        }
    });
    AuthenticationInfo authenticationInfo = new AuthenticationInfo("user");
    return InterpreterContext.builder().setNoteId("noteId").setNoteName("noteName").setParagraphId("paragraphId").setAuthenticationInfo(authenticationInfo).setAngularObjectRegistry(registry).setInterpreterOut(new InterpreterOutput()).setIntpEventClient(mock(RemoteInterpreterEventClient.class)).build();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InterpreterOutput(org.apache.zeppelin.interpreter.InterpreterOutput) AngularObject(org.apache.zeppelin.display.AngularObject) AngularObjectRegistryListener(org.apache.zeppelin.display.AngularObjectRegistryListener) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo)

Aggregations

AngularObject (org.apache.zeppelin.display.AngularObject)38 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)21 IOException (java.io.IOException)12 Map (java.util.Map)11 TException (org.apache.thrift.TException)11 RemoteAngularObjectRegistry (org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)10 List (java.util.List)8 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)8 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)8 UnknownHostException (java.net.UnknownHostException)7 HashMap (java.util.HashMap)7 Message (org.apache.zeppelin.common.Message)7 ServiceException (org.apache.zeppelin.interpreter.thrift.ServiceException)7 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 ZeppelinConfiguration (org.apache.zeppelin.conf.ZeppelinConfiguration)5 Arrays (java.util.Arrays)4 LinkedList (java.util.LinkedList)4 OP (org.apache.zeppelin.common.Message.OP)4