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