Search in sources :

Example 1 with YarnSchedulerResponse

use of org.apache.ranger.services.yarn.client.json.model.YarnSchedulerResponse in project ranger by apache.

the class YarnClient method getQueueList.

public List<String> getQueueList(final String queueNameMatching, final List<String> existingQueueList) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Getting Yarn queue list for queueNameMatching : " + queueNameMatching);
    }
    final String errMsg = errMessage;
    List<String> ret = null;
    Callable<List<String>> callableYarnQListGetter = new Callable<List<String>>() {

        @Override
        public List<String> call() {
            List<String> yarnQueueListGetter = null;
            Subject subj = getLoginSubject();
            if (subj != null) {
                yarnQueueListGetter = Subject.doAs(subj, new PrivilegedAction<List<String>>() {

                    @Override
                    public List<String> run() {
                        if (yarnQUrl == null || yarnQUrl.trim().isEmpty()) {
                            return null;
                        }
                        String[] yarnQUrls = yarnQUrl.trim().split("[,;]");
                        if (yarnQUrls == null || yarnQUrls.length == 0) {
                            return null;
                        }
                        Client client = Client.create();
                        ClientResponse response = null;
                        for (String currentUrl : yarnQUrls) {
                            if (currentUrl == null || currentUrl.trim().isEmpty()) {
                                continue;
                            }
                            String url = currentUrl.trim() + YARN_LIST_API_ENDPOINT;
                            try {
                                response = getQueueResponse(url, client);
                                if (response != null) {
                                    if (response.getStatus() == 200) {
                                        break;
                                    } else {
                                        response.close();
                                    }
                                }
                            } catch (Throwable t) {
                                String msgDesc = "Exception while getting Yarn Queue List." + " URL : " + url;
                                LOG.error(msgDesc, t);
                            }
                        }
                        List<String> lret = new ArrayList<String>();
                        try {
                            if (response != null && response.getStatus() == 200) {
                                String jsonString = response.getEntity(String.class);
                                Gson gson = new GsonBuilder().setPrettyPrinting().create();
                                YarnSchedulerResponse yarnQResponse = gson.fromJson(jsonString, YarnSchedulerResponse.class);
                                if (yarnQResponse != null) {
                                    List<String> yarnQueueList = yarnQResponse.getQueueNames();
                                    if (yarnQueueList != null) {
                                        for (String yarnQueueName : yarnQueueList) {
                                            if (existingQueueList != null && existingQueueList.contains(yarnQueueName)) {
                                                continue;
                                            }
                                            if (queueNameMatching == null || queueNameMatching.isEmpty() || yarnQueueName.startsWith(queueNameMatching)) {
                                                if (LOG.isDebugEnabled()) {
                                                    LOG.debug("getQueueList():Adding yarnQueue " + yarnQueueName);
                                                }
                                                lret.add(yarnQueueName);
                                            }
                                        }
                                    }
                                }
                            } else {
                                String msgDesc = "Unable to get a valid response for " + "expected mime type : [" + EXPECTED_MIME_TYPE + "] URL : " + yarnQUrl + " - got null response.";
                                LOG.error(msgDesc);
                                HadoopException hdpException = new HadoopException(msgDesc);
                                hdpException.generateResponseDataMap(false, msgDesc, msgDesc + errMsg, null, null);
                                throw hdpException;
                            }
                        } catch (HadoopException he) {
                            throw he;
                        } catch (Throwable t) {
                            String msgDesc = "Exception while getting Yarn Queue List." + " URL : " + yarnQUrl;
                            HadoopException hdpException = new HadoopException(msgDesc, t);
                            LOG.error(msgDesc, t);
                            hdpException.generateResponseDataMap(false, BaseClient.getMessage(t), msgDesc + errMsg, null, null);
                            throw hdpException;
                        } finally {
                            if (response != null) {
                                response.close();
                            }
                            if (client != null) {
                                client.destroy();
                            }
                        }
                        return lret;
                    }

                    private ClientResponse getQueueResponse(String url, Client client) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("getQueueResponse():calling " + url);
                        }
                        WebResource webResource = client.resource(url);
                        ClientResponse response = webResource.accept(EXPECTED_MIME_TYPE).get(ClientResponse.class);
                        if (response != null) {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("getQueueResponse():response.getStatus()= " + response.getStatus());
                            }
                            if (response.getStatus() != 200) {
                                LOG.info("getQueueResponse():response.getStatus()= " + response.getStatus() + " for URL " + url + ", failed to get queue list");
                                String jsonString = response.getEntity(String.class);
                                LOG.info(jsonString);
                            }
                        }
                        return response;
                    }
                });
            }
            return yarnQueueListGetter;
        }
    };
    try {
        ret = timedTask(callableYarnQListGetter, 5, TimeUnit.SECONDS);
    } catch (Throwable t) {
        LOG.error("Unable to get Yarn Queue list from [" + yarnQUrl + "]", t);
        String msgDesc = "Unable to get a valid response for " + "expected mime type : [" + EXPECTED_MIME_TYPE + "] URL : " + yarnQUrl;
        HadoopException hdpException = new HadoopException(msgDesc, t);
        LOG.error(msgDesc, t);
        hdpException.generateResponseDataMap(false, BaseClient.getMessage(t), msgDesc + errMsg, null, null);
        throw hdpException;
    }
    return ret;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) WebResource(com.sun.jersey.api.client.WebResource) HadoopException(org.apache.ranger.plugin.client.HadoopException) Callable(java.util.concurrent.Callable) Subject(javax.security.auth.Subject) YarnSchedulerResponse(org.apache.ranger.services.yarn.client.json.model.YarnSchedulerResponse) PrivilegedAction(java.security.PrivilegedAction) ArrayList(java.util.ArrayList) List(java.util.List) BaseClient(org.apache.ranger.plugin.client.BaseClient) Client(com.sun.jersey.api.client.Client)

Aggregations

Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 Client (com.sun.jersey.api.client.Client)1 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 WebResource (com.sun.jersey.api.client.WebResource)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Callable (java.util.concurrent.Callable)1 Subject (javax.security.auth.Subject)1 BaseClient (org.apache.ranger.plugin.client.BaseClient)1 HadoopException (org.apache.ranger.plugin.client.HadoopException)1 YarnSchedulerResponse (org.apache.ranger.services.yarn.client.json.model.YarnSchedulerResponse)1