<commit_before>package mesosphere.marathon.upgrade

import mesosphere.marathon.api.v1.AppDefinition
import mesosphere.marathon.api.v2.{ScalingStrategy, Group}
import mesosphere.marathon.state.{Timestamp, MarathonState}
import mesosphere.marathon.Protos.DeploymentPlanDefinition
import scala.collection.JavaConversions._
import scala.concurrent.Future
import mesosphere.marathon.MarathonSchedulerService
import scala.concurrent.ExecutionContext.Implicits.global

case class DeploymentPlan(
  id: String,
  strategy: ScalingStrategy,
  original: List[AppDefinition],
  target: List[AppDefinition],
  version : Timestamp = Timestamp.now()
) extends MarathonState[DeploymentPlanDefinition, DeploymentPlan] {

  def originalIds : Set[String] = original.map(_.id).toSet

  def targetIds : Set[String] = target.map(_.id).toSet

  override def mergeFromProto(bytes: Array[Byte]): DeploymentPlan = mergeFromProto(DeploymentPlanDefinition.parseFrom(bytes))

  override def mergeFromProto(msg: DeploymentPlanDefinition): DeploymentPlan = DeploymentPlan(
    msg.getId,
    ScalingStrategy.fromProto(msg.getStrategy),
    msg.getOrigList.map(AppDefinition.fromProto).toList,
    msg.getTargetList.map(AppDefinition.fromProto).toList,
    Timestamp(msg.getVersion)
  )

  override def toProto: DeploymentPlanDefinition = {
    val builder = DeploymentPlanDefinition.newBuilder()
      .setId(id)
      .setVersion(version.toString)
      .setStrategy(strategy.toProto)
      .addAllOrig(original.map(_.toProto))
      .addAllTarget(target.map(_.toProto))
    builder.build()
  }

  def deploy(scheduler: MarathonSchedulerService) : Future[Boolean] = {
    val toRestart = targetIds.intersect(originalIds)
    val toStart = targetIds.filterNot(toRestart.contains)
    val toStop = originalIds.filterNot(toRestart.contains)
    val restartFuture = toRestart.flatMap(id => original.find(_.id==id)).map { app =>
      scheduler.upgradeApp(app, (app.instances * strategy.minimumHealthCapacity).toInt)
    }
    val startFuture = toStart.flatMap(id => target.find(_.id==id)).map(scheduler.startApp(_).map(_ => true))
    val stopFuture = toStop.flatMap(id => original.find(_.id==id)).map(scheduler.stopApp(_).map(_ => true))
    Future.sequence(restartFuture ++ startFuture ++ stopFuture).map(_.forall(identity))
  }
}

object DeploymentPlan {
  def apply(id:String, oldProduct:Group, newProduct:Group, runningTasks:Map[String, List[String]]) : DeploymentPlan = {
    DeploymentPlan(id, newProduct.scalingStrategy, oldProduct.apps.toList, newProduct.apps.toList)
  }

  def empty() = DeploymentPlan("", ScalingStrategy(0), Nil, Nil)
}<commit_msg>restart new app, not original one<commit_after>package mesosphere.marathon.upgrade

import mesosphere.marathon.api.v1.AppDefinition
import mesosphere.marathon.api.v2.{ScalingStrategy, Group}
import mesosphere.marathon.state.{Timestamp, MarathonState}
import mesosphere.marathon.Protos.DeploymentPlanDefinition
import scala.collection.JavaConversions._
import scala.concurrent.Future
import mesosphere.marathon.MarathonSchedulerService
import scala.concurrent.ExecutionContext.Implicits.global

case class DeploymentPlan(
  id: String,
  strategy: ScalingStrategy,
  original: List[AppDefinition],
  target: List[AppDefinition],
  version : Timestamp = Timestamp.now()
) extends MarathonState[DeploymentPlanDefinition, DeploymentPlan] {

  def originalIds : Set[String] = original.map(_.id).toSet

  def targetIds : Set[String] = target.map(_.id).toSet

  override def mergeFromProto(bytes: Array[Byte]): DeploymentPlan = mergeFromProto(DeploymentPlanDefinition.parseFrom(bytes))

  override def mergeFromProto(msg: DeploymentPlanDefinition): DeploymentPlan = DeploymentPlan(
    msg.getId,
    ScalingStrategy.fromProto(msg.getStrategy),
    msg.getOrigList.map(AppDefinition.fromProto).toList,
    msg.getTargetList.map(AppDefinition.fromProto).toList,
    Timestamp(msg.getVersion)
  )

  override def toProto: DeploymentPlanDefinition = {
    val builder = DeploymentPlanDefinition.newBuilder()
      .setId(id)
      .setVersion(version.toString)
      .setStrategy(strategy.toProto)
      .addAllOrig(original.map(_.toProto))
      .addAllTarget(target.map(_.toProto))
    builder.build()
  }

  def deploy(scheduler: MarathonSchedulerService) : Future[Boolean] = {
    val toRestart = targetIds.intersect(originalIds)
    val toStart = targetIds.filterNot(toRestart.contains)
    val toStop = originalIds.filterNot(toRestart.contains)
    val restartFuture = toRestart.flatMap(id => target.find(_.id==id)).map { app =>
      scheduler.upgradeApp(app, (app.instances * strategy.minimumHealthCapacity).toInt)
    }
    val startFuture = toStart.flatMap(id => target.find(_.id==id)).map(scheduler.startApp(_).map(_ => true))
    val stopFuture = toStop.flatMap(id => original.find(_.id==id)).map(scheduler.stopApp(_).map(_ => true))
    Future.sequence(restartFuture ++ startFuture ++ stopFuture).map(_.forall(identity))
  }
}

object DeploymentPlan {
  def apply(id:String, oldProduct:Group, newProduct:Group, runningTasks:Map[String, List[String]]) : DeploymentPlan = {
    DeploymentPlan(id, newProduct.scalingStrategy, oldProduct.apps.toList, newProduct.apps.toList)
  }

  def empty() = DeploymentPlan("", ScalingStrategy(0), Nil, Nil)
}
{'fraction_non_alphanumeric': 0.11703338391502276, 'fraction_numerical': 0.0011380880121396055, 'mean_word_length': 5.414841849148418, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6201858', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1590, 'n_words': 261}