DeReference the codebase

This change replaces all Ref objects in the code with Key objects. These are
stored in datastore as the same object (raw datastore keys), so this is not
a model change.

Our best practices doc says to use Keys not Refs because:
 * The .get() method obscures what's actually going on
   - Much harder to visually audit the code for datastore loads
   - Hard to distinguish Ref<T> get()'s from Optional get()'s and Supplier get()'s
 * Implicit ofy().load() offers much less control
   - Antipattern for ultimate goal of making Ofy injectable
   - Can't control cache use or batch loading without making ofy() explicit anyway
 * Serialization behavior is surprising and could be quite dangerous/incorrect
   - Can lead to serialization errors. If it actually worked "as intended",
     it would lead to a Ref<> on a serialized object being replaced upon
     deserialization with a stale copy of the old value, which could potentially
     break all kinds of transactional expectations
 * Having both Ref<T> and Key<T> introduces extra boilerplate everywhere
   - E.g. helper methods all need to have Ref and Key overloads, or you need to
     call .key() to get the Key<T> for every Ref<T> you want to pass in
   - Creating a Ref<T> is more cumbersome, since it doesn't have all the create()
     overloads that Key<T> has, only create(Key<T>) and create(Entity) - no way to
     create directly from kind+ID/name, raw Key, websafe key string, etc.

(Note that Refs are treated specially by Objectify's @Load method and Keys are not;
we don't use that feature, but it is the one advantage Refs have over Keys.)

The direct impetus for this change is that I am trying to audit our use of memcache,
and the implicit .get() calls to datastore were making that very hard.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131965491
This commit is contained in:
cgoldfeder 2016-09-01 10:44:23 -07:00 committed by Ben McIlwain
parent 1a60073b24
commit 5098b03af4
119 changed files with 772 additions and 817 deletions

View file

@ -28,7 +28,6 @@ import static google.registry.util.DateTimeUtils.leapSafeAddYears;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.EntitySubclass;
import com.googlecode.objectify.annotation.IgnoreSave;
@ -112,7 +111,7 @@ public class DomainResource extends DomainBase implements ForeignKeyedEppResourc
* should be created, and this field should be updated to point to the new one.
*/
@XmlTransient
Ref<BillingEvent.Recurring> autorenewBillingEvent;
Key<BillingEvent.Recurring> autorenewBillingEvent;
/**
* The recurring poll message associated with this domain's autorenewals.
@ -123,7 +122,7 @@ public class DomainResource extends DomainBase implements ForeignKeyedEppResourc
* should be created, and this field should be updated to point to the new one.
*/
@XmlTransient
Ref<PollMessage.Autorenew> autorenewPollMessage;
Key<PollMessage.Autorenew> autorenewPollMessage;
/** The unexpired grace periods for this domain (some of which may not be active yet). */
@XmlTransient
@ -146,12 +145,12 @@ public class DomainResource extends DomainBase implements ForeignKeyedEppResourc
DateTime applicationTime;
/**
* A reference to the application used to allocate this domain. Will only be populated for domains
* A key to the application used to allocate this domain. Will only be populated for domains
* allocated from an application.
*/
@IgnoreSave(IfNull.class)
@XmlTransient
Ref<DomainApplication> application;
Key<DomainApplication> application;
public ImmutableSet<String> getSubordinateHosts() {
return nullToEmptyImmutableCopy(subordinateHosts);
@ -165,11 +164,11 @@ public class DomainResource extends DomainBase implements ForeignKeyedEppResourc
return deletePollMessage;
}
public Ref<BillingEvent.Recurring> getAutorenewBillingEvent() {
public Key<BillingEvent.Recurring> getAutorenewBillingEvent() {
return autorenewBillingEvent;
}
public Ref<PollMessage.Autorenew> getAutorenewPollMessage() {
public Key<PollMessage.Autorenew> getAutorenewPollMessage() {
return autorenewPollMessage;
}
@ -185,7 +184,7 @@ public class DomainResource extends DomainBase implements ForeignKeyedEppResourc
return applicationTime;
}
public Ref<DomainApplication> getApplication() {
public Key<DomainApplication> getApplication() {
return application;
}
@ -290,13 +289,13 @@ public class DomainResource extends DomainBase implements ForeignKeyedEppResourc
// Set the speculatively-written new autorenew events as the domain's autorenew events.
.setAutorenewBillingEvent(transferData.getServerApproveAutorenewEvent())
.setAutorenewPollMessage(transferData.getServerApproveAutorenewPollMessage())
// Set the grace period using a ref to the prescheduled transfer billing event. Not using
// Set the grace period using a key to the prescheduled transfer billing event. Not using
// GracePeriod.forBillingEvent() here in order to avoid the actual datastore fetch.
.setGracePeriods(ImmutableSet.of(GracePeriod.create(
GracePeriodStatus.TRANSFER,
transferExpirationTime.plus(Registry.get(getTld()).getTransferGracePeriodLength()),
transferData.getGainingClientId(),
Ref.create(transferData.getServerApproveBillingEvent().key()))));
transferData.getServerApproveBillingEvent())));
// Set all remaining transfer properties.
setAutomaticTransferSuccessProperties(builder, transferData);
// Finish projecting to now.
@ -318,7 +317,7 @@ public class DomainResource extends DomainBase implements ForeignKeyedEppResourc
GracePeriodStatus.AUTO_RENEW,
lastAutorenewTime.plus(Registry.get(getTld()).getAutoRenewGracePeriodLength()),
getCurrentSponsorClientId(),
Ref.create(autorenewBillingEvent.key())));
autorenewBillingEvent));
}
// Remove any grace periods that have expired.
@ -398,12 +397,12 @@ public class DomainResource extends DomainBase implements ForeignKeyedEppResourc
return this;
}
public Builder setAutorenewBillingEvent(Ref<BillingEvent.Recurring> autorenewBillingEvent) {
public Builder setAutorenewBillingEvent(Key<BillingEvent.Recurring> autorenewBillingEvent) {
getInstance().autorenewBillingEvent = autorenewBillingEvent;
return this;
}
public Builder setAutorenewPollMessage(Ref<PollMessage.Autorenew> autorenewPollMessage) {
public Builder setAutorenewPollMessage(Key<PollMessage.Autorenew> autorenewPollMessage) {
getInstance().autorenewPollMessage = autorenewPollMessage;
return this;
}
@ -418,7 +417,7 @@ public class DomainResource extends DomainBase implements ForeignKeyedEppResourc
return this;
}
public Builder setApplication(Ref<DomainApplication> application) {
public Builder setApplication(Key<DomainApplication> application) {
getInstance().application = application;
return this;
}