# File lib/openshift-origin-msg-broker-mcollective/lib/openshift/mcollective_application_container_proxy.rb, line 1289
      def self.rpc_find_available(node_profile=nil, district_uuid=nil, require_specific_district=false, forceRediscovery=false)
        current_server, current_capacity = nil, nil
        additional_filters = [{:fact => "active_capacity",
                               :value => '100',
                               :operator => "<"}]

        district_uuid = nil if district_uuid == 'NONE'

        if Rails.configuration.msg_broker[:node_profile_enabled]
          if node_profile
            additional_filters.push({:fact => "node_profile",
                                     :value => node_profile,
                                     :operator => "=="})
          end
        end

        if district_uuid
          additional_filters.push({:fact => "district_uuid",
                                   :value => district_uuid,
                                   :operator => "=="})
          additional_filters.push({:fact => "district_active",
                                   :value => true.to_s,
                                   :operator => "=="})
        else
          #TODO how do you filter on a fact not being set
          additional_filters.push({:fact => "district_uuid",
                                   :value => "NONE",
                                   :operator => "=="})

        end
        
        rpc_opts = nil
        unless forceRediscovery
          rpc_opts = rpc_options
          rpc_opts[:disctimeout] = 1
        end

        server_infos = []
        rpc_get_fact('active_capacity', nil, forceRediscovery, additional_filters, rpc_opts) do |server, capacity|
          #Rails.logger.debug "Next server: #{server} active capacity: #{capacity}"
          server_infos << [server, capacity.to_f]
        end

        if !server_infos.empty?
          # Pick a random node amongst the best choices available
          server_infos = server_infos.sort_by { |server_info| server_info[1] }
          if server_infos.first[1] < 80
            # If any server is < 80 then only pick from servers with < 80
            server_infos.delete_if { |server_info| server_info[1] >= 80 }
          end
          max_index = [server_infos.length, 4].min - 1
          server_infos = server_infos.first(max_index + 1)
          # Weight the servers with the most active_capacity the highest 
          (0..max_index).each do |i|
            (max_index - i).times do
              server_infos << server_infos[i]
            end
          end
        elsif district_uuid && !require_specific_district
          # Well that didn't go too well.  They wanted a district.  Probably the most available one.  
          # But it has no available nodes.  Falling back to a best available algorithm.  First
          # Find the most available nodes and match to their districts.  Take out the almost
          # full nodes if possible and return one of the nodes within a district with a lot of space. 
          additional_filters = [{:fact => "active_capacity",
                                 :value => '100',
                                 :operator => "<"},
                                {:fact => "district_active",
                                 :value => true.to_s,
                                 :operator => "=="},
                                {:fact => "district_uuid",
                                 :value => "NONE",
                                 :operator => "!="}]

          if Rails.configuration.msg_broker[:node_profile_enabled]
            if node_profile
              additional_filters.push({:fact => "node_profile",
                                       :value => node_profile,
                                       :operator => "=="})
            end
          end
          
          rpc_opts = nil
          unless forceRediscovery
            rpc_opts = rpc_options
            rpc_opts[:disctimeout] = 1
          end
          districts = District.find_all # candidate for caching
          rpc_get_fact('active_capacity', nil, forceRediscovery, additional_filters, rpc_opts) do |server, capacity|
            districts.each do |district|
              if district.server_identities.has_key?(server)
                server_infos << [server, capacity.to_f, district]
                break
              end
            end
          end
          unless server_infos.empty?
            server_infos = server_infos.sort_by { |server_info| server_info[1] }
            if server_infos.first[1] < 80
              server_infos.delete_if { |server_info| server_info[1] >= 80 }
            end
            server_infos = server_infos.sort_by { |server_info| server_info[2].available_capacity }
            server_infos = server_infos.first(8)
          end
        end
        current_district = nil
        unless server_infos.empty?
          server_info = server_infos[rand(server_infos.length)]
          current_server = server_info[0]
          current_capacity = server_info[1]
          current_district = server_info[2]
          Rails.logger.debug "Current server: #{current_server} active capacity: #{current_capacity}"
        end

        return current_server, current_capacity, current_district
      end