This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
4,187 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
core/src/main/scala/dagr/core/execsystem2/DependencyGraph.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017 Fulcrum Genomics LLC | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
package dagr.core.execsystem2 | ||
|
||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
import com.fulcrumgenomics.commons.util.LazyLogging | ||
import dagr.core.tasksystem.Task | ||
|
||
trait DependencyGraph { | ||
|
||
/** Add a task to the dependency graph and returns true if the task has no dependencies, false otherwise. */ | ||
def add(task: Task): Boolean | ||
|
||
/** None if the task was already added, true if the task was added and has no dependencies, false otherwise. */ | ||
def maybeAdd(task: Task): Option[Boolean] | ||
|
||
/** Removes this task as a dependency for all other tasks in this dependency graph. The task should not depend on | ||
* any tasks, and all tasks that depend on it will have their dependency on this task removed. | ||
*/ | ||
def remove(task: Task): Seq[Task] | ||
|
||
/** Returns None if the task is not in the graph, true if it has dependencies, false otherwise. | ||
*/ | ||
def hasDependencies(task: Task): Option[Boolean] | ||
|
||
/** Returns true if the task is in the graph, false otherwise. */ | ||
def contains(task: Task): Boolean | ||
|
||
def size: Int | ||
|
||
def exceptIfCyclicalDependency(task: Task): Unit | ||
} | ||
|
||
object DependencyGraph { | ||
def apply(): DependencyGraph = new SimpleDependencyGraph | ||
} | ||
|
||
/** | ||
* Simple dependency graph that uses a [[CountDownLatch]] on the number of dependencies for a [[Task]] to block until a | ||
* task has no dependencies. | ||
*/ | ||
private class SimpleDependencyGraph extends DependencyGraph with LazyLogging { | ||
import scala.collection.mutable | ||
|
||
// NB: I think that the dependents in Task could be updated while were are doing this! How do we synchronize? Do we | ||
// have a global lock in the Task object? | ||
def exceptIfCyclicalDependency(task: Task): Unit = this.synchronized { | ||
// check for cycles | ||
if (Task.hasCycle(task)) { | ||
logger.error("Task was part of a graph that had a cycle") | ||
for (component <- Task.findStronglyConnectedComponents(task = task)) { | ||
if (Task.isComponentACycle(component = component)) { | ||
logger.error("Tasks were part of a strongly connected component with a cycle: " | ||
+ component.map(t => s"'${t.name}'").mkString(", ")) | ||
} | ||
} | ||
throw new IllegalArgumentException(s"Task was part of a graph that had a cycle '${task.name}'") | ||
} | ||
} | ||
|
||
// FIXME: no need to be package private | ||
private[execsystem2] val graph: mutable.Map[Task, AtomicInteger] = ExecDef.concurrentMap() | ||
|
||
def maybeAdd(task: Task): Option[Boolean] = this.synchronized { if (contains(task)) None else Some(add(task)) } | ||
|
||
def add(task: Task): Boolean = this.synchronized { | ||
require(!this.graph.contains(task), s"Task '${task.name}' is already part of the dependency graph") | ||
this.graph.put(task, new AtomicInteger(task.tasksDependedOn.size)) | ||
!this.hasDependencies(task).get | ||
} | ||
|
||
/** Removes this task from the dependency graph. It should not depend on any tasks itself, and all tasks that depend | ||
* on it will have their dependency on this task removed. Returns any dependent task that now has no more | ||
* dependencies. | ||
*/ | ||
def remove(task: Task): Seq[Task] = { | ||
// NB: we decrement the countdown latch for each task that depended on this task. If this decrement causes any of | ||
// the dependent tasks to have their latch reach zero, then they will return from waiting on their latch and exit | ||
// the [[add]] method. | ||
require(task.tasksDependedOn.isEmpty, | ||
s"Removing a task '${task.name}' from the dependency graph that has dependencies: " | ||
+ task.tasksDependedOn.map(_.name).mkString(", ")) | ||
// remove this as a dependency for all other tasks that depend on this task | ||
task.tasksDependingOnThisTask.flatMap { dependent => | ||
dependent.synchronized { | ||
require(this.graph.contains(dependent), s"Dependent '${dependent.name}' not in the dependency graph") | ||
task !=> dependent | ||
val latch = this.graph(dependent) | ||
if (latch.decrementAndGet() == 0) Some(dependent) else None | ||
} | ||
}.toSeq | ||
} | ||
|
||
def hasDependencies(task: Task): Option[Boolean] = { | ||
this.graph.get(task).map { e => e.get() > 0 } | ||
} | ||
|
||
def contains(task: Task): Boolean = this.graph.contains(task) | ||
|
||
def size: Int = this.graph.size | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017 Fulcrum Genomics LLC | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
package dagr.core.execsystem2 | ||
|
||
import scala.collection.mutable | ||
|
||
private[execsystem2] object ExecDef { | ||
/** Create a thread-safe mutable map. */ | ||
def concurrentMap[A,B](): mutable.Map[A,B] = { | ||
import scala.collection.convert.decorateAsScala._ | ||
new java.util.concurrent.ConcurrentHashMap[A, B]().asScala | ||
} | ||
|
||
def concurrentSet[A](): mutable.Set[A] = { | ||
import scala.collection.convert.decorateAsScala._ | ||
val map: java.util.Map[A, java.lang.Boolean] = new java.util.concurrent.ConcurrentHashMap[A, java.lang.Boolean]() | ||
val set: java.util.Set[A] = java.util.Collections.newSetFromMap[A](map) | ||
set.asScala | ||
} | ||
} |
Oops, something went wrong.