-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improve :tool:execution:parallel. (#2080)
* Integrate :tool:execution:parallel with :tool:log. * Add function for creating Parallel.Type object dynamically. * Improve error message for Parallel.Context.lazyProperty. * Move implementations to internal package. * Add function for checking returned state. * Add selector for context property
- Loading branch information
Showing
19 changed files
with
145 additions
and
70 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
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
15 changes: 12 additions & 3 deletions
15
tool/execution/parallel/src/main/kotlin/flank/exection/parallel/Reduce.kt
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 |
---|---|---|
@@ -1,23 +1,32 @@ | ||
package flank.exection.parallel | ||
|
||
import flank.exection.parallel.internal.contextValidators | ||
import flank.exection.parallel.internal.contextValidatorTypes | ||
import flank.exection.parallel.internal.reduceTo | ||
import flank.exection.parallel.internal.type | ||
|
||
/** | ||
* Reduce given [Tasks] by [expected] types to remove unneeded tasks from the graph. | ||
* The returned graph will hold only tasks that are returning selected types, their dependencies and derived dependencies. | ||
* Additionally this is keeping also the validators for initial state. | ||
* Additionally, this is keeping also the validators for initial state. | ||
* | ||
* @return Reduced [Tasks] | ||
*/ | ||
operator fun Tasks.invoke( | ||
expected: Set<Parallel.Type<*>> | ||
): Tasks = | ||
reduceTo(expected + contextValidators()) | ||
reduceTo(expected + contextValidatorTypes()) | ||
|
||
/** | ||
* Shortcut for tasks reducing. | ||
*/ | ||
operator fun Tasks.invoke( | ||
vararg expected: Parallel.Type<*> | ||
): Tasks = invoke(expected.toSet()) | ||
|
||
/** | ||
* Remove the [Tasks] by given [types]. | ||
*/ | ||
operator fun Tasks.minus( | ||
types: Set<Parallel.Type<*>> | ||
): Tasks = | ||
filterNot { task -> task.type in types }.toSet() |
7 changes: 7 additions & 0 deletions
7
tool/execution/parallel/src/main/kotlin/flank/exection/parallel/Select.kt
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,7 @@ | ||
package flank.exection.parallel | ||
|
||
/** | ||
* Select value by type. | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
fun <T : Any> ParallelState.select(type: Parallel.Type<T>) = get(type) as T |
36 changes: 3 additions & 33 deletions
36
tool/execution/parallel/src/main/kotlin/flank/exection/parallel/Validate.kt
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 |
---|---|---|
@@ -1,42 +1,12 @@ | ||
package flank.exection.parallel | ||
|
||
import flank.exection.parallel.internal.args | ||
import flank.exection.parallel.internal.graph.findCycles | ||
import flank.exection.parallel.internal.graph.findDuplicatedDependencies | ||
import flank.exection.parallel.internal.graph.findMissingDependencies | ||
import flank.exection.parallel.internal.type | ||
import kotlinx.coroutines.runBlocking | ||
import flank.exection.parallel.internal.validateExecutionGraphs | ||
|
||
/** | ||
* Validate the given [Tasks] and [ParallelState] for finding missing dependencies or broken paths. | ||
* | ||
* @param initial The initial arguments for tasks execution. | ||
* @return Valid [Tasks] if graph has no broken paths or missing dependencies. | ||
*/ | ||
fun Tasks.validate(initial: ParallelState = emptyMap()): Tasks = run { | ||
// Separate initial validators from tasks. Validators are important now but not during the execution. | ||
val (validators, tasks) = splitTasks() | ||
|
||
// check if initial state is providing all required values specified in context. | ||
runBlocking { validators.forEach { check -> check.execute(initial) } } | ||
|
||
map(Parallel.Task<*>::type).findDuplicatedDependencies(initial.keys).run { | ||
if (isNotEmpty()) throw Parallel.DependenciesError.Duplicate(this) | ||
} | ||
|
||
val graph = associate { task -> task.type to task.args } | ||
|
||
graph.findMissingDependencies(initial.keys).run { | ||
if (isNotEmpty()) throw Parallel.DependenciesError.Missing(this) | ||
} | ||
|
||
graph.findCycles().run { | ||
if (isNotEmpty()) throw Parallel.DependenciesError.Cycles(this) | ||
} | ||
|
||
tasks.toSet() | ||
} | ||
|
||
private fun Iterable<Parallel.Task<*>>.splitTasks() = this | ||
.groupBy { task -> task.type is Parallel.Context } | ||
.run { getOrDefault(true, emptyList()) to getOrDefault(false, emptyList()) } | ||
fun Tasks.validate(initial: ParallelState = emptyMap()): Tasks = | ||
validateExecutionGraphs(initial) |
8 changes: 8 additions & 0 deletions
8
tool/execution/parallel/src/main/kotlin/flank/exection/parallel/Verify.kt
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,8 @@ | ||
package flank.exection.parallel | ||
|
||
import flank.exection.parallel.internal.checkThrowableValues | ||
|
||
/** | ||
* Verify that given [ParallelState] has no errors. | ||
*/ | ||
fun ParallelState.verify(): ParallelState = checkThrowableValues() |
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
9 changes: 9 additions & 0 deletions
9
tool/execution/parallel/src/main/kotlin/flank/exection/parallel/internal/DynamicType.kt
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,9 @@ | ||
package flank.exection.parallel.internal | ||
|
||
import flank.exection.parallel.Parallel | ||
|
||
internal class DynamicType<T : Any>(val type: Class<T>) : Parallel.Type<T> { | ||
override fun equals(other: Any?): Boolean = (other as? DynamicType<*>)?.type == type | ||
override fun hashCode(): Int = type.hashCode() + javaClass.hashCode() | ||
override fun toString(): String = type.canonicalName | ||
} |
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
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
36 changes: 36 additions & 0 deletions
36
tool/execution/parallel/src/main/kotlin/flank/exection/parallel/internal/Validate.kt
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,36 @@ | ||
package flank.exection.parallel.internal | ||
|
||
import flank.exection.parallel.Parallel | ||
import flank.exection.parallel.ParallelState | ||
import flank.exection.parallel.Tasks | ||
import flank.exection.parallel.internal.graph.findCycles | ||
import flank.exection.parallel.internal.graph.findDuplicatedDependencies | ||
import flank.exection.parallel.internal.graph.findMissingDependencies | ||
import kotlinx.coroutines.runBlocking | ||
|
||
// TODO: Check all cases and collect results, instead of throwing the first encountered error. | ||
internal fun Tasks.validateExecutionGraphs(initial: ParallelState = emptyMap()): Tasks = run { | ||
// Separate initial validators from tasks. Validators are important now but not during the execution. | ||
val (validators, tasks) = this | ||
.groupBy { task -> task.type is Parallel.Context } | ||
.run { getOrDefault(true, emptyList()) to getOrDefault(false, emptyList()) } | ||
|
||
// check if initial state is providing all required values specified in context. | ||
runBlocking { validators.forEach { check -> check.execute(initial) } } | ||
|
||
map(Parallel.Task<*>::type).findDuplicatedDependencies(initial.keys).run { | ||
if (isNotEmpty()) throw Parallel.DependenciesError.Duplicate(this) | ||
} | ||
|
||
val graph = associate { task -> task.type to task.args } | ||
|
||
graph.findMissingDependencies(initial.keys).run { | ||
if (isNotEmpty()) throw Parallel.DependenciesError.Missing(this) | ||
} | ||
|
||
graph.findCycles().run { | ||
if (isNotEmpty()) throw Parallel.DependenciesError.Cycles(this) | ||
} | ||
|
||
tasks.toSet() | ||
} |
6 changes: 6 additions & 0 deletions
6
tool/execution/parallel/src/main/kotlin/flank/exection/parallel/internal/Verify.kt
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,6 @@ | ||
package flank.exection.parallel.internal | ||
|
||
import flank.exection.parallel.ParallelState | ||
|
||
internal fun ParallelState.checkThrowableValues(): ParallelState = | ||
onEach { (_, value) -> if (value is Throwable) throw value } |
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
Oops, something went wrong.