Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less aws requests #343

Merged
merged 4 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.conveyal.datatools.manager.models.FeedSource;
import com.conveyal.datatools.manager.models.FeedVersion;
import com.conveyal.datatools.manager.models.JsonViews;
import com.conveyal.datatools.manager.models.OtpServer;
import com.conveyal.datatools.manager.models.Project;
import com.conveyal.datatools.manager.persistence.Persistence;
import com.conveyal.datatools.manager.utils.json.JsonManager;
Expand Down Expand Up @@ -343,6 +344,7 @@ public static void register (String apiPrefix) {
JsonManager<Project> slimJson = new JsonManager<>(Project.class, JsonViews.UserInterface.class);
JsonManager<Project> fullJson = new JsonManager<>(Project.class, JsonViews.UserInterface.class);
fullJson.addMixin(Project.class, Project.ProjectWithOtpServers.class);
fullJson.addMixin(OtpServer.class, OtpServer.OtpServerWithoutEc2Instances.class);

get(apiPrefix + "secure/project/:id", ProjectController::getProject, fullJson::write);
get(apiPrefix + "secure/project", ProjectController::getAllProjects, slimJson::write);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
* These methods are mapped to API endpoints by Spark.
*/
public class ServerController {
private static final JsonManager<OtpServer> json = new JsonManager<>(OtpServer.class, JsonViews.UserInterface.class);
private static final Logger LOG = LoggerFactory.getLogger(ServerController.class);

/**
Expand Down Expand Up @@ -137,6 +136,25 @@ private static List<OtpServer> fetchServers (Request req, Response res) {
return Collections.emptyList();
}

/**
* HTTP controller to fetch a single server.
*/
private static OtpServer fetchServer (Request req, Response res) {
String serverId = req.params("id");
Auth0UserProfile userProfile = req.attribute("user");
OtpServer server = Persistence.servers.getById(serverId);
if (
server != null &&
server.projectId != null &&
!userProfile.canAdministerApplication() &&
!userProfile.canAdministerProject(server.projectId)
) {
logMessageAndHalt(req, 403, "Not authorized to view this server");
return null;
}
return server;
}

/**
* Update a single OTP server.
*/
Expand Down Expand Up @@ -207,12 +225,20 @@ private static void validateFields(Request req, OtpServer server) throws HaltExc
* Register HTTP methods with handler methods.
*/
public static void register (String apiPrefix) {
// Construct JSON managers which help serialize the response. Slim JSON is used for the fetchServers method
// while the Full JSON also contains the ec2Instances field. This is to make sure no unnecessary requests to AWS
// are made.
JsonManager<OtpServer> slimJson = new JsonManager<>(OtpServer.class, JsonViews.UserInterface.class);
JsonManager<OtpServer> fullJson = new JsonManager<>(OtpServer.class, JsonViews.UserInterface.class);
slimJson.addMixin(OtpServer.class, OtpServer.OtpServerWithoutEc2Instances.class);

options(apiPrefix + "secure/servers", (q, s) -> "");
delete(apiPrefix + "secure/servers/:id", ServerController::deleteServer, json::write);
delete(apiPrefix + "secure/servers/:id/ec2", ServerController::terminateEC2InstancesForServer, json::write);
get(apiPrefix + "secure/servers", ServerController::fetchServers, json::write);
post(apiPrefix + "secure/servers", ServerController::createServer, json::write);
put(apiPrefix + "secure/servers/:id", ServerController::updateServer, json::write);
delete(apiPrefix + "secure/servers/:id", ServerController::deleteServer, fullJson::write);
delete(apiPrefix + "secure/servers/:id/ec2", ServerController::terminateEC2InstancesForServer, fullJson::write);
get(apiPrefix + "secure/servers", ServerController::fetchServers, slimJson::write);
get(apiPrefix + "secure/servers/:id", ServerController::fetchServer, fullJson::write);
post(apiPrefix + "secure/servers", ServerController::createServer, fullJson::write);
put(apiPrefix + "secure/servers/:id", ServerController::updateServer, fullJson::write);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ private void failJob(String message, Exception e) {
* job should be failed.
*/
private void waitAndCheckInstanceHealth(String waitingFor) throws InstanceHealthException, InterruptedException {
checkInstanceHealth(1);
LOG.info("Waiting {} seconds for {}", DELAY_SECONDS, waitingFor);
Thread.sleep(1000 * DELAY_SECONDS);
checkInstanceHealth(1);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/conveyal/datatools/manager/models/OtpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,16 @@ public EC2ValidationResult validateEC2Config() throws ExecutionException, Interr
);
}

/**
* A MixIn to be applied to this OtpServer that will not include EC2InstanceSummaries when they are not needed in
* the JSON output. This will avoid making unneeded AWS requests.
*
* Usually a mixin would be used on an external class, but since we are changing one thing about a single class, it
* seemed unnecessary to define a new view.
*/
public abstract static class OtpServerWithoutEc2Instances {

@JsonIgnore
public abstract List<EC2InstanceSummary> retrieveEC2InstanceSummaries();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.conveyal.datatools.manager.models;

import com.conveyal.datatools.manager.persistence.Persistence;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.slf4j.Logger;
Expand Down