private void queryForResources()
      throws UnsupportedPropertyException,
      SystemException,
      NoSuchResourceException,
      NoSuchParentResourceException {

    Resource.Type resourceType    = getResourceDefinition().getType();
    Predicate     queryPredicate  = createPredicate(getKeyValueMap(), processUserPredicate(userPredicate));

    // must occur after processing user predicate and prior to creating request
    finalizeProperties();

    Request request = createRequest();

    // use linked hash sets so that we maintain insertion and traversal order
    // in the event that the resource provider already gave us a sorted set
    // back
    Set<Resource> resourceSet = new LinkedHashSet<>();
    Set<Resource> providerResourceSet = new LinkedHashSet<>();

    QueryResponse queryResponse = doQuery(resourceType, request, queryPredicate, true);

    // If there is a page request and the predicate does not contain properties
    // that need to be set
    if ((pageRequest != null || sortRequest != null ) &&
      !populateResourceRequired(resourceType)) {
      PageResponse pageResponse = clusterController.getPage(resourceType,
          queryResponse, request, queryPredicate, pageRequest, sortRequest);

      // build a new set
      for (Resource r : pageResponse.getIterable()) {
        resourceSet.add(r);
        providerResourceSet.add(r);
      }
    } else {
      resourceSet.addAll(queryResponse.getResources());
      providerResourceSet.addAll(queryResponse.getResources());
    }

    populatedQueryResults.put(null, new QueryResult(
      request, queryPredicate, userPredicate, getKeyValueMap(), new QueryResponseImpl(resourceSet)));

    queryResults.put(null, new QueryResult(
      request, queryPredicate, userPredicate, getKeyValueMap(), queryResponse));

    if (renderer.requiresPropertyProviderInput()) {
      clusterController.populateResources(resourceType, providerResourceSet, request, queryPredicate);
    }

    // Optimization:
    // Currently the steps executed when sub-resources are requested are:
    //   (1) Get *all* top-level resources
    //   (2) Populate all top-level resources
    //   (3) Query for and populate sub-resources of *all* top-level resources
    //   (4) Apply pagination and predicate on resources from above
    //
    // Though this works, it is very inefficient when either:
    //   (a) Predicate does not apply to sub-resources
    //   (b) Page request is present
    // It is inefficient because we needlessly populate sub-resources that might not get
    // used due to their top-level resources being filtered out by the predicate and paging
    //
    // The optimization is to apply the predicate and paging request on the top-level resources
    // directly if there are no sub-resources predicates.
    if ((pageRequest != null || userPredicate != null) && !hasSubResourcePredicate() && populateResourceRequired(resourceType)) {
      QueryResponse newResponse = new QueryResponseImpl(resourceSet, queryResponse.isSortedResponse(), queryResponse.isPagedResponse(),
          queryResponse.getTotalResourceCount());
      PageResponse pageResponse = clusterController.getPage(resourceType, newResponse, request, queryPredicate, pageRequest, sortRequest);
      // build a new set
      Set<Resource> newResourceSet = new LinkedHashSet<>();
      for (Resource r : pageResponse.getIterable()) {
        newResourceSet.add(r);
      }
      populatedQueryResults.put(null, new QueryResult(request, queryPredicate, userPredicate, getKeyValueMap(), new QueryResponseImpl(newResourceSet)));
    }

    queryForSubResources();
  }
Query the cluster controller for the top level resources.
ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java