From 3482b194fe8b8df94c7bd8dafffe604b243d8be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Rito?= <16895040+ritojoao@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:24:26 -0300 Subject: [PATCH] fix: blue/green re-selects routing from latest status and matches endpoints by host and port Two defects in the sync Blue/Green plugin (#1352): - connect() and execute() re-selected the routing from self._bg_status, captured before the retry loop, instead of the latest published status. Once the switchover moved on, the old routing kept returning None and was picked again forever, so the call never returned. The loop now updates self._bg_status and re-selects from latest_status, falling through to a direct connect/execute when no routing matches, as the async plugin does. - BaseRouting.is_match compared the routing endpoint ("host" or "host:port") with host_info.url, which ends with "/", so endpoint-scoped routings never matched. It now compares with get_host_and_port(), like the JDBC wrapper. Same fix in the async plugin. Adds sync unit tests for both paths and updates the async is_match test, which encoded the trailing "/". --- .../aio/blue_green_plugin.py | 4 +- .../blue_green_plugin.py | 20 ++- tests/unit/test_aio_blue_green_plugin.py | 11 +- tests/unit/test_blue_green_plugin.py | 123 ++++++++++++++++++ 4 files changed, 149 insertions(+), 9 deletions(-) diff --git a/aws_advanced_python_wrapper/aio/blue_green_plugin.py b/aws_advanced_python_wrapper/aio/blue_green_plugin.py index 7c1898d80..3e7054417 100644 --- a/aws_advanced_python_wrapper/aio/blue_green_plugin.py +++ b/aws_advanced_python_wrapper/aio/blue_green_plugin.py @@ -318,7 +318,9 @@ def is_match(self, host_info: Optional[HostInfo], bg_role: BlueGreenRole) -> boo if host_info is None: return False - return self._endpoint == host_info.url.lower() and (self._bg_role is None or self._bg_role == bg_role) + # Routing endpoints are "host" or "host:port"; host_info.url has a trailing "/" and would never match. + return (self._endpoint == host_info.get_host_and_port().lower() + and (self._bg_role is None or self._bg_role == bg_role)) def __str__(self) -> str: endpoint_str = "None" if self._endpoint is None else f"'{self._endpoint}'" diff --git a/aws_advanced_python_wrapper/blue_green_plugin.py b/aws_advanced_python_wrapper/blue_green_plugin.py index 19adb9d56..394543511 100644 --- a/aws_advanced_python_wrapper/blue_green_plugin.py +++ b/aws_advanced_python_wrapper/blue_green_plugin.py @@ -315,7 +315,9 @@ def is_match(self, host_info: Optional[HostInfo], bg_role: BlueGreenRole) -> boo if host_info is None: return False - return self._endpoint == host_info.url.lower() and (self._bg_role is None or self._bg_role == bg_role) + # Routing endpoints are "host" or "host:port"; host_info.url has a trailing "/" and would never match. + return (self._endpoint == host_info.get_host_and_port().lower() + and (self._bg_role is None or self._bg_role == bg_role)) def __str__(self): endpoint_str = "None" if self._endpoint is None else f"'{self._endpoint}'" @@ -689,8 +691,13 @@ def connect( self._end_time_ns.set(perf_counter_ns()) return self._open_direct_connection(connect_func, is_initial_connection) - routing = \ - next((r for r in self._bg_status.connect_routings if r.is_match(host_info, bg_role)), None) + # Re-select against the latest status, not the one captured before the loop: once the + # switchover moves on, the old routing keeps returning None and would be picked forever. + self._bg_status = latest_status + bg_role = latest_status.get_role(host_info) + if bg_role is None: + break + routing = next((r for r in latest_status.connect_routings if r.is_match(host_info, bg_role)), None) self._end_time_ns.set(perf_counter_ns()) if conn is None: @@ -761,8 +768,11 @@ def execute(self, target: type, method_name: str, execute_func: Callable, *args: self._end_time_ns.set(perf_counter_ns()) return execute_func() - routing = \ - next((r for r in self._bg_status.execute_routings if r.is_match(host_info, bg_role)), None) + self._bg_status = latest_status + bg_role = None if host_info is None else latest_status.get_role(host_info) + if bg_role is None: + break + routing = next((r for r in latest_status.execute_routings if r.is_match(host_info, bg_role)), None) self._end_time_ns.set(perf_counter_ns()) if result_container.is_present(): diff --git a/tests/unit/test_aio_blue_green_plugin.py b/tests/unit/test_aio_blue_green_plugin.py index 2ef8deecf..ef0bace85 100644 --- a/tests/unit/test_aio_blue_green_plugin.py +++ b/tests/unit/test_aio_blue_green_plugin.py @@ -299,13 +299,18 @@ def test_execute_passes_through_when_host_not_in_bg(): execute_func.assert_awaited_once() -def test_substitute_connect_routing_is_match_by_url(): - """Endpoint matching mirrors sync BaseRouting.is_match (host_info.url).""" +def test_substitute_connect_routing_is_match_by_host_and_port(): + """Endpoint matching mirrors sync BaseRouting.is_match: routing endpoints + are "host" or "host:port", compared against host_info.get_host_and_port().""" routing = SubstituteConnectRouting( - HostInfo("10.0.0.1", 5432), "h:5432/", BlueGreenRole.SOURCE) + HostInfo("10.0.0.1", 5432), "h:5432", BlueGreenRole.SOURCE) assert routing.is_match(HostInfo("h", 5432), BlueGreenRole.SOURCE) is True assert routing.is_match(HostInfo("other", 5432), BlueGreenRole.SOURCE) is False + host_only = SubstituteConnectRouting( + HostInfo("10.0.0.1", 5432), "h", BlueGreenRole.SOURCE) + assert host_only.is_match(HostInfo("h"), BlueGreenRole.SOURCE) is True + def test_factory_produces_async_blue_green_plugin(): factory = PLUGIN_FACTORIES["bg"] diff --git a/tests/unit/test_blue_green_plugin.py b/tests/unit/test_blue_green_plugin.py index c28726095..b1a263159 100644 --- a/tests/unit/test_blue_green_plugin.py +++ b/tests/unit/test_blue_green_plugin.py @@ -12,7 +12,70 @@ # See the License for the specific language governing permissions and # limitations under the License. +from typing import Any, Optional +from unittest.mock import MagicMock + +import pytest + +from aws_advanced_python_wrapper.blue_green_plugin import ( + BlueGreenPhase, BlueGreenPlugin, BlueGreenRole, BlueGreenStatus, + ConnectRouting, ExecuteRouting, PassThroughConnectRouting) +from aws_advanced_python_wrapper.hostinfo import HostInfo from aws_advanced_python_wrapper.utils.messages import Messages +from aws_advanced_python_wrapper.utils.properties import Properties +from aws_advanced_python_wrapper.utils.value_container import ValueContainer + + +@pytest.fixture(autouse=True) +def _clear_status_providers(): + BlueGreenPlugin._status_providers.clear() + yield + BlueGreenPlugin._status_providers.clear() + + +class _NoneConnectRouting(ConnectRouting): + """Matches every host and never produces a connection, like a suspend routing whose wait ended.""" + + def __init__(self): + self.calls = 0 + + def is_match(self, host_info: Optional[HostInfo], role: BlueGreenRole) -> bool: + return True + + def apply(self, plugin, host_info, props, is_initial_connection, connect_func, plugin_service): + self.calls += 1 + return None + + +class _EmptyExecuteRouting(ExecuteRouting): + """Matches every host and never produces a result.""" + + def __init__(self): + self.calls = 0 + + def is_match(self, host_info: Optional[HostInfo], role: BlueGreenRole) -> bool: + return True + + def apply(self, plugin, plugin_service, props, target, method_name, execute_func, + *args: Any, **kwargs: Any) -> ValueContainer[Any]: + self.calls += 1 + return ValueContainer.empty() + + +def _status(phase=BlueGreenPhase.IN_PROGRESS, connect_routings=None, execute_routings=None, host="h"): + return BlueGreenStatus( + "1", phase, connect_routings, execute_routings, {host: BlueGreenRole.SOURCE}) + + +def _plugin(statuses, current_host_info: Optional[HostInfo] = None): + svc = MagicMock() + svc.network_bound_methods = {"Cursor.execute"} + svc.current_host_info = current_host_info + # A bounded side_effect makes a regression fail with StopIteration instead of looping forever. + svc.get_status = MagicMock(side_effect=statuses) + plugin = BlueGreenPlugin(svc, Properties()) + plugin._provider_supplier = lambda *args: MagicMock() + return plugin def test_blue_green_log_message_keys_resolve(): @@ -25,3 +88,63 @@ def test_blue_green_log_message_keys_resolve(): "SuspendConnectRouting.WaitConnectUntilCorrespondingHostFound", "host-1") assert "host-1" in formatted assert Messages.get("BlueGreenStatusProvider.AllGreenHostsChangedName") + + +def test_routing_is_match_by_host_and_port(): + """Routing endpoints are "host" or "host:port"; host_info.url ends with "/" and must not be compared.""" + with_port = PassThroughConnectRouting("h:5432", BlueGreenRole.SOURCE) + assert with_port.is_match(HostInfo("h", 5432), BlueGreenRole.SOURCE) is True + assert with_port.is_match(HostInfo("H", 5432), BlueGreenRole.SOURCE) is True + assert with_port.is_match(HostInfo("h", 5433), BlueGreenRole.SOURCE) is False + assert with_port.is_match(HostInfo("h", 5432), BlueGreenRole.TARGET) is False + + host_only = PassThroughConnectRouting("h", BlueGreenRole.SOURCE) + assert host_only.is_match(HostInfo("h"), BlueGreenRole.SOURCE) is True + + assert PassThroughConnectRouting("h:5432/", BlueGreenRole.SOURCE).is_match( + HostInfo("h", 5432), BlueGreenRole.SOURCE) is False + + +def test_connect_reselects_from_latest_status_and_falls_through(): + """Once the published status has no matching routing, connect stops retrying the stale routing.""" + stale = _NoneConnectRouting() + status_active = _status(connect_routings=[stale]) + status_done = _status(phase=BlueGreenPhase.COMPLETED) + plugin = _plugin([status_active, status_done]) + + conn = MagicMock() + connect_func = MagicMock(return_value=conn) + result = plugin.connect(MagicMock(), MagicMock(), HostInfo("h", 5432), Properties(), False, connect_func) + + assert result is conn + assert stale.calls == 1 + connect_func.assert_called_once() + + +def test_connect_reselects_routing_from_latest_status(): + stale = _NoneConnectRouting() + status_active = _status(connect_routings=[stale]) + status_next = _status(phase=BlueGreenPhase.POST, connect_routings=[PassThroughConnectRouting(None, None)]) + plugin = _plugin([status_active, status_next]) + + conn = MagicMock() + connect_func = MagicMock(return_value=conn) + result = plugin.connect(MagicMock(), MagicMock(), HostInfo("h", 5432), Properties(), False, connect_func) + + assert result is conn + assert stale.calls == 1 + assert plugin._bg_status is status_next + + +def test_execute_reselects_from_latest_status_and_falls_through(): + stale = _EmptyExecuteRouting() + status_active = _status(execute_routings=[stale]) + status_done = _status(phase=BlueGreenPhase.COMPLETED) + plugin = _plugin([status_active, status_done], current_host_info=HostInfo("h", 5432)) + + execute_func = MagicMock(return_value="exec-ok") + result = plugin.execute(MagicMock(), "Cursor.execute", execute_func) + + assert result == "exec-ok" + assert stale.calls == 1 + execute_func.assert_called_once()