public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com, Dominik Csapak <d.csapak@proxmox.com>
Subject: Re: [pve-devel] [PATCH manager 2/3] pvescheduler: reworking child pid tracking
Date: Mon, 22 Nov 2021 09:46:22 +0100	[thread overview]
Message-ID: <59fb5326-3f84-6a83-acb2-f31a163ae8f2@proxmox.com> (raw)
In-Reply-To: <20211118132831.839774-2-d.csapak@proxmox.com>

On 18.11.21 14:28, Dominik Csapak wrote:
> previously, systemd timers were responsible for running replication jobs.
> those timers would not restart if the previous one is still running.
> 
> though trying again while it is running does no harm really, it spams
> the log with errors about not being able to acquire the correct lock
> 
> to fix this, we rework the handling of child processes such that we only
> start one per loop if there is currently none running. for that,
> introduce the types of forks we do and allow one child process per type
> (for now, we have 'jobs' and 'replication' as types)
> 
> Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
> ---
>   PVE/Service/pvescheduler.pm | 42 ++++++++++++++++++++++++-------------
>   1 file changed, 27 insertions(+), 15 deletions(-)
> 
> diff --git a/PVE/Service/pvescheduler.pm b/PVE/Service/pvescheduler.pm
> index d4f73702..466cc599 100755
> --- a/PVE/Service/pvescheduler.pm
> +++ b/PVE/Service/pvescheduler.pm
> @@ -17,12 +17,16 @@ my $cmdline = [$0, @ARGV];
>   my %daemon_options = (stop_wait_time => 180, max_workers => 0);
>   my $daemon = __PACKAGE__->new('pvescheduler', $cmdline, %daemon_options);
>   
> +my @types = qw(replication jobs);
> +
>   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});
> +    for my $type (@types) {
> +	if (my $cpid = $self->{jobs}->{$type}) {
> +	    my $waitpid = waitpid($cpid, WNOHANG);
> +	    if (defined($waitpid) && ($waitpid == $cpid)) {
> +		$self->{jobs}->{$type} = undef;
> +	    }
>   	}
>       }
>   };
> @@ -41,7 +45,11 @@ sub run {
>       };
>   
>       my $fork = sub {
> -	my ($sub) = @_;
> +	my ($type, $sub) = @_;
> +
> +	# don't fork again if the previous iteration still runs
> +	return if defined($self->{jobs}->{$type});
> +
>   	my $child = fork();
>   	if (!defined($child)) {
>   	    die "fork failed: $!\n";
> @@ -56,16 +64,16 @@ sub run {
>   	    POSIX::_exit(0);
>   	}
>   
> -	$jobs->{$child} = 1;
> +	$jobs->{$type} = $child;
>       };
>   
>       my $run_jobs = sub {
>   
> -	$fork->(sub {
> +	$fork->('replication', sub {
>   	    PVE::API2::Replication::run_jobs(undef, sub {}, 0, 1);
>   	});
>   
> -	$fork->(sub {
> +	$fork->('jobs', sub {
>   	    PVE::Jobs::run_jobs();
>   	});
>       };
> @@ -92,14 +100,16 @@ sub run {
>   	}
>       }
>   
> -    # jobs have a lock timeout of 60s, wait a bit more for graceful termination
> +    # replication 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);
> +    for my $type (@types) {
> +	while (defined($jobs->{$type}) && $timeout < 75) {
> +	    kill 'TERM', $jobs->{$type};
> +	    $timeout += sleep(5);
> +	}
> +	# ensure the rest gets stopped
> +	kill 'KILL', $jobs->{$type} if defined($jobs->{$type});
>       }
> -    # ensure the rest gets stopped
> -    kill 'KILL', keys %$jobs if (keys %$jobs > 0);
>   }

Nit: this changes the behavior a bit, as it can happen that the timeout 
is "used up" for one job type and all following ones only get the KILL 
singal. Because of the code below, each child still gets at least one 
TERM, so not a big deal.

>   
>   sub shutdown {
> @@ -107,7 +117,9 @@ sub shutdown {
>   
>       syslog('info', 'got shutdown request, signal running jobs to stop');
>   
> -    kill 'TERM', keys %{$self->{jobs}};
> +    for my $type (@types) {
> +	kill 'TERM', $self->{jobs}->{$type} if $self->{jobs}->{$type};
> +    }
>       $self->{shutdown_request} = 1;
>   }
>   
> 




  reply	other threads:[~2021-11-22  8:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-18 13:28 [pve-devel] [PATCH manager 1/3] pvescheduler: catch errors in forked childs Dominik Csapak
2021-11-18 13:28 ` [pve-devel] [PATCH manager 2/3] pvescheduler: reworking child pid tracking Dominik Csapak
2021-11-22  8:46   ` Fabian Ebner [this message]
2021-11-18 13:28 ` [pve-devel] [PATCH manager 3/3] pvescheduler: implement graceful reloading Dominik Csapak
2021-11-22 19:35 ` [pve-devel] applied-series: [PATCH manager 1/3] pvescheduler: catch errors in forked childs Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=59fb5326-3f84-6a83-acb2-f31a163ae8f2@proxmox.com \
    --to=f.ebner@proxmox.com \
    --cc=d.csapak@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal