-
Notifications
You must be signed in to change notification settings - Fork 85
Error rate circuit breaker #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RyanAD
wants to merge
11
commits into
Shopify:main
Choose a base branch
from
RyanAD:error-rate-circuit-breaker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d5fb106
add a sliding window where entries slide off after a period of time
RyanAD bad8a75
circuit breaker that opens based on error rate (% errors in time window)
RyanAD 73a839d
cleanup ErrorRateCircuitBreaker
RyanAD 08e851f
circuit breaker implementation that opens based on time spent in error
RyanAD 956211c
style fixes
RyanAD 8a6c96f
code cleanup
RyanAD 4adee5e
code cleanup
RyanAD 4cfcf59
code cleanup
RyanAD 35b47d1
add accessor for exceptions
RyanAD e2255d7
code cleanup
RyanAD 1bdff25
TimeSlidingWindow#clear lock in mutex to prevent issues in #remove_old
RyanAD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| module Semian | ||
| class ErrorRateCircuitBreaker #:nodoc: | ||
damianthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| extend Forwardable | ||
|
|
||
| def_delegators :@state, :closed?, :open?, :half_open? | ||
|
|
||
| attr_reader :name, :half_open_resource_timeout, :error_timeout, :state, :last_error, :error_percent_threshold, | ||
| :request_volume_threshold, :success_threshold, :time_window | ||
|
|
||
| def initialize(name, exceptions:, error_percent_threshold:, error_timeout:, time_window:, | ||
| request_volume_threshold:, success_threshold:, implementation:, | ||
| half_open_resource_timeout: nil, time_source: nil) | ||
|
|
||
| raise 'error_threshold_percent should be between 0.0 and 1.0 exclusive' unless (0.0001...1.0).cover?(error_percent_threshold) | ||
damianthe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @name = name.to_sym | ||
| @error_timeout = error_timeout * 1000 | ||
| @exceptions = exceptions | ||
| @half_open_resource_timeout = half_open_resource_timeout | ||
| @error_percent_threshold = error_percent_threshold | ||
| @last_error_time = nil | ||
| @request_volume_threshold = request_volume_threshold | ||
| @success_threshold = success_threshold | ||
| @time_source = time_source ? time_source : -> { Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) } | ||
| @window = implementation::TimeSlidingWindow.new(time_window, @time_source) | ||
| @state = implementation::State.new | ||
|
|
||
| reset | ||
| end | ||
|
|
||
| def acquire(resource = nil, &block) | ||
| return yield if disabled? | ||
| transition_to_half_open if transition_to_half_open? | ||
|
|
||
| raise OpenCircuitError unless request_allowed? | ||
|
|
||
| time_start = current_time | ||
| result = nil | ||
| begin | ||
| result = maybe_with_half_open_resource_timeout(resource, &block) | ||
| rescue *@exceptions => error | ||
| if !error.respond_to?(:marks_semian_circuits?) || error.marks_semian_circuits? | ||
| mark_failed(error, current_time - time_start) | ||
| end | ||
| raise error | ||
| else | ||
| mark_success(current_time - time_start) | ||
| end | ||
| result | ||
| end | ||
|
|
||
| def transition_to_half_open? | ||
| open? && error_timeout_expired? && !half_open? | ||
| end | ||
|
|
||
| def request_allowed? | ||
| closed? || half_open? || transition_to_half_open? | ||
| end | ||
|
|
||
| def mark_failed(error, time_spent) | ||
| push_error(error, time_spent) | ||
| if closed? | ||
| transition_to_open if error_threshold_reached? | ||
damianthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| elsif half_open? | ||
| transition_to_open | ||
| end | ||
| end | ||
|
|
||
| def mark_success(time_spent) | ||
| @window << [true, time_spent] | ||
| return unless half_open? | ||
| transition_to_close if success_threshold_reached? | ||
| end | ||
|
|
||
| def reset | ||
| @last_error_time = nil | ||
| @window.clear | ||
| transition_to_close | ||
| end | ||
|
|
||
| def destroy | ||
| @state.destroy | ||
| end | ||
|
|
||
| def in_use? | ||
| return false if error_timeout_expired? | ||
| error_count > 0 | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def current_time | ||
| @time_source.call | ||
| end | ||
|
|
||
| def transition_to_close | ||
| notify_state_transition(:closed) | ||
| log_state_transition(:closed) | ||
| @state.close! | ||
| end | ||
|
|
||
| def transition_to_open | ||
| notify_state_transition(:open) | ||
| log_state_transition(:open) | ||
| @state.open! | ||
| @window.clear | ||
| end | ||
|
|
||
| def transition_to_half_open | ||
| notify_state_transition(:half_open) | ||
| log_state_transition(:half_open) | ||
| @state.half_open! | ||
| @window.clear | ||
| end | ||
|
|
||
| def success_threshold_reached? | ||
| success_count >= @success_threshold | ||
| end | ||
|
|
||
| def error_threshold_reached? | ||
| return false if @window.empty? || @window.length < @request_volume_threshold | ||
| success_time_spent, error_time_spent = calculate_time_spent | ||
| total_time = error_time_spent + success_time_spent | ||
| error_time_spent / total_time >= @error_percent_threshold | ||
| end | ||
|
|
||
| def calculate_time_spent | ||
| @window.each_with_object([0.0, 0.0]) do |entry, sum| | ||
| if entry[0] == true | ||
| sum[0] = entry[1] + sum[0] | ||
| else | ||
| sum[1] = entry[1] + sum[1] | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def error_count | ||
| @window.count { |entry| entry[0] == false }.to_f | ||
| end | ||
|
|
||
| def success_count | ||
| @window.count { |entry| entry[0] == true }.to_f | ||
| end | ||
|
|
||
| def error_timeout_expired? | ||
| return false unless @last_error_time | ||
| current_time - @last_error_time >= @error_timeout | ||
| end | ||
|
|
||
| def push_error(error, time_spent) | ||
| @last_error = error | ||
| @last_error_time = current_time | ||
| @window << [false, time_spent] | ||
| end | ||
|
|
||
| def log_state_transition(new_state) | ||
| return if @state.nil? || new_state == @state.value | ||
|
|
||
| str = "[#{self.class.name}] State transition from #{@state.value} to #{new_state}." | ||
| str << " success_count=#{success_count} error_count=#{error_count}" | ||
| str << " success_count_threshold=#{@success_threshold} error_count_percent=#{@error_percent_threshold}" | ||
| str << " error_timeout=#{@error_timeout} error_last_at=\"#{@last_error_time}\"" | ||
| str << " name=\"#{@name}\"" | ||
| Semian.logger.info(str) | ||
| end | ||
|
|
||
| def notify_state_transition(new_state) | ||
| Semian.notify(:state_change, self, nil, nil, state: new_state) | ||
| end | ||
|
|
||
| def disabled? | ||
| ENV['SEMIAN_CIRCUIT_BREAKER_DISABLED'] || ENV['SEMIAN_DISABLED'] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requires rethinking of changes in master branch. Idea to not use ENV inside business logic, only during the configuration phase. |
||
| end | ||
|
|
||
| def maybe_with_half_open_resource_timeout(resource, &block) | ||
| result = | ||
| if half_open? && @half_open_resource_timeout && resource.respond_to?(:with_resource_timeout) | ||
| resource.with_resource_timeout(@half_open_resource_timeout) do | ||
| block.call | ||
| end | ||
| else | ||
| block.call | ||
| end | ||
|
|
||
| result | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| require 'thread' | ||
|
|
||
| module Semian | ||
| module Simple | ||
| class TimeSlidingWindow #:nodoc: | ||
| extend Forwardable | ||
|
|
||
| def_delegators :@window, :size, :empty?, :length | ||
| attr_reader :time_window_millis | ||
|
|
||
| Pair = Struct.new(:head, :tail) | ||
|
|
||
| # A sliding window is a structure that stores the most recent entries that were pushed within the last slice of time | ||
| def initialize(time_window, time_source = nil) | ||
| @time_window_millis = time_window * 1000 | ||
| @time_source = time_source ? time_source : -> { Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) } | ||
| @window = [] | ||
| end | ||
|
|
||
| def count(&block) | ||
| _remove_old | ||
| vals = @window.map(&:tail) | ||
| vals.count(&block) | ||
| end | ||
|
|
||
| def each_with_object(memo, &block) | ||
| _remove_old | ||
| vals = @window.map(&:tail) | ||
| vals.each_with_object(memo, &block) | ||
| end | ||
|
|
||
| def push(value) | ||
| _remove_old # make room | ||
| @window << Pair.new(current_time, value) | ||
| self | ||
| end | ||
|
|
||
| alias_method :<<, :push | ||
|
|
||
| def clear | ||
| @window.clear | ||
| self | ||
| end | ||
|
|
||
| def last | ||
| @window.last&.tail | ||
| end | ||
|
|
||
| def remove_old | ||
| _remove_old | ||
| end | ||
damianthe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| alias_method :destroy, :clear | ||
|
|
||
| private | ||
|
|
||
| def _remove_old | ||
| midtime = current_time - time_window_millis | ||
| # special case, everything is too old | ||
| @window.clear if !@window.empty? && @window.last.head < midtime | ||
damianthe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # otherwise we find the index position where the cutoff is | ||
| idx = (0...@window.size).bsearch { |n| @window[n].head >= midtime } | ||
damianthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @window.slice!(0, idx) if idx | ||
| end | ||
|
|
||
| def current_time | ||
| @time_source.call | ||
| end | ||
| end | ||
| end | ||
|
|
||
| module ThreadSafe | ||
| class TimeSlidingWindow < Simple::TimeSlidingWindow | ||
| def initialize(*) | ||
| super | ||
| @lock = Mutex.new | ||
| end | ||
|
|
||
| # #size, #last, and #clear are not wrapped in a mutex. For the first two, | ||
| # the worst-case is a thread-switch at a timing where they'd receive an | ||
| # out-of-date value--which could happen with a mutex as well. | ||
| # | ||
| # As for clear, it's an all or nothing operation. Doesn't matter if we | ||
| # have the lock or not. | ||
|
|
||
| def count(*) | ||
| @lock.synchronize { super } | ||
| end | ||
|
|
||
| def each_with_object(*) | ||
| @lock.synchronize { super } | ||
| end | ||
|
|
||
| def remove_old | ||
| @lock.synchronize { super } | ||
| end | ||
|
|
||
| def push(*) | ||
| @lock.synchronize { super } | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.