Use composite primary key for DomainHistory (#767)

* Use composite primary key for DomainHistory

* Move History table's SequenceGenerator to orm.xml

* Rebase on HEAD and remove default value for key in History tables

* Use primitive type for id.

* Revert the cache change
This commit is contained in:
Shicong Huang 2020-09-03 10:21:23 -04:00 committed by GitHub
parent b177532d58
commit b34da92f42
10 changed files with 219 additions and 78 deletions

View file

@ -19,8 +19,13 @@ import com.googlecode.objectify.annotation.EntitySubclass;
import google.registry.model.EppResource;
import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* A persisted history entry representing an EPP modification to a contact.
@ -38,6 +43,7 @@ import javax.persistence.Entity;
@javax.persistence.Index(columnList = "historyModificationTime")
})
@EntitySubclass
@Access(AccessType.FIELD)
public class ContactHistory extends HistoryEntry {
// Store ContactBase instead of ContactResource so we don't pick up its @Id
ContactBase contactBase;
@ -45,6 +51,15 @@ public class ContactHistory extends HistoryEntry {
@Column(nullable = false)
VKey<ContactResource> contactRepoId;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HistorySequenceGenerator")
@Column(name = "historyRevisionId")
@Access(AccessType.PROPERTY)
@Override
public long getId() {
return super.getId();
}
/** The state of the {@link ContactBase} object at this point in time. */
public ContactBase getContactBase() {
return contactBase;

View file

@ -14,20 +14,32 @@
package google.registry.model.domain;
import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.EntitySubclass;
import com.googlecode.objectify.annotation.Ignore;
import google.registry.model.EppResource;
import google.registry.model.contact.ContactResource;
import google.registry.model.ImmutableObject;
import google.registry.model.domain.DomainHistory.DomainHistoryId;
import google.registry.model.host.HostResource;
import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Index;
import javax.persistence.JoinTable;
import javax.persistence.PostLoad;
import javax.persistence.Table;
/**
* A persisted history entry representing an EPP modification to a domain.
@ -37,27 +49,43 @@ import javax.persistence.JoinTable;
* the foreign-keyed fields in that class can refer to this object.
*/
@Entity
@javax.persistence.Table(
@Table(
indexes = {
@javax.persistence.Index(columnList = "creationTime"),
@javax.persistence.Index(columnList = "historyRegistrarId"),
@javax.persistence.Index(columnList = "historyType"),
@javax.persistence.Index(columnList = "historyModificationTime")
@Index(columnList = "creationTime"),
@Index(columnList = "historyRegistrarId"),
@Index(columnList = "historyType"),
@Index(columnList = "historyModificationTime")
})
@EntitySubclass
@Access(AccessType.FIELD)
@IdClass(DomainHistoryId.class)
public class DomainHistory extends HistoryEntry {
// Store DomainContent instead of DomainBase so we don't pick up its @Id
DomainContent domainContent;
@Column(nullable = false)
VKey<DomainBase> domainRepoId;
@Id String domainRepoId;
// We could have reused domainContent.nsHosts here, but Hibernate throws a weird exception after
// we change to use a composite primary key.
// TODO(b/166776754): Investigate if we can reuse domainContent.nsHosts for storing host keys.
@Ignore
@ElementCollection
@JoinTable(name = "DomainHistoryHost")
@Access(AccessType.PROPERTY)
@Column(name = "host_repo_id")
Set<VKey<HostResource>> nsHosts;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HistorySequenceGenerator")
@Column(name = "historyRevisionId")
@Access(AccessType.PROPERTY)
@Override
public long getId() {
return super.getId();
}
/** Returns keys to the {@link HostResource} that are the nameservers for the domain. */
public Set<VKey<HostResource>> getNsHosts() {
return domainContent.nsHosts;
return nsHosts;
}
/** The state of the {@link DomainContent} object at this point in time. */
@ -65,16 +93,51 @@ public class DomainHistory extends HistoryEntry {
return domainContent;
}
/** The key to the {@link ContactResource} this is based off of. */
/** The key to the {@link DomainBase} this is based off of. */
public VKey<DomainBase> getDomainRepoId() {
return domainRepoId;
return VKey.create(DomainBase.class, domainRepoId, Key.create(DomainBase.class, domainRepoId));
}
// Hibernate needs this in order to populate nsHosts but no one else should ever use it
@SuppressWarnings("UnusedMethod")
private void setNsHosts(Set<VKey<HostResource>> nsHosts) {
public VKey<DomainHistory> createVKey() {
return VKey.createSql(DomainHistory.class, new DomainHistoryId(domainRepoId, getId()));
}
@PostLoad
void postLoad() {
if (domainContent != null) {
domainContent.nsHosts = nsHosts;
domainContent.nsHosts = nullToEmptyImmutableCopy(nsHosts);
}
}
/** Class to represent the composite primary key of {@link DomainHistory} entity. */
static class DomainHistoryId extends ImmutableObject implements Serializable {
private String domainRepoId;
private Long id;
/** Hibernate requires this default constructor. */
private DomainHistoryId() {}
DomainHistoryId(String domainRepoId, long id) {
this.domainRepoId = domainRepoId;
this.id = id;
}
String getDomainRepoId() {
return domainRepoId;
}
void setDomainRepoId(String domainRepoId) {
this.domainRepoId = domainRepoId;
}
long getId() {
return id;
}
void setId(long id) {
this.id = id;
}
}
@ -93,12 +156,15 @@ public class DomainHistory extends HistoryEntry {
public Builder setDomainContent(DomainContent domainContent) {
getInstance().domainContent = domainContent;
if (domainContent != null) {
getInstance().nsHosts = nullToEmptyImmutableCopy(domainContent.nsHosts);
}
return this;
}
public Builder setDomainRepoId(VKey<DomainBase> domainRepoId) {
public Builder setDomainRepoId(String domainRepoId) {
getInstance().domainRepoId = domainRepoId;
domainRepoId.maybeGetOfyKey().ifPresent(parent -> getInstance().parent = parent);
getInstance().parent = Key.create(DomainBase.class, domainRepoId);
return this;
}
@ -106,8 +172,7 @@ public class DomainHistory extends HistoryEntry {
@Override
public Builder setParent(Key<? extends EppResource> parent) {
super.setParent(parent);
getInstance().domainRepoId =
VKey.create(DomainBase.class, parent.getName(), (Key<DomainBase>) parent);
getInstance().domainRepoId = parent.getName();
return this;
}
}

View file

@ -19,8 +19,13 @@ import com.googlecode.objectify.annotation.EntitySubclass;
import google.registry.model.EppResource;
import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* A persisted history entry representing an EPP modification to a host.
@ -39,6 +44,7 @@ import javax.persistence.Entity;
@javax.persistence.Index(columnList = "historyModificationTime")
})
@EntitySubclass
@Access(AccessType.FIELD)
public class HostHistory extends HistoryEntry {
// Store HostBase instead of HostResource so we don't pick up its @Id
@ -47,6 +53,15 @@ public class HostHistory extends HistoryEntry {
@Column(nullable = false)
VKey<HostResource> hostRepoId;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HistorySequenceGenerator")
@Column(name = "historyRevisionId")
@Access(AccessType.PROPERTY)
@Override
public long getId() {
return super.getId();
}
/** The state of the {@link HostBase} object at this point in time. */
public HostBase getHostBase() {
return hostBase;

View file

@ -42,15 +42,14 @@ import google.registry.persistence.VKey;
import google.registry.persistence.WithStringVKey;
import java.util.Set;
import javax.annotation.Nullable;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.MappedSuperclass;
import javax.persistence.SequenceGenerator;
import javax.persistence.Transient;
import org.joda.time.DateTime;
@ -59,6 +58,7 @@ import org.joda.time.DateTime;
@Entity
@MappedSuperclass
@WithStringVKey // TODO(b/162229294): This should be resolved during the course of that bug
@Access(AccessType.FIELD)
public class HistoryEntry extends ImmutableObject implements Buildable {
/** Represents the type of history entry. */
@ -110,17 +110,13 @@ public class HistoryEntry extends ImmutableObject implements Buildable {
SYNTHETIC
}
/** The autogenerated id of this event. */
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HistorySequenceGenerator")
@SequenceGenerator(
name = "HistorySequenceGenerator",
sequenceName = "history_id_sequence",
allocationSize = 1)
@Id
@javax.persistence.Id
@Column(name = "historyRevisionId")
@VisibleForTesting
public Long id;
/**
* The autogenerated id of this event. Note that, this field is marked as {@link Transient} in the
* SQL schema, this is because the child class of {@link HistoryEntry}, e.g. {@link
* DomainHistory}, uses a composite primary key which the id is part of, and Hibernate requires
* that all the {@link javax.persistence.Id} fields must be put in the exact same class.
*/
@Id @Transient @VisibleForTesting public Long id;
/** The resource this event mutated. */
@Parent @Transient protected Key<? extends EppResource> parent;
@ -196,8 +192,17 @@ public class HistoryEntry extends ImmutableObject implements Buildable {
@Transient // domain-specific
Set<DomainTransactionRecord> domainTransactionRecords;
public Long getId() {
return id;
public long getId() {
// For some reason, Hibernate throws NPE during some initialization phase if we don't deal with
// the null case. Setting the id to 0L when it is null should be fine because 0L for primitive
// type is considered as null for wrapper class in the Hibernate context.
return id == null ? 0L : id;
}
// This method is required by Hibernate.
@SuppressWarnings("UnusedMethod")
private void setId(long id) {
this.id = id;
}
public Key<? extends EppResource> getParent() {
@ -279,11 +284,7 @@ public class HistoryEntry extends ImmutableObject implements Buildable {
// can't use a switch statement since we're calling getKind()
if (parentKind.equals(getKind(DomainBase.class))) {
resultEntity =
new DomainHistory.Builder()
.copyFrom(this)
.setDomainRepoId(
VKey.create(DomainBase.class, parent.getName(), (Key<DomainBase>) parent))
.build();
new DomainHistory.Builder().copyFrom(this).setDomainRepoId(parent.getName()).build();
} else if (parentKind.equals(getKind(HostResource.class))) {
resultEntity =
new HostHistory.Builder()
@ -349,6 +350,7 @@ public class HistoryEntry extends ImmutableObject implements Buildable {
return thisCastToDerived();
}
// Until we move completely to SQL, override this in subclasses (e.g. HostHistory) to set VKeys
public B setParent(Key<? extends EppResource> parent) {
getInstance().parent = parent;
return thisCastToDerived();

View file

@ -10,6 +10,9 @@
<basic name="amount" access="FIELD"/>
</attributes>
</embeddable>
<sequence-generator name="HistorySequenceGenerator" sequence-name="history_id_sequence" />
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>

View file

@ -14,6 +14,7 @@
package google.registry.model.history;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
@ -71,11 +72,15 @@ public class DomainHistoryTest extends EntityTestCase {
jpaTm()
.transact(
() -> {
DomainHistory fromDatabase =
jpaTm().load(VKey.createSql(DomainHistory.class, domainHistory.getId()));
DomainHistory fromDatabase = jpaTm().load(domainHistory.createVKey());
assertDomainHistoriesEqual(fromDatabase, domainHistory);
assertThat(fromDatabase.getDomainRepoId().getSqlKey())
.isEqualTo(domainHistory.getDomainRepoId().getSqlKey());
assertThat(fromDatabase.getNsHosts())
.containsExactlyElementsIn(
domainHistory.getNsHosts().stream()
.map(key -> VKey.createSql(HostResource.class, key.getSqlKey()))
.collect(toImmutableSet()));
});
}
@ -120,7 +125,7 @@ public class DomainHistoryTest extends EntityTestCase {
static void assertDomainHistoriesEqual(DomainHistory one, DomainHistory two) {
assertAboutImmutableObjects()
.that(one)
.isEqualExceptFields(two, "domainContent", "domainRepoId", "parent");
.isEqualExceptFields(two, "domainContent", "domainRepoId", "parent", "nsHosts");
}
private DomainHistory createDomainHistory(DomainContent domain) {
@ -134,7 +139,7 @@ public class DomainHistoryTest extends EntityTestCase {
.setReason("reason")
.setRequestedByRegistrar(true)
.setDomainContent(domain)
.setDomainRepoId(domain.createVKey())
.setDomainRepoId(domain.getRepoId())
.build();
}
}

View file

@ -270,9 +270,9 @@ class google.registry.model.domain.DomainHistory {
google.registry.model.domain.Period period;
google.registry.model.eppcommon.Trid trid;
google.registry.model.reporting.HistoryEntry$Type type;
google.registry.persistence.VKey<google.registry.model.domain.DomainBase> domainRepoId;
java.lang.Boolean requestedByRegistrar;
java.lang.String clientId;
java.lang.String domainRepoId;
java.lang.String otherClientId;
java.lang.String reason;
java.util.Set<google.registry.model.reporting.DomainTransactionRecord> domainTransactionRecords;