Checking in changes prior to tagging of version 2.66.
[MogileFS-Server.git] / lib / MogileFS / ReplicationRequest.pm
bloba5b0828d25df488baa9ce0a21edcf8ab89202f9f
1 package MogileFS::ReplicationRequest;
2 use strict;
3 use MogileFS::Server;
4 require Exporter;
5 our @ISA = qw(Exporter);
6 our @EXPORT_OK = qw(rr_upgrade ALL_GOOD TOO_GOOD TEMP_NO_ANSWER);
8 my $no_answer = bless { temp_fail => 1 };
9 sub TEMP_NO_ANSWER () { $no_answer }
10 my $all_good = bless { all_good => 1 };
11 sub ALL_GOOD () { $all_good }
12 my $too_good = bless { all_good => 1, too_good => 1 };
13 sub TOO_GOOD () { $too_good }
15 # upgrades the return values from old-style ReplicationPolicy classes
16 # to MogileFS::ReplicationRequest objects, unless they already are,
17 # in which case they're passed through unchanged. provides peaceful
18 # upgrade path for old plugins.
19 sub rr_upgrade {
20 my ($rv) = @_;
21 return $rv if ref $rv;
22 return TEMP_NO_ANSWER if !defined $rv;
23 return ALL_GOOD if !$rv;
24 return MogileFS::ReplicationRequest->replicate_to($rv);
27 # for ideal replications
28 sub replicate_to {
29 my ($class, @devs) = @_;
30 @devs = map { ref $_ ? $_ : Mgd::device_factory()->get_by_id($_) } @devs;
31 return bless {
32 ideal_next => \@devs,
33 }, $class;
36 sub new {
37 my ($class, %opts) = @_;
38 my $self = bless {}, $class;
39 $self->{ideal_next} = delete $opts{ideal} || [];
40 $self->{desperate_next} = delete $opts{desperate} || [];
41 Carp::croak("unknown args") if %opts;
42 return $self;
45 ############################################################################
47 sub is_happy {
48 my $self = shift;
49 return $self->{all_good};
52 sub too_happy {
53 my $self = shift;
54 return $self->{too_good};
57 sub temp_fail {
58 my $self = shift;
59 return $self->{temp_fail};
62 # returns array of MogileFS::Device objs, in preferred order, one of
63 # which (but not multiple) would satisfy the replication policy
64 # for its next step. at which point the replication policy needs
65 # to be asked again what the next step is.
66 sub copy_to_one_of_ideally {
67 my $self = shift;
68 return @{ $self->{ideal_next} || [] };
71 # like above, but replication policy isn't happy about these choices,
72 # so a reevaluation of this replication decision should be made in the
73 # future, when new disks/hosts might be available.
74 sub copy_to_one_of_desperate {
75 my $self = shift;
76 return @{ $self->{desperate_next} || [] };
79 # for test suite..
80 sub t_as_string {
81 my $self = shift;
82 return "too_good" if $self->{too_good};
83 return "all_good" if $self->{all_good};
84 return "temp_fail" if $self->{temp_fail};
85 my @devs;
86 if (@devs = $self->copy_to_one_of_ideally) {
87 return "ideal(" . join(",", sort {$a<=>$b} map { $_->id } @devs) . ")";
89 if (@devs = $self->copy_to_one_of_desperate) {
90 return "desperate(" . join(",", sort {$a<=>$b} map { $_->id } @devs) . ")";
92 die "unknown $self type";