Search in sources :

Example 1 with Code

use of org.apache.zeppelin.interpreter.InterpreterResult.Code in project zeppelin by apache.

the class ShellInterpreter method interpret.

@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
    LOGGER.debug("Run shell command '" + cmd + "'");
    OutputStream outStream = new ByteArrayOutputStream();
    CommandLine cmdLine = CommandLine.parse(shell);
    // they need to be delimited by '&&' instead
    if (isWindows) {
        String[] lines = StringUtils.split(cmd, "\n");
        cmd = StringUtils.join(lines, " && ");
    }
    cmdLine.addArgument(cmd, false);
    try {
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(contextInterpreter.out, contextInterpreter.out));
        executor.setWatchdog(new ExecuteWatchdog(Long.valueOf(getProperty(TIMEOUT_PROPERTY))));
        executors.put(contextInterpreter.getParagraphId(), executor);
        int exitVal = executor.execute(cmdLine);
        LOGGER.info("Paragraph " + contextInterpreter.getParagraphId() + " return with exit value: " + exitVal);
        return new InterpreterResult(Code.SUCCESS, outStream.toString());
    } catch (ExecuteException e) {
        int exitValue = e.getExitValue();
        LOGGER.error("Can not run " + cmd, e);
        Code code = Code.ERROR;
        String message = outStream.toString();
        if (exitValue == 143) {
            code = Code.INCOMPLETE;
            message += "Paragraph received a SIGTERM\n";
            LOGGER.info("The paragraph " + contextInterpreter.getParagraphId() + " stopped executing: " + message);
        }
        message += "ExitValue: " + exitValue;
        return new InterpreterResult(code, message);
    } catch (IOException e) {
        LOGGER.error("Can not run " + cmd, e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    } finally {
        executors.remove(contextInterpreter.getParagraphId());
    }
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 2 with Code

use of org.apache.zeppelin.interpreter.InterpreterResult.Code in project zeppelin by apache.

the class FlinkInterpreter method interpret.

public InterpreterResult interpret(String[] lines, InterpreterContext context) {
    final IMain imain = flinkIloop.intp();
    String[] linesToRun = new String[lines.length + 1];
    for (int i = 0; i < lines.length; i++) {
        linesToRun[i] = lines[i];
    }
    linesToRun[lines.length] = "print(\"\")";
    System.setOut(new PrintStream(out));
    out.reset();
    Code r = null;
    String incomplete = "";
    boolean inComment = false;
    for (int l = 0; l < linesToRun.length; l++) {
        final String s = linesToRun[l];
        // check if next line starts with "." (but not ".." or "./") it is treated as an invocation
        if (l + 1 < linesToRun.length) {
            String nextLine = linesToRun[l + 1].trim();
            boolean continuation = false;
            if (nextLine.isEmpty() || // skip empty line or comment
            nextLine.startsWith("//") || nextLine.startsWith("}") || nextLine.startsWith("object")) {
                // include "} object" for Scala companion object
                continuation = true;
            } else if (!inComment && nextLine.startsWith("/*")) {
                inComment = true;
                continuation = true;
            } else if (inComment && nextLine.lastIndexOf("*/") >= 0) {
                inComment = false;
                continuation = true;
            } else if (nextLine.length() > 1 && nextLine.charAt(0) == '.' && // ".."
            nextLine.charAt(1) != '.' && nextLine.charAt(1) != '/') {
                // "./"
                continuation = true;
            } else if (inComment) {
                continuation = true;
            }
            if (continuation) {
                incomplete += s + "\n";
                continue;
            }
        }
        final String currentCommand = incomplete;
        scala.tools.nsc.interpreter.Results.Result res = null;
        try {
            res = Console.withOut(System.out, new AbstractFunction0<Results.Result>() {

                @Override
                public Results.Result apply() {
                    return imain.interpret(currentCommand + s);
                }
            });
        } catch (Exception e) {
            logger.info("Interpreter exception", e);
            return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e));
        }
        r = getResultCode(res);
        if (r == Code.ERROR) {
            return new InterpreterResult(r, out.toString());
        } else if (r == Code.INCOMPLETE) {
            incomplete += s + "\n";
        } else {
            incomplete = "";
        }
    }
    if (r == Code.INCOMPLETE) {
        return new InterpreterResult(r, "Incomplete expression");
    } else {
        return new InterpreterResult(r, out.toString());
    }
}
Also used : PrintStream(java.io.PrintStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) IMain(scala.tools.nsc.interpreter.IMain) AbstractFunction0(scala.runtime.AbstractFunction0) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) InvocationTargetException(java.lang.reflect.InvocationTargetException) Results(scala.tools.nsc.interpreter.Results)

