create_open: parallelize directory vivification
[MogileFS-Server.git] / lib / MogileFS / DevFID.pm
blob52bffc19f2a31334d405192eff9b46311e7c6cca
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, $cb) = @_;
56 my $url = $self->url;
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)
64 sub size_on_disk {
65 my ($self, $cb) = @_;
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);
73 } else {
74 # monitor says we cannot read from this device, so do not try
76 Danga::Socket->AddTimer(0, sub { $cb->(undef) }) if $cb;
77 return undef;
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
97 sub size_matches {
98 my $self = shift;
99 my $fid = $self->fid;
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
110 sub uri_path {
111 my $self = shift;
112 my $devid = $self->{devid};
113 my $fidid = $self->{fidid};
115 my $nfid;
116 my $len = length $fidid;
117 if ($len < 10) {
118 $nfid = '0' x (10 - $len) . $fidid;
119 } else {
120 $nfid = $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";
127 sub _make_full_url {
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";
142 sub add_to_db {
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);
153 } else {
154 # was already on that device
155 return 1;
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 my $self = shift;
164 my %opts = @_;
166 my $httpfile = MogileFS::HTTPFile->at($self->url)
167 or die "Creation of HTTPFile object failed.";
169 my %delete_opts;
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);
184 __END__
186 =head1 NAME
188 MogileFS::DevFID - represents a FID on a device
190 =head1 ABOUT
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
194 L<MogileFS::FID>.