Search in sources :

Example 1 with WebServicesClient

use of com.datatorrent.stram.util.WebServicesClient in project apex-core by apache.

the class WebServicesVersionConversionTest method testVersioning.

@Test
public void testVersioning() throws Exception {
    WebServicesClient wsClient = new WebServicesClient();
    Client client = wsClient.getClient();
    WebResource ws = client.resource("http://localhost:" + port).path("/new_path");
    WebServicesVersionConversion.Converter versionConverter = new WebServicesVersionConversion.Converter() {

        @Override
        public String convertCommandPath(String path) {
            if (path.equals("/new_path")) {
                return "/old_path";
            }
            return path;
        }

        @Override
        public String convertResponse(String path, String response) {
            if (path.equals("/new_path")) {
                try {
                    JSONObject json = new JSONObject(response);
                    json.put("new_key", json.get("old_key"));
                    json.remove("old_key");
                    return json.toString();
                } catch (JSONException ex) {
                    throw new RuntimeException(ex);
                }
            }
            return response;
        }
    };
    VersionConversionFilter versionConversionFilter = new VersionConversionFilter(versionConverter);
    client.addFilter(versionConversionFilter);
    JSONObject result = new JSONObject(ws.get(String.class));
    Assert.assertEquals(result.getString("url"), "/old_path");
    Assert.assertEquals(result.getString("new_key"), "value");
    Assert.assertEquals(result.getString("other_key"), "other_value");
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) WebResource(com.sun.jersey.api.client.WebResource) JSONException(org.codehaus.jettison.json.JSONException) WebServicesClient(com.datatorrent.stram.util.WebServicesClient) WebServicesClient(com.datatorrent.stram.util.WebServicesClient) Client(com.sun.jersey.api.client.Client) VersionConversionFilter(com.datatorrent.stram.client.WebServicesVersionConversion.VersionConversionFilter) Test(org.junit.Test)

Example 2 with WebServicesClient

use of com.datatorrent.stram.util.WebServicesClient in project apex-core by apache.

the class RecordingsAgent method startRecording.

public JSONObject startRecording(String appId, String opId, String portName, long numWindows) throws IncompatibleVersionException {
    LOG.debug("Start recording requested for {}.{} ({} windows)", opId, portName, numWindows);
    try {
        final JSONObject request = new JSONObject();
        StramAgent.StramUriSpec uriSpec = new StramAgent.StramUriSpec();
        uriSpec = uriSpec.path(StramWebServices.PATH_PHYSICAL_PLAN_OPERATORS).path(opId);
        if (!StringUtils.isBlank(portName)) {
            uriSpec = uriSpec.path("ports").path(portName);
        }
        uriSpec = uriSpec.path(StramWebServices.PATH_RECORDINGS_START);
        request.put("numWindows", numWindows);
        WebServicesClient webServicesClient = new WebServicesClient();
        return stramAgent.issueStramWebRequest(webServicesClient, appId, uriSpec, new WebServicesClient.WebServicesHandler<JSONObject>() {

            @Override
            public JSONObject process(WebResource.Builder webResource, Class<JSONObject> clazz) {
                return webResource.type(MediaType.APPLICATION_JSON).post(clazz, request);
            }
        });
    } catch (Exception ex) {
        LOG.error("Cannot start recording: ", ex);
        return null;
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) WebResource(com.sun.jersey.api.client.WebResource) WebServicesClient(com.datatorrent.stram.util.WebServicesClient) IncompatibleVersionException(com.datatorrent.stram.client.WebServicesVersionConversion.IncompatibleVersionException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException)

Example 3 with WebServicesClient

use of com.datatorrent.stram.util.WebServicesClient in project apex-core by apache.

the class RecordingsAgent method stopRecording.

public JSONObject stopRecording(String appId, String opId, String portName) throws IncompatibleVersionException {
    try {
        final JSONObject request = new JSONObject();
        StramAgent.StramUriSpec uriSpec = new StramAgent.StramUriSpec();
        uriSpec = uriSpec.path(StramWebServices.PATH_PHYSICAL_PLAN_OPERATORS).path(opId);
        if (!StringUtils.isBlank(portName)) {
            uriSpec = uriSpec.path("ports").path(portName);
        }
        uriSpec = uriSpec.path(StramWebServices.PATH_RECORDINGS_STOP);
        WebServicesClient webServicesClient = new WebServicesClient();
        return stramAgent.issueStramWebRequest(webServicesClient, appId, uriSpec, new WebServicesClient.WebServicesHandler<JSONObject>() {

            @Override
            public JSONObject process(WebResource.Builder webResource, Class<JSONObject> clazz) {
                return webResource.type(MediaType.APPLICATION_JSON).post(clazz, request);
            }
        });
    } catch (Exception ex) {
        LOG.error("Cannot stop recording: ", ex);
        return null;
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) WebResource(com.sun.jersey.api.client.WebResource) WebServicesClient(com.datatorrent.stram.util.WebServicesClient) IncompatibleVersionException(com.datatorrent.stram.client.WebServicesVersionConversion.IncompatibleVersionException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException)

Example 4 with WebServicesClient

use of com.datatorrent.stram.util.WebServicesClient in project apex-core by apache.

the class RecordingsAgent method getRunningContainerIds.

private Set<String> getRunningContainerIds(String appId) {
    Set<String> result = new HashSet<>();
    try {
        WebServicesClient webServicesClient = new WebServicesClient();
        JSONObject response = stramAgent.issueStramWebGetRequest(webServicesClient, appId, StramWebServices.PATH_PHYSICAL_PLAN_CONTAINERS);
        Object containersObj = response.get("containers");
        JSONArray containers;
        if (containersObj instanceof JSONArray) {
            containers = (JSONArray) containersObj;
        } else {
            containers = new JSONArray();
            containers.put(containersObj);
        }
        int len = containers.length();
        for (int i = 0; i < len; i++) {
            JSONObject container = containers.getJSONObject(i);
            if (container.getString("state").equals("ACTIVE")) {
                result.add(container.getString("id"));
            }
        }
    } catch (Exception ex) {
        LOG.warn("Error {} getting running containers for {}. Assuming no containers are running.", ex.getMessage(), appId);
    }
    return result;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) WebServicesClient(com.datatorrent.stram.util.WebServicesClient) JSONObject(org.codehaus.jettison.json.JSONObject) ObjectMapperString(com.datatorrent.common.util.ObjectMapperString) IncompatibleVersionException(com.datatorrent.stram.client.WebServicesVersionConversion.IncompatibleVersionException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) HashSet(java.util.HashSet)

Example 5 with WebServicesClient

use of com.datatorrent.stram.util.WebServicesClient in project apex-core by apache.

the class StramAgent method retrieveWebServicesInfo.

private StramWebServicesInfo retrieveWebServicesInfo(String appId) {
    String url;
    try (YarnClient yarnClient = StramClientUtils.createYarnClient(conf)) {
        ApplicationReport ar = yarnClient.getApplicationReport(ConverterUtils.toApplicationId(appId));
        if (ar == null) {
            LOG.warn("YARN does not have record for this application {}", appId);
            return null;
        } else if (ar.getYarnApplicationState() != YarnApplicationState.RUNNING) {
            LOG.debug("Application {} is not running (state: {})", appId, ar.getYarnApplicationState());
            return null;
        }
        String trackingUrl = ar.getTrackingUrl();
        if (!trackingUrl.startsWith("http://") && !trackingUrl.startsWith("https://")) {
            url = "http://" + trackingUrl;
        } else {
            url = trackingUrl;
        }
        if (StringUtils.isBlank(url)) {
            LOG.error("Cannot get tracking url from YARN");
            return null;
        }
        if (url.endsWith("/")) {
            url = url.substring(0, url.length() - 1);
        }
        url += WebServices.PATH;
    } catch (Exception ex) {
        LOG.error("Cannot retrieve web services info", ex);
        return null;
    }
    WebServicesClient webServicesClient = new WebServicesClient();
    try {
        JSONObject response;
        String secToken = null;
        ClientResponse clientResponse;
        int i = 0;
        while (true) {
            LOG.debug("Accessing url {}", url);
            clientResponse = webServicesClient.process(url, ClientResponse.class, new WebServicesClient.GetWebServicesHandler<ClientResponse>());
            String val = clientResponse.getHeaders().getFirst("Refresh");
            if (val == null) {
                break;
            }
            int index = val.indexOf("url=");
            if (index < 0) {
                break;
            }
            url = val.substring(index + 4);
            if (i++ > MAX_REDIRECTS) {
                LOG.error("Cannot get web service info -- exceeded the max number of redirects");
                return null;
            }
        }
        if (!UserGroupInformation.isSecurityEnabled()) {
            response = new JSONObject(clientResponse.getEntity(String.class));
        } else {
            if (UserGroupInformation.isSecurityEnabled()) {
                for (NewCookie nc : clientResponse.getCookies()) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Cookie " + nc.getName() + " " + nc.getValue());
                    }
                    if (nc.getName().equals(StramWSFilter.CLIENT_COOKIE)) {
                        secToken = nc.getValue();
                    }
                }
            }
            response = new JSONObject(clientResponse.getEntity(String.class));
        }
        String version = response.getString("version");
        response = webServicesClient.process(url + "/" + version + "/stram/info", JSONObject.class, new WebServicesClient.GetWebServicesHandler<JSONObject>());
        String appMasterUrl = response.getString("appMasterTrackingUrl");
        String appPath = response.getString("appPath");
        String user = response.getString("user");
        JSONObject permissionsInfo = null;
        Path permissionsPath = new Path(appPath, "permissions.json");
        LOG.debug("Checking for permission information in file {}", permissionsPath);
        try {
            if (fileSystem.exists(permissionsPath)) {
                LOG.info("Loading permission information");
                try (FSDataInputStream is = fileSystem.open(permissionsPath)) {
                    permissionsInfo = new JSONObject(IOUtils.toString(is));
                }
                LOG.debug("Loaded permission file successfully");
            } else {
                // ignore and log messages if file is not found
                LOG.info("Permission information is not available as the application is not configured with it");
            }
        } catch (IOException ex) {
            // ignore and log message when unable to read the file
            LOG.info("Permission information is not available", ex);
        }
        return new StramWebServicesInfo(appMasterUrl, version, appPath, user, secToken, permissionsInfo);
    } catch (Exception ex) {
        LOG.warn("Cannot retrieve web service info for app {}", appId, ex);
        return null;
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Path(org.apache.hadoop.fs.Path) WebServicesClient(com.datatorrent.stram.util.WebServicesClient) IOException(java.io.IOException) YarnClient(org.apache.hadoop.yarn.client.api.YarnClient) IncompatibleVersionException(com.datatorrent.stram.client.WebServicesVersionConversion.IncompatibleVersionException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) ApplicationReport(org.apache.hadoop.yarn.api.records.ApplicationReport) JSONObject(org.codehaus.jettison.json.JSONObject) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) NewCookie(javax.ws.rs.core.NewCookie)

Aggregations

WebServicesClient (com.datatorrent.stram.util.WebServicesClient)5 JSONException (org.codehaus.jettison.json.JSONException)5 JSONObject (org.codehaus.jettison.json.JSONObject)5 IncompatibleVersionException (com.datatorrent.stram.client.WebServicesVersionConversion.IncompatibleVersionException)4 IOException (java.io.IOException)4 WebResource (com.sun.jersey.api.client.WebResource)3 ObjectMapperString (com.datatorrent.common.util.ObjectMapperString)1 VersionConversionFilter (com.datatorrent.stram.client.WebServicesVersionConversion.VersionConversionFilter)1 Client (com.sun.jersey.api.client.Client)1 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)1 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 HashSet (java.util.HashSet)1 NewCookie (javax.ws.rs.core.NewCookie)1 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)1 Path (org.apache.hadoop.fs.Path)1 ApplicationReport (org.apache.hadoop.yarn.api.records.ApplicationReport)1 YarnClient (org.apache.hadoop.yarn.client.api.YarnClient)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 Test (org.junit.Test)1