Class: RightAws::AWSErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/awsbase/right_awsbase.rb

Constant Summary collapse

DEFAULT_CLOSE_ON_4XX_PROBABILITY =

0-100 (%)

10
@@reiteration_start_delay =
0.2
@@reiteration_time =
5
@@close_on_error =
true
@@close_on_4xx_probability =
DEFAULT_CLOSE_ON_4XX_PROBABILITY

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aws, parser, params = {}) ⇒ AWSErrorHandler

params:

:reiteration_time 
:errors_list 
:close_on_error           = true | false 
:close_on_4xx_probability = 1-100


998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
# File 'lib/awsbase/right_awsbase.rb', line 998

def initialize(aws, parser, params={}) #:nodoc:     
  @aws           = aws              # Link to RightEc2 | RightSqs | RightS3 instance
  @parser        = parser           # parser to parse Amazon response
  @started_at    = Time.now
  @stop_at       = @started_at  + (params[:reiteration_time] || @@reiteration_time) 
  @errors_list   = params[:errors_list] || [] 
  @reiteration_delay = @@reiteration_start_delay
  @retries       = 0
  # close current HTTP(S) connection on 5xx, errors from list and 4xx errors 
  @close_on_error           = params[:close_on_error].nil? ? @@close_on_error : params[:close_on_error]
  @close_on_4xx_probability = params[:close_on_4xx_probability] || @@close_on_4xx_probability       
end

Class Method Details

.close_on_4xx_probabilityObject



986
987
988
# File 'lib/awsbase/right_awsbase.rb', line 986

def self.close_on_4xx_probability 
  @@close_on_4xx_probability 
end

.close_on_4xx_probability=(close_on_4xx_probability) ⇒ Object



989
990
991
# File 'lib/awsbase/right_awsbase.rb', line 989

def self.close_on_4xx_probability=(close_on_4xx_probability) 
  @@close_on_4xx_probability = close_on_4xx_probability 
end

.close_on_errorObject



978
979
980
# File 'lib/awsbase/right_awsbase.rb', line 978

def self.close_on_error 
  @@close_on_error 
end

.close_on_error=(close_on_error) ⇒ Object



981
982
983
# File 'lib/awsbase/right_awsbase.rb', line 981

def self.close_on_error=(close_on_error) 
  @@close_on_error = close_on_error 
end

.reiteration_start_delayObject



962
963
964
# File 'lib/awsbase/right_awsbase.rb', line 962

def self.reiteration_start_delay
  @@reiteration_start_delay
end

.reiteration_start_delay=(reiteration_start_delay) ⇒ Object



965
966
967
# File 'lib/awsbase/right_awsbase.rb', line 965

def self.reiteration_start_delay=(reiteration_start_delay)
  @@reiteration_start_delay = reiteration_start_delay
end

.reiteration_timeObject



970
971
972
# File 'lib/awsbase/right_awsbase.rb', line 970

def self.reiteration_time
  @@reiteration_time
end

.reiteration_time=(reiteration_time) ⇒ Object



973
974
975
# File 'lib/awsbase/right_awsbase.rb', line 973

def self.reiteration_time=(reiteration_time)
  @@reiteration_time = reiteration_time
end

Instance Method Details

#check(request) ⇒ Object

Returns false if



1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
# File 'lib/awsbase/right_awsbase.rb', line 1012

