]> git.proxmox.com Git - pve-ha-manager.git/blame - src/PVE/HA/LRM.pm
LRM: release lock and close watchdog if no service configured for >10min
[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 7use POSIX qw(:sys_wait_h);
5f095798
DM
8
9use PVE::SafeSyslog;
10use PVE::Tools;
a89ff919 11use PVE::HA::Tools ':exit_codes';
2a045f55 12use PVE::HA::Resources;
5f095798
DM
13
14# Server can have several states:
15
16my $valid_states = {
ec911edd 17 wait_for_agent_lock => "waiting for agent lock",
0bba8f60 18 active => "got agent_lock",
99278e06 19 maintenance => "going into maintenance",
5f095798
DM
20 lost_agent_lock => "lost agent_lock",
21};
22
21051707
TL
23# we sleep ~10s per 'active' round, so if no services is available for >= 10 min we'd go in wait
24# state givining up the watchdog and the LRM lock acquire voluntary, ensuring the WD can do no harm
25my $max_active_idle_rounds = 60;
26
5f095798
DM
27sub new {
28 my ($this, $haenv) = @_;
29
30 my $class = ref($this) || $this;
31
32 my $self = bless {
33 haenv => $haenv,
34 status => { state => 'startup' },
c4a221bc
DM
35 workers => {},
36 results => {},
ea4443cc 37 restart_tries => {},
067cdf33 38 shutdown_request => 0,
116dea30 39 shutdown_errors => 0,
9c7d068b
DM
40 # mode can be: active, reboot, shutdown, restart
41 mode => 'active',
3df15380 42 cluster_state_update => 0,
21051707 43 active_idle_rounds => 0,
5f095798
DM
44 }, $class;
45
289e4784 46 $self->set_local_status({ state => 'wait_for_agent_lock' });
9c7d068b 47
5f095798
DM
48 return $self;
49}
50
51sub shutdown_request {
52 my ($self) = @_;
53
f1be5b3a
DM
54 return if $self->{shutdown_request}; # already in shutdown mode
55
499f06e3
DM
56 my $haenv = $self->{haenv};
57
116dea30
DM
58 my $nodename = $haenv->nodename();
59
f65f41b9 60 my ($shutdown, $reboot) = $haenv->is_node_shutdown();
499f06e3 61
ba15a9b9
TL
62 my $dc_ha_cfg = $haenv->get_ha_settings();
63 my $shutdown_policy = $dc_ha_cfg->{shutdown_policy} // 'conditional';
64
7a20d688
TL
65 if ($shutdown) { # don't log this on service restart, only on node shutdown
66 $haenv->log('info', "got shutdown request with shutdown policy '$shutdown_policy'");
67 }
68
d2236278 69 my $freeze_all;
99278e06 70 my $maintenance;
ba15a9b9
TL
71 if ($shutdown_policy eq 'conditional') {
72 $freeze_all = $reboot;
73 } elsif ($shutdown_policy eq 'freeze') {
74 $freeze_all = 1;
75 } elsif ($shutdown_policy eq 'failover') {
76 $freeze_all = 0;
99278e06
TL
77 } elsif ($shutdown_policy eq 'migrate') {
78 $maintenance = 1;
ba15a9b9 79 } else {
d2236278
TL
80 $haenv->log('err', "unknown shutdown policy '$shutdown_policy', fall back to conditional");
81 $freeze_all = $reboot;
ba15a9b9
TL
82 }
83
99278e06
TL
84 if ($maintenance) {
85 # we get marked as unaivalable by the manager, then all services will
86 # be migrated away, we'll still have the same "can we exit" clause than
87 # a normal shutdown -> no running service on this node
88 # FIXME: after X minutes, add shutdown command for remaining services,
89 # e.g., if they have no alternative node???
90 } elsif ($shutdown) {
f65f41b9
TL
91 # *always* queue stop jobs for all services if the node shuts down,
92 # independent if it's a reboot or a poweroff, else we may corrupt
93 # services or hinder node shutdown
116dea30
DM
94 my $ss = $self->{service_status};
95
96 foreach my $sid (keys %$ss) {
97 my $sd = $ss->{$sid};
98 next if !$sd->{node};
99 next if $sd->{node} ne $nodename;
c0edbd7e 100 # Note: use undef uid to mark shutdown/stop jobs
116dea30
DM
101 $self->queue_resource_command($sid, undef, 'request_stop');
102 }
f65f41b9 103 }
116dea30 104
f65f41b9 105 if ($shutdown) {
41236dcf 106 my $shutdown_type = $reboot ? 'reboot' : 'shutdown';
99278e06
TL
107 if ($maintenance) {
108 $haenv->log('info', "$shutdown_type LRM, doing maintenance, removing this node from active list");
109 $self->{mode} = 'maintenance';
110 } elsif ($freeze_all) {
41236dcf 111 $haenv->log('info', "$shutdown_type LRM, stop and freeze all services");
f65f41b9
TL
112 $self->{mode} = 'restart';
113 } else {
114 $haenv->log('info', "shutdown LRM, stop all services");
115 $self->{mode} = 'shutdown';
116 }
499f06e3
DM
117 } else {
118 $haenv->log('info', "restart LRM, freeze all services");
119 $self->{mode} = 'restart';
120 }
9c7d068b 121
99278e06 122 $self->{shutdown_request} = $haenv->get_time();
9c7d068b 123
a19f2576 124 eval { $self->update_lrm_status() or die "not quorate?\n"; };
9c7d068b 125 if (my $err = $@) {
5bd7aa54 126 $self->log('err', "unable to update lrm status file - $err");
9c7d068b 127 }
5f095798
DM
128}
129
130sub get_local_status {
131 my ($self) = @_;
132
133 return $self->{status};
134}
135
136sub set_local_status {
137 my ($self, $new) = @_;
138
139 die "invalid state '$new->{state}'" if !$valid_states->{$new->{state}};
140
141 my $haenv = $self->{haenv};
142
143 my $old = $self->{status};
144
289e4784 145 # important: only update if if really changed
5f095798
DM
146 return if $old->{state} eq $new->{state};
147
0bba8f60 148 $haenv->log('info', "status change $old->{state} => $new->{state}");
5f095798
DM
149
150 $new->{state_change_time} = $haenv->get_time();
151
152 $self->{status} = $new;
153}
154
9c7d068b
DM
155sub update_lrm_status {
156 my ($self) = @_;
157
5bd7aa54
DM
158 my $haenv = $self->{haenv};
159
79829202 160 return 0 if !$haenv->quorate();
289e4784
TL
161
162 my $lrm_status = {
331a9f00 163 state => $self->{status}->{state},
9c7d068b
DM
164 mode => $self->{mode},
165 results => $self->{results},
aa330d1c 166 timestamp => $haenv->get_time(),
9c7d068b 167 };
289e4784 168
5bd7aa54
DM
169 eval { $haenv->write_lrm_status($lrm_status); };
170 if (my $err = $@) {
171 $haenv->log('err', "unable to write lrm status file - $err");
172 return 0;
173 }
174
175 return 1;
9c7d068b
DM
176}
177
8e940b68
TL
178sub update_service_status {
179 my ($self) = @_;
180
181 my $haenv = $self->{haenv};
182
183 my $ms = eval { $haenv->read_manager_status(); };
184 if (my $err = $@) {
185 $haenv->log('err', "updating service status from manager failed: $err");
186 return undef;
187 } else {
188 $self->{service_status} = $ms->{service_status} || {};
189 return 1;
190 }
191}
192
5f095798
DM
193sub get_protected_ha_agent_lock {
194 my ($self) = @_;
195
196 my $haenv = $self->{haenv};
197
198 my $count = 0;
199 my $starttime = $haenv->get_time();
200
201 for (;;) {
289e4784 202
5f095798
DM
203 if ($haenv->get_ha_agent_lock()) {
204 if ($self->{ha_agent_wd}) {
205 $haenv->watchdog_update($self->{ha_agent_wd});
206 } else {
207 my $wfh = $haenv->watchdog_open();
208 $self->{ha_agent_wd} = $wfh;
209 }
210 return 1;
211 }
289e4784 212
5f095798
DM
213 last if ++$count > 5; # try max 5 time
214
215 my $delay = $haenv->get_time() - $starttime;
216 last if $delay > 5; # for max 5 seconds
217
218 $haenv->sleep(1);
219 }
289e4784 220
5f095798
DM
221 return 0;
222}
223
21051707
TL
224# only cares if any service has the local node as their node, independent of which req.state it is
225sub has_configured_service_on_local_node {
226 my ($self) = @_;
227
228 my $haenv = $self->{haenv};
229 my $nodename = $haenv->nodename();
230
231 my $ss = $self->{service_status};
232 foreach my $sid (keys %$ss) {
233 my $sd = $ss->{$sid};
234 next if !$sd->{node} || $sd->{node} ne $nodename;
235
236 return 1;
237 }
238 return 0;
239}
240
546e2f1f
DM
241sub active_service_count {
242 my ($self) = @_;
289e4784 243
546e2f1f
DM
244 my $haenv = $self->{haenv};
245
246 my $nodename = $haenv->nodename();
247
248 my $ss = $self->{service_status};
249
250 my $count = 0;
289e4784 251
546e2f1f
DM
252 foreach my $sid (keys %$ss) {
253 my $sd = $ss->{$sid};
254 next if !$sd->{node};
255 next if $sd->{node} ne $nodename;
256 my $req_state = $sd->{state};
257 next if !defined($req_state);
258 next if $req_state eq 'stopped';
9c7d068b 259 next if $req_state eq 'freeze';
38545741
TL
260 # erroneous services are not managed by HA, don't count them as active
261 next if $req_state eq 'error';
546e2f1f
DM
262
263 $count++;
264 }
289e4784 265
546e2f1f
DM
266 return $count;
267}
5bd7aa54
DM
268
269my $wrote_lrm_status_at_startup = 0;
270
5f095798
DM
271sub do_one_iteration {
272 my ($self) = @_;
273
274 my $haenv = $self->{haenv};
275
da6f0416
TL
276 $haenv->loop_start_hook();
277
3df15380
TL
278 $self->{cluster_state_update} = $haenv->cluster_state_update();
279
da6f0416
TL
280 my $res = $self->work();
281
282 $haenv->loop_end_hook();
283
284 return $res;
285}
286
abc1499b
TL
287# NOTE: this is disabling the self-fence mechanism, so it must NOT be called with active services
288# It's normally *only* OK on graceful shutdown (with no services, or all services frozen)
289my sub give_up_watchdog_protection {
290 my ($self) = @_;
291
292 if ($self->{ha_agent_wd}) {
293 $self->{haenv}->watchdog_close($self->{ha_agent_wd});
294 delete $self->{ha_agent_wd}; # only delete after close!
295 }
296}
297
da6f0416
TL
298sub work {
299 my ($self) = @_;
300
301 my $haenv = $self->{haenv};
302
c5ec095f 303 if (!$wrote_lrm_status_at_startup) {
79829202 304 if ($self->update_lrm_status()) {
c5ec095f
DM
305 $wrote_lrm_status_at_startup = 1;
306 } else {
307 # do nothing
308 $haenv->sleep(5);
309 return $self->{shutdown_request} ? 0 : 1;
310 }
5bd7aa54 311 }
289e4784 312
5f095798
DM
313 my $status = $self->get_local_status();
314 my $state = $status->{state};
315
8e940b68 316 $self->update_service_status();
067cdf33 317
49777d09 318 my $fence_request = PVE::HA::Tools::count_fenced_services($self->{service_status}, $haenv->nodename());
289e4784
TL
319
320 # do state changes first
5f095798
DM
321
322 my $ctime = $haenv->get_time();
323
b0bf08a9 324 if ($state eq 'wait_for_agent_lock') {
5f095798 325
546e2f1f 326 my $service_count = $self->active_service_count();
5f095798 327
067cdf33 328 if (!$fence_request && $service_count && $haenv->quorate()) {
0bba8f60
DM
329 if ($self->get_protected_ha_agent_lock()) {
330 $self->set_local_status({ state => 'active' });
5f095798
DM
331 }
332 }
289e4784 333
5f095798
DM
334 } elsif ($state eq 'lost_agent_lock') {
335
067cdf33 336 if (!$fence_request && $haenv->quorate()) {
0bba8f60
DM
337 if ($self->get_protected_ha_agent_lock()) {
338 $self->set_local_status({ state => 'active' });
5f095798
DM
339 }
340 }
341
0bba8f60 342 } elsif ($state eq 'active') {
5f095798 343
289e4784 344 if ($fence_request) {
067cdf33 345 $haenv->log('err', "node need to be fenced - releasing agent_lock\n");
289e4784 346 $self->set_local_status({ state => 'lost_agent_lock'});
067cdf33 347 } elsif (!$self->get_protected_ha_agent_lock()) {
5f095798 348 $self->set_local_status({ state => 'lost_agent_lock'});
99278e06
TL
349 } elsif ($self->{mode} eq 'maintenance') {
350 $self->set_local_status({ state => 'maintenance'});
21051707
TL
351 } else {
352 if (!$self->has_configured_service_on_local_node() && !$self->run_workers()) {
353 # no active service configured for this node and all (old) workers are done
354 $self->{active_idle_rounds}++;
355 if ($self->{active_idle_rounds} > $max_active_idle_rounds) {
356 $haenv->log('info', "node had no service configured for $max_active_idle_rounds rounds, going idle.\n");
357 # safety: no active service & no running worker for quite some time -> OK
358 $haenv->release_ha_agent_lock();
359 give_up_watchdog_protection($self);
360 $self->set_local_status({ state => 'wait_for_agent_lock'});
361 $self->{active_idle_rounds} = 0;
362 }
363 } elsif ($self->{active_idle_rounds}) {
364 $self->{active_idle_rounds} = 0;
365 }
99278e06
TL
366 }
367 } elsif ($state eq 'maintenance') {
368
369 if ($fence_request) {
370 $haenv->log('err', "node need to be fenced during maintenance mode - releasing agent_lock\n");
371 $self->set_local_status({ state => 'lost_agent_lock'});
372 } elsif (!$self->get_protected_ha_agent_lock()) {
373 $self->set_local_status({ state => 'lost_agent_lock'});
5f095798
DM
374 }
375 }
376
377 $status = $self->get_local_status();
378 $state = $status->{state};
379
380 # do work
381
382 if ($state eq 'wait_for_agent_lock') {
383
384 return 0 if $self->{shutdown_request};
289e4784 385
79829202 386 $self->update_lrm_status();
289e4784 387
5f095798 388 $haenv->sleep(5);
289e4784 389
0bba8f60 390 } elsif ($state eq 'active') {
5f095798
DM
391
392 my $startime = $haenv->get_time();
393
394 my $max_time = 10;
395
396 my $shutdown = 0;
397
398 # do work (max_time seconds)
399 eval {
400 # fixme: set alert timer
401
8e940b68
TL
402 # if we could not get the current service status there's no point
403 # in doing anything, try again next round.
404 return if !$self->update_service_status();
405
5f095798
DM
406 if ($self->{shutdown_request}) {
407
499f06e3 408 if ($self->{mode} eq 'restart') {
5f095798 409
499f06e3 410 my $service_count = $self->active_service_count();
5f095798 411
499f06e3 412 if ($service_count == 0) {
116dea30 413 if ($self->run_workers() == 0) {
abc1499b
TL
414 # safety: no active services or workers -> OK
415 give_up_watchdog_protection($self);
116dea30 416 $shutdown = 1;
e23f674c
TL
417
418 # restart with no or freezed services, release the lock
419 $haenv->release_ha_agent_lock();
116dea30
DM
420 }
421 }
422 } else {
423
424 if ($self->run_workers() == 0) {
425 if ($self->{shutdown_errors} == 0) {
abc1499b
TL
426 # safety: no active services and LRM shutdown -> OK
427 give_up_watchdog_protection($self);
0e5b1a43
TL
428
429 # shutdown with all services stopped thus release the lock
430 $haenv->release_ha_agent_lock();
499f06e3 431 }
5f095798 432
499f06e3
DM
433 $shutdown = 1;
434 }
5f095798 435 }
c4a221bc 436 } else {
724bd3f3
TL
437 if (!$self->{cluster_state_update}) {
438 # update failed but we could still renew our lock (cfs restart?),
439 # safely skip manage and expect to update just fine next round
440 $haenv->log('notice', "temporary inconsistent cluster state " .
441 "(cfs restart?), skip round");
442 return;
443 }
c4a221bc
DM
444
445 $self->manage_resources();
067cdf33 446
5f095798
DM
447 }
448 };
449 if (my $err = $@) {
450 $haenv->log('err', "got unexpected error - $err");
451 }
452
79829202 453 $self->update_lrm_status();
289e4784 454
5f095798
DM
455 return 0 if $shutdown;
456
457 $haenv->sleep_until($startime + $max_time);
458
459 } elsif ($state eq 'lost_agent_lock') {
289e4784 460
abc1499b 461 # NOTE: watchdog is active an will trigger soon!
5f095798 462 # so we hope to get the lock back soon!
5f095798
DM
463 if ($self->{shutdown_request}) {
464
546e2f1f 465 my $service_count = $self->active_service_count();
5f095798 466
546e2f1f 467 if ($service_count > 0) {
289e4784 468 $haenv->log('err', "get shutdown request in state 'lost_agent_lock' - " .
546e2f1f 469 "detected $service_count running services");
5f095798 470
c5c7faf6
TL
471 if ($self->{mode} eq 'restart') {
472 my $state_mt = $self->{status}->{state_change_time};
473
474 # watchdog should have already triggered, so either it's set
475 # set to noboot or it failed. As we are in restart mode, and
476 # have infinity stoptimeout -> exit now - we don't touch services
477 # or change state, so this is save, relatively speaking
478 if (($haenv->get_time() - $state_mt) > 90) {
479 $haenv->log('err', "lost agent lock and restart request for over 90 seconds - giving up!");
480 return 0;
481 }
482 }
546e2f1f 483 } else {
abc1499b
TL
484 # safety: all services are stopped, so we can close the watchdog
485 give_up_watchdog_protection($self);
289e4784 486
546e2f1f 487 return 0;
5f095798 488 }
5f095798
DM
489 }
490
b0bf08a9
DM
491 $haenv->sleep(5);
492
99278e06
TL
493 } elsif ($state eq 'maintenance') {
494
495 my $startime = $haenv->get_time();
496 return if !$self->update_service_status();
497
498 # wait until all active services moved away
499 my $service_count = $self->active_service_count();
500
501 my $exit_lrm = 0;
502
503 if ($self->{shutdown_request}) {
504 if ($service_count == 0 && $self->run_workers() == 0) {
abc1499b
TL
505 # safety: going into maintenance and all active services got moved -> OK
506 give_up_watchdog_protection($self);
99278e06
TL
507
508 $exit_lrm = 1;
509
510 # restart with no or freezed services, release the lock
511 $haenv->release_ha_agent_lock();
512 }
513 }
514
515 $self->manage_resources() if !$exit_lrm;
516
517 $self->update_lrm_status();
518
519 return 0 if $exit_lrm;
520
521 $haenv->sleep_until($startime + 5);
522
5f095798
DM
523 } else {
524
525 die "got unexpected status '$state'\n";
526
527 }
528
529 return 1;
530}
531
116dea30 532sub run_workers {
c4a221bc
DM
533 my ($self) = @_;
534
535 my $haenv = $self->{haenv};
536
f31b7e94 537 my $starttime = $haenv->get_time();
c4a221bc 538
a28fa330
TL
539 # number of workers to start, if 0 we exec the command directly witouth forking
540 my $max_workers = $haenv->get_max_workers();
c4a221bc 541
6dbf93a0 542 my $sc = $haenv->read_service_config();
f31b7e94
DM
543
544 while (($haenv->get_time() - $starttime) < 5) {
c4a221bc
DM
545 my $count = $self->check_active_workers();
546
a5e4bef4 547 foreach my $sid (sort keys %{$self->{workers}}) {
a28fa330
TL
548 last if $count >= $max_workers && $max_workers > 0;
549
c4a221bc
DM
550 my $w = $self->{workers}->{$sid};
551 if (!$w->{pid}) {
a28fa330
TL
552 # only fork if we may else call exec_resource_agent
553 # directly (e.g. for regression tests)
554 if ($max_workers > 0) {
f31b7e94
DM
555 my $pid = fork();
556 if (!defined($pid)) {
557 $haenv->log('err', "fork worker failed");
558 $count = 0; last; # abort, try later
559 } elsif ($pid == 0) {
a2aae08a
TL
560 $haenv->after_fork(); # cleanup
561
f31b7e94
DM
562 # do work
563 my $res = -1;
564 eval {
3ac1ee6b 565 $res = $self->exec_resource_agent($sid, $sc->{$sid}, $w->{state}, $w->{params});
f31b7e94
DM
566 };
567 if (my $err = $@) {
568 $haenv->log('err', $err);
569 POSIX::_exit(-1);
289e4784
TL
570 }
571 POSIX::_exit($res);
f31b7e94
DM
572 } else {
573 $count++;
574 $w->{pid} = $pid;
575 }
576 } else {
c4a221bc
DM
577 my $res = -1;
578 eval {
3ac1ee6b 579 $res = $self->exec_resource_agent($sid, $sc->{$sid}, $w->{state}, $w->{params});
b33b5743 580 $res = $res << 8 if $res > 0;
c4a221bc
DM
581 };
582 if (my $err = $@) {
f31b7e94 583 $haenv->log('err', $err);
116dea30
DM
584 }
585 if (defined($w->{uid})) {
586 $self->resource_command_finished($sid, $w->{uid}, $res);
587 } else {
588 $self->stop_command_finished($sid, $res);
589 }
c4a221bc
DM
590 }
591 }
592 }
593
594 last if !$count;
595
f31b7e94 596 $haenv->sleep(1);
c4a221bc 597 }
116dea30
DM
598
599 return scalar(keys %{$self->{workers}});
600}
601
602sub manage_resources {
603 my ($self) = @_;
604
605 my $haenv = $self->{haenv};
606
607 my $nodename = $haenv->nodename();
608
609 my $ss = $self->{service_status};
610
5a28da91
TL
611 foreach my $sid (keys %{$self->{restart_tries}}) {
612 delete $self->{restart_tries}->{$sid} if !$ss->{$sid};
613 }
614
116dea30
DM
615 foreach my $sid (keys %$ss) {
616 my $sd = $ss->{$sid};
617 next if !$sd->{node};
618 next if !$sd->{uid};
619 next if $sd->{node} ne $nodename;
620 my $req_state = $sd->{state};
621 next if !defined($req_state);
622 next if $req_state eq 'freeze';
e4ef317d 623 $self->queue_resource_command($sid, $sd->{uid}, $req_state, {'target' => $sd->{target}, 'timeout' => $sd->{timeout}});
116dea30
DM
624 }
625
626 return $self->run_workers();
c4a221bc
DM
627}
628
c4a221bc 629sub queue_resource_command {
3ac1ee6b 630 my ($self, $sid, $uid, $state, $params) = @_;
c4a221bc 631
35cbb764
TL
632 # do not queue the excatly same command twice as this may lead to
633 # an inconsistent HA state when the first command fails but the CRM
634 # does not process its failure right away and the LRM starts a second
635 # try, without the CRM knowing of it (race condition)
636 # The 'stopped' command is an exception as we do not process its result
637 # in the CRM and we want to execute it always (even with no active CRM)
638 return if $state ne 'stopped' && $uid && defined($self->{results}->{$uid});
639
c4a221bc
DM
640 if (my $w = $self->{workers}->{$sid}) {
641 return if $w->{pid}; # already started
642 # else, delete and overwrite queue entry with new command
643 delete $self->{workers}->{$sid};
644 }
645
646 $self->{workers}->{$sid} = {
647 sid => $sid,
648 uid => $uid,
649 state => $state,
650 };
e88469ba 651
3ac1ee6b 652 $self->{workers}->{$sid}->{params} = $params if $params;
c4a221bc
DM
653}
654
655sub check_active_workers {
656 my ($self) = @_;
657
658 # finish/count workers
659 my $count = 0;
660 foreach my $sid (keys %{$self->{workers}}) {
661 my $w = $self->{workers}->{$sid};
662 if (my $pid = $w->{pid}) {
663 # check status
664 my $waitpid = waitpid($pid, WNOHANG);
665 if (defined($waitpid) && ($waitpid == $pid)) {
c0edbd7e 666 if (defined($w->{uid})) {
116dea30
DM
667 $self->resource_command_finished($sid, $w->{uid}, $?);
668 } else {
669 $self->stop_command_finished($sid, $?);
670 }
c4a221bc
DM
671 } else {
672 $count++;
673 }
674 }
675 }
289e4784 676
c4a221bc
DM
677 return $count;
678}
679
116dea30
DM
680sub stop_command_finished {
681 my ($self, $sid, $status) = @_;
682
683 my $haenv = $self->{haenv};
684
685 my $w = delete $self->{workers}->{$sid};
686 return if !$w; # should not happen
687
688 my $exit_code = -1;
689
690 if ($status == -1) {
691 $haenv->log('err', "resource agent $sid finished - failed to execute");
692 } elsif (my $sig = ($status & 127)) {
693 $haenv->log('err', "resource agent $sid finished - got signal $sig");
694 } else {
695 $exit_code = ($status >> 8);
696 }
697
698 if ($exit_code != 0) {
699 $self->{shutdown_errors}++;
700 }
701}
702
c4a221bc
DM
703sub resource_command_finished {
704 my ($self, $sid, $uid, $status) = @_;
705
706 my $haenv = $self->{haenv};
707
708 my $w = delete $self->{workers}->{$sid};
709 return if !$w; # should not happen
710
711 my $exit_code = -1;
712
713 if ($status == -1) {
289e4784 714 $haenv->log('err', "resource agent $sid finished - failed to execute");
c4a221bc 715 } elsif (my $sig = ($status & 127)) {
0f70400d 716 $haenv->log('err', "resource agent $sid finished - got signal $sig");
c4a221bc
DM
717 } else {
718 $exit_code = ($status >> 8);
c4a221bc
DM
719 }
720
ea4443cc
TL
721 $exit_code = $self->handle_service_exitcode($sid, $w->{state}, $exit_code);
722
280ee5d5
DM
723 return if $exit_code == ETRY_AGAIN; # tell nobody, simply retry
724
c4a221bc
DM
725 $self->{results}->{$uid} = {
726 sid => $w->{sid},
727 state => $w->{state},
728 exit_code => $exit_code,
729 };
730
731 my $ss = $self->{service_status};
732
733 # compute hash of valid/existing uids
734 my $valid_uids = {};
735 foreach my $sid (keys %$ss) {
736 my $sd = $ss->{$sid};
737 next if !$sd->{uid};
738 $valid_uids->{$sd->{uid}} = 1;
739 }
740
741 my $results = {};
742 foreach my $id (keys %{$self->{results}}) {
743 next if !$valid_uids->{$id};
744 $results->{$id} = $self->{results}->{$id};
745 }
746 $self->{results} = $results;
c4a221bc
DM
747}
748
ea4443cc
TL
749# processes the exit code from a finished resource agent, so that the CRM knows
750# if the LRM wants to retry an action based on the current recovery policies for
751# the failed service, or the CRM itself must try to recover from the failure.
752sub handle_service_exitcode {
753 my ($self, $sid, $cmd, $exit_code) = @_;
754
755 my $haenv = $self->{haenv};
756 my $tries = $self->{restart_tries};
757
758 my $sc = $haenv->read_service_config();
aaabde6a
DM
759
760 my $max_restart = 0;
761
762 if (my $cd = $sc->{$sid}) {
763 $max_restart = $cd->{max_restart};
764 }
ea4443cc
TL
765
766 if ($cmd eq 'started') {
767
a89ff919 768 if ($exit_code == SUCCESS) {
ea4443cc
TL
769
770 $tries->{$sid} = 0;
771
772 return $exit_code;
773
a89ff919 774 } elsif ($exit_code == ERROR) {
ea4443cc
TL
775
776 $tries->{$sid} = 0 if !defined($tries->{$sid});
777
aaabde6a 778 if ($tries->{$sid} >= $max_restart) {
ea4443cc
TL
779 $haenv->log('err', "unable to start service $sid on local node".
780 " after $tries->{$sid} retries");
781 $tries->{$sid} = 0;
a89ff919 782 return ERROR;
ea4443cc
TL
783 }
784
e9e1cd68
TL
785 $tries->{$sid}++;
786
787 $haenv->log('warning', "restart policy: retry number $tries->{$sid}" .
788 " for service '$sid'");
a89ff919
TL
789 # tell CRM that we retry the start
790 return ETRY_AGAIN;
ea4443cc
TL
791 }
792 }
793
794 return $exit_code;
795
796}
797
2a045f55 798sub exec_resource_agent {
3ac1ee6b 799 my ($self, $sid, $service_config, $cmd, $params) = @_;
2a045f55
TL
800
801 # setup execution environment
802
803 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
804
2a045f55
TL
805 my $haenv = $self->{haenv};
806
807 my $nodename = $haenv->nodename();
808
0087839a 809 my (undef, $service_type, $service_name) = $haenv->parse_sid($sid);
2a045f55
TL
810
811 my $plugin = PVE::HA::Resources->lookup($service_type);
812 if (!$plugin) {
813 $haenv->log('err', "service type '$service_type' not implemented");
814 return EUNKNOWN_SERVICE_TYPE;
815 }
816
aaabde6a
DM
817 if (!$service_config) {
818 $haenv->log('err', "missing resource configuration for '$sid'");
819 return EUNKNOWN_SERVICE;
820 }
821
d338a56f
TL
822 # process error state early
823 if ($cmd eq 'error') {
824
825 $haenv->log('err', "service $sid is in an error state and needs manual " .
826 "intervention. Look up 'ERROR RECOVERY' in the documentation.");
827
828 return SUCCESS; # error always succeeds
829 }
830
2a045f55
TL
831 if ($service_config->{node} ne $nodename) {
832 $haenv->log('err', "service '$sid' not on this node");
833 return EWRONG_NODE;
834 }
835
836 my $id = $service_name;
837
838 my $running = $plugin->check_running($haenv, $id);
839
840 if ($cmd eq 'started') {
841
842 return SUCCESS if $running;
843
844 $haenv->log("info", "starting service $sid");
845
846 $plugin->start($haenv, $id);
847
848 $running = $plugin->check_running($haenv, $id);
849
850 if ($running) {
851 $haenv->log("info", "service status $sid started");
852 return SUCCESS;
853 } else {
854 $haenv->log("warning", "unable to start service $sid");
855 return ERROR;
856 }
857
858 } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') {
859
860 return SUCCESS if !$running;
861
e4ef317d
FE
862 if (defined($params->{timeout})) {
863 $haenv->log("info", "stopping service $sid (timeout=$params->{timeout})");
864 } else {
865 $haenv->log("info", "stopping service $sid");
866 }
2a045f55 867
e4ef317d 868 $plugin->shutdown($haenv, $id, $params->{timeout});
2a045f55
TL
869
870 $running = $plugin->check_running($haenv, $id);
871
872 if (!$running) {
873 $haenv->log("info", "service status $sid stopped");
874 return SUCCESS;
875 } else {
876 $haenv->log("info", "unable to stop stop service $sid (still running)");
877 return ERROR;
878 }
879
880 } elsif ($cmd eq 'migrate' || $cmd eq 'relocate') {
881
3ac1ee6b 882 my $target = $params->{target};
2a045f55
TL
883 if (!defined($target)) {
884 die "$cmd '$sid' failed - missing target\n" if !defined($target);
885 return EINVALID_PARAMETER;
886 }
887
888 if ($service_config->{node} eq $target) {
889 # already there
890 return SUCCESS;
891 }
892
893 my $online = ($cmd eq 'migrate') ? 1 : 0;
894
ea28f873 895 my $res = $plugin->migrate($haenv, $id, $target, $online);
2a045f55
TL
896
897 # something went wrong if service is still on this node
ea28f873 898 if (!$res) {
2a045f55
TL
899 $haenv->log("err", "service $sid not moved (migration error)");
900 return ERROR;
901 }
902
903 return SUCCESS;
904
2a045f55
TL
905 }
906
907 $haenv->log("err", "implement me (cmd '$cmd')");
908 return EUNKNOWN_COMMAND;
909}
910
911
5f095798 9121;