Skip to content

Commit 304333a

Browse files
author
Marcelo Vanzin
committed
Fix propagation of properties file arg.
Also add a test. And remove some hardcoded strings, using the constants defined in SparkSubmitOptionParser.
1 parent bb67b93 commit 304333a

File tree

4 files changed

+50
-26
lines changed

4 files changed

+50
-26
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,8 @@ private List<String> createSparkSubmitCommand(Map<String, String> env) throws IO
112112
final List<String> sparkSubmitArgs = new ArrayList<String>();
113113
final List<String> appArgs = new ArrayList<String>();
114114

115-
// This parser exists for two reasons:
116-
// - to expose the command line args constants, since they're not static
117-
// - to special-case the HELP command line argument, and allow it to be propagated to
118-
// the app being launched.
115+
// Parse the command line and special-case the HELP command line argument, allowing it to be
116+
// propagated to the app being launched.
119117
SparkSubmitOptionParser parser = new SparkSubmitOptionParser() {
120118

121119
@Override

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public SparkLauncher setSparkHome(String sparkHome) {
135135
*/
136136
public SparkLauncher setPropertiesFile(String path) {
137137
checkNotNull(path, "path");
138+
this.propertiesFile = path;
138139
return this;
139140
}
140141

@@ -504,53 +505,54 @@ SparkLauncher addSparkArgs(String... args) {
504505
// Visible for testing.
505506
List<String> buildSparkSubmitArgs() {
506507
List<String> args = new ArrayList<String>();
508+
SparkSubmitOptionParser parser = new SparkSubmitOptionParser();
507509

508510
if (verbose) {
509-
args.add("--verbose");
511+
args.add(parser.VERBOSE);
510512
}
511513

512514
if (master != null) {
513-
args.add("--master");
515+
args.add(parser.MASTER);
514516
args.add(master);
515517
}
516518

517519
if (deployMode != null) {
518-
args.add("--deploy-mode");
520+
args.add(parser.DEPLOY_MODE);
519521
args.add(deployMode);
520522
}
521523

522524
if (appName != null) {
523-
args.add("--name");
525+
args.add(parser.NAME);
524526
args.add(appName);
525527
}
526528

527529
for (Map.Entry<String, String> e : conf.entrySet()) {
528-
args.add("--conf");
530+
args.add(parser.CONF);
529531
args.add(String.format("%s=%s", e.getKey(), e.getValue()));
530532
}
531533

532534
if (propertiesFile != null) {
533-
args.add("--properties-file");
535+
args.add(parser.PROPERTIES_FILE);
534536
args.add(propertiesFile);
535537
}
536538

537539
if (!jars.isEmpty()) {
538-
args.add("--jars");
540+
args.add(parser.JARS);
539541
args.add(join(",", jars));
540542
}
541543

542544
if (!files.isEmpty()) {
543-
args.add("--files");
545+
args.add(parser.FILES);
544546
args.add(join(",", files));
545547
}
546548

547549
if (!pyFiles.isEmpty()) {
548-
args.add("--py-files");
550+
args.add(parser.PY_FILES);
549551
args.add(join(",", pyFiles));
550552
}
551553

552554
if (mainClass != null) {
553-
args.add("--class");
555+
args.add(parser.CLASS);
554556
args.add(mainClass);
555557
}
556558

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
* is a single list of options that needs to be maintained (well, sort of, but it makes it harder
3131
* to break things).
3232
*/
33-
abstract class SparkSubmitOptionParser {
33+
class SparkSubmitOptionParser {
3434

3535
// The following constants define the "main" name for the available options. They're defined
3636
// to avoid copy & paste of the raw strings where they're needed.
3737
//
3838
// The fields are not static so that they're exposed to Scala code that uses this class. See
39-
// SparkSubmitArguments.scala.
39+
// SparkSubmitArguments.scala. That is also why this class is not abstract - to allow code to
40+
// easily use these constants without having to create dummy implementations of this class.
4041
protected final String CLASS = "--class";
4142
protected final String CONF = "--conf";
4243
protected final String DEPLOY_MODE = "--deploy-mode";
@@ -186,15 +187,19 @@ protected final void parse(List<String> args) {
186187
* @param value The value. This will be <i>null</i> if the option does not take a value.
187188
* @return Whether to continue parsing the argument list.
188189
*/
189-
protected abstract boolean handle(String opt, String value);
190+
protected boolean handle(String opt, String value) {
191+
throw new UnsupportedOperationException();
192+
}
190193

191194
/**
192195
* Callback for when an unrecognized option is parsed.
193196
*
194197
* @param opt Unrecognized option from the command line.
195198
* @return Whether to continue parsing the argument list.
196199
*/
197-
protected abstract boolean handleUnknown(String opt);
200+
protected boolean handleUnknown(String opt) {
201+
throw new UnsupportedOperationException();
202+
}
198203

199204
/**
200205
* Callback for remaining command line arguments after either {@link #handle(String, String)} or
@@ -203,7 +208,9 @@ protected final void parse(List<String> args) {
203208
*
204209
* @param extra List of remaining arguments.
205210
*/
206-
protected abstract void handleExtraArgs(List<String> extra);
211+
protected void handleExtraArgs(List<String> extra) {
212+
throw new UnsupportedOperationException();
213+
}
207214

208215
private String findCliOption(String name, String[][] available) {
209216
for (String[] candidates : available) {

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.Map;
2727
import java.util.regex.Pattern;
2828

29+
import org.junit.AfterClass;
30+
import org.junit.BeforeClass;
2931
import org.junit.Test;
3032
import org.slf4j.Logger;
3133
import org.slf4j.LoggerFactory;
@@ -38,6 +40,18 @@ public class SparkLauncherSuite {
3840

3941
private static final Logger LOG = LoggerFactory.getLogger(SparkLauncherSuite.class);
4042

43+
private static File dummyPropsFile;
44+
45+
@BeforeClass
46+
public static void setUp() throws Exception {
47+
dummyPropsFile = File.createTempFile("spark", "properties");
48+
}
49+
50+
@AfterClass
51+
public static void cleanUp() throws Exception {
52+
dummyPropsFile.delete();
53+
}
54+
4155
@Test
4256
public void testDriverCmdBuilder() throws Exception {
4357
testCmdBuilder(true);
@@ -75,6 +89,7 @@ private void testCmdBuilder(boolean isDriver) throws Exception {
7589
.setAppResource("/foo")
7690
.setAppName("MyApp")
7791
.setMainClass("my.Class")
92+
.setPropertiesFile(dummyPropsFile.getAbsolutePath())
7893
.addAppArgs("foo", "bar")
7994
.setConf(SparkLauncher.DRIVER_MEMORY, "1g")
8095
.setConf(SparkLauncher.DRIVER_EXTRA_CLASSPATH, "/driver")
@@ -128,11 +143,13 @@ private void testCmdBuilder(boolean isDriver) throws Exception {
128143
}
129144

130145
// Checks below are the same for both driver and non-driver mode.
146+
SparkSubmitOptionParser parser = new SparkSubmitOptionParser();
131147

132-
assertEquals("yarn", findArgValue(cmd, "--master"));
133-
assertEquals(deployMode, findArgValue(cmd, "--deploy-mode"));
134-
assertEquals("my.Class", findArgValue(cmd, "--class"));
135-
assertEquals("MyApp", findArgValue(cmd, "--name"));
148+
assertEquals(dummyPropsFile.getAbsolutePath(), findArgValue(cmd, parser.PROPERTIES_FILE));
149+
assertEquals("yarn", findArgValue(cmd, parser.MASTER));
150+
assertEquals(deployMode, findArgValue(cmd, parser.DEPLOY_MODE));
151+
assertEquals("my.Class", findArgValue(cmd, parser.CLASS));
152+
assertEquals("MyApp", findArgValue(cmd, parser.NAME));
136153

137154
boolean appArgsOk = false;
138155
for (int i = 0; i < cmd.size(); i++) {
@@ -146,7 +163,7 @@ private void testCmdBuilder(boolean isDriver) throws Exception {
146163
}
147164
assertTrue("App resource and args should be added to command.", appArgsOk);
148165

149-
Map<String, String> conf = parseConf(cmd);
166+
Map<String, String> conf = parseConf(cmd, parser);
150167
assertEquals("foo", conf.get("spark.foo"));
151168
}
152169

@@ -178,10 +195,10 @@ private boolean contains(String needle, Iterable<String> haystack) {
178195
return false;
179196
}
180197

181-
private Map<String, String> parseConf(List<String> cmd) {
198+
private Map<String, String> parseConf(List<String> cmd, SparkSubmitOptionParser parser) {
182199
Map<String, String> conf = new HashMap<String, String>();
183200
for (int i = 0; i < cmd.size(); i++) {
184-
if (cmd.get(i).equals("--conf")) {
201+
if (cmd.get(i).equals(parser.CONF)) {
185202
String[] val = cmd.get(i + 1).split("=", 2);
186203
conf.put(val[0], val[1]);
187204
i += 1;

0 commit comments

Comments
 (0)