[pve-devel] [RFC manager 1/1] WIP: replace systemd timer with pvesrd daemon

Thomas Lamprecht t.lamprecht at proxmox.com
Tue Apr 3 16:02:18 CEST 2018


The whole thing is already prepared for this, the systemd timer was
just a fixed periodic timer with a frequency of one minute. And we
just introduced it as the assumption was made that less memory usage
would be generated with this approach, AFAIK.

But logging 4+ lines just about that the timer was started, even if
it does nothing, and that 24/7 is not to cheap and a bit annoying.

So in a first step add a simple daemon, which forks of a child for
running jobs once a minute.
This could be made still a bit more intelligent, i.e., look if we
have jobs tor run before forking - as forking is not the cheapest
syscall. Further, we could adapt the sleep interval to the next time
we actually need to run a job (and sending a SIGUSR to the daemon if
a job interval changes such, that this interval got narrower)

We try to sync running on minute-change boundaries at start, this
emulates systemd.timer behaviour, we had until now. Also user can
configure jobs on minute precision, so they probably expect that
those also start really close to a minute change event.
Could be adapted to resync during running, to factor in time drift.
But, as long as enough cpu cycles are available we run in correct
monotonic intervalls, so this isn't a must, IMO.

Another improvement could be locking a bit more fine grained, i.e.
not on a per-all-local-job-runs basis, but per-job (per-guest?)
basis, which would improve temporary starvement  of small
high-periodic jobs through big, less peridoci jobs.
We argued that it's the user fault if such situations arise, but they
can evolve over time without noticing, especially in compolexer
setups.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 PVE/Service/Makefile      |   2 +-
 PVE/Service/pvesrd.pm     | 102 ++++++++++++++++++++++++++++++++++++++++++++++
 bin/Makefile              |   6 ++-
 bin/init.d/Makefile       |   3 +-
 bin/init.d/pvesr.service  |   7 ----
 bin/init.d/pvesr.timer    |  12 ------
 bin/init.d/pvesrd.service |  16 ++++++++
 bin/pvesrd                |  20 +++++++++
 debian/postinst           |   3 +-
 9 files changed, 147 insertions(+), 24 deletions(-)
 create mode 100644 PVE/Service/pvesrd.pm
 delete mode 100644 bin/init.d/pvesr.service
 delete mode 100644 bin/init.d/pvesr.timer
 create mode 100644 bin/init.d/pvesrd.service
 create mode 100755 bin/pvesrd

diff --git a/PVE/Service/Makefile b/PVE/Service/Makefile
index fc1cdb14..64f6be0d 100644
--- a/PVE/Service/Makefile
+++ b/PVE/Service/Makefile
@@ -1,6 +1,6 @@
 include ../../defines.mk
 
-SOURCES=pvestatd.pm pveproxy.pm pvedaemon.pm spiceproxy.pm
+SOURCES=pvestatd.pm pveproxy.pm pvedaemon.pm spiceproxy.pm pvesrd.pm
 
 all:
 
diff --git a/PVE/Service/pvesrd.pm b/PVE/Service/pvesrd.pm
new file mode 100644
index 00000000..47030575
--- /dev/null
+++ b/PVE/Service/pvesrd.pm
@@ -0,0 +1,102 @@
+package PVE::Service::pvesrd;
+
+use strict;
+use warnings;
+
+use POSIX qw(WNOHANG);
+use PVE::SafeSyslog;
+use PVE::API2::Replication;
+
+use PVE::Daemon;
+use base qw(PVE::Daemon);
+
+my $cmdline = [$0, @ARGV];
+my %daemon_options = (stop_wait_time => 180, max_workers => 0);
+my $daemon = __PACKAGE__->new('pvesrd', $cmdline, %daemon_options);
+
+my $finish_jobs = sub {
+    my ($self) = @_;
+    foreach my $cpid (keys %{$self->{jobs}}) {
+	my $waitpid = waitpid($cpid, WNOHANG);
+	if (defined($waitpid) && ($waitpid == $cpid)) {
+	    delete ($self->{jobs}->{$cpid});
+	}
+    }
+};
+
+sub run {
+    my ($self) = @_;
+
+    my $jobs= {};
+    $self->{jobs} = $jobs;
+
+    my $old_sig_chld = $SIG{CHLD};
+    local $SIG{CHLD} = sub {
+	local ($@, $!, $?); # do not overwrite error vars
+	$finish_jobs->($self);
+	$old_sig_chld->(@_) if $old_sig_chld;
+    };
+
+    my $logfunc = sub { syslog('info', $_[0]) };
+
+    my $run_jobs = sub {
+	my $child = fork();
+	if (!defined($child)) {
+	    die "fork failed: $!\n";
+	} elsif ($child == 0) {
+	    $self->after_fork_cleanup();
+	    PVE::API2::Replication::run_jobs(undef, $logfunc, 0, 1);
+	    POSIX::_exit(0);
+	}
+
+	$jobs->{$child} = 1;
+    };
+
+    # try to run near minute boundaries, makes more sense to the user as he
+    # configures jobs with minute precision
+    my ($current_seconds) = localtime;
+    sleep(60 - $current_seconds) if (60 - $current_seconds >= 5);
+
+    for (;;) {
+	last if $self->{shutdown_request};
+
+	$run_jobs->();
+
+	my $sleep_time = 60;
+	my $slept = 0; # SIGCHLD interrupts sleep, so we need to keep track
+	while ($slept < $sleep_time) {
+	    last if $self->{shutdown_request};
+	    $slept += sleep($sleep_time - $slept);
+	}
+    }
+
+    # jobs have a lock timeout of 60s, wait a bit more for graceful termination
+    my $timeout = 0;
+    while (keys %$jobs > 0 && $timeout < 75) {
+	kill 'TERM', keys %$jobs;
+	$timeout += sleep(5);
+    }
+    # ensure the rest gets stopped
+    kill 'KILL', keys %$jobs if (keys %$jobs > 0);
+}
+
+sub shutdown {
+    my ($self) = @_;
+
+    syslog('info', 'got shutdown request, signal running jobs to stop');
+
+    kill 'TERM', keys %{$self->{jobs}};
+    $self->{shutdown_request} = 1;
+}
+
+$daemon->register_start_command();
+$daemon->register_stop_command();
+$daemon->register_status_command();
+
+our $cmddef = {
+    start => [ __PACKAGE__, 'start', []],
+    stop => [ __PACKAGE__, 'stop', []],
+    status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
+};
+
+1;
diff --git a/bin/Makefile b/bin/Makefile
index c0b3f11b..94ea5a14 100644
--- a/bin/Makefile
+++ b/bin/Makefile
@@ -8,7 +8,7 @@ export PERLLIB=..
 
 SUBDIRS = init.d test
 
