Skip to content

Commit f12a0b1

Browse files
committed
Worked arount SPARK-2678
1 parent daee105 commit f12a0b1

File tree

4 files changed

+103
-29
lines changed

4 files changed

+103
-29
lines changed

bin/spark-sql

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,61 @@ CLASS="org.apache.spark.sql.hive.thriftserver.SparkSQLCLIDriver"
2828
# Figure out where Spark is installed
2929
FWDIR="$(cd `dirname $0`/..; pwd)"
3030

31-
if [[ "$@" = *--help ]] || [[ "$@" = *-h ]]; then
31+
function usage {
3232
echo "Usage: ./sbin/spark-sql [options] [cli option]"
3333
pattern="usage"
3434
pattern+="\|Spark assembly has been built with Hive"
3535
pattern+="\|NOTE: SPARK_PREPEND_CLASSES is set"
3636
pattern+="\|Spark Command: "
37+
pattern+="\|--help"
3738
pattern+="\|======="
3839

3940
$FWDIR/bin/spark-submit --help 2>&1 | grep -v Usage 1>&2
4041
echo
4142
echo "CLI options:"
4243
$FWDIR/bin/spark-class $CLASS --help 2>&1 | grep -v "$pattern" 1>&2
44+
}
45+
46+
function ensure_arg_number {
47+
arg_number=$1
48+
at_least=$2
49+
50+
if [[ $arg_number -lt $at_least ]]; then
51+
usage
52+
exit 1
53+
fi
54+
}
55+
56+
if [[ "$@" = --help ]] || [[ "$@" = -H ]]; then
57+
usage
4358
exit 0
4459
fi
4560

46-
exec "$FWDIR"/bin/spark-submit --class $CLASS spark-internal $@
61+
CLI_ARGS=""
62+
SUBMISSION_ARGS=""
63+
64+
while (($#)); do
65+
case $1 in
66+
-d | --define | --database | -e | -f | -h | --hiveconf | --hivevar | -i | -p)
67+
ensure_arg_number $# 2
68+
CLI_ARGS+=" $1"; shift
69+
CLI_ARGS+=" $1"; shift
70+
;;
71+
72+
-s | --silent)
73+
CLI_ARGS+=" $1"; shift
74+
;;
75+
76+
-v | --verbose)
77+
# Both SparkSubmit and SparkSQLCLIDriver recognizes -v | --verbose
78+
CLI_ARGS+=" $1"
79+
SUBMISSION_ARGS+=" $1"; shift
80+
;;
81+
82+
*)
83+
SUBMISSION_ARGS+=" $1"; shift
84+
;;
85+
esac
86+
done
87+
88+
exec "$FWDIR"/bin/spark-submit --class $CLASS $SUBMISSION_ARGS spark-internal $CLI_ARGS

