Improve cache loading in Registries.java (#1558)

* Improve cache loading in Registries.java

The loader for the TLD cache in Registries.java unnecessarily reads from
another cache when running with SQL, potentially triggering additional
database access. This code runs in the whois query path, and contributes
to the high latency in sandbox.
This commit is contained in:
Weimin Yu 2022-03-15 22:02:05 -04:00 committed by GitHub
parent 767e3935af
commit 6dd6ebce75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -24,6 +24,7 @@ import static com.google.common.collect.Maps.filterValues;
import static google.registry.model.CacheUtils.memoizeWithShortExpiration;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.auditedOfy;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.util.CollectionUtils.entriesToImmutableMap;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
@ -38,6 +39,8 @@ import com.google.common.net.InternetDomainName;
import com.googlecode.objectify.Key;
import google.registry.model.tld.Registry.TldType;
import java.util.Optional;
import java.util.stream.Stream;
import javax.persistence.EntityManager;
/** Utilities for finding and listing {@link Registry} entities. */
public final class Registries {
@ -58,9 +61,9 @@ public final class Registries {
() ->
tm().doTransactionless(
() -> {
if (tm().isOfy()) {
ImmutableSet<String> tlds =
tm().isOfy()
? auditedOfy()
auditedOfy()
.load()
.type(Registry.class)
.ancestor(getCrossTldKey())
@ -68,13 +71,21 @@ public final class Registries {
.list()
.stream()
.map(Key::getName)
.collect(toImmutableSet())
: tm().loadAllOf(Registry.class).stream()
.map(Registry::getTldStr)
.collect(toImmutableSet());
return Registry.getAll(tlds).stream()
.map(e -> Maps.immutableEntry(e.getTldStr(), e.getTldType()))
.collect(entriesToImmutableMap());
} else {
EntityManager entityManager = jpaTm().getEntityManager();
Stream<?> resultStream =
entityManager
.createQuery("SELECT tldStrId, tldType FROM Tld")
.getResultStream();
return resultStream
.map(e -> ((Object[]) e))
.map(e -> Maps.immutableEntry((String) e[0], ((TldType) e[1])))
.collect(entriesToImmutableMap());
}
}));
}