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