Skip to content

Commit

Permalink
Rework Unix spark-class to handle argument with newlines.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcelo Vanzin committed Jan 14, 2015
1 parent 8ac4e92 commit 525ef5b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
11 changes: 6 additions & 5 deletions bin/spark-class
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ fi

SPARK_LAUNCHER_CP="${SPARK_LAUNCHER_CP}${LAUNCHER_DIR}/${LAUNCHER_JARS}"

# The launcher library will print one argument per line of its output. The next line sets
# the new line char as the only delimiter used when parsing that output into an array.
IFS="
"
CMD=($($RUNNER -cp $SPARK_LAUNCHER_CP org.apache.spark.launcher.Main "$@"))
# The launcher library will print arguments separated by a NULL character. Read that in a while
# loop, populating an array that will be used to exec the final command.
CMD=()
while IFS= read -d '' -r ARG; do
CMD+=("$ARG")
done < <($RUNNER -cp "$SPARK_LAUNCHER_CP" org.apache.spark.launcher.Main "$@")

if [ "${CMD[0]}" = "usage" ]; then
"${CMD[@]}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ private List<String> prepareForWindows(
List<String> cmd,
String libPath,
Map<String, String> env) {
StringBuilder cmdline = new StringBuilder("cmd /c \"");
StringBuilder cmdline = new StringBuilder("\"");
if (libPath != null) {
cmdline.append("set PATH=%PATH%;").append(libPath).append(" &&");
}
Expand All @@ -445,7 +445,7 @@ private List<String> prepareForWindows(
cmdline.append(quoteForBatchScript(arg));
}
cmdline.append("\"");
return Arrays.asList(cmdline.toString());
return Arrays.asList("cmd", "/c", cmdline.toString());
}

/**
Expand Down
19 changes: 14 additions & 5 deletions launcher/src/main/java/org/apache/spark/launcher/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public class Main extends LauncherCommon {
* <li>"spark-class": if another class is provided, an internal Spark class is run.</li>
* </ul>
*
* The ultimate command will not be run in the same process. Instead, the command to be executed
* will be printed to stdout. On Unix systems, this will be one argument per line. On Windows
* systems, this will be a single line containing the command to be executed.
* This class works in tandem with the "bin/spark-class" script on Unix-like systems, and
* "bin/spark-class2.cmd" batch script on Windows to execute the final command.
* <p/>
* On Unix-like systems, the output is a list of command arguments, separated by the NULL
* character. On Windows, the output is single command line suitable for direct execution
* form the script.
*/
public static void main(String[] argsArray) throws Exception {
checkArgument(argsArray.length > 0, "Not enough arguments: missing class name.");
Expand Down Expand Up @@ -66,8 +69,14 @@ public static void main(String[] argsArray) throws Exception {
System.err.println("========================================");
}

for (String c : cmd) {
System.out.println(c);
if (isWindows()) {
String cmdLine = join(" ", cmd);
System.out.println(cmdLine);
} else {
for (String c : cmd) {
System.out.print(c);
System.out.print('\0');
}
}
}

Expand Down

0 comments on commit 525ef5b

Please sign in to comment.