|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.deploy.history |
| 19 | + |
| 20 | +import java.net.URI |
| 21 | +import javax.servlet.http.HttpServletRequest |
| 22 | + |
| 23 | +import scala.collection.mutable |
| 24 | + |
| 25 | +import org.apache.hadoop.fs.{FileStatus, Path} |
| 26 | +import org.eclipse.jetty.servlet.ServletContextHandler |
| 27 | + |
| 28 | +import org.apache.spark.{Logging, SecurityManager, SparkConf} |
| 29 | +import org.apache.spark.deploy.SparkUIContainer |
| 30 | +import org.apache.spark.ui.SparkUI |
| 31 | +import org.apache.spark.ui.JettyUtils._ |
| 32 | +import org.apache.spark.util.Utils |
| 33 | +import org.apache.spark.scheduler.ReplayListenerBus |
| 34 | + |
| 35 | +/** |
| 36 | + * A web server that re-renders SparkUIs of finished applications. |
| 37 | + * |
| 38 | + * For the standalone mode, MasterWebUI already achieves this functionality. Thus, the |
| 39 | + * main use case of the HistoryServer is in other deploy modes (e.g. Yarn or Mesos). |
| 40 | + * |
| 41 | + * The logging directory structure is as follows: Within the given base directory, each |
| 42 | + * application's event logs are maintained in the application's own sub-directory. |
| 43 | + * |
| 44 | + * @param baseLogDir The base directory in which event logs are found |
| 45 | + * @param requestedPort The requested port to which this server is to be bound |
| 46 | + */ |
| 47 | +class HistoryServer(baseLogDir: String, requestedPort: Int, conf: SparkConf) |
| 48 | + extends SparkUIContainer("History Server") with Logging { |
| 49 | + |
| 50 | + private val host = Utils.localHostName() |
| 51 | + private val port = requestedPort |
| 52 | + private val indexPage = new IndexPage(this) |
| 53 | + private val fileSystem = Utils.getHadoopFileSystem(new URI(baseLogDir)) |
| 54 | + private val securityManager = new SecurityManager(conf) |
| 55 | + |
| 56 | + private val handlers = Seq[ServletContextHandler]( |
| 57 | + createStaticHandler(HistoryServer.STATIC_RESOURCE_DIR, "/static"), |
| 58 | + createServletHandler("/", |
| 59 | + (request: HttpServletRequest) => indexPage.render(request), securityMgr = securityManager) |
| 60 | + ) |
| 61 | + |
| 62 | + // A mapping from an event log path to the associated, already rendered, SparkUI |
| 63 | + val logPathToUI = mutable.HashMap[String, SparkUI]() |
| 64 | + |
| 65 | + // A mapping from an event log path to a timestamp of when it was last updated |
| 66 | + val logPathToLastUpdated = mutable.HashMap[String, Long]() |
| 67 | + |
| 68 | + /** Bind to the HTTP server behind this web interface */ |
| 69 | + override def bind() { |
| 70 | + try { |
| 71 | + serverInfo = Some(startJettyServer(host, port, handlers, conf)) |
| 72 | + logInfo("Started HistoryServer at http://%s:%d".format(host, boundPort)) |
| 73 | + } catch { |
| 74 | + case e: Exception => |
| 75 | + logError("Failed to create HistoryServer", e) |
| 76 | + System.exit(1) |
| 77 | + } |
| 78 | + checkForLogs() |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Check for any updated event logs. |
| 83 | + * |
| 84 | + * If a new application is found, render the associated SparkUI and remember it. |
| 85 | + * If an existing application is updated, re-render the associated SparkUI. |
| 86 | + * If an existing application is removed, remove the associated SparkUI. |
| 87 | + */ |
| 88 | + def checkForLogs() { |
| 89 | + val logStatus = fileSystem.listStatus(new Path(baseLogDir)) |
| 90 | + val logDirs = if (logStatus != null) logStatus.filter(_.isDir).toSeq else Seq[FileStatus]() |
| 91 | + |
| 92 | + // Render any missing or outdated SparkUI |
| 93 | + logDirs.foreach { dir => |
| 94 | + val path = dir.getPath.toString |
| 95 | + val lastUpdated = dir.getModificationTime |
| 96 | + if (!logPathToLastUpdated.contains(path) || |
| 97 | + logPathToLastUpdated.getOrElse(path, -1L) < lastUpdated) { |
| 98 | + maybeRenderUI(path, lastUpdated) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Remove any outdated SparkUIs |
| 103 | + val logPaths = logDirs.map(_.getPath.toString) |
| 104 | + logPathToUI.keys.foreach { path => |
| 105 | + if (!logPaths.contains(path)) { |
| 106 | + logPathToUI.remove(path) |
| 107 | + logPathToLastUpdated.remove(path) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + logWarning("By the end of check for logs, the map looks like") |
| 112 | + logPathToUI.foreach { case (k, v) => logWarning("* %s".format(k)) } |
| 113 | + } |
| 114 | + |
| 115 | + /** Attempt to render a new SparkUI from event logs residing in the given log directory. */ |
| 116 | + def maybeRenderUI(logPath: String, lastUpdated: Long) { |
| 117 | + logWarning("Maybe rendering UI %s".format(logPath)) |
| 118 | + |
| 119 | + val appName = logPath.split("/").last |
| 120 | + val replayBus = new ReplayListenerBus(conf) |
| 121 | + val ui = new SparkUI(conf, replayBus, appName, "/history/%s".format(appName)) |
| 122 | + |
| 123 | + // Do not call ui.bind() to avoid creating a new server for each application |
| 124 | + ui.start() |
| 125 | + val success = replayBus.replay(logPath) |
| 126 | + logWarning("Just replayed the events. Successful? %s".format(success)) |
| 127 | + if (success) { |
| 128 | + attachUI(ui) |
| 129 | + logPathToUI(logPath) = ui |
| 130 | + logPathToLastUpdated(logPath) = lastUpdated |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | +object HistoryServer { |
| 137 | + val STATIC_RESOURCE_DIR = SparkUI.STATIC_RESOURCE_DIR |
| 138 | + |
| 139 | + def main(argStrings: Array[String]) { |
| 140 | + val conf = new SparkConf |
| 141 | + val args = new HistoryServerArguments(argStrings, conf) |
| 142 | + val server = new HistoryServer(args.logDir, args.port, conf) |
| 143 | + server.bind() |
| 144 | + |
| 145 | + // Wait until the end of the world... or if the HistoryServer process is manually stopped |
| 146 | + while(true) { Thread.sleep(Int.MaxValue) } |
| 147 | + } |
| 148 | +} |
0 commit comments