def check(request)  #:nodoc:
  result           = false
  error_found      = false
  redirect_detected= false
  error_match      = nil
  last_errors_text = ''
  response         = @aws.last_response
  # log error
  request_text_data = "#{request[:protocol]}://#{request[:server]}:#{request[:port]}#{request[:request].path}"
  # is this a redirect?
  # yes!
  if response.is_a?(Net::HTTPRedirection)
    redirect_detected = true 
  else
    # no, it's an error ...
    @aws.logger.warn("##### #{@aws.class.name} returned an error: #{response.code} #{response.message}\n#{response.body} #####")
    @aws.logger.warn("##### #{@aws.class.name} request: #{request_text_data} ####")
  end

  # Extract error/redirection message from the response body
  # Amazon claims that a redirection must have a body but somethimes it is nil....
  if response.body && response.body[/^(<\?xml|<ErrorResponse)/]
    error_parser = RightErrorResponseParser.new
    @aws.class.bench_xml.add! do
      error_parser.parse(response.body)
    end
    @aws.last_errors     = error_parser.errors
    @aws.last_request_id = error_parser.requestID
    last_errors_text     = @aws.last_errors.flatten.join("\n")
  else
    @aws.last_errors     = [[response.code, "#{response.message} (#{request_text_data})"]]
    @aws.last_request_id = '-undefined-'
    last_errors_text     = response.message
  end
  
  # Ok, it is a redirect, find the new destination location
  if redirect_detected
    location = response['location']
    # As for 301 ( Moved Permanently) Amazon does not return a 'Location' header but
    # it is possible to extract a new endpoint from the response body
    if location.right_blank? && response.code=='301' && response.body
      new_endpoint = response.body[/<Endpoint>(.*?)<\/Endpoint>/] && $1
      location     = "#{request[:protocol]}://#{new_endpoint}:#{request[:port]}#{request[:request].path}"
    end
    # ... log information and ...
    @aws.logger.info("##### #{@aws.class.name} redirect requested: #{response.code} #{response.message} #####")
    @aws.logger.info("      Old location: #{request_text_data}")
    @aws.logger.info("      New location: #{location}")
    @aws.logger.info("      Request Verb: #{request[:request].class.name}")
    # ... fix the connection data
    request[:server]   = URI.parse(location).host
    request[:protocol] = URI.parse(location).scheme
    request[:port]     = URI.parse(location).port
  else
    # Not a redirect but an error: try to find the error in our list
    @errors_list.each do |error_to_find|
      if last_errors_text[/#{error_to_find}/i]
        error_found = true
        error_match = error_to_find
        @aws.logger.warn("##### Retry is needed, error pattern match: #{error_to_find} #####")
        break
      end
    end
  end
  
    # check the time has gone from the first error come
  if redirect_detected || error_found
    # Close the connection to the server and recreate a new one. 
    # It may have a chance that one server is a semi-down and reconnection 
    # will help us to connect to the other server 
    if !redirect_detected && @close_on_error
      @aws.destroy_connection(request, "#{self.class.name}: error match to pattern '#{error_match}'")
    end 
             
    if (Time.now < @stop_at)
      @retries += 1
      unless redirect_detected
        @aws.logger.warn("##### Retry ##{@retries} is being performed. Sleeping for #{@reiteration_delay} sec. Whole time: #{Time.now-@started_at} sec ####")
        sleep @reiteration_delay 
        @reiteration_delay *= 2

        # Always make sure that the fp is set to point to the beginning(?)
        # of the File/IO. TODO: it assumes that offset is 0, which is bad.
        if(request[:request].body_stream && request[:request].body_stream.respond_to?(:pos))
          begin
            request[:request].body_stream.pos = 0
          rescue Exception => e
            @logger.warn("Retry may fail due to unable to reset the file pointer" +
                         " -- #{self.class.name} : #{e.inspect}")
          end
        end
      else
        @aws.logger.info("##### Retry ##{@retries} is being performed due to a redirect.  ####")
      end
      result = @aws.request_info(request, @parser)
    else
      @aws.logger.warn("##### Ooops, time is over... ####")
    end 
  # aha, this is unhandled error: 
  elsif @close_on_error 
    # On 5xx(Server errors), 403(RequestTimeTooSkewed) and 408(Request Timeout) a conection has to be closed
    if @aws.last_response.code.to_s[/^(5\d\d|403|408)$/]
      @aws.destroy_connection(request, "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.message}'")
    # Is this a 4xx error ? 
    elsif @aws.last_response.code.to_s[/^4\d\d$/] && @close_on_4xx_probability > rand(100) 
      @aws.destroy_connection(request, "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.message}', " +
                                       "probability: #{@close_on_4xx_probability}%")
    end
  end
  result
end