query: allow "0" key on all commands which take keys
[MogileFS-Server.git] / lib / MogileFS / ReplicationPolicy.pm
blobc8709dde5481db26f0bcc23fc9d1c3e1d8400102
1 package MogileFS::ReplicationPolicy;
2 use strict;
4 =head1 NAME
6 MogileFS::ReplicationPolicy - base class for file replication policies
8 =head1 DESCRIPTION
10 A MogileFS replication policy class implements policy for how files
11 should be replicated around.
13 ....
15 =cut
17 # parse a policy description string, instantiating object(s) along the way
18 # given $str can be either a scalar, or a scalarref that's eaten away as it's parsed.
19 sub new_from_policy_string {
20 my ($class, $str_a) = @_;
22 # simple case for normal callers: they give us a scalar, but internally
23 # we work with it as a scalarref that we eat away while parsing.
24 my $strref = ref $str_a ? $str_a : \$str_a;
26 $$strref =~ s/^\s*([\w:]+)// or die "Failed to parse policy string: $$strref";
27 my ($polclass) = ($1);
28 $polclass = "MogileFS::ReplicationPolicy::$polclass" unless $polclass =~ /:/;
30 my $rv = eval "use $polclass; 1";
31 if ($@ || !$rv) {
32 die "Failed to load replication policy class $polclass: $@\n";
35 return $polclass->new_from_policy_args($strref);
38 # returns:
39 # 0: replication sufficient
40 # undef: no suitable recommendations currently.
41 # >0: devid to replicate to.
42 sub replicate_to {
43 my ($self, %args) = @_;
44 my $fid = delete $args{fid}; # fid scalar to copy
45 my $on_devs = delete $args{on_devs}; # arrayref of device objects
46 my $all_devs = delete $args{all_devs}; # hashref of { devid => MogileFS::Device }
47 my $failed = delete $args{failed}; # hashref of { devid => 1 } of failed attempts this round
48 my $min = delete $args{min}; # configured min devcount for this class
50 warn "Unknown parameters: " . join(", ", sort keys %args) if %args;
51 die "Missing parameters" unless $on_devs && $all_devs && $failed && $fid;
53 die "UNIMPLEMENTED 'replicate_to' in " . (ref($self) || $self);