This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backoff strategy on audit extractions (#93)
Co-authored-by: felipesantoos <[email protected]> Co-authored-by: wallrony <[email protected]>
- Loading branch information
1 parent
db87135
commit ee67e72
Showing
6 changed files
with
153 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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,29 @@ | ||
|
||
require 'json' | ||
|
||
DEFAULT_RETRY_INTERVAL = 2 | ||
BACKOFF_MAX_RETRIES = 3 | ||
|
||
def backoff_retry(exec_fn, entity, error_file_path) | ||
retries = 0 | ||
begin | ||
result = exec_fn.() | ||
errors = "" | ||
if File.exist?(error_file_path) | ||
errors = File.read(error_file_path) | ||
end | ||
if errors != "" | ||
raise StandardError.new errors.chomp | ||
end | ||
result | ||
rescue StandardError => e | ||
if retries < BACKOFF_MAX_RETRIES | ||
puts "#{JSON.generate({"error_message" => "#{e.to_s} (retrying for the #{retries + 1} time)"})}" | ||
sleep DEFAULT_RETRY_INTERVAL ** retries | ||
retries += 1 | ||
retry | ||
else | ||
puts "#{JSON.generate({"error_message" => "Aborting audit #{entity} extraction - the retrials count was exceeded"})}" | ||
end | ||
end | ||
end |
This file contains 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 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,91 @@ | ||
|
||
require 'test/unit' | ||
require 'fluent/test' | ||
require 'fluent/test/helpers' | ||
require_relative '../../fluentd/scripts/dump_utils' | ||
require_relative '../stdout_utils' | ||
|
||
FILE_PATH = "#{`pwd`.chomp}/sdm-audit-test-errors.log" | ||
|
||
class TestBackoffRetry < Test::Unit::TestCase | ||
def cleanup | ||
super | ||
if File.exist?(FILE_PATH) | ||
File.delete(FILE_PATH) | ||
end | ||
end | ||
|
||
def teardown | ||
super | ||
if File.exist?(FILE_PATH) | ||
File.delete(FILE_PATH) | ||
end | ||
end | ||
|
||
def test_when_run_successfully | ||
calls_count = 0 | ||
result = nil | ||
omit_stdout do | ||
result = backoff_retry(-> () { | ||
calls_count += 1 | ||
mock_success | ||
}, "test", FILE_PATH) | ||
end | ||
assert_true(result) | ||
assert_true(File.exist?(FILE_PATH)) | ||
assert_empty(File.read(FILE_PATH)) | ||
assert_equal(1, calls_count) | ||
end | ||
|
||
def test_when_fails_and_dont_exceed_retry | ||
calls_count = 0 | ||
result = nil | ||
omit_stdout do | ||
result = backoff_retry(-> () { | ||
calls_count += 1 | ||
mock_fail_once | ||
}, "test", FILE_PATH) | ||
end | ||
assert_true(result) | ||
assert_true(File.exist?(FILE_PATH)) | ||
assert_empty(File.read(FILE_PATH)) | ||
assert_equal(2, calls_count) | ||
end | ||
|
||
def test_when_exceed_retry | ||
calls_count = 0 | ||
result = nil | ||
omit_stdout do | ||
result = backoff_retry(-> () { | ||
calls_count += 1 | ||
mock_fail | ||
}, "test", FILE_PATH) | ||
end | ||
assert_nil(result) | ||
assert_true(File.exist?(FILE_PATH)) | ||
assert_not_empty(File.read(FILE_PATH)) | ||
assert_equal(4, calls_count) | ||
end | ||
|
||
private | ||
def mock_success | ||
File.new(FILE_PATH, "w+") | ||
true | ||
end | ||
|
||
def mock_fail_once | ||
if File.exist?(FILE_PATH) | ||
mock_success | ||
else | ||
mock_fail | ||
nil | ||
end | ||
end | ||
|
||
def mock_fail | ||
File.write(FILE_PATH, "w") do |file| | ||
file.write("error") | ||
end | ||
nil | ||
end | ||
end |
This file contains 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,12 @@ | ||
|
||
require 'fluent/test/driver/output' | ||
|
||
def omit_stdout | ||
original_stdout = $stdout | ||
$stdout = StringIO.new | ||
begin | ||
yield | ||
ensure | ||
$stdout = original_stdout | ||
end | ||
end |