File Coverage

blib/lib/MogileFS/DevFID.pm
Criterion Covered Total %
statement 15 89 16.8
branch 0 42 0.0
condition 0 12 0.0
subroutine 5 23 21.7
pod 0 16 0.0
total 20 182 10.9


line stmt bran cond sub pod time code
1             package MogileFS::DevFID;
2 21     21   132 use strict;
  21         48  
  21         885  
3 21     21   120 use warnings;
  21         43  
  21         615  
4 21     21   129 use MogileFS::Server;
  21         41  
  21         708  
5 21     21   127 use overload '""' => \&as_string;
  21         49  
  21         262  
6 21     21   2215 use Carp qw(croak);
  21         52  
  21         34039  
7              
8             sub new {
9 0     0 0   my ($class, $devarg, $fidarg) = @_;
10 0 0         return bless {
    0          
    0          
    0          
11             devid => ref $devarg ? $devarg->id : $devarg,
12             dev => ref $devarg ? $devarg : undef,
13             fidid => ref $fidarg ? $fidarg->id : $fidarg,
14             fid => ref $fidarg ? $fidarg : undef,
15             }, $class;
16             }
17              
18             # --------------------------------------------------------------------------
19              
20 0     0 0   sub devid { $_[0]{devid} }
21 0     0 0   sub fidid { $_[0]{fidid} }
22              
23             sub as_string {
24 0     0 0   "DevFID[d=" . $_[0]{devid} . ";f=" . $_[0]{fidid} . "]";
25             }
26              
27             sub device {
28 0     0 0   my $self = shift;
29 0   0       return $self->{dev} ||= Mgd::device_factory()->get_by_id($self->{devid});
30             }
31              
32             sub fid {
33 0     0 0   my $self = shift;
34 0   0       return $self->{fid} ||= MogileFS::FID->new($self->{fidid});
35             }
36              
37             # returns true if DevFID actually exists in database
38             sub exists {
39 0     0 0   my $self = shift;
40 0           my $fid = $self->fid;
41 0 0         return (grep { $_ == $self->{devid} } $fid->devids) ? 1 : 0;
  0            
42             }
43              
44             sub url {
45 0     0 0   my $self = shift;
46 0           return $self->_make_full_url(0);
47             }
48              
49             sub get_url {
50 0     0 0   my $self = shift;
51 0           return $self->_make_full_url(1);
52             }
53              
54             sub vivify_directories {
55 0     0 0   my ($self, $cb) = @_;
56 0           my $url = $self->url;
57 0           $self->device()->vivify_directories($url, $cb);
58             }
59              
60             # returns 0 on missing,
61             # undef on connectivity error,
62             # else size of file on disk (after HTTP HEAD or mogstored stat)
63             # invokes $cb on the size if $cb is supplied (and Danga::Socket->EventLoop runs)
64             sub size_on_disk {
65 0     0 0   my ($self, $cb) = @_;
66              
67 0 0         if ($self->device->should_read_from) {
68 0           my $url = $self->get_url;
69 0   0       my $httpfile = $self->{_httpfile_get} ||= MogileFS::HTTPFile->at($url);
70              
71             # check that it has size (>0) and is reachable (not undef)
72 0           return $httpfile->size($cb);
73             } else {
74             # monitor says we cannot read from this device, so do not try
75              
76 0 0   0     Danga::Socket->AddTimer(0, sub { $cb->(undef) }) if $cb;
  0            
77 0           return undef;
78             }
79             }
80              
81             # returns -1 on missing,
82             # undef on connectivity error,
83             # else checksum of file on disk (after HTTP GET or mogstored read)
84             sub checksum_on_disk {
85 0     0 0   my ($self, $alg, $ping_cb, $reason) = @_;
86              
87 0 0         return undef unless $self->device->should_read_from;
88              
89 0           my $url = $self->get_url;
90 0   0       my $httpfile = $self->{_httpfile_get} ||= MogileFS::HTTPFile->at($url);
91              
92             # check that it has size (>0) and is reachable (not undef)
93 0           return $httpfile->digest($alg, $ping_cb, $reason);
94             }
95              
96             # returns true if size seen matches fid's length
97             sub size_matches {
98 0     0 0   my $self = shift;
99 0           my $fid = $self->fid;
100 0           my $disk_size = $self->size_on_disk;
101              
102             # Temporary connectivity error with that disk/machine..
103 0 0         return 0 unless defined $disk_size;
104 0 0         return 0 if $disk_size == MogileFS::HTTPFile::FILE_MISSING;
105              
106 0           return $disk_size == $fid->length;
107             }
108              
109             # returns just the URI path component without scheme/host
110             sub uri_path {
111 0     0 0   my $self = shift;
112 0           my $devid = $self->{devid};
113 0           my $fidid = $self->{fidid};
114              
115 0           my $nfid;
116 0           my $len = length $fidid;
117 0 0         if ($len < 10) {
118 0           $nfid = '0' x (10 - $len) . $fidid;
119             } else {
120 0           $nfid = $fidid;
121             }
122 0           my ( $b, $mmm, $ttt, $hto ) = ( $nfid =~ m{(\d)(\d{3})(\d{3})(\d{3})} );
123              
124 0           return "/dev$devid/$b/$mmm/$ttt/$nfid.fid";
125             }
126              
127             sub _make_full_url {
128             # set use_get_port to be true to specify to use the get port
129 0     0     my ($self, $use_get_port) = @_;
130              
131             # get some information we'll need
132 0 0         my $dev = $self->device or return undef;
133 0 0         my $host = $dev->host or return undef;
134              
135 0           my $path = $self->uri_path;
136 0           my $hostip = $host->ip;
137 0 0         my $port = $use_get_port ? $host->http_get_port : $host->http_port;
138              
139 0           return "http://$hostip:$port$path";
140             }
141              
142             sub add_to_db {
143 0     0 0   my ($self, $no_lock) = @_;
144 0 0         croak("fidid not non-zero") unless $self->{fidid};
145 0 0         croak("devid not non-zero") unless $self->{devid};
146              
147 0           my $sto = Mgd::get_store();
148 0 0         if ($sto->add_fidid_to_devid($self->{fidid}, $self->{devid})) {
149 0 0         if (my $memc = MogileFS::Config->memcache_client) {
150 0           $memc->delete("mogdevids:$self->{fidid}");
151             }
152 0           return $self->fid->update_devcount(no_lock => $no_lock);
153             } else {
154             # was already on that device
155 0           return 1;
156             }
157             }
158              
159             # Destroy a particular replica of a file via HTTP, remove it
160             # from the tracker, and update the replication counts to be
161             # accurate again.
162             sub destroy {
163 0     0 0   my $self = shift;
164 0           my %opts = @_;
165              
166 0 0         my $httpfile = MogileFS::HTTPFile->at($self->url)
167             or die "Creation of HTTPFile object failed.";
168              
169 0           my %delete_opts;
170              
171 0 0         $delete_opts{ignore_missing} = 1
172             if $opts{ignore_missing};
173              
174 0 0         $httpfile->delete(%delete_opts)
175             or die "Deletion of file via HTTP failed.";
176              
177 0           my $sto = Mgd::get_store();
178 0           $sto->remove_fidid_from_devid($self->fidid, $self->devid);
179 0           $self->fid->update_devcount(no_lock => 1);
180             }
181              
182             1;
183              
184             __END__