[pve-devel] [PATCH ha-manager] replace can_fork with get_max_workers

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Feb 10 11:58:24 CET 2016


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 <t.lamprecht at proxmox.com>
---
 src/PVE/HA/Env.pm         | 15 ++++++++-------
 src/PVE/HA/Env/PVE2.pm    | 14 ++++++++------
 src/PVE/HA/LRM.pm         | 11 +++++++----
 src/PVE/HA/Sim/Env.pm     | 13 +++++++------
 src/PVE/HA/Sim/TestEnv.pm | 13 +++++++------
 5 files changed, 37 insertions(+), 29 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 {
-    my ($self) = @_;
-
-    return $self->{plug}->can_fork();
-}
-
 sub after_fork {
     my ($self) = @_;
 
     return $self->{plug}->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}->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 2692ca8..f53f26d 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 {
-    my ($self) = @_;
-
-    return 1;
-}
-
 sub after_fork {
     my ($self) = @_;
 
     # nothing to clean up in the simulation environment
 }
 
+
+sub get_max_workers {
+    my ($self) = @_;
+
+    return 4;
+}
+
 1;
diff --git a/src/PVE/HA/Sim/TestEnv.pm b/src/PVE/HA/Sim/TestEnv.pm
index 3f92fb0..ceec2a5 100644
--- a/src/PVE/HA/Sim/TestEnv.pm
+++ b/src/PVE/HA/Sim/TestEnv.pm
@@ -110,12 +110,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) = @_;
 
@@ -127,4 +121,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.1.4





More information about the pve-devel mailing list