mirror of
https://github.com/google/nomulus.git
synced 2025-05-01 20:47:52 +02:00
Our goal is to be able to address every Action by looking at the class itself, and to make it clearer at a glance what you need to access the Action's endpoint Currently, we can know from the @Action annotation: - the endpoint path - the Method needed - the authentication level needed This CL adds the service where the Action is hosted, which also translates to the URL. NOTE - currently we don't have any Action hosted on multiple services. I don't think we will ever need it (since they do the same thing no matter which service they are on, so why host it twice?), but if we do we'll have to update the code to allow it. The next step after this is to make sure all the @Parameters are defined on the Action itself, and then we will be able to craft access to the endpoint programatically (or at least verify at run-time we crafted a correct URL) ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=229375735
191 lines
7.9 KiB
Java
191 lines
7.9 KiB
Java
// Copyright 2017 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.backup;
|
|
|
|
import static com.google.common.base.Preconditions.checkArgument;
|
|
import static com.google.common.collect.ImmutableList.toImmutableList;
|
|
import static com.google.common.collect.Iterators.peekingIterator;
|
|
import static google.registry.backup.BackupUtils.createDeserializingIterator;
|
|
import static google.registry.model.ofy.ObjectifyService.ofy;
|
|
import static java.util.Arrays.asList;
|
|
|
|
import com.google.appengine.api.datastore.DatastoreService;
|
|
import com.google.appengine.api.datastore.Entity;
|
|
import com.google.appengine.api.datastore.EntityTranslator;
|
|
import com.google.appengine.tools.cloudstorage.GcsFileMetadata;
|
|
import com.google.appengine.tools.cloudstorage.GcsService;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.PeekingIterator;
|
|
import com.google.common.collect.Streams;
|
|
import com.google.common.flogger.FluentLogger;
|
|
import com.googlecode.objectify.Key;
|
|
import com.googlecode.objectify.Result;
|
|
import com.googlecode.objectify.util.ResultNow;
|
|
import google.registry.config.RegistryEnvironment;
|
|
import google.registry.model.ImmutableObject;
|
|
import google.registry.model.ofy.CommitLogBucket;
|
|
import google.registry.model.ofy.CommitLogCheckpoint;
|
|
import google.registry.model.ofy.CommitLogCheckpointRoot;
|
|
import google.registry.model.ofy.CommitLogManifest;
|
|
import google.registry.model.ofy.CommitLogMutation;
|
|
import google.registry.request.Action;
|
|
import google.registry.request.Parameter;
|
|
import google.registry.request.auth.Auth;
|
|
import google.registry.util.Retrier;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.channels.Channels;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.stream.Stream;
|
|
import javax.inject.Inject;
|
|
import org.joda.time.DateTime;
|
|
|
|
/** Restore Registry 2 commit logs from GCS to Datastore. */
|
|
@Action(
|
|
service = Action.Service.TOOLS,
|
|
path = RestoreCommitLogsAction.PATH,
|
|
method = Action.Method.POST,
|
|
automaticallyPrintOk = true,
|
|
auth = Auth.AUTH_INTERNAL_OR_ADMIN)
|
|
public class RestoreCommitLogsAction implements Runnable {
|
|
|
|
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
|
|
|
static final int BLOCK_SIZE = 1024 * 1024; // Buffer 1mb at a time, for no particular reason.
|
|
|
|
public static final String PATH = "/_dr/task/restoreCommitLogs";
|
|
static final String DRY_RUN_PARAM = "dryRun";
|
|
static final String FROM_TIME_PARAM = "fromTime";
|
|
static final String TO_TIME_PARAM = "toTime";
|
|
|
|
@Inject GcsService gcsService;
|
|
@Inject @Parameter(DRY_RUN_PARAM) boolean dryRun;
|
|
@Inject @Parameter(FROM_TIME_PARAM) DateTime fromTime;
|
|
@Inject @Parameter(TO_TIME_PARAM) DateTime toTime;
|
|
@Inject DatastoreService datastoreService;
|
|
@Inject GcsDiffFileLister diffLister;
|
|
@Inject Retrier retrier;
|
|
@Inject RestoreCommitLogsAction() {}
|
|
|
|
@Override
|
|
public void run() {
|
|
checkArgument( // safety
|
|
RegistryEnvironment.get() == RegistryEnvironment.ALPHA
|
|
|| RegistryEnvironment.get() == RegistryEnvironment.CRASH
|
|
|| RegistryEnvironment.get() == RegistryEnvironment.UNITTEST,
|
|
"DO NOT RUN ANYWHERE ELSE EXCEPT ALPHA, CRASH OR TESTS.");
|
|
if (dryRun) {
|
|
logger.atInfo().log("Running in dryRun mode");
|
|
}
|
|
List<GcsFileMetadata> diffFiles = diffLister.listDiffFiles(fromTime, toTime);
|
|
if (diffFiles.isEmpty()) {
|
|
logger.atInfo().log("Nothing to restore");
|
|
return;
|
|
}
|
|
Map<Integer, DateTime> bucketTimestamps = new HashMap<>();
|
|
CommitLogCheckpoint lastCheckpoint = null;
|
|
for (GcsFileMetadata metadata : diffFiles) {
|
|
logger.atInfo().log("Restoring: %s", metadata.getFilename().getObjectName());
|
|
try (InputStream input = Channels.newInputStream(
|
|
gcsService.openPrefetchingReadChannel(metadata.getFilename(), 0, BLOCK_SIZE))) {
|
|
PeekingIterator<ImmutableObject> commitLogs =
|
|
peekingIterator(createDeserializingIterator(input));
|
|
lastCheckpoint = (CommitLogCheckpoint) commitLogs.next();
|
|
saveOfy(asList(lastCheckpoint)); // Save the checkpoint itself.
|
|
while (commitLogs.hasNext()) {
|
|
CommitLogManifest manifest = restoreOneTransaction(commitLogs);
|
|
bucketTimestamps.put(manifest.getBucketId(), manifest.getCommitTime());
|
|
}
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
// Restore the CommitLogCheckpointRoot and CommitLogBuckets.
|
|
saveOfy(
|
|
Streams.concat(
|
|
bucketTimestamps
|
|
.entrySet()
|
|
.stream()
|
|
.map(
|
|
entry ->
|
|
new CommitLogBucket.Builder()
|
|
.setBucketNum(entry.getKey())
|
|
.setLastWrittenTime(entry.getValue())
|
|
.build()),
|
|
Stream.of(CommitLogCheckpointRoot.create(lastCheckpoint.getCheckpointTime())))
|
|
.collect(toImmutableList()));
|
|
logger.atInfo().log("Restore complete");
|
|
}
|
|
|
|
/**
|
|
* Restore the contents of one transaction to Datastore.
|
|
*
|
|
* <p>The objects to delete are listed in the {@link CommitLogManifest}, which will be the first
|
|
* object in the iterable. The objects to save follow, each as a {@link CommitLogMutation}. We
|
|
* restore by deleting the deletes and recreating the saves from their proto form. We also save
|
|
* the commit logs themselves back to Datastore, so that the commit log system itself is
|
|
* transparently restored alongside the data.
|
|
*
|
|
* @return the manifest, for use in restoring the {@link CommitLogBucket}.
|
|
*/
|
|
private CommitLogManifest restoreOneTransaction(PeekingIterator<ImmutableObject> commitLogs) {
|
|
final CommitLogManifest manifest = (CommitLogManifest) commitLogs.next();
|
|
Result<?> deleteResult = deleteAsync(manifest.getDeletions());
|
|
List<Entity> entitiesToSave = Lists.newArrayList(ofy().save().toEntity(manifest));
|
|
while (commitLogs.hasNext() && commitLogs.peek() instanceof CommitLogMutation) {
|
|
CommitLogMutation mutation = (CommitLogMutation) commitLogs.next();
|
|
entitiesToSave.add(ofy().save().toEntity(mutation));
|
|
entitiesToSave.add(EntityTranslator.createFromPbBytes(mutation.getEntityProtoBytes()));
|
|
}
|
|
saveRaw(entitiesToSave);
|
|
try {
|
|
deleteResult.now();
|
|
} catch (Exception e) {
|
|
retrier.callWithRetry(
|
|
() -> deleteAsync(manifest.getDeletions()).now(), RuntimeException.class);
|
|
}
|
|
return manifest;
|
|
}
|
|
|
|
private void saveRaw(List<Entity> entitiesToSave) {
|
|
if (dryRun) {
|
|
logger.atInfo().log("Would have saved entities: %s", entitiesToSave);
|
|
return;
|
|
}
|
|
retrier.callWithRetry(() -> datastoreService.put(entitiesToSave), RuntimeException.class);
|
|
}
|
|
|
|
private void saveOfy(Iterable<? extends ImmutableObject> objectsToSave) {
|
|
if (dryRun) {
|
|
logger.atInfo().log("Would have saved entities: %s", objectsToSave);
|
|
return;
|
|
}
|
|
retrier.callWithRetry(
|
|
() -> ofy().saveWithoutBackup().entities(objectsToSave).now(), RuntimeException.class);
|
|
}
|
|
|
|
private Result<?> deleteAsync(Set<Key<?>> keysToDelete) {
|
|
if (dryRun) {
|
|
logger.atInfo().log("Would have deleted entities: %s", keysToDelete);
|
|
}
|
|
return dryRun || keysToDelete.isEmpty()
|
|
? new ResultNow<Void>(null)
|
|
: ofy().deleteWithoutBackup().keys(keysToDelete);
|
|
}
|
|
|
|
}
|