Skip to content

Commit

Permalink
fix: Deadlock in PostgreSQL.
Browse files Browse the repository at this point in the history
  • Loading branch information
armory-abedonik committed Apr 6, 2023
1 parent e200250 commit 69eb4d0
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions keiko-sql/src/main/kotlin/com/netflix/spinnaker/q/sql/SqlQueue.kt
Expand Up @@ -281,22 +281,13 @@ class SqlQueue(
return
}

candidates.shuffle()

var position = 0
var passes = 0
while (changed < maxMessages && position < candidates.size && passes < 3) {
passes++
val sliceNext = min(maxMessages - 1 - changed, candidates.size - 1 - position)
val ids = candidates.slice(IntRange(position, position + sliceNext))
when (sliceNext) {
0 -> position++
else -> position += sliceNext
}
var chunkedCandidates = candidates.sorted().chunked(maxMessages)
chunkedCandidates = if (chunkedCandidates.size > 3) chunkedCandidates.subList(0, 3) else chunkedCandidates

chunkedCandidates.forEach {
changed += jooq.update(queueTable)
.set(lockedField, "$lockId:$now")
.where(idField.`in`(*ids.toTypedArray()), lockedField.eq("0"))
.where(idField.`in`(*it.toTypedArray()), lockedField.eq("0"))
.execute()
}

Expand Down

0 comments on commit 69eb4d0

Please sign in to comment.