1 package MogileFS
::Device
;
6 use MogileFS
::Util
qw(throw);
7 use MogileFS
::Util
qw(okay_args device_state error);
11 MogileFS::Device - device class
16 my $testing = $ENV{TESTING
} ?
1 : 0;
17 eval "sub TESTING () { $testing }";
20 my @observed_fields = qw
/observed_state utilization reject_bad_md5/;
21 my @fields = (qw
/hostid status weight mb_total mb_used mb_asof devid/,
25 my ($class, $args, $host_factory) = @_;
27 host_factory
=> $host_factory,
31 # FIXME: No guarantee (as of now?) that hosts get loaded before devs.
32 #$self->host || die "No host for $self->{devid} (host $self->{hostid})";
34 croak
"invalid device observed state '$self->{observed_state}', valid: writeable, readable, unreachable"
35 if $self->{observed_state
} && $self->{observed_state
} !~ /^(?:writeable|readable|unreachable)$/;
42 sub id
{ return $_[0]{devid
} }
43 sub devid
{ return $_[0]{devid
} }
44 sub name
{ return $_[0]{devid
} }
45 sub status
{ return $_[0]{status
} }
46 sub weight
{ return $_[0]{weight
} }
47 sub hostid
{ return $_[0]{hostid
} }
51 return $self->{host_factory
}->get_by_id($self->{hostid
});
54 # returns 0 if not known, else [0,1]
57 return 0 unless $self->{mb_total
} && defined $self->{mb_used
};
58 return 1 - ($self->{mb_used
} / $self->{mb_total
});
61 # returns undef if not known, else [0,1]
64 return undef unless $self->{mb_total
} && defined $self->{mb_used
};
65 return $self->{mb_used
} / $self->{mb_total
};
68 # FIXME: $self->mb_free?
71 my @tofetch = @_ ?
@_ : @fields;
72 my $ret = { (map { $_ => $self->{$_} } @tofetch),
73 'mb_free' => $self->mb_free };
78 return $_[0]->fields(@observed_fields);
81 sub observed_utilization
{
85 my $weight_varname = 'T_FAKE_IO_DEV' . $self->id;
86 return $ENV{$weight_varname} if defined $ENV{$weight_varname};
89 return $self->{utilization
};
93 my $host = $_[0]->host;
94 return ($host && $host->observed_reachable);
97 sub observed_writeable
{
99 return 0 unless $self->host_ok;
100 return $self->{observed_state
} && $self->{observed_state
} eq 'writeable';
103 sub observed_readable
{
105 return 0 unless $self->host_ok;
106 return $self->{observed_state
} && $self->{observed_state
} eq 'readable';
109 sub observed_unreachable
{
111 # host is unreachability implies device unreachability
112 return 1 unless $self->host_ok;
113 return $self->{observed_state
} && $self->{observed_state
} eq 'unreachable';
116 # FIXME: This pattern is weird. Store the object on new?
118 my $ds = device_state
($_[0]->status);
120 error
("dev$_[0]->{devid} has bogus status '$_[0]->{status}', pretending 'down'");
121 return device_state
("down");
124 sub can_delete_from
{
125 return $_[0]->dstate->can_delete_from;
128 # this method is for Monitor, other workers should use should_read_from
130 return $_[0]->host->alive && $_[0]->dstate->can_read_from;
133 # this is the only method a worker should call for checking for readability
134 sub should_read_from
{
135 return $_[0]->can_read_from && ($_[0]->observed_readable || $_[0]->observed_writeable);
138 # FIXME: Is there a (unrelated to this code) bug where new files aren't tested
139 # against the free space limit before being stored or replicated somewhere?
140 sub should_get_new_files
{
142 my $dstate = $self->dstate;
144 return 0 unless $dstate->should_get_new_files;
145 return 0 unless $self->observed_writeable;
146 return 0 unless $self->host->alive;
147 # have enough disk space? (default: 100MB)
148 my $min_free = MogileFS
->config("min_free_space");
149 return 0 if $self->{mb_total
} &&
150 $self->mb_free < $min_free;
157 return $self->{mb_total
} - $self->{mb_used
}
158 if $self->{mb_total
} && defined $self->{mb_used
};
162 return $_[0]->{mb_used
};
165 # currently the same policy, but leaving it open for differences later.
166 sub should_get_replicated_files
{
167 return $_[0]->should_get_new_files;
171 my ($self, @hosts) = @_;
172 my @hostids = map { ref($_) ?
$_->id : $_ } @hosts;
173 my $my_hostid = $self->id;
174 return (grep { $my_hostid == $_ } @hostids) ?
0 : 1;
177 # "cached" by nature of the monitor worker testing this.
178 sub doesnt_know_mkcol
{
179 return $_[0]->{no_mkcol
};
182 # Gross class-based singleton cache.
183 my %dir_made; # /dev<n>/path -> $time
184 my $dir_made_lastclean = 0;
186 # if no callback is supplied: returns 1 on success, 0 on failure
187 # if a callback is supplied, the return value will be passed to the callback
189 sub create_directory
{
190 my ($self, $uri, $cb) = @_;
191 if ($self->doesnt_know_mkcol || MogileFS
::Config
->server_setting_cached('skip_mkcol')) {
192 return $cb ?
$cb->(1) : 1;
195 # rfc2518 says we "should" use a trailing slash. Some servers
196 # (nginx) appears to require it.
197 $uri .= '/' unless $uri =~ m
/\
/$/;
199 if ($dir_made{$uri}) {
200 return $cb ?
$cb->(1) : 1;
204 my $on_mkcol_response = sub {
205 if ($res->is_success) {
207 $dir_made{$uri} = $now;
209 # cleanup %dir_made occasionally.
210 my $clean_interval = 300; # every 5 minutes.
211 if ($dir_made_lastclean < $now - $clean_interval) {
212 $dir_made_lastclean = $now;
213 foreach my $k (keys %dir_made) {
214 delete $dir_made{$k} if $dir_made{$k} < $now - 3600;
218 } elsif ($res->code =~ /\A(?:400|501)\z/) {
219 # if they don't support this method, remember that
220 # TODO: move this into method in *monitor* worker
221 $self->{no_mkcol
} = 1;
228 my %opts = ( headers
=> { "Content-Length" => "0" } );
229 $self->host->http("MKCOL", $uri, \
%opts, sub {
231 $cb->($on_mkcol_response->()) if $cb;
236 Danga
::Socket
->SetPostLoopCallback(sub { !defined $res });
237 Danga
::Socket
->EventLoop;
238 return $on_mkcol_response->();
242 my ($self, %opts) = @_;
243 my $limit = delete $opts{limit
};
244 croak
("No limit specified") unless $limit && $limit =~ /^\d+$/;
245 croak
("Unknown options to fid_list") if %opts;
247 my $sto = Mgd
::get_store
();
248 my $fidids = $sto->get_fidids_by_device($self->devid, $limit);
250 MogileFS
::FID
->new($_)
255 my ($self, %opts) = @_;
257 my $sto = Mgd
::get_store
();
258 # storage function does validation.
259 my $fidids = $sto->get_fidid_chunks_by_device(devid
=> $self->devid, %opts);
261 MogileFS
::FID
->new($_)
266 my ($self, $fid) = @_;
267 Mgd
::get_store
()->remove_fidid_from_devid($fid->id, $self->id);
273 my $host = $self->host;
274 my $get_port = $host->http_get_port;
275 my $hostip = $host->ip;
276 return "http://$hostip:$get_port/dev$self->{devid}/usage";
279 sub can_change_to_state
{
280 my ($self, $newstate) = @_;
281 # don't allow dead -> alive transitions. (yes, still possible
282 # to go dead -> readonly -> alive to bypass this, but this is
283 # all more of a user-education thing than an absolute policy)
284 return 0 if $self->dstate->is_perm_dead && $newstate eq 'alive';
288 sub vivify_directories
{
289 my ($self, $path) = @_;
291 # $path is something like:
292 # http://10.0.0.26:7500/dev2/0/000/148/0000148056.fid
294 # three directories we'll want to make:
295 # http://10.0.0.26:7500/dev2/0
296 # http://10.0.0.26:7500/dev2/0/000
297 # http://10.0.0.26:7500/dev2/0/000/148
299 croak
"non-HTTP mode no longer supported" unless $path =~ /^http/;
300 return 0 unless $path =~ m!/dev(\d+)/(\d+)/(\d\d\d)/(\d\d\d)/\d+\.fid$!;
301 my ($devid, $p1, $p2, $p3) = ($1, $2, $3, $4);
303 die "devid mismatch" unless $self->id == $devid;
305 $self->create_directory("/dev$devid/$p1");
306 $self->create_directory("/dev$devid/$p1/$p2");
307 $self->create_directory("/dev$devid/$p1/$p2/$p3");
310 # Compatibility interface since this old routine is unfortunately called
311 # internally within plugins. This data should be passed into any hooks which
313 # Currently an issue with MogileFS::Network + ZoneLocal
314 # Remove this in 2012.
316 return Mgd
::device_factory
()->get_all;
320 return $_[0]->{reject_bad_md5
};