|
7 | 7 | automatic backoff/retry of HTTP requests. |
8 | 8 |
|
9 | 9 | """ |
| 10 | +import importlib |
| 11 | +import multiprocessing |
| 12 | +import random |
| 13 | +from contextlib import contextmanager |
10 | 14 | from http import HTTPStatus |
11 | 15 |
|
| 16 | +import requests_mock |
| 17 | +from distutils.util import strtobool |
| 18 | +from pds.ingress.util.config_util import ConfigUtil |
| 19 | + |
12 | 20 | # When leveraging this module with the Lambda service functions, the requests |
13 | 21 | # module will not be available within the Python runtime. |
14 | 22 | # It should not be needed by those Lambda's, so use MagicMock to bypass any |
|
24 | 32 | SSLError = MagicMock() |
25 | 33 |
|
26 | 34 |
|
| 35 | +MOCK_REQUESTS_SEMAPHORE = multiprocessing.Semaphore(1) |
| 36 | + |
| 37 | + |
27 | 38 | def fatal_code(err: requests.exceptions.RequestException) -> bool: |
28 | 39 | """ |
29 | 40 | Determines if the HTTP return code associated with a requests exception |
@@ -60,3 +71,138 @@ def fatal_code(err: requests.exceptions.RequestException) -> bool: |
60 | 71 | else: |
61 | 72 | # No response to interrogate, so default to no retry |
62 | 73 | return True |
| 74 | + |
| 75 | + |
| 76 | +def check_failure_chance(percentage: int) -> bool: |
| 77 | + """ |
| 78 | + Checks if a simulated failure event should occur based on a given percentage |
| 79 | + chance. |
| 80 | +
|
| 81 | + Parameters |
| 82 | + ---------- |
| 83 | + percentage : int |
| 84 | + The desired percentage chance (e.g., 70 for 70%). |
| 85 | +
|
| 86 | + Returns |
| 87 | + ------- |
| 88 | + bool: True if the failure should occur, False otherwise. |
| 89 | +
|
| 90 | + Raises |
| 91 | + ------ |
| 92 | + ValueError: If the percentage is not between 0 and 100. |
| 93 | +
|
| 94 | + """ |
| 95 | + if not (0 <= percentage <= 100): |
| 96 | + raise ValueError("Percentage must be between 0 and 100.") |
| 97 | + |
| 98 | + # Generate a random float between 0.0 and 1.0 |
| 99 | + random_number = random.random() |
| 100 | + |
| 101 | + # Convert the percentage to a decimal for comparison |
| 102 | + chance_threshold = percentage / 100.0 |
| 103 | + |
| 104 | + return random_number < chance_threshold |
| 105 | + |
| 106 | + |
| 107 | +def _simulate_requests_failure(mock_requests, url, http_method, enable_key, failure_rate_key, failure_class_key): |
| 108 | + """ |
| 109 | + Simulates a random failure for S3 ingress by registering the provided |
| 110 | + ingress URL with the requests mocker to raise an HTTPError exception. |
| 111 | +
|
| 112 | + Whether the failure is simulated is determined by the `simulate_ingress_failures` |
| 113 | + configuration option and the `ingress_failure_rate` percentage chance within |
| 114 | + the optional DEBUG section of the INI config. If this section is not present, |
| 115 | + this function should always default to not simulating a failure. |
| 116 | +
|
| 117 | + If the failure is not simulated, the mock_requests instance is reset to |
| 118 | + ensure no previous failures are registered. |
| 119 | +
|
| 120 | + Parameters |
| 121 | + ---------- |
| 122 | + mock_requests : requests_mock.Mocker |
| 123 | + The requests mocker instance to register the simulated failure with. |
| 124 | + url : str |
| 125 | + The URL to which the simulated failure will be applied. |
| 126 | + http_method : str |
| 127 | + HTTP method to register the failure for (e.g., 'POST', 'PUT'). |
| 128 | + enable_key : str |
| 129 | + Name of the INI key to check if failure simulation is enabled. |
| 130 | + failure_rate_key : str |
| 131 | + Name of the INI key that specifies the percentage chance of failure. |
| 132 | + failure_class_key : str |
| 133 | + Name of the INI key that specifies the exception class to raise on failure. |
| 134 | +
|
| 135 | + """ |
| 136 | + config = ConfigUtil.get_config() |
| 137 | + |
| 138 | + # Check if simulated failures are enabled, and if so, if we should simulate |
| 139 | + # a failure via mock_requests based on the configured failure chance |
| 140 | + if bool(strtobool(config.get("DEBUG", enable_key, fallback="false"))): |
| 141 | + if check_failure_chance(int(config.get("DEBUG", failure_rate_key, fallback="0"))): |
| 142 | + # Dynamically import the exception class to raise |
| 143 | + failure_class_str = config.get("DEBUG", failure_class_key, fallback="builtins.RuntimeError") |
| 144 | + failure_class_module, failure_exception_name = failure_class_str.rsplit(".", 1) |
| 145 | + |
| 146 | + module = importlib.import_module(failure_class_module) |
| 147 | + exception_klass = getattr(module, failure_exception_name) |
| 148 | + |
| 149 | + # Register the URL with the mock_requests to raise the specified exception |
| 150 | + mock_requests.register_uri(http_method, url, exc=exception_klass) |
| 151 | + |
| 152 | + return mock_requests |
| 153 | + |
| 154 | + |
| 155 | +@contextmanager |
| 156 | +def simulate_batch_request_failure(api_gateway_url): |
| 157 | + """ |
| 158 | + Simulates a random failure for an ingress batch request by registering the |
| 159 | + provided ingress URL with the requests mocker to raise an HTTPError exception. |
| 160 | +
|
| 161 | + Parameters |
| 162 | + ---------- |
| 163 | + api_gateway_url : str |
| 164 | + The API Gateway URL to which the simulated failure will be applied. |
| 165 | +
|
| 166 | + """ |
| 167 | + with MOCK_REQUESTS_SEMAPHORE: |
| 168 | + with requests_mock.Mocker(real_http=True) as mock_requests: |
| 169 | + try: |
| 170 | + yield _simulate_requests_failure( |
| 171 | + mock_requests, |
| 172 | + api_gateway_url, |
| 173 | + "POST", |
| 174 | + "simulate_batch_request_failures", |
| 175 | + "batch_request_failure_rate", |
| 176 | + "batch_request_failure_class", |
| 177 | + ) |
| 178 | + finally: |
| 179 | + # Remove any previously registered URL(s) |
| 180 | + mock_requests.reset() |
| 181 | + |
| 182 | + |
| 183 | +@contextmanager |
| 184 | +def simulate_ingress_failure(s3_ingress_url): |
| 185 | + """ |
| 186 | + Simulates a random failure for S3 ingress by registering the provided |
| 187 | + ingress URL with the requests mocker to raise an HTTPError exception. |
| 188 | +
|
| 189 | + Parameters |
| 190 | + ---------- |
| 191 | + s3_ingress_url : str |
| 192 | + The S3 ingress URL to which the simulated failure will be applied. |
| 193 | +
|
| 194 | + """ |
| 195 | + with MOCK_REQUESTS_SEMAPHORE: |
| 196 | + with requests_mock.Mocker(real_http=True) as mock_requests: |
| 197 | + try: |
| 198 | + yield _simulate_requests_failure( |
| 199 | + mock_requests, |
| 200 | + s3_ingress_url, |
| 201 | + "PUT", |
| 202 | + "simulate_ingress_failures", |
| 203 | + "ingress_failure_rate", |
| 204 | + "ingress_failure_class", |
| 205 | + ) |
| 206 | + finally: |
| 207 | + # Remove any previously registered URL(s) |
| 208 | + mock_requests.reset() |
0 commit comments