Cut over to batched async deletion for contacts/hosts

Also consolidates the DNS refresh functionality in AsyncFlowUtils that was
being used by HostUpdateFlow into AsyncFlowEnqueuer.

TESTED=I threw together some batch scripts to create dozens of contacts on
alpha and then request their deletion, and the [] ran fine and
successfully deleted them in batches.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133714691
This commit is contained in:
mcilwain 2016-09-20 09:29:39 -07:00 committed by Ben McIlwain
parent 65ff6b45d1
commit 2dcac3ca68
11 changed files with 74 additions and 190 deletions

View file

@ -14,17 +14,23 @@
package google.registry.flows.async;
import static com.google.appengine.api.taskqueue.QueueFactory.getQueue;
import static google.registry.flows.async.DeleteContactsAndHostsAction.PARAM_IS_SUPERUSER;
import static google.registry.flows.async.DeleteContactsAndHostsAction.PARAM_REQUESTING_CLIENT_ID;
import static google.registry.flows.async.DeleteContactsAndHostsAction.PARAM_RESOURCE_KEY;
import static google.registry.flows.async.DnsRefreshForHostRenameAction.PARAM_HOST_KEY;
import static google.registry.request.Actions.getPathForAction;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.RetryOptions;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.taskqueue.TaskOptions.Method;
import com.google.appengine.api.taskqueue.TransientFailureException;
import com.googlecode.objectify.Key;
import google.registry.config.ConfigModule.Config;
import google.registry.config.RegistryEnvironment;
import google.registry.model.EppResource;
import google.registry.model.host.HostResource;
import google.registry.util.FormattingLogger;
import google.registry.util.Retrier;
import java.util.concurrent.Callable;
@ -32,40 +38,58 @@ import javax.inject.Inject;
import javax.inject.Named;
import org.joda.time.Duration;
/** Helper class to enqueue tasks for handling asynchronous deletions to pull queues. */
/** Helper class to enqueue tasks for handling asynchronous operations in flows. */
public final class AsyncFlowEnqueuer {
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
@Inject @Config("asyncDeleteFlowMapreduceDelay") Duration asyncDeleteDelay;
@Inject @Named("async-delete-pull") Queue queue;
@Inject @Named("async-delete-pull") Queue asyncDeletePullQueue;
@Inject Retrier retrier;
@Inject AsyncFlowEnqueuer() {}
/**
* Enqueues a task to asynchronously delete a contact or host, by key.
*
* <p>Note that the clientId is of the logged-in registrar that is requesting the deletion, not
* necessarily the current owner of the resource.
*/
/** Enqueues a task to asynchronously delete a contact or host, by key. */
public void enqueueAsyncDelete(
EppResource resourceToDelete, String clientId, boolean isSuperuser) {
EppResource resourceToDelete, String requestingClientId, boolean isSuperuser) {
Key<EppResource> resourceKey = Key.create(resourceToDelete);
logger.infofmt(
"Enqueueing async action to delete %s on behalf of registrar %s.", resourceKey, clientId);
final TaskOptions options =
"Enqueueing async deletion of %s on behalf of registrar %s.",
resourceKey, requestingClientId);
final TaskOptions task =
TaskOptions.Builder
.withMethod(Method.PULL)
.countdownMillis(asyncDeleteDelay.getMillis())
.param(PARAM_RESOURCE_KEY, resourceKey.getString())
.param(PARAM_REQUESTING_CLIENT_ID, clientId)
.param(PARAM_REQUESTING_CLIENT_ID, requestingClientId)
.param(PARAM_IS_SUPERUSER, Boolean.toString(isSuperuser));
// Retry on transient failure exceptions so that the entire flow isn't aborted unnecessarily.
addTaskToQueueWithRetry(asyncDeletePullQueue, task);
}
/** Enqueues a task to asynchronously refresh DNS for a host. */
public void enqueueAsyncDnsRefresh(HostResource host) {
logger.infofmt("Enqueueing async DNS refresh for host %s", Key.create(host));
// Aggressively back off if the task fails, to minimize flooding the logs.
RetryOptions retryOptions =
RetryOptions.Builder.withMinBackoffSeconds(
RegistryEnvironment.get().config().getAsyncFlowFailureBackoff().getStandardSeconds());
final TaskOptions task =
TaskOptions.Builder.withUrl(getPathForAction(DnsRefreshForHostRenameAction.class))
.retryOptions(retryOptions)
.param(PARAM_HOST_KEY, Key.create(host).getString())
.method(Method.GET);
addTaskToQueueWithRetry(getQueue("flows-async"), task);
}
/**
* Adds a task to a queue with retrying, to avoid aborting the entire flow over a transient issue
* enqueuing a task.
*/
private void addTaskToQueueWithRetry(final Queue queue, final TaskOptions task) {
retrier.callWithRetry(new Callable<Void>() {
@Override
public Void call() throws Exception {
queue.add(options);
return null;
}}, TransientFailureException.class);
@Override
public Void call() throws Exception {
queue.add(task);
return null;
}}, TransientFailureException.class);
}
}