3 # Local Resource Manager
8 use POSIX
qw(:sys_wait_h);
12 use PVE
::HA
::Tools
':exit_codes';
13 use PVE
::HA
::Resources
;
15 # Server can have several states:
18 wait_for_agent_lock
=> "waiting for agent lock",
19 active
=> "got agent_lock",
20 lost_agent_lock
=> "lost agent_lock",
24 my ($this, $haenv) = @_;
26 my $class = ref($this) || $this;
30 status
=> { state => 'startup' },
34 shutdown_request
=> 0,
36 # mode can be: active, reboot, shutdown, restart
40 $self->set_local_status({ state => 'wait_for_agent_lock' });
45 sub shutdown_request
{
48 return if $self->{shutdown_request
}; # already in shutdown mode
50 my $haenv = $self->{haenv
};
52 my $nodename = $haenv->nodename();
54 my $shutdown = $haenv->is_node_shutdown();
57 $haenv->log('info', "shutdown LRM, stop all services");
58 $self->{mode
} = 'shutdown';
60 # queue stop jobs for all services
62 my $ss = $self->{service_status
};
64 foreach my $sid (keys %$ss) {
67 next if $sd->{node
} ne $nodename;
68 # Note: use undef uid to mark shutdown/stop jobs
69 $self->queue_resource_command($sid, undef, 'request_stop');
73 $haenv->log('info', "restart LRM, freeze all services");
74 $self->{mode
} = 'restart';
77 $self->{shutdown_request
} = 1;
79 eval { $self->update_lrm_status(); };
81 $self->log('err', "unable to update lrm status file - $err");
85 sub get_local_status
{
88 return $self->{status
};
91 sub set_local_status
{
92 my ($self, $new) = @_;
94 die "invalid state '$new->{state}'" if !$valid_states->{$new->{state}};
96 my $haenv = $self->{haenv
};
98 my $old = $self->{status
};
100 # important: only update if if really changed
101 return if $old->{state} eq $new->{state};
103 $haenv->log('info', "status change $old->{state} => $new->{state}");
105 $new->{state_change_time
} = $haenv->get_time();
107 $self->{status
} = $new;
110 sub update_lrm_status
{
113 my $haenv = $self->{haenv
};
115 return 0 if !$haenv->quorate();
118 state => $self->{status
}->{state},
119 mode
=> $self->{mode
},
120 results
=> $self->{results
},
121 timestamp
=> $haenv->get_time(),
124 eval { $haenv->write_lrm_status($lrm_status); };
126 $haenv->log('err', "unable to write lrm status file - $err");
133 sub get_protected_ha_agent_lock
{
136 my $haenv = $self->{haenv
};
139 my $starttime = $haenv->get_time();
143 if ($haenv->get_ha_agent_lock()) {
144 if ($self->{ha_agent_wd
}) {
145 $haenv->watchdog_update($self->{ha_agent_wd
});
147 my $wfh = $haenv->watchdog_open();
148 $self->{ha_agent_wd
} = $wfh;
153 last if ++$count > 5; # try max 5 time
155 my $delay = $haenv->get_time() - $starttime;
156 last if $delay > 5; # for max 5 seconds
164 sub active_service_count
{
167 my $haenv = $self->{haenv
};
169 my $nodename = $haenv->nodename();
171 my $ss = $self->{service_status
};
175 foreach my $sid (keys %$ss) {
176 my $sd = $ss->{$sid};
177 next if !$sd->{node
};
178 next if $sd->{node
} ne $nodename;
179 my $req_state = $sd->{state};
180 next if !defined($req_state);
181 next if $req_state eq 'stopped';
182 next if $req_state eq 'freeze';
183 # erroneous services are not managed by HA, don't count them as active
184 next if $req_state eq 'error';
192 my $wrote_lrm_status_at_startup = 0;
194 sub do_one_iteration
{
197 my $haenv = $self->{haenv
};
199 if (!$wrote_lrm_status_at_startup) {
200 if ($self->update_lrm_status()) {
201 $wrote_lrm_status_at_startup = 1;
205 return $self->{shutdown_request
} ?
0 : 1;
209 my $status = $self->get_local_status();
210 my $state = $status->{state};
212 my $ms = $haenv->read_manager_status();
213 $self->{service_status
} = $ms->{service_status
} || {};
215 my $fence_request = PVE
::HA
::Tools
::count_fenced_services
($self->{service_status
}, $haenv->nodename());
217 # do state changes first
219 my $ctime = $haenv->get_time();
221 if ($state eq 'wait_for_agent_lock') {
223 my $service_count = $self->active_service_count();
225 if (!$fence_request && $service_count && $haenv->quorate()) {
226 if ($self->get_protected_ha_agent_lock()) {
227 $self->set_local_status({ state => 'active' });
231 } elsif ($state eq 'lost_agent_lock') {
233 if (!$fence_request && $haenv->quorate()) {
234 if ($self->get_protected_ha_agent_lock()) {
235 $self->set_local_status({ state => 'active' });
239 } elsif ($state eq 'active') {
241 if ($fence_request) {
242 $haenv->log('err', "node need to be fenced - releasing agent_lock\n");
243 $self->set_local_status({ state => 'lost_agent_lock'});
244 } elsif (!$self->get_protected_ha_agent_lock()) {
245 $self->set_local_status({ state => 'lost_agent_lock'});
249 $status = $self->get_local_status();
250 $state = $status->{state};
254 if ($state eq 'wait_for_agent_lock') {
256 return 0 if $self->{shutdown_request
};
258 $self->update_lrm_status();
262 } elsif ($state eq 'active') {
264 my $startime = $haenv->get_time();
270 # do work (max_time seconds)
272 # fixme: set alert timer
274 if ($self->{shutdown_request
}) {
276 if ($self->{mode
} eq 'restart') {
278 my $service_count = $self->active_service_count();
280 if ($service_count == 0) {
282 if ($self->run_workers() == 0) {
283 if ($self->{ha_agent_wd
}) {
284 $haenv->watchdog_close($self->{ha_agent_wd
});
285 delete $self->{ha_agent_wd
};
290 # restart with no or freezed services, release the lock
291 $haenv->release_ha_agent_lock();
296 if ($self->run_workers() == 0) {
297 if ($self->{shutdown_errors
} == 0) {
298 if ($self->{ha_agent_wd
}) {
299 $haenv->watchdog_close($self->{ha_agent_wd
});
300 delete $self->{ha_agent_wd
};
303 # shutdown with all services stopped thus release the lock
304 $haenv->release_ha_agent_lock();
312 $self->manage_resources();
317 $haenv->log('err', "got unexpected error - $err");
320 $self->update_lrm_status();
322 return 0 if $shutdown;
324 $haenv->sleep_until($startime + $max_time);
326 } elsif ($state eq 'lost_agent_lock') {
328 # Note: watchdog is active an will triger soon!
330 # so we hope to get the lock back soon!
332 if ($self->{shutdown_request
}) {
334 my $service_count = $self->active_service_count();
336 if ($service_count > 0) {
337 $haenv->log('err', "get shutdown request in state 'lost_agent_lock' - " .
338 "detected $service_count running services");
342 # all services are stopped, so we can close the watchdog
344 if ($self->{ha_agent_wd
}) {
345 $haenv->watchdog_close($self->{ha_agent_wd
});
346 delete $self->{ha_agent_wd
};
357 die "got unexpected status '$state'\n";
367 my $haenv = $self->{haenv
};
369 my $starttime = $haenv->get_time();
371 # number of workers to start, if 0 we exec the command directly witouth forking
372 my $max_workers = $haenv->get_max_workers();
374 my $sc = $haenv->read_service_config();
376 while (($haenv->get_time() - $starttime) < 5) {
377 my $count = $self->check_active_workers();
379 foreach my $sid (sort keys %{$self->{workers
}}) {
380 last if $count >= $max_workers && $max_workers > 0;
382 my $w = $self->{workers
}->{$sid};
384 # only fork if we may else call exec_resource_agent
385 # directly (e.g. for regression tests)
386 if ($max_workers > 0) {
388 if (!defined($pid)) {
389 $haenv->log('err', "fork worker failed");
390 $count = 0; last; # abort, try later
391 } elsif ($pid == 0) {
392 $haenv->after_fork(); # cleanup
397 $res = $self->exec_resource_agent($sid, $sc->{$sid}, $w->{state}, $w->{target
});
400 $haenv->log('err', $err);
411 $res = $self->exec_resource_agent($sid, $sc->{$sid}, $w->{state}, $w->{target
});
412 $res = $res << 8 if $res > 0;
415 $haenv->log('err', $err);
417 if (defined($w->{uid
})) {
418 $self->resource_command_finished($sid, $w->{uid
}, $res);
420 $self->stop_command_finished($sid, $res);
431 return scalar(keys %{$self->{workers
}});
434 sub manage_resources
{
437 my $haenv = $self->{haenv
};
439 my $nodename = $haenv->nodename();
441 my $ss = $self->{service_status
};
443 foreach my $sid (keys %{$self->{restart_tries
}}) {
444 delete $self->{restart_tries
}->{$sid} if !$ss->{$sid};
447 foreach my $sid (keys %$ss) {
448 my $sd = $ss->{$sid};
449 next if !$sd->{node
};
451 next if $sd->{node
} ne $nodename;
452 my $req_state = $sd->{state};
453 next if !defined($req_state);
454 next if $req_state eq 'freeze';
455 $self->queue_resource_command($sid, $sd->{uid
}, $req_state, $sd->{target
});
458 return $self->run_workers();
461 sub queue_resource_command
{
462 my ($self, $sid, $uid, $state, $target) = @_;
464 # do not queue the excatly same command twice as this may lead to
465 # an inconsistent HA state when the first command fails but the CRM
466 # does not process its failure right away and the LRM starts a second
467 # try, without the CRM knowing of it (race condition)
468 # The 'stopped' command is an exception as we do not process its result
469 # in the CRM and we want to execute it always (even with no active CRM)
470 return if $state ne 'stopped' && $uid && defined($self->{results
}->{$uid});
472 if (my $w = $self->{workers
}->{$sid}) {
473 return if $w->{pid
}; # already started
474 # else, delete and overwrite queue entry with new command
475 delete $self->{workers
}->{$sid};
478 $self->{workers
}->{$sid} = {
484 $self->{workers
}->{$sid}->{target
} = $target if $target;
487 sub check_active_workers
{
490 # finish/count workers
492 foreach my $sid (keys %{$self->{workers
}}) {
493 my $w = $self->{workers
}->{$sid};
494 if (my $pid = $w->{pid
}) {
496 my $waitpid = waitpid($pid, WNOHANG
);
497 if (defined($waitpid) && ($waitpid == $pid)) {
498 if (defined($w->{uid
})) {
499 $self->resource_command_finished($sid, $w->{uid
}, $?);
501 $self->stop_command_finished($sid, $?);
512 sub stop_command_finished
{
513 my ($self, $sid, $status) = @_;
515 my $haenv = $self->{haenv
};
517 my $w = delete $self->{workers
}->{$sid};
518 return if !$w; # should not happen
523 $haenv->log('err', "resource agent $sid finished - failed to execute");
524 } elsif (my $sig = ($status & 127)) {
525 $haenv->log('err', "resource agent $sid finished - got signal $sig");
527 $exit_code = ($status >> 8);
530 if ($exit_code != 0) {
531 $self->{shutdown_errors
}++;
535 sub resource_command_finished
{
536 my ($self, $sid, $uid, $status) = @_;
538 my $haenv = $self->{haenv
};
540 my $w = delete $self->{workers
}->{$sid};
541 return if !$w; # should not happen
546 $haenv->log('err', "resource agent $sid finished - failed to execute");
547 } elsif (my $sig = ($status & 127)) {
548 $haenv->log('err', "resource agent $sid finished - got signal $sig");
550 $exit_code = ($status >> 8);
553 $exit_code = $self->handle_service_exitcode($sid, $w->{state}, $exit_code);
555 return if $exit_code == ETRY_AGAIN
; # tell nobody, simply retry
557 $self->{results
}->{$uid} = {
559 state => $w->{state},
560 exit_code
=> $exit_code,
563 my $ss = $self->{service_status
};
565 # compute hash of valid/existing uids
567 foreach my $sid (keys %$ss) {
568 my $sd = $ss->{$sid};
570 $valid_uids->{$sd->{uid
}} = 1;
574 foreach my $id (keys %{$self->{results
}}) {
575 next if !$valid_uids->{$id};
576 $results->{$id} = $self->{results
}->{$id};
578 $self->{results
} = $results;
581 # processes the exit code from a finished resource agent, so that the CRM knows
582 # if the LRM wants to retry an action based on the current recovery policies for
583 # the failed service, or the CRM itself must try to recover from the failure.
584 sub handle_service_exitcode
{
585 my ($self, $sid, $cmd, $exit_code) = @_;
587 my $haenv = $self->{haenv
};
588 my $tries = $self->{restart_tries
};
590 my $sc = $haenv->read_service_config();
594 if (my $cd = $sc->{$sid}) {
595 $max_restart = $cd->{max_restart
};
598 if ($cmd eq 'started') {
600 if ($exit_code == SUCCESS
) {
606 } elsif ($exit_code == ERROR
) {
608 $tries->{$sid} = 0 if !defined($tries->{$sid});
610 if ($tries->{$sid} >= $max_restart) {
611 $haenv->log('err', "unable to start service $sid on local node".
612 " after $tries->{$sid} retries");
619 $haenv->log('warning', "restart policy: retry number $tries->{$sid}" .
620 " for service '$sid'");
621 # tell CRM that we retry the start
630 sub exec_resource_agent
{
631 my ($self, $sid, $service_config, $cmd, @params) = @_;
633 # setup execution environment
635 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
637 my $haenv = $self->{haenv
};
639 my $nodename = $haenv->nodename();
641 my (undef, $service_type, $service_name) = PVE
::HA
::Tools
::parse_sid
($sid);
643 my $plugin = PVE
::HA
::Resources-
>lookup($service_type);
645 $haenv->log('err', "service type '$service_type' not implemented");
646 return EUNKNOWN_SERVICE_TYPE
;
649 if (!$service_config) {
650 $haenv->log('err', "missing resource configuration for '$sid'");
651 return EUNKNOWN_SERVICE
;
654 # process error state early
655 if ($cmd eq 'error') {
657 $haenv->log('err', "service $sid is in an error state and needs manual " .
658 "intervention. Look up 'ERROR RECOVERY' in the documentation.");
660 return SUCCESS
; # error always succeeds
663 if ($service_config->{node
} ne $nodename) {
664 $haenv->log('err', "service '$sid' not on this node");
668 my $id = $service_name;
670 my $running = $plugin->check_running($haenv, $id);
672 if ($cmd eq 'started') {
674 return SUCCESS
if $running;
676 $haenv->log("info", "starting service $sid");
678 $plugin->start($haenv, $id);
680 $running = $plugin->check_running($haenv, $id);
683 $haenv->log("info", "service status $sid started");
686 $haenv->log("warning", "unable to start service $sid");
690 } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') {
692 return SUCCESS
if !$running;
694 $haenv->log("info", "stopping service $sid");
696 $plugin->shutdown($haenv, $id);
698 $running = $plugin->check_running($haenv, $id);
701 $haenv->log("info", "service status $sid stopped");
704 $haenv->log("info", "unable to stop stop service $sid (still running)");
708 } elsif ($cmd eq 'migrate' || $cmd eq 'relocate') {
710 my $target = $params[0];
711 if (!defined($target)) {
712 die "$cmd '$sid' failed - missing target\n" if !defined($target);
713 return EINVALID_PARAMETER
;
716 if ($service_config->{node
} eq $target) {
721 my $online = ($cmd eq 'migrate') ?
1 : 0;
723 my $res = $plugin->migrate($haenv, $id, $target, $online);
725 # something went wrong if service is still on this node
727 $haenv->log("err", "service $sid not moved (migration error)");
735 $haenv->log("err", "implement me (cmd '$cmd')");
736 return EUNKNOWN_COMMAND
;