9 use MogileFS::Util qw(error_code);
16 find_mogclient_or_skip();
18 # use mogadm to init it,
19 # mogstored on temp dir,
20 # register mogstored temp dir,
25 my $sto = eval { temp_store(); };
27 plan skip_all => "Can't create temporary test database: $@";
34 my ($hostA_ip, $hostB_ip, $hostC_ip) = (qw/127.0.1.1 127.0.1.2 127.0.1.3/);
38 $mogroot{1} = File::Temp::tempdir( CLEANUP => 1 );
39 $mogroot{2} = File::Temp::tempdir( CLEANUP => 1 );
40 $mogroot{3} = File::Temp::tempdir( CLEANUP => 1 );
41 my $dev2host = { 1 => 1, 2 => 1,
44 foreach (sort { $a <=> $b } keys %$dev2host) {
45 my $root = $mogroot{$dev2host->{$_}};
46 mkdir("$root/dev$_") or die "Failed to create dev$_ dir: $!";
49 my $ms1 = create_mogstored($hostA_ip, $mogroot{1});
50 ok($ms1, "got mogstored1");
51 my $ms2 = create_mogstored($hostB_ip, $mogroot{2});
52 ok($ms2, "got mogstored2");
54 while (! -e "$mogroot{1}/dev1/usage" &&
55 ! -e "$mogroot{2}/dev4/usage") {
56 print "Waiting on usage...\n";
60 my $tmptrack = create_temp_tracker($sto);
63 my $mogc = MogileFS::Client->new(
65 hosts => [ "127.0.0.1:7001" ],
67 my $be = $mogc->{backend}; # gross, reaching inside of MogileFS::Client
69 my $lasttime = 1167609600; # Mon Jan 1 00:00:00 UTC 2007
71 my $timestamp = $dbh->selectrow_array("SELECT ".$sto->unix_timestamp);
72 # FIXME: Some databases might be pedantic about the FROM
73 # but having it on others means that if the table has no rows
74 # we won't get any results!
75 my $rv = $timestamp > $lasttime;
76 $lasttime = $timestamp;
78 }), "Store provides sane unix_timestamp");
80 # test some basic commands to backend
81 ok($be->do_request("test", {}), "test ping worked");
82 ok(!$be->do_request("test", {crash => 1}), "crash didn't");
83 ok($be->do_request("test", {}), "test ping again worked");
86 my $c = IO::Socket::INET->new(PeerAddr => '127.0.0.1:7001', Timeout => 3);
87 ok(want($c, 1, "queryworker"), "set 1 queryworker");
89 my $expect = "ERR no_domain No+domain+provided\r\n" x 2;
91 # bad domain won't return twice
92 my $cmd = "list_keys domain=\r\n";
93 $c->syswrite($cmd x 2);
97 $r = $c->sysread(my $buf, 500);
98 if (defined $r && $r > 0) {
101 } while ($r && length($resp) != length($expect));
102 is($resp, $expect, "response matches expected");
104 ok(want($c, 2, "queryworker"), "restored 2 queryworkers");
107 ok($tmptrack->mogadm("domain", "add", "todie"), "created todie domain");
108 ok($tmptrack->mogadm("domain", "delete", "todie"), "delete todie domain");
109 ok(!$tmptrack->mogadm("domain", "delete", "todie"), "didn't delete todie domain again");
111 # ensure "default" class is removed when its domain is removed
114 my $before = Dumper($sto->get_all_classes);
115 ok($tmptrack->mogadm("domain", "add", "def"), "created def domain");
117 my $dmid = $sto->get_domainid_by_name("def");
118 ok(defined($dmid), "def dmid retrieved");
120 isnt($sto->domain_has_classes($dmid), "domain_has_classes does not show default class");
121 ok($tmptrack->mogadm("class", "modify", "def", "default", "--mindevcount=3"), "modified default to have mindevcount=3");
123 my $classid = $sto->get_classid_by_name($dmid, "default");
124 is($classid, 0, "default class has classid=0");
125 isnt($sto->domain_has_classes($dmid), "domain_has_classes does not show default class");
126 ok($tmptrack->mogadm("domain", "delete", "def"), "remove def domain");
127 is($sto->get_domainid_by_name("def"), undef, "def nonexistent");
128 is($sto->get_classid_by_name($dmid, "default"), undef, "def/default class nonexistent");
130 my $after = Dumper($sto->get_all_classes);
131 is($after, $before, "class listing is unchanged");
134 ok($tmptrack->mogadm("domain", "add", "hasclass"), "created hasclass domain");
135 ok($tmptrack->mogadm("class", "add", "hasclass", "nodel"), "created nodel class");
136 ok(!$tmptrack->mogadm("domain", "delete", "hasclass"), "didn't delete hasclass domain");
137 ok($tmptrack->mogadm("class", "delete", "hasclass", "nodel"), "created nodel class");
138 ok($tmptrack->mogadm("domain", "delete", "hasclass"), "didn't delete hasclass domain");
140 ok($tmptrack->mogadm("domain", "add", "testdom"), "created test domain");
141 ok($tmptrack->mogadm("class", "add", "testdom", "1copy", "--mindevcount=1"), "created 1copy class in testdom");
142 ok($tmptrack->mogadm("class", "add", "testdom", "2copies", "--mindevcount=2"), "created 2copies class in testdom");
143 ok($tmptrack->mogadm("class", "add", "testdom", "poltest", "--replpolicy=MultipleHosts(3)"),
144 "created a specific policy class");
146 ok($tmptrack->mogadm("host", "add", "hostA", "--ip=$hostA_ip", "--status=alive"), "created hostA");
147 ok($tmptrack->mogadm("host", "add", "hostB", "--ip=$hostB_ip", "--status=alive"), "created hostB");
149 ok($tmptrack->mogadm("device", "add", "hostA", 1), "created dev1 on hostA");
150 ok($tmptrack->mogadm("device", "add", "hostA", 2), "created dev2 on hostA");
151 ok($tmptrack->mogadm("device", "add", "hostB", 3), "created dev3 on hostB");
152 ok($tmptrack->mogadm("device", "add", "hostB", 4), "created dev4 on hostB");
154 #ok($tmptrack->mogadm("device", "mark", "hostA", 1, "alive"), "dev1 alive");
155 #ok($tmptrack->mogadm("device", "mark", "hostA", 2, "alive"), "dev2 alive");
156 #ok($tmptrack->mogadm("device", "mark", "hostB", 3, "alive"), "dev3 alive");
157 #ok($tmptrack->mogadm("device", "mark", "hostB", 4, "alive"), "dev4 alive");
161 my $was = $be->{timeout}; # can't use local on phash :(
163 ok($be->do_request("clear_cache", {}), "waited for monitor")
164 or die "Failed to wait for monitor";
165 ok($be->do_request("clear_cache", {}), "waited for monitor")
166 or die "Failed to wait for monitor";
167 $be->{timeout} = $was;
171 my $fh = $mogc->new_file('no_content', "2copies");
172 die "Error: " . $mogc->errstr unless $fh;
173 ok(close($fh), "closed file");
177 my $fh = $mogc->new_file('no_content', "2copies");
178 die "Error: " . $mogc->errstr unless $fh;
179 ok(close($fh), "closed file");
182 # wait for it to replicate
184 my @urls = $mogc->get_paths("no_content");
187 diag("no_content still only on $nloc devices");
191 }), "replicated to 2 paths");
194 my $to_repl_rows = $dbh->selectrow_array("SELECT COUNT(*) FROM file_to_replicate");
195 return $to_repl_rows == 0;
196 }), "no more files to replicate");
199 ok($mogc->delete("no_content"), "deleted no_content")
200 or die "Error: " . $mogc->errstr;
202 # create two sample files
203 my $data = "My test file.\n" x 1024;
204 foreach my $k (qw(file1 file2)) {
205 my $fh = $mogc->new_file($k, "2copies");
206 ok($fh, "got filehandle") or
207 die "Error: " . $mogc->errstr;
209 ok(close($fh), "closed file");
213 ok($mogc->delete("file2"), "deleted file2")
214 or die "Error: " . $mogc->errstr;
216 # verify we can't delete the domain now
217 ok(!$tmptrack->mogadm("domain", "delete", "testdom"), "can't delete domain in use");
219 # wait for it to replicate
222 @urls = $mogc->get_paths("file1");
225 diag("file1 still only on $nloc devices");
229 }), "replicated to 2 paths");
232 my $to_repl_rows = $dbh->selectrow_array("SELECT COUNT(*) FROM file_to_replicate");
233 return $to_repl_rows == 0;
234 }), "no more files to replicate");
236 my $p1 = MogPath->new($urls[0]);
237 my $p2 = MogPath->new($urls[1]);
238 isnt($p1->host, $p2->host, "host1 and host2 are different");
239 my $path1 = $mogroot{$dev2host->{$p1->device}} . $p1->path;
240 my $path2 = $mogroot{$dev2host->{$p2->device}} . $p2->path;
241 is(-s $path1, length($data), "right length on disk for path1");
242 is(-s $path2, length($data), "right length on disk for path2");
244 ok(unlink($path1), "deleted path $path1");
245 my $dead_url = $urls[0];
247 @urls = $mogc->get_paths("file1");
248 isnt($urls[0], $dead_url, "didn't return dead url first (try $_)");
251 # Tests for updateclass command
253 my $fh = $mogc->new_file('file1copy', "1copy");
254 ok($fh, "got filehandle") or
255 die "Error: " . $mogc->errstr;
256 print $fh 'EXAMPLE DATA';
257 ok(close($fh), "closed file");
259 is scalar($mogc->get_paths("file1copy")), 1, 'File is on 1 device';
261 ok($mogc->update_class('file1copy', '2copies'), "updated class to 2 copies");
263 # wait for it to replicate
265 my @urls = $mogc->get_paths("file1copy");
268 diag("no_content still only on $nloc devices");
272 }), "replicated to 2 paths");
274 ok($mogc->update_class('file1copy', 'default'), "updated class to default");
276 ok($mogc->delete("file1copy"), "deleted updateclass testfile file1copy")
277 or die "Error: " . $mogc->errstr;
280 ok($be->do_request("rename", {
282 to_key => "file1renamed",
284 }), "renamed file1 to file1renamed");
286 ok($be->do_request("delete", {
287 key => "file1renamed",
289 }), "deleted file1renamed");
291 # create a couple hundred files now
293 diag("Creating $n_files files...");
294 for my $n (1..$n_files) {
295 my $fh = $mogc->new_file("manyhundred_$n", "2copies")
296 or die "Failed to create manyhundred_$n: " . $mogc->errstr;
297 my $data = "File number $n.\n" x 512;
299 close($fh) or die "Failed to close manyhundred_$n";
300 diag("created $n/$n_files") if $n % 10 == 0;
302 pass("Created a ton of files");
304 # wait for replication to go down
310 $to_repl_rows = $dbh->selectrow_array("SELECT COUNT(*) FROM file_to_replicate");
311 last if ! $to_repl_rows;
312 diag("Files to replicate: $to_repl_rows");
315 die "Failed to replicate all $n_files files" if $to_repl_rows;
316 pass("Replicated all $n_files files");
319 # now let's delete a host, which should fail hard, because there are still devices attached to it
321 die "Can't delete an active host" if
322 $tmptrack->mogadm("host", "delete", "hostB");
323 pass("didn't delete hostB");
326 # create a new host and device, for when we start killing some devices
327 my $ms3 = create_mogstored($hostC_ip, $mogroot{3});
328 ok($ms3, "got mogstored3");
329 ok($tmptrack->mogadm("host", "add", "hostC", "--ip=$hostC_ip", "--status=alive"), "created hostC");
330 ok($tmptrack->mogadm("device", "add", "hostC", 5), "created dev5 on hostC");
331 ok($tmptrack->mogadm("device", "add", "hostC", 6), "created dev6 on hostC");
333 ok($tmptrack->mogadm("device", "mark", "hostB", 3, "dead"), "marked device B/3 dead");
334 ok($tmptrack->mogadm("device", "mark", "hostB", 4, "dead"), "marked device B/4 dead");
338 my $sth = $dbh->prepare("SELECT devid, COUNT(*) FROM file_on GROUP BY devid");
340 while (my ($devid, $ct) = $sth->fetchrow_array) {
343 diag("Replication update: " . join(", ", map { "dev$_: " . sprintf("%3d", ($has{$_}||0)) } (1..6)));
344 return 0 if $has{3} || $has{4};
345 return $has{1} && $has{1} && $has{5} && $has{6};
346 }), "files replicated to hostC from hostB");
349 # hosts are no longer able to be nuked even if they have deleted devices.
350 # this saves us from some subtle bugs.
351 #ok($tmptrack->mogadm("host", "delete", "hostB"), "killed hostB");
353 # delete them all, see if they go away.
354 for my $n (1..$n_files) {
355 my $rv = $mogc->delete("manyhundred_$n")
356 or die "Failed to delete manyhundred_$n";
358 pass("deleted all $n_files files");
362 foreach my $hn (1, 3) {
363 my @lfiles = `find $mogroot{$hn} -type f -name '*.fid'`;
364 push @files, @lfiles;
365 diag("files on host $hn = " . scalar(@lfiles));
368 }), "and they're gone from filesystem");
370 foreach my $t (qw(file file_on file_to_delete)) {
372 return $dbh->selectrow_array("SELECT COUNT(*) FROM $t") == 0;
373 }), "table $t is empty");
376 # Test some broken client modes.
378 my $c = IO::Socket::INET->new(PeerAddr => '127.0.0.1:7001',
380 die "Failed to connect to test tracker" unless $c;
381 # Pretend to upload a file, then tell the server weird things.
382 # Not trying to be defensable to all sorts of things, but ensuring we're
383 # safe against double close, bad destdev, etc.
384 print $c "create_open "
385 . "domain=testdom&fid=0&class=&multi_dest=1&key=fufufu\n";
388 ok($res =~ m/fid=(\d+)/, "bare create_open worked");
390 # Pretend we uploaded something.
391 print $c "create_close "
392 . "domain=testdom&fid=$fidid&devid=4&size=0&key=fufufu"
393 . "&path=http://127.0.1.2:7500/dev4/0/000/000/0000000$fidid.fid\n";
395 ok($res2 =~ m/invalid_destdev/, "cannot upload to unlisted destdev");
397 # TODO: test double closing, etc.
400 # give an explicit fid, to prevent bugs like the reason behind
401 # commit ac5534a0c3d046e660fa7581c9173857f182bd81
402 # This is functionality is a bad idea otherwise.
404 my $expfid = 2147483632;
405 my $opts = { fid => $expfid };
406 my $fh = $mogc->new_file("explicit_fid", "1copy", 2, $opts);
407 die "Error: " . $mogc->errstr unless $fh;
408 ok((print $fh "hi" ), "wrote 2 bytes");
409 ok(close($fh), "closed file");
410 my $info = $mogc->file_info("explicit_fid");
412 is($info->{devcount}, 1, "devcount is 1");
413 is($info->{fid}, $opts->{fid}, "explicit fid is correctly set");
417 my $fh = $mogc->new_file("0", "1copy");
418 ok((print $fh "zero\n"), "wrote to file");
419 ok(close($fh), "closed file");
421 my $info = $mogc->file_info("0");
422 is("HASH", ref($info), "file_info returned a hash");
423 is("0", $info->{key}, "key matches 0");
424 is("1copy", $info->{class}, "class matches for 0 key");
426 my @paths = $mogc->get_paths("0");
427 is(1, scalar(@paths), "path returned for 0");
429 $mogc->rename("0", "zero");
430 is($info->{fid}, $mogc->file_info("zero")->{fid}, "rename from 0");
431 $mogc->rename("zero", "0");
432 is($info->{fid}, $mogc->file_info("0")->{fid}, "rename to 0");
433 $mogc->update_class("0", "2copies");
434 is("2copies", $mogc->file_info("0")->{class}, "class updated for 0 key");
436 my $debug = $mogc->file_debug(key => "0");
437 is($debug->{fid_fid}, $info->{fid}, "FID from debug matches");
438 is($debug->{fid_dkey}, "0", "key from debug matches");
440 ok($mogc->delete("0"), "delete 0 works");
443 # ensure all workers can be stopped/started
445 my $c = IO::Socket::INET->new(PeerAddr => '127.0.0.1:7001', Timeout => 3);
446 my @jobs = qw(fsck queryworker delete replicate reaper monitor job_master);
448 foreach my $j (@jobs) {
449 ok(want($c, 0, $j), "shut down all $j");
451 foreach my $j (@jobs) {
452 ok(want($c, 1, $j), "start 1 $j");
456 # list_keys with underscore
458 my $c = IO::Socket::INET->new(PeerAddr => '127.0.0.1:7001', Timeout => 3);
460 foreach my $k (qw(under_score under.score)) {
461 my $fh = $mogc->new_file($k, '1copy');
462 ok($fh, "got filehandle for $k");
463 ok(close($fh), "created file $k");
466 # we only have one queryworker from the previous test, so no need to
467 # clear cache twice and wait on monitor after "mogadm settings set"
468 foreach my $cslk (qw(on off)) {
469 ok($tmptrack->mogadm("settings", "set", "case_sensitive_list_keys", $cslk), "case_sensitive_list_keys = $cslk");
470 ok($be->do_request("clear_cache", {}), "cleared_cache");
471 my @l = $mogc->list_keys("under_");
472 is_deeply(['under_score', [ 'under_score' ]], \@l, "list_keys handled underscore properly (case-sensitive $cslk)");
476 $sto->set_server_setting('case_sensitive_list_keys', undef);