Search in sources :

Example 26 with InterpreterGroup

use of org.apache.zeppelin.interpreter.InterpreterGroup 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 InterpreterGroup

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

the class Notebook method loadNoteFromRepo.

@SuppressWarnings("rawtypes")
public Note loadNoteFromRepo(String id, AuthenticationInfo subject) {
    Note note = null;
    try {
        // note can be safely returned here, because it's just broadcasted in json later on
        note = processNote(id, n -> n);
    } catch (IOException e) {
        LOGGER.error("Fail to get note: {}", id, e);
        return null;
    }
    if (note == null) {
        return null;
    }
    // Manually inject ALL dependencies, as DI constructor was NOT used
    note.setCredentials(this.credentials);
    note.setInterpreterFactory(replFactory);
    note.setInterpreterSettingManager(interpreterSettingManager);
    note.setParagraphJobListener(this.paragraphJobListener);
    note.setCronSupported(getConf());
    if (note.getDefaultInterpreterGroup() == null) {
        note.setDefaultInterpreterGroup(conf.getString(ConfVars.ZEPPELIN_INTERPRETER_GROUP_DEFAULT));
    }
    Map<String, SnapshotAngularObject> angularObjectSnapshot = new HashMap<>();
    // restore angular object --------------
    Date lastUpdatedDate = new Date(0);
    for (Paragraph p : note.getParagraphs()) {
        p.setNote(note);
        if (p.getDateFinished() != null && lastUpdatedDate.before(p.getDateFinished())) {
            lastUpdatedDate = p.getDateFinished();
        }
    }
    Map<String, List<AngularObject>> savedObjects = note.getAngularObjects();
    if (savedObjects != null) {
        for (Entry<String, List<AngularObject>> intpGroupNameEntry : savedObjects.entrySet()) {
            String intpGroupName = intpGroupNameEntry.getKey();
            List<AngularObject> objectList = intpGroupNameEntry.getValue();
            for (AngularObject object : objectList) {
                SnapshotAngularObject snapshot = angularObjectSnapshot.get(object.getName());
                if (snapshot == null || snapshot.getLastUpdate().before(lastUpdatedDate)) {
                    angularObjectSnapshot.put(object.getName(), new SnapshotAngularObject(intpGroupName, object, lastUpdatedDate));
                }
            }
        }
    }
    note.setNoteEventListeners(this.noteEventListeners);
    for (Entry<String, SnapshotAngularObject> angularObjectSnapshotEntry : angularObjectSnapshot.entrySet()) {
        String name = angularObjectSnapshotEntry.getKey();
        SnapshotAngularObject snapshot = angularObjectSnapshotEntry.getValue();
        List<InterpreterSetting> settings = interpreterSettingManager.get();
        for (InterpreterSetting setting : settings) {
            InterpreterGroup intpGroup = setting.getInterpreterGroup(note.getExecutionContext());
            if (intpGroup != null && intpGroup.getId().equals(snapshot.getIntpGroupId())) {
                AngularObjectRegistry registry = intpGroup.getAngularObjectRegistry();
                String noteId = snapshot.getAngularObject().getNoteId();
                String paragraphId = snapshot.getAngularObject().getParagraphId();
                // at this point, remote interpreter process is not created.
                // so does not make sense add it to the remote.
                // 
                // therefore instead of addAndNotifyRemoteProcess(), need to use add()
                // that results add angularObject only in ZeppelinServer side not remoteProcessSide
                registry.add(name, snapshot.getAngularObject().get(), noteId, paragraphId);
            }
        }
    }
    return note;
}
Also used : ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) NoteNode(org.apache.zeppelin.notebook.NoteManager.NoteNode) Date(java.util.Date) NotebookRepoSync(org.apache.zeppelin.notebook.repo.NotebookRepoSync) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) NotebookRepo(org.apache.zeppelin.notebook.repo.NotebookRepo) SchedulerException(org.quartz.SchedulerException) Map(java.util.Map) ConfVars(org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars) Folder(org.apache.zeppelin.notebook.NoteManager.Folder) Job(org.apache.zeppelin.scheduler.Job) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) NotebookRepoWithVersionControl(org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl) InterpreterFactory(org.apache.zeppelin.interpreter.InterpreterFactory) LinkedList(java.util.LinkedList) AngularObject(org.apache.zeppelin.display.AngularObject) Revision(org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl.Revision) Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) InterpreterSettingManager(org.apache.zeppelin.interpreter.InterpreterSettingManager) Logger(org.slf4j.Logger) Set(java.util.Set) IOException(java.io.IOException) Credentials(org.apache.zeppelin.user.Credentials) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) Collectors(java.util.stream.Collectors) List(java.util.List) InterpreterNotFoundException(org.apache.zeppelin.interpreter.InterpreterNotFoundException) Entry(java.util.Map.Entry) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HashMap(java.util.HashMap) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) AngularObject(org.apache.zeppelin.display.AngularObject) IOException(java.io.IOException) Date(java.util.Date) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry)

