text:
package org.apache.spark.scheduler

import java.io.NotSerializableException
import java.nio.ByteBuffer
import java.util.Arrays
import java.util.concurrent.ConcurrentLinkedQueue

import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.HashMap
import scala.collection.mutable.HashSet
import scala.math.{max, min}
import scala.util.control.NonFatal

import org.apache.spark._
import org.apache.spark.internal.Logging
import org.apache.spark.scheduler.SchedulingMode._
import org.apache.spark.TaskState.TaskState
import org.apache.spark.util.{AccumulatorV2, Clock, SystemClock, Utils}

/**
 * Schedules the tasks within a single TaskSet in the TaskSchedulerImpl. This class keeps track of
 * each task, retries tasks if they fail (up to a limited number of times), and
 * handles locality-aware scheduling for this TaskSet via delay scheduling. The main interfaces
 * to it are resourceOffer, which asks the TaskSet whether it wants to run a task on one node,
 * and statusUpdate, which tells it that one of its tasks changed state (e.g. finished).
 *
 * THREADING: This class is designed to only be called from code with a lock on the
 * TaskScheduler (e.g. its event handlers). It should not be called from other threads.
 *
 * @param sched           the TaskSchedulerImpl associated with the TaskSetManager
 * @param taskSet         the TaskSet to manage scheduling for
 * @param maxTaskFailures if any particular task fails this number of times, the entire
 *                        task set will be aborted
 */