-SERVICES = pvestatd pveproxy pvedaemon spiceproxy
+SERVICES = pvestatd pveproxy pvedaemon spiceproxy pvesrd
 CLITOOLS = vzdump pvesubscription pveceph pveam pvesr
 
 SCRIPTS =  			\
@@ -45,6 +45,10 @@ all: ${SERVICE_MANS} ${CLI_MANS} pvemailforward
 	podselect $* > $@.tmp
 	mv $@.tmp $@
 
+pvesrd.8:
+	# FIXME: add to doc-generator
+	echo ".TH pvesrd 8" > $@
+
 pveversion.1.pod: pveversion
 pveupgrade.1.pod: pveupgrade
 pvesh.1.pod: pvesh
diff --git a/bin/init.d/Makefile b/bin/init.d/Makefile
index f0fc7f9c..6a95f866 100644
--- a/bin/init.d/Makefile
+++ b/bin/init.d/Makefile
@@ -13,8 +13,7 @@ SERVICES=			\
 	pve-storage.target	\
 	pve-daily-update.service\
 	pve-daily-update.timer	\
-	pvesr.service		\
-	pvesr.timer
+	pvesrd.service
 
 .PHONY: install
 install: ${SERVICES}
diff --git a/bin/init.d/pvesr.service b/bin/init.d/pvesr.service
deleted file mode 100644
index e0c082af..00000000
--- a/bin/init.d/pvesr.service
+++ /dev/null
@@ -1,7 +0,0 @@
-[Unit]
-Description=Proxmox VE replication runner
-ConditionPathExists=/usr/bin/pvesr
-
-[Service]
-Type=oneshot
-ExecStart=/usr/bin/pvesr run --mail 1
diff --git a/bin/init.d/pvesr.timer b/bin/init.d/pvesr.timer
deleted file mode 100644
index 01d7b9c7..00000000
--- a/bin/init.d/pvesr.timer
+++ /dev/null
@@ -1,12 +0,0 @@
-[Unit]
-Description=Proxmox VE replication runner
-
-[Timer]
-AccuracySec=1
-RemainAfterElapse=no
-
-[Timer]
-OnCalendar=minutely
-
-[Install]
-WantedBy=timers.target
\ No newline at end of file
diff --git a/bin/init.d/pvesrd.service b/bin/init.d/pvesrd.service
new file mode 100644
index 00000000..0a2813e0
--- /dev/null
+++ b/bin/init.d/pvesrd.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=Proxmox VE replication runner
+ConditionPathExists=/usr/bin/pvesrd
+Wants=pve-cluster.service
+After=pve-cluster.service
+After=pve-storage.target
+
+[Service]
+ExecStart=/usr/bin/pvesrd start
+ExecStop=/usr/bin/pvesrd stop
+PIDFile=/var/run/pvesrd.pid
+KillMode=process
+Type=forking
+
+[Install]
+WantedBy=multi-user.target
diff --git a/bin/pvesrd b/bin/pvesrd
new file mode 100755
index 00000000..0a37a87b
--- /dev/null
+++ b/bin/pvesrd
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use PVE::Service::pvesrd;
+
+use PVE::RPCEnvironment;
+use PVE::SafeSyslog;
+
+$SIG{'__WARN__'} = sub {
+    my $err = $@;
+    my $t = $_[0];
+    chomp $t;
+    print STDERR "$t\n";
+    syslog('warning', "%s", $t);
+    $@ = $err;
+};
+
+PVE::Service::pvesrd->run_cli_handler();
diff --git a/debian/postinst b/debian/postinst
index f1153fb3..2f9a45da 100755
--- a/debian/postinst
+++ b/debian/postinst
@@ -22,6 +22,7 @@ case "$1" in
     deb-systemd-invoke reload-or-try-restart pvestatd.service
     deb-systemd-invoke reload-or-try-restart pveproxy.service
     deb-systemd-invoke reload-or-try-restart spiceproxy.service
+    deb-systemd-invoke reload-or-try-restart pvesrd.service
 
     exit 0;;
 
@@ -45,7 +46,7 @@ case "$1" in
 
     # same as dh_systemd_enable (code copied)
 
-    UNITS="pvedaemon.service pveproxy.service spiceproxy.service pvestatd.service pvebanner.service pvesr.timer pve-daily-update.timer"
+    UNITS="pvedaemon.service pveproxy.service spiceproxy.service pvestatd.service pvebanner.service pvesrd.service pve-daily-update.timer"
     NO_RESTART_UNITS="pvenetcommit.service pve-guests.service"
 
     for unit in ${UNITS} ${NO_RESTART_UNITS}; do
-- 
2.14.2





More information about the pve-devel mailing list