dongjoon-hyun commented on code in PR #60350:
URL: https://github.com/apache/spark/pull/60350#discussion_r4589756853
##########
core/src/test/scala/org/apache/spark/ui/UISuite.scala:
##########
@@ -201,6 +201,23 @@ class UISuite extends SparkFunSuite {
}
}
+ test("SPARK-59613: /static/sql resources are served by the core static
handler") {
+ withSpark(newSparkContext()) { sc =>
+ val ui = sc.ui.get
+ Seq(
+ "allexecutionspage.js",
+ "executionpage.js",
+ "spark-sql-viz.css",
+ "spark-sql-viz.js",
+ "sql-table-utils.js").foreach { name =>
+ val url = new URI(s"${ui.webUrl}/static/sql/$name").toURL
+ // fromURL throws on a non-2xx response, so reading the body also
checks the status.
Review Comment:
**[10] This check accepts any 200 with a non-empty body, and the catch-all
`/` handler returns one too.**
So the test does not fail when `SparkUI.initialize` stops adding the core
`/static` handler. Without `addStaticHandler(SparkUI.STATIC_RESOURCE_DIR)`
(`SparkUI.scala:120`), `/static/sql/<file>` reaches the `/` redirect handler
(`SparkUI.scala:121`), which answers with a 302 to `/jobs/`.
`HttpURLConnection` follows it, so `Source.fromURL` returns the Jobs page and
`content.nonEmpty` holds. The comment above is not quite right either:
`getInputStream` throws only for a status of 400 or more, and a redirect is
followed silently.
The new `HistoryServerSuite` test has the same gap. Without
`HistoryServer.scala:157`, the request reaches `HistoryPage`, which is mounted
at `/` and renders the application list with a 200, so
`HistoryServerSuite.scala:793-794` pass. So [the reply on item
5](https://github.com/apache/spark/pull/60350#discussion_r4585577996),
"dropping addStaticHandler from either one fails its own test", does not hold
yet. Both tests still catch the bug of this PR, because without this change the
`/static` handler answers 404 for the missing `sql/` directory.
Comparing each body with the bundled file would close the gap in all the new
tests, for example:
```scala
val expected = Utils.tryWithResource(
getClass.getResourceAsStream(s"/${SparkUI.STATIC_RESOURCE_DIR}/sql/$name"))(Utils.toString)
assert(content === expected, s"$url did not return the bundled file")
```
##########
sql/core/src/test/scala/org/apache/spark/deploy/history/SQLHistoryServerStaticResourceSuite.scala:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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 org.apache.spark.deploy.history
+
+import java.net.URI
+
+import org.apache.spark.{SparkConf, SparkFunSuite}
+import org.apache.spark.internal.config.History.HISTORY_LOG_DIR
+
+/**
+ * Verifies that the SQL tab's /static/sql files, which are provided by core's
static resource
+ * directory, are served by the History Server for a real SQL application: for
the files its pages
+ * reference, and before and after its UI is detached (as happens when it is
evicted from the
+ * application cache).
+ */
+class SQLHistoryServerStaticResourceSuite extends SparkFunSuite {
+
+ private val appId = "local-1596020211915"
+
+ private val staticSqlFiles = Set(
+ "allexecutionspage.js",
+ "executionpage.js",
+ "spark-sql-viz.css",
+ "spark-sql-viz.js",
+ "sql-table-utils.js").map("/static/sql/" + _)
+
+ private def withHistoryServer(fn: (HistoryServer, FsHistoryProvider) =>
Unit): Unit = {
+ val logDir =
Thread.currentThread().getContextClassLoader.getResource("spark-events").toString
+ val conf = new SparkConf().set(HISTORY_LOG_DIR, logDir)
+ val provider = new FsHistoryProvider(conf)
+ provider.checkForLogs()
+ val server = new HistoryServer(conf, provider,
HistoryServer.createSecurityManager(conf), 0)
+ server.bind()
+ try {
+ fn(server, provider)
+ } finally {
+ server.stop()
+ provider.stop()
+ }
+ }
+
+ // Throws if the response is not a 200.
+ private def getContent(server: HistoryServer, path: String): String = {
+ HistoryServerSuite.getUrl(new
URI(s"http://localhost:${server.boundPort}$path").toURL)
Review Comment:
**[11] These requests go to `localhost`, but the History Server binds to
`SPARK_LOCAL_IP` when it is set.**
`WebUI.initServer` binds Jetty to `SPARK_LOCAL_IP` if it is set
(`WebUI.scala:149-150`). When it is set to a LAN, VPN or global IPv6 address of
the host, `http://localhost:<port>` is refused and all three tests fail with
`ConnectException`. The new `HistoryServerSuite` test passes in the same
environment, because it uses `Utils.localHostNameForURI()`
(`HistoryServerSuite.scala:87`). CI does not see this because it sets
`SPARK_LOCAL_IP: localhost`. SPARK-39464 and SPARK-39563 moved the History
Server and UI suites to `localHostNameForURI()` for this reason. Since this
package has its own `Utils` object, the fully qualified name is the simplest:
```scala
private val localhost = org.apache.spark.util.Utils.localHostNameForURI()
```
and then `s"http://$localhost:${server.boundPort}$path"` here.
##########
sql/core/src/test/scala/org/apache/spark/deploy/history/SQLHistoryServerStaticResourceSuite.scala:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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 org.apache.spark.deploy.history
+
+import java.net.URI
+
+import org.apache.spark.{SparkConf, SparkFunSuite}
+import org.apache.spark.internal.config.History.HISTORY_LOG_DIR
+
+/**
+ * Verifies that the SQL tab's /static/sql files, which are provided by core's
static resource
+ * directory, are served by the History Server for a real SQL application: for
the files its pages
+ * reference, and before and after its UI is detached (as happens when it is
evicted from the
+ * application cache).
+ */
+class SQLHistoryServerStaticResourceSuite extends SparkFunSuite {
+
+ private val appId = "local-1596020211915"
+
+ private val staticSqlFiles = Set(
+ "allexecutionspage.js",
+ "executionpage.js",
+ "spark-sql-viz.css",
+ "spark-sql-viz.js",
+ "sql-table-utils.js").map("/static/sql/" + _)
+
+ private def withHistoryServer(fn: (HistoryServer, FsHistoryProvider) =>
Unit): Unit = {
+ val logDir =
Thread.currentThread().getContextClassLoader.getResource("spark-events").toString
+ val conf = new SparkConf().set(HISTORY_LOG_DIR, logDir)
+ val provider = new FsHistoryProvider(conf)
+ provider.checkForLogs()
+ val server = new HistoryServer(conf, provider,
HistoryServer.createSecurityManager(conf), 0)
+ server.bind()
+ try {
+ fn(server, provider)
+ } finally {
+ server.stop()
+ provider.stop()
+ }
+ }
+
+ // Throws if the response is not a 200.
+ private def getContent(server: HistoryServer, path: String): String = {
+ HistoryServerSuite.getUrl(new
URI(s"http://localhost:${server.boundPort}$path").toURL)
+ }
+
+ test("SPARK-59613: every /static/sql file referenced by the SQL pages is
served") {
+ withHistoryServer { (server, _) =>
+ val pages = Seq(s"/history/$appId/SQL/",
s"/history/$appId/SQL/execution/?id=0")
+ val referenced = pages.flatMap { page =>
+ "/static/sql/[A-Za-z0-9_.-]+".r.findAllIn(getContent(server, page))
+ }.toSet
+ assert(staticSqlFiles.subsetOf(referenced),
+ s"the SQL pages should reference ${staticSqlFiles -- referenced}")
+ referenced.foreach { path =>
+ assert(getContent(server, path).nonEmpty, s"$path returned an empty
body")
+ }
+ }
+ }
+
+ test("SPARK-59613: /static/sql is served right after startup, before any UI
is attached") {
Review Comment:
**[13] Nit: this test checks the same state as the new `HistoryServerSuite`
test and the last line of the next test.**
Nothing from sql/core runs when the History Server starts. Plugins are
loaded only when an application UI is built (`FsHistoryProvider.scala:450` and
`FsHistoryProvider.scala:1442`). So with no application loaded, this test sees
the same handlers as the core test at `HistoryServerSuite.scala:779-797`, which
also checks the five files and `loadCount`, and as L95 after `detachSparkUI`.
In #58965 the History Server registered the plugin's resources at startup, so
this check meant something there. Here it could be dropped. I asked you to
bring that suite over, so this is only a nit.
##########
core/src/test/scala/org/apache/spark/ui/UISuite.scala:
##########
@@ -201,6 +201,23 @@ class UISuite extends SparkFunSuite {
}
}
+ test("SPARK-59613: /static/sql resources are served by the core static
handler") {
+ withSpark(newSparkContext()) { sc =>
+ val ui = sc.ui.get
+ Seq(
+ "allexecutionspage.js",
+ "executionpage.js",
+ "spark-sql-viz.css",
+ "spark-sql-viz.js",
+ "sql-table-utils.js").foreach { name =>
+ val url = new URI(s"${ui.webUrl}/static/sql/$name").toURL
+ // fromURL throws on a non-2xx response, so reading the body also
checks the status.
+ val content = Utils.tryWithResource(Source.fromURL(url))(_.mkString)
Review Comment:
**[12] `Source.fromURL` decodes with the default charset of the JVM, and
`spark-sql-viz.js` is not plain ASCII.**
`spark-sql-viz.js:362` has an em dash in a comment. On master this is fine,
since Java 21 defaults to UTF-8 and SBT passes `-Dfile.encoding=UTF-8`. If this
fix is backported to a Java 17 branch such as `branch-4.x`, a Maven run under a
POSIX locale decodes the file as US-ASCII, and `mkString` throws
`MalformedInputException` although the file was served with a 200. The other
new tests are not affected, because `HistoryServerSuite.getContentAndCode`
decodes as UTF-8. Passing the charset avoids it:
```scala
val content = Utils.tryWithResource(Source.fromURL(url, "UTF-8"))(_.mkString)
```
##########
sql/core/src/test/scala/org/apache/spark/deploy/history/SQLHistoryServerStaticResourceSuite.scala:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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 org.apache.spark.deploy.history
+
+import java.net.URI
+
+import org.apache.spark.{SparkConf, SparkFunSuite}
+import org.apache.spark.internal.config.History.HISTORY_LOG_DIR
+
+/**
+ * Verifies that the SQL tab's /static/sql files, which are provided by core's
static resource
+ * directory, are served by the History Server for a real SQL application: for
the files its pages
+ * reference, and before and after its UI is detached (as happens when it is
evicted from the
+ * application cache).
+ */
+class SQLHistoryServerStaticResourceSuite extends SparkFunSuite {
+
+ private val appId = "local-1596020211915"
+
+ private val staticSqlFiles = Set(
+ "allexecutionspage.js",
+ "executionpage.js",
+ "spark-sql-viz.css",
+ "spark-sql-viz.js",
+ "sql-table-utils.js").map("/static/sql/" + _)
+
+ private def withHistoryServer(fn: (HistoryServer, FsHistoryProvider) =>
Unit): Unit = {
+ val logDir =
Thread.currentThread().getContextClassLoader.getResource("spark-events").toString
+ val conf = new SparkConf().set(HISTORY_LOG_DIR, logDir)
+ val provider = new FsHistoryProvider(conf)
+ provider.checkForLogs()
+ val server = new HistoryServer(conf, provider,
HistoryServer.createSecurityManager(conf), 0)
+ server.bind()
+ try {
+ fn(server, provider)
+ } finally {
+ server.stop()
+ provider.stop()
+ }
+ }
+
+ // Throws if the response is not a 200.
+ private def getContent(server: HistoryServer, path: String): String = {
+ HistoryServerSuite.getUrl(new
URI(s"http://localhost:${server.boundPort}$path").toURL)
+ }
+
+ test("SPARK-59613: every /static/sql file referenced by the SQL pages is
served") {
+ withHistoryServer { (server, _) =>
+ val pages = Seq(s"/history/$appId/SQL/",
s"/history/$appId/SQL/execution/?id=0")
+ val referenced = pages.flatMap { page =>
+ "/static/sql/[A-Za-z0-9_.-]+".r.findAllIn(getContent(server, page))
+ }.toSet
+ assert(staticSqlFiles.subsetOf(referenced),
+ s"the SQL pages should reference ${staticSqlFiles -- referenced}")
+ referenced.foreach { path =>
+ assert(getContent(server, path).nonEmpty, s"$path returned an empty
body")
+ }
+ }
+ }
+
+ test("SPARK-59613: /static/sql is served right after startup, before any UI
is attached") {
+ withHistoryServer { (server, _) =>
+ staticSqlFiles.foreach(path => assert(getContent(server, path).nonEmpty,
path))
+ assert(server.cacheMetrics.loadCount.getCount === 0, "no apps should be
loaded")
+ }
+ }
+
+ test("SPARK-59613: /static/sql stays served after the SQL application UI is
detached") {
+ withHistoryServer { (server, provider) =>
+ val ui = provider.getAppUI(appId, None).getOrElse(fail(s"Failed to load
$appId")).ui
+ // The SQL tab no longer registers its own /static/sql handler.
+ assert(!ui.getHandlers.exists(_.getContextPath == "/static/sql"))
+
+ server.attachSparkUI(appId, None, ui, completed = true)
Review Comment:
**[14] Nit: the check while the UI is attached cannot fail, with or without
this change.**
The first test already requests the five files while this application's UI
is attached (L64-72). Without this change the attached `SQLTab`'s own
`/static/sql` handler serves them, and with it the History Server's `/static`
does, so L91 passes either way. Only L87 and L95 can catch a regression here.
Without L91, nothing can throw between attaching and detaching, so the
`try`/`finally` is not needed either:
```scala
server.attachSparkUI(appId, None, ui, completed = true)
server.detachSparkUI(appId, None, ui)
staticSqlFiles.foreach(path => assert(getContent(server, path).nonEmpty,
path))
```
##########
sql/core/src/test/scala/org/apache/spark/deploy/history/SQLHistoryServerStaticResourceSuite.scala:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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 org.apache.spark.deploy.history
+
+import java.net.URI
+
+import org.apache.spark.{SparkConf, SparkFunSuite}
+import org.apache.spark.internal.config.History.HISTORY_LOG_DIR
+
+/**
+ * Verifies that the SQL tab's /static/sql files, which are provided by core's
static resource
+ * directory, are served by the History Server for a real SQL application: for
the files its pages
+ * reference, and before and after its UI is detached (as happens when it is
evicted from the
+ * application cache).
+ */
+class SQLHistoryServerStaticResourceSuite extends SparkFunSuite {
+
+ private val appId = "local-1596020211915"
+
+ private val staticSqlFiles = Set(
+ "allexecutionspage.js",
+ "executionpage.js",
+ "spark-sql-viz.css",
+ "spark-sql-viz.js",
+ "sql-table-utils.js").map("/static/sql/" + _)
+
+ private def withHistoryServer(fn: (HistoryServer, FsHistoryProvider) =>
Unit): Unit = {
+ val logDir =
Thread.currentThread().getContextClassLoader.getResource("spark-events").toString
+ val conf = new SparkConf().set(HISTORY_LOG_DIR, logDir)
+ val provider = new FsHistoryProvider(conf)
+ provider.checkForLogs()
+ val server = new HistoryServer(conf, provider,
HistoryServer.createSecurityManager(conf), 0)
+ server.bind()
+ try {
+ fn(server, provider)
+ } finally {
+ server.stop()
+ provider.stop()
Review Comment:
**[15] Nit: `server.stop()` already stops the provider.**
`HistoryServer.stop()` calls `provider.stop()`
(`HistoryServer.scala:172-175`), so this second call does nothing, and it would
be skipped anyway if `server.stop()` threw. The provider is passed to the
callback only for `provider.getAppUI` in the last test, and
`HistoryServer.getAppUI` delegates to it. So this could be:
```scala
private def withHistoryServer(fn: HistoryServer => Unit): Unit = {
// the same setup as now
try fn(server) finally server.stop()
}
```
with `server.getAppUI(appId, None)` in the last test.
##########
core/src/test/scala/org/apache/spark/deploy/history/HistoryServerSuite.scala:
##########
@@ -775,6 +775,26 @@ abstract class HistoryServerSuite extends SparkFunSuite
with BeforeAndAfter with
assert(conn.getResponseCode === 302)
assert(conn.getHeaderField("Location") === "/")
}
+
+ test("SPARK-59613: /static/sql resources are served on startup without
loading any app") {
+
+ stop()
+ init()
+
+ val port = server.boundPort
+ Seq(
+ "allexecutionspage.js",
+ "executionpage.js",
+ "spark-sql-viz.css",
+ "spark-sql-viz.js",
+ "sql-table-utils.js").foreach { name =>
+ val url = new URI(s"http://$localhost:$port/static/sql/$name").toURL
+ val (code, content, error) = HistoryServerSuite.getContentAndCode(url)
Review Comment:
**[16] Nit: these three lines repeat `HistoryServerSuite.getUrl`.**
`getUrl` (`HistoryServerSuite.scala:833-841`) already fails on a non-200
status with the code, the URL and the error body, and the new sql/core suite
uses it for the same check. So this could be:
```scala
assert(HistoryServerSuite.getUrl(url).nonEmpty, s"$url returned an empty
body")
```
The blank line at L780 could go too.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]