Use reTransact when loading the cache for database objects (#2179)

Cache loads will likely always be inner transactions, if they have a transaction at all. Cache loads do not always call a transaction since they are only necessary if the cache is not fresh at the time it is called. Since the cache itself needs to decide whether or not a DB transaction is necessary, it should use the reTransact method to safely indicate that the isolation level of the outer transaction is what should be used.
This commit is contained in:
sarahcaseybot 2023-10-16 15:22:22 -04:00 committed by GitHub
parent 0e4ccf6b85
commit 52b94deb9e
5 changed files with 12 additions and 7 deletions

View file

@ -305,14 +305,14 @@ public class AllocationToken extends UpdateAutoTimestampEntity implements Builda
new CacheLoader<VKey<AllocationToken>, Optional<AllocationToken>>() { new CacheLoader<VKey<AllocationToken>, Optional<AllocationToken>>() {
@Override @Override
public Optional<AllocationToken> load(VKey<AllocationToken> key) { public Optional<AllocationToken> load(VKey<AllocationToken> key) {
return tm().transact(() -> tm().loadByKeyIfPresent(key)); return tm().reTransact(() -> tm().loadByKeyIfPresent(key));
} }
@Override @Override
public Map<VKey<AllocationToken>, Optional<AllocationToken>> loadAll( public Map<VKey<AllocationToken>, Optional<AllocationToken>> loadAll(
Iterable<? extends VKey<AllocationToken>> keys) { Iterable<? extends VKey<AllocationToken>> keys) {
ImmutableSet<VKey<AllocationToken>> keySet = ImmutableSet.copyOf(keys); ImmutableSet<VKey<AllocationToken>> keySet = ImmutableSet.copyOf(keys);
return tm().transact( return tm().reTransact(
() -> () ->
keySet.stream() keySet.stream()
.collect( .collect(

View file

@ -200,7 +200,11 @@ public class Registrar extends UpdateAutoTimestampEntity implements Buildable, J
/** A caching {@link Supplier} of a registrarId to {@link Registrar} map. */ /** A caching {@link Supplier} of a registrarId to {@link Registrar} map. */
private static final Supplier<ImmutableMap<String, Registrar>> CACHE_BY_REGISTRAR_ID = private static final Supplier<ImmutableMap<String, Registrar>> CACHE_BY_REGISTRAR_ID =
memoizeWithShortExpiration(() -> Maps.uniqueIndex(loadAll(), Registrar::getRegistrarId)); memoizeWithShortExpiration(
() ->
Maps.uniqueIndex(
tm().reTransact(() -> tm().loadAllOf(Registrar.class)),
Registrar::getRegistrarId));
/** /**
* Unique registrar client id. Must conform to "clIDType" as defined in RFC5730. * Unique registrar client id. Must conform to "clIDType" as defined in RFC5730.

View file

@ -28,7 +28,7 @@ public class SignedMarkRevocationListDao {
/** Loads the {@link SignedMarkRevocationList}. */ /** Loads the {@link SignedMarkRevocationList}. */
static SignedMarkRevocationList load() { static SignedMarkRevocationList load() {
Optional<SignedMarkRevocationList> smdrl = Optional<SignedMarkRevocationList> smdrl =
tm().transact( tm().reTransact(
() -> { () -> {
Long revisionId = Long revisionId =
tm().query("SELECT MAX(revisionId) FROM SignedMarkRevocationList", Long.class) tm().query("SELECT MAX(revisionId) FROM SignedMarkRevocationList", Long.class)

View file

@ -233,7 +233,8 @@ public class Tld extends ImmutableObject implements Buildable, UnsafeSerializabl
new CacheLoader<String, Tld>() { new CacheLoader<String, Tld>() {
@Override @Override
public Tld load(final String tld) { public Tld load(final String tld) {
return tm().transact(() -> tm().loadByKeyIfPresent(createVKey(tld))).orElse(null); return tm().reTransact(() -> tm().loadByKeyIfPresent(createVKey(tld)))
.orElse(null);
} }
@Override @Override
@ -241,7 +242,7 @@ public class Tld extends ImmutableObject implements Buildable, UnsafeSerializabl
ImmutableMap<String, VKey<Tld>> keysMap = ImmutableMap<String, VKey<Tld>> keysMap =
toMap(ImmutableSet.copyOf(tlds), Tld::createVKey); toMap(ImmutableSet.copyOf(tlds), Tld::createVKey);
Map<VKey<? extends Tld>, Tld> entities = Map<VKey<? extends Tld>, Tld> entities =
tm().transact(() -> tm().loadByKeysIfPresent(keysMap.values())); tm().reTransact(() -> tm().loadByKeysIfPresent(keysMap.values()));
return Maps.transformEntries(keysMap, (k, v) -> entities.getOrDefault(v, null)); return Maps.transformEntries(keysMap, (k, v) -> entities.getOrDefault(v, null));
} }
}); });

View file

@ -56,7 +56,7 @@ public final class Tlds {
private static Supplier<ImmutableMap<String, TldType>> createFreshCache() { private static Supplier<ImmutableMap<String, TldType>> createFreshCache() {
return memoizeWithShortExpiration( return memoizeWithShortExpiration(
() -> () ->
tm().transact( tm().reTransact(
() -> { () -> {
EntityManager entityManager = tm().getEntityManager(); EntityManager entityManager = tm().getEntityManager();
Stream<?> resultStream = Stream<?> resultStream =