Class: Fluent::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.



579
580
581
582
583
584
585
586
587
# File 'lib/fluent/plugin/out_forward.rb', line 579

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



593
594
595
596
597
598
599
600
601
602
603
# File 'lib/fluent/plugin/out_forward.rb', line 593

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



632
633
634
635
# File 'lib/fluent/plugin/out_forward.rb', line 632

def clear
  @window.clear
  @last = 0
end

#hard_timeout?(now) ⇒ Boolean

Returns:

  • (Boolean)


589
590
591
# File 'lib/fluent/plugin/out_forward.rb', line 589

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

#phi(now) ⇒ Object



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/fluent/plugin/out_forward.rb', line 605

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



628
629
630
# File 'lib/fluent/plugin/out_forward.rb', line 628

def sample_size
  @window.size
end