Search in sources :

Example 1 with LogicalPlanRequest

use of com.datatorrent.stram.plan.logical.requests.LogicalPlanRequest in project apex-core by apache.

the class StramWebServices method logicalPlanModification.

// not supported by WebAppProxyServlet, can only be called directly
@POST
@Path(PATH_LOGICAL_PLAN)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject logicalPlanModification(JSONObject request) {
    init();
    JSONObject response = new JSONObject();
    try {
        JSONArray jsonArray = request.getJSONArray("requests");
        List<LogicalPlanRequest> requests = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObj = (JSONObject) jsonArray.get(i);
            LogicalPlanRequest requestObj = (LogicalPlanRequest) Class.forName(LogicalPlanRequest.class.getPackage().getName() + "." + jsonObj.getString("requestType")).newInstance();
            @SuppressWarnings("unchecked") Map<String, String> properties = BeanUtils.describe(requestObj);
            @SuppressWarnings("unchecked") Iterator<String> keys = jsonObj.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                if (!key.equals("requestType")) {
                    properties.put(key, jsonObj.get(key).toString());
                }
            }
            BeanUtils.populate(requestObj, properties);
            requests.add(requestObj);
        }
        Future<?> fr = dagManager.logicalPlanModification(requests);
        fr.get(3000, TimeUnit.MILLISECONDS);
    } catch (Exception ex) {
        LOG.error("Error processing plan change", ex);
        try {
            if (ex instanceof ExecutionException) {
                response.put("error", ex.getCause().toString());
            } else {
                response.put("error", ex.toString());
            }
        } catch (Exception e) {
        // ignore
        }
    }
    return response;
}
Also used : JSONArray(org.codehaus.jettison.json.JSONArray) ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) JsonProcessingException(org.codehaus.jackson.JsonProcessingException) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JSONException(org.codehaus.jettison.json.JSONException) JSONObject(org.codehaus.jettison.json.JSONObject) LogicalPlanRequest(com.datatorrent.stram.plan.logical.requests.LogicalPlanRequest) ExecutionException(java.util.concurrent.ExecutionException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 2 with LogicalPlanRequest

use of com.datatorrent.stram.plan.logical.requests.LogicalPlanRequest in project apex-core by apache.

the class StramWebServicesTest method testSubmitLogicalPlanChange.

@Test
public void testSubmitLogicalPlanChange() throws JSONException, Exception {
    List<LogicalPlanRequest> requests = new ArrayList<>();
    WebResource r = resource();
    CreateOperatorRequest request1 = new CreateOperatorRequest();
    request1.setOperatorName("operatorName");
    request1.setOperatorFQCN("className");
    requests.add(request1);
    SetOperatorPropertyRequest request2 = new SetOperatorPropertyRequest();
    request2.setOperatorName("operatorName");
    request2.setPropertyName("propertyName");
    request2.setPropertyValue("propertyValue");
    requests.add(request2);
    ObjectMapper mapper = new ObjectMapper();
    final Map<String, Object> m = new HashMap<>();
    m.put("requests", requests);
    final JSONObject jsonRequest = new JSONObject(mapper.writeValueAsString(m));
    ClientResponse response = r.path(StramWebServices.PATH).path(StramWebServices.PATH_LOGICAL_PLAN).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, jsonRequest);
    assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
    assertEquals(DummyStreamingContainerManager.lastRequests.size(), 2);
    LogicalPlanRequest request = DummyStreamingContainerManager.lastRequests.get(0);
    assertTrue(request instanceof CreateOperatorRequest);
    request1 = (CreateOperatorRequest) request;
    assertEquals(request1.getOperatorName(), "operatorName");
    assertEquals(request1.getOperatorFQCN(), "className");
    request = DummyStreamingContainerManager.lastRequests.get(1);
    assertTrue(request instanceof SetOperatorPropertyRequest);
    request2 = (SetOperatorPropertyRequest) request;
    assertEquals(request2.getOperatorName(), "operatorName");
    assertEquals(request2.getPropertyName(), "propertyName");
    assertEquals(request2.getPropertyValue(), "propertyValue");
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SetOperatorPropertyRequest(com.datatorrent.stram.plan.logical.requests.SetOperatorPropertyRequest) WebResource(com.sun.jersey.api.client.WebResource) JSONObject(org.codehaus.jettison.json.JSONObject) LogicalPlanRequest(com.datatorrent.stram.plan.logical.requests.LogicalPlanRequest) JSONObject(org.codehaus.jettison.json.JSONObject) CreateOperatorRequest(com.datatorrent.stram.plan.logical.requests.CreateOperatorRequest) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) JerseyTest(com.sun.jersey.test.framework.JerseyTest) Test(org.junit.Test)

Aggregations

LogicalPlanRequest (com.datatorrent.stram.plan.logical.requests.LogicalPlanRequest)2 ArrayList (java.util.ArrayList)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 CreateOperatorRequest (com.datatorrent.stram.plan.logical.requests.CreateOperatorRequest)1 SetOperatorPropertyRequest (com.datatorrent.stram.plan.logical.requests.SetOperatorPropertyRequest)1 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 WebResource (com.sun.jersey.api.client.WebResource)1 JerseyTest (com.sun.jersey.test.framework.JerseyTest)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 NotFoundException (org.apache.hadoop.yarn.webapp.NotFoundException)1 JsonProcessingException (org.codehaus.jackson.JsonProcessingException)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 JSONArray (org.codehaus.jettison.json.JSONArray)1