Skip to content

Commit ad07b32

Browse files
authored
Refactor EppResourceUtils.loadByForeignKey(...) -> ForeignKeyUtils.loadResource(...) (#2864)
This doesn't make any underlying implementation details, and is mainly useful to reduce the number of diffs in PR #2852 (which does change implementation details) thus making that easier to review.
1 parent 8f69b48 commit ad07b32

36 files changed

+156
-154
lines changed

core/src/main/java/google/registry/dns/PublishDnsUpdatesAction.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static google.registry.dns.DnsUtils.DNS_PUBLISH_PUSH_QUEUE_NAME;
2626
import static google.registry.dns.DnsUtils.requestDomainDnsRefresh;
2727
import static google.registry.dns.DnsUtils.requestHostDnsRefresh;
28-
import static google.registry.model.EppResourceUtils.loadByForeignKey;
2928
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
3029
import static google.registry.request.Action.Method.POST;
3130
import static google.registry.request.RequestParameters.PARAM_TLD;
@@ -46,6 +45,7 @@
4645
import google.registry.dns.DnsMetrics.PublishStatus;
4746
import google.registry.dns.writer.DnsWriter;
4847
import google.registry.groups.GmailClient;
48+
import google.registry.model.ForeignKeyUtils;
4949
import google.registry.model.domain.Domain;
5050
import google.registry.model.host.Host;
5151
import google.registry.model.registrar.Registrar;
@@ -237,7 +237,8 @@ public Void call() {
237237
.findFirst()
238238
.ifPresent(
239239
dn -> {
240-
Optional<Domain> domain = loadByForeignKey(Domain.class, dn, clock.nowUtc());
240+
Optional<Domain> domain =
241+
ForeignKeyUtils.loadResource(Domain.class, dn, clock.nowUtc());
241242
if (domain.isPresent()) {
242243
notifyWithEmailAboutDnsUpdateFailure(
243244
domain.get().getCurrentSponsorRegistrarId(), dn, false);
@@ -250,7 +251,8 @@ public Void call() {
250251
.findFirst()
251252
.ifPresent(
252253
hn -> {
253-
Optional<Host> host = loadByForeignKey(Host.class, hn, clock.nowUtc());
254+
Optional<Host> host =
255+
ForeignKeyUtils.loadResource(Host.class, hn, clock.nowUtc());
254256
if (host.isPresent()) {
255257
notifyWithEmailAboutDnsUpdateFailure(
256258
host.get().getPersistedCurrentSponsorRegistrarId(), hn, true);

core/src/main/java/google/registry/dns/RefreshDnsAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
import static google.registry.dns.DnsUtils.requestDomainDnsRefresh;
1818
import static google.registry.dns.DnsUtils.requestHostDnsRefresh;
19-
import static google.registry.model.EppResourceUtils.loadByForeignKey;
2019
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
2120

2221
import google.registry.dns.DnsUtils.TargetType;
2322
import google.registry.model.EppResource;
2423
import google.registry.model.EppResource.ForeignKeyedEppResource;
24+
import google.registry.model.ForeignKeyUtils;
2525
import google.registry.model.annotations.ExternalMessagingName;
2626
import google.registry.model.domain.Domain;
2727
import google.registry.model.host.Host;
@@ -79,7 +79,7 @@ public void run() {
7979

8080
private <T extends EppResource & ForeignKeyedEppResource>
8181
T loadAndVerifyExistence(Class<T> clazz, String foreignKey) {
82-
return loadByForeignKey(clazz, foreignKey, clock.nowUtc())
82+
return ForeignKeyUtils.loadResource(clazz, foreignKey, clock.nowUtc())
8383
.orElseThrow(
8484
() ->
8585
new NotFoundException(

core/src/main/java/google/registry/dns/writer/clouddns/CloudDnsWriter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.collect.ImmutableSet.toImmutableSet;
1919
import static google.registry.dns.DnsUtils.getDnsAPlusAAAATtlForHost;
20-
import static google.registry.model.EppResourceUtils.loadByForeignKey;
2120
import static google.registry.util.DomainNameUtils.getSecondLevelDomain;
2221

2322
import com.google.api.client.googleapis.json.GoogleJsonError;
@@ -37,6 +36,7 @@
3736
import google.registry.dns.writer.BaseDnsWriter;
3837
import google.registry.dns.writer.DnsWriter;
3938
import google.registry.dns.writer.DnsWriterZone;
39+
import google.registry.model.ForeignKeyUtils;
4040
import google.registry.model.domain.Domain;
4141
import google.registry.model.domain.secdns.DomainDsData;
4242
import google.registry.model.host.Host;
@@ -123,7 +123,8 @@ public void publishDomain(String domainName) {
123123
String absoluteDomainName = getAbsoluteHostName(domainName);
124124

125125
// Load the target domain. Note that it can be absent if this domain was just deleted.
126-
Optional<Domain> domain = loadByForeignKey(Domain.class, domainName, clock.nowUtc());
126+
Optional<Domain> domain =
127+
ForeignKeyUtils.loadResource(Domain.class, domainName, clock.nowUtc());
127128

128129
// Return early if no DNS records should be published.
129130
// desiredRecordsBuilder is populated with an empty set to indicate that all existing records
@@ -189,7 +190,7 @@ private void publishSubordinateHost(String hostName) {
189190
// Load the target host. Note that it can be absent if this host was just deleted.
190191
// desiredRecords is populated with an empty set to indicate that all existing records
191192
// should be deleted.
192-
Optional<Host> host = loadByForeignKey(Host.class, hostName, clock.nowUtc());
193+
Optional<Host> host = ForeignKeyUtils.loadResource(Host.class, hostName, clock.nowUtc());
193194

194195
// Return early if the host is deleted.
195196
if (host.isEmpty()) {

core/src/main/java/google/registry/dns/writer/dnsupdate/DnsUpdateWriter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static com.google.common.collect.Sets.intersection;
2020
import static com.google.common.collect.Sets.union;
2121
import static google.registry.dns.DnsUtils.getDnsAPlusAAAATtlForHost;
22-
import static google.registry.model.EppResourceUtils.loadByForeignKey;
2322

2423
import com.google.common.base.Joiner;
2524
import com.google.common.collect.ImmutableList;
@@ -28,6 +27,7 @@
2827
import google.registry.config.RegistryConfig.Config;
2928
import google.registry.dns.writer.BaseDnsWriter;
3029
import google.registry.dns.writer.DnsWriterZone;
30+
import google.registry.model.ForeignKeyUtils;
3131
import google.registry.model.domain.Domain;
3232
import google.registry.model.domain.secdns.DomainDsData;
3333
import google.registry.model.host.Host;
@@ -129,7 +129,8 @@ public DnsUpdateWriter(
129129
* this domain refresh request
130130
*/
131131
private void publishDomain(String domainName, String requestingHostName) {
132-
Optional<Domain> domainOptional = loadByForeignKey(Domain.class, domainName, clock.nowUtc());
132+
Optional<Domain> domainOptional =
133+
ForeignKeyUtils.loadResource(Domain.class, domainName, clock.nowUtc());
133134
update.delete(toAbsoluteName(domainName), Type.ANY);
134135
// If the domain is now deleted, then don't update DNS for it.
135136
if (domainOptional.isPresent()) {
@@ -218,7 +219,7 @@ private void deleteSubordinateHostAddressSet(
218219
private void addInBailiwickNameServerSet(Domain domain, Update update) {
219220
for (String hostName :
220221
intersection(domain.loadNameserverHostNames(), domain.getSubordinateHosts())) {
221-
Optional<Host> host = loadByForeignKey(Host.class, hostName, clock.nowUtc());
222+
Optional<Host> host = ForeignKeyUtils.loadResource(Host.class, hostName, clock.nowUtc());
222223
checkState(host.isPresent(), "Host %s cannot be loaded", hostName);
223224
update.add(makeAddressSet(host.get()));
224225
update.add(makeV6AddressSet(host.get()));

core/src/main/java/google/registry/flows/ResourceFlowUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import static com.google.common.collect.Sets.intersection;
1818
import static google.registry.model.EppResourceUtils.isLinked;
19-
import static google.registry.model.EppResourceUtils.loadByForeignKey;
2019
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
2120

2221
import com.google.common.collect.ImmutableSet;
@@ -97,7 +96,7 @@ public static <R extends EppResource & ResourceWithTransferData> void verifyTran
9796

9897
public static <R extends EppResource & ForeignKeyedEppResource> R loadAndVerifyExistence(
9998
Class<R> clazz, String targetId, DateTime now) throws ResourceDoesNotExistException {
100-
return verifyExistence(clazz, targetId, loadByForeignKey(clazz, targetId, now));
99+
return verifyExistence(clazz, targetId, ForeignKeyUtils.loadResource(clazz, targetId, now));
101100
}
102101

103102
public static <R extends EppResource> R verifyExistence(

core/src/main/java/google/registry/flows/domain/DomainInfoFlow.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static google.registry.flows.domain.DomainFlowUtils.addSecDnsExtensionIfPresent;
2121
import static google.registry.flows.domain.DomainFlowUtils.handleFeeRequest;
2222
import static google.registry.flows.domain.DomainFlowUtils.loadForeignKeyedDesignatedContacts;
23-
import static google.registry.model.EppResourceUtils.loadByForeignKey;
2423
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
2524

2625
import com.google.common.collect.ImmutableList;
@@ -38,6 +37,7 @@
3837
import google.registry.flows.custom.DomainInfoFlowCustomLogic.AfterValidationParameters;
3938
import google.registry.flows.custom.DomainInfoFlowCustomLogic.BeforeResponseParameters;
4039
import google.registry.flows.custom.DomainInfoFlowCustomLogic.BeforeResponseReturnData;
40+
import google.registry.model.ForeignKeyUtils;
4141
import google.registry.model.domain.Domain;
4242
import google.registry.model.domain.DomainCommand.Info;
4343
import google.registry.model.domain.DomainCommand.Info.HostsRequest;
@@ -109,7 +109,8 @@ public EppResponse run() throws EppException {
109109
extensionManager.validate();
110110
DateTime now = clock.nowUtc();
111111
Domain domain =
112-
verifyExistence(Domain.class, targetId, loadByForeignKey(Domain.class, targetId, now));
112+
verifyExistence(
113+
Domain.class, targetId, ForeignKeyUtils.loadResource(Domain.class, targetId, now));
113114
verifyOptionalAuthInfo(authInfo, domain);
114115
flowCustomLogic.afterValidation(
115116
AfterValidationParameters.newBuilder().setDomain(domain).build());

core/src/main/java/google/registry/flows/host/HostFlowUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package google.registry.flows.host;
1616

1717
import static google.registry.model.EppResourceUtils.isActive;
18-
import static google.registry.model.EppResourceUtils.loadByForeignKey;
1918
import static google.registry.model.tld.Tlds.findTldForName;
2019
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
2120
import static java.util.stream.Collectors.joining;
@@ -29,6 +28,7 @@
2928
import google.registry.flows.EppException.ParameterValueRangeErrorException;
3029
import google.registry.flows.EppException.ParameterValueSyntaxErrorException;
3130
import google.registry.flows.EppException.StatusProhibitsOperationException;
31+
import google.registry.model.ForeignKeyUtils;
3232
import google.registry.model.domain.Domain;
3333
import google.registry.model.eppcommon.StatusValue;
3434
import google.registry.util.Idn;
@@ -90,7 +90,8 @@ public static Optional<Domain> lookupSuperordinateDomain(
9090
hostName.parts().stream()
9191
.skip(hostName.parts().size() - (tld.get().parts().size() + 1))
9292
.collect(joining("."));
93-
Optional<Domain> superordinateDomain = loadByForeignKey(Domain.class, domainName, now);
93+
Optional<Domain> superordinateDomain =
94+
ForeignKeyUtils.loadResource(Domain.class, domainName, now);
9495
if (superordinateDomain.isEmpty() || !isActive(superordinateDomain.get(), now)) {
9596
throw new SuperordinateDomainDoesNotExistException(domainName);
9697
}

core/src/main/java/google/registry/model/EppResourceUtils.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,6 @@ private static <T extends EppResource> T cloneProjectedAtTime(T resource, DateTi
9090
return (T) resource.cloneProjectedAtTime(now);
9191
}
9292

93-
/**
94-
* Loads the last created version of an {@link EppResource} from the database by foreign key.
95-
*
96-
* <p>Returns empty if no resource with this foreign key was ever created, or if the most recently
97-
* created resource was deleted before time "now".
98-
*
99-
* <p>Loading an {@link EppResource} by itself is not sufficient to know its current state since
100-
* it may have various expirable conditions and status values that might implicitly change its
101-
* state as time progresses even if it has not been updated in the database. Rather, the resource
102-
* must be combined with a timestamp to view its current state. We use a global last updated
103-
* timestamp on the resource's entity group (which is essentially free since all writes to the
104-
* entity group must be serialized anyways) to guarantee monotonically increasing write times, and
105-
* forward our projected time to the greater of this timestamp or "now". This guarantees that
106-
* we're not projecting into the past.
107-
*
108-
* @param clazz the resource type to load
109-
* @param foreignKey id to match
110-
* @param now the current logical time to project resources at
111-
*/
112-
public static <T extends EppResource> Optional<T> loadByForeignKey(
113-
Class<T> clazz, String foreignKey, DateTime now) {
114-
return loadByForeignKeyHelper(tm(), clazz, foreignKey, now, false);
115-
}
116-
11793
/**
11894
* Loads the last created version of an {@link EppResource} from the database by foreign key,
11995
* using a cache, if caching is enabled in config settings.
@@ -132,7 +108,6 @@ public static <T extends EppResource> Optional<T> loadByForeignKey(
132108
* <p>Do not call this cached version for anything that needs transactional consistency. It should
133109
* only be used when it's OK if the data is potentially being out of date, e.g. RDAP.
134110
*
135-
* @param clazz the resource type to load
136111
* @param foreignKey id to match
137112
* @param now the current logical time to project resources at
138113
*/
@@ -154,7 +129,7 @@ public static <T extends EppResource> Optional<T> loadByForeignKeyByCache(
154129
return loadByForeignKeyHelper(replicaTm(), clazz, foreignKey, now, true);
155130
}
156131

157-
private static <T extends EppResource> Optional<T> loadByForeignKeyHelper(
132+
static <T extends EppResource> Optional<T> loadByForeignKeyHelper(
158133
TransactionManager txnManager,
159134
Class<T> clazz,
160135
String foreignKey,

core/src/main/java/google/registry/model/ForeignKeyUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.common.collect.ImmutableMap.toImmutableMap;
1919
import static google.registry.config.RegistryConfig.getEppResourceCachingDuration;
2020
import static google.registry.config.RegistryConfig.getEppResourceMaxCachedEntries;
21+
import static google.registry.model.EppResourceUtils.loadByForeignKeyHelper;
2122
import static google.registry.persistence.transaction.TransactionManagerFactory.replicaTm;
2223
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
2324

@@ -91,6 +92,17 @@ public static <E extends EppResource> ImmutableMap<String, VKey<E>> load(
9192
.collect(toImmutableMap(Entry::getKey, e -> VKey.create(clazz, e.getValue().repoId())));
9293
}
9394

95+
/**
96+
* Loads an {@link EppResource} from the database by foreign key.
97+
*
98+
* <p>Returns null if no resource with this foreign key was ever created or if the most recently
99+
* created resource was deleted before time "now".
100+
*/
101+
public static <E extends EppResource> Optional<E> loadResource(
102+
Class<E> clazz, String foreignKey, DateTime now) {
103+
return loadByForeignKeyHelper(tm(), clazz, foreignKey, now, false);
104+
}
105+
94106
/**
95107
* Helper method to load {@link VKey}s to all the most recent {@link EppResource}s for the given
96108
* foreign keys, regardless of whether they have been soft-deleted.

core/src/main/java/google/registry/tools/EnqueuePollMessageCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package google.registry.tools;
1616

1717
import static com.google.common.base.Preconditions.checkArgument;
18-
import static google.registry.model.EppResourceUtils.loadByForeignKey;
1918
import static google.registry.model.reporting.HistoryEntry.Type.SYNTHETIC;
2019
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
2120
import static google.registry.util.CollectionUtils.isNullOrEmpty;
@@ -25,6 +24,7 @@
2524
import com.google.common.collect.ImmutableList;
2625
import com.google.common.collect.Streams;
2726
import google.registry.config.RegistryConfig.Config;
27+
import google.registry.model.ForeignKeyUtils;
2828
import google.registry.model.domain.Domain;
2929
import google.registry.model.domain.DomainHistory;
3030
import google.registry.model.poll.PollMessage;
@@ -86,7 +86,7 @@ protected final void init() {
8686
tm().transact(
8787
() -> {
8888
Optional<Domain> domainOpt =
89-
loadByForeignKey(Domain.class, domainName, tm().getTransactionTime());
89+
ForeignKeyUtils.loadResource(Domain.class, domainName, tm().getTransactionTime());
9090
checkArgument(
9191
domainOpt.isPresent(), "Domain %s doesn't exist or isn't active", domainName);
9292
Domain domain = domainOpt.get();

0 commit comments

Comments
 (0)