Skip to content

Commit

Permalink
Change cpu threshold to a percent
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonin committed Jun 4, 2020
1 parent 01011e7 commit 07c5295
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
7 changes: 5 additions & 2 deletions app/models/miq_worker/container_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ def zone_selector

def resource_constraints
mem_threshold = self.class.worker_settings[:memory_threshold]
cpu_threshold = self.class.worker_settings[:cpu_threshold]
cpu_threshold = self.class.worker_settings[:cpu_threshold_percent]

return {} if !Settings.server.worker_monitor.enforce_resource_constraints || (mem_threshold.nil? && cpu_threshold.nil?)

{:limits => {}}.tap do |h|
h[:limits][:memory] = "#{mem_threshold / 1.megabyte}Mi" if mem_threshold
h[:limits][:cpu] = "#{cpu_threshold}m" if cpu_threshold
if cpu_threshold
millicores = ((cpu_threshold / 100.0) * 1000).to_i
h[:limits][:cpu] = "#{millicores}m"
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@
:worker_base:
:defaults:
:count: 1
:cpu_threshold: 500
:cpu_threshold_percent: 50
:gc_interval: 15.minutes
:heartbeat_freq: 10.seconds
:heartbeat_timeout: 2.minutes
Expand Down
8 changes: 4 additions & 4 deletions spec/models/miq_worker/container_common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def deployment_name_for(name)
end

it "returns the correct hash when both values are set" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:memory_threshold => 500.megabytes, :cpu_threshold => 500)
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:memory_threshold => 500.megabytes, :cpu_threshold_percent => 50)
constraints = {
:limits => {
:memory => "500Mi",
Expand All @@ -124,10 +124,10 @@ def deployment_name_for(name)
end

it "returns only cpu when cpu is set" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:cpu_threshold => 500)
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:cpu_threshold_percent => 80)
constraints = {
:limits => {
:cpu => "500m"
:cpu => "800m"
}
}
expect(MiqGenericWorker.new.resource_constraints).to eq(constraints)
Expand All @@ -138,7 +138,7 @@ def deployment_name_for(name)
before { stub_settings(:server => {:worker_monitor => {:enforce_resource_constraints => false}}) }

it "always returns an empty hash" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:memory_threshold => 500.megabytes, :cpu_threshold => 500)
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:memory_threshold => 500.megabytes, :cpu_threshold => 50)
expect(MiqGenericWorker.new.resource_constraints).to eq({})
end
end
Expand Down

0 comments on commit 07c5295

Please sign in to comment.