Skip to content

Commit 525ef5b

Browse files
author
Marcelo Vanzin
committed
Rework Unix spark-class to handle argument with newlines.
1 parent 8ac4e92 commit 525ef5b

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

bin/spark-class

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ fi
6767

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

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

7677
if [ "${CMD[0]}" = "usage" ]; then
7778
"${CMD[@]}"

launcher/src/main/java/org/apache/spark/launcher/AbstractLauncher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ private List<String> prepareForWindows(
427427
List<String> cmd,
428428
String libPath,
429429
Map<String, String> env) {
430-
StringBuilder cmdline = new StringBuilder("cmd /c \"");
430+
StringBuilder cmdline = new StringBuilder("\"");
431431
if (libPath != null) {
432432
cmdline.append("set PATH=%PATH%;").append(libPath).append(" &&");
433433
}
@@ -445,7 +445,7 @@ private List<String> prepareForWindows(
445445
cmdline.append(quoteForBatchScript(arg));
446446
}
447447
cmdline.append("\"");
448-
return Arrays.asList(cmdline.toString());
448+
return Arrays.asList("cmd", "/c", cmdline.toString());
449449
}
450450

451451
/**

launcher/src/main/java/org/apache/spark/launcher/Main.java

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

69-
for (String c : cmd) {
70-
System.out.println(c);
72+
if (isWindows()) {
73+
String cmdLine = join(" ", cmd);
74+
System.out.println(cmdLine);
75+
} else {
76+
for (String c : cmd) {
77+
System.out.print(c);
78+
System.out.print('\0');
79+
}
7180
}
7281
}
7382

0 commit comments

Comments
 (0)