Add SqlEntity and DatastoreEntity interfaces (#562)

* Add SqlEntity and DatastoreEntity interfaces

These will be used when replaying transactions from either the Datastore
commit logs or the SQL Transaction objects.

When Datastore is the primary database, we will read in the
Datastore commit logs, convert each saved entity to however many SQL
entities, then save those SQL entities in SQL.

When SQL is the primary database, we will read in the SQL objects from a
yet-to-be-created SQL table, convert them to however many Datastore
entities, then save those Datastore entities in Datastore.

This PR includes a couple simple examples of how this will work for entities that are
saveable in both SQL and Datastore (the simple case).

* Add 1-1 mapping between entity annotations and interfaces
This commit is contained in:
gbrodman 2020-04-21 17:28:49 -04:00 committed by GitHub
parent 295251ee78
commit ca3ae9b0e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 161 additions and 3 deletions

View file

@ -66,6 +66,7 @@ import google.registry.model.registry.Registry;
import google.registry.model.transfer.TransferData; import google.registry.model.transfer.TransferData;
import google.registry.model.transfer.TransferStatus; import google.registry.model.transfer.TransferStatus;
import google.registry.persistence.VKey; import google.registry.persistence.VKey;
import google.registry.schema.replay.DatastoreAndSqlEntity;
import google.registry.util.CollectionUtils; import google.registry.util.CollectionUtils;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
@ -107,7 +108,7 @@ import org.joda.time.Interval;
}) })
@ExternalMessagingName("domain") @ExternalMessagingName("domain")
public class DomainBase extends EppResource public class DomainBase extends EppResource
implements ForeignKeyedEppResource, ResourceWithTransferData { implements ForeignKeyedEppResource, ResourceWithTransferData, DatastoreAndSqlEntity {
/** The max number of years that a domain can be registered for, as set by ICANN policy. */ /** The max number of years that a domain can be registered for, as set by ICANN policy. */
public static final int MAX_REGISTRATION_YEARS = 10; public static final int MAX_REGISTRATION_YEARS = 10;

View file

@ -73,6 +73,7 @@ import google.registry.model.annotations.ReportedOn;
import google.registry.model.common.EntityGroupRoot; import google.registry.model.common.EntityGroupRoot;
import google.registry.model.registrar.Registrar.BillingAccountEntry.CurrencyMapper; import google.registry.model.registrar.Registrar.BillingAccountEntry.CurrencyMapper;
import google.registry.model.registry.Registry; import google.registry.model.registry.Registry;
import google.registry.schema.replay.DatastoreAndSqlEntity;
import google.registry.util.CidrAddressBlock; import google.registry.util.CidrAddressBlock;
import java.security.cert.CertificateParsingException; import java.security.cert.CertificateParsingException;
import java.util.Comparator; import java.util.Comparator;
@ -107,11 +108,12 @@ import org.joda.time.DateTime;
columnList = "ianaIdentifier", columnList = "ianaIdentifier",
name = "registrar_iana_identifier_idx"), name = "registrar_iana_identifier_idx"),
}) })
public class Registrar extends ImmutableObject implements Buildable, Jsonifiable { public class Registrar extends ImmutableObject
implements Buildable, Jsonifiable, DatastoreAndSqlEntity {
/** Represents the type of a registrar entity. */ /** Represents the type of a registrar entity. */
public enum Type { public enum Type {
/** A real-world, third-party registrar. Should have non-null IANA and billing IDs. */ /** A real-world, third-party registrar. Should have non-null IANA and billing IDs. */
REAL(Objects::nonNull), REAL(Objects::nonNull),
/** /**

View file

@ -0,0 +1,31 @@
// Copyright 2020 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.schema.replay;
import com.google.common.collect.ImmutableList;
/** An entity that has the same Java object representation in SQL and Datastore. */
public interface DatastoreAndSqlEntity extends DatastoreEntity, SqlEntity {
@Override
default ImmutableList<DatastoreEntity> toDatastoreEntities() {
return ImmutableList.of(this);
}
@Override
default ImmutableList<SqlEntity> toSqlEntities() {
return ImmutableList.of(this);
}
}

View file

@ -0,0 +1,30 @@
// Copyright 2020 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.schema.replay;
import com.google.common.collect.ImmutableList;
/**
* An object that can be stored in Datastore and serialized using Objectify's {@link
* com.googlecode.objectify.cmd.Saver#toEntity(Object)} code.
*
* <p>This is used when replaying {@link google.registry.model.ofy.CommitLogManifest}s to import
* transactions and data into the secondary SQL store during the first, Datastore-primary, phase of
* the migration.
*/
public interface DatastoreEntity {
ImmutableList<SqlEntity> toSqlEntities();
}

View file

@ -0,0 +1,29 @@
// Copyright 2020 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.schema.replay;
import com.google.common.collect.ImmutableList;
/**
* An object that can be stored in Cloud SQL using {@link
* javax.persistence.EntityManager#persist(Object)}
*
* <p>This will be used when replaying SQL transactions into Datastore, during the second,
* SQL-primary, phase of the migration from Datastore to SQL.
*/
public interface SqlEntity {
ImmutableList<DatastoreEntity> toDatastoreEntities();
}

View file

@ -0,0 +1,65 @@
// Copyright 2020 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.schema.replay;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableSet;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ClassInfoList;
import io.github.classgraph.ScanResult;
import org.junit.Ignore;
import org.junit.Test;
/**
* Test to verify classes implement {@link SqlEntity} and {@link DatastoreEntity} when they should.
*/
public class EntityTest {
@Test
@Ignore("This won't be done until b/152410794 is done, since it requires many entity changes")
public void testSqlEntityPersistence() {
try (ScanResult scanResult =
new ClassGraph().enableAnnotationInfo().whitelistPackages("google.registry").scan()) {
// All javax.persistence entities must implement SqlEntity and vice versa
ImmutableSet<String> javaxPersistenceClasses =
getClassNames(
scanResult.getClassesWithAnnotation(javax.persistence.Entity.class.getName()));
ImmutableSet<String> sqlEntityClasses =
getClassNames(scanResult.getClassesImplementing(SqlEntity.class.getName()));
assertThat(javaxPersistenceClasses).isEqualTo(sqlEntityClasses);
// All com.googlecode.objectify.annotation.Entity classes must implement DatastoreEntity and
// vice versa
ImmutableSet<String> objectifyClasses =
getClassNames(
scanResult.getClassesWithAnnotation(
com.googlecode.objectify.annotation.Entity.class.getName()));
ImmutableSet<String> datastoreEntityClasses =
getClassNames(scanResult.getClassesImplementing(DatastoreEntity.class.getName()));
assertThat(objectifyClasses).isEqualTo(datastoreEntityClasses);
}
}
private ImmutableSet<String> getClassNames(ClassInfoList classInfoList) {
return classInfoList.stream()
.filter(ClassInfo::isStandardClass)
.map(ClassInfo::loadClass)
.map(Class::getName)
.collect(toImmutableSet());
}
}