Search in sources :

Example 16 with AngularObjectRegistry

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

the class Notebook method loadNoteFromRepo.

@SuppressWarnings("rawtypes")
public Note loadNoteFromRepo(String id, AuthenticationInfo subject) {
    Note note = null;
    try {
        note = notebookRepo.get(id, subject);
    } catch (IOException e) {
        logger.error("Failed to load " + id, e);
    }
    if (note == null) {
        return null;
    }
    convertFromSingleResultToMultipleResultsFormat(note);
    //Manually inject ALL dependencies, as DI constructor was NOT used
    note.setIndex(this.noteSearchService);
    note.setCredentials(this.credentials);
    note.setInterpreterFactory(replFactory);
    note.setInterpreterSettingManager(interpreterSettingManager);
    note.setJobListenerFactory(jobListenerFactory);
    note.setNotebookRepo(notebookRepo);
    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();
        }
        p.clearRuntimeInfo(null);
    }
    Map<String, List<AngularObject>> savedObjects = note.getAngularObjects();
    if (savedObjects != null) {
        for (String intpGroupName : savedObjects.keySet()) {
            List<AngularObject> objectList = savedObjects.get(intpGroupName);
            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.setNoteEventListener(this);
    note.setNoteNameListener(folders);
    synchronized (notes) {
        notes.put(note.getId(), note);
        folders.putNote(note);
        refreshCron(note.getId());
    }
    for (String name : angularObjectSnapshot.keySet()) {
        SnapshotAngularObject snapshot = angularObjectSnapshot.get(name);
        List<InterpreterSetting> settings = interpreterSettingManager.get();
        for (InterpreterSetting setting : settings) {
            InterpreterGroup intpGroup = setting.getInterpreterGroup(subject.getUser(), note.getId());
            if (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 : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AngularObject(org.apache.zeppelin.display.AngularObject) IOException(java.io.IOException) Date(java.util.Date) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)

Example 17 with AngularObjectRegistry

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

the class Notebook method removeNote.

public void removeNote(String id, AuthenticationInfo subject) {
    Preconditions.checkNotNull(subject, "AuthenticationInfo should not be null");
    Note note;
    synchronized (notes) {
        note = notes.remove(id);
        folders.removeNote(note);
    }
    interpreterSettingManager.removeNoteInterpreterSettingBinding(subject.getUser(), id);
    noteSearchService.deleteIndexDocs(note);
    notebookAuthorization.removeNote(id);
    // remove from all interpreter instance's angular object registry
    for (InterpreterSetting settings : interpreterSettingManager.get()) {
        AngularObjectRegistry registry = settings.getInterpreterGroup(subject.getUser(), id).getAngularObjectRegistry();
        if (registry instanceof RemoteAngularObjectRegistry) {
            // remove paragraph scope object
            for (Paragraph p : note.getParagraphs()) {
                ((RemoteAngularObjectRegistry) registry).removeAllAndNotifyRemoteProcess(id, p.getId());
                // remove app scope object
                List<ApplicationState> appStates = p.getAllApplicationStates();
                if (appStates != null) {
                    for (ApplicationState app : appStates) {
                        ((RemoteAngularObjectRegistry) registry).removeAllAndNotifyRemoteProcess(id, app.getId());
                    }
                }
            }
            // remove note scope object
            ((RemoteAngularObjectRegistry) registry).removeAllAndNotifyRemoteProcess(id, null);
        } else {
            // remove paragraph scope object
            for (Paragraph p : note.getParagraphs()) {
                registry.removeAll(id, p.getId());
                // remove app scope object
                List<ApplicationState> appStates = p.getAllApplicationStates();
                if (appStates != null) {
                    for (ApplicationState app : appStates) {
                        registry.removeAll(id, app.getId());
                    }
                }
            }
            // remove note scope object
            registry.removeAll(id, null);
        }
    }
    ResourcePoolUtils.removeResourcesBelongsToNote(id);
    fireNoteRemoveEvent(note);
    try {
        note.unpersist(subject);
    } catch (IOException e) {
        logger.error(e.toString(), e);
    }
}
Also used : RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry) IOException(java.io.IOException) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)

Example 18 with AngularObjectRegistry

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

the class Paragraph method getInterpreterContextWithoutRunner.

private InterpreterContext getInterpreterContextWithoutRunner(InterpreterOutput output) {
    AngularObjectRegistry registry = null;
    ResourcePool resourcePool = null;
    if (!interpreterSettingManager.getInterpreterSettings(note.getId()).isEmpty()) {
        InterpreterSetting intpGroup = interpreterSettingManager.getInterpreterSettings(note.getId()).get(0);
        registry = intpGroup.getInterpreterGroup(getUser(), note.getId()).getAngularObjectRegistry();
        resourcePool = intpGroup.getInterpreterGroup(getUser(), note.getId()).getResourcePool();
    }
    List<InterpreterContextRunner> runners = new LinkedList<>();
    final Paragraph self = this;
    Credentials credentials = note.getCredentials();
    if (authenticationInfo != null) {
        UserCredentials userCredentials = credentials.getUserCredentials(authenticationInfo.getUser());
        authenticationInfo.setUserCredentials(userCredentials);
    }
    InterpreterContext interpreterContext = new InterpreterContext(note.getId(), getId(), getRequiredReplName(), this.getTitle(), this.getText(), this.getAuthenticationInfo(), this.getConfig(), this.settings, registry, resourcePool, runners, output);
    return interpreterContext;
}
Also used : ResourcePool(org.apache.zeppelin.resource.ResourcePool) UserCredentials(org.apache.zeppelin.user.UserCredentials) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) UserCredentials(org.apache.zeppelin.user.UserCredentials) Credentials(org.apache.zeppelin.user.Credentials)

Example 19 with AngularObjectRegistry

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

the class Paragraph method getInterpreterContext.

private InterpreterContext getInterpreterContext(InterpreterOutput output) {
    AngularObjectRegistry registry = null;
    ResourcePool resourcePool = null;
    if (!interpreterSettingManager.getInterpreterSettings(note.getId()).isEmpty()) {
        InterpreterSetting intpGroup = interpreterSettingManager.getInterpreterSettings(note.getId()).get(0);
        registry = intpGroup.getInterpreterGroup(getUser(), note.getId()).getAngularObjectRegistry();
        resourcePool = intpGroup.getInterpreterGroup(getUser(), note.getId()).getResourcePool();
    }
    List<InterpreterContextRunner> runners = new LinkedList<>();
    for (Paragraph p : note.getParagraphs()) {
        runners.add(new ParagraphRunner(note, note.getId(), p.getId()));
    }
    final Paragraph self = this;
    Credentials credentials = note.getCredentials();
    if (authenticationInfo != null) {
        UserCredentials userCredentials = credentials.getUserCredentials(authenticationInfo.getUser());
        authenticationInfo.setUserCredentials(userCredentials);
    }
    InterpreterContext interpreterContext = new InterpreterContext(note.getId(), getId(), getRequiredReplName(), this.getTitle(), this.getText(), this.getAuthenticationInfo(), this.getConfig(), this.settings, registry, resourcePool, runners, output);
    return interpreterContext;
}
Also used : ResourcePool(org.apache.zeppelin.resource.ResourcePool) UserCredentials(org.apache.zeppelin.user.UserCredentials) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) UserCredentials(org.apache.zeppelin.user.UserCredentials) Credentials(org.apache.zeppelin.user.Credentials)

