Search in sources :

Example 1 with ApplicationsRequestScope

use of org.apache.hadoop.yarn.api.protocolrecords.ApplicationsRequestScope in project hadoop by apache.

the class TestGetApplicationsRequest method testGetApplicationsRequest.

@Test
public void testGetApplicationsRequest() {
    GetApplicationsRequest request = GetApplicationsRequest.newInstance();
    EnumSet<YarnApplicationState> appStates = EnumSet.of(YarnApplicationState.ACCEPTED);
    request.setApplicationStates(appStates);
    Set<String> tags = new HashSet<String>();
    tags.add("tag1");
    request.setApplicationTags(tags);
    Set<String> types = new HashSet<String>();
    types.add("type1");
    request.setApplicationTypes(types);
    long startBegin = System.currentTimeMillis();
    long startEnd = System.currentTimeMillis() + 1;
    request.setStartRange(startBegin, startEnd);
    long finishBegin = System.currentTimeMillis() + 2;
    long finishEnd = System.currentTimeMillis() + 3;
    request.setFinishRange(finishBegin, finishEnd);
    long limit = 100L;
    request.setLimit(limit);
    Set<String> queues = new HashSet<String>();
    queues.add("queue1");
    request.setQueues(queues);
    Set<String> users = new HashSet<String>();
    users.add("user1");
    request.setUsers(users);
    ApplicationsRequestScope scope = ApplicationsRequestScope.ALL;
    request.setScope(scope);
    GetApplicationsRequest requestFromProto = new GetApplicationsRequestPBImpl(((GetApplicationsRequestPBImpl) request).getProto());
    // verify the whole record equals with original record
    Assert.assertEquals(requestFromProto, request);
    // verify all properties are the same as original request
    Assert.assertEquals("ApplicationStates from proto is not the same with original request", requestFromProto.getApplicationStates(), appStates);
    Assert.assertEquals("ApplicationTags from proto is not the same with original request", requestFromProto.getApplicationTags(), tags);
    Assert.assertEquals("ApplicationTypes from proto is not the same with original request", requestFromProto.getApplicationTypes(), types);
    Assert.assertEquals("StartRange from proto is not the same with original request", requestFromProto.getStartRange(), new LongRange(startBegin, startEnd));
    Assert.assertEquals("FinishRange from proto is not the same with original request", requestFromProto.getFinishRange(), new LongRange(finishBegin, finishEnd));
    Assert.assertEquals("Limit from proto is not the same with original request", requestFromProto.getLimit(), limit);
    Assert.assertEquals("Queues from proto is not the same with original request", requestFromProto.getQueues(), queues);
    Assert.assertEquals("Users from proto is not the same with original request", requestFromProto.getUsers(), users);
}
Also used : LongRange(org.apache.commons.lang.math.LongRange) ApplicationsRequestScope(org.apache.hadoop.yarn.api.protocolrecords.ApplicationsRequestScope) YarnApplicationState(org.apache.hadoop.yarn.api.records.YarnApplicationState) GetApplicationsRequestPBImpl(org.apache.hadoop.yarn.api.protocolrecords.impl.pb.GetApplicationsRequestPBImpl) GetApplicationsRequest(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with ApplicationsRequestScope

use of org.apache.hadoop.yarn.api.protocolrecords.ApplicationsRequestScope in project hadoop by apache.

the class ClientRMService method getApplications.

/**
   * Get applications matching the {@link GetApplicationsRequest}. If
   * caseSensitive is set to false, applicationTypes in
   * GetApplicationRequest are expected to be in all-lowercase
   */
@Private
public GetApplicationsResponse getApplications(GetApplicationsRequest request, boolean caseSensitive) throws YarnException {
    UserGroupInformation callerUGI;
    try {
        callerUGI = UserGroupInformation.getCurrentUser();
    } catch (IOException ie) {
        LOG.info("Error getting UGI ", ie);
        throw RPCUtil.getRemoteException(ie);
    }
    Set<String> applicationTypes = request.getApplicationTypes();
    EnumSet<YarnApplicationState> applicationStates = request.getApplicationStates();
    Set<String> users = request.getUsers();
    Set<String> queues = request.getQueues();
    Set<String> tags = request.getApplicationTags();
    long limit = request.getLimit();
    LongRange start = request.getStartRange();
    LongRange finish = request.getFinishRange();
    ApplicationsRequestScope scope = request.getScope();
    final Map<ApplicationId, RMApp> apps = rmContext.getRMApps();
    Iterator<RMApp> appsIter;
    // of those queues by asking the scheduler for the apps in those queues.
    if (queues != null && !queues.isEmpty()) {
        // Construct an iterator over apps in given queues
        // Collect list of lists to avoid copying all apps
        final List<List<ApplicationAttemptId>> queueAppLists = new ArrayList<List<ApplicationAttemptId>>();
        for (String queue : queues) {
            List<ApplicationAttemptId> appsInQueue = scheduler.getAppsInQueue(queue);
            if (appsInQueue != null && !appsInQueue.isEmpty()) {
                queueAppLists.add(appsInQueue);
            }
        }
        appsIter = new Iterator<RMApp>() {

            Iterator<List<ApplicationAttemptId>> appListIter = queueAppLists.iterator();

            Iterator<ApplicationAttemptId> schedAppsIter;

            @Override
            public boolean hasNext() {
                // current list hasNext or whether there are any remaining lists
                return (schedAppsIter != null && schedAppsIter.hasNext()) || appListIter.hasNext();
            }

            @Override
            public RMApp next() {
                if (schedAppsIter == null || !schedAppsIter.hasNext()) {
                    schedAppsIter = appListIter.next().iterator();
                }
                return apps.get(schedAppsIter.next().getApplicationId());
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Remove not supported");
            }
        };
    } else {
        appsIter = apps.values().iterator();
    }
    List<ApplicationReport> reports = new ArrayList<ApplicationReport>();
    while (appsIter.hasNext() && reports.size() < limit) {
        RMApp application = appsIter.next();
        // Check if current application falls under the specified scope
        if (scope == ApplicationsRequestScope.OWN && !callerUGI.getUserName().equals(application.getUser())) {
            continue;
        }
        if (applicationTypes != null && !applicationTypes.isEmpty()) {
            String appTypeToMatch = caseSensitive ? application.getApplicationType() : StringUtils.toLowerCase(application.getApplicationType());
            if (!applicationTypes.contains(appTypeToMatch)) {
                continue;
            }
        }
        if (applicationStates != null && !applicationStates.isEmpty()) {
            if (!applicationStates.contains(application.createApplicationState())) {
                continue;
            }
        }
        if (users != null && !users.isEmpty() && !users.contains(application.getUser())) {
            continue;
        }
        if (start != null && !start.containsLong(application.getStartTime())) {
            continue;
        }
        if (finish != null && !finish.containsLong(application.getFinishTime())) {
            continue;
        }
        if (tags != null && !tags.isEmpty()) {
            Set<String> appTags = application.getApplicationTags();
            if (appTags == null || appTags.isEmpty()) {
                continue;
            }
            boolean match = false;
            for (String tag : tags) {
                if (appTags.contains(tag)) {
                    match = true;
                    break;
                }
            }
            if (!match) {
                continue;
            }
        }
        // checkAccess can grab the scheduler lock so call it last
        boolean allowAccess = checkAccess(callerUGI, application.getUser(), ApplicationAccessType.VIEW_APP, application);
        if (scope == ApplicationsRequestScope.VIEWABLE && !allowAccess) {
            continue;
        }
        reports.add(application.createAndGetApplicationReport(callerUGI.getUserName(), allowAccess));
    }
    GetApplicationsResponse response = recordFactory.newRecordInstance(GetApplicationsResponse.class);
    response.setApplicationList(reports);
    return response;
}
Also used : RMApp(org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp) YarnApplicationState(org.apache.hadoop.yarn.api.records.YarnApplicationState) ArrayList(java.util.ArrayList) LongRange(org.apache.commons.lang.math.LongRange) ArrayList(java.util.ArrayList) List(java.util.List) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) ApplicationsRequestScope(org.apache.hadoop.yarn.api.protocolrecords.ApplicationsRequestScope) IOException(java.io.IOException) ApplicationAttemptId(org.apache.hadoop.yarn.api.records.ApplicationAttemptId) ApplicationReport(org.apache.hadoop.yarn.api.records.ApplicationReport) GetApplicationsResponse(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) Private(org.apache.hadoop.classification.InterfaceAudience.Private)

Aggregations

LongRange (org.apache.commons.lang.math.LongRange)2 ApplicationsRequestScope (org.apache.hadoop.yarn.api.protocolrecords.ApplicationsRequestScope)2 YarnApplicationState (org.apache.hadoop.yarn.api.records.YarnApplicationState)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Private (org.apache.hadoop.classification.InterfaceAudience.Private)1 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)1 GetApplicationsRequest (org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest)1 GetApplicationsResponse (org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse)1 GetApplicationsRequestPBImpl (org.apache.hadoop.yarn.api.protocolrecords.impl.pb.GetApplicationsRequestPBImpl)1 ApplicationAttemptId (org.apache.hadoop.yarn.api.records.ApplicationAttemptId)1 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)1 ApplicationReport (org.apache.hadoop.yarn.api.records.ApplicationReport)1 RMApp (org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp)1 Test (org.junit.Test)1