Search in sources :

Example 36 with AngularObject

use of org.apache.zeppelin.display.AngularObject in project SSM by Intel-bigdata.

the class RemoteInterpreterEventPoller method run.

@Override
public void run() {
    Client client = null;
    AppendOutputRunner runner = new AppendOutputRunner(listener);
    ScheduledFuture<?> appendFuture = appendService.scheduleWithFixedDelay(runner, 0, AppendOutputRunner.BUFFER_TIME_MS, TimeUnit.MILLISECONDS);
    while (!shutdown) {
        // wait and retry
        if (!interpreterProcess.isRunning()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            // nothing to do
            }
            continue;
        }
        try {
            client = interpreterProcess.getClient();
        } catch (Exception e1) {
            logger.error("Can't get RemoteInterpreterEvent", e1);
            waitQuietly();
            continue;
        }
        RemoteInterpreterEvent event = null;
        boolean broken = false;
        try {
            event = client.getEvent();
        } catch (TException e) {
            broken = true;
            logger.error("Can't get RemoteInterpreterEvent", e);
            waitQuietly();
            continue;
        } finally {
            interpreterProcess.releaseClient(client, broken);
        }
        Gson gson = new Gson();
        AngularObjectRegistry angularObjectRegistry = interpreterGroup.getAngularObjectRegistry();
        try {
            if (event.getType() == RemoteInterpreterEventType.NO_OP) {
                continue;
            } else if (event.getType() == RemoteInterpreterEventType.ANGULAR_OBJECT_ADD) {
                AngularObject angularObject = gson.fromJson(event.getData(), AngularObject.class);
                angularObjectRegistry.add(angularObject.getName(), angularObject.get(), angularObject.getNoteId(), angularObject.getParagraphId());
            } else if (event.getType() == RemoteInterpreterEventType.ANGULAR_OBJECT_UPDATE) {
                AngularObject angularObject = gson.fromJson(event.getData(), AngularObject.class);
                AngularObject localAngularObject = angularObjectRegistry.get(angularObject.getName(), angularObject.getNoteId(), angularObject.getParagraphId());
                if (localAngularObject instanceof RemoteAngularObject) {
                    // to avoid ping-pong loop
                    ((RemoteAngularObject) localAngularObject).set(angularObject.get(), true, false);
                } else {
                    localAngularObject.set(angularObject.get());
                }
            } else if (event.getType() == RemoteInterpreterEventType.ANGULAR_OBJECT_REMOVE) {
                AngularObject angularObject = gson.fromJson(event.getData(), AngularObject.class);
                angularObjectRegistry.remove(angularObject.getName(), angularObject.getNoteId(), angularObject.getParagraphId());
            } else if (event.getType() == RemoteInterpreterEventType.RUN_INTERPRETER_CONTEXT_RUNNER) {
                InterpreterContextRunner runnerFromRemote = gson.fromJson(event.getData(), RemoteInterpreterContextRunner.class);
                listener.onRemoteRunParagraph(runnerFromRemote.getNoteId(), runnerFromRemote.getParagraphId());
            } else if (event.getType() == RemoteInterpreterEventType.RESOURCE_POOL_GET_ALL) {
                ResourceSet resourceSet = getAllResourcePoolExcept();
                sendResourcePoolResponseGetAll(resourceSet);
            } else if (event.getType() == RemoteInterpreterEventType.RESOURCE_GET) {
                String resourceIdString = event.getData();
                ResourceId resourceId = gson.fromJson(resourceIdString, ResourceId.class);
                logger.debug("RESOURCE_GET {} {}", resourceId.getResourcePoolId(), resourceId.getName());
                Object o = getResource(resourceId);
                sendResourceResponseGet(resourceId, o);
            } else if (event.getType() == RemoteInterpreterEventType.OUTPUT_APPEND) {
                // on output append
                Map<String, String> outputAppend = gson.fromJson(event.getData(), new TypeToken<Map<String, Object>>() {
                }.getType());
                String noteId = (String) outputAppend.get("noteId");
                String paragraphId = (String) outputAppend.get("paragraphId");
                int index = Integer.parseInt(outputAppend.get("index"));
                String outputToAppend = (String) outputAppend.get("data");
                String appId = (String) outputAppend.get("appId");
                if (appId == null) {
                    runner.appendBuffer(noteId, paragraphId, index, outputToAppend);
                } else {
                    appListener.onOutputAppend(noteId, paragraphId, index, appId, outputToAppend);
                }
            } else if (event.getType() == RemoteInterpreterEventType.OUTPUT_UPDATE_ALL) {
                Map<String, Object> outputUpdate = gson.fromJson(event.getData(), new TypeToken<Map<String, Object>>() {
                }.getType());
                String noteId = (String) outputUpdate.get("noteId");
                String paragraphId = (String) outputUpdate.get("paragraphId");
                // clear the output
                listener.onOutputClear(noteId, paragraphId);
                List<Map<String, String>> messages = (List<Map<String, String>>) outputUpdate.get("messages");
                if (messages != null) {
                    for (int i = 0; i < messages.size(); i++) {
                        Map<String, String> m = messages.get(i);
                        InterpreterResult.Type type = InterpreterResult.Type.valueOf((String) m.get("type"));
                        String outputToUpdate = (String) m.get("data");
                        listener.onOutputUpdated(noteId, paragraphId, i, type, outputToUpdate);
                    }
                }
            } else if (event.getType() == RemoteInterpreterEventType.OUTPUT_UPDATE) {
                // on output update
                Map<String, String> outputAppend = gson.fromJson(event.getData(), new TypeToken<Map<String, Object>>() {
                }.getType());
                String noteId = (String) outputAppend.get("noteId");
                String paragraphId = (String) outputAppend.get("paragraphId");
                int index = Integer.parseInt(outputAppend.get("index"));
                InterpreterResult.Type type = InterpreterResult.Type.valueOf((String) outputAppend.get("type"));
                String outputToUpdate = (String) outputAppend.get("data");
                String appId = (String) outputAppend.get("appId");
                if (appId == null) {
                    listener.onOutputUpdated(noteId, paragraphId, index, type, outputToUpdate);
                } else {
                    appListener.onOutputUpdated(noteId, paragraphId, index, appId, type, outputToUpdate);
                }
            } else if (event.getType() == RemoteInterpreterEventType.APP_STATUS_UPDATE) {
                // on output update
                Map<String, String> appStatusUpdate = gson.fromJson(event.getData(), new TypeToken<Map<String, String>>() {
                }.getType());
                String noteId = appStatusUpdate.get("noteId");
                String paragraphId = appStatusUpdate.get("paragraphId");
                String appId = appStatusUpdate.get("appId");
                String status = appStatusUpdate.get("status");
                appListener.onStatusChange(noteId, paragraphId, appId, status);
            } else if (event.getType() == RemoteInterpreterEventType.REMOTE_ZEPPELIN_SERVER_RESOURCE) {
                RemoteZeppelinServerResource reqResourceBody = gson.fromJson(event.getData(), RemoteZeppelinServerResource.class);
                progressRemoteZeppelinControlEvent(reqResourceBody.getResourceType(), listener, reqResourceBody);
            } else if (event.getType() == RemoteInterpreterEventType.META_INFOS) {
                Map<String, String> metaInfos = gson.fromJson(event.getData(), new TypeToken<Map<String, String>>() {
                }.getType());
                String id = interpreterGroup.getId();
                int indexOfColon = id.indexOf(":");
                String settingId = id.substring(0, indexOfColon);
                listener.onMetaInfosReceived(settingId, metaInfos);
            }
            logger.debug("Event from remote process {}", event.getType());
        } catch (Exception e) {
            logger.error("Can't handle event " + event, e);
        }
    }
    if (appendFuture != null) {
        appendFuture.cancel(true);
    }
}
Also used : TException(org.apache.thrift.TException) InterpreterContextRunner(org.apache.zeppelin.interpreter.InterpreterContextRunner) Gson(com.google.gson.Gson) LinkedList(java.util.LinkedList) List(java.util.List) Client(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterService.Client) RemoteZeppelinServerResource(org.apache.zeppelin.interpreter.RemoteZeppelinServerResource) RemoteInterpreterEvent(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterEvent) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) AngularObject(org.apache.zeppelin.display.AngularObject) ResourceSet(org.apache.zeppelin.resource.ResourceSet) TException(org.apache.thrift.TException) RemoteInterpreterEventType(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterEventType) ResourceId(org.apache.zeppelin.resource.ResourceId) TypeToken(com.google.gson.reflect.TypeToken) AngularObject(org.apache.zeppelin.display.AngularObject) Map(java.util.Map) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry)

