From a28fa3301e09a940cd6df26ed8150b3e9a1e62f5 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Wed, 10 Feb 2016 11:58:24 +0100 Subject: [PATCH] replace can_fork with get_max_workers This patch implements the use of the new max_workers setting from the datacenter.cfg. Adding a 'get_max_worker' method to the enviornment allows us to do that and to replace 'can_fork' with that method. can_fork isn't needed anymore as get_max_worker may simply return 0 to signal that the respective enviornment can not fork. Signed-off-by: Thomas Lamprecht --- src/PVE/HA/Env.pm | 11 ++++++----- src/PVE/HA/Env/PVE2.pm | 14 ++++++++------ src/PVE/HA/LRM.pm | 11 +++++++---- src/PVE/HA/Sim/Env.pm | 9 +++++---- src/PVE/HA/Sim/TestEnv.pm | 13 +++++++------ 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/PVE/HA/Env.pm b/src/PVE/HA/Env.pm index b5aba32..3d96a6e 100644 --- a/src/PVE/HA/Env.pm +++ b/src/PVE/HA/Env.pm @@ -205,17 +205,18 @@ sub watchdog_close { return $self->{plug}->watchdog_close($wfh); } -# hack to support regression tests -sub can_fork { +sub after_fork { my ($self) = @_; - return $self->{plug}->can_fork(); + return $self->{plug}->after_fork(); } -sub after_fork { +# maximal number of workers to fork, +# return 0 as a hack to support regression tests +sub get_max_workers { my ($self) = @_; - return $self->{plug}->after_fork(); + return $self->{plug}->get_max_workers(); } 1; diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm index e4b0535..7e2906d 100644 --- a/src/PVE/HA/Env/PVE2.pm +++ b/src/PVE/HA/Env/PVE2.pm @@ -389,12 +389,6 @@ sub watchdog_close { } } -sub can_fork { - my ($self) = @_; - - return 1; -} - sub after_fork { my ($self) = @_; @@ -405,4 +399,12 @@ sub after_fork { PVE::Cluster::cfs_update(); } +sub get_max_workers { + my ($self) = @_; + + my $datacenterconfig = cfs_read_file('datacenter.cfg'); + + return $datacenterconfig->{max_workers} || 4; +} + 1; diff --git a/src/PVE/HA/LRM.pm b/src/PVE/HA/LRM.pm index d7b54da..1f554fa 100644 --- a/src/PVE/HA/LRM.pm +++ b/src/PVE/HA/LRM.pm @@ -366,8 +366,8 @@ sub run_workers { my $starttime = $haenv->get_time(); - # start workers - my $max_workers = 4; + # number of workers to start, if 0 we exec the command directly witouth forking + my $max_workers = $haenv->get_max_workers(); my $sc = $haenv->read_service_config(); @@ -375,10 +375,13 @@ sub run_workers { my $count = $self->check_active_workers(); foreach my $sid (keys %{$self->{workers}}) { - last if $count >= $max_workers; + last if $count >= $max_workers && $max_workers > 0; + my $w = $self->{workers}->{$sid}; if (!$w->{pid}) { - if ($haenv->can_fork()) { + # only fork if we may else call exec_resource_agent + # directly (e.g. for regression tests) + if ($max_workers > 0) { my $pid = fork(); if (!defined($pid)) { $haenv->log('err', "fork worker failed"); diff --git a/src/PVE/HA/Sim/Env.pm b/src/PVE/HA/Sim/Env.pm index e154988..246ac60 100644 --- a/src/PVE/HA/Sim/Env.pm +++ b/src/PVE/HA/Sim/Env.pm @@ -308,16 +308,17 @@ sub watchdog_close { return $self->{hardware}->watchdog_close($wfh); } -sub can_fork { +sub after_fork { my ($self) = @_; - return 1; + # nothing to clean up in the simulation environment } -sub after_fork { + +sub get_max_workers { my ($self) = @_; - # nothing to clean up in the simulation environment + return 4; } 1; diff --git a/src/PVE/HA/Sim/TestEnv.pm b/src/PVE/HA/Sim/TestEnv.pm index 414c274..f4f70c9 100644 --- a/src/PVE/HA/Sim/TestEnv.pm +++ b/src/PVE/HA/Sim/TestEnv.pm @@ -109,12 +109,6 @@ sub loop_end_hook { $self->{cur_time} += 1; # easier for simulation } -sub can_fork { - my ($self) = @_; - - return 0; -} - sub is_node_shutdown { my ($self) = @_; @@ -126,4 +120,11 @@ sub is_node_shutdown { return defined($cstatus->{$node}->{shutdown}) ? 1 : 0; } +# must be 0 as we do not want to fork in the regression tests +sub get_max_workers { + my ($self) = @_; + + return 0; +} + 1; -- 2.39.2