1 # -*- encoding: binary -*-
2 # here are internal implementation details, do not use them in your code
5 require 'mogilefs/chunker'
8 # HTTPFile wraps up the new file operations for storing files onto an HTTP
11 # You really don't want to create an HTTPFile by hand. Instead you want to
12 # create a new file using MogileFS::MogileFS.new_file.
14 class MogileFS::HTTPFile < StringIO
15 class EmptyResponseError < MogileFS::Error; end
16 class BadResponseError < MogileFS::Error; end
17 class UnparseableResponseError < MogileFS::Error; end
18 class NoStorageNodesError < MogileFS::Error
19 def message; 'Unable to open socket to storage node'; end
21 class NonRetryableError < MogileFS::Error; end
23 class HTTPSock < MogileFS::Socket
26 # Increase timeout as we become more invested in uploading with
27 # this socket. The server could be experiencing I/O delays
28 # from large uploads because the sysadmin forgot to tune the
29 # VM sysctls for handling large files.
31 timed_write(buf, Time.now - @start + 5.0)
36 MD5_TRAILER_NODES = {} # :nodoc: # EXPERIMENTAL
38 attr_accessor :response_timeout_cb
41 # temporary directories (nginx) may not be configured on the
42 # same device, necessitating a time-consuming full file copy
43 # instead of a quick rename(2)/link(2) operation
44 @response_timeout_cb = lambda do |elapsed_time, bytes_uploaded|
45 mbytes_uploaded = bytes_uploaded / (1024.0 * 1024.0)
46 # assumes worst case is 10M/s on the remote storage disk
47 t = mbytes_uploaded * 10 + elapsed_time
53 # The URI this file will be stored to.
60 # The big_io name in case we have file > 256M
64 attr_accessor :streaming_io
67 # Creates a new HTTPFile with MogileFS-specific data. Use
68 # MogileFS::MogileFS#new_file instead of this method.
70 def initialize(dests, content_length)
72 @streaming_io = @big_io = @uri = @devid = @active = nil
76 def request_put(sock, uri, file_size, input = nil)
77 host_with_port = "#{uri.host}:#{uri.port}"
79 if MD5_TRAILER_NODES[host_with_port]
85 sock.write("PUT #{uri.request_uri} HTTP/1.0\r\n" \
86 "Content-Length: #{file_size}\r\n\r\n")
87 input ? MogileFS.io.copy_stream(@active = input, sock) : yield(sock)
89 trailers = md5 ? "Trailer: Content-MD5\r\n" : ""
90 sock.write("PUT #{uri.request_uri} HTTP/1.1\r\n" \
91 "Host: #{host_with_port}\r\n#{trailers}" \
92 "Transfer-Encoding: chunked\r\n\r\n")
93 tmp = MogileFS::Chunker.new(sock, md5)
94 rv = input ? MogileFS.io.copy_stream(@active = input, tmp) : yield(tmp)
100 def put_streaming_io(sock, uri) # unlikely to be used
101 file_size = @streaming_io.length
103 request_put(sock, uri, file_size) do |wr|
104 @streaming_io.call(Proc.new do |data_to_write|
105 written += wr.write(data_to_write)
108 file_size ? file_size : written
111 def rewind_or_raise!(uri, err)
112 @active.rewind if @active
114 msg = "#{uri} failed with #{err.message} (#{err.class}) and " \
115 "retrying is impossible as rewind on " \
116 "#{@active.inspect} failed with: #{e.message} (#{e.class})"
117 raise NonRetryableError, msg, e.backtrace
121 # Writes an HTTP PUT request to +sock+ to upload the file and
122 # returns file size if the socket finished writing
123 def upload(devid, uri) # :nodoc:
125 sock = HTTPSock.tcp(uri.host, uri.port)
130 file_size = put_streaming_io(sock, uri)
132 if String === @big_io || @big_io.respond_to?(:to_path)
133 file = File.open(@big_io)
135 file_size = request_put(sock, uri, stat.file? ? stat.size : nil, file)
138 if @big_io.respond_to?(:stat)
140 size = stat.size if stat.file?
141 elsif @big_io.respond_to?(:size)
144 file_size = request_put(sock, uri, size, @big_io)
148 request_put(sock, uri, file_size, self)
151 tout = self.class.response_timeout_cb.call(Time.now - start, file_size)
153 case line = sock.timed_read(23, "", tout)
154 when %r{^HTTP/\d\.\d\s+(2\d\d)\s} # success!
157 raise EmptyResponseError, 'Unable to read response line from server'
158 when %r{^HTTP/\d\.\d\s+(\d+)}
159 raise BadResponseError, "HTTP response status from upload: #$1"
161 raise UnparseableResponseError,
162 "Response line not understood: #{line.inspect}"
165 rewind_or_raise!(uri, err)
174 @dests.each do |devid, path|
176 uri = URI.parse(path)
177 bytes_uploaded = upload(devid, uri)
178 @devid, @uri = devid, uri
179 return bytes_uploaded
180 rescue NonRetryableError
184 errors << "#{path} - #{e.message} (#{e.class})"
188 raise NoStorageNodesError,
189 "all paths failed with PUT: #{errors.join(', ')}", []