-
Notifications
You must be signed in to change notification settings - Fork 2.8k
ZEPPELIN-3375: Make PySparkInterpreter extends PythonInterpreter #2919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,9 +31,10 @@ | |
|
|
||
| /** | ||
| * Conda support | ||
| * TODO(zjffdu) Add removing conda env | ||
| */ | ||
| public class PythonCondaInterpreter extends Interpreter { | ||
| Logger logger = LoggerFactory.getLogger(PythonCondaInterpreter.class); | ||
| private static Logger logger = LoggerFactory.getLogger(PythonCondaInterpreter.class); | ||
| public static final String ZEPPELIN_PYTHON = "zeppelin.python"; | ||
| public static final String CONDA_PYTHON_PATH = "/bin/python"; | ||
| public static final String DEFAULT_ZEPPELIN_PYTHON = "python"; | ||
|
|
@@ -145,33 +146,22 @@ private void changePythonEnvironment(String envName) | |
| } | ||
| } | ||
| setCurrentCondaEnvName(envName); | ||
| python.setPythonCommand(binPath); | ||
| python.setPythonExec(binPath); | ||
| } | ||
|
|
||
| private void restartPythonProcess() throws InterpreterException { | ||
| PythonInterpreter python = getPythonInterpreter(); | ||
| logger.debug("Restarting PythonInterpreter"); | ||
| Interpreter python = | ||
| getInterpreterInTheSameSessionByClassName(PythonInterpreter.class.getName()); | ||
| python.close(); | ||
| python.open(); | ||
| } | ||
|
|
||
| protected PythonInterpreter getPythonInterpreter() throws InterpreterException { | ||
| LazyOpenInterpreter lazy = null; | ||
| PythonInterpreter python = null; | ||
| Interpreter p = | ||
| getInterpreterInTheSameSessionByClassName(PythonInterpreter.class.getName()); | ||
|
|
||
| while (p instanceof WrappedInterpreter) { | ||
| if (p instanceof LazyOpenInterpreter) { | ||
| lazy = (LazyOpenInterpreter) p; | ||
| } | ||
| p = ((WrappedInterpreter) p).getInnerInterpreter(); | ||
| } | ||
| python = (PythonInterpreter) p; | ||
|
|
||
| if (lazy != null) { | ||
| lazy.open(); | ||
| } | ||
| return python; | ||
| return (PythonInterpreter) ((LazyOpenInterpreter)p).getInnerInterpreter(); | ||
| } | ||
|
|
||
| public static String runCondaCommandForTextOutput(String title, List<String> commands) | ||
|
|
@@ -392,27 +382,50 @@ public Scheduler getScheduler() { | |
|
|
||
| public static String runCommand(List<String> commands) | ||
| throws IOException, InterruptedException { | ||
| logger.info("Starting shell commands: " + StringUtils.join(commands, " ")); | ||
| Process process = Runtime.getRuntime().exec(commands.toArray(new String[0])); | ||
| StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream()); | ||
| StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream()); | ||
| errorGobbler.start(); | ||
| outputGobbler.start(); | ||
| if (process.waitFor() != 0) { | ||
| throw new IOException("Fail to run shell commands: " + StringUtils.join(commands, " ")); | ||
| } | ||
| logger.info("Complete shell commands: " + StringUtils.join(commands, " ")); | ||
| return outputGobbler.getOutput(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we had some launcher wrapper for something like this? |
||
| } | ||
|
|
||
| StringBuilder sb = new StringBuilder(); | ||
| private static class StreamGobbler extends Thread { | ||
| InputStream is; | ||
| StringBuilder output = new StringBuilder(); | ||
|
|
||
| ProcessBuilder builder = new ProcessBuilder(commands); | ||
| builder.redirectErrorStream(true); | ||
| Process process = builder.start(); | ||
| InputStream stdout = process.getInputStream(); | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); | ||
| String line; | ||
| while ((line = br.readLine()) != null) { | ||
| sb.append(line); | ||
| sb.append("\n"); | ||
| // reads everything from is until empty. | ||
| StreamGobbler(InputStream is) { | ||
| this.is = is; | ||
| } | ||
| int r = process.waitFor(); // Let the process finish. | ||
|
|
||
| if (r != 0) { | ||
| throw new RuntimeException("Failed to execute `" + | ||
| StringUtils.join(commands, " ") + "` exited with " + r); | ||
| public void run() { | ||
| try { | ||
| InputStreamReader isr = new InputStreamReader(is); | ||
| BufferedReader br = new BufferedReader(isr); | ||
| String line = null; | ||
| long startTime = System.currentTimeMillis(); | ||
| while ( (line = br.readLine()) != null) { | ||
| output.append(line + "\n"); | ||
| // logging per 5 seconds | ||
| if ((System.currentTimeMillis() - startTime) > 5000) { | ||
| logger.info(line); | ||
| startTime = System.currentTimeMillis(); | ||
| } | ||
| } | ||
| } catch (IOException ioe) { | ||
| ioe.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| return sb.toString(); | ||
| public String getOutput() { | ||
| return output.toString(); | ||
| } | ||
| } | ||
|
|
||
| public static String runCommand(String ... command) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, 2.3 is running with Py4J 0.10.6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this detect any mismatch here? check spark version or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is fine to use py4j 0.9.2 here for IPythonInterpreter, as for IPySparkInterpreter it would use the py4j of spark instead of py4j 0.9.2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why Spark doesn't ship py4j zip file version-agnostic? Filed https://issues.apache.org/jira/browse/SPARK-23965
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a strong reason to rename or make a link for Spark's Py4J within Spark. Also, to be clear, I think It's an orthogonal issue with the current change here, if I am not mistaken.