private[spark] class TaskSetManager(
    sched: TaskSchedulerImpl,
    val taskSet: TaskSet,
    val maxTaskFailures: Int,
    clock: Clock = new SystemClock())
  extends Schedulable with Logging {

  val conf = sched.sc.conf

  /*
   * Sometimes if an executor is dead or in an otherwise invalid state, the driver
   * does not realize right away leading to repeated task failures. If enabled,
   * this temporarily prevents a task from re-launching on an executor where
   * it just failed.
   */
  private val EXECUTOR_TASK_BLACKLIST_TIMEOUT =
    conf.getLong("spark.scheduler.executorTaskBlacklistTime", 0L)

  // Quantile of tasks at which to start speculation
  val SPECULATION_QUANTILE = conf.getDouble("spark.speculation.quantile", 0.75)
  val SPECULATION_MULTIPLIER = conf.getDouble("spark.speculation.multiplier", 1.5)

  // Limit of bytes for total size of results (default is 1GB)
  val maxResultSize = Utils.getMaxResultSize(conf)

  // Serializer for closures and tasks.
  val env = SparkEnv.get
  val ser = env.closureSerializer.newInstance()

  val tasks = taskSet.tasks
  val numTasks = tasks.length
  val copiesRunning = new Array[Int](numTasks)
  val successful = new Array[Boolean](numTasks)
  private val numFailures = new Array[Int](numTasks)
  // key is taskId, value is a Map of executor id to when it failed
  private val failedExecutors = new HashMap[Int, HashMap[String, Long]]()

  val taskAttempts = Array.fill[List[TaskInfo]](numTasks)(Nil)
  var tasksSuccessful = 0

  var weight = 1
  var minShare = 0
  var priority = taskSet.priority
  var stageId = taskSet.stageId
  var name = "TaskSet_" + taskSet.stageId.toString
  var parent: Pool = null
  var totalResultSize = 0L
  var calculatedTasks = 0

  val runningTasksSet = new HashSet[Long]

  override def runningTasks: Int = runningTasksSet.size

  // True once no more tasks should be launched for this task set manager. TaskSetManagers enter
  // the zombie state once at least one attempt of each task has completed successfully, or if the
  // task set is aborted (for example, because it was killed).  TaskSetManagers remain in the zombie
  // state until all tasks have finished running; we keep TaskSetManagers that are in the zombie
  // state in order to continue to track and account for the running tasks.
  // TODO: We should kill any running task attempts when the task set manager becomes a zombie.
  var isZombie = false

  // Set of pending tasks for each executor. These collections are actually
  // treated as stacks, in which new tasks are added to the end of the
  // ArrayBuffer and removed from the end. This makes it faster to detect
  // tasks that repeatedly fail because whenever a task failed, it is put
  // back at the head of the stack. These collections may contain duplicates
  // for two reasons:
  // (1): Tasks are only removed lazily; when a task is launched, it remains
  // in all the pending lists except the one that it was launched from.
  // (2): Tasks may be re-added to these lists multiple times as a result
  // of failures.
  // Duplicates are handled in dequeueTaskFromList, which ensures that a
  // task hasn't already started running before launching it.
  private val pendingTasksForExecutor = new HashMap[String, ArrayBuffer[Int]]

  // Set of pending tasks for each host. Similar to pendingTasksForExecutor,
  // but at host level.
  private val pendingTasksForHost = new HashMap[String, ArrayBuffer[Int]]

  // Set of pending tasks for each rack -- similar to the above.
  private val pendingTasksForRack = new HashMap[String, ArrayBuffer[Int]]

  // Set containing pending tasks with no locality preferences.
  var pendingTasksWithNoPrefs = new ArrayBuffer[Int]

  // Set containing all pending tasks (also used as a stack, as above).
  val allPendingTasks = new ArrayBuffer[Int]

  // Tasks that can be speculated. Since these will be a small fraction of total
  // tasks, we'll just hold them in a HashSet.
  val speculatableTasks = new HashSet[Int]

  // Task index, start and finish time for each task attempt (indexed by task ID)
  val taskInfos = new HashMap[Long, TaskInfo]

  // How frequently to reprint duplicate exceptions in full, in milliseconds
  val EXCEPTION_PRINT_INTERVAL =
    conf.getLong("spark.logging.exceptionPrintInterval", 10000)

  // Map of recent exceptions (identified by string representation and top stack frame) to
  // duplicate count (how many times the same exception has appeared) and time the full exception
  // was printed. This should ideally be an LRU map that can drop old exceptions automatically.
  val recentExceptions = HashMap[String, (Int, Long)]()

  // Figure out the current map output tracker epoch and set it on all tasks
  val epoch = sched.mapOutputTracker.getEpoch
  logDebug("Epoch for " + taskSet + ": " + epoch)
  for (t <- tasks) {
    t.epoch = epoch
  }

  // Add all our tasks to the pending lists. We do this in reverse order
  // of task index so that tasks with low indices get launched first.
  for (i <- (0 until numTasks).reverse) {
    addPendingTask(i)
  }

  // Figure out which locality levels we have in our TaskSet, so we can do delay scheduling
  var myLocalityLevels = computeValidLocalityLevels()
  var localityWaits = myLocalityLevels.map(getLocalityWait) // Time to wait at each level

  // Delay scheduling variables: we keep track of our current locality level and the time we
  // last launched a task at that level, and move up a level when localityWaits[curLevel] expires.
  // We then move down if we manage to launch a "more local" task.
  var currentLocalityIndex = 0    // Index of our current locality level in validLocalityLevels
  var lastLaunchTime = clock.getTimeMillis()  // Time we last launched a task at this level

  override def schedulableQueue: ConcurrentLinkedQueue[Schedulable] = null

  override def schedulingMode: SchedulingMode = SchedulingMode.NONE

  var emittedTaskSizeWarning = false

  /** Add a task to all the pending-task lists that it should be on. */
  private def addPendingTask(index: Int) {
    for (loc <- tasks(index).preferredLocations) {
      loc match {
        case e: ExecutorCacheTaskLocation =>
          pendingTasksForExecutor.getOrElseUpdate(e.executorId, new ArrayBuffer) += index
        case e: HDFSCacheTaskLocation =>
          val exe = sched.getExecutorsAliveOnHost(loc.host)
          exe match {
            case Some(set) =>
              for (e <- set) {
                pendingTasksForExecutor.getOrElseUpdate(e, new ArrayBuffer) += index
              }
              logInfo(s"Pending task $index has a cached location at ${e.host} " +
                ", where there are executors " + set.mkString(","))
            case None => logDebug(s"Pending task $index has a cached location at ${e.host} " +
                ", but there are no executors alive there.")
          }
        case _ =>
      }
      pendingTasksForHost.getOrElseUpdate(loc.host, new ArrayBuffer) += index
      for (rack <- sched.getRackForHost(loc.host)) {
        pendingTasksForRack.getOrElseUpdate(rack, new ArrayBuffer) += index
      }
    }

    if (tasks(index).preferredLocations == Nil) {
      pendingTasksWithNoPrefs += index
    }

    allPendingTasks += index  // No point scanning this whole list to find the old task there
  }

  /**
   * Return the pending tasks list for a given executor ID, or an empty list if
   * there is no map entry for that host
   */
  private def getPendingTasksForExecutor(executorId: String): ArrayBuffer[Int] = {
    pendingTasksForExecutor.getOrElse(executorId, ArrayBuffer())
  }

  /**
   * Return the pending tasks list for a given host, or an empty list if
   * there is no map entry for that host
   */
  private def getPendingTasksForHost(host: String): ArrayBuffer[Int] = {
    pendingTasksForHost.getOrElse(host, ArrayBuffer())
  }

  /**
   * Return the pending rack-local task list for a given rack, or an empty list if
   * there is no map entry for that rack
   */
  private def getPendingTasksForRack(rack: String): ArrayBuffer[Int] = {
    pendingTasksForRack.getOrElse(rack, ArrayBuffer())
  }

  /**
   * Dequeue a pending task from the given list and return its index.
   * Return None if the list is empty.
   * This method also cleans up any tasks in the list that have already
   * been launched, since we want that to happen lazily.
   */
  private def dequeueTaskFromList(execId: String, list: ArrayBuffer[Int]): Option[Int] = {
    var indexOffset = list.size
    while (indexOffset > 0) {
      indexOffset -= 1
      val index = list(indexOffset)
      if (!executorIsBlacklisted(execId, index)) {
        // This should almost always be list.trimEnd(1) to remove tail
        list.remove(indexOffset)
        if (copiesRunning(index) == 0 && !successful(index)) {
          return Some(index)
        }
      }
    }
    None
  }

  /** Check whether a task is currently running an attempt on a given host */
  private def hasAttemptOnHost(taskIndex: Int, host: String): Boolean = {
    taskAttempts(taskIndex).exists(_.host == host)
  }

  /**
   * Is this re-execution of a failed task on an executor it already failed in before
   * EXECUTOR_TASK_BLACKLIST_TIMEOUT has elapsed ?
   */
  private def executorIsBlacklisted(execId: String, taskId: Int): Boolean = {
    if (failedExecutors.contains(taskId)) {
      val failed = failedExecutors.get(taskId).get

      return failed.contains(execId) &&
        clock.getTimeMillis() - failed.get(execId).get < EXECUTOR_TASK_BLACKLIST_TIMEOUT
    }

    false
  }

  /**
   * Return a speculative task for a given executor if any are available. The task should not have
   * an attempt running on this host, in case the host is slow. In addition, the task should meet
   * the given locality constraint.
   */
  // Labeled as protected to allow tests to override providing speculative tasks if necessary
  protected def dequeueSpeculativeTask(execId: String, host: String, locality: TaskLocality.Value)
    : Option[(Int, TaskLocality.Value)] =
  {
    speculatableTasks.retain(index => !successful(index)) // Remove finished tasks from set

    def canRunOnHost(index: Int): Boolean =
      !hasAttemptOnHost(index, host) && !executorIsBlacklisted(execId, index)

    if (!speculatableTasks.isEmpty) {
      // Check for process-local tasks; note that tasks can be process-local
      // on multiple nodes when we replicate cached blocks, as in Spark Streaming
      for (index <- speculatableTasks if canRunOnHost(index)) {
        val prefs = tasks(index).preferredLocations
        val executors = prefs.flatMap(_ match {
          case e: ExecutorCacheTaskLocation => Some(e.executorId)
          case _ => None
        });
        if (executors.contains(execId)) {
          speculatableTasks -= index
          return Some((index, TaskLocality.PROCESS_LOCAL))
        }
      }

      // Check for node-local tasks
      if (TaskLocality.isAllowed(locality, TaskLocality.NODE_LOCAL)) {
        for (index <- speculatableTasks if canRunOnHost(index)) {
          val locations = tasks(index).preferredLocations.map(_.host)
          if (locations.contains(host)) {
            speculatableTasks -= index
            return Some((index, TaskLocality.NODE_LOCAL))
          }
        }
      }

      // Check for no-preference tasks
      if (TaskLocality.isAllowed(locality, TaskLocality.NO_PREF)) {
        for (index <- speculatableTasks if canRunOnHost(index)) {
          val locations = tasks(index).preferredLocations
          if (locations.size == 0) {
            speculatableTasks -= index
            return Some((index, TaskLocality.PROCESS_LOCAL))
          }
        }
      }

      // Check for rack-local tasks
      if (TaskLocality.isAllowed(locality, TaskLocality.RACK_LOCAL)) {
        for (rack <- sched.getRackForHost(host)) {
          for (index <- speculatableTasks if canRunOnHost(index)) {
            val racks = tasks(index).preferredLocations.map(_.host).flatMap(sched.getRackForHost)
            if (racks.contains(rack)) {
              speculatableTasks -= index
              return Some((index, TaskLocality.RACK_LOCAL))
            }
          }
        }
      }

      // Check for non-local tasks
      if (TaskLocality.isAllowed(locality, TaskLocality.ANY)) {
        for (index <- speculatableTasks if canRunOnHost(index)) {
          speculatableTasks -= index
          return Some((index, TaskLocality.ANY))
        }
      }
    }

    None
  }

  /**
   * Dequeue a pending task for a given node and return its index and locality level.
   * Only search for tasks matching the given locality constraint.
   *
   * @return An option containing (task index within the task set, locality, is speculative?)
   */
  private def dequeueTask(execId: String, host: String, maxLocality: TaskLocality.Value)
    : Option[(Int, TaskLocality.Value, Boolean)] =
  {
    for (index <- dequeueTaskFromList(execId, getPendingTasksForExecutor(execId))) {
      return Some((index, TaskLocality.PROCESS_LOCAL, false))
    }

    if (TaskLocality.isAllowed(maxLocality, TaskLocality.NODE_LOCAL)) {
      for (index <- dequeueTaskFromList(execId, getPendingTasksForHost(host))) {
        return Some((index, TaskLocality.NODE_LOCAL, false))
      }
    }

    if (TaskLocality.isAllowed(maxLocality, TaskLocality.NO_PREF)) {
      // Look for noPref tasks after NODE_LOCAL for minimize cross-rack traffic
      for (index <- dequeueTaskFromList(execId, pendingTasksWithNoPrefs)) {
        return Some((index, TaskLocality.PROCESS_LOCAL, false))
      }
    }

    if (TaskLocality.isAllowed(maxLocality, TaskLocality.RACK_LOCAL)) {
      for {
        rack <- sched.getRackForHost(host)
        index <- dequeueTaskFromList(execId, getPendingTasksForRack(rack))
      } {
        return Some((index, TaskLocality.RACK_LOCAL, false))
      }
    }

    if (TaskLocality.isAllowed(maxLocality, TaskLocality.ANY)) {
      for (index <- dequeueTaskFromList(execId, allPendingTasks)) {
        return Some((index, TaskLocality.ANY, false))
      }
    }

    // find a speculative task if all others tasks have been scheduled
    dequeueSpeculativeTask(execId, host, maxLocality).map {
      case (taskIndex, allowedLocality) => (taskIndex, allowedLocality, true)}
  }

  /**
   * Respond to an offer of a single executor from the scheduler by finding a task
   *
   * NOTE: this function is either called with a maxLocality which
   * would be adjusted by delay scheduling algorithm or it will be with a special
   * NO_PREF locality which will be not modified
   *
   * @param execId the executor Id of the offered resource
   * @param host  the host Id of the offered resource
   * @param maxLocality the maximum locality we want to schedule the tasks at
   */
  @throws[TaskNotSerializableException]
  def resourceOffer(
      execId: String,
      host: String,
      maxLocality: TaskLocality.TaskLocality)
    : Option[TaskDescription] =
  {
    if (!isZombie) {
      val curTime = clock.getTimeMillis()

      var allowedLocality = maxLocality

      if (maxLocality != TaskLocality.NO_PREF) {
        allowedLocality = getAllowedLocalityLevel(curTime)
        if (allowedLocality > maxLocality) {
          // We're not allowed to search for farther-away tasks
          allowedLocality = maxLocality
        }
      }

      dequeueTask(execId, host, allowedLocality) match {
        case Some((index, taskLocality, speculative)) =>
          // Found a task; do some bookkeeping and return a task description
          val task = tasks(index)
          val taskId = sched.newTaskId()
          // Do various bookkeeping
          copiesRunning(index) += 1
          val attemptNum = taskAttempts(index).size
          val info = new TaskInfo(taskId, index, attemptNum, curTime,
            execId, host, taskLocality, speculative)
          taskInfos(taskId) = info
          taskAttempts(index) = info :: taskAttempts(index)
          // Update our locality level for delay scheduling
          // NO_PREF will not affect the variables related to delay scheduling
          if (maxLocality != TaskLocality.NO_PREF) {
            currentLocalityIndex = getLocalityIndex(taskLocality)
            lastLaunchTime = curTime
          }
          // Serialize and return the task
          val startTime = clock.getTimeMillis()
          val serializedTask: ByteBuffer = try {
            Task.serializeWithDependencies(task, sched.sc.addedFiles, sched.sc.addedJars, ser)
          } catch {
            // If the task cannot be serialized, then there's no point to re-attempt the task,
            // as it will always fail. So just abort the whole task-set.
            case NonFatal(e) =>
              val msg = s"Failed to serialize task $taskId, not attempting to retry it."
              logError(msg, e)
              abort(s"$msg Exception during serialization: $e")
              throw new TaskNotSerializableException(e)
          }
          if (serializedTask.limit > TaskSetManager.TASK_SIZE_TO_WARN_KB * 1024 &&
              !emittedTaskSizeWarning) {
            emittedTaskSizeWarning = true
            logWarning(s"Stage ${task.stageId} contains a task of very large size " +
              s"(${serializedTask.limit / 1024} KB). The maximum recommended task size is " +
              s"${TaskSetManager.TASK_SIZE_TO_WARN_KB} KB.")
          }
          addRunningTask(taskId)

          // We used to log the time it takes to serialize the task, but task size is already
          // a good proxy to task serialization time.
          // val timeTaken = clock.getTime() - startTime
          val taskName = s"task ${info.id} in stage ${taskSet.id}"
          logInfo(s"Starting $taskName (TID $taskId, $host, partition ${task.partitionId}," +
            s" $taskLocality, ${serializedTask.limit} bytes)")

          sched.dagScheduler.taskStarted(task, info)
          return Some(new TaskDescription(taskId = taskId, attemptNumber = attemptNum, execId,
            taskName, index, serializedTask))
        case _ =>
      }
    }
    None
  }

  private def maybeFinishTaskSet() {
    if (isZombie && runningTasks == 0) {
      sched.taskSetFinished(this)
    }
  }

  /**
   * Get the level we can launch tasks according to delay scheduling, based on current wait time.
   */
  private def getAllowedLocalityLevel(curTime: Long): TaskLocality.TaskLocality = {
    // Remove the scheduled or finished tasks lazily
    def tasksNeedToBeScheduledFrom(pendingTaskIds: ArrayBuffer[Int]): Boolean = {
      var indexOffset = pendingTaskIds.size
      while (indexOffset > 0) {
        indexOffset -= 1
        val index = pendingTaskIds(indexOffset)
        if (copiesRunning(index) == 0 && !successful(index)) {
          return true
        } else {
          pendingTaskIds.remove(indexOffset)
        }
      }
      false
    }
    // Walk through the list of tasks that can be scheduled at each location and returns true
    // if there are any tasks that still need to be scheduled. Lazily cleans up tasks that have
    // already been scheduled.
    def moreTasksToRunIn(pendingTasks: HashMap[String, ArrayBuffer[Int]]): Boolean = {
      val emptyKeys = new ArrayBuffer[String]
      val hasTasks = pendingTasks.exists {
        case (id: String, tasks: ArrayBuffer[Int]) =>
          if (tasksNeedToBeScheduledFrom(tasks)) {
            true
          } else {
            emptyKeys += id
            false
          }
      }
      // The key could be executorId, host or rackId
      emptyKeys.foreach(id => pendingTasks.remove(id))
      hasTasks
    }

    while (currentLocalityIndex < myLocalityLevels.length - 1) {
      val moreTasks = myLocalityLevels(currentLocalityIndex) match {
        case TaskLocality.PROCESS_LOCAL => moreTasksToRunIn(pendingTasksForExecutor)
        case TaskLocality.NODE_LOCAL => moreTasksToRunIn(pendingTasksForHost)
        case TaskLocality.NO_PREF => pendingTasksWithNoPrefs.nonEmpty
        case TaskLocality.RACK_LOCAL => moreTasksToRunIn(pendingTasksForRack)
      }
      if (!moreTasks) {
        // This is a performance optimization: if there are no more tasks that can
        // be scheduled at a particular locality level, there is no point in waiting
        // for the locality wait timeout (SPARK-4939).
        lastLaunchTime = curTime
        logDebug(s"No tasks for locality level ${myLocalityLevels(currentLocalityIndex)}, " +
          s"so moving to locality level ${myLocalityLevels(currentLocalityIndex + 1)}")
        currentLocalityIndex += 1
      } else if (curTime - lastLaunchTime >= localityWaits(currentLocalityIndex)) {
        // Jump to the next locality level, and reset lastLaunchTime so that the next locality
        // wait timer doesn't immediately expire
        lastLaunchTime += localityWaits(currentLocalityIndex)
        logDebug(s"Moving to ${myLocalityLevels(currentLocalityIndex + 1)} after waiting for " +
          s"${localityWaits(currentLocalityIndex)}ms")
        currentLocalityIndex += 1
      } else {
        return myLocalityLevels(currentLocalityIndex)
      }
    }
    myLocalityLevels(currentLocalityIndex)
  }

  /**
   * Find the index in myLocalityLevels for a given locality. This is also designed to work with
   * localities that are not in myLocalityLevels (in case we somehow get those) by returning the
   * next-biggest level we have. Uses the fact that the last value in myLocalityLevels is ANY.
   */
  def getLocalityIndex(locality: TaskLocality.TaskLocality): Int = {
    var index = 0
    while (locality > myLocalityLevels(index)) {
      index += 1
    }
    index
  }

  /**
   * Marks the task as getting result and notifies the DAG Scheduler
   */
  def handleTaskGettingResult(tid: Long): Unit = {
    val info = taskInfos(tid)
    info.markGettingResult()
    sched.dagScheduler.taskGettingResult(info)
  }

  /**
   * Check whether has enough quota to fetch the result with `size` bytes
   */
  def canFetchMoreResults(size: Long): Boolean = sched.synchronized {
    totalResultSize += size
    calculatedTasks += 1
    if (maxResultSize > 0 && totalResultSize > maxResultSize) {
      val msg = s"Total size of serialized results of ${calculatedTasks} tasks " +
        s"(${Utils.bytesToString(totalResultSize)}) is bigger than spark.driver.maxResultSize " +
        s"(${Utils.bytesToString(maxResultSize)})"
      logError(msg)
      abort(msg)
      false
    } else {
      true
    }
  }

  /**
   * Marks a task as successful and notifies the DAGScheduler that the task has ended.
   */
  def handleSuccessfulTask(tid: Long, result: DirectTaskResult[_]): Unit = {
    val info = taskInfos(tid)
    val index = info.index
    info.markSuccessful()
    removeRunningTask(tid)
    // This method is called by "TaskSchedulerImpl.handleSuccessfulTask" which holds the
    // "TaskSchedulerImpl" lock until exiting. To avoid the SPARK-7655 issue, we should not
    // "deserialize" the value when holding a lock to avoid blocking other threads. So we call
    // "result.value()" in "TaskResultGetter.enqueueSuccessfulTask" before reaching here.
    // Note: "result.value()" only deserializes the value when it's called at the first time, so
    // here "result.value()" just returns the value and won't block other threads.
    sched.dagScheduler.taskEnded(tasks(index), Success, result.value(), result.accumUpdates, info)
    if (!successful(index)) {
      tasksSuccessful += 1
      logInfo("Finished task %s in stage %s (TID %d) in %d ms on %s (%d/%d)".format(
        info.id, taskSet.id, info.taskId, info.duration, info.host, tasksSuccessful, numTasks))
      // Mark successful and stop if all the tasks have succeeded.
      successful(index) = true
      if (tasksSuccessful == numTasks) {
        isZombie = true
      }
    } else {
      logInfo("Ignoring task-finished event for " + info.id + " in stage " + taskSet.id +
        " because task " + index + " has already completed successfully")
    }
    failedExecutors.remove(index)
    maybeFinishTaskSet()
  }

  /**
   * Marks the task as failed, re-adds it to the list of pending tasks, and notifies the
   * DAG Scheduler.
   */
  def handleFailedTask(tid: Long, state: TaskState, reason: TaskEndReason) {
    val info = taskInfos(tid)
    if (info.failed) {
      return
    }
    removeRunningTask(tid)
    info.markFailed()
    val index = info.index
    copiesRunning(index) -= 1
    var accumUpdates: Seq[AccumulatorV2[_, _]] = Seq.empty
    val failureReason = s"Lost task ${info.id} in stage ${taskSet.id} (TID $tid, ${info.host}): " +
      reason.asInstanceOf[TaskFailedReason].toErrorString
    val failureException: Option[Throwable] = reason match {
      case fetchFailed: FetchFailed =>
        logWarning(failureReason)
        if (!successful(index)) {
          successful(index) = true
          tasksSuccessful += 1
        }
        // Not adding to failed executors for FetchFailed.
        isZombie = true
        None

      case ef: ExceptionFailure =>
        // ExceptionFailure's might have accumulator updates
        accumUpdates = ef.accums
        if (ef.className == classOf[NotSerializableException].getName) {
          // If the task result wasn't serializable, there's no point in trying to re-execute it.
          logError("Task %s in stage %s (TID %d) had a not serializable result: %s; not retrying"
            .format(info.id, taskSet.id, tid, ef.description))
          abort("Task %s in stage %s (TID %d) had a not serializable result: %s".format(
            info.id, taskSet.id, tid, ef.description))
          return
        }
        val key = ef.description
        val now = clock.getTimeMillis()
        val (printFull, dupCount) = {
          if (recentExceptions.contains(key)) {
            val (dupCount, printTime) = recentExceptions(key)
            if (now - printTime > EXCEPTION_PRINT_INTERVAL) {
              recentExceptions(key) = (0, now)
              (true, 0)
            } else {
              recentExceptions(key) = (dupCount + 1, printTime)
              (false, dupCount + 1)
            }
          } else {
            recentExceptions(key) = (0, now)
            (true, 0)
          }
        }
        if (printFull) {
          logWarning(failureReason)
        } else {
          logInfo(
            s"Lost task ${info.id} in stage ${taskSet.id} (TID $tid) on executor ${info.host}: " +
            s"${ef.className} (${ef.description}) [duplicate $dupCount]")
        }
        ef.exception

      case e: ExecutorLostFailure if !e.exitCausedByApp =>
        logInfo(s"Task $tid failed because while it was being computed, its executor " +
          "exited for a reason unrelated to the task. Not counting this failure towards the " +
          "maximum number of failures for the task.")
        None

      case e: TaskFailedReason =>  // TaskResultLost, TaskKilled, and others
        logWarning(failureReason)
        None

      case e: TaskEndReason =>
        logError("Unknown TaskEndReason: " + e)
        None
    }
    // always add to failed executors
    failedExecutors.getOrElseUpdate(index, new HashMap[String, Long]()).
      put(info.executorId, clock.getTimeMillis())
    sched.dagScheduler.taskEnded(tasks(index), reason, null, accumUpdates, info)

    if (successful(index)) {
      logInfo(
        s"Task ${info.id} in stage ${taskSet.id} (TID $tid) failed, " +
        "but another instance of the task has already succeeded, " +
        "so not re-queuing the task to be re-executed.")
    } else {
      addPendingTask(index)
    }

    if (!isZombie && state != TaskState.KILLED
        && reason.isInstanceOf[TaskFailedReason]
        && reason.asInstanceOf[TaskFailedReason].countTowardsTaskFailures) {
      assert (null != failureReason)
      numFailures(index) += 1
      if (numFailures(index) >= maxTaskFailures) {
        logError("Task %d in stage %s failed %d times; aborting job".format(
          index, taskSet.id, maxTaskFailures))
        abort("Task %d in stage %s failed %d times, most recent failure: %s\nDriver stacktrace:"
          .format(index, taskSet.id, maxTaskFailures, failureReason), failureException)
        return
      }
    }
    maybeFinishTaskSet()
  }

  def abort(message: String, exception: Option[Throwable] = None): Unit = sched.synchronized {
    // TODO: Kill running tasks if we were not terminated due to a Mesos error
    sched.dagScheduler.taskSetFailed(taskSet, message, exception)
    isZombie = true
    maybeFinishTaskSet()
  }

  /** If the given task ID is not in the set of running tasks, adds it.
   *
   * Used to keep track of the number of running tasks, for enforcing scheduling policies.
   */
  def addRunningTask(tid: Long) {
    if (runningTasksSet.add(tid) && parent != null) {
      parent.increaseRunningTasks(1)
    }
  }

  /** If the given task ID is in the set of running tasks, removes it. */
  def removeRunningTask(tid: Long) {
    if (runningTasksSet.remove(tid) && parent != null) {
      parent.decreaseRunningTasks(1)
    }
  }

  override def getSchedulableByName(name: String): Schedulable = {
    null
  }

  override def addSchedulable(schedulable: Schedulable) {}

  override def removeSchedulable(schedulable: Schedulable) {}

  override def getSortedTaskSetQueue(): ArrayBuffer[TaskSetManager] = {
    var sortedTaskSetQueue = new ArrayBuffer[TaskSetManager]()
    sortedTaskSetQueue += this
    sortedTaskSetQueue
  }

  /** Called by TaskScheduler when an executor is lost so we can re-enqueue our tasks */
  override def executorLost(execId: String, host: String, reason: ExecutorLossReason) {
    // Re-enqueue any tasks that ran on the failed executor if this is a shuffle map stage,
    // and we are not using an external shuffle server which could serve the shuffle outputs.
    // The reason is the next stage wouldn't be able to fetch the data from this dead executor
    // so we would need to rerun these tasks on other executors.
    if (tasks(0).isInstanceOf[ShuffleMapTask] && !env.blockManager.externalShuffleServiceEnabled) {
      for ((tid, info) <- taskInfos if info.executorId == execId) {
        val index = taskInfos(tid).index
        if (successful(index)) {
          successful(index) = false
          copiesRunning(index) -= 1
          tasksSuccessful -= 1
          addPendingTask(index)
          // Tell the DAGScheduler that this task was resubmitted so that it doesn't think our
          // stage finishes when a total of tasks.size tasks finish.
          sched.dagScheduler.taskEnded(
            tasks(index), Resubmitted, null, Seq.empty, info)
        }
      }
    }
    for ((tid, info) <- taskInfos if info.running && info.executorId == execId) {
      val exitCausedByApp: Boolean = reason match {
        case exited: ExecutorExited => exited.exitCausedByApp
        case ExecutorKilled => false
        case _ => true
      }
      handleFailedTask(tid, TaskState.FAILED, ExecutorLostFailure(info.executorId, exitCausedByApp,
        Some(reason.toString)))
    }
    // recalculate valid locality levels and waits when executor is lost
    recomputeLocality()
  }

  /**
   * Check for tasks to be speculated and return true if there are any. This is called periodically
   * by the TaskScheduler.
   *
   * TODO: To make this scale to large jobs, we need to maintain a list of running tasks, so that
   * we don't scan the whole task set. It might also help to make this sorted by launch time.
   */
  override def checkSpeculatableTasks(): Boolean = {
    // Can't speculate if we only have one task, and no need to speculate if the task set is a
    // zombie.
    if (isZombie || numTasks == 1) {
      return false
    }
    var foundTasks = false
    val minFinishedForSpeculation = (SPECULATION_QUANTILE * numTasks).floor.toInt
    logDebug("Checking for speculative tasks: minFinished = " + minFinishedForSpeculation)
    if (tasksSuccessful >= minFinishedForSpeculation && tasksSuccessful > 0) {
      val time = clock.getTimeMillis()
      val durations = taskInfos.values.filter(_.successful).map(_.duration).toArray
      Arrays.sort(durations)
      val medianDuration = durations(min((0.5 * tasksSuccessful).round.toInt, durations.length - 1))
      val threshold = max(SPECULATION_MULTIPLIER * medianDuration, 100)
      // TODO: Threshold should also look at standard deviation of task durations and have a lower
      // bound based on that.
      logDebug("Task length threshold for speculation: " + threshold)
      for ((tid, info) <- taskInfos) {
        val index = info.index
        if (!successful(index) && copiesRunning(index) == 1 && info.timeRunning(time) > threshold &&
          !speculatableTasks.contains(index)) {
          logInfo(
            "Marking task %d in stage %s (on %s) as speculatable because it ran more than %.0f ms"
              .format(index, taskSet.id, info.host, threshold))
          speculatableTasks += index
          foundTasks = true
        }
      }
    }
    foundTasks
  }

  private def getLocalityWait(level: TaskLocality.TaskLocality): Long = {
    val defaultWait = conf.get("spark.locality.wait", "3s")
    val localityWaitKey = level match {
      case TaskLocality.PROCESS_LOCAL => "spark.locality.wait.process"
      case TaskLocality.NODE_LOCAL => "spark.locality.wait.node"
      case TaskLocality.RACK_LOCAL => "spark.locality.wait.rack"
      case _ => null
    }

    if (localityWaitKey != null) {
      conf.getTimeAsMs(localityWaitKey, defaultWait)
    } else {
      0L
    }
  }

  /**
   * Compute the locality levels used in this TaskSet. Assumes that all tasks have already been
   * added to queues using addPendingTask.
   *
   */
  private def computeValidLocalityLevels(): Array[TaskLocality.TaskLocality] = {
    import TaskLocality.{PROCESS_LOCAL, NODE_LOCAL, NO_PREF, RACK_LOCAL, ANY}
    val levels = new ArrayBuffer[TaskLocality.TaskLocality]
    if (!pendingTasksForExecutor.isEmpty && getLocalityWait(PROCESS_LOCAL) != 0 &&
        pendingTasksForExecutor.keySet.exists(sched.isExecutorAlive(_))) {
      levels += PROCESS_LOCAL
    }
    if (!pendingTasksForHost.isEmpty && getLocalityWait(NODE_LOCAL) != 0 &&
        pendingTasksForHost.keySet.exists(sched.hasExecutorsAliveOnHost(_))) {
      levels += NODE_LOCAL
    }
    if (!pendingTasksWithNoPrefs.isEmpty) {
      levels += NO_PREF
    }
    if (!pendingTasksForRack.isEmpty && getLocalityWait(RACK_LOCAL) != 0 &&
        pendingTasksForRack.keySet.exists(sched.hasHostAliveOnRack(_))) {
      levels += RACK_LOCAL
    }
    levels += ANY
    logDebug("Valid locality levels for " + taskSet + ": " + levels.mkString(", "))
    levels.toArray
  }

  def recomputeLocality() {
    val previousLocalityLevel = myLocalityLevels(currentLocalityIndex)
    myLocalityLevels = computeValidLocalityLevels()
    localityWaits = myLocalityLevels.map(getLocalityWait)
    currentLocalityIndex = getLocalityIndex(previousLocalityLevel)
  }

  def executorAdded() {
    recomputeLocality()
  }
}

