-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] adding a random jitter to the delay (#18)
* [feature] adding a random jitter to the delay Also, exposing delay on the command line for run-job and watch-job
- Loading branch information
Showing
6 changed files
with
135 additions
and
9 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
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,33 @@ | ||
from random import uniform | ||
from typing import Union | ||
|
||
# The minimum delay in seconds between batch job API requests | ||
MINIMUM_DELAY: int = 1 | ||
|
||
# The default jitter width for adding jitter to the delay | ||
DEFAULT_JITTER_WIDTH: int = 2 | ||
|
||
|
||
def add_jitter( | ||
delay: Union[int, float] = 0, | ||
width: Union[int, float] = DEFAULT_JITTER_WIDTH, | ||
minima: Union[int, float] = 0, | ||
) -> float: | ||
"""Apply a jitter to the delay, to help avoid AWS batch API limits for monitoring batch | ||
jobs in the cases of many requests across concurrent jobs. | ||
Args: | ||
delay: the number of seconds to wait upon making a subsequent request | ||
width: the width for the random jitter, centered around delay, must be > 0 | ||
minima: the minimum delay allowed, must be >= 0 | ||
Returns: | ||
the new delay with the jitter applied (`uniform(delay - width, delay + width)`) | ||
""" | ||
assert width > 0, f"Width must be > 0: {width}" | ||
assert minima >= 0, f"Minima must be >= 0: {minima}" | ||
delay = max(0, delay) | ||
lower = max(minima, delay - width) | ||
upper = lower + (2 * width) | ||
assert upper >= lower | ||
return uniform(lower, upper) |
Empty file.
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,27 @@ | ||
"""Tests for :module:`~pyfgaws.core`""" | ||
|
||
from pyfgaws.core import add_jitter | ||
import pytest | ||
|
||
|
||
def test_add_jitter() -> None: | ||
for delay in [-10, 0, 1, 5, 10, 5.5]: | ||
for width in [1, 5, 10, 2.5]: | ||
for minima in [0, 1, 5, 10, 0.5]: | ||
for _ in range(15): | ||
jittered = add_jitter(delay=delay, width=width, minima=minima) | ||
label: str = f"add_jitter(delay={delay}, width={width}, minima={minima})" | ||
assert jittered >= minima, label | ||
assert jittered >= delay - width, label | ||
assert jittered <= max(minima, delay - width) + (2 * width), label | ||
|
||
|
||
def test_add_jitter_bad_args() -> None: | ||
with pytest.raises(AssertionError): | ||
add_jitter(width=0) | ||
with pytest.raises(AssertionError): | ||
add_jitter(width=-10) | ||
with pytest.raises(AssertionError): | ||
add_jitter(minima=-0.001) | ||
with pytest.raises(AssertionError): | ||
add_jitter(minima=-10) |