Example 3 with Code

use of org.apache.zeppelin.interpreter.InterpreterResult.Code in project zeppelin by apache.

the class ScaldingInterpreter method interpretInput.

public InterpreterResult interpretInput(String[] lines) {
    // add print("") to make sure not finishing with comment
    // see https://github.com/NFLabs/zeppelin/issues/151
    String[] linesToRun = new String[lines.length + 1];
    for (int i = 0; i < lines.length; i++) {
        linesToRun[i] = lines[i];
    }
    linesToRun[lines.length] = "print(\"\")";
    out.reset();
    // Moving two lines below from open() to this function.
    // If they are in open output is incomplete.
    PrintStream printStream = new PrintStream(out, true);
    Console.setOut(printStream);
    Code r = null;
    String incomplete = "";
    boolean inComment = false;
    for (int l = 0; l < linesToRun.length; l++) {
        String s = linesToRun[l];
        // check if next line starts with "." (but not ".." or "./") it is treated as an invocation
        if (l + 1 < linesToRun.length) {
            String nextLine = linesToRun[l + 1].trim();
            boolean continuation = false;
            if (nextLine.isEmpty() || // skip empty line or comment
            nextLine.startsWith("//") || nextLine.startsWith("}") || nextLine.startsWith("object")) {
                // include "} object" for Scala companion object
                continuation = true;
            } else if (!inComment && nextLine.startsWith("/*")) {
                inComment = true;
                continuation = true;
            } else if (inComment && nextLine.lastIndexOf("*/") >= 0) {
                inComment = false;
                continuation = true;
            } else if (nextLine.length() > 1 && nextLine.charAt(0) == '.' && // ".."
            nextLine.charAt(1) != '.' && nextLine.charAt(1) != '/') {
                // "./"
                continuation = true;
            } else if (inComment) {
                continuation = true;
            }
            if (continuation) {
                incomplete += s + "\n";
                continue;
            }
        }
        scala.tools.nsc.interpreter.Results.Result res = null;
        try {
            res = interpreter.intp().interpret(incomplete + s);
        } catch (Exception e) {
            LOGGER.error("Interpreter exception: ", e);
            return new InterpreterResult(Code.ERROR, e.getMessage());
        }
        r = getResultCode(res);
        if (r == Code.ERROR) {
            Console.flush();
            return new InterpreterResult(r, out.toString());
        } else if (r == Code.INCOMPLETE) {
            incomplete += s + "\n";
        } else {
            incomplete = "";
        }
    }
    if (r == Code.INCOMPLETE) {
        return new InterpreterResult(r, "Incomplete expression");
    } else {
        Console.flush();
        return new InterpreterResult(r, out.toString());
    }
}
Also used : PrintStream(java.io.PrintStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) IOException(java.io.IOException)

Example 4 with Code

use of org.apache.zeppelin.interpreter.InterpreterResult.Code in project zeppelin by apache.

the class NotebookServiceTest method testParagraphOperations.

