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


626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/awsbase/right_awsbase.rb', line 626

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



614
615
616
# File 'lib/awsbase/right_awsbase.rb', line 614

def self.close_on_4xx_probability 
  @@close_on_4xx_probability 
end

.close_on_4xx_probability=(close_on_4xx_probability) ⇒ Object



617
618
619
# File 'lib/awsbase/right_awsbase.rb', line 617

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

.close_on_errorObject



606
607
608
# File 'lib/awsbase/right_awsbase.rb', line 606

def self.close_on_error 
  @@close_on_error 
end

.close_on_error=(close_on_error) ⇒ Object



609
610
611
# File 'lib/awsbase/right_awsbase.rb', line 609

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

.reiteration_start_delayObject



590
591
592
# File 'lib/awsbase/right_awsbase.rb', line 590

def self.reiteration_start_delay
  @@reiteration_start_delay
end

.reiteration_start_delay=(reiteration_start_delay) ⇒ Object



593
594
595
# File 'lib/awsbase/right_awsbase.rb', line 593

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

.reiteration_timeObject



598
599
600
# File 'lib/awsbase/right_awsbase.rb', line 598

def self.reiteration_time
  @@reiteration_time
end

.reiteration_time=(reiteration_time) ⇒ Object



601
602
603
# File 'lib/awsbase/right_awsbase.rb', line 601

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

Instance Method Details

#check(request) ⇒ Object

Returns false if



640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/awsbase/right_awsbase.rb', line 640

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']
    # ... 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}")
    # ... 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.connection.finish "#{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 
    # Is this a 5xx error ? 
    if @aws.last_response.code.to_s[/^5\d\d$/] 
      @aws.connection.finish "#{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.connection.finish "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.message}', " + 
                             "probability: #{@close_on_4xx_probability}%"           
    end
  end
  result
end