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

    https://github.com/apache/spark/pull/10993#discussion_r51594185
  
    --- Diff: 
core/src/main/scala/org/apache/spark/scheduler/cluster/mesos/CoarseMesosSchedulerBackend.scala
 ---
    @@ -245,99 +239,182 @@ private[spark] class CoarseMesosSchedulerBackend(
        */
       override def resourceOffers(d: SchedulerDriver, offers: JList[Offer]) {
         stateLock.synchronized {
    -      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]) {
    +    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)
    +    }
    +  }
    +
    +  private def handleMatchedOffers(d: SchedulerDriver, offers: 
Buffer[Offer]) {
    +    val tasks = getTasks(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 filters = Filters.newBuilder().setRefuseSeconds(5).build()
    +        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,
    +          filters)
    +      } else { // decline
    +        logDebug(s"Declining offer: $id with attributes: $offerAttributes 
" +
    +          s"mem: $offerMem cpu: $offerCpus")
    +
    +        d.declineOffer(offer.getId)
    +      }
    +    }
    +  }
    +
    +  private def getTasks(offers: Buffer[Offer]): mutable.Map[OfferID, 
List[MesosTaskInfo]] = {
    +    // offerID -> tasks
    +    val tasks = new HashMap[OfferID, 
List[MesosTaskInfo]].withDefaultValue(Nil)
    +
    +    // offerID -> resources
    +    val remainingResources = HashMap[String, 
JList[Resource]](offers.map(offer =>
    +      (offer.getId.getValue, offer.getResourcesList)): _*)
    --- End diff --
    
    A simpler way to do the same thing: `offers.map(offer => 
(offer.getId.getValue, offer.getResourcesList)).toMap`


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to