]> git.proxmox.com Git - pve-ha-manager.git/blame - src/PVE/HA/LRM.pm
fix relocate/restart trial count leak on service deletion
[pve-ha-manager.git] / src / PVE / HA / LRM.pm
CommitLineData
5f095798
DM
1package PVE::HA::LRM;
2
3# Local Resource Manager
4
5use strict;
6use warnings;
c4a221bc
DM
7use Data::Dumper;
8use POSIX qw(:sys_wait_h);
5f095798
DM
9
10use PVE::SafeSyslog;
11use PVE::Tools;
a89ff919 12use PVE::HA::Tools ':exit_codes';
2a045f55 13use PVE::HA::Resources;
5f095798
DM
14
15# Server can have several states:
16
17my $valid_states = {
ec911edd 18 wait_for_agent_lock => "waiting for agent lock",
0bba8f60 19 active => "got agent_lock",
5f095798
DM
20 lost_agent_lock => "lost agent_lock",
21};
22
23sub new {
24 my ($this, $haenv) = @_;
25
26 my $class = ref($this) || $this;
27
28 my $self = bless {
29 haenv => $haenv,
30 status => { state => 'startup' },
c4a221bc
DM
31 workers => {},
32 results => {},
ea4443cc 33 restart_tries => {},
067cdf33 34 shutdown_request => 0,
116dea30 35 shutdown_errors => 0,
9c7d068b
DM
36 # mode can be: active, reboot, shutdown, restart
37 mode => 'active',
5f095798
DM
38 }, $class;
39
b0bf08a9 40 $self->set_local_status({ state => 'wait_for_agent_lock' });
9c7d068b 41
5f095798
DM
42 return $self;
43}
44
45sub shutdown_request {
46 my ($self) = @_;
47
f1be5b3a
DM
48 return if $self->{shutdown_request}; # already in shutdown mode
49
499f06e3
DM
50 my $haenv = $self->{haenv};
51
116dea30
DM
52 my $nodename = $haenv->nodename();
53
cde77779 54 my $shutdown = $haenv->is_node_shutdown();
499f06e3
DM
55
56 if ($shutdown) {
57 $haenv->log('info', "shutdown LRM, stop all services");
58 $self->{mode} = 'shutdown';
116dea30
DM
59
60 # queue stop jobs for all services
61
62 my $ss = $self->{service_status};
63
64 foreach my $sid (keys %$ss) {
65 my $sd = $ss->{$sid};
66 next if !$sd->{node};
67 next if $sd->{node} ne $nodename;
c0edbd7e 68 # Note: use undef uid to mark shutdown/stop jobs
116dea30
DM
69 $self->queue_resource_command($sid, undef, 'request_stop');
70 }
71
499f06e3
DM
72 } else {
73 $haenv->log('info', "restart LRM, freeze all services");
74 $self->{mode} = 'restart';
75 }
9c7d068b 76
499f06e3 77 $self->{shutdown_request} = 1;
9c7d068b
DM
78
79 eval { $self->update_lrm_status(); };
80 if (my $err = $@) {
5bd7aa54 81 $self->log('err', "unable to update lrm status file - $err");
9c7d068b 82 }
5f095798
DM
83}
84
85sub get_local_status {
86 my ($self) = @_;
87
88 return $self->{status};
89}
90
91sub set_local_status {
92 my ($self, $new) = @_;
93
94 die "invalid state '$new->{state}'" if !$valid_states->{$new->{state}};
95
96 my $haenv = $self->{haenv};
97
98 my $old = $self->{status};
99
100 # important: only update if if really changed
101 return if $old->{state} eq $new->{state};
102
0bba8f60 103 $haenv->log('info', "status change $old->{state} => $new->{state}");
5f095798
DM
104
105 $new->{state_change_time} = $haenv->get_time();
106
107 $self->{status} = $new;
108}
109
9c7d068b
DM
110sub update_lrm_status {
111 my ($self) = @_;
112
5bd7aa54
DM
113 my $haenv = $self->{haenv};
114
79829202
DM
115 return 0 if !$haenv->quorate();
116
9c7d068b 117 my $lrm_status = {
331a9f00 118 state => $self->{status}->{state},
9c7d068b
DM
119 mode => $self->{mode},
120 results => $self->{results},
aa330d1c 121 timestamp => $haenv->get_time(),
9c7d068b
DM
122 };
123
5bd7aa54
DM
124 eval { $haenv->write_lrm_status($lrm_status); };
125 if (my $err = $@) {
126 $haenv->log('err', "unable to write lrm status file - $err");
127 return 0;
128 }
129
130 return 1;
9c7d068b
DM
131}
132
5f095798
DM
133sub get_protected_ha_agent_lock {
134 my ($self) = @_;
135
136 my $haenv = $self->{haenv};
137
138 my $count = 0;
139 my $starttime = $haenv->get_time();
140
141 for (;;) {
142
143 if ($haenv->get_ha_agent_lock()) {
144 if ($self->{ha_agent_wd}) {
145 $haenv->watchdog_update($self->{ha_agent_wd});
146 } else {
147 my $wfh = $haenv->watchdog_open();
148 $self->{ha_agent_wd} = $wfh;
149 }
150 return 1;
151 }
152
153 last if ++$count > 5; # try max 5 time
154
155 my $delay = $haenv->get_time() - $starttime;
156 last if $delay > 5; # for max 5 seconds
157
158 $haenv->sleep(1);
159 }
160
161 return 0;
162}
163
546e2f1f
DM
164sub active_service_count {
165 my ($self) = @_;
166
167 my $haenv = $self->{haenv};
168
169 my $nodename = $haenv->nodename();
170
171 my $ss = $self->{service_status};
172
173 my $count = 0;
174
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';
9c7d068b 182 next if $req_state eq 'freeze';
546e2f1f
DM
183
184 $count++;
185 }
186
187 return $count;
188}
5bd7aa54
DM
189
190my $wrote_lrm_status_at_startup = 0;
191
5f095798
DM
192sub do_one_iteration {
193 my ($self) = @_;
194
195 my $haenv = $self->{haenv};
196
c5ec095f 197 if (!$wrote_lrm_status_at_startup) {
79829202 198 if ($self->update_lrm_status()) {
c5ec095f
DM
199 $wrote_lrm_status_at_startup = 1;
200 } else {
201 # do nothing
202 $haenv->sleep(5);
203 return $self->{shutdown_request} ? 0 : 1;
204 }
5bd7aa54
DM
205 }
206
5f095798
DM
207 my $status = $self->get_local_status();
208 my $state = $status->{state};
209
067cdf33
DM
210 my $ms = $haenv->read_manager_status();
211 $self->{service_status} = $ms->{service_status} || {};
212
49777d09 213 my $fence_request = PVE::HA::Tools::count_fenced_services($self->{service_status}, $haenv->nodename());
067cdf33 214
5f095798
DM
215 # do state changes first
216
217 my $ctime = $haenv->get_time();
218
b0bf08a9 219 if ($state eq 'wait_for_agent_lock') {
5f095798 220
546e2f1f 221 my $service_count = $self->active_service_count();
5f095798 222
067cdf33 223 if (!$fence_request && $service_count && $haenv->quorate()) {
0bba8f60
DM
224 if ($self->get_protected_ha_agent_lock()) {
225 $self->set_local_status({ state => 'active' });
5f095798
DM
226 }
227 }
228
229 } elsif ($state eq 'lost_agent_lock') {
230
067cdf33 231 if (!$fence_request && $haenv->quorate()) {
0bba8f60
DM
232 if ($self->get_protected_ha_agent_lock()) {
233 $self->set_local_status({ state => 'active' });
5f095798
DM
234 }
235 }
236
0bba8f60 237 } elsif ($state eq 'active') {
5f095798 238
067cdf33
DM
239 if ($fence_request) {
240 $haenv->log('err', "node need to be fenced - releasing agent_lock\n");
241 $self->set_local_status({ state => 'lost_agent_lock'});
242 } elsif (!$self->get_protected_ha_agent_lock()) {
5f095798
DM
243 $self->set_local_status({ state => 'lost_agent_lock'});
244 }
245 }
246
247 $status = $self->get_local_status();
248 $state = $status->{state};
249
250 # do work
251
252 if ($state eq 'wait_for_agent_lock') {
253
254 return 0 if $self->{shutdown_request};
79829202
DM
255
256 $self->update_lrm_status();
257
5f095798
DM
258 $haenv->sleep(5);
259
0bba8f60 260 } elsif ($state eq 'active') {
5f095798
DM
261
262 my $startime = $haenv->get_time();
263
264 my $max_time = 10;
265
266 my $shutdown = 0;
267
268 # do work (max_time seconds)
269 eval {
270 # fixme: set alert timer
271
272 if ($self->{shutdown_request}) {
273
499f06e3 274 if ($self->{mode} eq 'restart') {
5f095798 275
499f06e3 276 my $service_count = $self->active_service_count();
5f095798 277
499f06e3 278 if ($service_count == 0) {
5f095798 279
116dea30
DM
280 if ($self->run_workers() == 0) {
281 if ($self->{ha_agent_wd}) {
282 $haenv->watchdog_close($self->{ha_agent_wd});
283 delete $self->{ha_agent_wd};
284 }
285
286 $shutdown = 1;
e23f674c
TL
287
288 # restart with no or freezed services, release the lock
289 $haenv->release_ha_agent_lock();
116dea30
DM
290 }
291 }
292 } else {
293
294 if ($self->run_workers() == 0) {
295 if ($self->{shutdown_errors} == 0) {
296 if ($self->{ha_agent_wd}) {
297 $haenv->watchdog_close($self->{ha_agent_wd});
298 delete $self->{ha_agent_wd};
299 }
0e5b1a43
TL
300
301 # shutdown with all services stopped thus release the lock
302 $haenv->release_ha_agent_lock();
499f06e3 303 }
5f095798 304
499f06e3
DM
305 $shutdown = 1;
306 }
5f095798 307 }
c4a221bc 308 } else {
c4a221bc
DM
309
310 $self->manage_resources();
067cdf33 311
5f095798
DM
312 }
313 };
314 if (my $err = $@) {
315 $haenv->log('err', "got unexpected error - $err");
316 }
317
79829202
DM
318 $self->update_lrm_status();
319
5f095798
DM
320 return 0 if $shutdown;
321
322 $haenv->sleep_until($startime + $max_time);
323
324 } elsif ($state eq 'lost_agent_lock') {
325
326 # Note: watchdog is active an will triger soon!
327
328 # so we hope to get the lock back soon!
329
330 if ($self->{shutdown_request}) {
331
546e2f1f 332 my $service_count = $self->active_service_count();
5f095798 333
546e2f1f 334 if ($service_count > 0) {
5f095798 335 $haenv->log('err', "get shutdown request in state 'lost_agent_lock' - " .
546e2f1f 336 "detected $service_count running services");
5f095798 337
546e2f1f 338 } else {
5f095798 339
546e2f1f 340 # all services are stopped, so we can close the watchdog
5f095798 341
546e2f1f
DM
342 if ($self->{ha_agent_wd}) {
343 $haenv->watchdog_close($self->{ha_agent_wd});
344 delete $self->{ha_agent_wd};
345 }
346
347 return 0;
5f095798 348 }
5f095798
DM
349 }
350
b0bf08a9
DM
351 $haenv->sleep(5);
352
5f095798
DM
353 } else {
354
355 die "got unexpected status '$state'\n";
356
357 }
358
359 return 1;
360}
361
116dea30 362sub run_workers {
c4a221bc
DM
363 my ($self) = @_;
364
365 my $haenv = $self->{haenv};
366
f31b7e94 367 my $starttime = $haenv->get_time();
c4a221bc 368
a28fa330
TL
369 # number of workers to start, if 0 we exec the command directly witouth forking
370 my $max_workers = $haenv->get_max_workers();
c4a221bc 371
6dbf93a0 372 my $sc = $haenv->read_service_config();
f31b7e94
DM
373
374 while (($haenv->get_time() - $starttime) < 5) {
c4a221bc
DM
375 my $count = $self->check_active_workers();
376
377 foreach my $sid (keys %{$self->{workers}}) {
a28fa330
TL
378 last if $count >= $max_workers && $max_workers > 0;
379
c4a221bc
DM
380 my $w = $self->{workers}->{$sid};
381 if (!$w->{pid}) {
a28fa330
TL
382 # only fork if we may else call exec_resource_agent
383 # directly (e.g. for regression tests)
384 if ($max_workers > 0) {
f31b7e94
DM
385 my $pid = fork();
386 if (!defined($pid)) {
387 $haenv->log('err', "fork worker failed");
388 $count = 0; last; # abort, try later
389 } elsif ($pid == 0) {
a2aae08a
TL
390 $haenv->after_fork(); # cleanup
391
f31b7e94
DM
392 # do work
393 my $res = -1;
394 eval {
aaabde6a 395 $res = $self->exec_resource_agent($sid, $sc->{$sid}, $w->{state}, $w->{target});
f31b7e94
DM
396 };
397 if (my $err = $@) {
398 $haenv->log('err', $err);
399 POSIX::_exit(-1);
400 }
401 POSIX::_exit($res);
402 } else {
403 $count++;
404 $w->{pid} = $pid;
405 }
406 } else {
c4a221bc
DM
407 my $res = -1;
408 eval {
aaabde6a 409 $res = $self->exec_resource_agent($sid, $sc->{$sid}, $w->{state}, $w->{target});
b33b5743 410 $res = $res << 8 if $res > 0;
c4a221bc
DM
411 };
412 if (my $err = $@) {
f31b7e94 413 $haenv->log('err', $err);
116dea30
DM
414 }
415 if (defined($w->{uid})) {
416 $self->resource_command_finished($sid, $w->{uid}, $res);
417 } else {
418 $self->stop_command_finished($sid, $res);
419 }
c4a221bc
DM
420 }
421 }
422 }
423
424 last if !$count;
425
f31b7e94 426 $haenv->sleep(1);
c4a221bc 427 }
116dea30
DM
428
429 return scalar(keys %{$self->{workers}});
430}
431
432sub manage_resources {
433 my ($self) = @_;
434
435 my $haenv = $self->{haenv};
436
437 my $nodename = $haenv->nodename();
438
439 my $ss = $self->{service_status};
440
5a28da91
TL
441 foreach my $sid (keys %{$self->{restart_tries}}) {
442 delete $self->{restart_tries}->{$sid} if !$ss->{$sid};
443 }
444
116dea30
DM
445 foreach my $sid (keys %$ss) {
446 my $sd = $ss->{$sid};
447 next if !$sd->{node};
448 next if !$sd->{uid};
449 next if $sd->{node} ne $nodename;
450 my $req_state = $sd->{state};
451 next if !defined($req_state);
452 next if $req_state eq 'freeze';
453 $self->queue_resource_command($sid, $sd->{uid}, $req_state, $sd->{target});
454 }
455
456 return $self->run_workers();
c4a221bc
DM
457}
458
c4a221bc 459sub queue_resource_command {
e88469ba 460 my ($self, $sid, $uid, $state, $target) = @_;
c4a221bc 461
35cbb764
TL
462 # do not queue the excatly same command twice as this may lead to
463 # an inconsistent HA state when the first command fails but the CRM
464 # does not process its failure right away and the LRM starts a second
465 # try, without the CRM knowing of it (race condition)
466 # The 'stopped' command is an exception as we do not process its result
467 # in the CRM and we want to execute it always (even with no active CRM)
468 return if $state ne 'stopped' && $uid && defined($self->{results}->{$uid});
469
c4a221bc
DM
470 if (my $w = $self->{workers}->{$sid}) {
471 return if $w->{pid}; # already started
472 # else, delete and overwrite queue entry with new command
473 delete $self->{workers}->{$sid};
474 }
475
476 $self->{workers}->{$sid} = {
477 sid => $sid,
478 uid => $uid,
479 state => $state,
480 };
e88469ba
DM
481
482 $self->{workers}->{$sid}->{target} = $target if $target;
c4a221bc
DM
483}
484
485sub check_active_workers {
486 my ($self) = @_;
487
488 # finish/count workers
489 my $count = 0;
490 foreach my $sid (keys %{$self->{workers}}) {
491 my $w = $self->{workers}->{$sid};
492 if (my $pid = $w->{pid}) {
493 # check status
494 my $waitpid = waitpid($pid, WNOHANG);
495 if (defined($waitpid) && ($waitpid == $pid)) {
c0edbd7e 496 if (defined($w->{uid})) {
116dea30
DM
497 $self->resource_command_finished($sid, $w->{uid}, $?);
498 } else {
499 $self->stop_command_finished($sid, $?);
500 }
c4a221bc
DM
501 } else {
502 $count++;
503 }
504 }
505 }
506
507 return $count;
508}
509
116dea30
DM
510sub stop_command_finished {
511 my ($self, $sid, $status) = @_;
512
513 my $haenv = $self->{haenv};
514
515 my $w = delete $self->{workers}->{$sid};
516 return if !$w; # should not happen
517
518 my $exit_code = -1;
519
520 if ($status == -1) {
521 $haenv->log('err', "resource agent $sid finished - failed to execute");
522 } elsif (my $sig = ($status & 127)) {
523 $haenv->log('err', "resource agent $sid finished - got signal $sig");
524 } else {
525 $exit_code = ($status >> 8);
526 }
527
528 if ($exit_code != 0) {
529 $self->{shutdown_errors}++;
530 }
531}
532
c4a221bc
DM
533sub resource_command_finished {
534 my ($self, $sid, $uid, $status) = @_;
535
536 my $haenv = $self->{haenv};
537
538 my $w = delete $self->{workers}->{$sid};
539 return if !$w; # should not happen
540
541 my $exit_code = -1;
542
543 if ($status == -1) {
0f70400d 544 $haenv->log('err', "resource agent $sid finished - failed to execute");
c4a221bc 545 } elsif (my $sig = ($status & 127)) {
0f70400d 546 $haenv->log('err', "resource agent $sid finished - got signal $sig");
c4a221bc
DM
547 } else {
548 $exit_code = ($status >> 8);
c4a221bc
DM
549 }
550
ea4443cc
TL
551 $exit_code = $self->handle_service_exitcode($sid, $w->{state}, $exit_code);
552
280ee5d5
DM
553 return if $exit_code == ETRY_AGAIN; # tell nobody, simply retry
554
c4a221bc
DM
555 $self->{results}->{$uid} = {
556 sid => $w->{sid},
557 state => $w->{state},
558 exit_code => $exit_code,
559 };
560
561 my $ss = $self->{service_status};
562
563 # compute hash of valid/existing uids
564 my $valid_uids = {};
565 foreach my $sid (keys %$ss) {
566 my $sd = $ss->{$sid};
567 next if !$sd->{uid};
568 $valid_uids->{$sd->{uid}} = 1;
569 }
570
571 my $results = {};
572 foreach my $id (keys %{$self->{results}}) {
573 next if !$valid_uids->{$id};
574 $results->{$id} = $self->{results}->{$id};
575 }
576 $self->{results} = $results;
c4a221bc
DM
577}
578
ea4443cc
TL
579# processes the exit code from a finished resource agent, so that the CRM knows
580# if the LRM wants to retry an action based on the current recovery policies for
581# the failed service, or the CRM itself must try to recover from the failure.
582sub handle_service_exitcode {
583 my ($self, $sid, $cmd, $exit_code) = @_;
584
585 my $haenv = $self->{haenv};
586 my $tries = $self->{restart_tries};
587
588 my $sc = $haenv->read_service_config();
aaabde6a
DM
589
590 my $max_restart = 0;
591
592 if (my $cd = $sc->{$sid}) {
593 $max_restart = $cd->{max_restart};
594 }
ea4443cc
TL
595
596 if ($cmd eq 'started') {
597
a89ff919 598 if ($exit_code == SUCCESS) {
ea4443cc
TL
599
600 $tries->{$sid} = 0;
601
602 return $exit_code;
603
a89ff919 604 } elsif ($exit_code == ERROR) {
ea4443cc
TL
605
606 $tries->{$sid} = 0 if !defined($tries->{$sid});
607
aaabde6a 608 if ($tries->{$sid} >= $max_restart) {
ea4443cc
TL
609 $haenv->log('err', "unable to start service $sid on local node".
610 " after $tries->{$sid} retries");
611 $tries->{$sid} = 0;
a89ff919 612 return ERROR;
ea4443cc
TL
613 }
614
e9e1cd68
TL
615 $tries->{$sid}++;
616
617 $haenv->log('warning', "restart policy: retry number $tries->{$sid}" .
618 " for service '$sid'");
a89ff919
TL
619 # tell CRM that we retry the start
620 return ETRY_AGAIN;
ea4443cc
TL
621 }
622 }
623
624 return $exit_code;
625
626}
627
2a045f55
TL
628sub exec_resource_agent {
629 my ($self, $sid, $service_config, $cmd, @params) = @_;
630
631 # setup execution environment
632
633 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
634
2a045f55
TL
635 my $haenv = $self->{haenv};
636
637 my $nodename = $haenv->nodename();
638
639 my (undef, $service_type, $service_name) = PVE::HA::Tools::parse_sid($sid);
640
641 my $plugin = PVE::HA::Resources->lookup($service_type);
642 if (!$plugin) {
643 $haenv->log('err', "service type '$service_type' not implemented");
644 return EUNKNOWN_SERVICE_TYPE;
645 }
646
aaabde6a
DM
647 if (!$service_config) {
648 $haenv->log('err', "missing resource configuration for '$sid'");
649 return EUNKNOWN_SERVICE;
650 }
651
d338a56f
TL
652 # process error state early
653 if ($cmd eq 'error') {
654
655 $haenv->log('err', "service $sid is in an error state and needs manual " .
656 "intervention. Look up 'ERROR RECOVERY' in the documentation.");
657
658 return SUCCESS; # error always succeeds
659 }
660
2a045f55
TL
661 if ($service_config->{node} ne $nodename) {
662 $haenv->log('err', "service '$sid' not on this node");
663 return EWRONG_NODE;
664 }
665
666 my $id = $service_name;
667
668 my $running = $plugin->check_running($haenv, $id);
669
670 if ($cmd eq 'started') {
671
672 return SUCCESS if $running;
673
674 $haenv->log("info", "starting service $sid");
675
676 $plugin->start($haenv, $id);
677
678 $running = $plugin->check_running($haenv, $id);
679
680 if ($running) {
681 $haenv->log("info", "service status $sid started");
682 return SUCCESS;
683 } else {
684 $haenv->log("warning", "unable to start service $sid");
685 return ERROR;
686 }
687
688 } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') {
689
690 return SUCCESS if !$running;
691
692 $haenv->log("info", "stopping service $sid");
693
694 $plugin->shutdown($haenv, $id);
695
696 $running = $plugin->check_running($haenv, $id);
697
698 if (!$running) {
699 $haenv->log("info", "service status $sid stopped");
700 return SUCCESS;
701 } else {
702 $haenv->log("info", "unable to stop stop service $sid (still running)");
703 return ERROR;
704 }
705
706 } elsif ($cmd eq 'migrate' || $cmd eq 'relocate') {
707
708 my $target = $params[0];
709 if (!defined($target)) {
710 die "$cmd '$sid' failed - missing target\n" if !defined($target);
711 return EINVALID_PARAMETER;
712 }
713
714 if ($service_config->{node} eq $target) {
715 # already there
716 return SUCCESS;
717 }
718
719 my $online = ($cmd eq 'migrate') ? 1 : 0;
720
ea28f873 721 my $res = $plugin->migrate($haenv, $id, $target, $online);
2a045f55
TL
722
723 # something went wrong if service is still on this node
ea28f873 724 if (!$res) {
2a045f55
TL
725 $haenv->log("err", "service $sid not moved (migration error)");
726 return ERROR;
727 }
728
729 return SUCCESS;
730
2a045f55
TL
731 }
732
733 $haenv->log("err", "implement me (cmd '$cmd')");
734 return EUNKNOWN_COMMAND;
735}
736
737
5f095798 7381;