Add a convenience class for making REST API handlers retryable
We want to get all callers of BatchUpdate to use the new RetryHelper, but explicitly passing a lambda to the RetryHelper instance in every case is going to be a lot of boilerplate. Add some sugar for the most common case, a REST API handler. Using the RetryingRestModifyView, converting a handler to be retry-friendly is as simple as adding the base class and changing a method name (and some minor cleanup like removing a BatchUpdate.Factory field and twiddling exceptions). Adding an intervening abstract class requires a tweak to the way RestApiServlet extracts the input type using reflection. Fortunately, there is a TypeLiteral method that does exactly what we want. Change-Id: If2b78f0d556a49972ea9976057dd5f9dc21ec750
This commit is contained in:
@@ -19,7 +19,6 @@ import com.google.gerrit.common.TimeUtil;
|
||||
import com.google.gerrit.extensions.restapi.DefaultInput;
|
||||
import com.google.gerrit.extensions.restapi.Response;
|
||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||
import com.google.gerrit.extensions.restapi.RestModifyView;
|
||||
import com.google.gerrit.extensions.webui.UiAction;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.reviewdb.client.ChangeMessage;
|
||||
@@ -34,6 +33,8 @@ import com.google.gerrit.server.update.BatchUpdate;
|
||||
import com.google.gerrit.server.update.BatchUpdateOp;
|
||||
import com.google.gerrit.server.update.ChangeContext;
|
||||
import com.google.gerrit.server.update.Context;
|
||||
import com.google.gerrit.server.update.RetryHelper;
|
||||
import com.google.gerrit.server.update.RetryingRestModifyView;
|
||||
import com.google.gerrit.server.update.UpdateException;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
@@ -41,10 +42,10 @@ import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class PutTopic implements RestModifyView<ChangeResource, Input>, UiAction<ChangeResource> {
|
||||
public class PutTopic extends RetryingRestModifyView<ChangeResource, Input, Response<String>>
|
||||
implements UiAction<ChangeResource> {
|
||||
private final Provider<ReviewDb> dbProvider;
|
||||
private final ChangeMessagesUtil cmUtil;
|
||||
private final BatchUpdate.Factory batchUpdateFactory;
|
||||
private final TopicEdited topicEdited;
|
||||
|
||||
public static class Input {
|
||||
@@ -55,29 +56,27 @@ public class PutTopic implements RestModifyView<ChangeResource, Input>, UiAction
|
||||
PutTopic(
|
||||
Provider<ReviewDb> dbProvider,
|
||||
ChangeMessagesUtil cmUtil,
|
||||
BatchUpdate.Factory batchUpdateFactory,
|
||||
RetryHelper retryHelper,
|
||||
TopicEdited topicEdited) {
|
||||
super(retryHelper);
|
||||
this.dbProvider = dbProvider;
|
||||
this.cmUtil = cmUtil;
|
||||
this.batchUpdateFactory = batchUpdateFactory;
|
||||
this.topicEdited = topicEdited;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<String> apply(ChangeResource req, Input input)
|
||||
protected Response<String> applyImpl(
|
||||
BatchUpdate.Factory updateFactory, ChangeResource req, Input input)
|
||||
throws UpdateException, RestApiException, PermissionBackendException {
|
||||
req.permissions().check(ChangePermission.EDIT_TOPIC_NAME);
|
||||
|
||||
Op op = new Op(input != null ? input : new Input());
|
||||
try (BatchUpdate u =
|
||||
batchUpdateFactory.create(
|
||||
updateFactory.create(
|
||||
dbProvider.get(), req.getChange().getProject(), req.getUser(), TimeUtil.nowTs())) {
|
||||
u.addOp(req.getId(), op);
|
||||
u.execute();
|
||||
}
|
||||
return Strings.isNullOrEmpty(op.newTopicName)
|
||||
? Response.<String>none()
|
||||
: Response.ok(op.newTopicName);
|
||||
return Strings.isNullOrEmpty(op.newTopicName) ? Response.none() : Response.ok(op.newTopicName);
|
||||
}
|
||||
|
||||
private class Op implements BatchUpdateOp {
|
||||
|
||||
Reference in New Issue
Block a user