Skip to content

Commit

Permalink
fix(model-server): deadlock between transactions
Browse files Browse the repository at this point in the history
The server sometimes didn't respond anymore because of a deadlock
between ignite transactions. Lock ordering is a common strategy to avoid
deadlocks, which is simply done by sorting the entries before writing
them to the ignite cache.
  • Loading branch information
slisson committed May 17, 2024
1 parent 71ca7cd commit 852a0c5
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,16 @@ class IgniteStoreClient(jdbcConfFile: File? = null, inmemory: Boolean = false) :
}

override fun putAll(entries: Map<String, String?>, silent: Boolean) {
val deletes = entries.filterValues { it == null }
val puts = entries.filterValues { it != null }
// Sorting is important to avoid deadlocks (lock ordering).
// The documentation of IgniteCache.putAll also states that this a requirement.
val sortedEntries = entries.toSortedMap()
val deletes = sortedEntries.filterValues { it == null }
val puts = sortedEntries.filterValues { it != null }
runTransaction {
if (deletes.isNotEmpty()) cache.removeAll(deletes.keys)
if (puts.isNotEmpty()) cache.putAll(puts)
if (!silent) {
for (key in entries.keys) {
for (key in sortedEntries.keys) {
if (HashUtil.isSha256(key)) continue
pendingChangeMessages.entryChanged(key)
}
Expand Down

0 comments on commit 852a0c5

Please sign in to comment.