1 # Copyright (c) 2009,2010 Zmanda, Inc. All Rights Reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19 package Amanda::Changer::aggregate;
24 @ISA = qw( Amanda::Changer );
26 use File::Glob qw( :glob );
28 use Amanda::Config qw( :getconf );
30 use Amanda::Debug qw( debug warning );
31 use Amanda::Util qw( :alternates );
34 use Amanda::Device qw( :constants );
38 Amanda::Changer::aggregate
42 This changer operates several child changers.
49 See the amanda-changers(7) manpage for usage information.
55 my ($config, $tpchanger) = @_;
56 my ($kidspecs) = ( $tpchanger =~ /chg-aggregate:(.*)/ );
58 my @kidspecs = Amanda::Util::expand_braced_alternates($kidspecs);
60 return Amanda::Changer->make_error("fatal", undef,
61 message => "chg-aggregate needs at least two child changers");
65 ($_ eq "ERROR")? "ERROR" : Amanda::Changer->new($_)
69 if (defined $config->{'properties'}->{'fail-on-error'}) {
70 $fail_on_error = string_to_boolean($config->{'properties'}->{'fail-on-error'}->{'values'}[0]);
75 if (grep { $_->isa("Amanda::Changer::Error") } @children) {
78 for my $i (0 .. @children-1) {
79 if ($children[$i]->isa("Amanda::Changer::Error")) {
81 [ $kidspecs[$i], $children[$i] ];
86 if ($valid == 0 || $fail_on_error) {
87 return Amanda::Changer->make_combined_error(
88 "fatal", [ @annotated_errs ]);
93 my $state_filename_prop = $config->{'properties'}->{'state_filename'};
95 if (defined $state_filename_prop) {
96 $state_filename = $state_filename_prop->{'values'}[0];
98 if (!defined $state_filename) {
99 $state_filename = $Amanda::Paths::CONFIG_DIR . '/' . $config->{'name'} . ".state";
104 child_names => \@kidspecs,
105 children => \@children,
106 num_children => scalar @children,
107 current_slot => undef,
108 state_filename => $state_filename,
110 bless ($self, $class);
118 foreach my $child (@{$self->{'children'}}) {
122 $self->SUPER::quit();
125 sub _get_current_slot
130 $self->with_locked_state($self->{'state_filename'}, $cb, sub {
131 my ($state, $cb) = @_;
132 $self->{'current_slot'} = $state->{'current_slot'};
133 $self->{'current_slot'} = "0:first" if !defined $self->{'current_slot'};
138 sub _set_current_slot
143 $self->with_locked_state($self->{'state_filename'}, $cb, sub {
144 my ($state, $cb) = @_;
145 $state->{'current_slot'} = $self->{'current_slot'};
155 return if $self->check_error($params{'res_cb'});
157 my $res_cb = $params{'res_cb'};
158 my $orig_slot = $params{'slot'};
159 $self->validate_params('load', \%params);
161 my $steps = define_steps
164 step which_slot => sub {
165 if (exists $params{'relative_slot'} &&
166 $params{'relative_slot'} eq "current") {
167 if (defined $self->{'current_slot'}) {
168 return $steps->{'set_from_current'}->();
170 return $self->_get_current_slot($steps->{'set_from_current'});
172 } elsif (exists $params{'relative_slot'} &&
173 $params{'relative_slot'} eq "next") {
174 if (defined $self->{'current_slot'}) {
175 return $steps->{'get_inventory_next'}->();
177 return $self->_get_current_slot($steps->{'get_inventory_next'});
179 } elsif (exists $params{'label'}) {
180 return $self->inventory(inventory_cb => $steps->{'got_inventory_label'});
182 return $steps->{'slot_set'}->();
185 step get_inventory_next => sub {
186 return $self->inventory(inventory_cb => $steps->{'got_inventory_next'})
189 step got_inventory_next => sub {
190 my ($err, $inv) = @_;
196 for my $i (0.. scalar(@$inv)-1) {
197 $slot = @$inv[$i]->{'slot'};
198 if ($slot eq $self->{'current_slot'}) {
200 } elsif ($found >= 0 && (!exists $params{'except_slots'} ||
201 !exists $params{'except_slots'}->{$slot})) {
203 return $steps->{'slot_set'}->();
207 for my $i (0..($found-1)) {
208 $slot = @$inv[$i]->{'slot'};
209 if (!exists($params{'except_slots'}) ||
210 !exists($params{'except_slots'}->{$slot})) {
212 return $steps->{'slot_set'}->();
216 return $self->make_error("failed", $res_cb,
217 reason => "notfound",
218 message => "all slots have been loaded");
221 step got_inventory_label => sub {
222 my ($err, $inv) = @_;
227 for my $i (0.. scalar(@$inv)-1) {
228 my $slot = @$inv[$i]->{'slot'};
229 my $label = @$inv[$i]->{'label'};
230 if ($label eq $params{'label'}) {
232 return $steps->{'slot_set'}->();
235 return $self->make_error("failed", $res_cb,
236 reason => "notfound",
237 message => "label $params{'label'} not found");
240 step set_from_current => sub {
241 $orig_slot = $self->{'current_slot'};
242 return $steps->{'slot_set'}->();
245 step slot_set => sub {
246 my ($kid, $slot) = split(':', $orig_slot, 2);
247 my $child = $self->{'children'}[$kid];
248 if (!defined $child) {
249 return $self->make_error("failed", $res_cb,
251 message => "no changer $kid");
253 delete $params{'relative_slot'};
254 $params{'slot'} = $slot;
255 $params{'res_cb'} = sub {
256 my ($err, $res) = @_;
258 if ($slot ne "first" && $res->{'this_slot'} != $slot) {
259 return $self->make_error("failed", $res_cb,
261 message => "slot doesn't match: $res->{'this_slot'} != $slot");
263 $self->{'current_slot'} = "$kid:$res->{'this_slot'}";
264 $aggregate_res = Amanda::Changer::aggregate::Reservation->new($self, $res, $self->{'current_slot'});
265 return $self->_set_current_slot($steps->{'done'});
268 return $res_cb->($err, undef);
270 return $child->load(%params);
274 $res_cb->(undef, $aggregate_res);
280 my ($key, %params) = @_;
282 return if $self->check_error($params{'info_cb'});
284 my $check_and_report_errors = sub {
285 my ($kid_results) = @_;
287 if (grep { defined($_->[0]) } @$kid_results) {
288 # we have errors, so collect them and make a "combined" error.
291 for my $i (0 .. $self->{'num_children'}-1) {
292 my $kr = $kid_results->[$i];
293 next unless defined($kr->[0]);
294 push @annotated_errs,
295 [ $self->{'child_names'}[$i], $kr->[0] ];
296 push @err_slots, $kr->[0]->{'slot'}
297 if (defined $kr->[0] and defined $kr->[0]->{'slot'});
301 if (@err_slots == $self->{'num_children'}) {
302 @slotarg = (slot => collapse_braced_alternates([@err_slots]));
305 $self->make_combined_error(
306 $params{'info_cb'}, [ @annotated_errs ],
312 if ($key eq 'num_slots') {
313 my $all_kids_done_cb = sub {
314 my ($kid_results) = @_;
315 return if ($check_and_report_errors->($kid_results));
319 for (@$kid_results) {
320 my ($err, %kid_info) = @$_;
321 next unless exists($kid_info{'num_slots'});
322 my $kid_num_slots = $kid_info{'num_slots'};
323 $num_slots += $kid_num_slots;
325 $params{'info_cb'}->(undef, num_slots => $num_slots) if $params{'info_cb'};
328 $self->_for_each_child(
330 my ($kid_chg, $kid_cb) = @_;
331 $kid_chg->info(info => [ 'num_slots' ], info_cb => $kid_cb);
334 parent_cb => $all_kids_done_cb,
336 } elsif ($key eq "vendor_string") {
337 my $all_kids_done_cb = sub {
338 my ($kid_results) = @_;
339 return if ($check_and_report_errors->($kid_results));
343 map { my ($e, %r) = @$_; $r{'vendor_string'} }
347 $vendor_string = collapse_braced_alternates([@kid_vendors]);
348 $params{'info_cb'}->(undef, vendor_string => $vendor_string) if $params{'info_cb'};
350 $params{'info_cb'}->(undef) if $params{'info_cb'};
354 $self->_for_each_child(
356 my ($kid_chg, $kid_cb) = @_;
357 $kid_chg->info(info => [ 'vendor_string' ], info_cb => $kid_cb);
360 parent_cb => $all_kids_done_cb,
362 } elsif ($key eq 'fast_search') {
363 my $all_kids_done_cb = sub {
364 my ($kid_results) = @_;
365 return if ($check_and_report_errors->($kid_results));
369 map { my ($e, %r) = @$_; $r{'fast_search'} }
373 # conduct a logical AND of all child fastnesses
374 for my $f (@kid_fastness) {
375 $fast_search = $fast_search && $f;
377 $params{'info_cb'}->(undef, fast_search => $fast_search) if $params{'info_cb'};
379 $params{'info_cb'}->(undef, fast_search => 0) if $params{'info_cb'};
383 $self->_for_each_child(
385 my ($kid_chg, $kid_cb) = @_;
386 $kid_chg->info(info => [ 'fast_search' ], info_cb => $kid_cb);
389 parent_cb => $all_kids_done_cb,
394 # reset, clean, etc. are all *very* similar to one another, so we create them
397 my ($op, $has_drive) = @_;
402 return if $self->check_error($params{'finished_cb'});
404 if (exists $params{'drive'}) {
405 return $self->make_error("failed", $params{'finished_cb'},
407 message => "Can't specify drive fo $op command");
410 my $all_kids_done_cb = sub {
411 my ($kid_results) = @_;
412 if (grep { defined($_->[0]) } @$kid_results) {
413 # we have errors, so collect them and make a "combined" error.
415 for my $i (0 .. $self->{'num_children'}-1) {
416 my $kr = $kid_results->[$i];
417 next unless defined($kr->[0]);
418 push @annotated_errs,
419 [ $self->{'child_names'}[$i], $kr->[0] ];
421 $self->make_combined_error(
422 $params{'finished_cb'}, [ @annotated_errs ]);
425 $params{'finished_cb'}->() if $params{'finished_cb'};
428 $self->_for_each_child(
430 my ($kid_chg, $kid_cb) = @_;
431 $kid_chg->$op(%params, finished_cb => $kid_cb);
434 parent_cb => $all_kids_done_cb,
440 # perl doesn't like that these symbols are only mentioned once
443 *reset = _mk_simple_op("reset");
444 *clean = _mk_simple_op("clean");
445 *eject = _mk_simple_op("eject");
454 my $user_msg_fn = $params{'user_msg_fn'};
455 $user_msg_fn ||= sub { Amanda::Debug::info("chg-aggregate: " . $_[0]); };
457 if (exists $params{'changed'}) {
458 for my $range (split ',', $params{'changed'}) {
459 my ($first, $last) = ($range =~ /([:\d]+)(?:-([:\d]+))?/);
460 $last = $first unless defined($last);
462 my ($f_kid, $f_slot) = split(':', $first, 2);
463 my ($l_kid, $l_slot) = split(':', $last, 2);
464 if ($f_kid != $l_kid) {
467 if ($changed{$f_kid} != 1) {
468 for my $slot ($f_slot..$l_slot) {
469 $changed{$f_kid}{$slot} = 1;
473 for my $kid ($first..$last) {
478 for my $kid (0..$self->{'num_children'}-1) {
479 if ($changed{$kid} == 1) {
480 $kid_args[$kid] = "ALL";
481 } elsif (keys %{$changed{$kid}} > 0) {
482 $kid_args[$kid] = { changed => join(',',sort(keys %{$changed{$kid}})) };
484 $kid_args[$kid] = "NONE";
488 for my $kid (0..$self->{'num_children'}-1) {
489 $kid_args[$kid] = "ALL";
493 my $all_kids_done_cb = sub {
494 my ($kid_results) = @_;
495 if (grep { defined($_->[0]) } @$kid_results) {
496 # we have errors, so collect them and make a "combined" error.
498 for my $i (0 .. $self->{'num_children'}-1) {
499 my $kr = $kid_results->[$i];
500 next unless defined($kr->[0]);
501 push @annotated_errs,
502 [ $self->{'child_names'}[$i], $kr->[0] ];
504 $self->make_combined_error(
505 $params{'finished_cb'}, [ @annotated_errs ]);
508 $params{'finished_cb'}->() if $params{'finished_cb'};
511 $self->_for_each_child(
513 my ($kid_chg, $kid_cb, $args) = @_;
514 if (ref($args) eq "HASH") {
515 $kid_chg->update(%params, finished_cb => $kid_cb, %$args);
516 } elsif ($args eq "ALL") {
517 $kid_chg->update(%params, finished_cb => $kid_cb);
523 parent_cb => $all_kids_done_cb,
532 return if $self->check_error($params{'inventory_cb'});
534 my $steps = define_steps
535 cb_ref => \$params{'inventory_cb'};
537 step get_current => sub {
538 return $self->_get_current_slot($steps->{'got_current_slot'});
541 step got_current_slot => sub {
542 $self->_for_each_child(
544 my ($kid_chg, $kid_cb) = @_;
545 $kid_chg->inventory(inventory_cb => $kid_cb);
548 parent_cb => $steps->{'all_kids_done_cb'},
552 step all_kids_done_cb => sub {
553 my ($kid_results) = @_;
554 if (grep { defined($_->[0]) } @$kid_results) {
555 # we have errors, so collect them and make a "combined" error.
557 for my $i (0 .. $self->{'num_children'}-1) {
558 my $kr = $kid_results->[$i];
559 next unless defined($kr->[0]);
560 push @annotated_errs,
561 [ $self->{'child_names'}[$i], $kr->[0] ];
563 return $self->make_combined_error(
564 $params{'inventory_cb'}, [ @annotated_errs ]);
567 my $inv = $self->_merge_inventories($kid_results);
569 return $self->make_error("failed", $params{'inventory_cb'},
571 message => "could not generate consistent inventory from aggregate child changers");
574 $params{'inventory_cb'}->(undef, $inv);
584 return if $self->check_error($params{'finished_cb'});
586 my $finished_cb = $params{'finished_cb'};
587 my $orig_slot = $params{'slot'};
589 if (!defined $params{'slot'}) {
590 return $self->make_error("failed", $finished_cb,
592 message => "no 'slot' params set.");
595 if (!defined $params{'meta'}) {
596 return $self->make_error("failed", $finished_cb,
598 message => "no 'meta' params set.");
601 my ($kid, $slot) = split(':', $orig_slot, 2);
602 my $child = $self->{'children'}[$kid];
603 if (!defined $child) {
604 return $self->make_error("failed", $finished_cb,
606 message => "no changer $kid");
609 $params{'slot'} = $slot;
610 return $child->set_meta_label(%params);
618 return if $self->check_error($params{'finished_cb'});
620 my $finished_cb = $params{'finished_cb'};
621 my $orig_slot = $params{'slot'};
623 if (!defined $params{'slot'}) {
624 return $self->make_error("failed", $finished_cb,
626 message => "no 'slot' params set.");
629 my ($kid, $slot) = split(':', $orig_slot, 2);
630 my $child = $self->{'children'}[$kid];
631 if (!defined $child) {
632 return $self->make_error("failed", $finished_cb,
634 message => "no changer $kid");
637 $params{'slot'} = $slot;
638 return $child->get_meta_label(%params);
641 # Takes keyword parameters 'oksub', 'errsub', 'parent_cb', and 'args'. For
642 # each child, runs $oksub (or, if the child is "ERROR", $errsub), passing it
643 # the changer, an aggregating callback, and the corresponding element from
644 # @$args (if specified). The callback combines its results with the results
645 # from other changers, and when all results are available, calls $parent_cb.
647 # This forms a kind of "AND" combinator for a parallel operation on multiple
648 # changers, providing the caller with a simple collection of the results of
649 # the operation. The parent_cb is called as
650 # $parent_cb->([ [ <chg_1_results> ], [ <chg_2_results> ], .. ]).
651 sub _for_each_child {
654 my ($oksub, $errsub, $parent_cb, $args) =
655 ($params{'oksub'}, $params{'errsub'}, $params{'parent_cb'}, $params{'args'});
657 if (defined($args)) {
658 die "number of args did not match number of children"
659 unless (@$args == $self->{'num_children'});
661 $args = [ ( undef ) x $self->{'num_children'} ];
664 my $remaining = $self->{'num_children'};
665 my @results = ( undef ) x $self->{'num_children'};
666 my $maybe_done = sub {
667 return if (--$remaining);
668 $parent_cb->([ @results ]);
671 for my $i (0 .. $self->{'num_children'}-1) {
672 my $child = $self->{'children'}[$i];
673 my $arg = @$args? $args->[$i] : undef;
676 $results[$i] = [ @_ ];
680 if ($child eq "ERROR") {
681 if (defined $errsub) {
682 $errsub->("ERROR", $child_cb, $arg);
684 # no errsub; just call $child_cb directly
685 $child_cb->(undef) if $child_cb;
688 $oksub->($child, $child_cb, $arg) if $oksub;
693 sub _merge_inventories {
695 my ($kid_results) = @_;
699 for my $kid_result (@$kid_results) {
700 my $kid_inv = $kid_result->[1];
702 for my $x (@$kid_inv) {
703 my $slotname = "$nb:" . $x->{'slot'};
704 my $current = $slotname eq $self->{'current_slot'};
706 state => $x->{'state'},
707 device_status => $x->{'device_status'},
708 f_type => $x->{'f_type'},
709 label => $x->{'label'},
710 barcode => $x->{'barcode'},
711 reserved => $x->{'reserved'},
713 import_export => $x->{'import_export'},
714 loaded_in => $x->{'loaded_in'},
721 return [ @combined ];
724 package Amanda::Changer::aggregate::Reservation;
726 @ISA = qw( Amanda::Changer::Reservation );
730 my ($chg, $kid_res, $slot) = @_;
731 my $self = Amanda::Changer::Reservation::new($class);
733 $self->{'chg'} = $chg;
734 $self->{'kid_res'} = $kid_res;
735 $self->{'device'} = $kid_res->{'device'};
736 $self->{'barcode'} = $kid_res->{'barcode'};
737 $self->{'this_slot'} = $slot;
746 # unref the device, for good measure
747 $self->{'device'} = undef;
749 $self->{'kid_res'}->release(%params);
750 $self->{'kid_res'} = undef;
757 $self->{'kid_res'}->get_meta_label(%params);
764 $self->{'kid_res'}->set_meta_label(%params);