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