Class: Fluent::PluginHelper::RetryState::RetryStateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin_helper/retry_state.rb

Direct Known Subclasses

ExponentialBackOffRetry, PeriodicRetry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, wait, timeout, forever, max_steps, randomize, randomize_width, secondary, secondary_threshold) ⇒ RetryStateMachine

Returns a new instance of RetryStateMachine.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fluent/plugin_helper/retry_state.rb', line 38

def initialize(title, wait, timeout, forever, max_steps, randomize, randomize_width, secondary, secondary_threshold)
  @title = title

  @start = current_time
  @steps = 0
  @next_time = nil # should be initialized for first retry by child class

  @timeout = timeout
  @timeout_at = @start + timeout
  @current = :primary

  if randomize_width < 0 || randomize_width > 0.5
    raise "BUG: randomize_width MUST be between 0 and 0.5"
  end

  @randomize = randomize
  @randomize_width = randomize_width

  @forever = forever
  @max_steps = max_steps

  @secondary = secondary
  @secondary_threshold = secondary_threshold
  if @secondary
    raise "BUG: secondary_transition_threshold MUST be between 0 and 1" if @secondary_threshold <= 0 || @secondary_threshold >= 1
    max_retry_timeout = timeout
    if max_steps
      timeout_by_max_steps = calc_max_retry_timeout(max_steps)
      max_retry_timeout = timeout_by_max_steps if timeout_by_max_steps < max_retry_timeout
    end
    @secondary_transition_at = @start + max_retry_timeout * @secondary_threshold
    @secondary_transition_steps = nil
  end
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



36
37
38
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36

def current
  @current
end

#next_timeObject (readonly)

Returns the value of attribute next_time.



36
37
38
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36

def next_time
  @next_time
end

#secondary_transition_atObject (readonly)

Returns the value of attribute secondary_transition_at.



36
37
38
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36

def secondary_transition_at
  @secondary_transition_at
end

#secondary_transition_stepsObject (readonly)

Returns the value of attribute secondary_transition_steps.



36
37
38
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36

def secondary_transition_steps
  @secondary_transition_steps
end

#startObject (readonly)

Returns the value of attribute start.



36
37
38
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36

def start
  @start
end

#stepsObject (readonly)

Returns the value of attribute steps.



36
37
38
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36

def steps
  @steps
end

#timeout_atObject (readonly)

Returns the value of attribute timeout_at.



36
37
38
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36

def timeout_at
  @timeout_at
end

#titleObject (readonly)

Returns the value of attribute title.



36
37
38
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36

def title
  @title
end

Instance Method Details

#calc_next_timeObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fluent/plugin_helper/retry_state.rb', line 83

def calc_next_time
  if @forever || !@secondary # primary
    naive = naive_next_time(@steps)
    if @forever
      naive
    elsif naive >= @timeout_at
      @timeout_at
    else
      naive
    end
  elsif @current == :primary && @secondary
    naive = naive_next_time(@steps)
    if naive >= @secondary_transition_at
      @secondary_transition_at
    else
      naive
    end
  elsif @current == :secondary
    naive = naive_next_time(@steps - @secondary_transition_steps + 1)
    if naive >= @timeout_at
      @timeout_at
    else
      naive
    end
  else
    raise "BUG: it's out of design"
  end
end

#current_timeObject



73
74
75
# File 'lib/fluent/plugin_helper/retry_state.rb', line 73

def current_time
  Time.now
end

#limit?Boolean

Returns:

  • (Boolean)


134
135
136
137
138
139
140
# File 'lib/fluent/plugin_helper/retry_state.rb', line 134

def limit?
  if @forever
    false
  else
    @next_time >= @timeout_at || !!(@max_steps && @steps >= @max_steps)
  end
end

#naive_next_time(retry_times) ⇒ Object

Raises:

  • (NotImplementedError)


112
113
114
# File 'lib/fluent/plugin_helper/retry_state.rb', line 112

def naive_next_time(retry_times)
  raise NotImplementedError
end

#randomize(interval) ⇒ Object



77
78
79
80
81
# File 'lib/fluent/plugin_helper/retry_state.rb', line 77

def randomize(interval)
  return interval unless @randomize

  interval + (interval * @randomize_width * (2 * rand - 1.0))
end

#recalc_next_timeObject



130
131
132
# File 'lib/fluent/plugin_helper/retry_state.rb', line 130

def recalc_next_time
  @next_time = calc_next_time
end

#secondary?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/fluent/plugin_helper/retry_state.rb', line 116

def secondary?
  !@forever && @secondary && (@current == :secondary || current_time >= @secondary_transition_at)
end

#stepObject



120
121
122
123
124
125
126
127
128
# File 'lib/fluent/plugin_helper/retry_state.rb', line 120

def step
  @steps += 1
  if !@forever && @secondary && @current != :secondary && current_time >= @secondary_transition_at
    @current = :secondary
    @secondary_transition_steps = @steps
  end
  @next_time = calc_next_time
  nil
end