From 73699894dcdd225d30985eaa040aa20a62562753 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Mon, 20 Jun 2022 20:12:13 +0900 Subject: [PATCH] Add sleep in cache cron jobs ... so that users can avoid executing the same cron command at the same time in multiple nodes. Technically this can be done without this change by setting a different cron job schedule in each node but it requires complicated parameter setting. Change-Id: I596879d5d46f23e54fe344d5bf93a57837a6d287 (cherry picked from commit 21c95657eff96c17e4a87c123589e01c2fbeb059) (cherry picked from commit 62ad4d6a7cc44d80b549d493270228610a6d1f1e) (cherry picked from commit 1913dd18d976d40cc9771b7ead307155831c0b1a) --- manifests/cache/cleaner.pp | 19 +++++++++++++++--- manifests/cache/pruner.pp | 20 +++++++++++++++---- .../cache-cron-maxdelay-bfd1585eaa15aeda.yaml | 7 +++++++ spec/classes/glance_cache_cleaner_spec.rb | 3 ++- spec/classes/glance_cache_pruner_spec.rb | 3 ++- 5 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 releasenotes/notes/cache-cron-maxdelay-bfd1585eaa15aeda.yaml diff --git a/manifests/cache/cleaner.pp b/manifests/cache/cleaner.pp index 64a70d08..7a676b2f 100644 --- a/manifests/cache/cleaner.pp +++ b/manifests/cache/cleaner.pp @@ -20,9 +20,15 @@ # (optional) Defaults to '*'. # # [*command_options*] -# command options to add to the cronjob +# (optional) command options to add to the cronjob # (eg. point to config file, or redirect output) -# (optional) Defaults to ''. +# Defaults to ''. +# +# [*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 glance::cache::cleaner( $minute = 1, @@ -31,13 +37,20 @@ class glance::cache::cleaner( $month = '*', $weekday = '*', $command_options = '', + $maxdelay = 0 ) { include glance::deps include glance::params + if $maxdelay == 0 { + $sleep = '' + } else { + $sleep = "sleep `expr \${RANDOM} \\% ${maxdelay}`; " + } + cron { 'glance-cache-cleaner': - command => "${glance::params::cache_cleaner_command} ${command_options}", + command => "${sleep}${glance::params::cache_cleaner_command} ${command_options}", environment => 'PATH=/bin:/usr/bin:/usr/sbin', user => 'glance', minute => $minute, diff --git a/manifests/cache/pruner.pp b/manifests/cache/pruner.pp index 7766c707..28edada5 100644 --- a/manifests/cache/pruner.pp +++ b/manifests/cache/pruner.pp @@ -20,9 +20,15 @@ # (optional) Defaults to '*'. # # [*command_options*] -# command options to add to the cronjob +# (optional) command options to add to the cronjob # (eg. point to config file, or redirect output) -# (optional) Defaults to ''. +# Defaults to ''. +# +# [*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 glance::cache::pruner( $minute = '*/30', @@ -31,13 +37,20 @@ class glance::cache::pruner( $month = '*', $weekday = '*', $command_options = '', + $maxdelay = 0 ) { include glance::deps include glance::params + if $maxdelay == 0 { + $sleep = '' + } else { + $sleep = "sleep `expr \${RANDOM} \\% ${maxdelay}`; " + } + cron { 'glance-cache-pruner': - command => "${glance::params::cache_pruner_command} ${command_options}", + command => "${sleep}${glance::params::cache_pruner_command} ${command_options}", environment => 'PATH=/bin:/usr/bin:/usr/sbin', user => 'glance', minute => $minute, @@ -46,6 +59,5 @@ class glance::cache::pruner( month => $month, weekday => $weekday, require => Anchor['glance::config::end'], - } } diff --git a/releasenotes/notes/cache-cron-maxdelay-bfd1585eaa15aeda.yaml b/releasenotes/notes/cache-cron-maxdelay-bfd1585eaa15aeda.yaml new file mode 100644 index 00000000..76ec8cc6 --- /dev/null +++ b/releasenotes/notes/cache-cron-maxdelay-bfd1585eaa15aeda.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + The ``glance::cache::cleaner`` class and the ``glance::cache::pruner`` + class now support the ``maxdelay`` parameter. This parameter would be + useful in a multi-node deployment, to avoid launching the same cron command + at the same time in multiple nodes. diff --git a/spec/classes/glance_cache_cleaner_spec.rb b/spec/classes/glance_cache_cleaner_spec.rb index 04c8d0f1..47604b4f 100644 --- a/spec/classes/glance_cache_cleaner_spec.rb +++ b/spec/classes/glance_cache_cleaner_spec.rb @@ -33,11 +33,12 @@ describe 'glance::cache::cleaner' do :month => '2', :weekday => '3', :command_options => '--config-dir /etc/glance/', + :maxdelay => 3600, } end it 'configures a cron' do is_expected.to contain_cron('glance-cache-cleaner').with( - :command => 'glance-cache-cleaner --config-dir /etc/glance/', + :command => 'sleep `expr ${RANDOM} \\% 3600`; glance-cache-cleaner --config-dir /etc/glance/', :environment => 'PATH=/bin:/usr/bin:/usr/sbin', :user => 'glance', :minute => 59, diff --git a/spec/classes/glance_cache_pruner_spec.rb b/spec/classes/glance_cache_pruner_spec.rb index 350e0f7f..3bcd5985 100644 --- a/spec/classes/glance_cache_pruner_spec.rb +++ b/spec/classes/glance_cache_pruner_spec.rb @@ -32,11 +32,12 @@ describe 'glance::cache::pruner' do :month => '2', :weekday => '3', :command_options => '--config-dir /etc/glance/', + :maxdelay => 3600, } end it 'configures a cron' do is_expected.to contain_cron('glance-cache-pruner').with( - :command => 'glance-cache-pruner --config-dir /etc/glance/', + :command => 'sleep `expr ${RANDOM} \\% 3600`; glance-cache-pruner --config-dir /etc/glance/', :environment => 'PATH=/bin:/usr/bin:/usr/sbin', :user => 'glance', :minute => 59,