@Test
public void testParagraphOperations() throws IOException {
    // create note
    String note1Id = notebookService.createNote("note1", "python", false, context, callback);
    notebook.processNote(note1Id, note1 -> {
        assertEquals("note1", note1.getName());
        assertEquals(0, note1.getParagraphCount());
        verify(callback).onSuccess(note1, context);
        return null;
    });
    // add paragraph
    reset(callback);
    Paragraph p = notebookService.insertParagraph(note1Id, 0, new HashMap<>(), context, callback);
    assertNotNull(p);
    verify(callback).onSuccess(p, context);
    notebook.processNote(note1Id, note1 -> {
        assertEquals(1, note1.getParagraphCount());
        return null;
    });
    // update paragraph
    reset(callback);
    notebookService.updateParagraph(note1Id, p.getId(), "my_title", "my_text", new HashMap<>(), new HashMap<>(), context, callback);
    assertEquals("my_title", p.getTitle());
    assertEquals("my_text", p.getText());
    // move paragraph
    reset(callback);
    notebookService.moveParagraph(note1Id, p.getId(), 0, context, callback);
    notebook.processNote(note1Id, note1 -> {
        assertEquals(p, note1.getParagraph(0));
        verify(callback).onSuccess(p, context);
        return null;
    });
    // run paragraph asynchronously
    reset(callback);
    p.getConfig().put("colWidth", "6.0");
    p.getConfig().put("title", true);
    boolean runStatus = notebook.processNote(note1Id, note1 -> {
        return notebookService.runParagraph(note1, p.getId(), "my_title", "1+1", new HashMap<>(), new HashMap<>(), null, false, false, context, callback);
    });
    assertTrue(runStatus);
    verify(callback).onSuccess(p, context);
    assertEquals(2, p.getConfig().size());
    // run paragraph synchronously via correct code
    reset(callback);
    runStatus = notebook.processNote(note1Id, note1 -> {
        return notebookService.runParagraph(note1, p.getId(), "my_title", "1+1", new HashMap<>(), new HashMap<>(), null, false, true, context, callback);
    });
    assertTrue(runStatus);
    verify(callback).onSuccess(p, context);
    assertEquals(2, p.getConfig().size());
    // run all paragraphs, with null paragraph list provided
    reset(callback);
    assertTrue(notebookService.runAllParagraphs(note1Id, null, context, callback));
    reset(callback);
    runStatus = notebook.processNote(note1Id, note1 -> {
        return notebookService.runParagraph(note1, p.getId(), "my_title", "invalid_code", new HashMap<>(), new HashMap<>(), null, false, true, context, callback);
    });
    assertTrue(runStatus);
    // TODO(zjffdu) Enable it after ZEPPELIN-3699
    // assertNotNull(p.getResult());
    verify(callback).onSuccess(p, context);
    // clean output
    reset(callback);
    notebookService.clearParagraphOutput(note1Id, p.getId(), context, callback);
    assertNull(p.getReturn());
    verify(callback).onSuccess(p, context);
}
Also used : ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) StringUtils(org.apache.commons.lang3.StringUtils) Matchers.eq(org.mockito.Matchers.eq) Gson(com.google.gson.Gson) After(org.junit.After) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) AuthorizationService(org.apache.zeppelin.notebook.AuthorizationService) Assert.fail(org.junit.Assert.fail) Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterSettingManager(org.apache.zeppelin.interpreter.InterpreterSettingManager) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) NoteInfo(org.apache.zeppelin.notebook.NoteInfo) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) Collectors(java.util.stream.Collectors) Matchers.any(org.mockito.Matchers.any) List(java.util.List) NoteManager(org.apache.zeppelin.notebook.NoteManager) LuceneSearch(org.apache.zeppelin.search.LuceneSearch) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) Mockito.doCallRealMethod(org.mockito.Mockito.doCallRealMethod) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) HashMap(java.util.HashMap) HashSet(java.util.HashSet) QuartzSchedulerService(org.apache.zeppelin.notebook.scheduler.QuartzSchedulerService) NotebookRepo(org.apache.zeppelin.notebook.repo.NotebookRepo) ArgumentCaptor(org.mockito.ArgumentCaptor) InterpreterFactory(org.apache.zeppelin.interpreter.InterpreterFactory) Before(org.junit.Before) Paragraph(org.apache.zeppelin.notebook.Paragraph) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) FileWriter(java.io.FileWriter) Note(org.apache.zeppelin.notebook.Note) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) NotePathAlreadyExistsException(org.apache.zeppelin.notebook.exception.NotePathAlreadyExistsException) Credentials(org.apache.zeppelin.user.Credentials) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Notebook(org.apache.zeppelin.notebook.Notebook) File(java.io.File) VFSNotebookRepo(org.apache.zeppelin.notebook.repo.VFSNotebookRepo) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) FormType(org.apache.zeppelin.interpreter.Interpreter.FormType) Assert.assertNull(org.junit.Assert.assertNull) Mockito.reset(org.mockito.Mockito.reset) SearchService(org.apache.zeppelin.search.SearchService) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 5 with Code

