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
72
73
# 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
  @has_reached_timeout = false
  @has_timed_out = false
  @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



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
111
112
# File 'lib/fluent/plugin_helper/retry_state.rb', line 85

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)
    if naive >= @timeout_at
      @timeout_at
    else
      naive
    end
  else
    raise "BUG: it's out of design"
  end
end

#current_timeObject



75
76
77
# File 'lib/fluent/plugin_helper/retry_state.rb', line 75

def current_time
  Time.now
end

#limit?Boolean

Returns:

  • (Boolean)


144
145
146
147
148
149
150
# File 'lib/fluent/plugin_helper/retry_state.rb', line 144

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

#naive_next_time(retry_times) ⇒ Object

Raises:

  • (NotImplementedError)


114
115
116
# File 'lib/fluent/plugin_helper/retry_state.rb', line 114

def naive_next_time(retry_times)
  raise NotImplementedError
end

#randomize(interval) ⇒ Object



79
80
81
82
83
# File 'lib/fluent/plugin_helper/retry_state.rb', line 79

def randomize(interval)
  return interval unless @randomize

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

#recalc_next_timeObject



140
141
142
# File 'lib/fluent/plugin_helper/retry_state.rb', line 140

def recalc_next_time
  @next_time = calc_next_time
end

#secondary?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/fluent/plugin_helper/retry_state.rb', line 118

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

#stepObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fluent/plugin_helper/retry_state.rb', line 122

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

  if @has_reached_timeout
    @has_timed_out = @next_time >= @timeout_at
  else
    @has_reached_timeout = @next_time >= @timeout_at
  end

  nil
end