Skip to content

Commit 3ccc9b5

Browse files
committed
test: add retries for is_db_instance_writer checks
1 parent e181670 commit 3ccc9b5

8 files changed

Lines changed: 95 additions & 13 deletions

File tree

‎tests/integration/container/django/test_django_plugins.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
enable_on_num_instances)
3939
from ..utils.database_engine import DatabaseEngine
4040
from ..utils.database_engine_deployment import DatabaseEngineDeployment
41+
from ..utils.retry_helper import retry_until
4142
from ..utils.test_environment import TestEnvironment
4243
from ..utils.test_environment_features import TestEnvironmentFeatures
4344

@@ -609,7 +610,8 @@ def test_django_failover_during_query(self, test_environment: TestEnvironment, d
609610
with connection.cursor() as cursor:
610611
cursor.execute(RdsTestUtility.get_instance_id_query())
611612
current_writer_id = cursor.fetchone()[0]
612-
assert rds_utils.is_db_instance_writer(current_writer_id) is True
613+
# RDS API lags behind the writer election, so we retry the check.
614+
assert retry_until(lambda: rds_utils.is_db_instance_writer(current_writer_id))
613615
assert current_writer_id != initial_writer_id, "Should be connected to a new writer after failover"
614616

615617
# Clean up test data
@@ -670,7 +672,8 @@ def test_django_custom_endpoint_failover_during_query(
670672
with connection.cursor() as cursor:
671673
cursor.execute(RdsTestUtility.get_instance_id_query())
672674
current_writer_id = cursor.fetchone()[0]
673-
assert rds_utils.is_db_instance_writer(current_writer_id) is True
675+
# RDS API lags behind the writer election, so we retry the check.
676+
assert retry_until(lambda: rds_utils.is_db_instance_writer(current_writer_id))
674677
assert current_writer_id != initial_writer_id, "Should be connected to a new writer after failover"
675678

676679
# Clean up test data

‎tests/integration/container/sqlalchemy/test_sqlalchemy_plugins.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
enable_on_num_instances)
4343
from ..utils.database_engine import DatabaseEngine
4444
from ..utils.database_engine_deployment import DatabaseEngineDeployment
45+
from ..utils.retry_helper import retry_until
4546
from ..utils.test_environment import TestEnvironment
4647
from ..utils.test_environment_features import TestEnvironmentFeatures
4748

@@ -512,7 +513,8 @@ def test_sqlalchemy_failover_during_query(self, test_environment: TestEnvironmen
512513
current_writer_id = row._tuple()[0]
513514
else:
514515
raise Exception("Failed to get current_writer_id from row because row was None.")
515-
assert rds_utils.is_db_instance_writer(current_writer_id) is True
516+
# RDS API lags behind the writer election, so we retry the check.
517+
assert retry_until(lambda: rds_utils.is_db_instance_writer(current_writer_id))
516518
assert current_writer_id != initial_writer_id
517519

518520
session.query(TestModel).delete()

‎tests/integration/container/test_aurora_failover.py‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from aws_advanced_python_wrapper.wrapper import AwsWrapperConnection
3939
from .utils.driver_helper import DriverHelper
4040
from .utils.rds_test_utility import RdsTestUtility
41+
from .utils.retry_helper import retry_until
4142
from .utils.test_environment import TestEnvironment
4243
from .utils.test_environment_features import TestEnvironmentFeatures
4344

@@ -116,7 +117,8 @@ def test_fail_from_writer_to_new_writer_fail_on_connection_invocation(
116117

117118
# assert that we are connected to the new writer after failover happens.
118119
current_connection_id = aurora_utility.query_instance_id(aws_conn)
119-
assert aurora_utility.is_db_instance_writer(current_connection_id) is True
120+
# RDS API lags behind the writer election, so we retry the check.
121+
assert retry_until(lambda: aurora_utility.is_db_instance_writer(current_connection_id))
120122
assert current_connection_id != initial_writer_id
121123

122124
@pytest.mark.parametrize("plugins", ["failover", "failover_v2"])
@@ -137,7 +139,8 @@ def test_fail_from_writer_to_new_writer_fail_on_connection_bound_object_invocati
137139

138140
# assert that we are connected to the new writer after failover happens and we can reuse the cursor
139141
current_connection_id = aurora_utility.query_instance_id(aws_conn)
140-
assert aurora_utility.is_db_instance_writer(current_connection_id) is True
142+
# RDS API lags behind the writer election, so we retry the check.
143+
assert retry_until(lambda: aurora_utility.is_db_instance_writer(current_connection_id))
141144
assert current_connection_id != initial_writer_id
142145

143146
@pytest.mark.parametrize("plugins", ["failover,host_monitoring", "failover,host_monitoring_v2",
@@ -166,7 +169,8 @@ def test_fail_from_reader_to_writer(
166169
current_connection_id = aurora_utility.query_instance_id(aws_conn)
167170

168171
assert writer_id == current_connection_id
169-
assert aurora_utility.is_db_instance_writer(current_connection_id) is True
172+
# RDS API lags behind the writer election, so we retry the check.
173+
assert retry_until(lambda: aurora_utility.is_db_instance_writer(current_connection_id))
170174

171175
@pytest.mark.parametrize("plugins", ["failover", "failover_v2"])
172176
@enable_on_features([TestEnvironmentFeatures.FAILOVER_SUPPORTED])
@@ -195,7 +199,8 @@ def test_fail_from_writer_with_session_states_autocommit(self, test_driver: Test
195199
# Attempt to query the instance id.
196200
current_connection_id = aurora_utility.query_instance_id(conn)
197201
# Assert that we are connected to the new writer after failover happens.
198-
assert aurora_utility.is_db_instance_writer(current_connection_id) is True
202+
# RDS API lags behind the writer election, so we retry the check.
203+
assert retry_until(lambda: aurora_utility.is_db_instance_writer(current_connection_id))
199204
next_cluster_writer_id = aurora_utility.get_cluster_writer_instance_id()
200205
assert current_connection_id == next_cluster_writer_id
201206
assert current_connection_id != initial_writer_id
@@ -229,7 +234,8 @@ def test_fail_from_writer_with_session_states_readonly(self, test_driver: TestDr
229234
# Attempt to query the instance id.
230235
current_connection_id = aurora_utility.query_instance_id(conn)
231236
# Assert that we are connected to the new writer after failover happens.
232-
assert aurora_utility.is_db_instance_writer(current_connection_id) is True
237+
# RDS API lags behind the writer election, so we retry the check.
238+
assert retry_until(lambda: aurora_utility.is_db_instance_writer(current_connection_id))
233239
next_cluster_writer_id = aurora_utility.get_cluster_writer_instance_id()
234240
assert current_connection_id == next_cluster_writer_id
235241
assert current_connection_id != initial_writer_id
@@ -265,7 +271,8 @@ def test_writer_fail_within_transaction_set_autocommit_false(
265271
current_connection_id: str = aurora_utility.query_instance_id(conn)
266272

267273
# assert that we are connected to the new writer after failover happens
268-
assert aurora_utility.is_db_instance_writer(current_connection_id)
274+
# RDS API lags behind the writer election, so we retry the check.
275+
assert retry_until(lambda: aurora_utility.is_db_instance_writer(current_connection_id))
269276
next_cluster_writer_id: str = aurora_utility.get_cluster_writer_instance_id()
270277

271278
assert current_connection_id == next_cluster_writer_id
@@ -311,7 +318,8 @@ def test_writer_fail_within_transaction_start_transaction(
311318
current_connection_id: str = aurora_utility.query_instance_id(conn)
312319

313320
# assert that we are connected to the new writer after failover happens
314-
assert aurora_utility.is_db_instance_writer(current_connection_id)
321+
# RDS API lags behind the writer election, so we retry the check.
322+
assert retry_until(lambda: aurora_utility.is_db_instance_writer(current_connection_id))
315323
next_cluster_writer_id: str = aurora_utility.get_cluster_writer_instance_id()
316324

317325
assert current_connection_id == next_cluster_writer_id

‎tests/integration/container/test_aws_secrets_manager.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from .utils.database_engine_deployment import DatabaseEngineDeployment
2929
from .utils.driver_helper import DriverHelper
3030
from .utils.rds_test_utility import RdsTestUtility
31+
from .utils.retry_helper import retry_until
3132
from .utils.test_environment import TestEnvironment
3233
from .utils.test_environment_features import TestEnvironmentFeatures
3334

@@ -231,7 +232,8 @@ def test_failover_with_secrets_manager(
231232
aurora_utility.assert_first_query_throws(aws_conn, FailoverSuccessError)
232233

233234
current_connection_id = aurora_utility.query_instance_id(aws_conn)
234-
assert aurora_utility.is_db_instance_writer(current_connection_id) is True
235+
# RDS API lags behind the writer election, so we retry the check.
236+
assert retry_until(lambda: aurora_utility.is_db_instance_writer(current_connection_id))
235237
assert current_connection_id != initial_writer_id
236238

237239
def validate_connection(self, target_driver_connect: Callable, **connect_params):

‎tests/integration/container/test_custom_endpoint.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
DatabaseEngineDeployment
4040
from tests.integration.container.utils.driver_helper import DriverHelper
4141
from tests.integration.container.utils.rds_test_utility import RdsTestUtility
42+
from tests.integration.container.utils.retry_helper import retry_until
4243
from tests.integration.container.utils.test_environment import TestEnvironment
4344
from tests.integration.container.utils.test_environment_features import \
4445
TestEnvironmentFeatures
@@ -327,6 +328,10 @@ def test_custom_endpoint_read_write_splitting__with_custom_endpoint_changes__wit
327328
with pytest.raises(ReadWriteSplittingError):
328329
conn.read_only = False
329330

331+
# The RDS API lags behind the writer election triggered during setup, so it may still report
332+
# the previous writer (now the reader we are connected to). Retry until the API reflects a
333+
# writer distinct from our reader, otherwise StaticMembers would contain duplicate ids.
334+
assert retry_until(lambda: rds_utils.get_cluster_writer_instance_id() != original_reader_id)
330335
writer_id = rds_utils.get_cluster_writer_instance_id()
331336

332337
rds_client = client('rds', region_name=TestEnvironment.get_current().get_aurora_region())
@@ -398,6 +403,10 @@ def test_custom_endpoint_read_write_splitting__with_custom_endpoint_changes__wit
398403
assert new_instance_id == original_writer_id
399404

400405
instances = TestEnvironment.get_current().get_instances()
406+
# The RDS API lags behind the writer election triggered during setup, so it may still report
407+
# a stale writer. Retry until the API agrees the instance we are connected to is the writer,
408+
# otherwise the reader selection below could pick our own writer and create duplicate ids.
409+
assert retry_until(lambda: rds_utils.get_cluster_writer_instance_id() == original_writer_id)
401410
writer_id = str(rds_utils.get_cluster_writer_instance_id())
402411

403412
reader_id_to_add = ""

‎tests/integration/container/test_iam_authentication.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
DatabaseEngineDeployment
4141
from tests.integration.container.utils.driver_helper import DriverHelper
4242
from tests.integration.container.utils.rds_test_utility import RdsTestUtility
43+
from tests.integration.container.utils.retry_helper import retry_until
4344
from tests.integration.container.utils.test_environment import TestEnvironment
4445

4546

@@ -164,7 +165,8 @@ def test_failover_with_iam(
164165

165166
# assert that we are connected to the new writer after failover happens and we can reuse the cursor
166167
current_connection_id = aurora_utility.query_instance_id(aws_conn)
167-
assert aurora_utility.is_db_instance_writer(current_connection_id) is True
168+
# RDS API lags behind the writer election, so we retry the check.
169+
assert retry_until(lambda: aurora_utility.is_db_instance_writer(current_connection_id))
168170
assert current_connection_id != initial_writer_id
169171

170172
def get_ip_address(self, hostname: str):

‎tests/integration/container/test_read_write_splitting.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from tests.integration.container.utils.driver_helper import DriverHelper
4141
from tests.integration.container.utils.proxy_helper import ProxyHelper
4242
from tests.integration.container.utils.rds_test_utility import RdsTestUtility
43+
from tests.integration.container.utils.retry_helper import retry_until
4344
from tests.integration.container.utils.test_driver import TestDriver
4445
from tests.integration.container.utils.test_environment import TestEnvironment
4546
from tests.integration.container.utils.test_environment_features import \
@@ -536,7 +537,8 @@ def test_failover_to_new_writer__switch_read_only(
536537

537538
new_writer_id = rds_utils.query_instance_id(conn)
538539
assert original_writer_id != new_writer_id
539-
assert rds_utils.is_db_instance_writer(new_writer_id)
540+
# RDS API lags behind the writer election, so we retry the check.
541+
assert retry_until(lambda: rds_utils.is_db_instance_writer(new_writer_id))
540542

541543
PluginServiceImpl._host_availability_expiring_cache.clear()
542544

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License").
4+
# You may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
import timeit
18+
from time import sleep
19+
from typing import TYPE_CHECKING, Callable
20+
21+
from aws_advanced_python_wrapper.utils.log import Logger
22+
23+
if TYPE_CHECKING:
24+
from .rds_test_utility import RdsTestUtility
25+
26+
logger = Logger(__name__)
27+
28+
_DEFAULT_TIMEOUT_SEC: float = 5 * 60 # 5 minutes
29+
_DEFAULT_DELAY_SEC: float = 5 # 5 seconds
30+
31+
32+
def retry_until(
33+
condition: Callable[[], bool],
34+
timeout_sec: float = _DEFAULT_TIMEOUT_SEC,
35+
delay_sec: float = _DEFAULT_DELAY_SEC) -> bool:
36+
deadline = timeit.default_timer() + timeout_sec
37+
38+
while timeit.default_timer() < deadline:
39+
if condition():
40+
return True
41+
sleep(delay_sec)
42+
43+
return False
44+
45+
46+
def verify_writer(aurora_util: RdsTestUtility, expected_writer_id: str) -> bool:
47+
logger.debug("Expected writer (API): " + expected_writer_id)
48+
49+
def _condition() -> bool:
50+
api_writer_id = aurora_util.get_cluster_writer_instance_id()
51+
logger.debug("Writer (API): " + api_writer_id)
52+
return expected_writer_id.lower() == api_writer_id.lower()
53+
54+
return retry_until(_condition)

0 commit comments

Comments
 (0)