Example 20 with AngularObjectRegistry

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

the class ParagraphTest method should_extract_variable_from_angular_object_registry.

@Test
public void should_extract_variable_from_angular_object_registry() throws Exception {
    //Given
    final String noteId = "noteId";
    final AngularObjectRegistry registry = mock(AngularObjectRegistry.class);
    final Note note = mock(Note.class);
    final Map<String, Input> inputs = new HashMap<>();
    inputs.put("name", null);
    inputs.put("age", null);
    inputs.put("job", null);
    final String scriptBody = "My name is ${name} and I am ${age=20} years old. " + "My occupation is ${ job = engineer | developer | artists}";
    final Paragraph paragraph = new Paragraph(note, null, null, null);
    final String paragraphId = paragraph.getId();
    final AngularObject nameAO = AngularObjectBuilder.build("name", "DuyHai DOAN", noteId, paragraphId);
    final AngularObject ageAO = AngularObjectBuilder.build("age", 34, noteId, null);
    when(note.getId()).thenReturn(noteId);
    when(registry.get("name", noteId, paragraphId)).thenReturn(nameAO);
    when(registry.get("age", noteId, null)).thenReturn(ageAO);
    final String expected = "My name is DuyHai DOAN and I am 34 years old. " + "My occupation is ${ job = engineer | developer | artists}";
    //When
    final String actual = paragraph.extractVariablesFromAngularRegistry(scriptBody, inputs, registry);
    //Then
    verify(registry).get("name", noteId, paragraphId);
    verify(registry).get("age", noteId, null);
    assertEquals(actual, expected);
}
Also used : Input(org.apache.zeppelin.display.Input) HashMap(java.util.HashMap) AngularObject(org.apache.zeppelin.display.AngularObject) Matchers.anyString(org.mockito.Matchers.anyString) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) Test(org.junit.Test)

Aggregations

AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)48 Test (org.junit.Test)20 HashMap (java.util.HashMap)19 LinkedList (java.util.LinkedList)18 GUI (org.apache.zeppelin.display.GUI)18 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)18 Properties (java.util.Properties)16 AngularObject (org.apache.zeppelin.display.AngularObject)16 LocalResourcePool (org.apache.zeppelin.resource.LocalResourcePool)16 RemoteAngularObjectRegistry (org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)11 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)8 File (java.io.File)5 Map (java.util.Map)5 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)5 Note (org.apache.zeppelin.notebook.Note)5 Before (org.junit.Before)5 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)4 MockInterpreterA (org.apache.zeppelin.interpreter.remote.mock.MockInterpreterA)4 Message (org.apache.zeppelin.notebook.socket.Message)4 Job (org.apache.zeppelin.scheduler.Job)4