Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aws_advanced_python_wrapper/cluster_topology_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,10 +840,10 @@ def __call__(self) -> None:
try:
is_writer = self._monitor._topology_utils.get_writer_id_if_connected(
connection, self._monitor._plugin_service.driver_dialect) is not None
except Exception:
except Exception as ex:
logger.debug("HostMonitor.WriterCheckFailed", self._host_info.host, ex)
self._monitor._close_connection(connection)
connection = None
continue

if is_writer:
try:
Expand Down
17 changes: 7 additions & 10 deletions aws_advanced_python_wrapper/host_list_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,16 +583,13 @@ def create_host(
return host_info

def get_writer_id_if_connected(self, connection: Connection, driver_dialect: DriverDialect) -> Optional[str]:
try:
cursor_execute_func_with_timeout = preserve_transaction_status_with_timeout(
self._thread_pool, self._max_timeout_sec, driver_dialect, connection)(self._get_writer_id)
result = cursor_execute_func_with_timeout(connection)
if result:
host_id: str = result[0]
return host_id
return None
except Exception:
return None
cursor_execute_func_with_timeout = preserve_transaction_status_with_timeout(
self._thread_pool, self._max_timeout_sec, driver_dialect, connection)(self._get_writer_id)
result = cursor_execute_func_with_timeout(connection)
if result:
host_id: str = result[0]
return host_id
return None

def _get_writer_id(self, conn: Connection):
with closing(conn.cursor()) as cursor:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ HostMonitor.Exception=[HostMonitor] Host monitor for host {} is exiting due to a
HostMonitor.MonitorCompleted=[HostMonitor] Host monitor for {} completed in {} ms.
HostMonitor.WriterHostChanged=[HostMonitor] Writer host changed from {} to {}.
HostMonitor.WriterChangeExitTriggered=[HostMonitor] Reader observed a writer change to {} while some regions are inaccessible; signaling panic-mode exit.
HostMonitor.WriterCheckFailed=[HostMonitor] The writer check on host '{}' failed, so the host monitor connects to it again. Error: '{}'

HostMonitoringPlugin.ActivatedMonitoring=[HostMonitoringPlugin] Executing method '{}', monitoring is activated.
HostMonitoringPlugin.ClusterEndpointHostInfo=[HostMonitoringPlugin] The HostInfo to monitor is associated with a cluster endpoint. The plugin will attempt to identify the connected database instance.
Expand Down
21 changes: 11 additions & 10 deletions tests/integration/container/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@
from botocore.config import Config
from pytest_html import extras as html_extras # type: ignore[import-untyped]

from tests.integration.container.utils.attempt_state import AttemptState
from .utils.connection_utils import ConnectionUtils
from .utils.database_engine_deployment import DatabaseEngineDeployment
from .utils.proxy_helper import ProxyHelper
from .utils.rds_test_utility import RdsTestUtility
from .utils.rds_test_utility import RDS_API_RETRY_CONFIG, RdsTestUtility
from .utils.test_environment import TestEnvironment
from .utils.test_environment_features import TestEnvironmentFeatures
from .utils.test_timings import (CLUSTER_OUTAGE_RERUN_DELAY_SEC,
Expand Down Expand Up @@ -120,9 +121,11 @@ def pytest_runtest_setup(item):
_Attempt.failed_other_than_outage = False
ProxyHelper.connectivity_disabled = False
RdsTestUtility.failover_requested = False
timeout_settings = _get_attempt_timeout_settings(item)
AttemptState.reset(None if timeout_settings is None else timeit.default_timer() + timeout_settings.timeout)
# pytest-timeout cancels its timer when an attempt fails, so a rerun needs a new one
if getattr(item, "execution_count", 1) > 1:
_set_timeout_timer(item)
if timeout_settings is not None and getattr(item, "execution_count", 1) > 1:
item.config.hook.pytest_timeout_set_timer(item=item, settings=timeout_settings)

segment: Optional[Segment] = None
if TestEnvironmentFeatures.TELEMETRY_TRACES_ENABLED in TestEnvironment.get_current().get_features():
Expand All @@ -142,7 +145,7 @@ def pytest_runtest_setup(item):

deployment = request.get_database_engine_deployment()
if DatabaseEngineDeployment.AURORA == deployment or DatabaseEngineDeployment.RDS_MULTI_AZ_CLUSTER == deployment:
rds_utility = RdsTestUtility(info.get_region(), info.get_rds_endpoint())
rds_utility = RdsTestUtility(info.get_region(), info.get_rds_endpoint(), RDS_API_RETRY_CONFIG)
rds_utility.wait_until_cluster_has_desired_status(info.get_db_name(), "available")

# Need to ensure that cluster details through API matches topology fetched through SQL
Expand Down Expand Up @@ -274,17 +277,15 @@ class _Attempt:


def _phase_failed_on_cluster_outage(error: Optional[BaseException]) -> bool:
# A failure the test caused by cutting connectivity is not an outage
return not ProxyHelper.connectivity_disabled and RdsTestUtility.is_cluster_outage(
error, RdsTestUtility.failover_requested)
return RdsTestUtility.is_rerun_cause(error, RdsTestUtility.failover_requested, ProxyHelper.connectivity_disabled)


def _rerun_after_cluster_outage(error: Optional[BaseException]) -> bool:
# Rerun only when every failed phase of the attempt was an outage
return not _Attempt.failed_other_than_outage and _phase_failed_on_cluster_outage(error)


def _set_timeout_timer(item):
def _get_attempt_timeout_settings(item):
timeout_settings = pytest_timeout.get_env_settings(item.config)
timeout_marker = item.get_closest_marker("timeout")
if timeout_marker is not None:
Expand All @@ -302,8 +303,8 @@ def _set_timeout_timer(item):
func_only=timeout_marker.kwargs.get("func_only", timeout_settings.func_only))
# pytest-timeout sets a function-only timer itself at the call. No timeout, or 0, means none.
if timeout_settings.func_only or not timeout_settings.timeout:
return
item.config.hook.pytest_timeout_set_timer(item=item, settings=timeout_settings)
return None
return timeout_settings


# trylast makes this the inner wrapper, so the attempt's failures are recorded before
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/container/django/test_django_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@

from aws_advanced_python_wrapper.errors import FailoverSuccessError
from aws_advanced_python_wrapper.hostinfo import HostRole
from tests.integration.container.utils.rds_test_utility import RdsTestUtility
from tests.integration.container.utils.rds_test_utility import (
RDS_API_RETRY_CONFIG, RdsTestUtility)
from ..utils.conditions import (disable_on_features, enable_on_deployments,
enable_on_engines, enable_on_features,
enable_on_num_instances)
Expand Down Expand Up @@ -103,7 +104,7 @@ def create_custom_endpoint(self):
env_info = TestEnvironment.get_current().get_info()
region = env_info.get_region()

rds_client = client('rds', region_name=region)
rds_client = client('rds', region_name=region, config=RDS_API_RETRY_CONFIG)
if not self.reuse_existing_endpoint:
instances = env_info.get_database_info().get_instances()
self._create_endpoint(rds_client, instances[0:1])
Expand Down
9 changes: 5 additions & 4 deletions tests/integration/container/test_custom_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
from tests.integration.container.utils.database_engine_deployment import \
DatabaseEngineDeployment
from tests.integration.container.utils.driver_helper import DriverHelper
from tests.integration.container.utils.rds_test_utility import RdsTestUtility
from tests.integration.container.utils.rds_test_utility import (
RDS_API_RETRY_CONFIG, RdsTestUtility)
from tests.integration.container.utils.retry_helper import retry_until
from tests.integration.container.utils.test_environment import TestEnvironment
from tests.integration.container.utils.test_environment_features import \
Expand Down Expand Up @@ -114,7 +115,7 @@ def setup_and_teardown(self):
env_info = TestEnvironment.get_current().get_info()
region = env_info.get_region()

rds_client = client('rds', region_name=region)
rds_client = client('rds', region_name=region, config=RDS_API_RETRY_CONFIG)
if not self.reuse_existing_endpoint:
instances = env_info.get_database_info().get_instances()
self._create_endpoint(rds_client, instances[0:1])
Expand Down Expand Up @@ -370,7 +371,7 @@ def test_custom_endpoint_read_write_splitting__with_custom_endpoint_changes__wit
with pytest.raises(ReadWriteSplittingError):
conn.read_only = False

rds_client = client('rds', region_name=TestEnvironment.get_current().get_aurora_region())
rds_client = client('rds', region_name=TestEnvironment.get_current().get_aurora_region(), config=RDS_API_RETRY_CONFIG)

# Capture writer_id from the control plane, but re-confirm it after
# the endpoint stabilization wait. The preceding test_custom_endpoint_failover
Expand Down Expand Up @@ -484,7 +485,7 @@ def test_custom_endpoint_read_write_splitting__with_custom_endpoint_changes__wit
reader_id_to_add = instance_id
break

rds_client = client('rds', region_name=TestEnvironment.get_current().get_aurora_region())
rds_client = client('rds', region_name=TestEnvironment.get_current().get_aurora_region(), config=RDS_API_RETRY_CONFIG)
rds_client.modify_db_cluster_endpoint(
DBClusterEndpointIdentifier=self.endpoint_id,
StaticMembers=[original_writer_id, reader_id_to_add]
Expand Down
11 changes: 7 additions & 4 deletions tests/integration/container/test_custom_endpoint_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
from tests.integration.container.utils.database_engine import DatabaseEngine
from tests.integration.container.utils.database_engine_deployment import \
DatabaseEngineDeployment
from tests.integration.container.utils.rds_test_utility import RdsTestUtility
from tests.integration.container.utils.rds_test_utility import (
RDS_API_RETRY_CONFIG, RdsTestUtility)
from tests.integration.container.utils.test_environment import TestEnvironment
from tests.integration.container.utils.test_environment_features import \
TestEnvironmentFeatures
Expand Down Expand Up @@ -106,6 +107,8 @@ async def _query_host_role_async(
@disable_on_features([TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY,
TestEnvironmentFeatures.BLUE_GREEN_DEPLOYMENT,
TestEnvironmentFeatures.PERFORMANCE])
# Custom-endpoint changes can outlast the harness's 600 s per-test timeout.
@pytest.mark.timeout(1800)
class TestCustomEndpointAsync:
logger: ClassVar[Logger] = Logger(__name__)
endpoint_id: ClassVar[str] = f"test-endpoint-1-async-{uuid4()}"
Expand Down Expand Up @@ -151,7 +154,7 @@ def setup_and_teardown(self):
env_info = TestEnvironment.get_current().get_info()
region = env_info.get_region()

rds_client = client('rds', region_name=region)
rds_client = client('rds', region_name=region, config=RDS_API_RETRY_CONFIG)
if not self.reuse_existing_endpoint:
instances = env_info.get_database_info().get_instances()
self._create_endpoint(rds_client, instances[0:1])
Expand Down Expand Up @@ -441,7 +444,7 @@ async def inner():

writer_id = rds_utils.get_cluster_writer_instance_id()

rds_client = client('rds', region_name=TestEnvironment.get_current().get_aurora_region())
rds_client = client('rds', region_name=TestEnvironment.get_current().get_aurora_region(), config=RDS_API_RETRY_CONFIG)
rds_client.modify_db_cluster_endpoint(
DBClusterEndpointIdentifier=self.endpoint_id,
StaticMembers=[original_reader_id, writer_id]
Expand Down Expand Up @@ -533,7 +536,7 @@ async def inner():
reader_id_to_add = instance_id
break

rds_client = client('rds', region_name=TestEnvironment.get_current().get_aurora_region())
rds_client = client('rds', region_name=TestEnvironment.get_current().get_aurora_region(), config=RDS_API_RETRY_CONFIG)
rds_client.modify_db_cluster_endpoint(
DBClusterEndpointIdentifier=self.endpoint_id,
StaticMembers=[original_writer_id, reader_id_to_add]
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/container/utils/attempt_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from typing import ClassVar, Optional


class AttemptState:
"""What the failover helper and the conftest share about the current test attempt.

The container imports these utils under two package names, so every user imports this module as
``tests.integration.container.utils.attempt_state`` to get the one copy.
"""

# The timeit.default_timer() value at which pytest-timeout ends the attempt
deadline: ClassVar[Optional[float]] = None
slow_failover_sec: ClassVar[Optional[float]] = None

@classmethod
def reset(cls, deadline: Optional[float]) -> None:
cls.deadline = deadline
cls.slow_failover_sec = None
Loading
Loading