Search in sources :

Example 1 with RemoteInterpreterContext

use of org.apache.zeppelin.interpreter.thrift.RemoteInterpreterContext in project zeppelin by apache.

the class RemoteInterpreterServer method interpret.

@Override
public RemoteInterpreterResult interpret(String sessionId, String className, String st, RemoteInterpreterContext interpreterContext) throws InterpreterRPCException, TException {
    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("st:\n{}", st);
        }
        lifecycleManager.onInterpreterUse(interpreterGroupId);
        Interpreter intp = getInterpreter(sessionId, className);
        InterpreterContext context = convert(interpreterContext);
        context.setInterpreterClassName(intp.getClassName());
        InterpretJob interpretJob = null;
        boolean isRecover = Boolean.parseBoolean(context.getLocalProperties().getOrDefault("isRecover", "false"));
        if (isRecover) {
            LOGGER.info("Recovering paragraph: {} of note: {}", context.getParagraphId(), context.getNoteId());
            interpretJob = runningJobs.get(context.getParagraphId());
            if (interpretJob == null) {
                InterpreterResult result = new InterpreterResult(Code.ERROR, "Job is finished, unable to recover it");
                return convert(result, context.getConfig(), context.getGui(), context.getNoteGui());
            }
        } else {
            Scheduler scheduler = intp.getScheduler();
            InterpretJobListener jobListener = new InterpretJobListener();
            interpretJob = new InterpretJob(context.getParagraphId(), "RemoteInterpretJob_" + System.currentTimeMillis(), jobListener, intp, st, context);
            runningJobs.put(context.getParagraphId(), interpretJob);
            scheduler.submit(interpretJob);
        }
        while (!interpretJob.isTerminated()) {
            JobListener jobListener = interpretJob.getListener();
            synchronized (jobListener) {
                try {
                    jobListener.wait(1000);
                } catch (InterruptedException e) {
                    LOGGER.info("Exception in RemoteInterpreterServer while interpret, jobListener.wait", e);
                }
            }
        }
        progressMap.remove(context.getParagraphId());
        resultCleanService.schedule(() -> {
            runningJobs.remove(context.getParagraphId());
        }, resultCacheInSeconds, TimeUnit.SECONDS);
        InterpreterResult result = interpretJob.getReturn();
        // in case of job abort in PENDING status, result can be null
        if (result == null) {
            result = new InterpreterResult(Code.KEEP_PREVIOUS_RESULT);
        }
        return convert(result, context.getConfig(), context.getGui(), context.getNoteGui());
    } catch (Exception e) {
        LOGGER.error("Internal error when interpret code", e);
        throw new InterpreterRPCException(e.toString());
    }
}
Also used : InterpreterRPCException(org.apache.zeppelin.interpreter.thrift.InterpreterRPCException) Interpreter(org.apache.zeppelin.interpreter.Interpreter) LazyOpenInterpreter(org.apache.zeppelin.interpreter.LazyOpenInterpreter) Scheduler(org.apache.zeppelin.scheduler.Scheduler) RemoteInterpreterResult(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterResult) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) RemoteInterpreterContext(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterContext) JobListener(org.apache.zeppelin.scheduler.JobListener) TTransportException(org.apache.thrift.transport.TTransportException) InterpreterRPCException(org.apache.zeppelin.interpreter.thrift.InterpreterRPCException) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) ApplicationException(org.apache.zeppelin.helium.ApplicationException) TException(org.apache.thrift.TException) IOException(java.io.IOException)

Example 2 with RemoteInterpreterContext

use of org.apache.zeppelin.interpreter.thrift.RemoteInterpreterContext in project zeppelin by apache.

the class RemoteInterpreterServerTest method testInterpreter.