core/src/main/scala/org/apache/spark/deploy/SparkSubmitArguments.scala

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ private[spark] class SparkSubmitArguments(args: Seq[String]) {
220220
/** Fill in values by parsing user options. */
221221
private def parseOpts(opts: Seq[String]): Unit = {
222222
var inSparkOpts = true
223+
val EQ_SEPARATED_OPT="""(--[^=]+)=(.+)""".r
223224

224225
// Delineates parsing of Spark options from parsing of user options.
225226
parse(opts)
@@ -322,33 +323,21 @@ private[spark] class SparkSubmitArguments(args: Seq[String]) {
322323
verbose = true
323324
parse(tail)
324325

326+
case EQ_SEPARATED_OPT(opt, value) :: tail =>
327+
parse(opt :: value :: tail)
328+
329+
case value :: tail if value.startsWith("-") =>
330+
SparkSubmit.printErrorAndExit(s"Unrecognized option '$value'.")
331+
325332
case value :: tail =>
326-
if (inSparkOpts) {
327-
value match {
328-
// convert --foo=bar to --foo bar
329-
case v if v.startsWith("--") && v.contains("=") && v.split("=").size == 2 =>
330-
val parts = v.split("=")
331-
parse(Seq(parts(0), parts(1)) ++ tail)
332-
case v if v.startsWith("-") =>
333-
val errMessage = s"Unrecognized option '$value'."
334-
SparkSubmit.printErrorAndExit(errMessage)
335-
case v =>
336-
primaryResource =
337-
if (!SparkSubmit.isShell(v) && !SparkSubmit.isInternal(v)) {
338-
Utils.resolveURI(v).toString
339-
} else {
340-
v
341-
}
342-
inSparkOpts = false
343-
isPython = SparkSubmit.isPython(v)
344-
parse(tail)
333+
primaryResource =
334+
if (!SparkSubmit.isShell(value) && !SparkSubmit.isInternal(value)) {
335+
Utils.resolveURI(value).toString
336+
} else {
337+
value
345338
}
346-
} else {
347-
if (!value.isEmpty) {
348-
childArgs += value
349-
}
350-
parse(tail)
351-
}
339+
isPython = SparkSubmit.isPython(value)
340+
childArgs ++= tail
352341

353342
case Nil =>
354343
}

core/src/test/scala/org/apache/spark/deploy/SparkSubmitSuite.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ class SparkSubmitSuite extends FunSuite with Matchers {
106106
appArgs.childArgs should be (Seq("some", "--weird", "args"))
107107
}
108108

109+
test("handles arguments to user program with name collision") {
110+
val clArgs = Seq(
111+
"--name", "myApp",
112+
"--class", "Foo",
113+
"userjar.jar",
114+
"--master", "local",
115+
"some",
116+
"--weird", "args")
117+
val appArgs = new SparkSubmitArguments(clArgs)
118+
appArgs.childArgs should be (Seq("--master", "local", "some", "--weird", "args"))
119+
}
120+
109121
test("handles YARN cluster mode") {
110122
val clArgs = Seq(
111123
"--deploy-mode", "cluster",

sbin/start-thriftserver.sh

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ FWDIR="$(cd `dirname $0`/..; pwd)"
2828

2929
CLASS="org.apache.spark.sql.hive.thriftserver.HiveThriftServer2"
3030

31-
if [[ "$@" = *--help ]] || [[ "$@" = *-h ]]; then
31+
function usage {
3232
echo "Usage: ./sbin/start-thriftserver [options] [thrift server options]"
3333
pattern="usage"
3434
pattern+="\|Spark assembly has been built with Hive"
@@ -41,7 +41,38 @@ if [[ "$@" = *--help ]] || [[ "$@" = *-h ]]; then
4141
echo
4242
echo "Thrift server options:"
4343
$FWDIR/bin/spark-class $CLASS --help 2>&1 | grep -v "$pattern" 1>&2
44+
}
45+
46+
function ensure_arg_number {
47+
arg_number=$1
48+
at_least=$2
49+
50+
if [[ $arg_number -lt $at_least ]]; then
51+
usage
52+
exit 1
53+
fi
54+
}
55+
56+
if [[ "$@" = --help ]] || [[ "$@" = -h ]]; then
57+
usage
4458
exit 0
4559
fi
4660

47-
exec "$FWDIR"/bin/spark-submit --class $CLASS spark-internal $@
61+
THRIFT_SERVER_ARGS=""
62+
SUBMISSION_ARGS=""
63+
64+
while (($#)); do
65+
case $1 in
66+
--hiveconf)
67+
ensure_arg_number $# 2
68+
THRIFT_SERVER_ARGS+=" $1"; shift
69+
THRIFT_SERVER_ARGS+=" $1"; shift
70+
;;
71+
72+
*)
73+
SUBMISSION_ARGS+=" $1"; shift
74+
;;
75+
esac
76+
done
77+
78+
exec "$FWDIR"/bin/spark-submit --class $CLASS $SUBMISSION_ARGS spark-internal $THRIFT_SERVER_ARGS

0 commit comments

Comments
 (0)