[pve-devel] [PATCH v2 pve-manager 6/7] move code to seperate find_best_node_target sub

Alexandre Derumier aderumier at odiso.com
Wed Oct 9 16:01:36 CEST 2019


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/API2/Nodes.pm | 91 +++++++++++++++++++++++++----------------------
 1 file changed, 49 insertions(+), 42 deletions(-)

diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
index 93bf1bd2..25199249 100644
--- a/PVE/API2/Nodes.pm
+++ b/PVE/API2/Nodes.pm
@@ -1973,49 +1973,10 @@ __PACKAGE__->register_method ({
 	    my $workers = {};
 	    foreach my $vmid (sort keys %$vmlist) {
 		my $d = $vmlist->{$vmid};
+		$target = find_best_node_target($vmid, $d, $nodename, $storecfg) if !$target;
 		if(!$target) {
-
-		    my $vmconf = PVE::QemuConfig->load_config($vmid);
-		    my $members = PVE::Cluster::get_members();
-		    my $rrd = PVE::Cluster::rrd_dump();
-		    my $nodelist = PVE::Cluster::get_nodelist();
-		    my $mem_threshold = 0.8;
-		    my $cpu_threshold = 0.8;
-		    my $vm_stats = PVE::API2Tools::extract_vm_stats($vmid, $d, $rrd);
-		    my $vm_cpu = $vm_stats->{cpu} * $vm_stats->{maxcpu};
-		    my $vm_mem = $vm_stats->{mem};
-		    my @vec_vm = ($vm_cpu, $vm_mem);  #? add network usage dimension ?
-
-		    my $nodes_weight = {};
-		    my $highest_weight = 0;
-		    foreach my $node (@$nodelist) {
-			next if $node eq $nodename;
-
-			my $node_stats = PVE::API2Tools::extract_node_stats($node, $members, $rrd);
-			my $node_freemem = $node_stats->{maxmem} - $node_stats->{mem};
-			my $node_freecpu = (100 - $node_stats->{cpu}) * $node_stats->{maxcpu};  #how to handle different cpu model power ? bogomips ?
-			next if $node_stats->{status} ne 'online';
-			next if $node_freecpu * $cpu_threshold < $vm_cpu;
-			next if $node_freemem * $mem_threshold < $vm_mem;
-			next if $node_stats->{maxcpu} < $vm_stats->{maxcpu};
-			eval { PVE::QemuServer::check_storage_availability($storecfg, $vmconf, $node) };
-			next if $@;
-			# fixme: check vmbr available
-
-			#for target node ordering, take swap && ksm in account;
-			#$node_freemem = $node_freemem - $node_stats->{swapused} - $node_stats->{ksm_shared};
-
-			my @vec_node = ($node_freecpu, $node_freemem); #? add network usage dimension ?
-			my $weight = dotprod(\@vec_vm,\@vec_node, 'normR');
-			$nodes_weight->{$weight} = $node;
-			$highest_weight = $weight if $weight > $highest_weight;
-		    }	
-		    $target = $nodes_weight->{$highest_weight};	
-		    if(!$target) {
-			warn "couldn't find a target for vmid $vmid\n";
-			next;
-		    }
-		    print "vm:$vmid best target:$target\n";
+		    warn "couldn't find a target for vmid $vmid\n";
+		    next;
 		}
 
 		my $pid;
@@ -2161,6 +2122,52 @@ sub dotprod {
     return $sum;
 }
 
+sub find_best_node_target {
+    my($vmid, $d, $nodename, $storecfg) = @_;
+
+    my $vmconf = PVE::QemuConfig->load_config($vmid);
+    my $members = PVE::Cluster::get_members();
+    my $rrd = PVE::Cluster::rrd_dump();
+    my $nodelist = PVE::Cluster::get_nodelist();
+    my $mem_threshold = 0.8;
+    my $cpu_threshold = 0.8;
+    my $vm_stats = PVE::API2Tools::extract_vm_stats($vmid, $d, $rrd);
+    my $vm_cpu = $vm_stats->{cpu} * $vm_stats->{maxcpu};
+    my $vm_mem = $vm_stats->{mem};
+    my @vec_vm = ($vm_cpu, $vm_mem);  #? add network usage dimension ?
+
+    my $nodes_weight = {};
+    my $highest_weight = 0;
+    foreach my $node (@$nodelist) {
+	next if $node eq $nodename;
+
+	my $node_stats = PVE::API2Tools::extract_node_stats($node, $members, $rrd);
+	my $node_freemem = $node_stats->{maxmem} - $node_stats->{mem};
+	my $node_freecpu = (100 - $node_stats->{cpu}) * $node_stats->{maxcpu};  #how to handle different cpu model power ? bogomips ?
+	next if $node_stats->{status} ne 'online';
+	next if $node_freecpu * $cpu_threshold < $vm_cpu;
+	next if $node_freemem * $mem_threshold < $vm_mem;
+	next if $node_stats->{maxcpu} < $vm_stats->{maxcpu};
+	next if $node_freemem < $vm_stats->{maxmem};
+	eval { PVE::QemuServer::check_storage_availability($storecfg, $vmconf, $node) };
+	next if $@;
+	# fixme: check vmbr available
+
+	#for target node ordering, take swap && ksm in account;
+	#$node_freemem = $node_freemem - $node_stats->{swapused} - $node_stats->{ksm_shared};
+
+	my @vec_node = ($node_freecpu, $node_freemem); #? add network usage dimension ?
+	my $weight = dotprod(\@vec_vm,\@vec_node, 'normR');
+	$nodes_weight->{$weight} = $node;
+	$highest_weight = $weight if $weight > $highest_weight;
+    }	
+    my $target = $nodes_weight->{$highest_weight};	
+
+    return undef if(!$target);
+    print "vm:$vmid best target:$target\n";
+    return $target;
+}
+
 package PVE::API2::Nodes;
 
 use strict;
-- 
2.20.1




More information about the pve-devel mailing list