Add cloneProjectedInTime() to ResaveAllEppResources mapreduce

We're planning on adding a cronjob to run this mapreduce monthly, so
we may as well also project the resources being re-saved to the present
time so as to handle pending transfers, grace periods, and such. This will
make the BigQuery exports more useful.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=168831056
This commit is contained in:
mcilwain 2017-09-15 07:08:38 -07:00 committed by jianglai
parent 04a61794e0
commit 3a9d7f9b70
2 changed files with 32 additions and 4 deletions

View file

@ -16,6 +16,7 @@ package google.registry.tools.server;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.PipelineUtils.createJobPath; import static google.registry.util.PipelineUtils.createJobPath;
import static org.joda.time.DateTimeZone.UTC;
import com.google.appengine.tools.mapreduce.Mapper; import com.google.appengine.tools.mapreduce.Mapper;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -28,13 +29,15 @@ import google.registry.request.Action;
import google.registry.request.Response; import google.registry.request.Response;
import google.registry.request.auth.Auth; import google.registry.request.auth.Auth;
import javax.inject.Inject; import javax.inject.Inject;
import org.joda.time.DateTime;
/** /**
* A mapreduce that re-saves all EppResources without otherwise modifying them. * A mapreduce that re-saves all EppResources, projecting them forward to the current time.
* *
* <p>This is useful for completing data migrations on EppResource fields that are accomplished * <p>This is useful for completing data migrations on EppResource fields that are accomplished
* with @OnSave or @OnLoad annotations, and also guarantees that all EppResources will get fresh * with @OnSave or @OnLoad annotations, and also guarantees that all EppResources will get fresh
* commit logs (for backup purposes). * commit logs (for backup purposes). Additionally, pending actions such as transfers or grace
* periods that are past their effective time will be resolved.
* *
* <p>Because there are no auth settings in the {@link Action} annotation, this command can only be * <p>Because there are no auth settings in the {@link Action} annotation, this command can only be
* run internally, or by pretending to be internal by setting the X-AppEngine-QueueName header, * run internally, or by pretending to be internal by setting the X-AppEngine-QueueName header,
@ -50,7 +53,6 @@ public class ResaveAllEppResourcesAction implements Runnable {
@Inject Response response; @Inject Response response;
@Inject ResaveAllEppResourcesAction() {} @Inject ResaveAllEppResourcesAction() {}
@SuppressWarnings("unchecked")
@Override @Override
public void run() { public void run() {
response.sendJavaScriptRedirect(createJobPath(mrRunner response.sendJavaScriptRedirect(createJobPath(mrRunner
@ -73,7 +75,9 @@ public class ResaveAllEppResourcesAction implements Runnable {
ofy().transact(new VoidWork() { ofy().transact(new VoidWork() {
@Override @Override
public void vrun() { public void vrun() {
ofy().save().entity(ofy().load().key(resourceKey).now()).now(); EppResource projectedResource =
ofy().load().key(resourceKey).now().cloneProjectedAtTime(DateTime.now(UTC));
ofy().save().entity(projectedResource).now();
}}); }});
getContext().incrementCounter(String.format("%s entities re-saved", resourceKey.getKind())); getContext().incrementCounter(String.format("%s entities re-saved", resourceKey.getKind()));
} }

View file

@ -17,8 +17,11 @@ package google.registry.tools.server;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.persistActiveContact; import static google.registry.testing.DatastoreHelper.persistActiveContact;
import static google.registry.testing.DatastoreHelper.persistContactWithPendingTransfer;
import static org.joda.time.DateTimeZone.UTC;
import google.registry.model.contact.ContactResource; import google.registry.model.contact.ContactResource;
import google.registry.model.transfer.TransferStatus;
import google.registry.testing.FakeResponse; import google.registry.testing.FakeResponse;
import google.registry.testing.mapreduce.MapreduceTestCase; import google.registry.testing.mapreduce.MapreduceTestCase;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -55,4 +58,25 @@ public class ResaveAllEppResourcesActionTest
assertThat(ofy().load().entity(contact).now().getUpdateAutoTimestamp().getTimestamp()) assertThat(ofy().load().entity(contact).now().getUpdateAutoTimestamp().getTimestamp())
.isGreaterThan(creationTime); .isGreaterThan(creationTime);
} }
@Test
public void test_mapreduceResolvesPendingTransfer() throws Exception {
DateTime now = DateTime.now(UTC);
// Set up a contact with a transfer that implicitly completed five days ago.
ContactResource contact =
persistContactWithPendingTransfer(
persistActiveContact("meh789"),
now.minusDays(10),
now.minusDays(10),
now.minusDays(10));
assertThat(contact.getTransferData().getTransferStatus()).isEqualTo(TransferStatus.PENDING);
runMapreduce();
ofy().clearSessionCache();
// The transfer should be effective after the contact is re-saved, as it should've been
// projected to the current time.
ContactResource resavedContact = ofy().load().entity(contact).now();
assertThat(resavedContact.getTransferData().getTransferStatus())
.isEqualTo(TransferStatus.SERVER_APPROVED);
}
} }