private[spark] object TaskSetManager {
  // The user will be warned if any stages contain a task that has a serialized size greater than
  // this.
  val TASK_SIZE_TO_WARN_KB = 100
}


text:
'use strict';

const {
    Datagram,
} = require('./resol-vbus');

const expect = require('./expect');
const _ = require('./lodash');

describe('Datagram', () => {

    describe('.fromLiveBuffer', () => {

        it('should be a function', () => {
            expect(Datagram.fromLiveBuffer).to.be.a('function');
        });

        it('should have a fromLiveBuffer function', () => {
            const options = {
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x4334,
                valueId: 0x5333,
                value: 0x63328330
            };

            const buffer = Buffer.from('aa362335332034433353300332630851', 'hex');

            const datagram = Datagram.fromLiveBuffer(buffer, 0, buffer.length);

            expect(datagram).to.be.an.instanceOf(Datagram);

            _.forEach(options, (refValue, key) => {
                const value = datagram [key];

                expect(value).to.equal(refValue, key);
            });
        });

    });

    describe('constructor', () => {

        it('should be a constructor function', () => {
            expect(Datagram).to.be.a('function');
            expect(Datagram.extend).to.be.a('function');
        });

        it('should have reasonable defaults', () => {
            const before = new Date();
            const datagram = new Datagram();
            const after = new Date();

            expect(datagram).to.be.an('object');
            expect(datagram.timestamp).to.be.an.instanceOf(Date);
            expect(datagram.timestamp.getTime()).to.be.within(before.getTime(), after.getTime());
            expect(datagram.channel).to.equal(0);
            expect(datagram.destinationAddress).to.equal(0);
            expect(datagram.sourceAddress).to.equal(0);
            expect(datagram.command).to.equal(0);
            expect(datagram.valueId).to.equal(0);
            expect(datagram.value).to.equal(0);
        });

        it('should copy certain options', () => {
            const options = {
                timestamp: new Date(0),
                channel: 0x1337,
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x4334,
                valueId: 0x5333,
                value: 0x63328330,
                junk: 0x7331
            };

            const datagram = new Datagram(options);

            expect(datagram).to.be.an('object');
            _.forEach(options, (refValue, key) => {
                const value = datagram [key];

                if (key === 'junk') {
                    refValue = undefined;
                }

                expect(value).to.equal(refValue, key);
            });
        });

    });

    describe('#toLiveBuffer', () => {

        it('should be a method', () => {
            expect(Datagram.prototype.toLiveBuffer).to.be.a('function');
        });

        it('should work correctly', () => {
            const options = {
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x4334,
                valueId: 0x5333,
                value: 0x63328330
            };

            const datagram = new Datagram(options);

            const buffer = datagram.toLiveBuffer();

            expect(buffer).to.be.an.instanceOf(Buffer);
            expect(buffer.length).to.equal(16);
            expect(buffer.toString('hex')).to.equal('aa362335332034433353300332630851');
        });

    });

    describe('#getProtocolVersion', () => {

        it('should be a method', () => {
            expect(Datagram.prototype.getProtocolVersion).to.be.a('function');
        });

        it('should work correctly', () => {
            const options = {
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x4334,
                valueId: 0x5333,
                value: 0x63328330
            };

            const datagram = new Datagram(options);

            expect(datagram.getProtocolVersion()).to.equal(0x20);
        });

    });

    describe('#getInfo', () => {

        it('should be a method', () => {
            expect(Datagram.prototype.getInfo).to.be.a('function');
        });

        it('should work correctly for non-notifications', () => {
            const options = {
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x4334,
                valueId: 0x5333,
                value: 0x63328330
            };

            const datagram = new Datagram(options);

            expect(datagram.getInfo()).to.equal(0);
        });

        it('should work correctly for notifications', () => {
            const options = {
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x0900,
                valueId: 0x5333,
                value: 0x63328330
            };

            const datagram = new Datagram(options);

            expect(datagram.getInfo()).to.equal(0x5333);
        });

    });

    describe('#getId', () => {

        it('should be a method', () => {
            expect(Datagram.prototype.getId).to.be.a('function');
        });

        it('should work correctly for non-notifications', () => {
            const options = {
                timestamp: new Date(0),
                channel: 0x13,
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x4334,
                valueId: 0x5333,
                value: 0x63328330,
            };

            const datagram = new Datagram(options);

            expect(datagram.getId()).to.equal('13_2336_3335_20_4334_0000');
        });

        it('should work correctly for notifications', () => {
            const options = {
                timestamp: new Date(0),
                channel: 0x13,
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x0900,
                valueId: 0x5333,
                value: 0x63328330,
            };

            const datagram = new Datagram(options);

            expect(datagram.getId()).to.equal('13_2336_3335_20_0900_5333');
        });

    });

    describe('#compareTo', () => {

        it('should be a method', () => {
            expect(Datagram.prototype.compareTo).to.be.a('function');
        });

        it('should work correctly for channel', () => {
            const options = {
                timestamp: new Date(0),
                channel: 0x13,
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x0900,
                valueId: 0x5333,
                value: 0x63328330,
            };

            const datagram = new Datagram(options);

            expect(datagram.compareTo(new Datagram(options))).to.equal(0);

            options.channel = 0x00;

            expect(datagram.compareTo(new Datagram(options))).to.be.above(0);

            options.channel = 0x7F;

            expect(datagram.compareTo(new Datagram(options))).to.be.below(0);
        });

        it('should work correctly for destinationAddress', () => {
            const options = {
                timestamp: new Date(0),
                channel: 0x13,
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x0900,
                valueId: 0x5333,
                value: 0x63328330,
            };

            const datagram = new Datagram(options);

            expect(datagram.compareTo(new Datagram(options))).to.equal(0);

            options.destinationAddress = 0x0000;

            expect(datagram.compareTo(new Datagram(options))).to.be.above(0);

            options.destinationAddress = 0x7F7F;

            expect(datagram.compareTo(new Datagram(options))).to.be.below(0);
        });

        it('should work correctly for sourceAddress', () => {
            const options = {
                timestamp: new Date(0),
                channel: 0x13,
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x0900,
                valueId: 0x5333,
                value: 0x63328330,
            };

            const datagram = new Datagram(options);

            expect(datagram.compareTo(new Datagram(options))).to.equal(0);

            options.sourceAddress = 0x0000;

            expect(datagram.compareTo(new Datagram(options))).to.be.above(0);

            options.sourceAddress = 0x7F7F;

            expect(datagram.compareTo(new Datagram(options))).to.be.below(0);
        });

        it('should work correctly for command', () => {
            const options = {
                timestamp: new Date(0),
                channel: 0x13,
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x0900,
                valueId: 0x5333,
                value: 0x63328330,
            };

            const datagram = new Datagram(options);

            expect(datagram.compareTo(new Datagram(options))).to.equal(0);

            options.command = 0x0000;

            expect(datagram.compareTo(new Datagram(options))).to.be.above(0);

            options.command = 0x7F7F;

            expect(datagram.compareTo(new Datagram(options))).to.be.below(0);
        });

        it('should work correctly for info', () => {
            const options = {
                timestamp: new Date(0),
                channel: 0x13,
                destinationAddress: 0x2336,
                sourceAddress: 0x3335,
                command: 0x0900,
                valueId: 0x5333,
                value: 0x63328330,
            };

            const datagram = new Datagram(options);

            expect(datagram.compareTo(new Datagram(options))).to.equal(0);

            options.valueId = 0x0000;

            expect(datagram.compareTo(new Datagram(options))).to.be.above(0);

            options.valueId = 0x7F7F;

            expect(datagram.compareTo(new Datagram(options))).to.be.below(0);
        });

    });

});

