1 package MogileFS
::DevFID
;
5 use overload
'""' => \
&as_string
;
9 my ($class, $devarg, $fidarg) = @_;
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,
18 # --------------------------------------------------------------------------
20 sub devid
{ $_[0]{devid
} }
21 sub fidid
{ $_[0]{fidid
} }
24 "DevFID[d=" . $_[0]{devid
} . ";f=" . $_[0]{fidid
} . "]";
29 return $self->{dev
} ||= Mgd
::device_factory
()->get_by_id($self->{devid
});
34 return $self->{fid
} ||= MogileFS
::FID
->new($self->{fidid
});
37 # returns true if DevFID actually exists in database
41 return (grep { $_ == $self->{devid
} } $fid->devids) ?
1 : 0;
46 return $self->_make_full_url(0);
51 return $self->_make_full_url(1);
54 sub vivify_directories
{
57 $self->device()->vivify_directories($url, $cb);
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)
67 if ($self->device->should_read_from) {
68 my $url = $self->get_url;
69 my $httpfile = $self->{_httpfile_get
} ||= MogileFS
::HTTPFile
->at($url);
71 # check that it has size (>0) and is reachable (not undef)
72 return $httpfile->size($cb);
74 # monitor says we cannot read from this device, so do not try
76 Danga
::Socket
->AddTimer(0, sub { $cb->(undef) }) if $cb;
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 my ($self, $alg, $ping_cb, $reason) = @_;
87 return undef unless $self->device->should_read_from;
89 my $url = $self->get_url;
90 my $httpfile = $self->{_httpfile_get
} ||= MogileFS
::HTTPFile
->at($url);
92 # check that it has size (>0) and is reachable (not undef)
93 return $httpfile->digest($alg, $ping_cb, $reason);
96 # returns true if size seen matches fid's length
100 my $disk_size = $self->size_on_disk;
102 # Temporary connectivity error with that disk/machine..
103 return 0 unless defined $disk_size;
104 return 0 if $disk_size == MogileFS
::HTTPFile
::FILE_MISSING
;
106 return $disk_size == $fid->length;
109 # returns just the URI path component without scheme/host
112 my $devid = $self->{devid
};
113 my $fidid = $self->{fidid
};
116 my $len = length $fidid;
118 $nfid = '0' x
(10 - $len) . $fidid;
122 my ( $b, $mmm, $ttt, $hto ) = ( $nfid =~ m{(\d)(\d{3})(\d
{3})(\d
{3})} );
124 return "/dev$devid/$b/$mmm/$ttt/$nfid.fid";
128 # set use_get_port to be true to specify to use the get port
129 my ($self, $use_get_port) = @_;
131 # get some information we'll need
132 my $dev = $self->device or return undef;
133 my $host = $dev->host or return undef;
135 my $path = $self->uri_path;
136 my $hostip = $host->ip;
137 my $port = $use_get_port ?
$host->http_get_port : $host->http_port;
139 return "http://$hostip:$port$path";
143 my ($self, $no_lock) = @_;
144 croak
("fidid not non-zero") unless $self->{fidid
};
145 croak
("devid not non-zero") unless $self->{devid
};
147 my $sto = Mgd
::get_store
();
148 if ($sto->add_fidid_to_devid($self->{fidid
}, $self->{devid
})) {
149 if (my $memc = MogileFS
::Config
->memcache_client) {
150 $memc->delete("mogdevids:$self->{fidid}");
152 return $self->fid->update_devcount(no_lock
=> $no_lock);
154 # was already on that device
159 # Destroy a particular replica of a file via HTTP, remove it
160 # from the tracker, and update the replication counts to be
166 my $httpfile = MogileFS
::HTTPFile
->at($self->url)
167 or die "Creation of HTTPFile object failed.";
171 $delete_opts{ignore_missing
} = 1
172 if $opts{ignore_missing
};
174 $httpfile->delete(%delete_opts)
175 or die "Deletion of file via HTTP failed.";
177 my $sto = Mgd
::get_store
();
178 $sto->remove_fidid_from_devid($self->fidid, $self->devid);
179 $self->fid->update_devcount(no_lock
=> 1);
188 MogileFS::DevFID - represents a FID on a device
192 This class represents the (devid, fidid) tuple. That is, a specific
193 version on a file on a specific device. See L<MogileFS::Device> and