peter-toth commented on code in PR #58714:
URL: https://github.com/apache/spark/pull/58714#discussion_r4542603736


##########
core/src/main/scala/org/apache/spark/scheduler/TaskSchedulerImpl.scala:
##########
@@ -439,6 +465,192 @@ private[spark] class TaskSchedulerImpl(
     logInfo(log"Removed TaskSet " + manager.taskSet.logId +
       log" whose tasks have all completed, from pool ${MDC(LogKeys.POOL_NAME, 
manager.parent.name)}"
     )
+    if (releasedOomReservation) {
+      // A cancelled pending retry has no task exit to wake work held behind 
its reservation.
+      backend.reviveOffers()
+    }
+  }
+
+  private def setOomRetryReservation(reservation: 
Option[OomRetryReservation]): Unit = {
+    oomRetryReservation = reservation
+    // Publish an immutable snapshot without calling into the allocation 
manager: it can already
+    // hold its lock while acquiring the scheduler lock through the backend.
+    oomRetryReservationSnapshot = reservation.map { r =>
+      val taskSet = r.taskSet.taskSet
+      OomRetryReservationInfo(taskSet.resourceProfileId, r.executorId,
+        taskSet.stageId, taskSet.stageAttemptId, r.index)
+    }
+  }
+
+  private def refreshOomRetryReservation(): Boolean = {
+    val hadReservation = oomRetryReservation.nonEmpty
+    setOomRetryReservation(oomRetryReservation.filter { reservation =>
+      reservation.taskId match {
+        // Cancellation or another attempt's success can precede the task's 
exit. Keep its
+        // executor isolated until the terminal status update releases the 
running task.
+        case Some(tid) => 
taskIdToExecutorId.get(tid).contains(reservation.executorId)
+        case None =>
+          isExecutorAlive(reservation.executorId) &&
+            executorIdToHost.get(reservation.executorId).exists { host =>
+              !healthTrackerOpt.exists { tracker =>
+                tracker.isExecutorExcluded(reservation.executorId) || 
tracker.isNodeExcluded(host)
+              } && reservation.taskSet.canRunOomRetry(
+                reservation.index, reservation.executorId, host)
+            } && reservation.taskSet.oomRetryNeedsIsolation(reservation.index)
+      }
+    })
+    hadReservation && oomRetryReservation.isEmpty
+  }
+
+  /**
+   * Give OOM retries idle capacity before ordinary placement. Repeated OOMs 
reserve at most one
+   * executor across offer rounds, allowing existing tasks to drain without 
backfilling it. Only
+   * this recovery pass ignores locality preferences; resource requirements 
and exclusions remain
+   * mandatory. A bounded wait in TaskSetManager makes an unavailable 
reservation fall back to
+   * ordinary scheduling instead of turning an OOM into an indefinitely 
pending retry.
+   */
+  private def scheduleOomRetries(
+      taskSets: ArrayBuffer[TaskSetManager],
+      offers: IndexedSeq[WorkerOffer],
+      isAllFreeResources: Boolean,
+      availableCpus: Array[BigDecimal],
+      availableResources: Array[ExecutorResourcesAmounts],
+      tasks: IndexedSeq[ArrayBuffer[TaskDescription]]): Set[TaskSetManager] = {
+    refreshOomRetryReservation()
+    val launchedTaskSets = new HashSet[TaskSetManager]
+    def taskCpusFor(taskSet: TaskSetManager): BigDecimal = {
+      val profile = sc.resourceProfileManager
+        .resourceProfileFromId(taskSet.taskSet.resourceProfileId)
+      ResourceProfile.getTaskCpusOrDefaultForProfile(profile, conf)
+    }
+
+    // A compatible profile does not guarantee that every executor has enough 
total resources.
+    // Once a reserved executor is idle, its offer tells us whether the retry 
can actually fit.
+    setOomRetryReservation(oomRetryReservation.filterNot { reservation =>
+      reservation.taskId.isEmpty && offers.indices.exists { i =>
+        offers(i).executorId == reservation.executorId &&
+          !isExecutorBusy(reservation.executorId) &&
+          resourcesMeetTaskRequirements(
+            reservation.taskSet, taskCpusFor(reservation.taskSet),
+            availableCpus(i), availableResources(i)).isEmpty
+      }
+    })
+
+    def eligible(taskSet: TaskSetManager, index: Int, i: Int): Boolean = {
+      val offer = offers(i)
+      isExecutorAlive(offer.executorId) &&
+        sc.resourceProfileManager.canBeScheduled(
+          taskSet.taskSet.resourceProfileId, offer.resourceProfileId) &&
+        taskSet.canRunOomRetry(index, offer.executorId, offer.host)
+    }
+
+    def launch(taskSet: TaskSetManager, index: Int, i: Int): Option[Long] = {
+      val offer = offers(i)
+      if (!eligible(taskSet, index, i) || isExecutorBusy(offer.executorId)) {
+        return None
+      }
+      resourcesMeetTaskRequirements(
+        taskSet, taskCpusFor(taskSet), availableCpus(i), 
availableResources(i)).flatMap {
+        assignments =>
+          val taskCpus = taskCpusFor(taskSet)
+          try {
+            taskSet.resourceOfferOomRetry(
+              index, offer.executorId, offer.host, taskCpus, assignments)
+              .map { task =>
+                tasks(i) += task
+                addRunningTask(task.taskId, offer.executorId, taskSet)
+                launchedTaskSets += taskSet
+                availableCpus(i) -= task.cpus
+                availableResources(i).acquire(task.resources)
+                task.taskId
+              }
+          } catch {
+            case e: TaskNotSerializableException =>
+              // prepareLaunchingTask already aborts the task set on 
serialization failure.
+              logError("Failed to serialize an OOM retry", e)
+              None
+          }
+      }
+    }
+
+    val pendingRetries = taskSets.flatMap { taskSet =>
+      taskSet.pendingOomRetries.map(index => (taskSet, index))
+    }
+    // A partial round may omit a better executor. Wait for a full round to 
choose a new
+    // reservation, but continue draining and launching an existing one on 
partial offers.
+    if (isAllFreeResources && oomRetryReservation.isEmpty) {

Review Comment:
   **Finding 2.** A reservation is chosen only while none exists. It then stays 
until its retry launches, its executor goes away or the wait expires. So once a 
busy executor is picked, the retry waits for it even after another eligible 
executor goes idle. The idle pass serves only first-OOM retries.
   
   What I ran: a probe in `TaskSchedulerImplSuite` with a `ManualClock`. 
Executor `a` runs one task and `b` runs two, then a task OOMs twice.
   - The next full round reserves `a`, since it has fewer running tasks.
   - `b`'s tasks finish. In the following rounds `b` stays idle, nothing 
launches, and the reservation stays on `a`.
   - At +61 s the reservation expires and the retry runs on `b` as an ordinary 
task.
   
   The same happens when one task on the reserved executor runs longer than 
`isolationTimeout`. While the reservation has no task yet, a full round could 
run the selection again and move it to an eligible executor that is idle and 
fits.
   



##########
resource-managers/yarn/src/main/scala/org/apache/spark/deploy/yarn/YarnAllocator.scala:
##########
@@ -933,7 +933,11 @@ private[yarn] class YarnAllocator(
         } else {
           logInfo(containerExitReason)
         }
-        ExecutorExited(exitStatus, exitCausedByApp, 
containerExitReason.message)
+        val reason = ExecutorExited(exitStatus, exitCausedByApp, 
containerExitReason.message)
+        if (exitCausedByApp && exitStatus == 
ContainerExitStatus.KILLED_EXCEEDED_PMEM) {

Review Comment:
   **Finding 3.** This classifies YARN memory-limit kills. I don't think a heap 
OOM reaches the driver as an OOM on YARN, though. 
`spark.yarn.executor.oomKill.enabled` is true by default, and then 
`ExecutorRunnable.scala:194-196` adds `-XX:OnOutOfMemoryError='kill %p'`. So 
the JVM sends itself SIGTERM when it throws the error. Then:
   - The task's `ExceptionFailure` is usually not sent. `Executor.scala:1118` 
skips the report once shutdown has started.
   - The container exits with 143, which this PR doesn't classify. So the tasks 
lost with it get `isOutOfMemoryError = false`.
   
   This is from reading only. A YARN run with a task that fills the heap would 
settle it, by checking the `TaskEnd` reason on the driver. If it holds, the 
config doc could say that on YARN a heap OOM is recognized only with 
`spark.yarn.executor.oomKill.enabled=false`. Or the policy could leave out the 
`kill %p` option when it is enabled. The standalone gap is the one at 
[r4541465279](https://github.com/apache/spark/pull/58714#discussion_r4541465279).
   



-- 
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]

Reply via email to