use of org.apache.zeppelin.interpreter.InterpreterResult.Code in project zeppelin by apache.

the class SparkInterpreter method interpretInput.

public InterpreterResult interpretInput(String[] lines, InterpreterContext context) {
    SparkEnv.set(env);
    String[] linesToRun = new String[lines.length];
    for (int i = 0; i < lines.length; i++) {
        linesToRun[i] = lines[i];
    }
    Console.setOut(context.out);
    out.setInterpreterOutput(context.out);
    context.out.clear();
    Code r = null;
    String incomplete = "";
    boolean inComment = false;
    for (int l = 0; l < linesToRun.length; l++) {
        String s = linesToRun[l];
        // check if next line starts with "." (but not ".." or "./") it is treated as an invocation
        if (l + 1 < linesToRun.length) {
            String nextLine = linesToRun[l + 1].trim();
            boolean continuation = false;
            if (nextLine.isEmpty() || // skip empty line or comment
            nextLine.startsWith("//") || nextLine.startsWith("}") || nextLine.startsWith("object")) {
                // include "} object" for Scala companion object
                continuation = true;
            } else if (!inComment && nextLine.startsWith("/*")) {
                inComment = true;
                continuation = true;
            } else if (inComment && nextLine.lastIndexOf("*/") >= 0) {
                inComment = false;
                continuation = true;
            } else if (nextLine.length() > 1 && nextLine.charAt(0) == '.' && // ".."
            nextLine.charAt(1) != '.' && nextLine.charAt(1) != '/') {
                // "./"
                continuation = true;
            } else if (inComment) {
                continuation = true;
            }
            if (continuation) {
                incomplete += s + "\n";
                continue;
            }
        }
        scala.tools.nsc.interpreter.Results.Result res = null;
        try {
            res = interpret(incomplete + s);
        } catch (Exception e) {
            sc.clearJobGroup();
            out.setInterpreterOutput(null);
            logger.info("Interpreter exception", e);
            return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e));
        }
        r = getResultCode(res);
        if (r == Code.ERROR) {
            sc.clearJobGroup();
            out.setInterpreterOutput(null);
            return new InterpreterResult(r, "");
        } else if (r == Code.INCOMPLETE) {
            incomplete += s + "\n";
        } else {
            incomplete = "";
        }
    }
    // make sure code does not finish with comment
    if (r == Code.INCOMPLETE) {
        scala.tools.nsc.interpreter.Results.Result res = null;
        res = interpret(incomplete + "\nprint(\"\")");
        r = getResultCode(res);
    }
    if (r == Code.INCOMPLETE) {
        sc.clearJobGroup();
        out.setInterpreterOutput(null);
        return new InterpreterResult(r, "Incomplete expression");
    } else {
        sc.clearJobGroup();
        putLatestVarInResourcePool(context);
        out.setInterpreterOutput(null);
        return new InterpreterResult(Code.SUCCESS);
    }
}
Also used : Results(scala.tools.nsc.interpreter.Results) scala(scala) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Aggregations

InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)9 Code (org.apache.zeppelin.interpreter.InterpreterResult.Code)9 IOException (java.io.IOException)6 File (java.io.File)3 PrintStream (java.io.PrintStream)3 CommandLine (org.apache.commons.exec.CommandLine)3 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)3 ExecuteException (org.apache.commons.exec.ExecuteException)3 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)3 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Results (scala.tools.nsc.interpreter.Results)2 Gson (com.google.gson.Gson)1 FileWriter (java.io.FileWriter)1 OutputStream (java.io.OutputStream)1 MalformedURLException (java.net.MalformedURLException)1 URLClassLoader (java.net.URLClassLoader)1 Files (java.nio.file.Files)1 HashMap (java.util.HashMap)1