Skip to content

Make the peek method behaviour in the path selector deterministic #598 #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ abstract class NonUniformRandomSearch(

private val randomGen: Random? = seed?.let { Random(seed) }

// We use this value to avoid non-deterministic behaviour of the
// peek method. Without it, we might have different states for
// a sequence of the `peek` calls.
// Now we remember it at the first `peek` call and use it
// until a first `poll` call. The first call should reset it back to null.
private var lastTakenRandomValue: Double? = null

override fun update() {
executionQueue.updateAll()
}
Expand All @@ -75,15 +82,22 @@ abstract class NonUniformRandomSearch(
* with probability executionState.asWeight.weight / sumWeights
*/
override fun peekImpl(): ExecutionState? {
val rand = (randomGen?.nextDouble() ?: 1.0) * sumWeights
if (lastTakenRandomValue == null) {
lastTakenRandomValue = randomGen?.nextDouble()
}

val rand = (lastTakenRandomValue ?: 1.0) * sumWeights

return executionQueue.findLeftest(rand)?.first
}

override fun removeImpl(state: ExecutionState): Boolean = executionQueue.remove(state)

override fun pollImpl(): ExecutionState? =
peekImpl()?.also { remove(it) }
peekImpl()?.also {
remove(it)
lastTakenRandomValue = null
}

override fun isEmpty() =
executionQueue.isEmpty()
Expand Down