e959a21ecf772bf5dca23fabd6911fcf8e702585
[ruby-mogilefs-client.git] / lib / mogilefs / new_file / common.rb
blobe959a21ecf772bf5dca23fabd6911fcf8e702585
1 # -*- encoding: binary -*-
2 # here are internal implementation details, do not use them in your code
3 require 'socket'
4 require 'uri'
5 require 'digest/md5'
6 require 'mogilefs/chunker'
8 module MogileFS::NewFile::Common
9   # :stopdoc:
10   class RetryableError < MogileFS::Error; end
11   class EmptyResponseError < RetryableError; end
12   class BadResponseError < RetryableError; end
13   class UnparseableResponseError < RetryableError; end
14   class NoStorageNodesError < MogileFS::Error
15     def message; 'Unable to open socket to storage node'; end
16   end
17   class NonRetryableError < MogileFS::Error; end
19   MD5_TRAILER_NODES = {} # :nodoc: # EXPERIMENTAL
21   def read_response(sock)
22     tout = @opts[:new_file_max_time] || 3600.0
23     start_time = @opts[:start_time] and tout -= Time.now - start_time
24     case line = sock.timed_read(23, "", tout > 0.0 ? tout : 0)
25     when %r{^HTTP/\d\.\d\s+(2\d\d)\s} # success!
26     when nil
27       raise EmptyResponseError, 'Unable to read response line from server'
28     when %r{^HTTP/\d\.\d\s+(\d+)}
29       raise BadResponseError, "HTTP response status from upload: #$1"
30     else
31       raise UnparseableResponseError,
32             "Response line not understood: #{line.inspect}"
33     end
34   end
36   def create_close(devid, uri, bytes_uploaded)
37     args = {
38       :fid => @opts[:fid],
39       :devid => devid,
40       :key => @opts[:key],
41       :domain => @opts[:domain],
42       :size => bytes_uploaded,
43       :path => uri.to_s,
44     }
45     if @md5
46       args[:checksum] = "MD5:#{@md5.hexdigest}"
47     elsif String === @opts[:content_md5]
48       hex = @opts[:content_md5].unpack('m')[0].unpack('H*')[0]
49       args[:checksum] = "MD5:#{hex}"
50     end
51     args[:checksumverify] = 1 if @opts[:checksumverify]
52     backend = @opts[:backend]
54     # upload could've taken a long time, ping and try to ensure socket
55     # is valid to minimize (but not completely eliminate) the chance
56     # create_close hits a stale socket (while reading the response after
57     # writing to it) and becomes non-retryable.  We treat create_close
58     # specially as its less idempotent than any other command
59     # (even other non-idempotent ones).  There may be no hope of retrying
60     # the upload at all if data was streamed and calling create_close
61     # twice will hurt us...
62     backend.noop
64     backend.create_close(args)
65     bytes_uploaded
66   end
68   # aggressive keepalive settings on Linux + Ruby 1.9.2+
69   TCP_KEEPALIVE = {
70     :TCP_KEEPIDLE => 60, # seconds time before keepalive packet is sent
71     :TCP_KEEPINTVL => 5,
72     :TCP_KEEPCNT => 2,  # number of retries
73   }
75   req_consts = TCP_KEEPALIVE.keys
76   if (Socket.constants & req_consts).size == req_consts.size
77     def set_socket_options(sock)
78       sock.setsockopt(:SOL_SOCKET, :SO_KEEPALIVE, 1)
79       TCP_KEEPALIVE.each do |k,v|
80         sock.setsockopt(:IPPROTO_TCP, k, v)
81       end
82     end
83   else
84     def set_socket_options(sock)
85       sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
86     end
87   end
88   # :startdoc:
89 end