2010-06-15 12:43:46 +08:00
|
|
|
package org.broadinstitute.sting.queue.engine
|
|
|
|
|
|
|
|
|
|
import org.jgrapht.graph.SimpleDirectedGraph
|
|
|
|
|
import scala.collection.JavaConversions
|
2010-06-23 02:39:20 +08:00
|
|
|
import scala.collection.JavaConversions._
|
2010-06-15 12:43:46 +08:00
|
|
|
import org.broadinstitute.sting.queue.function.{MappingFunction, CommandLineFunction, QFunction}
|
2010-06-23 02:39:20 +08:00
|
|
|
import org.broadinstitute.sting.queue.function.scattergather.ScatterGatherableFunction
|
|
|
|
|
import org.broadinstitute.sting.queue.util.{CollectionUtils, Logging}
|
|
|
|
|
import org.broadinstitute.sting.queue.QException
|
|
|
|
|
import org.jgrapht.alg.CycleDetector
|
|
|
|
|
import org.jgrapht.EdgeFactory
|
2010-06-15 12:43:46 +08:00
|
|
|
|
|
|
|
|
class QGraph extends Logging {
|
|
|
|
|
var dryRun = true
|
|
|
|
|
var bsubAllJobs = false
|
2010-06-29 03:52:17 +08:00
|
|
|
var bsubWaitJobs = false
|
|
|
|
|
var properties = Map.empty[String, String]
|
2010-06-23 02:39:20 +08:00
|
|
|
val jobGraph = newGraph
|
2010-06-15 12:43:46 +08:00
|
|
|
def numJobs = JavaConversions.asSet(jobGraph.edgeSet).filter(_.isInstanceOf[CommandLineFunction]).size
|
|
|
|
|
|
|
|
|
|
def add(command: CommandLineFunction) {
|
2010-06-26 04:51:13 +08:00
|
|
|
addFunction(command)
|
2010-06-15 12:43:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Looks through functions with multiple inputs and outputs and adds mapping functions for single inputs and outputs.
|
|
|
|
|
*/
|
|
|
|
|
def fillIn = {
|
|
|
|
|
// clone since edgeSet is backed by the graph
|
|
|
|
|
for (function <- JavaConversions.asSet(jobGraph.edgeSet).clone) {
|
2010-06-26 04:51:13 +08:00
|
|
|
addCollectionOutputs(function.outputs)
|
|
|
|
|
addCollectionInputs(function.inputs)
|
2010-06-23 02:39:20 +08:00
|
|
|
}
|
2010-06-15 12:43:46 +08:00
|
|
|
|
2010-06-23 02:39:20 +08:00
|
|
|
var pruning = true
|
|
|
|
|
while (pruning) {
|
|
|
|
|
pruning = false
|
|
|
|
|
val filler = jobGraph.edgeSet.filter(isFiller(_))
|
|
|
|
|
if (filler.size > 0) {
|
|
|
|
|
jobGraph.removeAllEdges(filler)
|
|
|
|
|
pruning = true
|
|
|
|
|
}
|
2010-06-15 12:43:46 +08:00
|
|
|
}
|
2010-06-23 02:39:20 +08:00
|
|
|
|
|
|
|
|
jobGraph.removeAllVertices(jobGraph.vertexSet.filter(isOrphan(_)))
|
2010-06-15 12:43:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def run = {
|
|
|
|
|
var isReady = true
|
|
|
|
|
for (function <- JavaConversions.asSet(jobGraph.edgeSet)) {
|
2010-06-23 02:39:20 +08:00
|
|
|
function match {
|
|
|
|
|
case cmd: CommandLineFunction =>
|
|
|
|
|
val missingValues = cmd.missingValues
|
|
|
|
|
if (missingValues.size > 0) {
|
|
|
|
|
isReady = false
|
|
|
|
|
logger.error("Missing values for function: %s".format(cmd.commandLine))
|
|
|
|
|
for (missing <- missingValues)
|
|
|
|
|
logger.error(" " + missing)
|
|
|
|
|
}
|
|
|
|
|
case _ =>
|
2010-06-15 12:43:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
2010-06-23 02:39:20 +08:00
|
|
|
|
|
|
|
|
val detector = new CycleDetector(jobGraph)
|
|
|
|
|
if (detector.detectCycles) {
|
|
|
|
|
logger.error("Cycles were detected in the graph:")
|
|
|
|
|
for (cycle <- detector.findCycles)
|
|
|
|
|
logger.error(" " + cycle)
|
|
|
|
|
isReady = false
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-15 12:43:46 +08:00
|
|
|
if (isReady || this.dryRun)
|
|
|
|
|
(new TopologicalJobScheduler(this) with LsfJobRunner).runJobs
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-23 02:39:20 +08:00
|
|
|
private def newGraph = new SimpleDirectedGraph[QNode, QFunction](new EdgeFactory[QNode, QFunction] {
|
2010-06-26 04:51:13 +08:00
|
|
|
def createEdge(input: QNode, output: QNode) = new MappingFunction(input.items, output.items)})
|
2010-06-23 02:39:20 +08:00
|
|
|
|
2010-06-26 04:51:13 +08:00
|
|
|
private def addFunction(f: QFunction): Unit = {
|
2010-06-23 02:39:20 +08:00
|
|
|
try {
|
|
|
|
|
f.freeze
|
|
|
|
|
|
|
|
|
|
f match {
|
|
|
|
|
case scatterGather: ScatterGatherableFunction if (bsubAllJobs && scatterGather.scatterGatherable) =>
|
|
|
|
|
val functions = scatterGather.generateFunctions()
|
|
|
|
|
if (logger.isTraceEnabled)
|
|
|
|
|
logger.trace("Scattered into %d parts: %s".format(functions.size, functions))
|
2010-06-26 04:51:13 +08:00
|
|
|
functions.foreach(addFunction(_))
|
2010-06-23 02:39:20 +08:00
|
|
|
case _ =>
|
2010-06-26 04:51:13 +08:00
|
|
|
val inputs = QNode(f.inputs)
|
|
|
|
|
val outputs = QNode(f.outputs)
|
2010-06-23 02:39:20 +08:00
|
|
|
val newSource = jobGraph.addVertex(inputs)
|
|
|
|
|
val newTarget = jobGraph.addVertex(outputs)
|
2010-06-26 04:51:13 +08:00
|
|
|
val removedEdges = jobGraph.removeAllEdges(inputs, outputs)
|
2010-06-23 02:39:20 +08:00
|
|
|
val added = jobGraph.addEdge(inputs, outputs, f)
|
|
|
|
|
if (logger.isTraceEnabled) {
|
|
|
|
|
logger.trace("Mapped from: " + inputs)
|
|
|
|
|
logger.trace("Mapped to: " + outputs)
|
|
|
|
|
logger.trace("Mapped via: " + f)
|
|
|
|
|
logger.trace("Removed edges: " + removedEdges)
|
|
|
|
|
logger.trace("New source?: " + newSource)
|
|
|
|
|
logger.trace("New target?: " + newTarget)
|
|
|
|
|
logger.trace("")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
case e: Exception =>
|
|
|
|
|
throw new QException("Error adding function: " + f, e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-26 04:51:13 +08:00
|
|
|
private def addCollectionInputs(value: Any): Unit = {
|
2010-06-23 02:39:20 +08:00
|
|
|
CollectionUtils.foreach(value, (item, collection) =>
|
2010-06-26 04:51:13 +08:00
|
|
|
addMappingEdge(item, collection))
|
2010-06-23 02:39:20 +08:00
|
|
|
}
|
|
|
|
|
|
2010-06-26 04:51:13 +08:00
|
|
|
private def addCollectionOutputs(value: Any): Unit = {
|
2010-06-23 02:39:20 +08:00
|
|
|
CollectionUtils.foreach(value, (item, collection) =>
|
2010-06-26 04:51:13 +08:00
|
|
|
addMappingEdge(collection, item))
|
2010-06-15 12:43:46 +08:00
|
|
|
}
|
|
|
|
|
|
2010-06-26 04:51:13 +08:00
|
|
|
private def addMappingEdge(input: Any, output: Any) = {
|
|
|
|
|
val inputSet = asSet(input)
|
|
|
|
|
val outputSet = asSet(output)
|
|
|
|
|
val hasEdge = inputSet == outputSet ||
|
|
|
|
|
jobGraph.getEdge(QNode(inputSet), QNode(outputSet)) != null ||
|
|
|
|
|
jobGraph.getEdge(QNode(outputSet), QNode(inputSet)) != null
|
|
|
|
|
if (!hasEdge)
|
|
|
|
|
addFunction(new MappingFunction(inputSet, outputSet))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def asSet(value: Any): Set[Any] = if (value.isInstanceOf[Set[_]]) value.asInstanceOf[Set[Any]] else Set(value)
|
2010-06-23 02:39:20 +08:00
|
|
|
|
|
|
|
|
private def isMappingEdge(edge: QFunction) =
|
|
|
|
|
edge.isInstanceOf[MappingFunction]
|
|
|
|
|
|
|
|
|
|
private def isFiller(edge: QFunction) = {
|
|
|
|
|
if (isMappingEdge(edge)) {
|
2010-06-26 04:51:13 +08:00
|
|
|
if (jobGraph.outgoingEdgesOf(jobGraph.getEdgeTarget(edge)).size == 0)
|
2010-06-23 02:39:20 +08:00
|
|
|
true
|
2010-06-26 04:51:13 +08:00
|
|
|
else if (jobGraph.incomingEdgesOf(jobGraph.getEdgeSource(edge)).size == 0)
|
2010-06-23 02:39:20 +08:00
|
|
|
true
|
|
|
|
|
else false
|
|
|
|
|
} else false
|
2010-06-15 12:43:46 +08:00
|
|
|
}
|
2010-06-23 02:39:20 +08:00
|
|
|
|
|
|
|
|
private def isOrphan(node: QNode) =
|
|
|
|
|
(jobGraph.incomingEdgesOf(node).size + jobGraph.outgoingEdgesOf(node).size) == 0
|
2010-06-15 12:43:46 +08:00
|
|
|
}
|