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);
}
}
}
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;
}
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;
}
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()));
}
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;
}
Aggregations