Skip to content

Commit 9aa2275

Browse files
committed
(PUP-12061) Recalculate splay when splaylimit changes
If puppet.conf was modified, but splaylimit wasn't, then we recalculated the splay offset. This goes against the desired behavior of only splaying when the agent is initially started and running every runinterval seconds from that point forward or until runinterval/splaylimit are changed in the future. Now the SplayJob remembers its last splaylimit so that it only recalculates splay if the limit changes. It's important to recalculate splay so that the agent chooses a slot over the new range, either smaller or bigger than before.
1 parent 578261d commit 9aa2275

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

lib/puppet/daemon.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ def run_event_loop
182182

183183
reparse_run.disable if Puppet[:filetimeout] == 0
184184

185+
# these are added in a different order than they are defined
185186
@scheduler.run_loop([reparse_run, agent_run, signal_loop])
186187
end
187188
end

lib/puppet/scheduler/splay_job.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class SplayJob < Job
55
attr_reader :splay
66

77
def initialize(run_interval, splay_limit, &block)
8-
@splay = calculate_splay(splay_limit)
8+
@splay, @splay_limit = calculate_splay(splay_limit)
99
super(run_interval, &block)
1010
end
1111

@@ -31,13 +31,15 @@ def ready?(time)
3131
# @return @splay [Integer] a random integer less than or equal to the splay limit that represents the seconds to
3232
# delay before next agent run.
3333
def splay_limit=(splay_limit)
34-
@splay = calculate_splay(splay_limit)
34+
if @splay_limit != splay_limit
35+
@splay, @splay_limit = calculate_splay(splay_limit)
36+
end
3537
end
3638

3739
private
3840

3941
def calculate_splay(limit)
40-
rand(limit + 1)
42+
[rand(limit + 1), limit]
4143
end
4244
end
4345
end

spec/unit/daemon_spec.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ def run_loop(jobs)
8383
expect(reparse_run).not_to be_enabled
8484
end
8585

86+
it "does not splay the agent run by default" do
87+
daemon.start
88+
expect(agent_run).to be_an_instance_of(Puppet::Scheduler::Job)
89+
end
90+
8691
describe "and calculating splay" do
8792
before do
8893
# Set file timeout so the daemon reparses
@@ -91,12 +96,24 @@ def run_loop(jobs)
9196
end
9297

9398
it "recalculates when splaylimit changes" do
94-
allow(agent).to receive(:run)
9599
daemon.start
96-
first_splay = agent_run.splay
97-
allow(Kernel).to receive(:rand).and_return(1738)
100+
101+
Puppet[:splaylimit] = 60
102+
init_splay = agent_run.splay
103+
next_splay = init_splay + 1
104+
allow(agent_run).to receive(:rand).and_return(next_splay)
98105
reparse_run.run(Time.now)
99-
expect(agent_run.splay).to_not eq(first_splay)
106+
107+
expect(agent_run.splay).to eq(next_splay)
108+
end
109+
110+
it "does not change splay if splaylimit is unmodified" do
111+
daemon.start
112+
113+
init_splay = agent_run.splay
114+
reparse_run.run(Time.now)
115+
116+
expect(agent_run.splay).to eq(init_splay)
100117
end
101118
end
102119
end

spec/unit/scheduler/splay_job_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,17 @@
3131
expect(job).to receive(:splay).and_return(6)
3232
expect(job.ready?(start_time)).not_to be
3333
end
34+
35+
it "does not apply a splay if the splaylimit is unchanged" do
36+
old_splay = job.splay
37+
job.splay_limit = splay_limit
38+
expect(job.splay).to eq(old_splay)
39+
end
40+
41+
it "applies a splay if the splaylimit is changed" do
42+
new_splay = 999
43+
allow(job).to receive(:rand).and_return(new_splay)
44+
job.splay_limit = splay_limit + 1
45+
expect(job.splay).to eq(new_splay)
46+
end
3447
end

0 commit comments

Comments
 (0)