Add a back-off period when replacing cache entries fails

Closes #28388

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
Alexander Schwartz 2024-04-03 11:10:39 +02:00 committed by Alexander Schwartz
parent 63e7523a6d
commit 355901dfd8
2 changed files with 65 additions and 40 deletions

View file

@ -105,6 +105,7 @@ public class EmbeddedCachesChangesPerformer<K, V extends SessionEntity> implemen
if (LOG.isDebugEnabled()) {
LOG.debugf("Replace failed for entity: %s, old version %s, new version %s. Will try again", key, oldVersion.getVersion(), newVersionEntity.getVersion());
}
backoff(iteration);
oldVersion = cache.get(key);
@ -129,6 +130,18 @@ public class EmbeddedCachesChangesPerformer<K, V extends SessionEntity> implemen
});
}
/**
* Wait a random amount of time to avoid a conflict with other concurrent actors on the next attempt.
*/
private static void backoff(int iteration) {
try {
Thread.sleep(new Random().nextInt(iteration));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
private SessionEntityWrapper<V> generateNewVersionAndWrapEntity(V entity, Map<String, String> localMetadata) {
return new SessionEntityWrapper<>(localMetadata, entity);
}

View file

@ -254,6 +254,7 @@ public class InfinispanChangelogBasedTransaction<K, V extends SessionEntity> ext
if (logger.isDebugEnabled()) {
logger.debugf("Replace failed for entity: %s, old version %s, new version %s. Will try again", key, oldVersion.getVersion(), newVersionEntity.getVersion());
}
backoff(iteration);
oldVersion = cache.get(key);
@ -278,6 +279,17 @@ public class InfinispanChangelogBasedTransaction<K, V extends SessionEntity> ext
});
}
/**
* Wait a random amount of time to avoid a conflict with other concurrent actors on the next attempt.
*/
private static void backoff(int iteration) {
try {
Thread.sleep(new Random().nextInt(iteration));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
@Override
protected void rollbackImpl() {