redpajama_set_name: RedPajamaGithub


text:
package logger

import (
	l4g "github.com/alecthomas/log4go"
)

var Log = make(l4g.Logger)

func Init() {
	// var config = `{"filename":"server.log","maxdays":30,"daily":true,"rotate":true}`
	// // level:Trace 1|Debug 2|Info 3|Warn 4|Error 5|Critical 6
	// Log := log.NewLogger(10)
	// Log.SetLogger("file", config)
	// Log.EnableFuncCallDepth(true)
	Log.LoadConfiguration("./log4go.xml")
}


text:
<?xml version="1.0" encoding="utf-8"?>
<items version="2" >

    <item
        jar="/Users/nsadras/Development/compcellscope_processing/mainActivity/build/intermediates/exploded-aar/com.android.support/support-v4/21.0.3/libs/internal_impl-21.0.3.jar"
        jumboMode="false"
        revision="22.0.1"
        sha1="01ec05bfbafcc07646ba813000bf2ef11742dd03">
        <dex dex="/Users/nsadras/Development/compcellscope_processing/mainActivity/build/intermediates/pre-dexed/debug/internal_impl-21.0.3-42feb00dc08dcafa12ecfccdd1113caf3954b389.jar" />
    </item>
    <item
        jar="/Users/nsadras/Development/compcellscope_processing/mainActivity/libs/prefuse.jar"
        jumboMode="false"
        revision="22.0.1"
        sha1="8119c678486a022a01ef3c25ecf10f85360158bd">
        <dex dex="/Users/nsadras/Development/compcellscope_processing/mainActivity/build/intermediates/pre-dexed/debug/prefuse-85251f98a2377e1c593d44e2e3e90a76dfc58156.jar" />
    </item>
    <item
        jar="/Users/nsadras/Development/adt-bundle-mac-x86_64-20140702/sdk/extras/android/m2repository/com/android/support/support-annotations/21.0.3/support-annotations-21.0.3.jar"
        jumboMode="false"
        revision="22.0.1"
        sha1="4b74cefe1f0c1b819e7260c8627a14674e37fd35">
        <dex dex="/Users/nsadras/Development/compcellscope_processing/mainActivity/build/intermediates/pre-dexed/debug/support-annotations-21.0.3-4a66267f55e8b3c1f3799e77dbeaf5bad09fd8ef.jar" />
    </item>
    <item
        jar="/Users/nsadras/Development/compcellscope_processing/mainActivity/build/intermediates/exploded-aar/com.android.support/support-v4/21.0.3/classes.jar"
        jumboMode="false"
        revision="22.0.1"
        sha1="2c91c949a45a21cdecf26e03951e46c7beec9ad8">
        <dex dex="/Users/nsadras/Development/compcellscope_processing/mainActivity/build/intermediates/pre-dexed/debug/classes-7266ab7759017e433f75cfe2b7cce7601618b592.jar" />
    </item>
    <item
        jar="/Users/nsadras/Development/compcellscope_processing/mainActivity/build/intermediates/exploded-aar/compcellscope_processing/openCVLibrary2410/unspecified/classes.jar"
        jumboMode="false"
        revision="22.0.1"
        sha1="1c730575f5fd3920898e34d084321d2243e62b9f">
        <dex dex="/Users/nsadras/Development/compcellscope_processing/mainActivity/build/intermediates/pre-dexed/debug/classes-6bcefb7fef253a67419d785a03e038d8478bafeb.jar" />
    </item>
    <item
        jar="/Users/nsadras/Development/compcellscope_processing/mainActivity/build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.3/classes.jar"
        jumboMode="false"
        revision="22.0.1"
        sha1="81d42bf983a8741f4888a6e333e454e4a5e0eeb7">
        <dex dex="/Users/nsadras/Development/compcellscope_processing/mainActivity/build/intermediates/pre-dexed/debug/classes-ae6043c51f5e10615a19dd767471ca35a0ccfd6c.jar" />
    </item>

