checksums: use a low-priority task queue for fsck digests
[MogileFS-Server.git] / lib / MogileFS / DevFID.pm
blobf62c05798383cbbb7f9832de3cf061325bc8c77b
1 package MogileFS::DevFID;
2 use strict;
3 use warnings;
4 use MogileFS::Server;
5 use overload '""' => \&as_string;
6 use Carp qw(croak);
8 sub new {
9 my ($class, $devarg, $fidarg) = @_;
10 return bless {
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;
18 # --------------------------------------------------------------------------
20 sub devid { $_[0]{devid} }
21 sub fidid { $_[0]{fidid} }
23 sub as_string {
24 "DevFID[d=" . $_[0]{devid} . ";f=" . $_[0]{fidid} . "]";
27 sub device {
28 my $self = shift;
29 return $self->{dev} ||= Mgd::device_factory()->get_by_id($self->{devid});
32 sub fid {
33 my $self = shift;
34 return $self->{fid} ||= MogileFS::FID->new($self->{fidid});
37 # returns true if DevFID actually exists in database
38 sub exists {
39 my $self = shift;
40 my $fid = $self->fid;
41 return (grep { $_ == $self->{devid} } $fid->devids) ? 1 : 0;
44 sub url {
45 my $self = shift;
46 return $self->_make_full_url(0);
49 sub get_url {
50 my $self = shift;
51 return $self->_make_full_url(1);
54 sub vivify_directories {
55 my $self = shift;
56 my $url = $self->url;
57 $self->device()->vivify_directories($url);
60 # returns 0 on missing,
61 # undef on connectivity error,
62 # else size of file on disk (after HTTP HEAD or mogstored stat)
63 sub size_on_disk {
64 my $self = shift;
65 my $url = $self->get_url;
67 # check that it has size (>0) and is reachable (not undef)
68 return MogileFS::HTTPFile->at($url)->size;
71 # returns -1 on missing,
72 # undef on connectivity error,
73 # else checksum of file on disk (after HTTP GET or mogstored read)
74 sub checksum_on_disk {
75 my ($self, $alg, $ping_cb, $reason) = @_;
76 my $url = $self->get_url;
78 # check that it has size (>0) and is reachable (not undef)
79 return MogileFS::HTTPFile->at($url)->digest($alg, $ping_cb, $reason);
82 # returns true if size seen matches fid's length
83 sub size_matches {
84 my $self = shift;
85 my $fid = $self->fid;
86 my $disk_size = $self->size_on_disk;
88 # Temporary connectivity error with that disk/machine..
89 return 0 unless defined $disk_size;
90 return 0 if $disk_size == MogileFS::HTTPFile::FILE_MISSING;
92 return $disk_size == $fid->length;
95 # returns just the URI path component without scheme/host
96 sub uri_path {
97 my $self = shift;
98 my $devid = $self->{devid};
99 my $fidid = $self->{fidid};
101 my $nfid;
102 my $len = length $fidid;
103 if ($len < 10) {
104 $nfid = '0' x (10 - $len) . $fidid;
105 } else {
106 $nfid = $fidid;
108 my ( $b, $mmm, $ttt, $hto ) = ( $nfid =~ m{(\d)(\d{3})(\d{3})(\d{3})} );
110 return "/dev$devid/$b/$mmm/$ttt/$nfid.fid";
113 sub _make_full_url {
114 # set use_get_port to be true to specify to use the get port
115 my ($self, $use_get_port) = @_;
117 # get some information we'll need
118 my $dev = $self->device or return undef;
119 my $host = $dev->host or return undef;
121 my $path = $self->uri_path;
122 my $hostip = $host->ip;
123 my $port = $use_get_port ? $host->http_get_port : $host->http_port;
125 return "http://$hostip:$port$path";
128 sub add_to_db {
129 my ($self, $no_lock) = @_;
130 croak("fidid not non-zero") unless $self->{fidid};
131 croak("devid not non-zero") unless $self->{devid};
133 my $sto = Mgd::get_store();
134 if ($sto->add_fidid_to_devid($self->{fidid}, $self->{devid})) {
135 return $self->fid->update_devcount(no_lock => $no_lock);
136 } else {
137 # was already on that device
138 return 1;
142 # Destroy a particular replica of a file via HTTP, remove it
143 # from the tracker, and update the replication counts to be
144 # accurate again.
145 sub destroy {
146 my $self = shift;
147 my %opts = @_;
149 my $httpfile = MogileFS::HTTPFile->at($self->url)
150 or die "Creation of HTTPFile object failed.";
152 my %delete_opts;
154 $delete_opts{ignore_missing} = 1
155 if $opts{ignore_missing};
157 $httpfile->delete(%delete_opts)
158 or die "Deletion of file via HTTP failed.";
160 my $sto = Mgd::get_store();
161 $sto->remove_fidid_from_devid($self->fidid, $self->devid);
162 $sto->update_devcount($self->fidid);
167 __END__
169 =head1 NAME
171 MogileFS::DevFID - represents a FID on a device
173 =head1 ABOUT
175 This class represents the (devid, fidid) tuple. That is, a specific
176 version on a file on a specific device. See L<MogileFS::Device> and
177 L<MogileFS::FID>.