Replace PubApi master calls with replica (#1742)

This commit is contained in:
Pavlo Tkach 2022-08-26 10:15:30 -04:00 committed by GitHub
parent 7aec579d96
commit 78249e1329
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 39 additions and 26 deletions

View file

@ -15,7 +15,7 @@
package google.registry.rdap; package google.registry.rdap;
import static google.registry.flows.domain.DomainFlowUtils.validateDomainName; import static google.registry.flows.domain.DomainFlowUtils.validateDomainName;
import static google.registry.model.EppResourceUtils.loadByForeignKey; import static google.registry.model.EppResourceUtils.loadByForeignKeyCached;
import static google.registry.request.Action.Method.GET; import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD; import static google.registry.request.Action.Method.HEAD;
import static google.registry.util.DateTimeUtils.START_OF_TIME; import static google.registry.util.DateTimeUtils.START_OF_TIME;
@ -60,7 +60,7 @@ public class RdapDomainAction extends RdapActionBase {
} }
// The query string is not used; the RDAP syntax is /rdap/domain/mydomain.com. // The query string is not used; the RDAP syntax is /rdap/domain/mydomain.com.
Optional<Domain> domain = Optional<Domain> domain =
loadByForeignKey( loadByForeignKeyCached(
Domain.class, Domain.class,
pathSearchString, pathSearchString,
shouldIncludeDeleted() ? START_OF_TIME : rdapJsonFormatter.getRequestTime()); shouldIncludeDeleted() ? START_OF_TIME : rdapJsonFormatter.getRequestTime());

View file

