]>
Commit | Line | Data |
---|---|---|
5f095798 DM |
1 | package PVE::HA::LRM; |
2 | ||
3 | # Local Resource Manager | |
4 | ||
5 | use strict; | |
6 | use warnings; | |
c4a221bc DM |
7 | use Data::Dumper; |
8 | use POSIX qw(:sys_wait_h); | |
5f095798 DM |
9 | |
10 | use PVE::SafeSyslog; | |
11 | use PVE::Tools; | |
12 | use PVE::HA::Tools; | |
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 => {}, | |
067cdf33 | 32 | shutdown_request => 0, |
9c7d068b DM |
33 | # mode can be: active, reboot, shutdown, restart |
34 | mode => 'active', | |
5f095798 DM |
35 | }, $class; |
36 | ||
b0bf08a9 | 37 | $self->set_local_status({ state => 'wait_for_agent_lock' }); |
9c7d068b | 38 | |
5f095798 DM |
39 | return $self; |
40 | } | |
41 | ||
42 | sub shutdown_request { | |
43 | my ($self) = @_; | |
44 | ||
45 | $self->{shutdown_request} = 1; | |
9c7d068b DM |
46 | |
47 | $self->{mode} = 'restart'; # fixme: detect shutdown/reboot | |
48 | ||
49 | eval { $self->update_lrm_status(); }; | |
50 | if (my $err = $@) { | |
5bd7aa54 | 51 | $self->log('err', "unable to update lrm status file - $err"); |
9c7d068b | 52 | } |
5f095798 DM |
53 | } |
54 | ||
55 | sub get_local_status { | |
56 | my ($self) = @_; | |
57 | ||
58 | return $self->{status}; | |
59 | } | |
60 | ||
61 | sub set_local_status { | |
62 | my ($self, $new) = @_; | |
63 | ||
64 | die "invalid state '$new->{state}'" if !$valid_states->{$new->{state}}; | |
65 | ||
66 | my $haenv = $self->{haenv}; | |
67 | ||
68 | my $old = $self->{status}; | |
69 | ||
70 | # important: only update if if really changed | |
71 | return if $old->{state} eq $new->{state}; | |
72 | ||
0bba8f60 | 73 | $haenv->log('info', "status change $old->{state} => $new->{state}"); |
5f095798 DM |
74 | |
75 | $new->{state_change_time} = $haenv->get_time(); | |
76 | ||
77 | $self->{status} = $new; | |
78 | } | |
79 | ||
9c7d068b DM |
80 | sub update_lrm_status { |
81 | my ($self) = @_; | |
82 | ||
5bd7aa54 DM |
83 | my $haenv = $self->{haenv}; |
84 | ||
9c7d068b DM |
85 | my $lrm_status = { |
86 | mode => $self->{mode}, | |
87 | results => $self->{results}, | |
88 | }; | |
89 | ||
5bd7aa54 DM |
90 | eval { $haenv->write_lrm_status($lrm_status); }; |
91 | if (my $err = $@) { | |
92 | $haenv->log('err', "unable to write lrm status file - $err"); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | return 1; | |
9c7d068b DM |
97 | } |
98 | ||
5f095798 DM |
99 | sub get_protected_ha_agent_lock { |
100 | my ($self) = @_; | |
101 | ||
102 | my $haenv = $self->{haenv}; | |
103 | ||
104 | my $count = 0; | |
105 | my $starttime = $haenv->get_time(); | |
106 | ||
107 | for (;;) { | |
108 | ||
109 | if ($haenv->get_ha_agent_lock()) { | |
110 | if ($self->{ha_agent_wd}) { | |
111 | $haenv->watchdog_update($self->{ha_agent_wd}); | |
112 | } else { | |
113 | my $wfh = $haenv->watchdog_open(); | |
114 | $self->{ha_agent_wd} = $wfh; | |
115 | } | |
116 | return 1; | |
117 | } | |
118 | ||
119 | last if ++$count > 5; # try max 5 time | |
120 | ||
121 | my $delay = $haenv->get_time() - $starttime; | |
122 | last if $delay > 5; # for max 5 seconds | |
123 | ||
124 | $haenv->sleep(1); | |
125 | } | |
126 | ||
127 | return 0; | |
128 | } | |
129 | ||
546e2f1f DM |
130 | sub active_service_count { |
131 | my ($self) = @_; | |
132 | ||
133 | my $haenv = $self->{haenv}; | |
134 | ||
135 | my $nodename = $haenv->nodename(); | |
136 | ||
137 | my $ss = $self->{service_status}; | |
138 | ||
139 | my $count = 0; | |
140 | ||
141 | foreach my $sid (keys %$ss) { | |
142 | my $sd = $ss->{$sid}; | |
143 | next if !$sd->{node}; | |
144 | next if $sd->{node} ne $nodename; | |
145 | my $req_state = $sd->{state}; | |
146 | next if !defined($req_state); | |
147 | next if $req_state eq 'stopped'; | |
9c7d068b | 148 | next if $req_state eq 'freeze'; |
546e2f1f DM |
149 | |
150 | $count++; | |
151 | } | |
152 | ||
153 | return $count; | |
154 | } | |
5bd7aa54 DM |
155 | |
156 | my $wrote_lrm_status_at_startup = 0; | |
157 | ||
5f095798 DM |
158 | sub do_one_iteration { |
159 | my ($self) = @_; | |
160 | ||
161 | my $haenv = $self->{haenv}; | |
162 | ||
c5ec095f DM |
163 | if (!$wrote_lrm_status_at_startup) { |
164 | if ($haenv->quorate() && $self->update_lrm_status()) { | |
165 | $wrote_lrm_status_at_startup = 1; | |
166 | } else { | |
167 | # do nothing | |
168 | $haenv->sleep(5); | |
169 | return $self->{shutdown_request} ? 0 : 1; | |
170 | } | |
5bd7aa54 DM |
171 | } |
172 | ||
5f095798 DM |
173 | my $status = $self->get_local_status(); |
174 | my $state = $status->{state}; | |
175 | ||
067cdf33 DM |
176 | my $ms = $haenv->read_manager_status(); |
177 | $self->{service_status} = $ms->{service_status} || {}; | |
178 | ||
49777d09 | 179 | my $fence_request = PVE::HA::Tools::count_fenced_services($self->{service_status}, $haenv->nodename()); |
067cdf33 | 180 | |
5f095798 DM |
181 | # do state changes first |
182 | ||
183 | my $ctime = $haenv->get_time(); | |
184 | ||
b0bf08a9 | 185 | if ($state eq 'wait_for_agent_lock') { |
5f095798 | 186 | |
546e2f1f | 187 | my $service_count = $self->active_service_count(); |
5f095798 | 188 | |
067cdf33 | 189 | if (!$fence_request && $service_count && $haenv->quorate()) { |
0bba8f60 DM |
190 | if ($self->get_protected_ha_agent_lock()) { |
191 | $self->set_local_status({ state => 'active' }); | |
5f095798 DM |
192 | } |
193 | } | |
194 | ||
195 | } elsif ($state eq 'lost_agent_lock') { | |
196 | ||
067cdf33 | 197 | if (!$fence_request && $haenv->quorate()) { |
0bba8f60 DM |
198 | if ($self->get_protected_ha_agent_lock()) { |
199 | $self->set_local_status({ state => 'active' }); | |
5f095798 DM |
200 | } |
201 | } | |
202 | ||
0bba8f60 | 203 | } elsif ($state eq 'active') { |
5f095798 | 204 | |
067cdf33 DM |
205 | if ($fence_request) { |
206 | $haenv->log('err', "node need to be fenced - releasing agent_lock\n"); | |
207 | $self->set_local_status({ state => 'lost_agent_lock'}); | |
208 | } elsif (!$self->get_protected_ha_agent_lock()) { | |
5f095798 DM |
209 | $self->set_local_status({ state => 'lost_agent_lock'}); |
210 | } | |
211 | } | |
212 | ||
213 | $status = $self->get_local_status(); | |
214 | $state = $status->{state}; | |
215 | ||
216 | # do work | |
217 | ||
218 | if ($state eq 'wait_for_agent_lock') { | |
219 | ||
220 | return 0 if $self->{shutdown_request}; | |
221 | ||
222 | $haenv->sleep(5); | |
223 | ||
0bba8f60 | 224 | } elsif ($state eq 'active') { |
5f095798 DM |
225 | |
226 | my $startime = $haenv->get_time(); | |
227 | ||
228 | my $max_time = 10; | |
229 | ||
230 | my $shutdown = 0; | |
231 | ||
232 | # do work (max_time seconds) | |
233 | eval { | |
234 | # fixme: set alert timer | |
235 | ||
236 | if ($self->{shutdown_request}) { | |
237 | ||
238 | # fixme: request service stop or relocate ? | |
239 | ||
546e2f1f | 240 | my $service_count = $self->active_service_count(); |
5f095798 DM |
241 | |
242 | if ($service_count == 0) { | |
243 | ||
244 | if ($self->{ha_agent_wd}) { | |
245 | $haenv->watchdog_close($self->{ha_agent_wd}); | |
246 | delete $self->{ha_agent_wd}; | |
247 | } | |
248 | ||
249 | $shutdown = 1; | |
250 | } | |
c4a221bc | 251 | } else { |
c4a221bc DM |
252 | |
253 | $self->manage_resources(); | |
067cdf33 | 254 | |
5f095798 DM |
255 | } |
256 | }; | |
257 | if (my $err = $@) { | |
258 | $haenv->log('err', "got unexpected error - $err"); | |
259 | } | |
260 | ||
261 | return 0 if $shutdown; | |
262 | ||
263 | $haenv->sleep_until($startime + $max_time); | |
264 | ||
265 | } elsif ($state eq 'lost_agent_lock') { | |
266 | ||
267 | # Note: watchdog is active an will triger soon! | |
268 | ||
269 | # so we hope to get the lock back soon! | |
270 | ||
271 | if ($self->{shutdown_request}) { | |
272 | ||
546e2f1f | 273 | my $service_count = $self->active_service_count(); |
5f095798 | 274 | |
546e2f1f | 275 | if ($service_count > 0) { |
5f095798 | 276 | $haenv->log('err', "get shutdown request in state 'lost_agent_lock' - " . |
546e2f1f | 277 | "detected $service_count running services"); |
5f095798 | 278 | |
546e2f1f | 279 | } else { |
5f095798 | 280 | |
546e2f1f | 281 | # all services are stopped, so we can close the watchdog |
5f095798 | 282 | |
546e2f1f DM |
283 | if ($self->{ha_agent_wd}) { |
284 | $haenv->watchdog_close($self->{ha_agent_wd}); | |
285 | delete $self->{ha_agent_wd}; | |
286 | } | |
287 | ||
288 | return 0; | |
5f095798 | 289 | } |
5f095798 DM |
290 | } |
291 | ||
b0bf08a9 DM |
292 | $haenv->sleep(5); |
293 | ||
5f095798 DM |
294 | } else { |
295 | ||
296 | die "got unexpected status '$state'\n"; | |
297 | ||
298 | } | |
299 | ||
300 | return 1; | |
301 | } | |
302 | ||
c4a221bc DM |
303 | sub manage_resources { |
304 | my ($self) = @_; | |
305 | ||
306 | my $haenv = $self->{haenv}; | |
307 | ||
308 | my $nodename = $haenv->nodename(); | |
309 | ||
c4a221bc DM |
310 | my $ss = $self->{service_status}; |
311 | ||
312 | foreach my $sid (keys %$ss) { | |
313 | my $sd = $ss->{$sid}; | |
314 | next if !$sd->{node}; | |
315 | next if !$sd->{uid}; | |
316 | next if $sd->{node} ne $nodename; | |
317 | my $req_state = $sd->{state}; | |
318 | next if !defined($req_state); | |
9c7d068b | 319 | next if $req_state eq 'freeze'; |
c4a221bc | 320 | eval { |
e88469ba | 321 | $self->queue_resource_command($sid, $sd->{uid}, $req_state, $sd->{target}); |
c4a221bc DM |
322 | }; |
323 | if (my $err = $@) { | |
f31b7e94 | 324 | $haenv->log('err', "unable to run resource agent for '$sid' - $err"); # fixme |
c4a221bc DM |
325 | } |
326 | } | |
327 | ||
f31b7e94 | 328 | my $starttime = $haenv->get_time(); |
c4a221bc DM |
329 | |
330 | # start workers | |
331 | my $max_workers = 4; | |
332 | ||
6dbf93a0 | 333 | my $sc = $haenv->read_service_config(); |
f31b7e94 DM |
334 | |
335 | while (($haenv->get_time() - $starttime) < 5) { | |
c4a221bc DM |
336 | my $count = $self->check_active_workers(); |
337 | ||
338 | foreach my $sid (keys %{$self->{workers}}) { | |
339 | last if $count >= $max_workers; | |
340 | my $w = $self->{workers}->{$sid}; | |
6dbf93a0 DM |
341 | my $cd = $sc->{$sid}; |
342 | if (!$cd) { | |
f31b7e94 | 343 | $haenv->log('err', "missing resource configuration for '$sid'"); |
6dbf93a0 DM |
344 | next; |
345 | } | |
c4a221bc | 346 | if (!$w->{pid}) { |
f31b7e94 DM |
347 | if ($haenv->can_fork()) { |
348 | my $pid = fork(); | |
349 | if (!defined($pid)) { | |
350 | $haenv->log('err', "fork worker failed"); | |
351 | $count = 0; last; # abort, try later | |
352 | } elsif ($pid == 0) { | |
353 | # do work | |
354 | my $res = -1; | |
355 | eval { | |
356 | $res = $haenv->exec_resource_agent($sid, $cd, $w->{state}, $w->{target}); | |
357 | }; | |
358 | if (my $err = $@) { | |
359 | $haenv->log('err', $err); | |
360 | POSIX::_exit(-1); | |
361 | } | |
362 | POSIX::_exit($res); | |
363 | } else { | |
364 | $count++; | |
365 | $w->{pid} = $pid; | |
366 | } | |
367 | } else { | |
c4a221bc DM |
368 | my $res = -1; |
369 | eval { | |
6dbf93a0 | 370 | $res = $haenv->exec_resource_agent($sid, $cd, $w->{state}, $w->{target}); |
c4a221bc DM |
371 | }; |
372 | if (my $err = $@) { | |
f31b7e94 DM |
373 | $haenv->log('err', $err); |
374 | } | |
375 | $self->resource_command_finished($sid, $w->{uid}, $res); | |
c4a221bc DM |
376 | } |
377 | } | |
378 | } | |
379 | ||
380 | last if !$count; | |
381 | ||
f31b7e94 | 382 | $haenv->sleep(1); |
c4a221bc DM |
383 | } |
384 | } | |
385 | ||
386 | # fixme: use a queue an limit number of parallel workers? | |
387 | sub queue_resource_command { | |
e88469ba | 388 | my ($self, $sid, $uid, $state, $target) = @_; |
c4a221bc DM |
389 | |
390 | if (my $w = $self->{workers}->{$sid}) { | |
391 | return if $w->{pid}; # already started | |
392 | # else, delete and overwrite queue entry with new command | |
393 | delete $self->{workers}->{$sid}; | |
394 | } | |
395 | ||
396 | $self->{workers}->{$sid} = { | |
397 | sid => $sid, | |
398 | uid => $uid, | |
399 | state => $state, | |
400 | }; | |
e88469ba DM |
401 | |
402 | $self->{workers}->{$sid}->{target} = $target if $target; | |
c4a221bc DM |
403 | } |
404 | ||
405 | sub check_active_workers { | |
406 | my ($self) = @_; | |
407 | ||
408 | # finish/count workers | |
409 | my $count = 0; | |
410 | foreach my $sid (keys %{$self->{workers}}) { | |
411 | my $w = $self->{workers}->{$sid}; | |
412 | if (my $pid = $w->{pid}) { | |
413 | # check status | |
414 | my $waitpid = waitpid($pid, WNOHANG); | |
415 | if (defined($waitpid) && ($waitpid == $pid)) { | |
416 | $self->resource_command_finished($sid, $w->{uid}, $?); | |
417 | } else { | |
418 | $count++; | |
419 | } | |
420 | } | |
421 | } | |
422 | ||
423 | return $count; | |
424 | } | |
425 | ||
426 | sub resource_command_finished { | |
427 | my ($self, $sid, $uid, $status) = @_; | |
428 | ||
429 | my $haenv = $self->{haenv}; | |
430 | ||
431 | my $w = delete $self->{workers}->{$sid}; | |
432 | return if !$w; # should not happen | |
433 | ||
434 | my $exit_code = -1; | |
435 | ||
436 | if ($status == -1) { | |
0f70400d | 437 | $haenv->log('err', "resource agent $sid finished - failed to execute"); |
c4a221bc | 438 | } elsif (my $sig = ($status & 127)) { |
0f70400d | 439 | $haenv->log('err', "resource agent $sid finished - got signal $sig"); |
c4a221bc DM |
440 | } else { |
441 | $exit_code = ($status >> 8); | |
c4a221bc DM |
442 | } |
443 | ||
444 | $self->{results}->{$uid} = { | |
445 | sid => $w->{sid}, | |
446 | state => $w->{state}, | |
447 | exit_code => $exit_code, | |
448 | }; | |
449 | ||
450 | my $ss = $self->{service_status}; | |
451 | ||
452 | # compute hash of valid/existing uids | |
453 | my $valid_uids = {}; | |
454 | foreach my $sid (keys %$ss) { | |
455 | my $sd = $ss->{$sid}; | |
456 | next if !$sd->{uid}; | |
457 | $valid_uids->{$sd->{uid}} = 1; | |
458 | } | |
459 | ||
460 | my $results = {}; | |
461 | foreach my $id (keys %{$self->{results}}) { | |
462 | next if !$valid_uids->{$id}; | |
463 | $results->{$id} = $self->{results}->{$id}; | |
464 | } | |
465 | $self->{results} = $results; | |
466 | ||
9c7d068b | 467 | $self->update_lrm_status(); |
c4a221bc DM |
468 | } |
469 | ||
5f095798 | 470 | 1; |