public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH manager 11/15] ui: migrate: sort target nodes by scheduler recommendation
Date: Wed, 18 Nov 2026 12:05:18 +0100	[thread overview]
Message-ID: <20261118110705.595989-12-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261118110705.595989-1-t.lamprecht@proxmox.com>

Order the target node list of the migrate dialog by the recommendation
of the cluster resource scheduler and preselect the best node, instead
of the alphabetically first one. For a running guest, also show the
cluster load imbalance that the migration to each node would result
in.

The scheduler only considers the load, so offline nodes and nodes that
the migration preconditions rule out, for example due to HA rules, are
sorted last and never preselected. The latter are marked as not allowed
instead of showing their imbalance.

The recommendation is loaded after the dialog opened and never
overrides a target node the user already looked at or picked. If it is
not available, the node list keeps its previous order.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
 www/manager6/window/Migrate.js | 146 +++++++++++++++++++++++++++++++++
 1 file changed, 146 insertions(+)

diff --git a/www/manager6/window/Migrate.js b/www/manager6/window/Migrate.js
index 40bca4228..b27d679df 100644
--- a/www/manager6/window/Migrate.js
+++ b/www/manager6/window/Migrate.js
@@ -112,6 +112,112 @@ Ext.define('PVE.window.Migrate', {
                 onlineHelp: vm.get(view.vmtype).onlineHelp,
             });
             me.lookup('formPanel').isValid();
+
+            me.sortTargetsByRecommendation();
+        },
+
+        // Order the target nodes by the cluster resource scheduler's recommendation and preselect
+        // the best one. Best-effort: without a recommendation the default order stays.
+        sortTargetsByRecommendation: async function () {
+            let me = this;
+            let selector = me.lookup('pveNodeSelector');
+
+            let nodes;
+            try {
+                let { result } = await Proxmox.Async.api2({
+                    url: '/cluster/resource-scheduling/recommend-target-nodes',
+                    method: 'GET',
+                    params: { vmid: me.getViewModel().get('vmid') },
+                });
+                nodes = result.data.nodes ?? [];
+            } catch {
+                return;
+            }
+            if (me.destroyed || !nodes.length) {
+                return;
+            }
+
+            let store = selector.getStore();
+            // read by the imbalance column, which may render before or after the store loads
+            store.resultingImbalance = Object.fromEntries(
+                nodes.map((entry) => [entry.node, entry.imbalance]),
+            );
+            let picker = selector.getPicker();
+            let column = picker.down('#imbalance');
+            if (column?.isHidden() && nodes.some((entry) => entry.imbalance !== undefined)) {
+                column.setHidden(false);
+                picker.setWidth(picker.width + column.width);
+            }
+
+            me.recommendedNodes = nodes.map((entry) => entry.node);
+            me.applyRecommendation();
+        },
+
+        // The scheduler only considers the load, so sort the nodes that the precondition check rules
+        // out, for example due to HA rules, last. Missing storage only blocks an offline migration,
+        // as a running VM can migrate its disks.
+        noteBlockedTargets: function (notAllowedNodes, storageBlocks = false) {
+            let me = this;
+            let blocked = Object.entries(notAllowedNodes ?? {})
+                .filter(
+                    ([_node, reasons]) =>
+                        reasons['blocking-ha-resources']?.length ||
+                        reasons['unavailable-resources']?.length ||
+                        (storageBlocks && reasons.unavailable_storages?.length),
+                )
+                .map(([node]) => node)
+                .sort();
+            if (blocked.join() !== (me.blockedTargets ?? []).join()) {
+                me.blockedTargets = blocked;
+                me.applyRecommendation();
+            }
+        },
+
+        // Sort the target nodes by recommendation, with blocked and offline ones last, and make the
+        // best usable node the default, unless the user already chose a target.
+        applyRecommendation: function () {
+            let me = this;
+            let recommended = me.recommendedNodes;
+            if (me.destroyed || !recommended) {
+                return;
+            }
+            let selector = me.lookup('pveNodeSelector');
+            let store = selector.getStore();
+            let blocked = me.blockedTargets ?? [];
+            store.blockedTargets = blocked; // read by the imbalance column
+
+            let rankOf = (node) => {
+                let rank = recommended.indexOf(node);
+                return rank === -1 ? Number.POSITIVE_INFINITY : rank;
+            };
+            let unusable = (rec) =>
+                blocked.includes(rec.data.node) || rec.data.status !== 'online' ? 1 : 0;
+            // a single sorter without a property, so no column header claims to define the order
+            store.getSorters().clear();
+            store.sort(
+                new Ext.util.Sorter({
+                    sorterFn: (a, b) =>
+                        unusable(a) - unusable(b) ||
+                        rankOf(a.data.node) - rankOf(b.data.node) ||
+                        a.data.node.localeCompare(b.data.node),
+                }),
+            );
+
+            // While its store loads, the selector auto-selects the first node of the new order.
+            // Once loaded, it already picked the first node of the old order, so correct that.
+            if (me.targetTouched || !store.isLoaded()) {
+                return;
+            }
+            let allowed = selector.allowedNodes;
+            let best = recommended.find(
+                (node) =>
+                    store.findExact('node', node) !== -1 &&
+                    !blocked.includes(node) &&
+                    (!allowed || allowed.includes(node)),
+            );
+            if (best && best !== selector.getValue()) {
+                selector.setValue(best);
+            }
         },
 
         onTargetChange: function (nodeSelector) {
@@ -120,6 +226,11 @@ Ext.define('PVE.window.Migrate', {
             this.checkMigratePreconditions(true);
         },
 
+        // the recommendation arrives asynchronously and must not override the user's choice
+        onTargetTouched: function () {
+            this.targetTouched = true;
+        },
+
         startMigration: function () {
             var me = this,
                 view = me.getView(),
@@ -286,6 +397,7 @@ Ext.define('PVE.window.Migrate', {
             }
             migration.preconditions = [];
             let disallowed = migrateStats.not_allowed_nodes?.[target] ?? {};
+            me.noteBlockedTargets(migrateStats.not_allowed_nodes, !vm.get('running'));
 
             if (migrateStats.allowed_nodes && !vm.get('running')) {
                 migration.allowedNodes = migrateStats.allowed_nodes;
@@ -534,6 +646,7 @@ Ext.define('PVE.window.Migrate', {
             migration.preconditions = [];
             let targetNode = me.lookup('pveNodeSelector').value;
             let disallowed = migrateStats['not-allowed-nodes']?.[targetNode] ?? {};
+            me.noteBlockedTargets(migrateStats['not-allowed-nodes']);
 
             let blockingHAResources = disallowed['blocking-ha-resources'] ?? [];
             if (blockingHAResources.length) {
@@ -637,8 +750,41 @@ Ext.define('PVE.window.Migrate', {
                             allowBlank: false,
                             disallowedNodes: undefined,
                             onlineValidator: true,
+                            // replaces the class default, so extend its columns
+                            listConfig: {
+                                columns: [
+                                    ...PVE.form.NodeSelector.prototype.listConfig.columns.map(
+                                        (column) => ({ ...column }),
+                                    ),
+                                    {
+                                        header: gettext('Resulting imbalance'),
+                                        itemId: 'imbalance',
+                                        dataIndex: 'node',
+                                        hidden: true, // shown once a recommendation provides it
+                                        sortable: false,
+                                        width: 140,
+                                        tooltip: gettext(
+                                            'Estimated cluster load imbalance after migrating the guest to this node (lower is better)',
+                                        ),
+                                        renderer: function (node, meta, record) {
+                                            let store = record.store;
+                                            // a better score of a blocked node would mislead
+                                            if (store?.blockedTargets?.includes(node)) {
+                                                return gettext('Not allowed');
+                                            }
+                                            let value = store?.resultingImbalance?.[node];
+                                            if (value === undefined) {
+                                                return '';
+                                            }
+                                            return `${(value * 100).toFixed(1)}%`;
+                                        },
+                                    },
+                                ],
+                            },
                             listeners: {
                                 change: 'onTargetChange',
+                                expand: 'onTargetTouched',
+                                select: 'onTargetTouched',
                             },
                         },
                         {
-- 
2.47.3





  parent reply	other threads:[~2026-11-18 11:08 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 11:05 [PATCH guest-common,ha-manager,manager,docs 0/15] cluster resource scheduling for all guests Thomas Lamprecht
2026-11-18 11:05 ` [PATCH guest-common 01/15] scheduling: add cluster resource scheduling library Thomas Lamprecht
2026-11-18 11:05 ` [PATCH ha-manager 02/15] helpers: block both locations of a migrating negative affinity partner Thomas Lamprecht
2026-11-18 11:05 ` [PATCH ha-manager 03/15] usage: static: count services per node again for scoring fallback Thomas Lamprecht
2026-11-18 11:05 ` [PATCH ha-manager 04/15] config: allow getting motion info of many resources at once Thomas Lamprecht
2026-11-18 11:05 ` [PATCH ha-manager 05/15] env: pve2: delegate cluster stat gathering to scheduling library Thomas Lamprecht
2026-11-18 11:05 ` [PATCH ha-manager 06/15] usage: share one scheduler base between static and dynamic Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 07/15] ui: bulk action: translate error title and CT restart warning Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 08/15] ui: bulk migrate: show CT restart warning for cluster-wide migration Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 09/15] ui: migrate: recheck preconditions if the target changed meanwhile Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 10/15] api: cluster: add resource scheduling endpoints Thomas Lamprecht
2026-11-18 11:05 ` Thomas Lamprecht [this message]
2026-11-18 11:05 ` [PATCH manager 12/15] api: bulk migrate: allow a per-guest target node mapping Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 13/15] ui: bulk migrate: support per-guest target nodes Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 14/15] ui: dc: add a Rebalance Guests bulk action Thomas Lamprecht
2026-11-18 11:05 ` [PATCH docs 15/15] pvecm: document balancing the cluster load with migrations Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118110705.595989-12-t.lamprecht@proxmox.com \
    --to=t.lamprecht@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal