Github user tnachen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10993#discussion_r52188015
  
    --- Diff: 
core/src/main/scala/org/apache/spark/scheduler/cluster/mesos/CoarseMesosSchedulerBackend.scala
 ---
    @@ -260,113 +257,208 @@ private[spark] class CoarseMesosSchedulerBackend(
             offers.asScala.map(_.getId).foreach(d.declineOffer)
             return
           }
    -      val filters = Filters.newBuilder().setRefuseSeconds(5).build()
    -      for (offer <- offers.asScala) {
    +
    +      logDebug(s"Received ${offers.size} resource offers.")
    +
    +      val (matchedOffers, unmatchedOffers) = offers.asScala.partition { 
offer =>
             val offerAttributes = toAttributeMap(offer.getAttributesList)
    -        val meetsConstraints = 
matchesAttributeRequirements(slaveOfferConstraints, offerAttributes)
    +        matchesAttributeRequirements(slaveOfferConstraints, 
offerAttributes)
    +      }
    +
    +      declineUnmatchedOffers(d, unmatchedOffers)
    +      handleMatchedOffers(d, matchedOffers)
    +    }
    +  }
    +
    +  private def declineUnmatchedOffers(d: SchedulerDriver, offers: 
Buffer[Offer]): Unit = {
    +    for (offer <- offers) {
    +      val id = offer.getId.getValue
    +      val offerAttributes = toAttributeMap(offer.getAttributesList)
    +      val mem = getResource(offer.getResourcesList, "mem")
    +      val cpus = getResource(offer.getResourcesList, "cpus")
    +      val filters = Filters.newBuilder()
    +        .setRefuseSeconds(rejectOfferDurationForUnmetConstraints).build()
    +
    +      logDebug(s"Declining offer: $id with attributes: $offerAttributes 
mem: $mem cpu: $cpus"
    +        + s" for $rejectOfferDurationForUnmetConstraints seconds")
    +
    +      d.declineOffer(offer.getId, filters)
    +    }
    +  }
    +
    +  /**
    +    * Launches executors on accepted offers, and declines unused offers. 
Executors are launched
    +    * round-robin on offers.
    +    *
    +    * @param d SchedulerDriver
    +    * @param offers Mesos offers that match attribute constraints
    +    */
    +  private def handleMatchedOffers(d: SchedulerDriver, offers: 
Buffer[Offer]): Unit = {
    +    val tasks = buildMesosTasks(offers)
    +    for (offer <- offers) {
    +      val offerAttributes = toAttributeMap(offer.getAttributesList)
    +      val offerMem = getResource(offer.getResourcesList, "mem")
    +      val offerCpus = getResource(offer.getResourcesList, "cpus")
    +      val id = offer.getId.getValue
    +
    +      if (tasks.contains(offer.getId)) { // accept
    +        val offerTasks = tasks(offer.getId)
    +
    +        logDebug(s"Accepting offer: $id with attributes: $offerAttributes 
" +
    +          s"mem: $offerMem cpu: $offerCpus.  Launching ${offerTasks.size} 
Mesos tasks.")
    +
    +        for (task <- offerTasks) {
    +          val taskId = task.getTaskId
    +          val mem = getResource(task.getResourcesList, "mem")
    +          val cpus = getResource(task.getResourcesList, "cpus")
    +
    +          logDebug(s"Launching Mesos task: ${taskId.getValue} with mem: 
$mem cpu: $cpus.")
    +        }
    +
    +        d.launchTasks(
    +          Collections.singleton(offer.getId),
    +          offerTasks.asJava)
    +      } else { // decline
    +        logDebug(s"Declining offer: $id with attributes: $offerAttributes 
" +
    +          s"mem: $offerMem cpu: $offerCpus")
    +
    +        d.declineOffer(offer.getId)
    +      }
    +    }
    +  }
    +
    +  /**
    +    * Returns a map from OfferIDs to the tasks to launch on those offers.  
In order to maximize
    +    * per-task memory and IO, tasks are round-robin assigned to offers.
    +    *
    +    * @param offers Mesos offers that match attribute constraints
    +    * @return A map from OfferID to a list of Mesos tasks to launch on 
that offer
    +    */
    +  private def buildMesosTasks(offers: Buffer[Offer]): Map[OfferID, 
List[MesosTaskInfo]] = {
    +    // offerID -> tasks
    +    val tasks = new HashMap[OfferID, 
List[MesosTaskInfo]].withDefaultValue(Nil)
    +
    +    // offerID -> resources
    +    val remainingResources = mutable.Map(offers.map(offer =>
    +      (offer.getId.getValue, offer.getResourcesList)): _*)
    +
    +    var launchTasks = true
    +
    +    // TODO(mgummelt): combine offers for a single slave
    +    //
    +    // round-robin create executors on the available offers
    +    while (launchTasks) {
    +      launchTasks = false
    +
    +      for (offer <- offers) {
             val slaveId = offer.getSlaveId.getValue
    -        val mem = getResource(offer.getResourcesList, "mem")
    -        val cpus = getResource(offer.getResourcesList, "cpus").toInt
    -        val id = offer.getId.getValue
    -        if (meetsConstraints) {
    -          if (taskIdToSlaveId.size < executorLimit &&
    -              totalCoresAcquired < maxCores &&
    -              mem >= calculateTotalMemory(sc) &&
    -              cpus >= 1 &&
    -              failuresBySlaveId.getOrElse(slaveId, 0) < MAX_SLAVE_FAILURES 
&&
    -              !slaveIdsWithExecutors.contains(slaveId)) {
    -            // Launch an executor on the slave
    -            val cpusToUse = math.min(cpus, maxCores - totalCoresAcquired)
    -            totalCoresAcquired += cpusToUse
    -            val taskId = newMesosTaskId()
    -            taskIdToSlaveId.put(taskId, slaveId)
    -            slaveIdsWithExecutors += slaveId
    -            coresByTaskId(taskId) = cpusToUse
    -            // Gather cpu resources from the available resources and use 
them in the task.
    -            val (remainingResources, cpuResourcesToUse) =
    -              partitionResources(offer.getResourcesList, "cpus", cpusToUse)
    -            val (_, memResourcesToUse) =
    -              partitionResources(remainingResources.asJava, "mem", 
calculateTotalMemory(sc))
    -            val taskBuilder = MesosTaskInfo.newBuilder()
    -              
.setTaskId(TaskID.newBuilder().setValue(taskId.toString).build())
    -              .setSlaveId(offer.getSlaveId)
    -              .setCommand(createCommand(offer, cpusToUse + 
extraCoresPerSlave, taskId))
    -              .setName("Task " + taskId)
    -              .addAllResources(cpuResourcesToUse.asJava)
    -              .addAllResources(memResourcesToUse.asJava)
    -
    -            sc.conf.getOption("spark.mesos.executor.docker.image").foreach 
{ image =>
    -              MesosSchedulerBackendUtil
    -                .setupContainerBuilderDockerInfo(image, sc.conf, 
taskBuilder.getContainerBuilder())
    -            }
    -
    -            // Accept the offer and launch the task
    -            logDebug(s"Accepting offer: $id with attributes: 
$offerAttributes mem: $mem cpu: $cpus")
    -            slaveIdToHost(offer.getSlaveId.getValue) = offer.getHostname
    -            d.launchTasks(
    -              Collections.singleton(offer.getId),
    -              Collections.singleton(taskBuilder.build()), filters)
    -          } else {
    -            // Decline the offer
    -            logDebug(s"Declining offer: $id with attributes: 
$offerAttributes mem: $mem cpu: $cpus")
    -            d.declineOffer(offer.getId)
    +        val offerId = offer.getId.getValue
    +        val resources = remainingResources(offerId)
    +
    +        if (canLaunchTask(slaveId, resources)) {
    +          // Create a task
    +          launchTasks = true
    +          val taskId = newMesosTaskId()
    +          val offerCPUs = getResource(resources, "cpus").toInt
    +
    +          val taskCPUs = executorCores(offerCPUs)
    +          val taskMemory = executorMemory(sc)
    +
    +          slaves.getOrElseUpdate(slaveId, new 
Slave(offer.getHostname)).taskIDs.add(taskId)
    +
    +          val (afterCPUResources, cpuResourcesToUse) =
    +            partitionResources(resources, "cpus", taskCPUs)
    +          val (resourcesLeft, memResourcesToUse) =
    +            partitionResources(afterCPUResources.asJava, "mem", taskMemory)
    +
    +          val taskBuilder = MesosTaskInfo.newBuilder()
    +            
.setTaskId(TaskID.newBuilder().setValue(taskId.toString).build())
    +            .setSlaveId(offer.getSlaveId)
    +            .setCommand(createCommand(offer, taskCPUs + 
extraCoresPerExecutor, taskId))
    +            .setName("Task " + taskId)
    +            .addAllResources(cpuResourcesToUse.asJava)
    +            .addAllResources(memResourcesToUse.asJava)
    +
    +          sc.conf.getOption("spark.mesos.executor.docker.image").foreach { 
image =>
    +            MesosSchedulerBackendUtil
    +              .setupContainerBuilderDockerInfo(image, sc.conf, 
taskBuilder.getContainerBuilder)
               }
    -        } else {
    -          // This offer does not meet constraints. We don't need to see it 
again.
    -          // Decline the offer for a long period of time.
    -          logDebug(s"Declining offer: $id with attributes: 
$offerAttributes mem: $mem cpu: $cpus"
    -              + s" for $rejectOfferDurationForUnmetConstraints seconds")
    -          d.declineOffer(offer.getId, Filters.newBuilder()
    -            
.setRefuseSeconds(rejectOfferDurationForUnmetConstraints).build())
    +
    +          tasks(offer.getId) ::= taskBuilder.build()
    +          remainingResources(offerId) = resourcesLeft.asJava
    +          totalCoresAcquired += taskCPUs
    +          coresByTaskId(taskId) = taskCPUs
             }
           }
         }
    +    tasks.toMap
    +  }
    +
    +  private def canLaunchTask(slaveId: String, resources: JList[Resource]): 
Boolean = {
    +    val offerMem = getResource(resources, "mem")
    +    val offerCPUs = getResource(resources, "cpus").toInt
    +    val cpus = executorCores(offerCPUs)
    +    val mem = executorMemory(sc)
    +
    +    cpus > 0 &&
    +      cpus <= offerCPUs &&
    +      cpus + totalCoresAcquired <= maxCores &&
    +      mem <= offerMem &&
    +      numExecutors() < executorLimit &&
    +      slaves.get(slaveId).map(_.taskFailures).getOrElse(0) < 
MAX_SLAVE_FAILURES
       }
     
    +  private def executorCores(offerCPUs: Int): Int = {
    +    sc.conf.getInt("spark.executor.cores",
    +      math.min(offerCPUs, maxCores - totalCoresAcquired))
    +  }
     
       override def statusUpdate(d: SchedulerDriver, status: TaskStatus) {
    -    val taskId = status.getTaskId.getValue.toInt
    -    val state = status.getState
    -    logInfo(s"Mesos task $taskId is now $state")
    -    val slaveId: String = status.getSlaveId.getValue
    +    val taskId = status.getTaskId.getValue
    +    val slaveId = status.getSlaveId.getValue
    +    val slave = slaves(slaveId)
    --- End diff --
    
    This needs to be moved in the stateLock right?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to