Skip to content

Commit

Permalink
Changed locking mechanism in MaxNormalizingScoreBoard
Browse files Browse the repository at this point in the history
might be cause of #439, requires more investigation
  • Loading branch information
lucaro committed Oct 19, 2023
1 parent c0e326a commit b8d348f
Showing 1 changed file with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import dev.dres.run.eventstream.EventStreamProcessor
import dev.dres.run.eventstream.ScoreUpdateEvent
import dev.dres.utilities.extensions.convertWriteLock
import dev.dres.utilities.extensions.write
import java.util.concurrent.locks.ReentrantReadWriteLock
import java.util.concurrent.locks.StampedLock
import kotlin.concurrent.read
import kotlin.concurrent.write
import kotlin.math.max

/**
Expand All @@ -22,7 +25,7 @@ import kotlin.math.max
class MaxNormalizingScoreBoard(override val name: String, override val run: EvaluationRun, private val teamIds: List<TeamId>, private val taskFilter: (ApiTaskTemplate) -> Boolean, private val taskGroupName: String? = null, private val maxScoreNormalized: Double = 1000.0) : Scoreboard {

/** A [StampedLock] to synchronise access to this [MaxNormalizingScoreBoard]. */
private val lock = StampedLock()
private val lock = ReentrantReadWriteLock()

/** Tracks the score per [TeamId]. */
@Volatile
Expand All @@ -39,15 +42,11 @@ class MaxNormalizingScoreBoard(override val name: String, override val run: Eval
* @return List of [Score] for this [MaxNormalizingScoreBoard].
*/
override fun scores(): List<Score> {
var stamp = this.lock.readLock()
try {
this.lock.read {
if (this.dirty) {
stamp = this.lock.convertWriteLock(stamp)
this.recalculate()
}
return this.teamIds.map { Score(it, this.scores[it] ?: 0.0) }
} finally {
this.lock.unlock(stamp)
}
}

Expand All @@ -58,15 +57,11 @@ class MaxNormalizingScoreBoard(override val name: String, override val run: Eval
* @return The score for the given [TeamId].
*/
override fun score(teamId: TeamId): Double {
var stamp = this.lock.readLock()
try {
this.lock.read {
if (this.dirty) {
stamp = this.lock.convertWriteLock(stamp)
recalculate()
this.recalculate()
}
return this.scores[teamId] ?: 0.0
} finally {
this.lock.unlock(stamp)
}
}

Expand Down Expand Up @@ -103,8 +98,10 @@ class MaxNormalizingScoreBoard(override val name: String, override val run: Eval
val maxScore = max(1.0, scoreSums.values.maxOrNull() ?: return)

/* Update local score map. */
this.scores = scoreSums.mapValues { it.value * maxScoreNormalized / maxScore }
this.dirty = false
this.lock.write {
this.scores = scoreSums.mapValues { it.value * maxScoreNormalized / maxScore }
this.dirty = false
}

/* Emit event */
EventStreamProcessor.event(ScoreUpdateEvent(this.run.id, this.name, this.scores))
Expand Down

0 comments on commit b8d348f

Please sign in to comment.