@ -15,7 +15,7 @@
package google.registry.rdap; package google.registry.rdap;
import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.model.EppResourceUtils.loadByForeignKey; import static google.registry.model.EppResourceUtils.loadByForeignKeyCached;
import static google.registry.model.index.ForeignKeyIndex.loadAndGetKey; import static google.registry.model.index.ForeignKeyIndex.loadAndGetKey;
import static google.registry.model.ofy.ObjectifyService.auditedOfy; import static google.registry.model.ofy.ObjectifyService.auditedOfy;
import static google.registry.persistence.transaction.TransactionManagerFactory.replicaJpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.replicaJpaTm;
@ -188,7 +188,8 @@ public class RdapDomainSearchAction extends RdapSearchActionBase {
private DomainSearchResponse searchByDomainNameWithoutWildcard( private DomainSearchResponse searchByDomainNameWithoutWildcard(
final RdapSearchPattern partialStringQuery) { final RdapSearchPattern partialStringQuery) {
Optional<Domain> domain = Optional<Domain> domain =
loadByForeignKey(Domain.class, partialStringQuery.getInitialString(), getRequestTime()); loadByForeignKeyCached(
Domain.class, partialStringQuery.getInitialString(), getRequestTime());
return makeSearchResults( return makeSearchResults(
shouldBeVisible(domain) ? ImmutableList.of(domain.get()) : ImmutableList.of()); shouldBeVisible(domain) ? ImmutableList.of(domain.get()) : ImmutableList.of());
} }
@ -389,7 +390,7 @@ public class RdapDomainSearchAction extends RdapSearchActionBase {
Optional<String> desiredRegistrar = getDesiredRegistrar(); Optional<String> desiredRegistrar = getDesiredRegistrar();
if (desiredRegistrar.isPresent()) { if (desiredRegistrar.isPresent()) {
Optional<Host> host = Optional<Host> host =
loadByForeignKey( loadByForeignKeyCached(
Host.class, Host.class,
partialStringQuery.getInitialString(), partialStringQuery.getInitialString(),
shouldIncludeDeleted() ? START_OF_TIME : getRequestTime()); shouldIncludeDeleted() ? START_OF_TIME : getRequestTime());
@ -414,7 +415,7 @@ public class RdapDomainSearchAction extends RdapSearchActionBase {
// through the subordinate hosts. This is more efficient, and lets us permit wildcard searches // through the subordinate hosts. This is more efficient, and lets us permit wildcard searches
// with no initial string. // with no initial string.
Domain domain = Domain domain =
loadByForeignKey( loadByForeignKeyCached(
Domain.class, Domain.class,
partialStringQuery.getSuffix(), partialStringQuery.getSuffix(),
shouldIncludeDeleted() ? START_OF_TIME : getRequestTime()) shouldIncludeDeleted() ? START_OF_TIME : getRequestTime())
@ -431,7 +432,7 @@ public class RdapDomainSearchAction extends RdapSearchActionBase {
if (partialStringQuery.matches(fqhn)) { if (partialStringQuery.matches(fqhn)) {
if (desiredRegistrar.isPresent()) { if (desiredRegistrar.isPresent()) {
Optional<Host> host = Optional<Host> host =
loadByForeignKey( loadByForeignKeyCached(
Host.class, fqhn, shouldIncludeDeleted() ? START_OF_TIME : getRequestTime()); Host.class, fqhn, shouldIncludeDeleted() ? START_OF_TIME : getRequestTime());
if (host.isPresent() if (host.isPresent()
&& desiredRegistrar && desiredRegistrar

View file

@ -14,7 +14,7 @@
package google.registry.rdap; package google.registry.rdap;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.replicaJpaTm;
import static google.registry.rdap.RdapUtils.getRegistrarByIanaIdentifier; import static google.registry.rdap.RdapUtils.getRegistrarByIanaIdentifier;
import static google.registry.rdap.RdapUtils.getRegistrarByName; import static google.registry.rdap.RdapUtils.getRegistrarByName;
import static google.registry.request.Action.Method.GET; import static google.registry.request.Action.Method.GET;
@ -71,7 +71,7 @@ public class RdapEntityAction extends RdapActionBase {
if (ROID_PATTERN.matcher(pathSearchString).matches()) { if (ROID_PATTERN.matcher(pathSearchString).matches()) {
VKey<ContactResource> contactVKey = VKey.create(ContactResource.class, pathSearchString); VKey<ContactResource> contactVKey = VKey.create(ContactResource.class, pathSearchString);
Optional<ContactResource> contactResource = Optional<ContactResource> contactResource =
tm().transact(() -> tm().loadByKeyIfPresent(contactVKey)); replicaJpaTm().transact(() -> replicaJpaTm().loadByKeyIfPresent(contactVKey));
// As per Andy Newton on the regext mailing list, contacts by themselves have no role, since // As per Andy Newton on the regext mailing list, contacts by themselves have no role, since
// they are global, and might have different roles for different domains. // they are global, and might have different roles for different domains.
if (contactResource.isPresent() && isAuthorized(contactResource.get())) { if (contactResource.isPresent() && isAuthorized(contactResource.get())) {

View file

@ -324,9 +324,11 @@ public class RdapEntitySearchAction extends RdapSearchActionBase {
contactResourceList = ImmutableList.of(); contactResourceList = ImmutableList.of();
} else { } else {
Optional<ContactResource> contactResource = Optional<ContactResource> contactResource =
tm().transact( replicaJpaTm()
.transact(
() -> () ->
tm().loadByKeyIfPresent( replicaJpaTm()
.loadByKeyIfPresent(
VKey.create( VKey.create(
ContactResource.class, partialStringQuery.getInitialString()))); ContactResource.class, partialStringQuery.getInitialString())));
contactResourceList = contactResourceList =

View file

@ -20,7 +20,7 @@ import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap; import static com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap;
import static google.registry.model.EppResourceUtils.isLinked; import static google.registry.model.EppResourceUtils.isLinked;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.replicaJpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.rdap.RdapIcannStandardInformation.CONTACT_REDACTED_VALUE; import static google.registry.rdap.RdapIcannStandardInformation.CONTACT_REDACTED_VALUE;
import static google.registry.util.CollectionUtils.union; import static google.registry.util.CollectionUtils.union;
@ -357,10 +357,15 @@ public class RdapJsonFormatter {
// Kick off the database loads of the nameservers that we will need, so it can load // Kick off the database loads of the nameservers that we will need, so it can load
// asynchronously while we load and process the contacts. // asynchronously while we load and process the contacts.
ImmutableSet<Host> loadedHosts = ImmutableSet<Host> loadedHosts =
tm().transact(() -> ImmutableSet.copyOf(tm().loadByKeys(domain.getNameservers()).values())); replicaJpaTm()
.transact(
() ->
ImmutableSet.copyOf(
replicaJpaTm().loadByKeys(domain.getNameservers()).values()));
// Load the registrant and other contacts and add them to the data. // Load the registrant and other contacts and add them to the data.
ImmutableMap<VKey<? extends ContactResource>, ContactResource> loadedContacts = ImmutableMap<VKey<? extends ContactResource>, ContactResource> loadedContacts =
tm().transact(() -> tm().loadByKeysIfPresent(domain.getReferencedContacts())); replicaJpaTm()
.transact(() -> replicaJpaTm().loadByKeysIfPresent(domain.getReferencedContacts()));
// RDAP Response Profile 2.7.3, A domain MUST have the REGISTRANT, ADMIN, TECH roles and MAY // RDAP Response Profile 2.7.3, A domain MUST have the REGISTRANT, ADMIN, TECH roles and MAY
// have others. We also add the BILLING. // have others. We also add the BILLING.
// //
@ -439,9 +444,11 @@ public class RdapJsonFormatter {
statuses.add(StatusValue.LINKED); statuses.add(StatusValue.LINKED);
} }
if (host.isSubordinate() if (host.isSubordinate()
&& tm().transact( && replicaJpaTm()
.transact(
() -> () ->
tm().loadByKey(host.getSuperordinateDomain()) replicaJpaTm()
.loadByKey(host.getSuperordinateDomain())
.cloneProjectedAtTime(getRequestTime()) .cloneProjectedAtTime(getRequestTime())
.getStatusValues() .getStatusValues()
.contains(StatusValue.PENDING_TRANSFER))) { .contains(StatusValue.PENDING_TRANSFER))) {
@ -899,7 +906,8 @@ public class RdapJsonFormatter {
.replace("%repoIdField%", repoIdFieldName) .replace("%repoIdField%", repoIdFieldName)
.replace("%repoIdValue%", resourceVkey.getSqlKey().toString()); .replace("%repoIdValue%", resourceVkey.getSqlKey().toString());
historyEntries = historyEntries =
jpaTm().transact(() -> jpaTm().getEntityManager().createQuery(jpql).getResultList()); replicaJpaTm()
.transact(() -> replicaJpaTm().getEntityManager().createQuery(jpql).getResultList());
} }
for (HistoryEntry historyEntry : historyEntries) { for (HistoryEntry historyEntry : historyEntries) {
EventAction rdapEventAction = EventAction rdapEventAction =

View file

@ -15,7 +15,7 @@
package google.registry.rdap; package google.registry.rdap;
import static google.registry.flows.host.HostFlowUtils.validateHostName; import static google.registry.flows.host.HostFlowUtils.validateHostName;
import static google.registry.model.EppResourceUtils.loadByForeignKey; import static google.registry.model.EppResourceUtils.loadByForeignKeyCached;
import static google.registry.request.Action.Method.GET; import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD; import static google.registry.request.Action.Method.HEAD;
import static google.registry.util.DateTimeUtils.START_OF_TIME; import static google.registry.util.DateTimeUtils.START_OF_TIME;
@ -62,7 +62,7 @@ public class RdapNameserverAction extends RdapActionBase {
// If there are no undeleted nameservers with the given name, the foreign key should point to // If there are no undeleted nameservers with the given name, the foreign key should point to
// the most recently deleted one. // the most recently deleted one.
Optional<Host> host = Optional<Host> host =
loadByForeignKey( loadByForeignKeyCached(
Host.class, Host.class,
pathSearchString, pathSearchString,
shouldIncludeDeleted() ? START_OF_TIME : getRequestTime()); shouldIncludeDeleted() ? START_OF_TIME : getRequestTime());

View file

@ -14,7 +14,7 @@
package google.registry.rdap; package google.registry.rdap;
import static google.registry.model.EppResourceUtils.loadByForeignKey; import static google.registry.model.EppResourceUtils.loadByForeignKeyCached;
import static google.registry.persistence.transaction.TransactionManagerFactory.replicaJpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.replicaJpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.request.Action.Method.GET; import static google.registry.request.Action.Method.GET;
@ -160,7 +160,7 @@ public class RdapNameserverSearchAction extends RdapSearchActionBase {
.setIncompletenessWarningType(IncompletenessWarningType.COMPLETE); .setIncompletenessWarningType(IncompletenessWarningType.COMPLETE);
Optional<Host> host = Optional<Host> host =
loadByForeignKey(Host.class, partialStringQuery.getInitialString(), getRequestTime()); loadByForeignKeyCached(Host.class, partialStringQuery.getInitialString(), getRequestTime());
metricInformationBuilder.setNumHostsRetrieved(host.isPresent() ? 1 : 0); metricInformationBuilder.setNumHostsRetrieved(host.isPresent() ? 1 : 0);
@ -176,7 +176,7 @@ public class RdapNameserverSearchAction extends RdapSearchActionBase {
private NameserverSearchResponse searchByNameUsingSuperordinateDomain( private NameserverSearchResponse searchByNameUsingSuperordinateDomain(
RdapSearchPattern partialStringQuery) { RdapSearchPattern partialStringQuery) {
Optional<Domain> domain = Optional<Domain> domain =
loadByForeignKey(Domain.class, partialStringQuery.getSuffix(), getRequestTime()); loadByForeignKeyCached(Domain.class, partialStringQuery.getSuffix(), getRequestTime());
if (!domain.isPresent()) { if (!domain.isPresent()) {
// Don't allow wildcards with suffixes which are not domains we manage. That would risk a // Don't allow wildcards with suffixes which are not domains we manage. That would risk a
// table scan in many easily foreseeable cases. The user might ask for ns*.zombo.com, // table scan in many easily foreseeable cases. The user might ask for ns*.zombo.com,
@ -194,7 +194,7 @@ public class RdapNameserverSearchAction extends RdapSearchActionBase {
// We can't just check that the host name starts with the initial query string, because // We can't just check that the host name starts with the initial query string, because
// then the query ns.exam*.example.com would match against nameserver ns.example.com. // then the query ns.exam*.example.com would match against nameserver ns.example.com.
if (partialStringQuery.matches(fqhn)) { if (partialStringQuery.matches(fqhn)) {
Optional<Host> host = loadByForeignKey(Host.class, fqhn, getRequestTime()); Optional<Host> host = loadByForeignKeyCached(Host.class, fqhn, getRequestTime());
if (shouldBeVisible(host)) { if (shouldBeVisible(host)) {
hostList.add(host.get()); hostList.add(host.get());
if (hostList.size() > rdapResultSetMaxSize) { if (hostList.size() > rdapResultSetMaxSize) {

View file

@ -17,7 +17,7 @@ package google.registry.whois;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.replicaJpaTm;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
@ -53,12 +53,14 @@ final class NameserverWhoisResponse extends WhoisResponseImpl {
ImmutableMap<Host, String> hostRegistrars = ImmutableMap<Host, String> hostRegistrars =
subordinateHosts.isEmpty() subordinateHosts.isEmpty()
? ImmutableMap.of() ? ImmutableMap.of()
: tm().transact( : replicaJpaTm()
.transact(
() -> () ->
Maps.toMap( Maps.toMap(
subordinateHosts.iterator(), subordinateHosts.iterator(),
host -> host ->
tm().loadByKey(host.getSuperordinateDomain()) replicaJpaTm()
.loadByKey(host.getSuperordinateDomain())
.cloneProjectedAtTime(getTimestamp()) .cloneProjectedAtTime(getTimestamp())
.getCurrentSponsorRegistrarId())); .getCurrentSponsorRegistrarId()));