@Test
public void testInterpreter() throws Exception {
    final RemoteInterpreterServer server = new RemoteInterpreterServer("localhost", RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces(), ":", "groupId", true);
    server.init(new HashMap<>());
    server.intpEventClient = mock(RemoteInterpreterEventClient.class);
    Map<String, String> intpProperties = new HashMap<>();
    intpProperties.put("property_1", "value_1");
    intpProperties.put("zeppelin.interpreter.localRepo", "/tmp");
    // create Test1Interpreter in session_1
    server.createInterpreter("group_1", "session_1", Test1Interpreter.class.getName(), intpProperties, "user_1");
    Test1Interpreter interpreter1 = (Test1Interpreter) ((LazyOpenInterpreter) server.getInterpreterGroup().get("session_1").get(0)).getInnerInterpreter();
    assertEquals(1, server.getInterpreterGroup().getSessionNum());
    assertEquals(1, server.getInterpreterGroup().get("session_1").size());
    assertEquals(2, interpreter1.getProperties().size());
    assertEquals("value_1", interpreter1.getProperty("property_1"));
    // create Test2Interpreter in session_1
    server.createInterpreter("group_1", "session_1", Test1Interpreter.class.getName(), intpProperties, "user_1");
    assertEquals(2, server.getInterpreterGroup().get("session_1").size());
    // create Test1Interpreter in session_2
    server.createInterpreter("group_1", "session_2", Test1Interpreter.class.getName(), intpProperties, "user_1");
    assertEquals(2, server.getInterpreterGroup().getSessionNum());
    assertEquals(2, server.getInterpreterGroup().get("session_1").size());
    assertEquals(1, server.getInterpreterGroup().get("session_2").size());
    final RemoteInterpreterContext intpContext = new RemoteInterpreterContext();
    intpContext.setNoteId("note_1");
    intpContext.setParagraphId("paragraph_1");
    intpContext.setGui("{}");
    intpContext.setNoteGui("{}");
    intpContext.setLocalProperties(new HashMap<>());
    // single output of SUCCESS
    RemoteInterpreterResult result = server.interpret("session_1", Test1Interpreter.class.getName(), "SINGLE_OUTPUT_SUCCESS", intpContext);
    assertEquals("SUCCESS", result.code);
    assertEquals(1, result.getMsg().size());
    assertEquals("SINGLE_OUTPUT_SUCCESS", result.getMsg().get(0).getData());
    // combo output of SUCCESS
    result = server.interpret("session_1", Test1Interpreter.class.getName(), "COMBO_OUTPUT_SUCCESS", intpContext);
    assertEquals("SUCCESS", result.code);
    assertEquals(2, result.getMsg().size());
    assertEquals("INTERPRETER_OUT", result.getMsg().get(0).getData());
    assertEquals("SINGLE_OUTPUT_SUCCESS", result.getMsg().get(1).getData());
    // single output of ERROR
    result = server.interpret("session_1", Test1Interpreter.class.getName(), "SINGLE_OUTPUT_ERROR", intpContext);
    assertEquals("ERROR", result.code);
    assertEquals(1, result.getMsg().size());
    assertEquals("SINGLE_OUTPUT_ERROR", result.getMsg().get(0).getData());
    // getFormType
    String formType = server.getFormType("session_1", Test1Interpreter.class.getName());
    assertEquals("NATIVE", formType);
    // cancel
    Thread sleepThread = new Thread() {

        @Override
        public void run() {
            try {
                server.interpret("session_1", Test1Interpreter.class.getName(), "SLEEP", intpContext);
            } catch (TException e) {
                e.printStackTrace();
            }
        }
    };
    sleepThread.start();
    Thread.sleep(1000);
    assertFalse(interpreter1.cancelled.get());
    server.cancel("session_1", Test1Interpreter.class.getName(), intpContext);
    // Sleep 1 second, because cancel is async.
    Thread.sleep(1000);
    assertTrue(interpreter1.cancelled.get());
    // getProgress
    assertEquals(10, server.getProgress("session_1", Test1Interpreter.class.getName(), intpContext));
    // close
    server.close("session_1", Test1Interpreter.class.getName());
    assertTrue(interpreter1.closed.get());
}
Also used : RemoteInterpreterContext(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterContext) TException(org.apache.thrift.TException) RemoteInterpreterResult(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterResult) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

TException (org.apache.thrift.TException)2 RemoteInterpreterContext (org.apache.zeppelin.interpreter.thrift.RemoteInterpreterContext)2 RemoteInterpreterResult (org.apache.zeppelin.interpreter.thrift.RemoteInterpreterResult)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 TTransportException (org.apache.thrift.transport.TTransportException)1 ApplicationException (org.apache.zeppelin.helium.ApplicationException)1 Interpreter (org.apache.zeppelin.interpreter.Interpreter)1 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)1 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1 LazyOpenInterpreter (org.apache.zeppelin.interpreter.LazyOpenInterpreter)1 InterpreterRPCException (org.apache.zeppelin.interpreter.thrift.InterpreterRPCException)1 JobListener (org.apache.zeppelin.scheduler.JobListener)1 Scheduler (org.apache.zeppelin.scheduler.Scheduler)1 Test (org.junit.Test)1