이 콘텐츠는 선택한 언어로 제공되지 않습니다.

8.5.2. Example Gear Placement Algorithms


The example gear placement algorithms in this section are provided as a reference to help with the development of your own custom algorithms. They are for use on brokers that have the gear placement plug-in installed and are intended for testing or development purposes.
After installing the gear placement plug-in, these examples are also installed on the broker host for convenience. Most of the following example algorithms are located in the Gem_Location/lib/openshift/ directory, with any related example initializers and configuration files located in the Gem_Location/config/initializers/ and /etc/openshift/plugins.d/ directories, respectively. See Section 8.5.1, “Developing and Implementing a Custom Gear Placement Algorithm” for information on implementing custom algorithms in your environment.
Administrator Constraint Examples

The following are administrator constraint example algorithms for the gear placement plug-in.

Example 8.8. Return the First Node in the List

def self.select_best_fit_node_impl(server_infos, app_props, current_gears, comp_list, user_props, request_time)
  return server_infos.first
end
Copy to Clipboard Toggle word wrap

Example 8.9. Place PHP Applications on Specific Nodes

def self.select_best_fit_node_impl(server_infos, app_props, current_gears, comp_list, user_props, request_time)
  unless %w[php-5.3 php-5.4].include? app_props.web_cartridge 
    Rails.logger.debug("'#{app_props.web_cartridge}' is not a PHP app; selecting a node normally.")
    return OpenShift::MCollectiveApplicationContainerProxy.select_best_fit_node_impl(server_infos)
  end
  php_hosts = Broker::Application.config.gear_placement[:php_hosts]
  Rails.logger.debug("selecting a php node from: #{php_hosts.join ', '}")
  # figure out which of the nodes given is allowed for php carts
  matched_server_infos = server_infos.select {|x| php_hosts.include?(x.name) }
  matched_server_infos.empty? and
    raise "The gear-placement PHP_HOSTS setting doesn't match any of the NodeProfile names"
  return matched_server_infos.sample #chooses randomly from the matched hosts
end
Copy to Clipboard Toggle word wrap
This example can also be found in the Gem_Location/lib/openshift/gear_placement_plugin.rb.pin-php-to-host-example file. However, to prevent scalable or highly-available applications from behaving unpredictably as a result of the server_infos filters mentioned in Section 8.5.1, “Developing and Implementing a Custom Gear Placement Algorithm”, use the VALID_GEAR_SIZES_FOR_CARTRIDGE parameter in the /etc/openshift/broker.conf file in conjunction with profiles.

Example 8.10. Restrict a User's Applications to Slow Hosts

def self.select_best_fit_node_impl(server_infos, app_props, current_gears, comp_list, user_props, request_time) 
  config = Broker::Application.config.gear_placement  
  pinned_user = config[:pinned_user]           
  if pinned_user == user_props.login
    slow_hosts = config[:slow_hosts]
    Rails.logger.debug("user '#{pinned_user}' needs a gear; restrict to '#{slow_hosts.join ', '}'")
    matched_server_infos = server_infos.select {|x| slow_hosts.include?(x.name)}
    matched_server_infos.empty? and
      raise "The gear-placement SLOW_HOSTS setting does not match any available NodeProfile names"
    return matched_server_infos.first
  else
    Rails.logger.debug("user '#{user_props.login}' is not pinned; choose a node normally")
    return OpenShift::MCollectiveApplicationContainerProxy.select_best_fit_node_impl(server_infos)
  end
end
Copy to Clipboard Toggle word wrap
This example can also be found in the Gem_Location/lib/openshift/gear_placement_plugin.rb.pin-user-to-host-example file. However, this could prevent the user from scaling applications in some situations as a result of the server_infos filters mentioned in Section 8.5.1, “Developing and Implementing a Custom Gear Placement Algorithm”.

Example 8.11. Ban a Specific Vendor's Cartridges

def self.select_best_fit_node_impl(server_infos, app_props, current_gears, comp_list, user_props, request_time) 
  Rails.logger.debug("Using blacklist gear placement plugin to choose node.")
  Rails.logger.debug("selecting from nodes: #{server_infos.map(:name).join ', '}")
  blacklisted_vendor = Broker::Application.config.gear_placement[:blacklisted_vendor]
  unless blacklisted_vendor.nil?    
    comp_list.each do |comp|
      if blacklisted_vendor == comp.cartridge_vendor      
        raise "Applications containing cartridges from #{blacklisted_vendor} are blacklisted"
      end
    end
  end
  Rails.logger.debug("no contraband found, choosing node as usual")
  return OpenShift::MCollectiveApplicationContainerProxy.select_best_fit_node_impl(server_infos)
end
Copy to Clipboard Toggle word wrap
This example can also be found in the Gem_Location/lib/openshift/gear_placement_plugin.rb.blacklisted-vendor-example file.
Resource Usage Examples

The following are resource usage example algorithms for the gear placement plug-in.

Example 8.12. Place a Gear on the Node with the Most Free Memory

def self.select_best_fit_node_impl(server_infos, app_props, current_gears, comp_list, user_props, request_time) 
  # collect memory statistic from all nodes
  memhash = Hash.new(0)
  OpenShift::MCollectiveApplicationContainerProxy.rpc_get_fact('memoryfree') {|name,mem| memhash[name] = to_bytes(mem)}
  Rails.logger.debug("node memory hash: #{memhash.inspect}")
  # choose the one from our list with the largest value
  return server_infos.max_by {|server| memhash[server.name]} 
end

def self.to_bytes(mem)
  mem.to_f * case mem
    when /TB/; 1024 ** 4
    when /GB/; 1024 ** 3
    when /MB/; 1024 ** 2
    when /KB/; 1024
    else     ; 1
  end
end
Copy to Clipboard Toggle word wrap
This example can also be found in the Gem_Location/lib/openshift/gear_placement_plugin.rb.free-memory-example file.

Example 8.13. Sort Nodes by Gear Usage (Round Robin)

def self.select_best_fit_node_impl(server_infos, app_props, current_gears, comp_list, user_props, request_time) 
  return server_infos.sort_by {|x| x.node_consumed_capacity.to_f}.first
end
Copy to Clipboard Toggle word wrap
This example can also be found in the Gem_Location/lib/openshift/gear_placement_plugin.rb.round-robin-example file. The nodes in each profile fill evenly, unless complications arise, for example due to scaled applications, gears being deleted unevenly, or MCollective fact updates trailing behind. Implementing true round robin requires writing out a state file owned by this algorithm and using that for scheduling the placement rotation.
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat