all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: "Daniel Kral" <d.kral@proxmox.com>
To: "Thomas Lamprecht" <t.lamprecht@proxmox.com>,
	<pve-devel@lists.proxmox.com>
Subject: Re: [PATCH ha-manager v2 2/2] manager: avoid nodes where moving a service failed
Date: Mon, 23 Nov 2026 13:10:34 +0100	[thread overview]
Message-ID: <DN2KVVS20592.114CDHPOCIDN5@proxmox.com> (raw)
In-Reply-To: <20261123072228.1506031-3-t.lamprecht@proxmox.com>

On Mon Nov 23, 2026 at 7:53 AM CET, Thomas Lamprecht wrote:
> When moving a service failed, for example because the LRM refused a
> target that would drop the guest identity, the next automatic move of
> the service selected the same node again. Maintenance mode and shutdown
> with the 'migrate' policy then retried that node every round and never
> finished, even if other nodes would have worked, and rebalancing kept
> trying to move the service there.
>
> Remember for five minutes where moving a service failed, and only
> select such a node if no other node is left. Other nodes include nodes
> with a lower priority and the current node, so a failed failback leaves
> the service where it is. If only such nodes are left, try the one where
> moving failed longest ago first. Rebalancing does not select them, as
> it can always leave the service where it is. Explicit migration
> requests are not affected.
>
> Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
> ---
>
> new in v2
>
>  src/PVE/HA/Manager.pm                         | 78 +++++++++++++--
>  .../test-crm-maintenance-request5/log.expect  | 10 +-
>  src/test/test-migrate-old-target2/README      |  5 +-
>  src/test/test-migrate-old-target2/log.expect  | 77 +++++++--------
>  src/test/test-migrate-old-target5/README      |  7 ++
>  src/test/test-migrate-old-target5/cmdlist     |  3 +
>  .../test-migrate-old-target5/datacenter.cfg   |  6 ++
>  .../test-migrate-old-target5/hardware_status  |  5 +
>  src/test/test-migrate-old-target5/log.expect  | 98 +++++++++++++++++++
>  .../test-migrate-old-target5/manager_status   |  1 +
>  .../test-migrate-old-target5/service_config   |  6 ++
>  .../static_service_stats                      |  6 ++
>  src/test/test-migrate-old-target6/README      | 14 +++
>  src/test/test-migrate-old-target6/cmdlist     |  5 +
>  .../test-migrate-old-target6/hardware_status  |  5 +
>  src/test/test-migrate-old-target6/log.expect  | 74 ++++++++++++++
>  .../test-migrate-old-target6/manager_status   |  1 +
>  .../test-migrate-old-target6/rules_config     |  7 ++
>  .../test-migrate-old-target6/service_config   |  4 +
>  .../log.expect                                | 36 +++----
>  20 files changed, 374 insertions(+), 74 deletions(-)
>  create mode 100644 src/test/test-migrate-old-target5/README
>  create mode 100644 src/test/test-migrate-old-target5/cmdlist
>  create mode 100644 src/test/test-migrate-old-target5/datacenter.cfg
>  create mode 100644 src/test/test-migrate-old-target5/hardware_status
>  create mode 100644 src/test/test-migrate-old-target5/log.expect
>  create mode 100644 src/test/test-migrate-old-target5/manager_status
>  create mode 100644 src/test/test-migrate-old-target5/service_config
>  create mode 100644 src/test/test-migrate-old-target5/static_service_stats
>  create mode 100644 src/test/test-migrate-old-target6/README
>  create mode 100644 src/test/test-migrate-old-target6/cmdlist
>  create mode 100644 src/test/test-migrate-old-target6/hardware_status
>  create mode 100644 src/test/test-migrate-old-target6/log.expect
>  create mode 100644 src/test/test-migrate-old-target6/manager_status
>  create mode 100644 src/test/test-migrate-old-target6/rules_config
>  create mode 100644 src/test/test-migrate-old-target6/service_config
>
> diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm
> index b719f23..bb3938f 100644
> --- a/src/PVE/HA/Manager.pm
> +++ b/src/PVE/HA/Manager.pm
> @@ -31,6 +31,9 @@ eval {
>      $have_dynamic_scheduling = 1;
>  };
>  
> +# How long a node where moving a service failed is avoided for that service, in seconds.
> +my $FAILED_TARGET_EXPIRY = 5 * 60;

Can be a static time window for now, contemplating whether using
exponential backoff with a upper time bound as in Michael Ryom's patch
series [0] here could be useful.

However, the main issue with exponential backoff is that the time window
can grow too large and users cannot control or look into how much the
HA Manager waits, so that could end up making the HA Manager wait a long
time even though the issue we're waiting on has already been resolved in
the mean time.

So probably better to stay at a static, predictable time window.

[0] https://lore.proxmox.com/pve-devel/20261018124559.24305-5-Michael@RyomHerold.dk/

> +
>  ## Variable Name & Abbreviations Convention
>  #
>  # The HA stack has some variables it uses frequently and thus abbreviates it such that it may be
> @@ -215,6 +218,13 @@ sub get_resource_migration_candidates {
>  
>          delete $target_nodes->{$current_leader_node};
>  
> +        # rebalancing can always leave the resources where they are, so it does not retry a node
> +        # where moving one of them failed
> +        for my $sid ($resource_bundles->{$leader_sid}->@*) {
> +            my $failed_targets = $ss->{$sid}->{failed_targets} // {};
> +            delete $target_nodes->@{ keys %$failed_targets };
> +        }
> +
>          next if !%$target_nodes;
>  
>          push @compact_migration_candidates,
> @@ -339,6 +349,9 @@ The C<$node_preference> can be set to:
>  
>  =back
>  
> +Nodes in C<< $sd->{failed_targets} >>, where moving the service failed recently, are only selected
> +if no other node is left, also considering nodes with a lower priority and the current node.
> +
>  =cut
>  
>  sub select_service_node {
> @@ -372,15 +385,45 @@ sub select_service_node {
>          return $current_node;
>      }
>  
> -    # try to avoid nodes where the service failed already if we want to relocate
> -    if ($node_preference eq 'try-next') {
> -        foreach my $node (@$tried_nodes) {
> -            delete $pri_nodes->{$node};
> +    my $narrow_down = sub {
> +        my ($nodes) = @_;
> +
> +        # try to avoid nodes where the service failed already if we want to relocate
> +        if ($node_preference eq 'try-next') {
> +            foreach my $node (@$tried_nodes) {
> +                delete $nodes->{$node};
> +            }
>          }
> -    }
>  
> -    apply_negative_resource_affinity($separate, $pri_nodes);
> -    apply_positive_resource_affinity($together, $pri_nodes);
> +        apply_negative_resource_affinity($separate, $nodes);
> +        apply_positive_resource_affinity({ $together->%* }, $nodes);
> +    };
> +
> +    # Nodes where moving the service failed are only used if no other node is left, which may also
> +    # be a node with a lower priority or the current node, so leave them out before the priorities.
> +    my $failed_targets = $sd->{failed_targets} // {};
> +    my @avoided_nodes = grep { $_ ne $current_node && $online_nodes->{$_} } keys %$failed_targets;
> +    if (@avoided_nodes) {
> +        my $other_nodes = { $online_nodes->%* };
> +        delete $other_nodes->@{@avoided_nodes};
> +        my (undef, $other_pri_nodes) = get_node_affinity($node_affinity, $sid, $other_nodes);
> +        $narrow_down->($other_pri_nodes);
> +
> +        if (%$other_pri_nodes) {
> +            $pri_nodes = $other_pri_nodes;
> +        } else {
> +            $narrow_down->($pri_nodes);
> +            # retry the node where moving the service failed longest ago first
> +            if (!grep { !$failed_targets->{$_} } keys %$pri_nodes) {
> +                my ($oldest) = sort {
> +                    $failed_targets->{$a} <=> $failed_targets->{$b} || $a cmp $b
> +                } keys %$pri_nodes;
> +                $pri_nodes = { $oldest => 1 } if defined($oldest);
> +            }
> +        }
> +    } else {
> +        $narrow_down->($pri_nodes);
> +    }

select_service_node() is quite a stateful subroutine already and I'm
currently working on fixing cases, where affinity rules and/or the
maintenance fallback node aren't resolved correctly/efficiently here.
This needs some care to get right and should come with a v3 for the
maintenance node edge case series [1].

Unfortunately, this patch collides with this series quite much and would
make untangling the existing errors in select_service_node() harder, so
I'd prefer to wait for this functionality after select_service_node()
has settled again.

In general, it would be nice to make the special use cases from
select_service_node() ('try-next', maintenance node fallback, avoid
random migrations) be moved out, because most of them are for
next_state_started() anyway, rather than adding more complexity in here,
so it's easier to fix issues and review changes done here.

Additionally, I think it would also be nice to make this remember failed
starts for longer as well, because then the 'try-next' behavior could
probably be made more stable in some scenarios as well... maybe even
streamlined with remembering failed migrations, because it has the same
incentive: to not issue migrations there as we suspect that an
HA resource cannot be moved and/or started there. Then these might be
able to share the same code path here as well, reducing the overall
complexity.

[1] https://lore.proxmox.com/pve-devel/20261106095650.72865-1-d.kral@proxmox.com/

>  
>      return $maintenance_fallback
>          if defined($maintenance_fallback) && $pri_nodes->{$maintenance_fallback};
> @@ -518,6 +561,7 @@ my $change_service_state = sub {
>      my $old_state = $sd->{state};
>      my $old_node = $sd->{node};
>      my $old_failed_nodes = $sd->{failed_nodes};
> +    my $old_failed_targets = $sd->{failed_targets};
>      my $old_maintenance_node = $sd->{maintenance_node};
>  
>      die "no state change" if $old_state eq $new_state; # just to be sure
> @@ -529,6 +573,7 @@ my $change_service_state = sub {
>      $sd->{state} = $new_state;
>      $sd->{node} = $old_node;
>      $sd->{failed_nodes} = $old_failed_nodes if defined($old_failed_nodes);
> +    $sd->{failed_targets} = $old_failed_targets if defined($old_failed_targets);
>      $sd->{maintenance_node} = $old_maintenance_node if defined($old_maintenance_node);
>  
>      my $text_state = '';
> @@ -1170,6 +1215,8 @@ sub manage {
>          $self->{last_services_digest} = $services_digest;
>      }
>  
> +    $self->forget_expired_failed_targets();
> +
>      $self->update_crm_commands();
>  
>      my $deferred_sids;
> @@ -1353,6 +1400,7 @@ sub next_state_migrate_relocate {
>              $change_service_state->($self, $sid, $req_state, node => $sd->{node});
>          } else {
>              $haenv->log('err', "service '$sid' - migration failed (exit code $exit_code)");
> +            $sd->{failed_targets}->{ $sd->{target} } = $haenv->get_time();

nit: $sd->{target} coulde be factored out as $target_node before to
     avoid dereferencing a hash value to dereference another hash value

It should be noted that now any HA resource migration marks failed
targets.

Also, should we log that the HA resource will not be automatically moved
to the node again for $FAILED_TARGET_EXPIRY, so that users are aware for
that behavior when looking at the logs?

>              &$change_service_state($self, $sid, $req_state, node => $sd->{node});
>              return;
>          }
> @@ -1483,6 +1531,20 @@ sub next_state_request_start {
>      $change_service_state->($self, $sid, 'started', node => $current_node);
>  }
>  
> +sub forget_expired_failed_targets {
> +    my ($self) = @_;
> +
> +    my $now = $self->{haenv}->get_time();
> +    for my $sd (values $self->{ss}->%*) {
> +        my $failed_targets = $sd->{failed_targets} or next;
> +        for my $node (keys %$failed_targets) {
> +            delete $failed_targets->{$node}
> +                if $now - $failed_targets->{$node} >= $FAILED_TARGET_EXPIRY;
> +        }
> +        delete $sd->{failed_targets} if !%$failed_targets;
> +    }
> +}
> +
>  sub record_service_failed_on_node {
>      my ($self, $sid, $node) = @_;
>  
> @@ -1702,6 +1764,7 @@ sub next_state_error {
>      if ($cd->{state} eq 'disabled') {
>          # clean up on error recovery
>          delete $sd->{failed_nodes};
> +        delete $sd->{failed_targets};
>  
>          &$change_service_state($self, $sid, 'stopped');
>          return;
> @@ -1760,6 +1823,7 @@ sub next_state_recovery {
>          if ($cd->{state} eq 'disabled') {
>              # allow getting a service out of recovery manually if an admin disables it.
>              delete $sd->{failed_nodes}; # clean up on recovery to stopped
> +            delete $sd->{failed_targets};
>              $change_service_state->($self, $sid, 'stopped'); # must NOT go through request_stop
>              return;
>          }




  reply	other threads:[~2026-11-23 12:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  6:53 [PATCH ha-manager v2 0/2] avoid stopping guests for refused moves and retrying failed targets Thomas Lamprecht
2026-11-23  6:53 ` [PATCH ha-manager v2 1/2] lrm: refuse targets dropping the guest identity before stopping guests Thomas Lamprecht
2026-11-23  6:53 ` [PATCH ha-manager v2 2/2] manager: avoid nodes where moving a service failed Thomas Lamprecht
2026-11-23 12:10   ` Daniel Kral [this message]
2026-11-23 12:33     ` Dominik Rusovac

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=DN2KVVS20592.114CDHPOCIDN5@proxmox.com \
    --to=d.kral@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    --cc=t.lamprecht@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal