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