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