Augment KillAllCommitLogsAction to also kill checkpoints

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=120338924
This commit is contained in:
cgoldfeder 2016-04-20 08:23:14 -07:00 committed by Justine Tunney
parent 145de1d4db
commit c99711c7e3
2 changed files with 34 additions and 9 deletions

View file

@ -15,7 +15,6 @@
package com.google.domain.registry.tools.server;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Lists.partition;
import static com.google.domain.registry.model.ofy.ObjectifyService.ofy;
import static com.google.domain.registry.request.Action.Method.POST;
import static com.google.domain.registry.util.PipelineUtils.createJobPath;
@ -23,16 +22,21 @@ import static com.google.domain.registry.util.PipelineUtils.createJobPath;
import com.google.appengine.tools.mapreduce.Input;
import com.google.appengine.tools.mapreduce.Mapper;
import com.google.appengine.tools.mapreduce.inputs.InMemoryInput;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.domain.registry.config.RegistryEnvironment;
import com.google.domain.registry.mapreduce.MapreduceAction;
import com.google.domain.registry.mapreduce.MapreduceRunner;
import com.google.domain.registry.model.ofy.CommitLogBucket;
import com.google.domain.registry.model.ofy.CommitLogCheckpointRoot;
import com.google.domain.registry.request.Action;
import com.google.domain.registry.request.Response;
import com.googlecode.objectify.Key;
import java.util.Arrays;
import javax.inject.Inject;
/** Deletes all commit logs in datastore. */
@ -49,9 +53,15 @@ public class KillAllCommitLogsAction implements MapreduceAction {
RegistryEnvironment.get() == RegistryEnvironment.CRASH
|| RegistryEnvironment.get() == RegistryEnvironment.UNITTEST,
"DO NOT RUN ANYWHERE ELSE EXCEPT CRASH OR TESTS.");
// Create a in-memory input, assigning each bucket to its own shard for maximum parallelization.
Input<Key<CommitLogBucket>> input =
new InMemoryInput<>(partition(CommitLogBucket.getAllBucketKeys().asList(), 1));
// Create a in-memory input, assigning each bucket to its own shard for maximum parallelization,
// with one extra shard for the CommitLogCheckpointRoot.
Input<Key<?>> input = new InMemoryInput<>(
Lists.partition(
FluentIterable
.from(Arrays.<Key<?>>asList(CommitLogCheckpointRoot.getKey()))
.append(CommitLogBucket.getAllBucketKeys())
.toList(),
1));
response.sendJavaScriptRedirect(createJobPath(mrRunner
.setJobName("Delete all commit logs")
.setModuleName("tools")
@ -62,23 +72,26 @@ public class KillAllCommitLogsAction implements MapreduceAction {
}
/**
* Mapper to delete a {@link CommitLogBucket} and any commit logs in the bucket.
* Mapper to delete a {@link CommitLogBucket} or {@link CommitLogCheckpointRoot} and any commit
* logs or checkpoints that descend from it.
*
* <p>This will delete:
* <ul>
* <li>{@link CommitLogBucket}
* <li>{@code CommitLogCheckpoint}
* <li>{@link CommitLogCheckpointRoot}
* <li>{@code CommitLogManifest}
* <li>{@code CommitLogMutation}
* </ul>
*/
static class KillAllCommitLogsMapper extends Mapper<Key<CommitLogBucket>, Key<?>, Key<?>> {
static class KillAllCommitLogsMapper extends Mapper<Key<?>, Key<?>, Key<?>> {
private static final long serialVersionUID = 1504266335352952033L;
@Override
public void map(Key<CommitLogBucket> bucket) {
for (Key<Object> key : ofy().load().ancestor(bucket).keys()) {
emit(bucket, key);
public void map(Key<?> bucketOrRoot) {
for (Key<Object> key : ofy().load().ancestor(bucketOrRoot).keys()) {
emit(bucketOrRoot, key);
getContext().incrementCounter("entities emitted");
getContext().incrementCounter(String.format("%s emitted", key.getKind()));
}

View file

@ -21,7 +21,9 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.domain.registry.model.ofy.ObjectifyService.ofy;
import static com.google.domain.registry.testing.DatastoreHelper.createTld;
import static com.google.domain.registry.testing.DatastoreHelper.newContactResource;
import static com.google.domain.registry.testing.DatastoreHelper.persistResource;
import static com.google.domain.registry.testing.DatastoreHelper.persistResourceWithCommitLog;
import static com.google.domain.registry.util.DateTimeUtils.START_OF_TIME;
import static java.util.Arrays.asList;
import com.google.appengine.api.datastore.Entity;
@ -29,9 +31,12 @@ import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.domain.registry.mapreduce.MapreduceRunner;
import com.google.domain.registry.model.ImmutableObject;
import com.google.domain.registry.model.ofy.CommitLogBucket;
import com.google.domain.registry.model.ofy.CommitLogCheckpoint;
import com.google.domain.registry.model.ofy.CommitLogCheckpointRoot;
import com.google.domain.registry.model.ofy.CommitLogManifest;
import com.google.domain.registry.model.ofy.CommitLogMutation;
import com.google.domain.registry.testing.FakeResponse;
@ -49,6 +54,8 @@ public class KillAllCommitLogsActionTest extends MapreduceTestCase<KillAllCommit
static final List<Class<? extends ImmutableObject>> AFFECTED_TYPES = ImmutableList.of(
CommitLogBucket.class,
CommitLogCheckpoint.class,
CommitLogCheckpointRoot.class,
CommitLogMutation.class,
CommitLogManifest.class);
@ -68,6 +75,11 @@ public class KillAllCommitLogsActionTest extends MapreduceTestCase<KillAllCommit
persistResourceWithCommitLog(
newContactResource(String.format("abc%d", nextContactId++)));
}
persistResource(CommitLogCheckpointRoot.create(START_OF_TIME.plusDays(1)));
persistResource(
CommitLogCheckpoint.create(
START_OF_TIME.plusDays(1),
ImmutableMap.of(1, START_OF_TIME.plusDays(2))));
for (Class<?> clazz : AFFECTED_TYPES) {
assertThat(ofy().load().type(clazz)).named("entities of type " + clazz).isNotEmpty();
}