bigfile: fix retry on rebalanced keys
[ruby-mogilefs-client.git] / lib / mogilefs / bigfile.rb
blobb681a089250ea7ebdea347eb04005d5d01562ed0
1 # -*- encoding: binary -*-
2 require 'uri'
3 require 'mogilefs/util'
5 # Used for reading deprecated "bigfile" objects generated by the deprecated
6 # mogtool(1) utility.  This is for reading legacy data and not recommended for
7 # new projects.  MogileFS itself is capable of storing standalone objects
8 # of arbitrary length (as long as the underlying database and underlying
9 # filesystem on the DAV devices accept them).
11 module MogileFS::Bigfile
12   # VALID_TYPES = %w(file tarball partition).map { |x| x.freeze }.freeze
14   # returns a big_info hash if successful
15   def bigfile_stat(key)
16     bigfile_parse_info(get_file_data(key))
17   end
19   # returns total bytes written and the big_info hash if successful, raises an
20   # exception if not.  wr_io is expected to be an IO-like object capable of
21   # receiving the write method.
22   def bigfile_write(key, wr_io, opts = { :verify => false })
23     info = bigfile_stat(key)
24     total = 0
25     t = @get_file_data_timeout
27     # we only decode raw zlib deflated streams that mogtool (unfortunately)
28     # generates.  tarballs and gzip(1) are up to to the application to decrypt.
29     if info[:compressed] || opts[:verify]
30       wr_io = MogileFS::Bigfile::Filter.new(wr_io, info, opts)
31     end
33     info[:parts].each_with_index do |part,part_nr|
34       next if part_nr == 0 # info[:parts][0] is always empty
36       begin
37         sock = MogileFS::HTTPReader.first(part[:paths], "GET", t)
38       rescue
39         # part[:paths] may not be valid anymore due to rebalancing, however we
40         # can get_keys on key,<part_nr> and retry paths if all paths fail
41         part_key = "#{key.sub(/^_big_info:/, '')},#{part_nr}"
42         paths = get_paths(part_key)
43         paths.empty? and
44           raise MogileFS::Backend::NoDevices,
45                 "no device for key=#{part_key.inspect}", []
46         sock = MogileFS::HTTPReader.first(paths, "GET", t)
47       end
49       w = MogileFS::X.copy_stream(sock, wr_io)
51       wr_io.respond_to?(:md5_check!) and wr_io.md5_check!(part[:md5])
52       total += w
53     end
54     wr_io.flush
55     total += wr_io.flushed_bytes if wr_io.respond_to?(:flushed_bytes)
57     [ total, info ]
58   end
60   ##
61   # parses the contents of a _big_info: string or IO object
62   def bigfile_parse_info(info) # :nodoc:
63     rv = { :parts => [] }
64     info.each_line do |line|
65       line.chomp!
66       case line
67       when /^(des|type|filename)\s+(.+)$/
68         rv[$1.to_sym] = $2
69       when /^compressed\s+([01])$/
70         rv[:compressed] = ($1 == '1')
71       when /^(chunks|size)\s+(\d+)$/
72         rv[$1.to_sym] = $2.to_i
73       when /^part\s+(\d+)\s+bytes=(\d+)\s+md5=(.+)\s+paths:\s+(.+)$/
74         rv[:parts][$1.to_i] = {
75           :bytes => $2.to_i,
76           :md5 => $3.downcase,
77           :paths => $4.split(/\s*,\s*/),
78         }
79       end
80     end
82     rv
83   end
84 end
85 require "mogilefs/bigfile/filter"
87 __END__
88 # Copied from mogtool:
89 # http://code.sixapart.com/svn/mogilefs/utils/mogtool, r1221
91 # this is a temporary file that we delete when we're doing recording all chunks
93 _big_pre:<key>
95     starttime=UNIXTIMESTAMP
97 # when done, we write the _info file and delete the _pre.
99 _big_info:<key>
101     des Cow's ljdb backup as of 2004-11-17
102     type  { partition, file, tarball }
103     compressed {0, 1}
104     filename  ljbinlog.305.gz
105     partblocks  234324324324
108     part 1 <bytes> <md5hex>
109     part 2 <bytes> <md5hex>
110     part 3 <bytes> <md5hex>
111     part 4 <bytes> <md5hex>
112     part 5 <bytes> <md5hex>
114 _big:<key>,<n>
115 _big:<key>,<n>
116 _big:<key>,<n>
119 Receipt format:
121 BEGIN MOGTOOL RECIEPT
122 type partition
123 des Foo
124 compressed foo
126 part 1 bytes=23423432 md5=2349823948239423984 paths: http://dev5/2/23/23/.fid, http://dev6/23/423/4/324.fid
127 part 1 bytes=23423432 md5=2349823948239423984 paths: http://dev5/2/23/23/.fid, http://dev6/23/423/4/324.fid
128 part 1 bytes=23423432 md5=2349823948239423984 paths: http://dev5/2/23/23/.fid, http://dev6/23/423/4/324.fid
129 part 1 bytes=23423432 md5=2349823948239423984 paths: http://dev5/2/23/23/.fid, http://dev6/23/423/4/324.fid
132 END RECIEPT