-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-8981][CORE][FOLLOW-UP] Clean up MDC properties after running a task #28756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -322,11 +322,15 @@ private[spark] class Executor( | |
| val taskId = taskDescription.taskId | ||
| val threadName = s"Executor task launch worker for task $taskId" | ||
| val taskName = taskDescription.name | ||
| val mdcProperties = taskDescription.properties.asScala | ||
| .filter(_._1.startsWith("mdc.")).map { item => | ||
| val mdcProperties = (taskDescription.properties.asScala ++ | ||
| Seq((Executor.TASK_MDC_KEY, taskName))) | ||
| .filter(_._1.startsWith(Executor.MDC_KEY)).map { item => | ||
| val key = item._1.substring(4) | ||
| if (key == Executor.TASK_MDC_KEY && item._2 != taskName) { | ||
| logWarning(s"Override mdc.taskName is not allowed, ignore ${item._2}") | ||
| } | ||
| (key, item._2) | ||
| }.toSeq | ||
| }.toMap | ||
|
|
||
| /** If specified, this task has been killed and this option contains the reason. */ | ||
| @volatile private var reasonIfKilled: Option[String] = None | ||
|
|
@@ -401,9 +405,22 @@ private[spark] class Executor( | |
| } | ||
|
|
||
| override def run(): Unit = { | ||
| val oldMdcProperties = mdcProperties.keys.map(k => (k, MDC.get(k))) | ||
|
||
| try { | ||
| mdcProperties.foreach { case (k, v) => MDC.put(k, v) } | ||
| runInternal() | ||
| } finally { | ||
| oldMdcProperties.foreach { case (k, v) => | ||
| if (v == null) { | ||
| MDC.remove(k) | ||
| } else { | ||
| MDC.put(k, v) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| setMDCForTask(taskName, mdcProperties) | ||
|
|
||
| private def runInternal(): Unit = { | ||
| threadId = Thread.currentThread.getId | ||
| Thread.currentThread.setName(threadName) | ||
| val threadMXBean = ManagementFactory.getThreadMXBean | ||
|
|
@@ -702,14 +719,6 @@ private[spark] class Executor( | |
| } | ||
| } | ||
|
|
||
| private def setMDCForTask(taskName: String, mdc: Seq[(String, String)]): Unit = { | ||
| MDC.put("taskName", taskName) | ||
|
|
||
Ngone51 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mdc.foreach { case (key, value) => | ||
| MDC.put(key, value) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Supervises the killing / cancellation of a task by sending the interrupted flag, optionally | ||
| * sending a Thread.interrupt(), and monitoring the task until it finishes. | ||
|
|
@@ -750,9 +759,23 @@ private[spark] class Executor( | |
| private[this] val takeThreadDump: Boolean = conf.get(TASK_REAPER_THREAD_DUMP) | ||
|
|
||
| override def run(): Unit = { | ||
| val mdcProperties = taskRunner.mdcProperties | ||
| val oldMdcProperties = mdcProperties.keys.map(k => (k, MDC.get(k))) | ||
| try { | ||
| mdcProperties.foreach { case (k, v) => MDC.put(k, v) } | ||
| runInternal() | ||
| } finally { | ||
| oldMdcProperties.foreach { case (k, v) => | ||
| if (v == null) { | ||
| MDC.remove(k) | ||
| } else { | ||
| MDC.put(k, v) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| setMDCForTask(taskRunner.taskName, taskRunner.mdcProperties) | ||
|
|
||
| private def runInternal(): Unit = { | ||
| val startTimeNs = System.nanoTime() | ||
| def elapsedTimeNs = System.nanoTime() - startTimeNs | ||
| def timeoutExceeded(): Boolean = killTimeoutNs > 0 && elapsedTimeNs > killTimeoutNs | ||
|
|
@@ -969,4 +992,7 @@ private[spark] object Executor { | |
| // task is fully deserialized. When possible, the TaskContext.getLocalProperty call should be | ||
| // used instead. | ||
| val taskDeserializationProps: ThreadLocal[Properties] = new ThreadLocal[Properties] | ||
|
|
||
| val MDC_KEY = "mdc." | ||
| val TASK_MDC_KEY = s"${MDC_KEY}taskName" | ||
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we simply set the task mdc key at the end? then it will not be overwritten.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, we've already add the task mdc key at the new line 326. We can remove the warning if it's ok to override silently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the original
setMDCForTaskmethod looks OK as long as we set task mdc key at the end. It also helps avoid duplicated code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we do not let override the task name in MDC?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then our document is wrong. We must make sure
taskNamealways represent the value as we documented.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should let the user override it, and write to the log that is overridden.
(windows way is not let you do things, Linux way: with great power comes greater responsibility)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the benefit to let users override the task name? It's just confusing to me. Let's not support a non-existing use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the UI you can set the description I think many users can benefit from setting the taskName to be the same as that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then, they can use custom MDC properties instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok