Skip to content

Commit 76a27ca

Browse files
committed
cache zone names for MiqQueue create
We are concerned that newly created zones will not get into the cache quickly enough. So this strategy is to lookup unknown zones every time. Since this is not common, deciding to taking a hit here.
1 parent 422e938 commit 76a27ca

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

app/models/miq_queue.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def self.lower_priority?(p1, p2)
105105
serialize :args, Array
106106
serialize :miq_callback, Hash
107107

108-
validates :zone, :inclusion => {:in => proc { Zone.in_my_region.pluck(:name) }}, :allow_nil => true
108+
validate :validate_zone_name
109109

110110
STATE_READY = 'ready'.freeze
111111
STATE_DEQUEUE = 'dequeue'.freeze
@@ -623,6 +623,16 @@ def self.display_name(number = 1)
623623

624624
private
625625

626+
# NOTE: this will intentionally lookup missing zones every time
627+
def validate_zone_name
628+
if zone && !self.class.valid_zone_names[zone]
629+
found = self.class.valid_zone_names[zone] = Zone.in_my_region.exists?(:name => zone)
630+
errors.add(:zone, N_("Unknown Zone")) unless found
631+
end
632+
end
633+
634+
cache_with_timeout(:valid_zone_names, 1.minute) { Hash.new }
635+
626636
def activate_miq_task(args)
627637
MiqTask.update_status(miq_task_id, MiqTask::STATE_ACTIVE, MiqTask::STATUS_OK, "Task starting") if miq_task_id
628638
params = args.first

spec/models/miq_queue_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,40 @@
44
let(:miq_server) { EvmSpecHelper.local_miq_server }
55
let(:zone) { miq_server.zone }
66

7+
context "#validate" do
8+
it "allows nil zones" do
9+
msg = FactoryBot.build(:miq_queue)
10+
expect(msg.zone).to be_nil
11+
expect(msg).to be_valid
12+
end
13+
14+
it "allows valid zone names" do
15+
msg = FactoryBot.build(:miq_queue, :zone => zone.name)
16+
expect(msg).to be_valid
17+
end
18+
19+
it "complains about unknown zone names" do
20+
msg = FactoryBot.build(:miq_queue, :zone => "#{zone.name}x")
21+
expect(msg).not_to be_valid
22+
end
23+
24+
it "only queries a single zone one time" do
25+
# cache the value
26+
msg = FactoryBot.build(:miq_queue, :zone => zone.name)
27+
expect(msg).to be_valid
28+
expect { expect(FactoryBot.build(:miq_queue, :zone => zone.name)).to be_valid }.not_to make_database_queries
29+
end
30+
31+
it "can find new zones" do
32+
# potentially load the cached
33+
msg = FactoryBot.build(:miq_queue, :zone => zone.name)
34+
expect(msg).to be_valid
35+
zone2 = FactoryBot.create(:zone)
36+
msg = FactoryBot.build(:miq_queue, :zone => zone2.name)
37+
expect(msg).to be_valid
38+
end
39+
end
40+
741
context "#deliver" do
842
before { miq_server }
943
it "requires class_name" do

0 commit comments

Comments
 (0)