diff options
author | Michael Vogt <mvo@ubuntu.com> | 2016-03-17 08:56:58 +0100 |
---|---|---|
committer | Michael Vogt <mvo@debian.org> | 2016-04-01 13:02:39 +0200 |
commit | 14669d4b95f0f6a9b215d7fa5aebbc3b7198585d (patch) | |
tree | f22ee5dda27ea2a6fb3ee7bf07a553fbb5cd3d41 | |
parent | 6a4958d3134a3a61c036bc9ccaccc393c2bb99f2 (diff) | |
download | apt-14669d4b95f0f6a9b215d7fa5aebbc3b7198585d.tar.gz |
Use systemd.timer instead of a cron job
The rational is that we need to spread the load on the mirrors
that apt update and unattended-upgrades cause. To do so, we
leverage the RandomizeDelay feature of systemd. The other advantage
is that the timer is not run at a fixed daily.daily time but
instead every 24h. This also fixes the problem that the randomized
deplay in the current apt.cron.daily causes other cron jobs to
be deplayed.
A compatibility cron job is also provided for systems that do not
use systemd.
Note that the time is fired two times a day, but the logic inside
of apt.systemd.daily will ensure (via stamp files) that the
servers are hit at most every 24h. Firing two times a day helps
with the worst case update time and it also helps with systems
that are not always on.
LP: #246381, #727685
Closes: #600262, #709675, #663290
-rw-r--r-- | debian/apt-daily.service | 8 | ||||
-rw-r--r-- | debian/apt-daily.timer | 11 | ||||
-rw-r--r-- | debian/apt.apt-compat.cron.daily | 33 | ||||
-rw-r--r-- | debian/apt.dirs | 3 | ||||
-rw-r--r-- | debian/apt.install.in | 2 | ||||
-rw-r--r-- | debian/apt.maintscript | 3 | ||||
-rw-r--r-- | debian/apt.systemd.daily (renamed from debian/apt.cron.daily) | 35 | ||||
-rw-r--r-- | debian/control | 2 | ||||
-rwxr-xr-x | debian/rules | 8 |
9 files changed, 66 insertions, 39 deletions
diff --git a/debian/apt-daily.service b/debian/apt-daily.service new file mode 100644 index 000000000..941263d05 --- /dev/null +++ b/debian/apt-daily.service @@ -0,0 +1,8 @@ +[Unit] +Description=Daily apt activities +Documentation=man:apt(8) + +[Service] +Type=oneshot +ExecStart=/usr/lib/apt/apt.systemd.daily + diff --git a/debian/apt-daily.timer b/debian/apt-daily.timer new file mode 100644 index 000000000..037f00e24 --- /dev/null +++ b/debian/apt-daily.timer @@ -0,0 +1,11 @@ +[Unit] +Description=Daily apt activities + +[Timer] +OnCalendar=*-*-* 6:00,18:00 +RandomizedDelaySec=12h +AccuracySec=1h +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/debian/apt.apt-compat.cron.daily b/debian/apt.apt-compat.cron.daily new file mode 100644 index 000000000..1ea843008 --- /dev/null +++ b/debian/apt.apt-compat.cron.daily @@ -0,0 +1,33 @@ +#!/bin/sh + +set -e + +# Systemd systems use a systemd timer unit which is preferable to +# run. We want to randomize the apt update and unattended-upgrade +# runs as much as possible to avoid hitting the mirrors all at the +# same time. The systemd time is better at this than the fixed +# cron.daily time +if [ -d /run/systemd/system ]; then + exit 0 +fi + +# sleep for a random interval of time (default 30min) +# (some code taken from cron-apt, thanks) +random_sleep() +{ + RandomSleep=1800 + eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep) + if [ $RandomSleep -eq 0 ]; then + return + fi + if [ -z "$RANDOM" ] ; then + # A fix for shells that do not have this bash feature. + RANDOM=$(( $(dd if=/dev/urandom bs=2 count=1 2> /dev/null | cksum | cut -d' ' -f1) % 32767 )) + fi + TIME=$(($RANDOM % $RandomSleep)) + sleep $TIME +} + +# run daily job +random_sleep +exec /usr/lib/apt/apt.systemd.daily diff --git a/debian/apt.dirs b/debian/apt.dirs index e4bb2c824..4a83d1151 100644 --- a/debian/apt.dirs +++ b/debian/apt.dirs @@ -15,4 +15,5 @@ var/lib/apt/mirrors/partial var/lib/apt/periodic var/log/apt usr/share/bug/apt -usr/share/bash-completion/completions/
\ No newline at end of file +usr/share/bash-completion/completions/ +lib/systemd/system/
\ No newline at end of file diff --git a/debian/apt.install.in b/debian/apt.install.in index 4b7330340..f390c87aa 100644 --- a/debian/apt.install.in +++ b/debian/apt.install.in @@ -5,3 +5,5 @@ scripts/dselect/* usr/lib/dpkg/methods/apt/ usr/share/locale/*/*/apt.mo bin/libapt-private.so.* usr/lib/@DEB_HOST_MULTIARCH@/ ../completions/bash/* /usr/share/bash-completion/completions/ +../debian/*.service /lib/systemd/system/ +../debian/*.timer /lib/systemd/system/
\ No newline at end of file diff --git a/debian/apt.maintscript b/debian/apt.maintscript index 2a1e8819f..f461e673a 100644 --- a/debian/apt.maintscript +++ b/debian/apt.maintscript @@ -1,3 +1,4 @@ rm_conffile /etc/apt/apt.conf.d/20changelog 1.2.4~ - +# we use a systemd timer unit now +rm_conffile /etc/cron.daily/apt 1.2.10~ diff --git a/debian/apt.cron.daily b/debian/apt.systemd.daily index 7e976a087..ee772cc80 100644 --- a/debian/apt.cron.daily +++ b/debian/apt.systemd.daily @@ -72,18 +72,6 @@ # 2: + command outputs (remove -qq, remove 2>/dev/null, add -d) # 3: + trace on # -# APT::Periodic::RandomSleep "1800"; -# - The apt cron job will delay its execution by a random -# time span between zero and 'APT::Periodic::RandomSleep' -# seconds. -# This is done because otherwise everyone would access the -# mirror servers at the same time and put them collectively -# under very high strain. -# You can set this to '0' if you are using a local mirror and -# do not care about the load spikes. -# Note that sleeping in the apt job will be delaying the -# execution of all subsequent cron.daily jobs. -# check_stamp() { @@ -294,25 +282,6 @@ do_cache_backup() fi } -# sleep for a random interval of time (default 30min) -# (some code taken from cron-apt, thanks) -random_sleep() -{ - RandomSleep=1800 - eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep) - if [ $RandomSleep -eq 0 ]; then - return - fi - if [ -z "$RANDOM" ] ; then - # A fix for shells that do not have this bash feature. - RANDOM=$(( $(dd if=/dev/urandom bs=2 count=1 2> /dev/null | cksum | cut -d' ' -f1) % 32767 )) - fi - TIME=$(($RANDOM % $RandomSleep)) - debug_echo "sleeping for $TIME seconds" - sleep $TIME -} - - debug_echo() { # Display message if $VERBOSE >= 1 @@ -440,9 +409,7 @@ fi # deal with BackupArchiveInterval do_cache_backup $BackupArchiveInterval -# sleep random amount of time to avoid hitting the -# mirrors at the same time -random_sleep +# ensure we don't do this on battery check_power || exit 0 # include default system language so that "apt-get update" will diff --git a/debian/control b/debian/control index bfcfb26c2..319aea118 100644 --- a/debian/control +++ b/debian/control @@ -9,7 +9,7 @@ Build-Depends: dpkg-dev (>= 1.17.14), debhelper (>= 9.20141010), libdb-dev, gettext (>= 0.12), libcurl4-gnutls-dev (>= 7.19.4~), zlib1g-dev, libbz2-dev, liblzma-dev, liblz4-dev (>= 0.0~r126), xsltproc, docbook-xsl, docbook-xml, po4a (>= 0.34-2), - autotools-dev, autoconf, automake, libgtest-dev <!nocheck> + autotools-dev, autoconf, automake, libgtest-dev <!nocheck>, dh-systemd Build-Depends-Indep: doxygen, w3m, graphviz Build-Conflicts: autoconf2.13, automake1.4 Vcs-Git: git://anonscm.debian.org/apt/apt.git diff --git a/debian/rules b/debian/rules index ce7d19dec..7b7df7ad9 100755 --- a/debian/rules +++ b/debian/rules @@ -222,13 +222,17 @@ apt: build-binary build-manpages debian/apt.install # debian/$@/usr/lib/apt/apt-report-mirror-failure \ # move the apt-helper in place - mv debian/$@/usr/bin/apt-helper debian/$@/usr/lib/apt/apt-helper + mv debian/$@/usr/bin/apt-helper debian/$@/usr/lib/apt/apt-helper + # install apt.systemd.daily helper in the right place + install -m755 debian/apt.systemd.daily debian/$@/usr/lib/apt/ dh_bugfiles -p$@ dh_lintian -p$@ dh_installexamples -p$@ $(BLD)/docs/examples/* dh_installman -p$@ $(wildcard $(patsubst %,doc/en/%.[158],$(apt_MANPAGES)) $(patsubst %,doc/*/%.*.[158],$(apt_MANPAGES))) - dh_installcron -p$@ + dh_installcron -p$@ --name=apt-compat + dh_systemd_enable -p$@ apt-daily.timer + dh_systemd_start -p$@ apt-daily.timer dh_installdocs -p$@ dh_installchangelogs -p$@ dh_installlogrotate -p$@ |