Skip to content

Commit a69850b

Browse files
committed
[SPARK-24319][SPARK SUBMIT] Fix spark-submit execution where no main class is required.
1 parent aca65c6 commit a69850b

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class SparkSubmitCommandBuilder extends AbstractCommandBuilder {
9191
final List<String> userArgs;
9292
private final List<String> parsedArgs;
9393
private final boolean requiresAppResource;
94+
private final boolean requiresMainClass;
9495
private final boolean isExample;
9596

9697
/**
@@ -106,6 +107,7 @@ class SparkSubmitCommandBuilder extends AbstractCommandBuilder {
106107
*/
107108
SparkSubmitCommandBuilder() {
108109
this.requiresAppResource = true;
110+
this.requiresMainClass = false;
109111
this.isExample = false;
110112
this.parsedArgs = new ArrayList<>();
111113
this.userArgs = new ArrayList<>();
@@ -145,9 +147,11 @@ class SparkSubmitCommandBuilder extends AbstractCommandBuilder {
145147
OptionParser parser = new OptionParser(true);
146148
parser.parse(submitArgs);
147149
this.requiresAppResource = parser.requiresAppResource;
150+
this.requiresMainClass = parser.requiresMainClass;
148151
} else {
149152
this.isExample = isExample;
150153
this.requiresAppResource = false;
154+
this.requiresMainClass = false;
151155
}
152156
}
153157

@@ -229,7 +233,7 @@ List<String> buildSparkSubmitArgs() {
229233
args.add(join(",", pyFiles));
230234
}
231235

232-
if (isExample) {
236+
if (isExample && requiresMainClass) {
233237
checkArgument(mainClass != null, "Missing example class name.");
234238
}
235239

@@ -422,6 +426,7 @@ private List<String> findExamplesJars() {
422426
private class OptionParser extends SparkSubmitOptionParser {
423427

424428
boolean requiresAppResource = true;
429+
boolean requiresMainClass = true;
425430
private final boolean errorOnUnknownArgs;
426431

427432
OptionParser(boolean errorOnUnknownArgs) {
@@ -471,16 +476,19 @@ protected boolean handle(String opt, String value) {
471476
case KILL_SUBMISSION:
472477
case STATUS:
473478
requiresAppResource = false;
479+
requiresMainClass = false;
474480
parsedArgs.add(opt);
475481
parsedArgs.add(value);
476482
break;
477483
case HELP:
478484
case USAGE_ERROR:
479485
requiresAppResource = false;
486+
requiresMainClass = false;
480487
parsedArgs.add(opt);
481488
break;
482489
case VERSION:
483490
requiresAppResource = false;
491+
requiresMainClass = false;
484492
parsedArgs.add(opt);
485493
break;
486494
default:

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.spark.launcher;
1919

2020
import java.io.File;
21+
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.Collections;
2324
import java.util.HashMap;
@@ -74,8 +75,11 @@ public void testCliHelpAndNoArg() throws Exception {
7475

7576
@Test
7677
public void testCliKillAndStatus() throws Exception {
77-
testCLIOpts(parser.STATUS);
78-
testCLIOpts(parser.KILL_SUBMISSION);
78+
List<String> params = Arrays.asList("driver-20160531171222-0000");
79+
testCLIOpts(null, parser.STATUS, params);
80+
testCLIOpts(null, parser.KILL_SUBMISSION, params);
81+
testCLIOpts(SparkSubmitCommandBuilder.RUN_EXAMPLE, parser.STATUS, params);
82+
testCLIOpts(SparkSubmitCommandBuilder.RUN_EXAMPLE, parser.KILL_SUBMISSION, params);
7983
}
8084

8185
@Test
@@ -190,6 +194,13 @@ public void testSparkRShell() throws Exception {
190194
env.get("SPARKR_SUBMIT_ARGS"));
191195
}
192196

197+
@Test
198+
public void testExamplesRunnerNoMainClass() throws Exception {
199+
testCLIOpts(SparkSubmitCommandBuilder.RUN_EXAMPLE, parser.HELP, null);
200+
testCLIOpts(SparkSubmitCommandBuilder.RUN_EXAMPLE, parser.USAGE_ERROR, null);
201+
testCLIOpts(SparkSubmitCommandBuilder.RUN_EXAMPLE, parser.VERSION, null);
202+
}
203+
193204
@Test
194205
public void testExamplesRunner() throws Exception {
195206
List<String> sparkSubmitArgs = Arrays.asList(
@@ -344,8 +355,15 @@ private List<String> buildCommand(List<String> args, Map<String, String> env) th
344355
return newCommandBuilder(args).buildCommand(env);
345356
}
346357

347-
private void testCLIOpts(String opt) throws Exception {
348-
List<String> helpArgs = Arrays.asList(opt, "driver-20160531171222-0000");
358+
private void testCLIOpts(String appResource, String opt, List<String> params) throws Exception {
359+
List<String> helpArgs = new ArrayList<>();
360+
if (appResource != null) {
361+
helpArgs.add(appResource);
362+
}
363+
helpArgs.add(opt);
364+
if (params != null) {
365+
helpArgs.addAll(params);
366+
}
349367
Map<String, String> env = new HashMap<>();
350368
List<String> cmd = buildCommand(helpArgs, env);
351369
assertTrue(opt + " should be contained in the final cmd.",

0 commit comments

Comments
 (0)