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
# 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

  if forever && secondary
    raise "BUG: forever and secondary are exclusive to each other"
  end

  @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
    @secondary_transition_at = @start + 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



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

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



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

def current_time
  Time.now
end

#limit?Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
# File 'lib/fluent/plugin_helper/retry_state.rb', line 129

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)


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

def naive_next_time(retry_times)
  raise NotImplementedError
end

#randomize(interval) ⇒ Object



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

def randomize(interval)
  return interval unless @randomize

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

#secondary?Boolean

Returns:

  • (Boolean)


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

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

#stepObject



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

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