From 7ed02f4612dc77b7e7d0c58f667e0c29f97392be Mon Sep 17 00:00:00 2001 From: mcilwain Date: Fri, 11 Nov 2016 13:26:39 -0800 Subject: [PATCH] Reload resources before saving in ReloadAllEppResourcesAction This prevents a potential blind write scenario in which something else has concurrently modified the EppResource in between load and save, and those changes then get overwritten. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=138911873 --- .../tools/server/ResaveAllEppResourcesAction.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/java/google/registry/tools/server/ResaveAllEppResourcesAction.java b/java/google/registry/tools/server/ResaveAllEppResourcesAction.java index 803d7562b..dac159697 100644 --- a/java/google/registry/tools/server/ResaveAllEppResourcesAction.java +++ b/java/google/registry/tools/server/ResaveAllEppResourcesAction.java @@ -14,14 +14,15 @@ package google.registry.tools.server; -import static google.registry.mapreduce.inputs.EppResourceInputs.createEntityInput; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.util.PipelineUtils.createJobPath; import com.google.appengine.tools.mapreduce.Mapper; import com.google.common.collect.ImmutableList; +import com.googlecode.objectify.Key; import com.googlecode.objectify.VoidWork; import google.registry.mapreduce.MapreduceRunner; +import google.registry.mapreduce.inputs.EppResourceInputs; import google.registry.model.EppResource; import google.registry.request.Action; import google.registry.request.Response; @@ -49,24 +50,24 @@ public class ResaveAllEppResourcesAction implements Runnable { .setModuleName("tools") .runMapOnly( new ResaveAllEppResourcesActionMapper(), - ImmutableList.of(createEntityInput(EppResource.class))))); + ImmutableList.of(EppResourceInputs.createKeyInput(EppResource.class))))); } /** Mapper to re-save all EPP resources. */ - public static class ResaveAllEppResourcesActionMapper extends Mapper { + public static class ResaveAllEppResourcesActionMapper + extends Mapper, Void, Void> { private static final long serialVersionUID = -7721628665138087001L; public ResaveAllEppResourcesActionMapper() {} @Override - public final void map(final EppResource resource) { + public final void map(final Key resourceKey) { ofy().transact(new VoidWork() { @Override public void vrun() { - ofy().save().entity(resource).now(); + ofy().save().entity(ofy().load().key(resourceKey).now()).now(); }}); - getContext().incrementCounter( - String.format("%s entities re-saved", resource.getClass().getSimpleName())); + getContext().incrementCounter(String.format("%s entities re-saved", resourceKey.getKind())); } } }