[pve-devel] [PATCH ha-manager 03/15] usage: add get_service_node and pin_service_node methods

Daniel Kral d.kral at proxmox.com
Tue Mar 25 16:12:42 CET 2025


Add methods get_service_node() and pin_service_node() to the Usage class
to retrieve and pin the current node of a specific service.

This is used to retrieve the current node of a service for colocation
rules inside of select_service_node(), where there is currently no
access to the global services state.

Signed-off-by: Daniel Kral <d.kral at proxmox.com>
---
For me this is more of a temporary change, since I don't think putting
this information here is very useful in the future. It was more of a
workaround for the moment, since `select_service_node()` doesn't have
access to the global service configuration data, which is needed here.

I would like to give `select_service_node()` the information from e.g.
$sc directly post-RFC.

 src/PVE/HA/Usage.pm        | 12 ++++++++++++
 src/PVE/HA/Usage/Basic.pm  | 15 +++++++++++++++
 src/PVE/HA/Usage/Static.pm | 14 ++++++++++++++
 3 files changed, 41 insertions(+)

diff --git a/src/PVE/HA/Usage.pm b/src/PVE/HA/Usage.pm
index 66d9572..e4f86d7 100644
--- a/src/PVE/HA/Usage.pm
+++ b/src/PVE/HA/Usage.pm
@@ -27,6 +27,18 @@ sub list_nodes {
     die "implement in subclass";
 }
 
+sub get_service_node {
+    my ($self, $sid) = @_;
+
+    die "implement in subclass";
+}
+
+sub pin_service_node {
+    my ($self, $sid, $node) = @_;
+
+    die "implement in subclass";
+}
+
 sub contains_node {
     my ($self, $nodename) = @_;
 
diff --git a/src/PVE/HA/Usage/Basic.pm b/src/PVE/HA/Usage/Basic.pm
index d6b3d6c..50d687b 100644
--- a/src/PVE/HA/Usage/Basic.pm
+++ b/src/PVE/HA/Usage/Basic.pm
@@ -10,6 +10,7 @@ sub new {
 
     return bless {
 	nodes => {},
+	services => {},
 	haenv => $haenv,
     }, $class;
 }
@@ -38,11 +39,25 @@ sub contains_node {
     return defined($self->{nodes}->{$nodename});
 }
 
+sub get_service_node {
+    my ($self, $sid) = @_;
+
+    return $self->{services}->{$sid};
+}
+
+sub pin_service_node {
+    my ($self, $sid, $node) = @_;
+
+    $self->{services}->{$sid} = $node;
+}
+
 sub add_service_usage_to_node {
     my ($self, $nodename, $sid, $service_node, $migration_target) = @_;
 
     if ($self->contains_node($nodename)) {
+	$self->{total}++;
 	$self->{nodes}->{$nodename}++;
+	$self->{services}->{$sid} = $nodename;
     } else {
 	$self->{haenv}->log(
 	    'warning',
diff --git a/src/PVE/HA/Usage/Static.pm b/src/PVE/HA/Usage/Static.pm
index 3d0af3a..8db9202 100644
--- a/src/PVE/HA/Usage/Static.pm
+++ b/src/PVE/HA/Usage/Static.pm
@@ -22,6 +22,7 @@ sub new {
 	'service-stats' => {},
 	haenv => $haenv,
 	scheduler => $scheduler,
+	'service-nodes' => {},
 	'service-counts' => {}, # Service count on each node. Fallback if scoring calculation fails.
     }, $class;
 }
@@ -85,9 +86,22 @@ my sub get_service_usage {
     return $service_stats;
 }
 
+sub get_service_node {
+    my ($self, $sid) = @_;
+
+    return $self->{'service-nodes'}->{$sid};
+}
+
+sub pin_service_node {
+    my ($self, $sid, $node) = @_;
+
+    $self->{'service-nodes'}->{$sid} = $node;
+}
+
 sub add_service_usage_to_node {
     my ($self, $nodename, $sid, $service_node, $migration_target) = @_;
 
+    $self->{'service-nodes'}->{$sid} = $nodename;
     $self->{'service-counts'}->{$nodename}++;
 
     eval {
-- 
2.39.5





More information about the pve-devel mailing list