Refactor update snapshot view servlet into a Daggerized Action

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=120224051
This commit is contained in:
mcilwain 2016-04-19 06:47:25 -07:00 committed by Justine Tunney
parent 4fbf613955
commit c5d09227c5
8 changed files with 68 additions and 121 deletions

View file

@ -61,7 +61,7 @@ public interface RegistryConfig {
/**
* Returns the BigQuery dataset for storing views pointing to the latest datastore snapshot.
*
* @see com.google.domain.registry.export.UpdateSnapshotViewServlet
* @see com.google.domain.registry.export.UpdateSnapshotViewAction
*/
public String getLatestSnapshotDataset();

View file

@ -158,14 +158,9 @@
<url-pattern>/_dr/task/loadSnapshot</url-pattern>
</servlet-mapping>
<servlet>
<description>Updates a view to point at a certain snapshot in BigQuery.</description>
<display-name>Update snapshot view in BigQuery</display-name>
<servlet-name>updateSnapshotView</servlet-name>
<servlet-class>com.google.domain.registry.export.UpdateSnapshotViewServlet</servlet-class>
</servlet>
<!-- Updates a view to point at a certain snapshot in BigQuery. -->
<servlet-mapping>
<servlet-name>updateSnapshotView</servlet-name>
<servlet-name>backend-servlet</servlet-name>
<url-pattern>/_dr/task/updateSnapshotView</url-pattern>
</servlet-mapping>

View file

@ -17,9 +17,13 @@ package com.google.domain.registry.export;
import static com.google.domain.registry.export.BigqueryPollJobAction.CHAINED_TASK_QUEUE_HEADER;
import static com.google.domain.registry.export.BigqueryPollJobAction.JOB_ID_HEADER;
import static com.google.domain.registry.export.BigqueryPollJobAction.PROJECT_ID_HEADER;
import static com.google.domain.registry.export.UpdateSnapshotViewAction.SNAPSHOT_DATASET_ID_PARAM;
import static com.google.domain.registry.export.UpdateSnapshotViewAction.SNAPSHOT_KIND_PARAM;
import static com.google.domain.registry.export.UpdateSnapshotViewAction.SNAPSHOT_TABLE_ID_PARAM;
import static com.google.domain.registry.request.RequestParameters.extractRequiredHeader;
import com.google.domain.registry.request.Header;
import com.google.domain.registry.request.Parameter;
import dagger.Module;
import dagger.Provides;
@ -29,6 +33,25 @@ import javax.servlet.http.HttpServletRequest;
/** Dagger module for data export tasks. */
@Module
public final class ExportRequestModule {
@Provides
@Parameter(SNAPSHOT_DATASET_ID_PARAM)
static String provideDatasetId(HttpServletRequest req) {
return extractRequiredHeader(req, SNAPSHOT_DATASET_ID_PARAM);
}
@Provides
@Parameter(SNAPSHOT_TABLE_ID_PARAM)
static String provideTableId(HttpServletRequest req) {
return extractRequiredHeader(req, SNAPSHOT_TABLE_ID_PARAM);
}
@Provides
@Parameter(SNAPSHOT_KIND_PARAM)
static String provideKind(HttpServletRequest req) {
return extractRequiredHeader(req, SNAPSHOT_KIND_PARAM);
}
@Provides
@Header(CHAINED_TASK_QUEUE_HEADER)
static String provideChainedTaskQueue(HttpServletRequest req) {

View file

@ -155,9 +155,9 @@ public class LoadSnapshotServlet extends HttpServlet {
// well-known view in BigQuery to point at the newly loaded snapshot table for this kind.
bigqueryPollEnqueuer.enqueuePollTask(
jobRef,
UpdateSnapshotViewServlet.createViewUpdateTask(
UpdateSnapshotViewAction.createViewUpdateTask(
ENVIRONMENT.config().getSnapshotsDataset(), tableId, kindName),
QueueFactory.getQueue(UpdateSnapshotViewServlet.QUEUE));
QueueFactory.getQueue(UpdateSnapshotViewAction.QUEUE));
builder.append(String.format(" - %s:%s\n", projectId, jobId));
logger.infofmt("Submitted load job %s:%s", projectId, jobId);

View file

@ -13,13 +13,7 @@
// limitations under the License.
package com.google.domain.registry.export;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.html.HtmlEscapers.htmlEscaper;
import static com.google.domain.registry.util.HttpServletUtils.getRequiredParameterValue;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static com.google.domain.registry.request.Action.Method.POST;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.bigquery.Bigquery;
@ -28,21 +22,20 @@ import com.google.api.services.bigquery.model.TableReference;
import com.google.api.services.bigquery.model.ViewDefinition;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.taskqueue.TaskOptions.Method;
import com.google.common.net.MediaType;
import com.google.domain.registry.bigquery.BigqueryFactory;
import com.google.domain.registry.config.RegistryEnvironment;
import com.google.domain.registry.request.Action;
import com.google.domain.registry.request.Parameter;
import com.google.domain.registry.util.FormattingLogger;
import com.google.domain.registry.util.NonFinalForTesting;
import com.google.domain.registry.util.SqlTemplate;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.inject.Inject;
/** Update a well-known view to point at a certain datastore snapshot table in BigQuery. */
public class UpdateSnapshotViewServlet extends HttpServlet {
@Action(path = UpdateSnapshotViewAction.PATH, method = POST)
public class UpdateSnapshotViewAction implements Runnable {
private static final RegistryEnvironment ENVIRONMENT = RegistryEnvironment.get();
@ -57,8 +50,11 @@ public class UpdateSnapshotViewServlet extends HttpServlet {
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
@NonFinalForTesting
private static BigqueryFactory bigqueryFactory = new BigqueryFactory();
@Inject @Parameter(SNAPSHOT_DATASET_ID_PARAM) String datasetId;
@Inject @Parameter(SNAPSHOT_TABLE_ID_PARAM) String tableId;
@Inject @Parameter(SNAPSHOT_KIND_PARAM) String kindName;
@Inject BigqueryFactory bigqueryFactory;
@Inject UpdateSnapshotViewAction() {}
/** Create a task for updating a snapshot view. */
public static TaskOptions createViewUpdateTask(
@ -71,26 +67,15 @@ public class UpdateSnapshotViewServlet extends HttpServlet {
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
public void run() {
try {
String datasetId = getRequiredParameterValue(req, SNAPSHOT_DATASET_ID_PARAM);
String tableId = getRequiredParameterValue(req, SNAPSHOT_TABLE_ID_PARAM);
String kindName = getRequiredParameterValue(req, SNAPSHOT_KIND_PARAM);
String message = updateSnapshotView(datasetId, tableId, kindName);
rsp.setStatus(SC_OK);
rsp.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString());
rsp.getWriter().write("OK\n\n" + message);
updateSnapshotView(datasetId, tableId, kindName);
} catch (Throwable e) {
logger.severe(e, e.toString());
rsp.sendError(
e instanceof IllegalArgumentException ? SC_BAD_REQUEST : SC_INTERNAL_SERVER_ERROR,
htmlEscaper().escape(firstNonNull(e.getMessage(), e.toString())));
throw new RuntimeException("Error in update snapshot view action.", e);
}
}
private String updateSnapshotView(String datasetId, String tableId, String kindName)
private void updateSnapshotView(String datasetId, String tableId, String kindName)
throws IOException {
String projectId = ENVIRONMENT.config().getProjectId();
Bigquery bigquery =
@ -111,7 +96,6 @@ public class UpdateSnapshotViewServlet extends HttpServlet {
"Updated view %s:%s to point at snapshot table %s:%s.",
ENVIRONMENT.config().getLatestSnapshotDataset(), kindName, datasetId, tableId);
logger.info(message);
return message;
}
private static void updateTable(Bigquery bigquery, Table table) throws IOException {

View file

@ -32,6 +32,7 @@ import com.google.domain.registry.export.ExportDomainListsAction;
import com.google.domain.registry.export.ExportRequestModule;
import com.google.domain.registry.export.ExportReservedTermsAction;
import com.google.domain.registry.export.SyncGroupMembersAction;
import com.google.domain.registry.export.UpdateSnapshotViewAction;
import com.google.domain.registry.export.sheet.SheetModule;
import com.google.domain.registry.export.sheet.SyncRegistrarsSheetAction;
import com.google.domain.registry.flows.async.AsyncFlowsModule;
@ -101,6 +102,7 @@ interface BackendRequestComponent {
TmchCrlAction tmchCrlAction();
TmchDnlAction tmchDnlAction();
TmchSmdrlAction tmchSmdrlAction();
UpdateSnapshotViewAction updateSnapshotViewAction();
WriteDnsAction writeDnsAction();
VerifyEntityIntegrityAction verifyEntityIntegrityAction();
}