Skip to content

Commit 495d8cf

Browse files
jaceklaskowskiMarcelo Vanzin
authored and
Marcelo Vanzin
committed
[SPARK-24490][WEBUI] Use WebUI.addStaticHandler in web UIs
`WebUI` defines `addStaticHandler` that web UIs don't use (and simply introduce duplication). Let's clean them up and remove duplications. Local build and waiting for Jenkins Author: Jacek Laskowski <jacek@japila.pl> Closes #21510 from jaceklaskowski/SPARK-24490-Use-WebUI.addStaticHandler.
1 parent 6567fc4 commit 495d8cf

File tree

7 files changed

+33
-31
lines changed

7 files changed

+33
-31
lines changed

core/src/main/scala/org/apache/spark/deploy/history/HistoryServer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class HistoryServer(
124124

125125
attachHandler(ApiRootResource.getServletHandler(this))
126126

127-
attachHandler(createStaticHandler(SparkUI.STATIC_RESOURCE_DIR, "/static"))
127+
addStaticHandler(SparkUI.STATIC_RESOURCE_DIR)
128128

129129
val contextHandler = new ServletContextHandler
130130
contextHandler.setContextPath(HistoryServer.UI_PATH_PREFIX)

core/src/main/scala/org/apache/spark/deploy/master/ui/MasterWebUI.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MasterWebUI(
4343
val masterPage = new MasterPage(this)
4444
attachPage(new ApplicationPage(this))
4545
attachPage(masterPage)
46-
attachHandler(createStaticHandler(MasterWebUI.STATIC_RESOURCE_DIR, "/static"))
46+
addStaticHandler(MasterWebUI.STATIC_RESOURCE_DIR)
4747
attachHandler(createRedirectHandler(
4848
"/app/kill", "/", masterPage.handleAppKillRequest, httpMethods = Set("POST")))
4949
attachHandler(createRedirectHandler(

core/src/main/scala/org/apache/spark/deploy/worker/ui/WorkerWebUI.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class WorkerWebUI(
4747
val logPage = new LogPage(this)
4848
attachPage(logPage)
4949
attachPage(new WorkerPage(this))
50-
attachHandler(createStaticHandler(WorkerWebUI.STATIC_RESOURCE_BASE, "/static"))
50+
addStaticHandler(WorkerWebUI.STATIC_RESOURCE_BASE)
5151
attachHandler(createServletHandler("/log",
5252
(request: HttpServletRequest) => logPage.renderLog(request),
5353
worker.securityMgr,

core/src/main/scala/org/apache/spark/ui/SparkUI.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private[spark] class SparkUI private (
6565
attachTab(new StorageTab(this, store))
6666
attachTab(new EnvironmentTab(this, store))
6767
attachTab(new ExecutorsTab(this))
68-
attachHandler(createStaticHandler(SparkUI.STATIC_RESOURCE_DIR, "/static"))
68+
addStaticHandler(SparkUI.STATIC_RESOURCE_DIR)
6969
attachHandler(createRedirectHandler("/", "/jobs/", basePath = basePath))
7070
attachHandler(ApiRootResource.getServletHandler(this))
7171

core/src/main/scala/org/apache/spark/ui/WebUI.scala

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,25 @@ private[spark] abstract class WebUI(
6060
def getHandlers: Seq[ServletContextHandler] = handlers
6161
def getSecurityManager: SecurityManager = securityManager
6262

63-
/** Attach a tab to this UI, along with all of its attached pages. */
64-
def attachTab(tab: WebUITab) {
63+
/** Attaches a tab to this UI, along with all of its attached pages. */
64+
def attachTab(tab: WebUITab): Unit = {
6565
tab.pages.foreach(attachPage)
6666
tabs += tab
6767
}
6868

69-
def detachTab(tab: WebUITab) {
69+
/** Detaches a tab from this UI, along with all of its attached pages. */
70+
def detachTab(tab: WebUITab): Unit = {
7071
tab.pages.foreach(detachPage)
7172
tabs -= tab
7273
}
7374

74-
def detachPage(page: WebUIPage) {
75+
/** Detaches a page from this UI, along with all of its attached handlers. */
76+
def detachPage(page: WebUIPage): Unit = {
7577
pageToHandlers.remove(page).foreach(_.foreach(detachHandler))
7678
}
7779

78-
/** Attach a page to this UI. */
79-
def attachPage(page: WebUIPage) {
80+
/** Attaches a page to this UI. */
81+
def attachPage(page: WebUIPage): Unit = {
8082
val pagePath = "/" + page.prefix
8183
val renderHandler = createServletHandler(pagePath,
8284
(request: HttpServletRequest) => page.render(request), securityManager, conf, basePath)
@@ -88,41 +90,41 @@ private[spark] abstract class WebUI(
8890
handlers += renderHandler
8991
}
9092

91-
/** Attach a handler to this UI. */
92-
def attachHandler(handler: ServletContextHandler) {
93+
/** Attaches a handler to this UI. */
94+
def attachHandler(handler: ServletContextHandler): Unit = {
9395
handlers += handler
9496
serverInfo.foreach(_.addHandler(handler))
9597
}
9698

97-
/** Detach a handler from this UI. */
98-
def detachHandler(handler: ServletContextHandler) {
99+
/** Detaches a handler from this UI. */
100+
def detachHandler(handler: ServletContextHandler): Unit = {
99101
handlers -= handler
100102
serverInfo.foreach(_.removeHandler(handler))
101103
}
102104

103105
/**
104-
* Add a handler for static content.
106+
* Detaches the content handler at `path` URI.
105107
*
106-
* @param resourceBase Root of where to find resources to serve.
107-
* @param path Path in UI where to mount the resources.
108+
* @param path Path in UI to unmount.
108109
*/
109-
def addStaticHandler(resourceBase: String, path: String): Unit = {
110-
attachHandler(JettyUtils.createStaticHandler(resourceBase, path))
110+
def detachHandler(path: String): Unit = {
111+
handlers.find(_.getContextPath() == path).foreach(detachHandler)
111112
}
112113

113114
/**
114-
* Remove a static content handler.
115+
* Adds a handler for static content.
115116
*
116-
* @param path Path in UI to unmount.
117+
* @param resourceBase Root of where to find resources to serve.
118+
* @param path Path in UI where to mount the resources.
117119
*/
118-
def removeStaticHandler(path: String): Unit = {
119-
handlers.find(_.getContextPath() == path).foreach(detachHandler)
120+
def addStaticHandler(resourceBase: String, path: String = "/static"): Unit = {
121+
attachHandler(JettyUtils.createStaticHandler(resourceBase, path))
120122
}
121123

122-
/** Initialize all components of the server. */
124+
/** A hook to initialize components of the UI */
123125
def initialize(): Unit
124126

125-
/** Bind to the HTTP server behind this web interface. */
127+
/** Binds to the HTTP server behind this web interface. */
126128
def bind(): Unit = {
127129
assert(serverInfo.isEmpty, s"Attempted to bind $className more than once!")
128130
try {
@@ -136,17 +138,17 @@ private[spark] abstract class WebUI(
136138
}
137139
}
138140

139-
/** Return the url of web interface. Only valid after bind(). */
141+
/** @return The url of web interface. Only valid after [[bind]]. */
140142
def webUrl: String = s"http://$publicHostName:$boundPort"
141143

142-
/** Return the actual port to which this server is bound. Only valid after bind(). */
144+
/** @return The actual port to which this server is bound. Only valid after [[bind]]. */
143145
def boundPort: Int = serverInfo.map(_.boundPort).getOrElse(-1)
144146

145-
/** Stop the server behind this web interface. Only valid after bind(). */
147+
/** Stops the server behind this web interface. Only valid after [[bind]]. */
146148
def stop(): Unit = {
147149
assert(serverInfo.isDefined,
148150
s"Attempted to stop $className before binding to a server!")
149-
serverInfo.get.stop()
151+
serverInfo.foreach(_.stop())
150152
}
151153
}
152154

resource-managers/mesos/src/main/scala/org/apache/spark/deploy/mesos/ui/MesosClusterUI.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private[spark] class MesosClusterUI(
4040
override def initialize() {
4141
attachPage(new MesosClusterPage(this))
4242
attachPage(new DriverPage(this))
43-
attachHandler(createStaticHandler(MesosClusterUI.STATIC_RESOURCE_DIR, "/static"))
43+
addStaticHandler(MesosClusterUI.STATIC_RESOURCE_DIR)
4444
}
4545
}
4646

streaming/src/main/scala/org/apache/spark/streaming/ui/StreamingTab.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private[spark] class StreamingTab(val ssc: StreamingContext)
4949

5050
def detach() {
5151
getSparkUI(ssc).detachTab(this)
52-
getSparkUI(ssc).removeStaticHandler("/static/streaming")
52+
getSparkUI(ssc).detachHandler("/static/streaming")
5353
}
5454
}
5555

0 commit comments

Comments
 (0)