[pve-devel] [PATCH ha-manager 04/10] Status: factor out new service state calculation

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Dec 21 16:44:41 CET 2016


Factor out the new "fast feedback for user" service state calculation
and use it also in the HA Simulator to provide the same feedback as
in the real world.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/API2/HA/Status.pm    | 53 ++------------------------------------------
 src/PVE/HA/Sim/RTHardware.pm |  8 ++++---
 src/PVE/HA/Tools.pm          | 49 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 54 deletions(-)

diff --git a/src/PVE/API2/HA/Status.pm b/src/PVE/API2/HA/Status.pm
index dbf23d5..344588b 100644
--- a/src/PVE/API2/HA/Status.pm
+++ b/src/PVE/API2/HA/Status.pm
@@ -146,66 +146,17 @@ __PACKAGE__->register_method ({
 	    }
 	}
 
-	my $add_service = sub {
-	    my ($sid, $sc, $ss) = @_;
-
-	    my $data = { id => "service:$sid", type => 'service', sid => $sid };
-
-	    if ($ss) {
-		my $req = $sc->{state} || 'ignore';
-		my $cur = $ss->{state};
-		my $state = $cur;
-
-		# give fast feedback to the user
-		if ($cur eq 'stopped') {
-		    if ($req eq 'started') {
-			$state = 'starting';
-		    } elsif ($req eq 'disabled') {
-			$state = 'disabled';
-		    }
-		} elsif ($cur eq 'started') {
-		    if ($req eq 'stopped' || $req eq 'disabled') {
-			$state = 'stopping';
-		    }
-		    $state = 'starting' if !$ss->{running};
-		} elsif ($cur eq 'error') {
-		    if ($req eq 'disabled') {
-			$state = 'clearing error flag';
-		    }
-		}
-
-		$data->{node} = $ss->{node};
-		$data->{status} =  "$sid ($ss->{node}, $state)"; # backward compatibility
-		$data->{state} = $state;
-		$data->{crm_state} = $ss->{state};
-	    } else {
-		$data->{node} = $sc->{node};
-		$data->{state} = 'queued';
-		$data->{status} = "$sid ($sc->{node}, queued)"; # backward compatibility
-	    }
-
-	    # also return common resource attributes
-	    if (defined($sc)) {
-		$data->{request_state} = $sc->{state};
-		foreach my $key (qw(group max_restart max_relocate comment)) {
-		    $data->{$key} = $sc->{$key} if defined($sc->{$key});
-		}
-	    }
-
-	    push @$res, $data;
-	};
-
 	foreach my $sid (sort keys %{$status->{service_status}}) {
 	    my $sc = $service_config->{$sid};
 	    my $ss = $status->{service_status}->{$sid};
-	    $add_service->($sid, $sc, $ss);
+	    push @$res, PVE::HA::Tools::get_current_service_state($sid, $sc, $ss);
 	}
 
 	# show also service which aren't yet processed by the CRM
 	foreach my $sid (sort keys %$service_config) {
 	    next if $status->{service_status}->{$sid};
 	    my $sc = $service_config->{$sid};
-	    $add_service->($sid, $sc);
+	    push @$res, PVE::HA::Tools::get_current_service_state($sid, $sc);
 	}
 
 	return $res;
diff --git a/src/PVE/HA/Sim/RTHardware.pm b/src/PVE/HA/Sim/RTHardware.pm
index 5496fb1..f30a14a 100644
--- a/src/PVE/HA/Sim/RTHardware.pm
+++ b/src/PVE/HA/Sim/RTHardware.pm
@@ -607,16 +607,18 @@ sub run {
 	foreach my $sid (@services) {
 	    my $sc = $self->{service_config}->{$sid};
 	    my $ss = $service_status->{$sid};
+
+	    my $sdata = PVE::HA::Tools::get_current_service_state($sid, $sc, $ss);
+
 	    my $sgui = $self->{service_gui}->{$sid};
 	    next if !$sgui;
 	    my $nl = $sgui->{node_label};
-	    $nl->set_text($sc->{node});
+	    $nl->set_text($sdata->{node});
 
 	    my $sl = $sgui->{status_label};
 	    next if !$sl;
 		
-	    my $text = ($ss && $ss->{state}) ? $ss->{state} : '-';
-	    $sl->set_text($text);
+	    $sl->set_text($sdata->{state});
 	}
 
 	if (my $sv = $self->{gui}->{stat_view}) { 
diff --git a/src/PVE/HA/Tools.pm b/src/PVE/HA/Tools.pm
index dca7daf..395f133 100644
--- a/src/PVE/HA/Tools.pm
+++ b/src/PVE/HA/Tools.pm
@@ -183,6 +183,55 @@ sub count_fenced_services {
     return $count;
 }
 
+sub get_current_service_state {
+    my ($sid, $sc, $ss) = @_;
+
+    my $data = { id => "service:$sid", type => 'service', sid => $sid };
+
+    if ($ss) {
+	my $req = $sc->{state} || 'ignore';
+	my $cur = $ss->{state};
+	my $state = $cur;
+
+	# give fast feedback to the user
+	if ($cur eq 'stopped') {
+	    if ($req eq 'started') {
+		$state = 'starting';
+	    } elsif ($req eq 'disabled') {
+		$state = 'disabled';
+	    }
+	} elsif ($cur eq 'started') {
+	    if ($req eq 'stopped' || $req eq 'disabled') {
+		$state = 'stopping';
+	    }
+	    $state = 'starting' if !$ss->{running};
+	} elsif ($cur eq 'error') {
+	    if ($req eq 'disabled') {
+		$state = 'clearing error flag';
+	    }
+	}
+
+	$data->{node} = $ss->{node};
+	$data->{status} =  "$sid ($ss->{node}, $state)"; # backward compatibility
+	$data->{state} = $state;
+	$data->{crm_state} = $ss->{state};
+    } else {
+	$data->{node} = $sc->{node};
+	$data->{state} = 'queued';
+	$data->{status} = "$sid ($sc->{node}, queued)"; # backward compatibility
+    }
+
+    # also return common resource attributes
+    if (defined($sc)) {
+	$data->{request_state} = $sc->{state};
+	foreach my $key (qw(group max_restart max_relocate comment)) {
+	    $data->{$key} = $sc->{$key} if defined($sc->{$key});
+	}
+    }
+
+    return $data;
+}
+
 sub upid_wait {
     my ($upid, $haenv) = @_;
 
-- 
2.1.4





More information about the pve-devel mailing list