Skip to content

Commit

Permalink
adding REST call and turning on machine
Browse files Browse the repository at this point in the history
  • Loading branch information
jortiz16 committed Aug 8, 2016
1 parent f8e2e5e commit 7b16827
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 17 deletions.
84 changes: 84 additions & 0 deletions src/edu/washington/escience/myria/api/DriverAddWorker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package edu.washington.escience.myria.api;

import java.io.IOException;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.apache.reef.tang.exceptions.BindException;
import org.apache.reef.tang.exceptions.InjectionException;
import org.slf4j.LoggerFactory;

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.InstanceStateChange;
import com.amazonaws.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.StartInstancesResult;
import com.wordnik.swagger.annotations.Api;

import edu.washington.escience.myria.DbException;
import edu.washington.escience.myria.coordinator.ConfigFileException;
import edu.washington.escience.myria.daemon.MyriaDriver;

/*
* TEST TEST TEST
*/
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MyriaApiConstants.JSON_UTF_8)
@Path("/driver")
@Api(value = "/driver", description = "Adding calls to the driver")
public class DriverAddWorker {

/** The Myria server running on the master. */
@Context private MyriaDriver driver;

/** Logger. */
protected static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DriverAddWorker.class);

/**
* @throws InjectionException
* @throws IOException
* @throws BindException
* @throws InterruptedException
*/
@GET
@Path("/test/")
public Response test()
throws DbException, BindException, IOException, InjectionException, InterruptedException,
ConfigFileException {
LOGGER.warn("HERE");
/* Build the response to return the queryId */

//Call some worker from Amazon here and start it
StartInstancesRequest request = new StartInstancesRequest().withInstanceIds("ID HERE");
AmazonEC2Client client = new AmazonEC2Client(new DefaultAWSCredentialsProviderChain());
StartInstancesResult startInstancesResult = client.startInstances(request);

List<InstanceStateChange> listStates = startInstancesResult.getStartingInstances();
while (true) {
LOGGER.warn("IN LOOP");
boolean isDone = false;
for (InstanceStateChange l : listStates) {
LOGGER.warn("STATUS " + l.getCurrentState().getName());
if (l.getCurrentState().getName() == "Running") {
isDone = true;
}
}

if (isDone) {
break;
}
}

ResponseBuilder response = Response.ok();
int workerCount = driver.fakeAdditionalWorker();
return response.entity(workerCount).build();
}
}
88 changes: 88 additions & 0 deletions src/edu/washington/escience/myria/api/DriverApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package edu.washington.escience.myria.api;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;

import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.message.GZipEncoder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.filter.EncodingFilter;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.wordnik.swagger.jaxrs.config.BeanConfig;

import edu.washington.escience.myria.daemon.MyriaDriver;

public class DriverApplication extends ResourceConfig {

public DriverApplication(final MyriaDriver server) {
/*
* Tell Jersey to look for resources inside the entire project, and also for Swagger.
*/
packages(new String[] {"edu.washington.escience.myria", "com.wordnik.swagger.jersey.listing"});

/*
* Disable WADL - throws error messages when using Swagger, and not needed.
*/
property(ServerProperties.WADL_FEATURE_DISABLE, true);

/* Enable Jackson's JSON Serialization/Deserialization. */
register(JacksonJsonProvider.class);

/* Enable Multipart. */
register(MultiPartFeature.class);

/* Register the singleton binder. */
register(
new AbstractBinder() {
@Override
protected void configure() {
/* Singletons binding. */
bind(server).to(MyriaDriver.class);
}
});

/* Enable GZIP compression/decompression */
register(EncodingFilter.class);
register(GZipEncoder.class);

/* Swagger configuration -- must come BEFORE Swagger classes are added. */
BeanConfig myriaBeanConfig = new BeanConfig();
/*
* TODO(dhalperi): make this more dynamic based on either Catalog or runtime option.
*/
myriaBeanConfig.setBasePath("http://rest.myria.cs.washington.edu:1776");
myriaBeanConfig.setVersion("0.1.0");
myriaBeanConfig.setResourcePackage("edu.washington.escience.myria.api");
myriaBeanConfig.setScan(true);

/*
* Add a response filter (i.e., runs on all responses) that sets headers for cross-origin
* objects.
*/
register(new CrossOriginResponseFilter());
}

/**
* This is a container response filter. It will run on all responses leaving the server and add
* the CORS filters saying that these API calls should be allowed from any website. This is a
* mechanism increasingly supported by modern browsers instead of, e.g., JSONP.
*
* For more information, visit http://www.w3.org/TR/cors/ and http://enable-cors.org/
*
* TODO revisit the security of this model
*
*/
private class CrossOriginResponseFilter implements ContainerResponseFilter {
@Override
public void filter(
final ContainerRequestContext request, final ContainerResponseContext response) {
response.getHeaders().add("Access-Control-Allow-Origin", "*");
response.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
response.getHeaders().add("Access-Control-Allow-Headers", "Content-Type");
}
}
}
54 changes: 54 additions & 0 deletions src/edu/washington/escience/myria/api/MyriaDriverApiServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package edu.washington.escience.myria.api;

import java.io.IOException;
import java.net.URI;

import javax.inject.Inject;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.washington.escience.myria.daemon.MyriaDriver;

public class MyriaDriverApiServer {
/** The logger for this class. */
private static final Logger LOGGER = LoggerFactory.getLogger(MasterApiServer.class);

/** The Jersey web server. */
private final HttpServer webServer;

//Where is this declared?
@Inject
public MyriaDriverApiServer(final MyriaDriver driver) throws IOException {
URI baseUri = UriBuilder.fromUri("http://0.0.0.0/").port(8777).build();
ResourceConfig masterApplication = new DriverApplication(driver);
LOGGER.info("Not enabling SSL");
webServer = GrizzlyHttpServerFactory.createHttpServer(baseUri, masterApplication);
}

/**
* Starts the master Restlet API server.
*
* @throws Exception if there is an error starting the server.
*/
public void start() throws Exception {
LOGGER.info("Starting API server");
webServer.start();
LOGGER.info("API server started.");
}

/**
* Stops the master Restlet API server.
*
* @throws Exception if there is an error stopping the server.
*/
public void stop() throws Exception {
LOGGER.info("Stopping API server");
webServer.shutdownNow();
LOGGER.info("API server stopped");
}
}
Loading

0 comments on commit 7b16827

Please sign in to comment.