Example 37 with AngularObject

use of org.apache.zeppelin.display.AngularObject in project SSM by Intel-bigdata.

the class RemoteInterpreterTest method should_push_local_angular_repo_to_remote.

@Test
public void should_push_local_angular_repo_to_remote() throws Exception {
    // Given
    final Client client = Mockito.mock(Client.class);
    final RemoteInterpreter intr = new RemoteInterpreter(new Properties(), "noteId", MockInterpreterA.class.getName(), "runner", "path", "localRepo", env, 10 * 1000, null, null, "anonymous", false);
    final AngularObjectRegistry registry = new AngularObjectRegistry("spark", null);
    registry.add("name", "DuyHai DOAN", "nodeId", "paragraphId");
    final InterpreterGroup interpreterGroup = new InterpreterGroup("groupId");
    interpreterGroup.setAngularObjectRegistry(registry);
    intr.setInterpreterGroup(interpreterGroup);
    final java.lang.reflect.Type registryType = new TypeToken<Map<String, Map<String, AngularObject>>>() {
    }.getType();
    final Gson gson = new Gson();
    final String expected = gson.toJson(registry.getRegistry(), registryType);
    // When
    intr.pushAngularObjectRegistryToRemote(client);
    // Then
    Mockito.verify(client).angularRegistryPush(expected);
}
Also used : Gson(com.google.gson.Gson) AngularObject(org.apache.zeppelin.display.AngularObject) Properties(java.util.Properties) Client(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterService.Client) MockInterpreterA(org.apache.zeppelin.interpreter.remote.mock.MockInterpreterA) HashMap(java.util.HashMap) Map(java.util.Map) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) Test(org.junit.Test)

Example 38 with AngularObject

use of org.apache.zeppelin.display.AngularObject in project SSM by Intel-bigdata.

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

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