Skip to content

Instantly share code, notes, and snippets.

@ppat
Created March 7, 2014 15:47
Show Gist options
  • Save ppat/9413946 to your computer and use it in GitHub Desktop.
Save ppat/9413946 to your computer and use it in GitHub Desktop.
/* interleave slots so that slots of different nodes are given priority during assignment of topology as opposed to slots of the same node */
private def interleaveSlotsByNode(slots: Set[WorkerSlot]): List[WorkerSlot] = {
val groupedSeq = slots.groupBy(_.getNodeId).toSeq
val slotsByNode = mutable.Map[String, Set[WorkerSlot]](groupedSeq: _*)
val nodes = slotsByNode.keys.toList.sorted
val results = mutable.ListBuffer[WorkerSlot]()
while (!slotsByNode.isEmpty) {
for (node <- nodes) {
slotsByNode.get(node) match {
case Some(slots) => {
val firstSlot = slots.head
results += firstSlot
val remaining = slots - firstSlot
if (remaining.isEmpty)
slotsByNode.remove(node)
else
slotsByNode.put(node, remaining)
}
case None => {}
}
}
}
results.toList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment