7 use Time::HiRes qw(sleep);
13 $ENV{T_FAKE_IO_DEV1} = 95; # Simulating high device load (should get fewer requests).
14 $ENV{T_FAKE_IO_DEV2} = 5; # Simulating low device load (should get more requests).
18 find_mogclient_or_skip();
20 # create temp mysql db,
21 # use mogadm to init it,
22 # mogstored on temp dir,
23 # register mogstored temp dir,
28 my $sto = eval { temp_store(); };
30 plan skip_all => "Can't create temporary test database: $@";
39 $mogroot{1} = File::Temp::tempdir( CLEANUP => 1 );
40 $mogroot{2} = File::Temp::tempdir( CLEANUP => 1 );
41 my $dev2host = { 1 => 1, 2 => 2, };
42 foreach (sort { $a <=> $b } keys %$dev2host) {
43 my $root = $mogroot{$dev2host->{$_}};
44 mkdir("$root/dev$_") or die "Failed to create dev$_ dir: $!";
47 my $ms1 = create_mogstored("127.0.1.1", $mogroot{1});
48 ok($ms1, "got mogstored1");
49 my $ms2 = create_mogstored("127.0.1.2", $mogroot{2});
50 ok($ms1, "got mogstored2");
52 while (! -e "$mogroot{1}/dev1/usage" &&
53 ! -e "$mogroot{2}/dev2/usage") {
54 print "Waiting on usage...\n";
58 my $tmptrack = create_temp_tracker($sto);
61 my $mogc = MogileFS::Client->new(
63 hosts => [ "127.0.0.1:7001" ],
65 my $be = $mogc->{backend}; # gross, reaching inside of MogileFS::Client
67 # test some basic commands to backend
68 ok($tmptrack->mogadm("domain", "add", "testdom"), "created test domain");
69 ok($tmptrack->mogadm("class", "add", "testdom", "2copies", "--mindevcount=2"), "created 2copies class in testdom");
71 ok($tmptrack->mogadm("host", "add", "hostA", "--ip=127.0.1.1", "--status=alive"), "created hostA");
72 ok($tmptrack->mogadm("host", "add", "hostB", "--ip=127.0.1.2", "--status=alive"), "created hostB");
74 ok($tmptrack->mogadm("device", "add", "hostA", 1), "created dev1 on hostA");
75 ok($tmptrack->mogadm("device", "add", "hostB", 2), "created dev2 on hostB");
77 # just ensure the "set_weight" command doesn't ERR out
78 ok($tmptrack->mogadm("device", "modify", "hostA", 1, "--weight=50"), "set dev1 weight=50 on hostA");
79 ok($tmptrack->mogadm("device", "modify", "hostB", 2, "--weight=50"), "set dev2 weight=50 on hostB");
83 my $was = $be->{timeout}; # can't use local on phash :(
85 ok($be->do_request("clear_cache", {}), "waited for monitor")
86 or die "Failed to wait for monitor";
87 ok($be->do_request("clear_cache", {}), "waited for monitor")
88 or die "Failed to wait for monitor";
89 $be->{timeout} = $was;
92 # create one sample file
93 my $fh = $mogc->new_file("file1", "2copies");
94 ok($fh, "got filehandle");
96 die "Error: " . $mogc->errstr;
99 my $data = "My test file.\n" x 1024;
101 ok(close($fh), "closed file");
103 # wait for it to replicate
106 while ($tries++ < 40 && (@urls = $mogc->get_paths("file1")) < 2) {
109 is(scalar @urls, 2, "replicated to 2 paths");
110 my $to_repl_rows = $dbh->selectrow_array("SELECT COUNT(*) FROM file_to_replicate");
111 is($to_repl_rows, 0, "no more files to replicate");
115 @urls = $mogc->get_paths("file1");
116 my ($devno) = $urls[0] =~ m!/dev(\d+)/!;
120 ok($stats{1} < 15, "Device 1 should get roughly 5% of traffic, got: $stats{1}");
121 ok($stats{2} > 80, "Device 2 should get roughly 95% of traffic, got: $stats{2}");