Convert remaining ofy() calls to auditedOfy() (#1200)

* Convert remaining ofy() calls to auditedOfy()
This commit is contained in:
gbrodman 2021-06-08 13:52:13 -04:00 committed by GitHub
parent 3f41f7f444
commit a1b56b0521
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 19 deletions

View file

@ -51,7 +51,7 @@ import google.registry.model.translators.VKeyTranslatorFactory;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
/** /**
* An instance of Ofy, obtained via {@code #ofy()}, should be used to access all persistable * An instance of Ofy, obtained via {@code #auditedOfy()}, should be used to access all persistable
* objects. The class contains a static initializer to call factory().register(...) on all * objects. The class contains a static initializer to call factory().register(...) on all
* persistable objects in this package. * persistable objects in this package.
*/ */

View file

@ -14,7 +14,7 @@
package google.registry.tools.javascrap; package google.registry.tools.javascrap;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.auditedOfy;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import com.google.appengine.tools.mapreduce.Mapper; import com.google.appengine.tools.mapreduce.Mapper;
@ -111,7 +111,7 @@ public class CreateSyntheticHistoryEntriesAction implements Runnable {
public final void map(final Key<EppResource> resourceKey) { public final void map(final Key<EppResource> resourceKey) {
tm().transact( tm().transact(
() -> { () -> {
EppResource eppResource = ofy().load().key(resourceKey).now(); EppResource eppResource = auditedOfy().load().key(resourceKey).now();
tm().put( tm().put(
HistoryEntry.createBuilderForResource(eppResource) HistoryEntry.createBuilderForResource(eppResource)
.setClientId(registryAdminRegistrarId) .setClientId(registryAdminRegistrarId)

View file

@ -22,7 +22,7 @@ import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Lists.partition; import static com.google.common.collect.Lists.partition;
import static google.registry.backup.BackupUtils.serializeEntity; import static google.registry.backup.BackupUtils.serializeEntity;
import static google.registry.model.ofy.CommitLogBucket.getBucketKey; import static google.registry.model.ofy.CommitLogBucket.getBucketKey;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.auditedOfy;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.util.DateTimeUtils.START_OF_TIME; import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static google.registry.util.DateTimeUtils.isAtOrAfter; import static google.registry.util.DateTimeUtils.isAtOrAfter;
@ -80,7 +80,7 @@ public final class CommitLogExports {
public static CommitLogCheckpoint computeCheckpoint(Clock clock) { public static CommitLogCheckpoint computeCheckpoint(Clock clock) {
CommitLogCheckpointStrategy strategy = new CommitLogCheckpointStrategy(); CommitLogCheckpointStrategy strategy = new CommitLogCheckpointStrategy();
strategy.clock = clock; strategy.clock = clock;
strategy.ofy = ofy(); strategy.ofy = auditedOfy();
CommitLogCheckpoint checkpoint = strategy.computeCheckpoint(); CommitLogCheckpoint checkpoint = strategy.computeCheckpoint();
tm().transact( tm().transact(
@ -90,7 +90,7 @@ public final class CommitLogExports {
checkpoint.getCheckpointTime().isAfter(lastWrittenTime), checkpoint.getCheckpointTime().isAfter(lastWrittenTime),
"Newer checkpoint already written at time: %s", "Newer checkpoint already written at time: %s",
lastWrittenTime); lastWrittenTime);
ofy() auditedOfy()
.saveWithoutBackup() .saveWithoutBackup()
.entities( .entities(
checkpoint, CommitLogCheckpointRoot.create(checkpoint.getCheckpointTime())); checkpoint, CommitLogCheckpointRoot.create(checkpoint.getCheckpointTime()));
@ -135,17 +135,17 @@ public final class CommitLogExports {
// asynchronously load the entities for the next one. // asynchronously load the entities for the next one.
List<List<Key<CommitLogManifest>>> keyChunks = partition(sortedKeys, EXPORT_DIFF_BATCH_SIZE); List<List<Key<CommitLogManifest>>> keyChunks = partition(sortedKeys, EXPORT_DIFF_BATCH_SIZE);
// Objectify's map return type is asynchronous. Calling .values() will block until it loads. // Objectify's map return type is asynchronous. Calling .values() will block until it loads.
Map<?, CommitLogManifest> nextChunkToExport = ofy().load().keys(keyChunks.get(0)); Map<?, CommitLogManifest> nextChunkToExport = auditedOfy().load().keys(keyChunks.get(0));
for (int i = 0; i < keyChunks.size(); i++) { for (int i = 0; i < keyChunks.size(); i++) {
// Force the async load to finish. // Force the async load to finish.
Collection<CommitLogManifest> chunkValues = nextChunkToExport.values(); Collection<CommitLogManifest> chunkValues = nextChunkToExport.values();
// Since there is no hard bound on how much data this might be, take care not to let the // Since there is no hard bound on how much data this might be, take care not to let the
// Objectify session cache fill up and potentially run out of memory. This is the only safe // Objectify session cache fill up and potentially run out of memory. This is the only safe
// point to do this since at this point there is no async load in progress. // point to do this since at this point there is no async load in progress.
ofy().clearSessionCache(); auditedOfy().clearSessionCache();
// Kick off the next async load, which can happen in parallel to the current GCS export. // Kick off the next async load, which can happen in parallel to the current GCS export.
if (i + 1 < keyChunks.size()) { if (i + 1 < keyChunks.size()) {
nextChunkToExport = ofy().load().keys(keyChunks.get(i + 1)); nextChunkToExport = auditedOfy().load().keys(keyChunks.get(i + 1));
} }
exportChunk(commitLogStream, chunkValues); exportChunk(commitLogStream, chunkValues);
} }
@ -205,7 +205,7 @@ public final class CommitLogExports {
return ImmutableSet.of(); return ImmutableSet.of();
} }
Key<CommitLogBucket> bucketKey = getBucketKey(bucketNum); Key<CommitLogBucket> bucketKey = getBucketKey(bucketNum);
return ofy() return auditedOfy()
.load() .load()
.type(CommitLogManifest.class) .type(CommitLogManifest.class)
.ancestor(bucketKey) .ancestor(bucketKey)
@ -222,7 +222,7 @@ public final class CommitLogExports {
new ImmutableList.Builder<>(); new ImmutableList.Builder<>();
for (CommitLogManifest manifest : chunk) { for (CommitLogManifest manifest : chunk) {
entities.add(ImmutableList.of(manifest)); entities.add(ImmutableList.of(manifest));
entities.add(ofy().load().type(CommitLogMutation.class).ancestor(manifest)); entities.add(auditedOfy().load().type(CommitLogMutation.class).ancestor(manifest));
} }
for (ImmutableObject entity : concat(entities.build())) { for (ImmutableObject entity : concat(entities.build())) {
serializeEntity(entity, gcsStream); serializeEntity(entity, gcsStream);

View file

@ -16,7 +16,7 @@ package google.registry.model.index;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.config.RegistryConfig.getEppResourceIndexBucketCount; import static google.registry.config.RegistryConfig.getEppResourceIndexBucketCount;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.auditedOfy;
import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.persistActiveContact; import static google.registry.testing.DatabaseHelper.persistActiveContact;
import static google.registry.testing.DatabaseHelper.persistResource; import static google.registry.testing.DatabaseHelper.persistResource;
@ -44,7 +44,7 @@ class EppResourceIndexTest extends EntityTestCase {
@Test @Test
void testPersistence() { void testPersistence() {
EppResourceIndex loadedIndex = Iterables.getOnlyElement(getEppResourceIndexObjects()); EppResourceIndex loadedIndex = Iterables.getOnlyElement(getEppResourceIndexObjects());
assertThat(ofy().load().key(loadedIndex.reference).now()).isEqualTo(contact); assertThat(auditedOfy().load().key(loadedIndex.reference).now()).isEqualTo(contact);
} }
@Test @Test
@ -56,7 +56,7 @@ class EppResourceIndexTest extends EntityTestCase {
void testIdempotentOnUpdate() { void testIdempotentOnUpdate() {
contact = persistResource(contact.asBuilder().setEmailAddress("abc@def.fake").build()); contact = persistResource(contact.asBuilder().setEmailAddress("abc@def.fake").build());
EppResourceIndex loadedIndex = Iterables.getOnlyElement(getEppResourceIndexObjects()); EppResourceIndex loadedIndex = Iterables.getOnlyElement(getEppResourceIndexObjects());
assertThat(ofy().load().key(loadedIndex.reference).now()).isEqualTo(contact); assertThat(auditedOfy().load().key(loadedIndex.reference).now()).isEqualTo(contact);
} }
/** /**
@ -65,9 +65,11 @@ class EppResourceIndexTest extends EntityTestCase {
private static ImmutableList<EppResourceIndex> getEppResourceIndexObjects() { private static ImmutableList<EppResourceIndex> getEppResourceIndexObjects() {
ImmutableList.Builder<EppResourceIndex> indexEntities = new ImmutableList.Builder<>(); ImmutableList.Builder<EppResourceIndex> indexEntities = new ImmutableList.Builder<>();
for (int i = 0; i < getEppResourceIndexBucketCount(); i++) { for (int i = 0; i < getEppResourceIndexBucketCount(); i++) {
indexEntities.addAll(ofy().load() indexEntities.addAll(
.type(EppResourceIndex.class) auditedOfy()
.ancestor(Key.create(EppResourceIndexBucket.class, i + 1))); .load()
.type(EppResourceIndex.class)
.ancestor(Key.create(EppResourceIndexBucket.class, i + 1)));
} }
return indexEntities.build(); return indexEntities.build();
} }

View file

@ -21,7 +21,6 @@ import static com.google.common.collect.Iterables.filter;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.model.ofy.ObjectifyService.auditedOfy; import static google.registry.model.ofy.ObjectifyService.auditedOfy;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.newContactResource; import static google.registry.testing.DatabaseHelper.newContactResource;
import static google.registry.testing.DatabaseHelper.persistResource; import static google.registry.testing.DatabaseHelper.persistResource;
@ -78,7 +77,9 @@ class KillAllCommitLogsActionTest extends MapreduceTestCase<KillAllCommitLogsAct
START_OF_TIME.plusDays(1), START_OF_TIME.plusDays(1),
ImmutableMap.of(1, bucketTime, 2, bucketTime, 3, bucketTime))); ImmutableMap.of(1, bucketTime, 2, bucketTime, 3, bucketTime)));
for (Class<?> clazz : AFFECTED_TYPES) { for (Class<?> clazz : AFFECTED_TYPES) {
assertWithMessage("entities of type " + clazz).that(ofy().load().type(clazz)).isNotEmpty(); assertWithMessage("entities of type " + clazz)
.that(auditedOfy().load().type(clazz))
.isNotEmpty();
} }
ImmutableList<?> otherStuff = ImmutableList<?> otherStuff =
Streams.stream(auditedOfy().load()) Streams.stream(auditedOfy().load())