Class: Fluent::Plugin::ForwardOutput::FailureDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/out_forward.rb

Constant Summary collapse

PHI_FACTOR =
1.0 / Math.log(10.0)
SAMPLE_SIZE =
1000

Instance Method Summary collapse

Constructor Details

#initialize(heartbeat_interval, hard_timeout, init_last) ⇒ FailureDetector

Returns a new instance of FailureDetector.



858
859
860
861
862
863
864
865
866
# File 'lib/fluent/plugin/out_forward.rb', line 858

def initialize(heartbeat_interval, hard_timeout, init_last)
  @heartbeat_interval = heartbeat_interval
  @last = init_last
  @hard_timeout = hard_timeout

  # microsec
  @init_gap = (heartbeat_interval * 1e6).to_i
  @window = [@init_gap]
end

Instance Method Details

#add(now) ⇒ Object



872
873
874
875
876
877
878
879
880
881
882
# File 'lib/fluent/plugin/out_forward.rb', line 872

def add(now)
  if @window.empty?
    @window << @init_gap
    @last = now
  else
    gap = now - @last
    @window << (gap * 1e6).to_i
    @window.shift if @window.length > SAMPLE_SIZE
    @last = now
  end
end

#clearObject



911
912
913
914
# File 'lib/fluent/plugin/out_forward.rb', line 911

def clear
  @window.clear
  @last = 0
end

#hard_timeout?(now) ⇒ Boolean

Returns:

  • (Boolean)


868
869
870
# File 'lib/fluent/plugin/out_forward.rb', line 868

def hard_timeout?(now)
  now - @last > @hard_timeout
end

#phi(now) ⇒ Object



884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
# File 'lib/fluent/plugin/out_forward.rb', line 884

def phi(now)
  size = @window.size
  return 0.0 if size == 0

  # Calculate weighted moving average
  mean_usec = 0
  fact = 0
  @window.each_with_index {|gap,i|
    mean_usec += gap * (1+i)
    fact += (1+i)
  }
  mean_usec = mean_usec / fact

  # Normalize arrive intervals into 1sec
  mean = (mean_usec.to_f / 1e6) - @heartbeat_interval + 1

  # Calculate phi of the phi accrual failure detector
  t = now - @last - @heartbeat_interval + 1
  phi = PHI_FACTOR * t / mean

  return phi
end

#sample_sizeObject



907
908
909
# File 'lib/fluent/plugin/out_forward.rb', line 907

def sample_size
  @window.size
end