new_file: don't pass private field to create_open
[ruby-mogilefs-client.git] / examples / stale_fid_checker.rb
blob2dab866fd8d060cd06268b29cd65ec02073b1d87
1 #!/usr/bin/env ruby
2 # This requires:
3 # * net-http-persistent RubyGem
4 # * Ruby 1.9.2+
5 # * upstream MogileFS::Server 2.45 or later
6 $stdout.sync = $stderr.sync = true
7 usage = <<EOF
8 Usage: #$0 -t TRACKERS"
10 The output of this script can be piped to awk + curl to DELETE the files:
11 #$0 -t TRACKERS | awk '{system("curl -XDELETE "$3)}'
12 EOF
14 require 'uri'
15 require 'optparse'
16 require 'mogilefs'
17 require 'net/http/persistent'
18 Thread.abort_on_exception = true
19 MogileFS::VERSION <= "3.0.0" and
20   abort "Upgrade mogilefs-client (to a version that distributes this script)" \
21         "MogileFS::Admin#each_fid is probably broken in this version"
23 trackers = []
24 ARGV.options do |x|
25   x.banner = usage.strip
26   x.separator ''
27   x.on('-t', '--trackers=host1[,host2]', '--hosts=host1[,host2]',
28        Array, 'hostnames/IP addresses of trackers') do |args|
29     trackers = args
30   end
32   x.on('-h', '--help', 'Show this help message.') { puts x; exit }
33   x.parse!
34 end
36 adm = MogileFS::Admin.new(:hosts => trackers)
37 NHP = Net::HTTP::Persistent.new(File.basename($0))
38 client = MogileFS::MogileFS.new(:hosts => trackers, :domain => "none")
40 def start_perdev_thread(pfx)
41   todo = Queue.new
42   done = Queue.new
43   Thread.new do
44     while fid_path = todo.shift
45       path = "#{pfx}#{fid_path}"
46       uri = URI.parse(path)
47       begin
48         resp = NHP.request(uri, Net::HTTP::Head.new(uri.path))
49         done << [ path, resp ]
50       rescue => err
51         done << [ path, err ]
52       end
53     end
54   end
55   [ todo, done ]
56 end
58 def setup_devices(dev_map, adm)
59   hosts = {}
60   adm.get_hosts.each do |host|
61     hosts[host["hostid"]] = "http://#{host['hostip']}:#{host['http_port']}/"
62   end
64   adm.get_devices.each do |device|
65     pfx = hosts[device["hostid"]] + "dev#{device['devid']}"
66     todo, done = start_perdev_thread(pfx)
67     dev_map[todo] = done
68   end
69 end
71 def check(bad, curfid, rv)
72   path, resp = rv
73   case resp
74   when Net::HTTPNotFound # good
75   when Net::HTTPOK
76     bad << "#{curfid} #{resp.content_length} #{path}\n"
77   else
78     warn "E: #{resp.inspect} (#{resp.class}) #{path}"
79   end
80 end
82 dev_map = {}
83 setup_devices(dev_map, adm)
84 next_fid = 0
85 adm.each_fid do |fid|
86   fidid = fid["fid"]
88   if fidid != next_fid
89     (next_fid..(fidid - 1)).each do |curfid|
90       nfid = sprintf("%010u", curfid)
91       /\A(\d)(\d{3})(\d{3})(?:\d{3})\z/ =~ nfid
92       fid_path = "/#$1/#$2/#$3/#{nfid}.fid"
93       bad = []
94       dev_map.each_key { |todo| todo << fid_path }
95       dev_map.each_value { |done| check(bad, curfid, done.shift) }
96       next if bad.empty?
98       begin
99         info = client.file_debug(curfid)
100         abort "BUG: #{info.inspect} found!" if info["fid_dkey"]
101       rescue MogileFS::Backend::UnknownFidError
102       end
104       puts bad.join
105     end
106   end
107   next_fid = fidid + 1