Skip to content

Commit b03e3de

Browse files
committed
Move buildCommand part under exception handling
1 parent 12a5145 commit b03e3de

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.spark.launcher;
1919

20+
import java.io.IOException;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.HashMap;
@@ -54,10 +55,12 @@ public static void main(String[] argsArray) throws Exception {
5455
String className = args.remove(0);
5556

5657
boolean printLaunchCommand = !isEmpty(System.getenv("SPARK_PRINT_LAUNCH_COMMAND"));
57-
AbstractCommandBuilder builder;
58+
Map<String, String> env = new HashMap<>();
59+
List<String> cmd;
5860
if (className.equals("org.apache.spark.deploy.SparkSubmit")) {
5961
try {
60-
builder = new SparkSubmitCommandBuilder(args);
62+
AbstractCommandBuilder builder = new SparkSubmitCommandBuilder(args);
63+
cmd = buildCommand(builder, env, printLaunchCommand);
6164
} catch (IllegalArgumentException e) {
6265
printLaunchCommand = false;
6366
System.err.println("Error: " + e.getMessage());
@@ -76,17 +79,12 @@ public static void main(String[] argsArray) throws Exception {
7679
help.add(parser.className);
7780
}
7881
help.add(parser.USAGE_ERROR);
79-
builder = new SparkSubmitCommandBuilder(help);
82+
AbstractCommandBuilder builder = new SparkSubmitCommandBuilder(help);
83+
cmd = buildCommand(builder, env, printLaunchCommand);
8084
}
8185
} else {
82-
builder = new SparkClassCommandBuilder(className, args);
83-
}
84-
85-
Map<String, String> env = new HashMap<>();
86-
List<String> cmd = builder.buildCommand(env);
87-
if (printLaunchCommand) {
88-
System.err.println("Spark Command: " + join(" ", cmd));
89-
System.err.println("========================================");
86+
AbstractCommandBuilder builder = new SparkClassCommandBuilder(className, args);
87+
cmd = buildCommand(builder, env, printLaunchCommand);
9088
}
9189

9290
if (isWindows()) {
@@ -101,6 +99,22 @@ public static void main(String[] argsArray) throws Exception {
10199
}
102100
}
103101

102+
/**
103+
* Prepare spark commands with the appropriate command builder.
104+
* If printLaunchCommand is set then the commands will be printed to the stderr.
105+
*/
106+
private static List<String> buildCommand(AbstractCommandBuilder builder,
107+
Map<String, String> env,
108+
boolean printLaunchCommand)
109+
throws IOException, IllegalArgumentException {
110+
List<String> cmd = builder.buildCommand(env);
111+
if (printLaunchCommand) {
112+
System.err.println("Spark Command: " + join(" ", cmd));
113+
System.err.println("========================================");
114+
}
115+
return cmd;
116+
}
117+
104118
/**
105119
* Prepare a command line for execution from a Windows batch script.
106120
*

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,8 @@ class SparkSubmitCommandBuilder extends AbstractCommandBuilder {
139139

140140
case RUN_EXAMPLE:
141141
isExample = true;
142+
appResource = SparkLauncher.NO_RESOURCE;
142143
submitArgs = args.subList(1, args.size());
143-
if (submitArgs.isEmpty()) {
144-
throw new IllegalArgumentException("Missing argument");
145-
}
146144
}
147145

148146
this.isExample = isExample;
@@ -151,7 +149,7 @@ class SparkSubmitCommandBuilder extends AbstractCommandBuilder {
151149
parser.parse(submitArgs);
152150
this.isSpecialCommand = parser.isSpecialCommand;
153151
} else {
154-
this.isSpecialCommand = true;
152+
this.isSpecialCommand = false;
155153
}
156154
} else {
157155
this.isExample = isExample;

launcher/src/test/java/org/apache/spark/launcher/SparkSubmitCommandBuilderSuite.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828

2929
import org.junit.AfterClass;
3030
import org.junit.BeforeClass;
31+
import org.junit.Rule;
3132
import org.junit.Test;
33+
import org.junit.rules.ExpectedException;
34+
3235
import static org.junit.Assert.*;
3336

3437
public class SparkSubmitCommandBuilderSuite extends BaseSuite {
@@ -208,6 +211,22 @@ public void testExamplesRunnerNoMainClass() throws Exception {
208211
testCLIOpts(SparkSubmitCommandBuilder.RUN_EXAMPLE, parser.VERSION, null);
209212
}
210213

214+
@Rule
215+
public ExpectedException testExamplesRunnerWithMasterNoMainClassEx = ExpectedException.none();
216+
217+
@Test
218+
public void testExamplesRunnerWithMasterNoMainClass() throws Exception {
219+
testExamplesRunnerWithMasterNoMainClassEx.expect(IllegalArgumentException.class);
220+
testExamplesRunnerWithMasterNoMainClassEx.expectMessage("Missing example class name.");
221+
222+
List<String> sparkSubmitArgs = Arrays.asList(
223+
SparkSubmitCommandBuilder.RUN_EXAMPLE,
224+
parser.MASTER + "=foo"
225+
);
226+
Map<String, String> env = new HashMap<>();
227+
buildCommand(sparkSubmitArgs, env);
228+
}
229+
211230
@Test
212231
public void testExamplesRunner() throws Exception {
213232
List<String> sparkSubmitArgs = Arrays.asList(

0 commit comments

Comments
 (0)