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

Thomas Lamprecht t.lamprecht at proxmox.com
Thu Jan 12 15:51:59 CET 2017


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>
---
Changes since v1:
* only refactor out the fast feedback state logic and do not return
  to much unnecessary info 

 src/PVE/API2/HA/Status.pm    | 29 +++--------------------------
 src/PVE/HA/Sim/RTHardware.pm |  7 ++++---
 src/PVE/HA/Tools.pm          | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 29 deletions(-)

diff --git a/src/PVE/API2/HA/Status.pm b/src/PVE/API2/HA/Status.pm
index dbf23d5..d21c174 100644
--- a/src/PVE/API2/HA/Status.pm
+++ b/src/PVE/API2/HA/Status.pm
@@ -152,38 +152,15 @@ __PACKAGE__->register_method ({
 	    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
 	    }
 
+	    $data->{state} = PVE::HA::Tools::get_verbose_service_state($ss, $sc);
+	    $data->{status} = "$sid ($data->{node}, $data->{state})"; # backward compatibility
+
 	    # also return common resource attributes
 	    if (defined($sc)) {
 		$data->{request_state} = $sc->{state};
diff --git a/src/PVE/HA/Sim/RTHardware.pm b/src/PVE/HA/Sim/RTHardware.pm
index b3e80d6..0b29340 100644
--- a/src/PVE/HA/Sim/RTHardware.pm
+++ b/src/PVE/HA/Sim/RTHardware.pm
@@ -608,6 +608,7 @@ sub run {
 	foreach my $sid (@services) {
 	    my $sc = $self->{service_config}->{$sid};
 	    my $ss = $service_status->{$sid};
+
 	    my $sgui = $self->{service_gui}->{$sid};
 	    next if !$sgui;
 	    my $nl = $sgui->{node_label};
@@ -615,9 +616,9 @@ sub run {
 
 	    my $sl = $sgui->{status_label};
 	    next if !$sl;
-		
-	    my $text = ($ss && $ss->{state}) ? $ss->{state} : '-';
-	    $sl->set_text($text);
+
+	    my $service_state = PVE::HA::Tools::get_verbose_service_state($ss, $sc);
+	    $sl->set_text($service_state);
 	}
 
 	if (my $sv = $self->{gui}->{stat_view}) {
diff --git a/src/PVE/HA/Tools.pm b/src/PVE/HA/Tools.pm
index dca7daf..565c275 100644
--- a/src/PVE/HA/Tools.pm
+++ b/src/PVE/HA/Tools.pm
@@ -183,6 +183,40 @@ sub count_fenced_services {
     return $count;
 }
 
+sub get_verbose_service_state {
+    my ($service_state, $service_conf) = @_;
+
+    # service not yet processed by manager
+    return 'queued' if !defined($service_state);
+    my $cur = $service_state->{state};
+
+    return 'ignore' if !defined($service_conf) || !defined($service_conf->{state});
+    my $req = $service_conf->{state};
+
+    # give fast feedback to the user
+    my $state = $cur;
+    if (!defined($cur)) {
+	$state = 'queued';
+    } elsif ($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 !$service_state->{running};
+    } elsif ($cur eq 'error') {
+	if ($req eq 'disabled') {
+	    $state = 'clearing error flag';
+	}
+    }
+
+    return $state;
+}
+
 sub upid_wait {
     my ($upid, $haenv) = @_;
 
-- 
2.1.4





More information about the pve-devel mailing list