Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ <h4 class="title-table">Executors</h4>
Shuffle Write</span></th>
<th>Logs</th>
<th>Thread Dump</th>
<th>Heap Histogram</th>
<th>Exec Loss Reason</th>
</tr>
</thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@
/* global jQuery, setDataTableDefaults */

var threadDumpEnabled = false;
var heapHistogramEnabled = false;

/* eslint-disable no-unused-vars */
function setThreadDumpEnabled(val) {
threadDumpEnabled = val;
}
function setHeapHistogramEnabled(val) {
heapHistogramEnabled = val;
}
/* eslint-enable no-unused-vars */

function getThreadDumpEnabled() {
return threadDumpEnabled;
}

function getHeapHistogramEnabled() {
return heapHistogramEnabled;
}

function formatLossReason(removeReason) {
if (removeReason) {
return removeReason
Expand Down Expand Up @@ -551,6 +559,12 @@ $(document).ready(function () {
return type === 'display' ? ("<a href='threadDump/?executorId=" + data + "'>Thread Dump</a>" ) : data;
}
},
{
name: 'heapHistogramCol',
data: 'id', render: function (data, type) {
return type === 'display' ? ("<a href='heapHistogram/?executorId=" + data + "'>Heap Histogram</a>") : data;
}
},
{
data: 'removeReason',
render: formatLossReason
Expand All @@ -566,14 +580,15 @@ $(document).ready(function () {
{"visible": false, "targets": 10},
{"visible": false, "targets": 13},
{"visible": false, "targets": 14},
{"visible": false, "targets": 25}
{"visible": false, "targets": 26}
],
"deferRender": true
};

execDataTable = $(selector).DataTable(conf);
execDataTable.column('executorLogsCol:name').visible(logsExist(response));
execDataTable.column('threadDumpCol:name').visible(getThreadDumpEnabled());
execDataTable.column('heapHistogramCol:name').visible(getHeapHistogramEnabled());
$('#active-executors [data-toggle="tooltip"]').tooltip();

// This section should be visible once API gives the response.
Expand Down Expand Up @@ -721,7 +736,7 @@ $(document).ready(function () {
"<div id='direct_mapped_pool_memory' class='direct_mapped_pool_memory-checkbox-div'><input type='checkbox' class='toggle-vis' data-sum-col-idx='' data-exec-col-idx='10'> Peak Pool Memory Direct / Mapped</div>" +
"<div id='extra_resources' class='resources-checkbox-div'><input type='checkbox' class='toggle-vis' data-sum-col-idx='' data-exec-col-idx='13'> Resources</div>" +
"<div id='resource_prof_id' class='resource-prof-id-checkbox-div'><input type='checkbox' class='toggle-vis' data-sum-col-idx='' data-exec-col-idx='14'> Resource Profile Id</div>" +
"<div id='exec_loss_reason' class='exec-loss-reason-checkbox-div'><input type='checkbox' class='toggle-vis' data-sum-col-idx='' data-exec-col-idx='25'> Exec Loss Reason</div>" +
"<div id='exec_loss_reason' class='exec-loss-reason-checkbox-div'><input type='checkbox' class='toggle-vis' data-sum-col-idx='' data-exec-col-idx='26'> Exec Loss Reason</div>" +
"</div>");

reselectCheckboxesBasedOnTaskTableState();
Expand Down
26 changes: 25 additions & 1 deletion core/src/main/scala/org/apache/spark/SparkContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import org.apache.spark.shuffle.api.ShuffleDriverComponents
import org.apache.spark.status.{AppStatusSource, AppStatusStore}
import org.apache.spark.status.api.v1.ThreadStackTrace
import org.apache.spark.storage._
import org.apache.spark.storage.BlockManagerMessages.TriggerThreadDump
import org.apache.spark.storage.BlockManagerMessages.{TriggerHeapHistogram, TriggerThreadDump}
import org.apache.spark.ui.{ConsoleProgressBar, SparkUI}
import org.apache.spark.util._
import org.apache.spark.util.logging.DriverLogger
Expand Down Expand Up @@ -750,6 +750,30 @@ class SparkContext(config: SparkConf) extends Logging {
}
}

/**
* Called by the web UI to obtain executor heap histogram.
*/
private[spark] def getExecutorHeapHistogram(executorId: String): Option[Array[String]] = {
try {
if (executorId == SparkContext.DRIVER_IDENTIFIER) {
Some(Utils.getHeapHistogram())
} else {
env.blockManager.master.getExecutorEndpointRef(executorId) match {
case Some(endpointRef) =>
Some(endpointRef.askSync[Array[String]](TriggerHeapHistogram))
case None =>
logWarning(s"Executor $executorId might already have stopped and " +
"can not request heap histogram from it.")
None
}
}
} catch {
case e: Exception =>
logError(s"Exception getting heap histogram from executor $executorId", e)
None
}
}

private[spark] def getLocalProperties: Properties = localProperties.get()

private[spark] def setLocalProperties(props: Properties): Unit = {
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/scala/org/apache/spark/internal/config/UI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package org.apache.spark.internal.config
import java.util.Locale
import java.util.concurrent.TimeUnit

import org.apache.commons.lang3.{JavaVersion, SystemUtils}

import org.apache.spark.network.util.ByteUnit

private[spark] object UI {
Expand Down Expand Up @@ -97,6 +99,11 @@ private[spark] object UI {
.booleanConf
.createWithDefault(true)

val UI_HEAP_HISTOGRAM_ENABLED = ConfigBuilder("spark.ui.heapHistogramEnabled")
.version("3.5.0")
.booleanConf
.createWithDefault(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_11))
Copy link
Member Author

@dongjoon-hyun dongjoon-hyun Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java 8 JRE doesn't have jmap. For Java 8 JDK users, they are able this manually.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the answer for jmap question, @viirya .


val UI_PROMETHEUS_ENABLED = ConfigBuilder("spark.ui.prometheus.enabled")
.internal()
.doc("Expose executor metrics at /metrics/executors/prometheus. " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ private[spark] object BlockManagerMessages {
*/
case object TriggerThreadDump extends ToBlockManagerMasterStorageEndpoint

/**
* Driver to Executor message to get a heap histogram.
*/
case object TriggerHeapHistogram extends ToBlockManagerMasterStorageEndpoint

//////////////////////////////////////////////////////////////////////////////////
// Messages from storage endpoints to the master.
//////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class BlockManagerStorageEndpoint(
case TriggerThreadDump =>
context.reply(Utils.getThreadDump())

case TriggerHeapHistogram =>
context.reply(Utils.getHeapHistogram())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, what if executor node doesn't have jmap installed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All Java 11+ has jmap because they are JDK~


case ReplicateBlock(blockId, replicas, maxReplicas) =>
context.reply(blockManager.replicateBlock(blockId, replicas.toSet, maxReplicas))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.ui.exec

import javax.servlet.http.HttpServletRequest

import scala.xml.{Node, Text}

import org.apache.spark.SparkContext
import org.apache.spark.ui.{SparkUITab, UIUtils, WebUIPage}

private[ui] class ExecutorHeapHistogramPage(
parent: SparkUITab,
sc: Option[SparkContext]) extends WebUIPage("heapHistogram") {

// Match the lines containing object informations
val pattern = """\s*([0-9]+):\s+([0-9]+)\s+([0-9]+)\s+(\S+)(.*)""".r

def render(request: HttpServletRequest): Seq[Node] = {
val executorId = Option(request.getParameter("executorId")).map { executorId =>
UIUtils.decodeURLParameter(executorId)
}.getOrElse {
throw new IllegalArgumentException(s"Missing executorId parameter")
}
val time = System.currentTimeMillis()
val maybeHeapHistogram = sc.get.getExecutorHeapHistogram(executorId)

val content = maybeHeapHistogram.map { heapHistogram =>
val rows = heapHistogram.map { row =>
row match {
case pattern(rank, instances, bytes, name, module) =>
<tr class="accordion-heading">
<td>{rank}</td>
<td>{instances}</td>
<td>{bytes}</td>
<td>{name}</td>
<td>{module}</td>
</tr>
case pattern(rank, instances, bytes, name) =>
<tr class="accordion-heading">
<td>{rank}</td>
<td>{instances}</td>
<td>{bytes}</td>
<td>{name}</td>
<td></td>
</tr>
case _ =>
// Ignore the first two lines and the last line
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, we will ignore all irregular messages in addition to these three lines.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the error will go to Executor log, and in UI it just doesn't show heap dump (empty)?

Copy link
Member Author

@dongjoon-hyun dongjoon-hyun Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, correct. We log and return None for Option[Array[String]]. So, the heap histogram UI will be empty. When the executor JVM is dead for some other reasons before driver notices it, it'll be handled in the same way.

private[spark] def getExecutorHeapHistogram(executorId: String): Option[Array[String]] = {
try {
if (executorId == SparkContext.DRIVER_IDENTIFIER) {
Some(Utils.getHeapHistogram())
} else {
env.blockManager.master.getExecutorEndpointRef(executorId) match {
case Some(endpointRef) =>
Some(endpointRef.askSync[Array[String]](TriggerHeapHistogram))
case None =>
logWarning(s"Executor $executorId might already have stopped and " +
"can not request heap histogram from it.")
None
}
}
} catch {
case e: Exception =>
logError(s"Exception getting heap histogram from executor $executorId", e)
None
}
}

//
// num #instances #bytes class name (module)
// -------------------------------------------------------
// ...
// Total 1267867 72845688
}
}
<div class="row">
<div class="col-12">
<p>Updated at {UIUtils.formatDate(time)}</p>
<table class={UIUtils.TABLE_CLASS_STRIPED + " accordion-group" + " sortable"}>
<thead>
<th>Rank</th>
<th>Instances</th>
<th>Bytes</th>
<th>Class Name</th>
<th>Module</th>
</thead>
<tbody>{rows}</tbody>
</table>
</div>
</div>
}.getOrElse(Text("Error fetching heap histogram"))
UIUtils.headerSparkPage(request, s"Heap Histogram for Executor $executorId", content, parent)
}
}
11 changes: 9 additions & 2 deletions core/src/main/scala/org/apache/spark/ui/exec/ExecutorsTab.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,24 @@ private[ui] class ExecutorsTab(parent: SparkUI) extends SparkUITab(parent, "exec
private def init(): Unit = {
val threadDumpEnabled =
parent.sc.isDefined && parent.conf.get(UI_THREAD_DUMPS_ENABLED)
val heapHistogramEnabled =
parent.sc.isDefined && parent.conf.get(UI_HEAP_HISTOGRAM_ENABLED)

attachPage(new ExecutorsPage(this, threadDumpEnabled))
attachPage(new ExecutorsPage(this, threadDumpEnabled, heapHistogramEnabled))
if (threadDumpEnabled) {
attachPage(new ExecutorThreadDumpPage(this, parent.sc))
}
if (heapHistogramEnabled) {
attachPage(new ExecutorHeapHistogramPage(this, parent.sc))
}
}

}

private[ui] class ExecutorsPage(
parent: SparkUITab,
threadDumpEnabled: Boolean)
threadDumpEnabled: Boolean,
heapHistogramEnabled: Boolean)
extends WebUIPage("") {

def render(request: HttpServletRequest): Seq[Node] = {
Expand All @@ -52,6 +58,7 @@ private[ui] class ExecutorsPage(
<script src={UIUtils.prependBaseUri(request, "/static/utils.js")}></script> ++
<script src={UIUtils.prependBaseUri(request, "/static/executorspage.js")}></script> ++
<script>setThreadDumpEnabled({threadDumpEnabled})</script>
<script>setHeapHistogramEnabled({heapHistogramEnabled})</script>
}

UIUtils.headerSparkPage(request, "Executors", content, parent, useDataTables = true)
Expand Down
17 changes: 17 additions & 0 deletions core/src/main/scala/org/apache/spark/util/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,23 @@ private[spark] object Utils extends Logging with SparkClassUtils {
}.map(threadInfoToThreadStackTrace)
}

/** Return a heap dump. Used to capture dumps for the web UI */
def getHeapHistogram(): Array[String] = {
// From Java 9+, we can use 'ProcessHandle.current().pid()'
val pid = getProcessName().split("@").head
val builder = new ProcessBuilder("jmap", "-histo:live", pid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get error from executing it, can we get the error back?

executeAndGetOutput seems can handle it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which error do you have in your mind? At this layer, we don't handle it like getThreadDump. For example, getThreadDump can throw SecurityException and UnsupportedOperationException, but we ignore them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message will go to stderr of Executor log.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at executeAndGetOutput, but it's the same. It throws SparkException instead of get the error back.

if (exitCode != 0) {
logError(s"Process $command exited with code $exitCode: $output")
throw new SparkException(s"Process $command exited with code $exitCode")
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it respect JAVA_HOME then PATH?

Copy link
Member Author

@dongjoon-hyun dongjoon-hyun Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you pointed, there is no problem to run jmap, isn't it, @pan3793 ? IIRC, jmap is just CLI command without any format change. Especially, if you are saying JDKs here.

Copy link
Member

@pan3793 pan3793 Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the $JAVA_HOME/bin/jmap(in above case, should be /opt/openjdk-11/bin/jmap instead of /opt/openjdk-8/bin/jmap) should be used, instead of the one first present in PATH.

And if only JAVA_HOME is set, but no jmap in PATH, the invocation will fail. e.g.

The JDK was installed by TGZ, it was unarchived to /opt/openjdk-11 whithout exposing to PATH nor creating softlink to /usr/bin, then Spark executor proccess respect JAVA_HOME to find $JAVA_HOME/bin/java but the subprocess can not find jmap even it knows where JAVA_HOME is.

Copy link
Member Author

@dongjoon-hyun dongjoon-hyun Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pan3793 . Here, the contract on jmap is just one of available binary, not the same JDK's jmap.

However, I'm interested in this case. Could you make a small reproducer with Docker image?

Spark executor proccess respect JAVA_HOME to find $JAVA_HOME/bin/java but the subprocess can not find jmap even it knows where JAVA_HOME is.

Copy link
Contributor

@mridulm mridulm Jun 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dongjoon-hyun This is an issue - we should use $JAVA_HOME/bin/jmap (more specifically whatever comes from System.getProperty("java.home")), not the first jmap which happens to be in the PATH. It is common to override JAVA_HOME to specify the java version to be used explicitly (or even to not have jdk in the PATH at all).

Also, there is no compatibility gaurantees that I am aware of between different versions of jdk and jmap (for example, jdk11 jmap against jdk17 or vice versa) - if I missed any, please do let me know !

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Let me take a look at that perspective, @mridulm and @pan3793 .

In the worst case, I'll limit this feature to K8s only environment because this was initially developed for K8s environment first.

builder.redirectErrorStream(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log errors in invocation to executor logs instead of sending it to driver as response ?

Copy link
Member Author

@dongjoon-hyun dongjoon-hyun Jun 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's totally different feature. This is designed as a part of Spark Driver UI, @mridulm .
Sorry, I misunderstood this request. Sure, I'll try.

val p = builder.start()
val r = new BufferedReader(new InputStreamReader(p.getInputStream()))
Copy link
Contributor

@mridulm mridulm Jun 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This reader is not closed and/or we are not doing waitFor on the process.

val rows = ArrayBuffer.empty[String]
var line = ""
while (line != null) {
if (line.nonEmpty) rows += line
line = r.readLine()
}
rows.toArray
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use IOUtils.readLines or Source.getLines instead ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use IOUtils.readLines or Source.getLines instead ?

For this one, I was thinking about adding a new configuration to limit the results like Top 100 or Top 1000. I'll handle this with that new configuration together.

}

def getThreadDumpForThread(threadId: Long): Option[ThreadStackTrace] = {
if (threadId <= 0) {
None
Expand Down