Example 28 with InterpreterGroup

use of org.apache.zeppelin.interpreter.InterpreterGroup in project SSM by Intel-bigdata.

the class RemoteInterpreterEventPoller method getAllResourcePoolExcept.

private ResourceSet getAllResourcePoolExcept() {
    ResourceSet resourceSet = new ResourceSet();
    for (InterpreterGroup intpGroup : InterpreterGroup.getAll()) {
        if (intpGroup.getId().equals(interpreterGroup.getId())) {
            continue;
        }
        RemoteInterpreterProcess remoteInterpreterProcess = intpGroup.getRemoteInterpreterProcess();
        if (remoteInterpreterProcess == null) {
            ResourcePool localPool = intpGroup.getResourcePool();
            if (localPool != null) {
                resourceSet.addAll(localPool.getAll());
            }
        } else if (interpreterProcess.isRunning()) {
            Client client = null;
            boolean broken = false;
            try {
                client = remoteInterpreterProcess.getClient();
                List<String> resourceList = client.resourcePoolGetAll();
                Gson gson = new Gson();
                for (String res : resourceList) {
                    resourceSet.add(gson.fromJson(res, Resource.class));
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                broken = true;
            } finally {
                if (client != null) {
                    intpGroup.getRemoteInterpreterProcess().releaseClient(client, broken);
                }
            }
        }
    }
    return resourceSet;
}
Also used : InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) Gson(com.google.gson.Gson) ResourcePool(org.apache.zeppelin.resource.ResourcePool) LinkedList(java.util.LinkedList) List(java.util.List) ResourceSet(org.apache.zeppelin.resource.ResourceSet) Client(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterService.Client) TException(org.apache.thrift.TException)

Example 29 with InterpreterGroup

use of org.apache.zeppelin.interpreter.InterpreterGroup in project SSM by Intel-bigdata.

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)

Example 30 with InterpreterGroup

use of org.apache.zeppelin.interpreter.InterpreterGroup in project SSM by Intel-bigdata.

the class MockInterpreterB method getInterpreterA.

public MockInterpreterA getInterpreterA() {
    InterpreterGroup interpreterGroup = getInterpreterGroup();
    synchronized (interpreterGroup) {
        for (List<Interpreter> interpreters : interpreterGroup.values()) {
            boolean belongsToSameNoteGroup = false;
            MockInterpreterA a = null;
            for (Interpreter intp : interpreters) {
                if (intp.getClassName().equals(MockInterpreterA.class.getName())) {
                    Interpreter p = intp;
                    while (p instanceof WrappedInterpreter) {
                        p = ((WrappedInterpreter) p).getInnerInterpreter();
                    }
                    a = (MockInterpreterA) p;
                }
                Interpreter p = intp;
                while (p instanceof WrappedInterpreter) {
                    p = ((WrappedInterpreter) p).getInnerInterpreter();
                }
                if (this == p) {
                    belongsToSameNoteGroup = true;
                }
            }
            if (belongsToSameNoteGroup) {
                return a;
            }
        }
    }
    return null;
}
Also used : WrappedInterpreter(org.apache.zeppelin.interpreter.WrappedInterpreter) Interpreter(org.apache.zeppelin.interpreter.Interpreter) WrappedInterpreter(org.apache.zeppelin.interpreter.WrappedInterpreter) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup)

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