From ad993612f31ad167ad891c399c46ecf2eeb243a2 Mon Sep 17 00:00:00 2001 From: Dongjoon Hyun Date: Sun, 30 Jul 2017 15:22:46 -0700 Subject: [PATCH 1/2] [SPARK-21578][CORE] Consolidate redundant SparkContext constructors due to SI-8479 --- .../scala/org/apache/spark/SparkContext.scala | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/core/src/main/scala/org/apache/spark/SparkContext.scala b/core/src/main/scala/org/apache/spark/SparkContext.scala index e1ce66a547bc6..d37ef136e65b8 100644 --- a/core/src/main/scala/org/apache/spark/SparkContext.scala +++ b/core/src/main/scala/org/apache/spark/SparkContext.scala @@ -145,41 +145,6 @@ class SparkContext(config: SparkConf) extends Logging { this(SparkContext.updatedConf(new SparkConf(), master, appName, sparkHome, jars, environment)) } - // NOTE: The below constructors could be consolidated using default arguments. Due to - // Scala bug SI-8479, however, this causes the compile step to fail when generating docs. - // Until we have a good workaround for that bug the constructors remain broken out. - - /** - * Alternative constructor that allows setting common Spark properties directly - * - * @param master Cluster URL to connect to (e.g. mesos://host:port, spark://host:port, local[4]). - * @param appName A name for your application, to display on the cluster web UI. - */ - private[spark] def this(master: String, appName: String) = - this(master, appName, null, Nil, Map()) - - /** - * Alternative constructor that allows setting common Spark properties directly - * - * @param master Cluster URL to connect to (e.g. mesos://host:port, spark://host:port, local[4]). - * @param appName A name for your application, to display on the cluster web UI. - * @param sparkHome Location where Spark is installed on cluster nodes. - */ - private[spark] def this(master: String, appName: String, sparkHome: String) = - this(master, appName, sparkHome, Nil, Map()) - - /** - * Alternative constructor that allows setting common Spark properties directly - * - * @param master Cluster URL to connect to (e.g. mesos://host:port, spark://host:port, local[4]). - * @param appName A name for your application, to display on the cluster web UI. - * @param sparkHome Location where Spark is installed on cluster nodes. - * @param jars Collection of JARs to send to the cluster. These can be paths on the local file - * system or HDFS, HTTP, HTTPS, or FTP URLs. - */ - private[spark] def this(master: String, appName: String, sparkHome: String, jars: Seq[String]) = - this(master, appName, sparkHome, jars, Map()) - // log out Spark Version in Spark driver log logInfo(s"Running Spark version $SPARK_VERSION") From 0111c14918a716bc2c24413a9379a62f0212a5ca Mon Sep 17 00:00:00 2001 From: Dongjoon Hyun Date: Mon, 31 Jul 2017 00:25:30 -0700 Subject: [PATCH 2/2] Fix outdate comments and add explicit testsuite. --- .../scala/org/apache/spark/SparkContext.scala | 34 ++++++++++ .../apache/spark/JavaSparkContextSuite.java | 62 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 core/src/test/java/test/org/apache/spark/JavaSparkContextSuite.java diff --git a/core/src/main/scala/org/apache/spark/SparkContext.scala b/core/src/main/scala/org/apache/spark/SparkContext.scala index d37ef136e65b8..531646891499b 100644 --- a/core/src/main/scala/org/apache/spark/SparkContext.scala +++ b/core/src/main/scala/org/apache/spark/SparkContext.scala @@ -145,6 +145,40 @@ class SparkContext(config: SparkConf) extends Logging { this(SparkContext.updatedConf(new SparkConf(), master, appName, sparkHome, jars, environment)) } + // The following constructors are required when Java code accesses SparkContext directly. + // Please see SI-4278 + + /** + * Alternative constructor that allows setting common Spark properties directly + * + * @param master Cluster URL to connect to (e.g. mesos://host:port, spark://host:port, local[4]). + * @param appName A name for your application, to display on the cluster web UI. + */ + private[spark] def this(master: String, appName: String) = + this(master, appName, null, Nil, Map()) + + /** + * Alternative constructor that allows setting common Spark properties directly + * + * @param master Cluster URL to connect to (e.g. mesos://host:port, spark://host:port, local[4]). + * @param appName A name for your application, to display on the cluster web UI. + * @param sparkHome Location where Spark is installed on cluster nodes. + */ + private[spark] def this(master: String, appName: String, sparkHome: String) = + this(master, appName, sparkHome, Nil, Map()) + + /** + * Alternative constructor that allows setting common Spark properties directly + * + * @param master Cluster URL to connect to (e.g. mesos://host:port, spark://host:port, local[4]). + * @param appName A name for your application, to display on the cluster web UI. + * @param sparkHome Location where Spark is installed on cluster nodes. + * @param jars Collection of JARs to send to the cluster. These can be paths on the local file + * system or HDFS, HTTP, HTTPS, or FTP URLs. + */ + private[spark] def this(master: String, appName: String, sparkHome: String, jars: Seq[String]) = + this(master, appName, sparkHome, jars, Map()) + // log out Spark Version in Spark driver log logInfo(s"Running Spark version $SPARK_VERSION") diff --git a/core/src/test/java/test/org/apache/spark/JavaSparkContextSuite.java b/core/src/test/java/test/org/apache/spark/JavaSparkContextSuite.java new file mode 100644 index 0000000000000..7e9cc70d8651f --- /dev/null +++ b/core/src/test/java/test/org/apache/spark/JavaSparkContextSuite.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.org.apache.spark; + +import java.io.*; + +import scala.collection.immutable.List; +import scala.collection.immutable.List$; +import scala.collection.immutable.Map; +import scala.collection.immutable.Map$; + +import org.junit.Test; + +import org.apache.spark.api.java.*; +import org.apache.spark.*; + +/** + * Java apps can uses both Java-friendly JavaSparkContext and Scala SparkContext. + */ +public class JavaSparkContextSuite implements Serializable { + + @Test + public void javaSparkContext() { + String[] jars = new String[] {}; + java.util.Map environment = new java.util.HashMap<>(); + + new JavaSparkContext(new SparkConf().setMaster("local").setAppName("name")).stop(); + new JavaSparkContext("local", "name", new SparkConf()).stop(); + new JavaSparkContext("local", "name").stop(); + new JavaSparkContext("local", "name", "sparkHome", "jarFile").stop(); + new JavaSparkContext("local", "name", "sparkHome", jars).stop(); + new JavaSparkContext("local", "name", "sparkHome", jars, environment).stop(); + } + + @Test + public void scalaSparkContext() { + List jars = List$.MODULE$.empty(); + Map environment = Map$.MODULE$.empty(); + + new SparkContext(new SparkConf().setMaster("local").setAppName("name")).stop(); + new SparkContext("local", "name", new SparkConf()).stop(); + new SparkContext("local", "name").stop(); + new SparkContext("local", "name", "sparkHome").stop(); + new SparkContext("local", "name", "sparkHome", jars).stop(); + new SparkContext("local", "name", "sparkHome", jars, environment).stop(); + } +}