Add sleep in cinder cron jobs

The current cron job for purge is executed at the precisely same time,
if we deploy multiple controller nodes with database clusterd,
for example using Galera cluster, it could cause DB spike.

This patch introduces a randomized sleep before executing purge job,
to avoid such kind of collision.

This commit is a kind of cherry-pick of the commit in puppet-nova
with change id I093a713a4f0ba48686de615aa8cb22b17a56917b .

Co-Authored-By: Rajesh Tailor <ratailor@redhat.com>

Change-Id: I36cf5f1e66fed580786970d9a953d983539a6e43
(cherry picked from commit I36cf5f1e66fed580786970d9a953d983539a6e43)
This commit is contained in:
Takashi Kajinami 2019-03-07 10:38:54 +09:00
parent d495287b74
commit d1e0e17353
2 changed files with 54 additions and 15 deletions

View File

@ -51,6 +51,12 @@
# (optional) Path to file to which rows should be archived
# Defaults to '/var/log/cinder/cinder-rowsflush.log'.
#
# [*maxdelay*]
# (optional) In Seconds. Should be a positive integer.
# Induces a random delay before running the cronjob to avoid running
# all cron jobs at the same time on all hosts this job is configured.
# Defaults to 0.
#
class cinder::cron::db_purge (
$minute = 1,
$hour = 0,
@ -59,13 +65,20 @@ class cinder::cron::db_purge (
$weekday = '*',
$user = 'cinder',
$age = 30,
$destination = '/var/log/cinder/cinder-rowsflush.log'
$destination = '/var/log/cinder/cinder-rowsflush.log',
$maxdelay = 0
) {
include ::cinder::deps
if $maxdelay == 0 {
$sleep = ''
} else {
$sleep = "sleep `expr \${RANDOM} \\% ${maxdelay}`; "
}
cron { 'cinder-manage db purge':
command => "cinder-manage db purge ${age} >>${destination} 2>&1",
command => "${sleep}cinder-manage db purge ${age} >>${destination} 2>&1",
environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
user => $user,
minute => $minute,

View File

@ -14,20 +14,46 @@ describe 'cinder::cron::db_purge' do
:weekday => '*',
:user => 'cinder',
:age => '30',
:destination => '/var/log/cinder/cinder-rowsflush.log' }
:maxdelay => 0,
:destination => '/var/log/cinder/cinder-rowsflush.log'
}
end
it 'configures a cron' do
is_expected.to contain_cron('cinder-manage db purge').with(
:command => "cinder-manage db purge #{params[:age]} >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user],
:minute => params[:minute],
:hour => params[:hour],
:monthday => params[:monthday],
:month => params[:month],
:weekday => params[:weekday],
:require => 'Anchor[cinder::install::end]'
)
context 'with required parameters' do
it 'configures a cron' do
is_expected.to contain_cron('cinder-manage db purge').with(
:command => "cinder-manage db purge #{params[:age]} >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user],
:minute => params[:minute],
:hour => params[:hour],
:monthday => params[:monthday],
:month => params[:month],
:weekday => params[:weekday],
:require => 'Anchor[cinder::install::end]'
)
end
end
context 'with required parameters with max delay enabled' do
before :each do
params.merge!(
:maxdelay => 600
)
end
it 'configures a cron with maxdelay' do
is_expected.to contain_cron('cinder-manage db purge').with(
:command => "sleep `expr ${RANDOM} \\% #{params[:maxdelay]}`; cinder-manage db purge #{params[:age]} >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user],
:minute => params[:minute],
:hour => params[:hour],
:monthday => params[:monthday],
:month => params[:month],
:weekday => params[:weekday],
:require => 'Anchor[cinder::install::end]'
)
end
end
end