</items>


text:
<?php

/* TwigBundle:Exception:error.atom.twig */
class __TwigTemplate_c2f804f118209db740a6bfc304bf594e73a5cce0375514964589ed1fd7346720 extends Twig_Template
{
    public function __construct(Twig_Environment $env)
    {
        parent::__construct($env);

        $this->parent = false;

        $this->blocks = array(
        );
    }

    protected function doDisplay(array $context, array $blocks = array())
    {
        // line 1
        $this->loadTemplate("TwigBundle:Exception:error.xml.twig", "TwigBundle:Exception:error.atom.twig", 1)->display($context);
    }

    public function getTemplateName()
    {
        return "TwigBundle:Exception:error.atom.twig";
    }

    public function getDebugInfo()
    {
        return array (  19 => 1,);
    }

    /** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */
    public function getSource()
    {
        @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', E_USER_DEPRECATED);

        return $this->getSourceContext()->getCode();
    }

    public function getSourceContext()
    {
        return new Twig_Source("{% include 'TwigBundle:Exception:error.xml.twig' %}
", "TwigBundle:Exception:error.atom.twig", "C:\\wamp64\\www\\tunisiehologram\\vendor\\symfony\\symfony\\src\\Symfony\\Bundle\\TwigBundle/Resources/views/Exception/error.atom.twig");
    }
}


text:
package org.apache.geode.modules.session.bootstrap;

import org.apache.geode.cache.CacheClosedException;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.control.RebalanceResults;
import org.apache.geode.modules.util.RegionHelper;

/**
 * This is a singleton class which maintains configuration properties as well as starting a
 * Peer-To-Peer cache.
 */

public class PeerToPeerCache extends AbstractCache {

  protected static final String DEFAULT_CACHE_XML_FILE_NAME = "cache-peer.xml";

  static {
    instance = new PeerToPeerCache();
  }

  private PeerToPeerCache() {
    // Singleton
    super();
  }

  public static AbstractCache getInstance() {
    return instance;
  }

  @Override
  protected void createOrRetrieveCache() {
    if (getLogger().isDebugEnabled()) {
      getLogger().debug("Creating cache");
    }
    // Get the existing cache if any
    try {
      this.cache = CacheFactory.getAnyInstance();
    } catch (CacheClosedException e) {
    }

    // If no cache exists, create one
    String message = null;
    if (this.cache == null || cache.isClosed()) {
      this.cache = new CacheFactory(createDistributedSystemProperties()).create();
      message = "Created ";
    } else {
      message = "Retrieved ";
    }
    getLogger().info(message + this.cache);
  }

  @Override
  protected void rebalanceCache() {
    try {
      getLogger().info("Rebalancing: " + this.cache);
      RebalanceResults results = RegionHelper.rebalanceCache(this.cache);
      if (getLogger().isDebugEnabled()) {
        getLogger().debug("Done rebalancing: " + this.cache);
        getLogger().debug(RegionHelper.getRebalanceResultsMessage(results));
      }
    } catch (Exception e) {
      getLogger().warn("Rebalance failed because of the following exception:", e);
    }
  }

  @Override
  protected String getDefaultCacheXmlFileName() {
    return DEFAULT_CACHE_XML_FILE_NAME;
  }
}


text:
### FontValue

* JavaScript Type: `tabris.Font`, `Object`, `string`
* TypeScript Type: `tabris.FontValue`

A `FontValue` describes a font by size, family, weight and style. This type allows various expressions that can all be used in place of a [`Font`](#class-font) instance for convenience. All API that accept these expressions will convert them to a `Font` object. (With the exception of `CanvasContext`.) Setting a FontValue property to null resets it to the default.

Generic **font size** is always given as DIP (device independent pixels), though the string shorthand expects `"px"` as a unit. It's still DIPs.

Generic **font families** are supported across all platforms: `"serif"`, `"sans-serif"`, `"condensed"` and `"monospace"`. These are available as static string properties of `Font`, e.g. `Font.serif`. These exist just to help with autocompletion. More families can be added via `app.registerFont`. If no family is given for a font the system default is used. If no font family is given the default system font will be used. The string `"initial"` represents the platform default.

Supported **font weights** are `"light"`, `"thin"`, `"normal"`, `"medium"`, `"bold"` and `"black"`. The default is `"normal"`

Supported **font styles** are `"italic"` and `"normal"`. The default is `"normal"`

In TypeScript you can import this type as a union with `import {FontValue} from 'tabris';` or use `tabris.FontValue`. [Type guards](https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) for `FontValue` are available as [`Font.isFontValue`](#isfontvaluevalue) and [`Font.isValidFontValue`](#isvalidfontvaluevalue).

In addition to `Font` instances `FontValue` includes:

### FontLikeObject

* JavaScript Type: `Object`
* TypeScript Type: `tabris.FontLikeObject`

```ts
interface FontLikeObject {
  size: number;
  family?: string[];
  weight?: FontWeight;
  style?: FontStyle;
}
```

A plain object implementing the same properties as [`Font`](#class-font).

Examples:

```js
{size: 16, weight: 'bold'}
{size: 24, family: 'sans-serif', style: 'italic'}
```

### FontString

* JavaScript Type: `string`
* TypeScript Type: `tabris.FontString`

 As a string, a subset of the shorthand syntax known from CSS is used: `"font-style font-weight font-size font-family"`, where every value except size is optional. The size also need to have a `"px"` postfix. Multiple families may be given separated by commas. Families with spaces in their name need to be put in single or double quotes.

Examples:

```js
"bold 24px"
"12px sans-serif"
"italic thin 12px sans-serif"
"24px 'My Font', sans-serif"
"initial"
```


text:
<?php

/**
 * Adds and remove cookies.
 *
 * @package    PHPUnit_Selenium
 * @author     Giorgio Sironi <giorgio.sironi@asp-poli.it>
 * @copyright  2010-2012 Sebastian Bergmann <sb@sebastian-bergmann.de>
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 * @version    Release: 1.2.6
 * @link       http://www.phpunit.de/
 * @since      Class available since Release 1.2.6
 */
class PHPUnit_Extensions_Selenium2TestCase_Session_Cookie
{
    private $driver;
    private $url;

    public function __construct(PHPUnit_Extensions_Selenium2TestCase_Driver $driver,
                                PHPUnit_Extensions_Selenium2TestCase_URL $url)
    {
        $this->driver = $driver;
        $this->url = $url;
    }

    /**
     * @param string $name
     * @param string $value
     * @return void
     */
    public function add($name, $value)
    {
        return new PHPUnit_Extensions_Selenium2TestCase_Session_Cookie_Builder($this, $name, $value);
    }

    /**
     * @param string $name
     * @return string
     */
    public function get($name)
    {
        $cookies = $this->driver->curl('GET', $this->url)->getValue();
        foreach ($cookies as $cookie) {
            if ($cookie['name'] == $name) {
                return $cookie['value'];
            }
        }
        throw new PHPUnit_Extensions_Selenium2TestCase_Exception("There is no '$name' cookie available on this page.");
    }

    /**
     * @param string $name
     * @return void
     */
    public function remove($name)
    {
        $url = $this->url->descend($name);
        $this->driver->curl('DELETE', $url);
    }

    /**
     * @return void
     */
    public function clear()
    {
        $this->driver->curl('DELETE', $this->url);
    }

    /**
     * @internal
     * @param array $data
     * @return void
     */
    public function postCookie(array $data)
    {
        $this->driver->curl('POST',
                            $this->url,
                            array(
                                'cookie' => $data
                            ));
    }
}


text:
@interface ZHMapViewController : ZHBaseViewController

@end


text:
<?php

namespace Autarky\Routing;

use Autarky\ConfiguratorInterface;
use Autarky\Config\ConfigInterface;

/**
 * This configurator reads the app/config/routes file and mounts it onto the
 * root path of your application.
 */
class DefaultRouteConfigurator implements ConfiguratorInterface
{
	/**
	 * The config instance.
	 *
	 * @var ConfigInterface
	 */
	protected $config;

	/**
	 * The router instance.
	 *
	 * @var RouterInterface
	 */
	protected $router;

	/**
	 * Constructor.
	 *
	 * @param RouterInterface $router
	 * @param ConfigInterface $config
	 */
	public function __construct(
		RouterInterface $router,
		ConfigInterface $config
	) {
		$this->router = $router;
		$this->config = $config;
	}

	/**
	 * {@inheritdoc}
	 */
	public function configure()
	{
		$this->router->mount($this->config->get('routes'), '/');
	}
}


text:
<?php

	// include all requirements
	include_once("configuration.php");
	include_once("class.blog.php");
	//include_once("header.php");

	$bloglist = new blog();

	// Blog Eintraege holen und ausgeben
	$blogs = $bloglist->Feed();
	print $blogs;

?>


text:
module.exports = (grunt) => {

    grunt.file.setBase('..');
    // eslint-disable-next-line import/no-extraneous-dependencies
    require('load-grunt-tasks')(grunt);

    grunt.initConfig({

        clean: {},
        express: {
            dev: {
                options: {
                    script: 'app/index.js',
                    node_env: 'dev',
                    port: process.env.PORT,
                    output: 'started'
                }
            }
        },

        mochaTest: {
            unit: {
                options: {
                    reporter: 'spec',
                    quiet: false, // Optionally suppress output to standard out (defaults to false)
                    clearRequireCache: true, // Optionally clear the require cache before running tests (defaults to false)
                    require: 'co-mocha'
                },
                src: ['app/test/unit/**/*.test.js']
            },
            e2e: {
                options: {
                    reporter: 'spec',
                    quiet: false, // Optionally suppress output to standard out (defaults to false)
                    clearRequireCache: true, // Optionally clear the require cache before running tests (defaults to false)

                },
                src: ['app/test/e2e/**/*.spec.js']
            }
        },
        watch: {
            options: {
                livereload: true
            },
            jssrc: {
                files: [
                    'app/src/**/*.js',
                ],
                tasks: ['mochaTest:unit', 'express:dev'],
                options: {
                    spawn: false
                }
            },
            unitTest: {
                files: [
                    'app/test/unit/**/*.test.js',
                ],
                tasks: ['mochaTest:unit'],
                options: {
                    spawn: false
                }
            },
            e2eTest: {
                files: [
                    'app/test/unit/**/*.spec.js',
                ],
                tasks: ['mochaTest:e2e'],
                options: {
                    spawn: false
                }
            },

        },

        nyc: {
            cover: {
                options: {
                    include: ['app/src/**'],
                    exclude: '*.test.*',
                    reporter: ['lcov', 'text-summary'],
                    reportDir: 'coverage',
                    all: true
                },
                cmd: false,
                args: ['grunt', '--gruntfile', 'app/Gruntfile.js', 'test']
            }
        }
    });

    grunt.registerTask('unitTest', ['mochaTest:unit']);

    grunt.registerTask('e2eTest', ['mochaTest:e2e']);

    grunt.registerTask('test', ['e2eTest', 'unitTest']);

    grunt.registerTask('serve', ['express:dev', 'watch']);

    grunt.registerTask('default', 'serve');

    grunt.loadNpmTasks('grunt-simple-nyc');

};


text:
namespace rocksdb {

struct Options;
struct FileMetaData;

class Env;
struct EnvOptions;
class Iterator;
class TableCache;
class VersionEdit;
class TableBuilder;
class WritableFileWriter;

TableBuilder* NewTableBuilder(
    const ImmutableCFOptions& options,
    const InternalKeyComparator& internal_comparator,
    const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>*
        int_tbl_prop_collector_factories,
    WritableFileWriter* file, const CompressionType compression_type,
    const CompressionOptions& compression_opts,
    const bool skip_filters = false);

// Build a Table file from the contents of *iter.  The generated file
// will be named according to number specified in meta. On success, the rest of
// *meta will be filled with metadata about the generated table.
// If no data is present in *iter, meta->file_size will be set to
// zero, and no Table file will be produced.
extern Status BuildTable(
    const std::string& dbname, Env* env, const ImmutableCFOptions& options,
    const EnvOptions& env_options, TableCache* table_cache, Iterator* iter,
    FileMetaData* meta, const InternalKeyComparator& internal_comparator,
    const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>*
        int_tbl_prop_collector_factories,
    const SequenceNumber newest_snapshot,
    const SequenceNumber earliest_seqno_in_memtable,
    const CompressionType compression,
    const CompressionOptions& compression_opts, bool paranoid_file_checks,
    const Env::IOPriority io_priority = Env::IO_HIGH,
    TableProperties* table_properties = nullptr);

}  // namespace rocksdb


text:
class RestaurantBuilder(object):

    def build(self, html):

        pass


text:
<?php
namespace ElasticSearch\Model;

/**
 * need this to use form classes
 */
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class ElasticSearchForm implements InputFilterAwareInterface {
	public $id, $email, $comment, $created;
	protected $inputFilter;

	/**
	 * the function name MUST be only exchangeArray
	 */
	public function exchangeArray( $data ) {
		$this->id = ( !empty( $data['id'] ) ) ? $data['id'] : null;
		$this->email = ( !empty( $data['email'] ) ) ? $data['email'] : null;
		$this->comment = ( !empty( $data['comment'] ) ) ? $data['comment'] : null;
		$this->date = ( !empty( $data['created'] ) ) ? $data['created'] : null;
	}

	/**
	 * Using this in order to implement bind function in the testController.
	 */
	public function getArrayCopy() {
		return get_object_vars($this);
	}

	public function setInputFilter( InputFilterInterface $inputFilter ) {
		throw new \Exception( "Not used" );
	}

	/**
	 * start form validation
	 * all validators can be found in the folder /www/zend/vendor/zendframework/zendframework/library/Zend/Validator/
	 * each array element is the class instance, i.e.
	 * 'validators' => array(
	 *      'name' => new EmailAddress()
	 * )
	 * is a name of the class in the above folder
	 *
	 * Same with filters array. Folder is /www/zend/vendor/zendframework/zendframework/library/Zend/Filter
	 */
	public function getInputFilter() {
		if ( !$this->inputFilter ) {
			$inputFilter = new InputFilter();

			$inputFilter->add(
				array(
					'name' => 'search',
				    'required' => true,
				)
			);
			$this->inputFilter = $inputFilter;
		}
		return $this->inputFilter;
	}
}


text:
HTML5 & Javascript Pong Game
============================

This was a weekend project I used to learn about the canvas tag in HTML5 and also practice some basic game mechanics.
Some enhancement can be done, like creating a beatable AI and using local storage in order to keep track of stats between different gaming sessions.

GOALS
=====

The goals for this project were:

 * develop a pong game in html5 having two vertical bars, a ball and two bouncing walls at the bottom and at the top of the screen
 * the player bar must be controlled by the mouse
 * there should be a start screen to inform the scores and wait for an event to start the game
 * there should be an end screen to inform the winner, the scores and wait for an event to start the game

During the development of the game the mechanics were still unclear. With a trial and error strategy the mechanics became:

 * the game should run at aproximately 60 fps
 * the ball should be given a random direction at the beginning of a new game and a speed of factor 4
 * the ball direction shouldn't exceed 60 degrees in relation to the horizontal axis, avoiding directions that turn the 'point of collision' almost unpredictable for the player
 * the ball speed shouldn't exceed a factor of 10, otherwise the human player can't follow it properly
 * for collisions, the idea was to use the momentum conservation formula to predict the resulting speed and direction of the ball, but given the paddle's movement is independant from the ball movement even after a collision, an easier approach was used: invert horizontal speed; sum paddle speed with ball vertical speed; then use vector knowledge to extract module and angle out of horizontal and vertical speeds.

FILES
=====

The game files are organized as follows:

	pong.js                # all the game mechanics and control logic
	pong.unbeatable_ai.js  # makes player1 unbeatable by always following the ball precisely
	pong.mouse_control.js  # gives player2 the hability to control the bar with the mouse

FUTURE GOALS
============

 * improve gameplay:  		adjust speed factors according to current fps
 * improve graphics:  		use images that better represent the bars, the ball and the field of the game
 * improve mechanics: 		collision algorithm depending on which part of the bar the ball collided, changing the outcome accordingly
 * code:     		  		use OO concepts to decouple control logic and easily interchange AI logic
 * artificial intelligence: study common strategies and develop beatable AI strategies that are randomly selected at the beginning of a game session

LICENSE
=======

[MIT](http://en.wikipedia.org/wiki/MIT_License) license.


text:
import json
import operator

with open("sanitation.json") as json_file:
	json_data = json.load(json_file)

	print(json_data)

	def sth(key):
		return float(key["sanitation"])

	b = sorted(json_data[:-1], key=sth ,reverse= True)

	print b
	json = json.dumps(b)
	f = open("sanitation1.json", "w")
	#f.write("district,population")
	f.write(json)
	f.close()


text:
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

    <!-- homeActivity one -->
    <!-- homeActivity two -->
    <!-- homeActivity three -->
</resources>


text:
package com.groupon.vertx.memcache.parser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;

import org.junit.Before;
import org.junit.Test;

import com.groupon.vertx.memcache.MemcacheException;
import com.groupon.vertx.memcache.client.JsendStatus;
import com.groupon.vertx.memcache.client.response.TouchCommandResponse;
import com.groupon.vertx.memcache.stream.MemcacheResponseType;

/**
 * Tests for TouchLineParser.
 *
 * @author Stuart Siegrist (fsiegrist at groupon dot com)
 * @since 1.0.0
 */
public class TouchLineParserTest {
    private TouchLineParser parser;
    private ByteArrayOutputStream outputStream;

    @Before
    public void setUp() {
        outputStream = new ByteArrayOutputStream();
        parser = new TouchLineParser();
    }

    @Test
    public void testTouchEndLine() throws Exception {
        outputStream.write(MemcacheResponseType.TOUCHED.type.getBytes());
        assertTrue("Failed to identify end", parser.isResponseEnd(outputStream));
        TouchCommandResponse response = parser.getResponse();
        assertEquals("Wrong status", JsendStatus.success, response.getStatus());
        assertEquals("Wrong data", MemcacheResponseType.TOUCHED.name(), response.getData());
    }

    @Test
    public void testNotFoundEndLine() throws Exception {
        outputStream.write(MemcacheResponseType.NOT_FOUND.type.getBytes());
        assertTrue("Failed to identify end", parser.isResponseEnd(outputStream));
        TouchCommandResponse response = parser.getResponse();
        assertEquals("Wrong status", JsendStatus.success, response.getStatus());
        assertEquals("Wrong data", MemcacheResponseType.NOT_FOUND.name(), response.getData());
    }

    @Test
    public void testErrorEndLine() throws Exception {
        outputStream.write(MemcacheResponseType.ERROR.type.getBytes());
        assertTrue("Failed to identify end", parser.isResponseEnd(outputStream));
        TouchCommandResponse response = parser.getResponse();
        assertEquals("Wrong status", JsendStatus.error, response.getStatus());
        assertEquals("Wrong data", MemcacheResponseType.ERROR.name(), response.getMessage());
    }

    @Test
    public void testClientErrorEndLine() throws Exception {
        String clientError = "CLIENT ERROR message";
        outputStream.write(clientError.getBytes());
        assertTrue("Failed to identify end", parser.isResponseEnd(outputStream));
        TouchCommandResponse response = parser.getResponse();
        assertEquals("Wrong status", JsendStatus.error, response.getStatus());
        assertEquals("Wrong data", clientError, response.getMessage());
    }

    @Test
    public void testServerErrorEndLine() throws Exception {
        String serverError = "SERVER ERROR message";
        outputStream.write(serverError.getBytes());
        assertTrue("Failed to identify end", parser.isResponseEnd(outputStream));
        TouchCommandResponse response = parser.getResponse();
        assertEquals("Wrong status", JsendStatus.error, response.getStatus());
        assertEquals("Wrong data", serverError, response.getMessage());
    }

    @Test
    public void testUnexpectedFormat() throws Exception {
        try {
            outputStream.write("foo".getBytes());
            parser.isResponseEnd(outputStream);
            assertTrue("Failed to throw exception", false);
        } catch (MemcacheException me) {
            assertEquals("Unexpected exception", "Unexpected format in response", me.getMessage());
        }
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:background="@color/bright_foreground_disabled_material_dark">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/chat_img"
        android:adjustViewBounds="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/video"

         />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/grp_nam"
        android:layout_alignParentTop="true"
        android:paddingLeft="8dp"
        android:layout_toRightOf="@+id/chat_img"
       android:paddingBottom="4dp"

       />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/lst_msg"
        android:paddingLeft="8dp"
        android:layout_toRightOf="@+id/chat_img"

        android:layout_below="@+id/grp_nam"/>

    <TextView
        android:layout_width="wrap_content"
        android:id="@+id/unread_msg_count"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:visibility="invisible"
        android:background="@drawable/circle"
        android:textColor="@color/background_floating_material_light"
        android:layout_below="@+id/lst_msg"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:singleLine="false"
        android:gravity="center"
        android:textStyle="bold" />
</RelativeLayout>


text:
@interface AcmeToolbar : UIToolbar
{

}

@end


text:
title: Tcl
nav: docs
renderer: Cucumber::Website::Reference
---

# Tcl

The documentation is currently on [GitHub](https://github.com/cucumber/cucumber-ruby-tcl).


text:
package Evolution_Strategies.Optimizers;

import Evolution_Strategies.Configs.Config;

public class Adam extends Optimizer
{
    private double beta1, beta2, epsilon;
    private double[] m;
    private double[] v;
    private double stepSize;
    private int timeStep;

    public Adam(int numParams, double step)
    {
        stepSize = step;

        beta1 = Config.ADAM_BETA1_DEFAULT;
        beta2 = Config.ADAM_BETA2_DEFAULT;
        epsilon = Config.ADAM_EPSILON_DEFAULT;

        m = new double[numParams];
        v = new double[numParams];
        timeStep = 0;
    }

    public double[] computeUpdate(double[] theta, double[] gradient)
    {
        double[] step = getUpdateStep(gradient);

        for(int i=0;i<theta.length;i++)
        {
            step[i]+=theta[i];
        }
        return step;
    }

    @Override
    public double[] getUpdateStep(double[] gradient)
    {
        timeStep++;
        double a = stepSize*Math.sqrt(1-Math.pow(beta2,timeStep))/(1 - Math.pow(beta1, timeStep));
        for(int i=0;i<m.length;i++)
        {
            m[i] = beta1*m[i] + (1.0 - beta1)*gradient[i];
            v[i] = beta2*v[i] + (1.0 - beta2)*(gradient[i]*gradient[i]);

        }
        double[] step = new double[gradient.length];
        for(int i=0;i<step.length;i++)
        {
            step[i] = -a*m[i]/(Math.sqrt(v[i]) + epsilon);
        }

        return step;
    }

}


text:
<!DOCTYPE HTML>

    <title>Blog | Zhou Ao</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="description" content="Zhou Ao's Blog" />
    <meta name="keywords" content="Zhou Ao, Blog, Tech, Developer, Programming, Physics, Peking University, Project" />

        <link rel="stylesheet" href="../css/blogskel.css" />
        <link rel="stylesheet" href="../css/blogstyle.css" />
        <link rel="stylesheet" href="../css/blogstyle-xlarge.css" />

    <link rel="icon" type="image/x-icon" href="/images/favicon.ico">

Zhou Ao's Blog

                Search

Back to the Past and Start Blogging (III)

Step-by-step instructions to establish a blog website hosted by Github Pages and using Jekyll with functional tools, such as code highlight, mathematic expressions rendering and full-text search.

                2014-12-07

Back to the Past and Start Blogging (II)

Step-by-step instructions to establish a blog website hosted by Github Pages and using Jekyll with functional tools, such as code highlight, mathematic expressions rendering and full-text search.

                2014-12-06

Back to the Past and Start Blogging (I)

Step-by-step instructions to establish a blog website hosted by Github Pages and using Jekyll with functional tools, such as code highlight, mathematic expressions rendering and full-text search.

                2014-12-05

Integration of Mergesort and Insertion Sort

Mergesort is faster than insertion sort, while whens the size of sub-arrays are small, the overhead of many recursive calls makes the algorithm inefficient. Integration of mergesort and insertion sort is a good way to develop advanced sort algorithm.

                2014-11-28

Binary Search Time Complexity: Average Number of Comparisons

Introduce binary search, and calculate the average number of comparisons in the algorithm.

                2014-11-25

Hacker on the Way

My first blog to the world.

                2014-11-22

Blog

Project

Me

About

© 2014 Zhou Ao.

All contents under (CC) BY-NC-SA license, unless otherwise noted.


text:
require 'spec_helper'
require 'gitsh/commands/internal_command'

describe Gitsh::Commands::InternalCommand do
  describe '.new' do
    it 'returns a Set command when given the command "set"' do
      command = described_class.new(double('env'), 'set', %w(foo bar))
      expect(command).to be_a described_class::Set
    end

    it 'returns an Exit command when given the command "exit"' do
      command = described_class.new(double('env'), 'exit', arguments())
      expect(command).to be_a described_class::Exit
    end

    it 'returns an Exit command when given the command "q"' do
      command = described_class.new(double('env'), 'q', arguments())
      expect(command).to be_a described_class::Exit
    end

    it 'returns a Chdir command when given the command "cd"' do
      command = described_class.new(double('env'), 'cd', '/some/path')
      expect(command).to be_a described_class::Chdir
    end

    it 'returns a Help command when given the command "help"' do
      command = described_class.new(double('env'), 'help', arguments())
      expect(command).to be_a described_class::Help
    end

    it 'returns a Source command when given the command "source"' do
      command = described_class.new(double('env'), 'source', arguments('/some/path'))
      expect(command).to be_a described_class::Source
    end

    it 'returns an Unknown command when given anything else' do
      command = described_class.new(double('env'), 'notacommand', %w(foo bar))
      expect(command).to be_a described_class::Unknown
    end
  end

  describe '.commands' do
    it 'returns a list of recognised commands formatted for autocomplete' do
      expect(described_class.commands).to include ':set', ':exit'
    end
  end

  describe '.command_class' do
    it 'returns a class object corresponding to the command' do
      expect(described_class.command_class('exit')).to eq described_class::Exit
    end

    it 'returns Unknown for an unknown command' do
      expect(described_class.command_class('banana')).to eq described_class::Unknown
    end
  end
end


text:
package it.uspread.android.remote.exception;

import android.content.res.Resources;

import it.uspread.android.R;

/**
 * Exception utilisé pour encapsuler tout types de problèmes survenant lors d'une communication avec le serveur.<br>
 *
 * @author Lone Décosterd,
 */
public class USpreadItException extends Exception {

    /**
     * Les types d'exception pouvant survenir
     */
    public static enum Type {
        AUTHENTICATION_ERROR(R.string.exception_authenticationError),
        AUTHENTICATION_REQUIRED(R.string.exception_authenticationRequired),
        FORBIDDEN(R.string.exception_forbidden),
        COMMUNICATION_UNREACHABLE(R.string.exception_communicationUnreachable),
        COMMUNICATION_UNAVAILABLE(R.string.exception_communicationUnavailable),
        COMMUNICATION_TIMEOUT(R.string.exception_communicationTimeout),
        INTERNET(R.string.exception_internet),
        QUOTA(R.string.exception_quota),
        OTHERS(R.string.exception_others);

        /** Id du message associé au type d'exception */
        private final int id;

        /**
         * Constructeur
         *
         * @param id
         *         Id du message associé au type d'exception
         */
        private Type(int id) {
            this.id = id;
        }

        /**
         * Message associé
         *
         * @param resources
         *         pour l'accès à une ressource de l'application
         * @return Le message
         */
        public String getMessage(final Resources resources) {
            return resources.getString(id);
        }
    }

    /** Le type de l'exception */
    private final Type type;

    /**
     * Constructeur pour une exception dont le message sera le nom de l'enum (a utiliser lorsque aucun message particulier a indiquer)
     *
     * @param type
     *         {@link #type} non null
     */
    public USpreadItException(final Type type) {
        this(type, null);
    }

    /**
     * Constructeur pour une exception dont on spécifie le message
     *
     * @param type
     *         {@link #type} non null
     * @param detailledMessage
     *         message ou null
     */
    public USpreadItException(final Type type, String detailledMessage) {
        this(type, detailledMessage, null);
    }

    /**
     * Constructeur pour une exception dont on spécifie le message
     *
     * @param type
     *         {@link #type} non null
     * @param detailledMessage
     *         message ou null
     * @param throwable
     *         exception originale éventuelle
     */
    public USpreadItException(final Type type, String detailledMessage, final Throwable throwable) {
        super(detailledMessage != null ? detailledMessage : type.name(), throwable);
        this.type = type;
    }

    /**
     * @return {@link #type}
     */
    public Type getType() {
        return type;
    }

    /**
     * Le message pour l'utilisateur associé à l'exception
     *
     * @param resources
     *         accès à une ressource de l'application
     * @return Un court message décrivant le problème
     */
    public String getUserMessage(final Resources resources) {
        return type.getMessage(resources);
    }
}


text:
package itso.bank.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;

/**
 * The persistent class for the CUSTOMER database table.
 *
 */
@Entity
@NamedQuery(name = "getCustomer", query = "SELECT c FROM Customer c")
public class Customer implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	private String ssn;

	@Column(name="FIRST_NAME")
	private String firstName;

	@Column(name="LAST_NAME")
	private String lastName;

	private String title;

	//bi-directional many-to-many association to Account
	@ManyToMany(mappedBy="customers")
	private List<Account> accounts;

    public Customer() {
    }

	public String getSsn() {
		return this.ssn;
	}

	public void setSsn(String ssn) {
		this.ssn = ssn;
	}

	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<Account> getAccounts() {
		return this.accounts;
	}

	public void setAccounts(List<Account> accounts) {
		this.accounts = accounts;
	}

}


text:
package ru.spbstu.icc.kspt.kuznetsov.fpf4mir.core.facts.artifact;

import java.io.File;
import java.io.Serializable;

import ru.spbstu.icc.kspt.kuznetsov.fpf4mir.core.facts.activity.Activity;
import ru.spbstu.icc.kspt.kuznetsov.fpf4mir.core.facts.activity.ActivityRelatedFact;
import ru.spbstu.icc.kspt.kuznetsov.fpf4mir.core.utils.FPFCloneable;
import ru.spbstu.icc.kspt.kuznetsov.fpf4mir.core.utils.FactWithName;

public interface Artifact extends ActivityRelatedFact, Serializable, FPFCloneable, FactWithName {

	@Override public Activity getActivity();
	@Override public void setActivity(Activity activity);

	@Override public String getName();
	@Override public void setName(String name);

	public boolean isDirectory();

	public String getFileName();
	public void setFileName(String fileName);

	public String getBaseDir();
	public void setBaseDir(String baseDir);

	public String getAbsolutePath();

	public File _getFile();

}


text:
@interface EMModel : NSManagedObject

@end


text:
package com.sdw.soft.demo.jmx;

/**
 * Created by shangyindong on 2018/5/5.
 */
public interface UserMBean {

    public String getName();

    public void setName(String name);

    public void printHello();

    public void printHello(String name);
}


text:
import { Popover } from 'antd';

// Eventually Popover can be wrapped and customized in this file
// for now we're just redirecting
export default Popover;


text:
package storage

import (
	"bytes"
	"errors"
	"fmt"
	"log"

	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/google/btree"
)

var (
	ErrReversionNotFound = errors.New("stroage: reversion not found")
)

// keyIndex stores the reversion of an key in the backend.
// Each keyIndex has at least one key generation.
// Each generation might have several key versions.
// Tombstone on a key appends an tombstone version at the end
// of the current generation and creates a new empty generation.
// Each version of a key has an index pointing to the backend.
//
// For example: put(1.0);put(2.0);tombstone(3.0);put(4.0);tombstone(5.0) on key "foo"
// generate a keyIndex:
// key:     "foo"
// rev: 5
// generations:
//    {empty}
//    {4.0, 5.0(t)}
//    {1.0, 2.0, 3.0(t)}
//
// Compact a keyIndex removes the versions with smaller or equal to
// rev except the largest one. If the generations becomes empty
// during compaction, it will be removed. if all the generations get
// removed, the keyIndex Should be removed.

// For example:
// compact(2) on the previous example
// generations:
//    {empty}
//    {4.0, 5.0(t)}
//    {2.0, 3.0(t)}
//
// compact(4)
// generations:
//    {empty}
//    {4.0, 5.0(t)}
//
// compact(5):
// generations:
//    {empty} -> key SHOULD be removed.
//
// compact(6):
// generations:
//    {empty} -> key SHOULD be removed.
type keyIndex struct {
	key         []byte
	rev         int64
	generations []generation
}

// put puts a reversion to the keyIndex.
func (ki *keyIndex) put(rev int64, subrev int64) {
	if rev < ki.rev {
		log.Panicf("store.keyindex: put with unexpected smaller reversion [%d / %d]", rev, ki.rev)
	}
	if len(ki.generations) == 0 {
		ki.generations = append(ki.generations, generation{})
	}
	g := &ki.generations[len(ki.generations)-1]
	g.revs = append(g.revs, reversion{rev, subrev})
	g.ver++
	ki.rev = rev
}

// tombstone puts a reversion, pointing to a tombstone, to the keyIndex.
// It also creates a new empty generation in the keyIndex.
func (ki *keyIndex) tombstone(rev int64, subrev int64) {
	if ki.isEmpty() {
		log.Panicf("store.keyindex: unexpected tombstone on empty keyIndex %s", string(ki.key))
	}
	ki.put(rev, subrev)
	ki.generations = append(ki.generations, generation{})
}

// get gets the reversion of the key that satisfies the given atRev.
// Rev must be higher than or equal to the given atRev.
func (ki *keyIndex) get(atRev int64) (rev reversion, err error) {
	if ki.isEmpty() {
		log.Panicf("store.keyindex: unexpected get on empty keyIndex %s", string(ki.key))
	}
	g := ki.findGeneration(atRev)
	if g.isEmpty() {
		return reversion{}, ErrReversionNotFound
	}

	f := func(rev reversion) bool {
		if rev.main <= atRev {
			return false
		}
		return true
	}

	n := g.walk(f)
	if n != -1 {
		return g.revs[n], nil
	}

	return reversion{}, ErrReversionNotFound
}

// compact compacts a keyIndex by removing the versions with smaller or equal
// reversion than the given atRev except the largest one (If the largest one is
// a tombstone, it will not be kept).
// If a generation becomes empty during compaction, it will be removed.
func (ki *keyIndex) compact(atRev int64, available map[reversion]struct{}) {
	if ki.isEmpty() {
		log.Panic("store.keyindex: unexpected compact on empty keyIndex %s", string(ki.key))
	}

	// walk until reaching the first reversion that has an reversion smaller or equal to
	// the atReversion.
	// add it to the available map
	f := func(rev reversion) bool {
		if rev.main <= atRev {
			available[rev] = struct{}{}
			return false
		}
		return true
	}

	g := ki.findGeneration(atRev)
	if g == nil {
		return
	}

	i := 0
	for i <= len(ki.generations)-1 {
		wg := &ki.generations[i]
		if wg == g {
			break
		}
		i++
	}

	if !g.isEmpty() {
		n := g.walk(f)
		// remove the previous contents.
		if n != -1 {
			g.revs = g.revs[n:]
		}
		// remove any tombstone
		if len(g.revs) == 1 && i != len(ki.generations)-1 {
			delete(available, g.revs[0])
			i++
		}
	}
	// remove the previous generations.
	ki.generations = ki.generations[i:]
	return
}

func (ki *keyIndex) isEmpty() bool {
	return len(ki.generations) == 1 && ki.generations[0].isEmpty()
}

// findGeneartion finds out the generation of the keyIndex that the
// given index belongs to.
func (ki *keyIndex) findGeneration(rev int64) *generation {
	cg := len(ki.generations) - 1

	for cg >= 0 {
		if len(ki.generations[cg].revs) == 0 {
			cg--
			continue
		}
		g := ki.generations[cg]
		if g.revs[0].main <= rev {
			return &ki.generations[cg]
		}
		cg--
	}
	return nil
}

func (a *keyIndex) Less(b btree.Item) bool {
	return bytes.Compare(a.key, b.(*keyIndex).key) == -1
}

func (a *keyIndex) equal(b *keyIndex) bool {
	if !bytes.Equal(a.key, b.key) {
		return false
	}
	if a.rev != b.rev {
		return false
	}
	if len(a.generations) != len(b.generations) {
		return false
	}
	for i := range a.generations {
		ag, bg := a.generations[i], b.generations[i]
		if !ag.equal(bg) {
			return false
		}
	}
	return true
}

func (ki *keyIndex) String() string {
	var s string
	for _, g := range ki.generations {
		s += g.String()
	}
	return s
}

type generation struct {
	ver  int64
	revs []reversion
}

func (g *generation) isEmpty() bool { return g == nil || len(g.revs) == 0 }

// walk walks through the reversions in the generation in ascending order.
// It passes the revision to the given function.
// walk returns until: 1. it finishs walking all pairs 2. the function returns false.
// walk returns the position at where it stopped. If it stopped after
// finishing walking, -1 will be returned.
func (g *generation) walk(f func(rev reversion) bool) int {
	l := len(g.revs)
	for i := range g.revs {
		ok := f(g.revs[l-i-1])
		if !ok {
			return l - i - 1
		}
	}
	return -1
}

func (g *generation) String() string {
	return fmt.Sprintf("g: ver[%d], revs %#v\n", g.ver, g.revs)
}

func (a generation) equal(b generation) bool {
	if a.ver != b.ver {
		return false
	}
	if len(a.revs) != len(b.revs) {
		return false
	}

	for i := range a.revs {
		ar, br := a.revs[i], b.revs[i]
		if ar != br {
			return false
		}
	}
	return true
}