From 30e67166ca3bb3f62e97fa492d9f6f49c31bed02 Mon Sep 17 00:00:00 2001 From: Juan Lee Date: Fri, 13 Nov 2026 02:31:29 -0700 Subject: [PATCH 1/4] test: assert Blue/Green switchover calls resume and are routed The Blue/Green switchover tests now check what #1352 broke, inside the one switchover each lane runs. Probes start on the phase the plugin publishes. An execute on an open blue connection and a new connect to a green instance, both started in IN_PROGRESS, must return within 60 s, after the phase moved on, and use at most 1.0 s of their thread's CPU. The 60 s is twice bg_connect_timeout_ms, which the tests pin at its 30 s default. A new connection to the blue endpoint opened in POST (and in PREPARATION, when sampled) must reach the substitute host the plugin's status lists for it. A call still in flight after 60 s is reported as a spin or as blocked from a thread CPU sample, so a hung wrapper fails the test instead of hanging the suite. The plugin's logger is held at INFO during the test, and after it while any test thread is still running, so a spinning call can't flood the report with debug records. The switchover metrics are printed before the verdict, and an error while printing them can't hide it. The old test asserted nothing about the wrapper's calls or where new connections went, left hung threads running, and could not finish under the harness's 600 s pytest timeout: it waited 6 minutes and then slept 12. It now waits until the switchover completes, bounded from the request, then for the RDS API to report completion. Every thread is a daemon and the joins share one deadline. The services container is built on the main thread before the monitor threads start: conftest releases it and its lazy re-initialisation isn't locked, so their first connects could find no monitor service. If the switchover is never requested, the failure names the wait result and any unhandled exceptions. The direct topology monitors now poll with autocommit on, as the plugin's own status connections do: without it MySQL's REPEATABLE READ kept returning the first snapshot, so they never saw the switchover complete. The async twin gets the same changes, unwraps AsyncBlueGreenPlugin instead of the sync class, and adds an execute probe on the blue cluster endpoint, through the connection that owns the status provider (the async status doesn't list instance endpoints). That probe takes the provider out of the shutdown hooks every async worker drains when it ends, and stops it itself, and its summary line shows the role and execute routing the plugin had for its host. The harness waits for an in-flight switchover to settle before it picks the cluster to delete, and also deletes a deployment whose switchover failed. It asks RDS for the cluster parameter group family of the engine version instead of assuming aurora-postgresql16, so the PostgreSQL task works on newer default versions, and it deletes that group when the cluster create fails. Covers #1352. --- .../container/test_blue_green_deployment.py | 409 +++++++++++-- .../test_blue_green_deployment_async.py | 288 +++++++-- .../container/utils/blue_green_checks.py | 548 ++++++++++++++++++ .../integration/host/TestEnvironment.java | 19 + .../integration/util/AuroraTestUtility.java | 29 +- 5 files changed, 1212 insertions(+), 81 deletions(-) create mode 100644 tests/integration/container/utils/blue_green_checks.py diff --git a/tests/integration/container/test_blue_green_deployment.py b/tests/integration/container/test_blue_green_deployment.py index aab38443b..41893ba6b 100644 --- a/tests/integration/container/test_blue_green_deployment.py +++ b/tests/integration/container/test_blue_green_deployment.py @@ -29,8 +29,10 @@ import math import socket +import sys from collections import deque from dataclasses import dataclass, field +from logging import INFO, getLogger from threading import Event, Thread from time import perf_counter_ns, sleep @@ -39,9 +41,11 @@ from aws_advanced_python_wrapper import AwsWrapperConnection from aws_advanced_python_wrapper.blue_green_plugin import (BlueGreenPlugin, - BlueGreenRole) + BlueGreenRole, + BlueGreenStatus) from aws_advanced_python_wrapper.database_dialect import DialectCode from aws_advanced_python_wrapper.driver_info import DriverInfo +from aws_advanced_python_wrapper.utils import services_container from aws_advanced_python_wrapper.utils.atomic import AtomicInt from aws_advanced_python_wrapper.utils.concurrent import (ConcurrentDict, CountDownLatch) @@ -49,6 +53,20 @@ from aws_advanced_python_wrapper.utils.properties import (Properties, WrapperProperties) from aws_advanced_python_wrapper.utils.rds_utils import RdsUtils +from .utils.blue_green_checks import (BG_CONNECT_TIMEOUT_MS, BG_ID, + PLUGIN_STATUS_POLL_SEC, + PROBE_CONNECT_TIMEOUT_SEC, + ROUTING_PROBE_ATTEMPTS, + SWITCHOVER_TEST_TIMEOUT_SEC, + THREAD_JOIN_SEC, PluginStatusObservation, + ProbeResult, RoutingAttempt, + connect_suspend_listed, describe_error, + evaluate_routing, + evaluate_suspended_call, get_probe_host, + join_all, listed_substitute, + record_plugin_phase, settle_switchover, + summary_lines, wait_for_phase, + wait_for_switchover) from .utils.conditions import enable_on_deployments, enable_on_features from .utils.database_engine import DatabaseEngine from .utils.database_engine_deployment import DatabaseEngineDeployment @@ -88,7 +106,11 @@ def test_utility(self): def rds_utils(self): return RdsUtils() + # Worst case on any tree: setup + 150 s (latest request) + 300 s (API wait from the request) + 5 s (trigger join) + # + 2 s (CPU sample) + 20 s (joins), so setup + 477 s. The harness's --timeout=600 stays for every other test. + @pytest.mark.timeout(SWITCHOVER_TEST_TIMEOUT_SEC) def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: TestEnvironment, test_driver): + test_start_ns = perf_counter_ns() self.results.clear() self.unhandled_exceptions.clear() @@ -119,6 +141,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: if rds_utils.is_not_green_or_old_instance(host): threads.append(Thread( + daemon=True, target=self.direct_topology_monitor, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -126,6 +149,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.direct_blue_connectivity_monitor, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -133,6 +157,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.direct_blue_idle_connectivity_monitor, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -140,6 +165,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.wrapper_blue_idle_connectivity_monitor, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -147,6 +173,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.wrapper_blue_executing_connectivity_monitor, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -154,6 +181,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.wrapper_blue_new_connection_monitor, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -161,6 +189,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.blue_dns_monitor, args=(host_id, host, start_latch, stop, finish_latch, bg_results))) thread_count += 1 @@ -168,6 +197,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: if rds_utils.is_green_instance(host): threads.append(Thread( + daemon=True, target=self.direct_topology_monitor, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -175,6 +205,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.wrapper_green_connectivity_monitor, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -182,6 +213,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.green_dns_monitor, args=(host_id, host, start_latch, stop, finish_latch, bg_results))) thread_count += 1 @@ -191,6 +223,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: rds_client = test_utility.get_rds_client() threads.append(Thread( + daemon=True, target=self.green_iam_connectivity_monitor, args=(test_driver, conn_utils, rds_client, host_id, "BlueHostToken", rds_utils.remove_green_instance_prefix(host), host, test_instance.get_port(), @@ -200,6 +233,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.green_iam_connectivity_monitor, args=(test_driver, conn_utils, rds_client, host_id, "GreenHostToken", host, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, @@ -208,52 +242,150 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_count += 1 thread_finish_count += 1 - threads.append(Thread( + switchover_requested = Event() + switchover_requested_ns = AtomicInt(0) + trigger_thread = Thread( + daemon=True, target=self.bg_switchover_trigger, - args=(test_utility, info.get_bg_deployment_id(), start_latch, finish_latch, self.results))) + args=(test_utility, info.get_bg_deployment_id(), start_latch, finish_latch, self.results, stop, + switchover_requested, switchover_requested_ns)) + threads.append(trigger_thread) thread_count += 1 thread_finish_count += 1 + # #1352 probes. Not counted in the latches: each one starts on the plugin phase it needs. + port = test_instance.get_port() + blue_endpoint = test_environment.get_database_info().get_cluster_endpoint() \ + if test_environment.get_deployment() == DatabaseEngineDeployment.AURORA else test_instance.get_host() + green_endpoint = self.get_green_endpoint(test_environment, test_utility, info.get_bg_deployment_id()) + writer_host = test_instance.get_host() + blue_writer_ip = self.resolve_ip(blue_endpoint) + green_writer_ip = "unresolved" if green_endpoint is None else self.resolve_ip(green_endpoint) + green_host = next((host for host in topology_instances if rds_utils.is_green_instance(host)), None) + observation = PluginStatusObservation() + execute_probe = ProbeResult("WrapperBlueSuspendedExecute") + connect_probe = ProbeResult("WrapperGreenSuspendedConnect", probe_host=green_host or "") + post_probe = ProbeResult("WrapperBlueRouting POST") + preparation_probe = ProbeResult("WrapperBlueRouting PREPARATION") + probes = [execute_probe, connect_probe, post_probe, preparation_probe] + threads.append(Thread( + daemon=True, + target=self.plugin_status_monitor, + args=(observation, switchover_requested, stop))) + threads.append(Thread( + daemon=True, + target=self.wrapper_blue_suspended_execute_probe, + args=(test_driver, conn_utils, blue_endpoint, writer_host, port, db_name, observation, switchover_requested, + stop, execute_probe))) + threads.append(Thread( + daemon=True, + target=self.wrapper_green_suspended_connect_probe, + args=(test_driver, conn_utils, green_host, port, db_name, observation, stop, connect_probe))) + threads.append(Thread( + daemon=True, + target=self.wrapper_blue_routing_probe, + args=(test_driver, conn_utils, blue_endpoint, writer_host, port, db_name, observation, switchover_requested, + stop, preparation_probe, post_probe))) + start_latch.set_count(thread_count) finish_latch.set_count(thread_finish_count) for result in self.results.values(): result.start_time_ns.set(start_time_ns) - for thread in threads: - thread.start() - - self.logger.debug("All threads started.") - - finish_latch.wait_sec(6 * 60) - self.logger.debug("All threads completed.") - - sleep(12 * 60) - - self.logger.debug("Stopping all threads...") - stop.set() - - for thread in threads: - thread.join(timeout=30) - if thread.is_alive(): - self.logger.debug("Timed out waiting for a thread to stop running...") + # An unfixed wrapper's suspended calls log two debug records per pass of their loop; keep them out of the report. + plugin_logger = getLogger(BlueGreenPlugin.__module__) + plugin_log_level = plugin_logger.level + plugin_logger.setLevel(INFO) + switch_interval = sys.getswitchinterval() + threads_start_ns = perf_counter_ns() + wait_result = "not run" + try: + try: + # conftest released the services container and its lazy re-initialisation isn't locked: build it here, + # before the monitors' first connects race on it. + services_container.get_monitor_service() + for thread in threads: + thread.start() + + self.logger.debug("All threads started.") + wait_result = wait_for_switchover( + probes, observation, switchover_requested, switchover_requested_ns, threads_start_ns, + self.is_switchover_completed_seen) + self.logger.debug(f"Done waiting for the switchover: {wait_result}.") + finally: + settle_switchover( + test_utility, info.get_bg_deployment_id(), trigger_thread, stop, switchover_requested, + switchover_requested_ns) + self.logger.debug("Stop set for all threads.") + + alive_count = join_all(threads, THREAD_JOIN_SEC) + self.logger.debug(f"Done waiting for threads to stop, {alive_count} still running.") + + writer_ips = f"green writer IP {green_writer_ip}, blue writer IP {blue_writer_ip}" + not_sampled = [] if preparation_probe.phase_at_start == "PREPARATION" else [f"{preparation_probe.name}: not sampled"] + for line in summary_lines( + test_start_ns, threads_start_ns, switchover_requested_ns.get(), wait_result, observation, probes, + [writer_ips] + not_sampled): + print(line) + + for host_id, result in self.results.items(): + assert result.bg_trigger_time_ns.get() > 0, \ + (f"bg_trigger_time for {host_id} was {result.bg_trigger_time_ns.get()}, wait result: {wait_result}, " + f"unhandled exceptions: {', '.join(describe_error(e) for e in self.unhandled_exceptions) or 'none'}") + + self.logger.debug("Test is over.") + now_ns = perf_counter_ns() + post_first_seen_ns = observation.plugin_phase_first_seen_ns.get("POST") + failures = ( + evaluate_suspended_call("execute", execute_probe, now_ns, post_first_seen_ns) + + evaluate_suspended_call("connect", connect_probe, now_ns, post_first_seen_ns) + + evaluate_routing(post_probe, "POST", required=True, writer_ips=writer_ips) + + evaluate_routing(preparation_probe, "PREPARATION", required=False, writer_ips=writer_ips)) + # A monitor still appending to its results can make print_metrics raise; it must not hide the verdict. + try: + self.print_metrics(rds_utils) + except Exception as e: + print(f"print_metrics failed: {describe_error(e)}") + if failures: + pytest.fail("\n".join(failures)) - self.logger.debug("Done waiting for threads to stop.") + if len(self.unhandled_exceptions) > 0: + self.log_unhandled_exceptions() + pytest.fail("There were unhandled exceptions.") - for host_id, result in self.results.items(): - assert result.bg_trigger_time_ns.get() > 0, \ - f"bg_trigger_time for {host_id} was {result.bg_trigger_time_ns.get()}" + self.assert_test() - self.logger.debug("Test is over.") - self.print_metrics(rds_utils) + self.logger.debug("Completed") + finally: + # Any thread still alive may be an unfixed call spinning (the inherited monitors as well as the probes). + # Restoring the interval or the DEBUG level under it floods the output until the harness can't keep up. + # A lane runs one test. + if not any(probe.is_in_flight() for probe in probes) and not any(thread.is_alive() for thread in threads): + sys.setswitchinterval(switch_interval) + plugin_logger.setLevel(plugin_log_level) - if len(self.unhandled_exceptions) > 0: - self.log_unhandled_exceptions() - pytest.fail("There were unhandled exceptions.") + def is_switchover_completed_seen(self) -> bool: + return any(result.green_status_time.get("SWITCHOVER_COMPLETED", 0) for result in self.results.values()) - self.assert_test() + def resolve_ip(self, host: str) -> str: + try: + return socket.gethostbyname(host) + except Exception: + return "unresolved" - self.logger.debug("Completed") + def get_green_endpoint(self, test_env: TestEnvironment, test_utility: RdsTestUtility, bg_id: str) -> Optional[str]: + try: + bg_deployment = test_utility.get_blue_green_deployment(bg_id) + if bg_deployment is None: + return None + if test_env.get_deployment() == DatabaseEngineDeployment.RDS_MULTI_AZ_INSTANCE: + green_instance = test_utility.get_rds_instance_info_by_arn(bg_deployment["Target"]) + return None if green_instance is None else green_instance["Endpoint"]["Address"] + green_cluster = test_utility.get_cluster_by_arn(bg_deployment["Target"]) + return None if green_cluster is None else green_cluster["Endpoint"] + except Exception: + return None def get_bg_endpoints( self, @@ -348,10 +480,12 @@ def direct_topology_monitor( else: pytest.fail(f"Unsupported database engine: {engine}") + # Each poll must read the status table as it is now: without autocommit, MySQL's REPEATABLE READ + # repeats the first read's snapshot (the plugin sets autocommit on its status connections for this reason). + connect_params = {**conn_utils.get_connect_params(host=host, port=port, dbname=db), "autocommit": True} + try: - conn = self.get_direct_connection_with_retry( - test_driver, - **conn_utils.get_connect_params(host=host, port=port, dbname=db)) + conn = self.get_direct_connection_with_retry(test_driver, **connect_params) self.logger.debug(f"[DirectTopology] @ {host_id}] Connection opened.") sleep(1) @@ -366,8 +500,7 @@ def direct_topology_monitor( end_time_ns = perf_counter_ns() + 15 * 60 * 1_000_000_000 # 15 minutes while not stop.is_set() and perf_counter_ns() < end_time_ns: if conn is None: - conn = self.get_direct_connection_with_retry( - test_driver, **conn_utils.get_connect_params(host=host, port=port, dbname=db)) + conn = self.get_direct_connection_with_retry(test_driver, **connect_params) self.logger.debug(f"[DirectTopology] @ {host_id}] Connection re-opened.") try: @@ -605,6 +738,7 @@ def get_wrapper_connect_params(self, conn_utils: ConnectionUtils, host: str, por params = conn_utils.get_connect_params(host=host, port=port, dbname=db) params = {**params, **self.get_telemetry_params()} params[WrapperProperties.CLUSTER_ID.name] = self.TEST_CLUSTER_ID + params[WrapperProperties.BG_CONNECT_TIMEOUT_MS.name] = BG_CONNECT_TIMEOUT_MS test_env = TestEnvironment.get_current() engine = test_env.get_engine() db_deployment = test_env.get_deployment() @@ -787,6 +921,198 @@ def wrapper_blue_new_connection_monitor( finish_latch.count_down() self.logger.debug(f"[WrapperBlueNewConnection @ {host_id}] Thread is completed.") + # #1352 observer: the first time each plugin phase is published. Sync status storage is process-wide. + def plugin_status_monitor(self, observation: PluginStatusObservation, switchover_requested: Event, stop: Event) -> None: + while not stop.is_set(): + try: + status = self.read_plugin_status() + if status is not None: + record_plugin_phase(observation.plugin_phase_first_seen_ns, observation.phase_events, status) + if not switchover_requested.is_set(): + observation.pre_trigger_status = status + observation.status_seen.set() + except Exception as e: + observation.error = describe_error(e) + sleep(PLUGIN_STATUS_POLL_SEC) + + def read_plugin_status(self) -> Optional[BlueGreenStatus]: + return services_container.get_storage_service().get(BlueGreenStatus, BG_ID, register_data_access=False) + + def read_plugin_phase(self) -> Optional[str]: + try: + status = self.read_plugin_status() + except Exception: + return None + return None if status is None else status.phase.name + + # #1352: an execute that starts in IN_PROGRESS must return once the phase moves on, without a busy loop. + def wrapper_blue_suspended_execute_probe( + self, + test_driver: TestDriver, + conn_utils: ConnectionUtils, + blue_endpoint: str, + writer_host: str, + port: int, + db: str, + observation: PluginStatusObservation, + switchover_requested: Event, + stop: Event, + result: ProbeResult) -> None: + conn = None + try: + result.probe_host = get_probe_host(observation, blue_endpoint, writer_host, switchover_requested, stop) + target_driver_connect = DriverHelper.get_connect_func(test_driver) + conn = AwsWrapperConnection.connect( + target_driver_connect, **self.get_wrapper_connect_params(conn_utils, result.probe_host, port, db)) + bg_plugin: Optional[BlueGreenPlugin] = conn._unwrap(BlueGreenPlugin) + assert bg_plugin is not None, f"Unable to find blue/green plugin in wrapper connection for {result.probe_host}." + # cursor() goes through the plugin, so a cursor made inside the window would take the hold. + cursor = conn.cursor() + if not wait_for_phase(observation, "IN_PROGRESS", stop): + return + + status = self.read_plugin_status() + result.phase_at_start = None if status is None else status.phase.name + error: Optional[Exception] = None + result.begin() + try: + cursor.execute("SELECT 1") + except Exception as e: + error = e + result.hold_ns = bg_plugin.get_hold_time_ns() + result.phase_at_end = self.read_plugin_phase() + result.end(error) + try: + cursor.fetchall() + except Exception: + pass # the old blue may drop the connection at POST + except Exception as e: + result.outcome = result.outcome or describe_error(e) + finally: + result.done = True + self.close_connection(conn) + + # #1352: a connect to a green host that starts in IN_PROGRESS must end once the phase moves on, without a busy loop. + def wrapper_green_suspended_connect_probe( + self, + test_driver: TestDriver, + conn_utils: ConnectionUtils, + green_host: Optional[str], + port: int, + db: str, + observation: PluginStatusObservation, + stop: Event, + result: ProbeResult) -> None: + conn = None + try: + if green_host is None: + result.outcome = "no green instance in the topology" + return + params = self.get_wrapper_connect_params(conn_utils, green_host, port, db) + params[WrapperProperties.CONNECT_TIMEOUT_SEC.name] = PROBE_CONNECT_TIMEOUT_SEC + target_driver_connect = DriverHelper.get_connect_func(test_driver) + if not wait_for_phase(observation, "IN_PROGRESS", stop): + return + + status = self.read_plugin_status() + result.phase_at_start = None if status is None else status.phase.name + result.suspend_listed, result.suspend_detail = connect_suspend_listed(status, green_host) + error: Optional[Exception] = None + result.begin() + try: + conn = AwsWrapperConnection.connect(target_driver_connect, **params) + except Exception as e: + error = e + result.phase_at_end = self.read_plugin_phase() + result.end(error) + except Exception as e: + result.outcome = result.outcome or describe_error(e) + finally: + result.done = True + self.close_connection(conn) + + # #1352: a new connection to the probe host opened in PREPARATION, then in POST, + # must reach the substitute host the plugin's status lists for it. + def wrapper_blue_routing_probe( + self, + test_driver: TestDriver, + conn_utils: ConnectionUtils, + blue_endpoint: str, + writer_host: str, + port: int, + db: str, + observation: PluginStatusObservation, + switchover_requested: Event, + stop: Event, + preparation_result: ProbeResult, + post_result: ProbeResult) -> None: + try: + probe_host = get_probe_host(observation, blue_endpoint, writer_host, switchover_requested, stop) + preparation_result.probe_host = probe_host + post_result.probe_host = probe_host + params = self.get_wrapper_connect_params(conn_utils, probe_host, port, db) + params[WrapperProperties.CONNECT_TIMEOUT_SEC.name] = PROBE_CONNECT_TIMEOUT_SEC + target_driver_connect = DriverHelper.get_connect_func(test_driver) + if wait_for_phase(observation, "PREPARATION", stop): + self.run_routing_attempts(target_driver_connect, params, port, "PREPARATION", 1, preparation_result) + preparation_result.done = True + if wait_for_phase(observation, "POST", stop): + self.run_routing_attempts(target_driver_connect, params, port, "POST", ROUTING_PROBE_ATTEMPTS, post_result) + except Exception as e: + post_result.outcome = post_result.outcome or describe_error(e) + finally: + preparation_result.done = True + post_result.done = True + + def run_routing_attempts( + self, + target_driver_connect: Any, + params: Dict[str, Any], + port: int, + phase: str, + max_attempts: int, + result: ProbeResult) -> None: + status = self.read_plugin_status() + result.phase_at_start = None if status is None else status.phase.name + if result.phase_at_start != phase: + return + + result.start_ns = perf_counter_ns() + for _ in range(max_attempts): + # Re-read before every attempt: late POST drops the substitutes and connects by name. + status = self.read_plugin_status() + substitute = listed_substitute(status, result.probe_host, port) + attempt = RoutingAttempt("none" if status is None else status.phase.name, substitute) + result.attempts.append(attempt) + if substitute is None: + break + + conn = None + try: + conn = AwsWrapperConnection.connect(target_driver_connect, **params) + if listed_substitute(self.read_plugin_status(), result.probe_host, port) != substitute: + attempt.outcome = "status changed during the attempt" + else: + attempt.driver_host = self.get_driver_host(conn) + attempt.outcome = "ok" + except Exception as e: + attempt.outcome = describe_error(e) + finally: + self.close_connection(conn) + if attempt.outcome == "ok": + break + result.end_ns = perf_counter_ns() + + def get_driver_host(self, conn: AwsWrapperConnection) -> Optional[str]: + """The host the driver connected to (never hostaddr).""" + try: + target = conn.target_connection + if TestEnvironment.get_current().get_engine() == DatabaseEngine.MYSQL: + return str(target.server_host) + return str(target.info.host) + except Exception: + return None + def is_timeout_exception(self, exception: Exception) -> bool: error_message = str(exception).lower() timeout_keywords = [ @@ -1055,7 +1381,10 @@ def bg_switchover_trigger( bg_id: str, start_latch: CountDownLatch, finish_latch: CountDownLatch, - results: Dict[str, BlueGreenResults]): + results: Dict[str, BlueGreenResults], + stop: Event, + switchover_requested: Event, + switchover_requested_ns: AtomicInt): try: start_latch.count_down() @@ -1067,6 +1396,12 @@ def bg_switchover_trigger( result.threads_sync_time.set(sync_time_ns) sleep(30) + # The main thread sets stop when it gives up waiting for the request; it then waits for the API only if + # switchover_requested is set, so the check and the set stay adjacent. + if stop.is_set(): + return + switchover_requested_ns.set(perf_counter_ns()) + switchover_requested.set() test_utility.switchover_blue_green_deployment(bg_id) bg_trigger_time_ns = perf_counter_ns() diff --git a/tests/integration/container/test_blue_green_deployment_async.py b/tests/integration/container/test_blue_green_deployment_async.py index 6ec996d8d..6e5edb41a 100644 --- a/tests/integration/container/test_blue_green_deployment_async.py +++ b/tests/integration/container/test_blue_green_deployment_async.py @@ -35,7 +35,7 @@ - ``conn.close()`` → ``await conn.close()``. - ``is_closed`` for psycopg AsyncConnection uses the sync ``.closed`` property; for aiomysql uses ``not .open``. See ``_is_conn_closed_async``. -- ``conn._unwrap(BlueGreenPlugin)`` → ``_unwrap_plugin_async(conn, BlueGreenPlugin)`` +- ``conn._unwrap(BlueGreenPlugin)`` → ``_unwrap_plugin_async(conn, AsyncBlueGreenPlugin)`` (searches ``conn._plugin_manager._plugins`` directly because ``AsyncAwsWrapperConnection`` does not expose ``_unwrap``). - ``DriverHelper.get_connect_func`` returns the async connect callable for @@ -52,8 +52,10 @@ import asyncio import math import socket +import sys from collections import deque from dataclasses import dataclass, field +from logging import INFO, getLogger from threading import Event, Thread from time import perf_counter_ns, sleep from typing import (TYPE_CHECKING, Any, Callable, Deque, Dict, List, Optional, @@ -63,11 +65,16 @@ import pytest from tabulate import tabulate # type: ignore[import-untyped] +from aws_advanced_python_wrapper.aio.blue_green_plugin import \ + AsyncBlueGreenPlugin +from aws_advanced_python_wrapper.aio.blue_green_plugin import \ + BlueGreenStatus as AsyncBlueGreenStatus +from aws_advanced_python_wrapper.aio.cleanup import unregister_shutdown_hook from aws_advanced_python_wrapper.aio.wrapper import AsyncAwsWrapperConnection -from aws_advanced_python_wrapper.blue_green_plugin import (BlueGreenPlugin, - BlueGreenRole) +from aws_advanced_python_wrapper.blue_green_plugin import BlueGreenRole from aws_advanced_python_wrapper.database_dialect import DialectCode from aws_advanced_python_wrapper.driver_info import DriverInfo +from aws_advanced_python_wrapper.utils import services_container from aws_advanced_python_wrapper.utils.atomic import AtomicInt from aws_advanced_python_wrapper.utils.concurrent import (ConcurrentDict, CountDownLatch) @@ -76,6 +83,13 @@ from aws_advanced_python_wrapper.utils.rds_utils import RdsUtils from tests.integration.container.utils.async_connection_helpers import \ cleanup_async +from tests.integration.container.utils.blue_green_checks import ( + ASYNC_CONNECT_WAIT_SEC, ASYNC_EXECUTE_TIMEOUT_SEC, BG_CONNECT_TIMEOUT_MS, + MSG_ASYNC_NOT_CONNECTED, PLUGIN_STATUS_POLL_SEC, + SWITCHOVER_TEST_TIMEOUT_SEC, THREAD_JOIN_SEC, PluginStatusObservation, + ProbeResult, choose_probe_host, describe_error, evaluate_suspended_call, + execute_routing_detail, failure_message, join_all, record_plugin_phase, + settle_switchover, summary_lines, wait_for_switchover) from tests.integration.container.utils.conditions import ( enable_on_deployments, enable_on_features) from tests.integration.container.utils.database_engine import DatabaseEngine @@ -88,6 +102,8 @@ TestEnvironmentFeatures if TYPE_CHECKING: + from aws_advanced_python_wrapper.aio.blue_green_plugin import \ + AsyncBlueGreenStatusProvider from tests.integration.container.utils.connection_utils import \ ConnectionUtils from tests.integration.container.utils.test_driver import TestDriver @@ -261,10 +277,15 @@ def test_utility(self): def rds_utils(self): return RdsUtils() + # Worst case on any tree: setup + 30 s (async probe connect) + 150 s (latest request) + 300 s (API wait from the + # request) + 5 s (trigger join) + 2 s (CPU sample) + 20 s (joins), so setup + 507 s. The harness's --timeout=600 + # stays for every other test. + @pytest.mark.timeout(SWITCHOVER_TEST_TIMEOUT_SEC) def test_switchover_async( self, conn_utils: ConnectionUtils, test_utility: RdsTestUtility, rds_utils: RdsUtils, test_environment: TestEnvironment, test_driver: TestDriver): + test_start_ns = perf_counter_ns() self.results.clear() self.unhandled_exceptions.clear() @@ -295,6 +316,7 @@ def test_switchover_async( if rds_utils.is_not_green_or_old_instance(host): threads.append(Thread( + daemon=True, target=self.direct_topology_monitor_async, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -302,6 +324,7 @@ def test_switchover_async( thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.direct_blue_connectivity_monitor_async, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -309,6 +332,7 @@ def test_switchover_async( thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.direct_blue_idle_connectivity_monitor_async, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -316,6 +340,7 @@ def test_switchover_async( thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.wrapper_blue_idle_connectivity_monitor_async, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -323,6 +348,7 @@ def test_switchover_async( thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.wrapper_blue_executing_connectivity_monitor_async, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -330,6 +356,7 @@ def test_switchover_async( thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.wrapper_blue_new_connection_monitor_async, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -337,6 +364,7 @@ def test_switchover_async( thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.blue_dns_monitor, args=(host_id, host, start_latch, stop, finish_latch, bg_results))) thread_count += 1 @@ -344,6 +372,7 @@ def test_switchover_async( if rds_utils.is_green_instance(host): threads.append(Thread( + daemon=True, target=self.direct_topology_monitor_async, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -351,6 +380,7 @@ def test_switchover_async( thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.wrapper_green_connectivity_monitor_async, args=(test_driver, conn_utils, host_id, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, bg_results))) @@ -358,6 +388,7 @@ def test_switchover_async( thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.green_dns_monitor, args=(host_id, host, start_latch, stop, finish_latch, bg_results))) thread_count += 1 @@ -367,6 +398,7 @@ def test_switchover_async( rds_client = test_utility.get_rds_client() threads.append(Thread( + daemon=True, target=self.green_iam_connectivity_monitor_async, args=(test_driver, conn_utils, rds_client, host_id, "BlueHostToken", rds_utils.remove_green_instance_prefix(host), host, test_instance.get_port(), @@ -376,6 +408,7 @@ def test_switchover_async( thread_finish_count += 1 threads.append(Thread( + daemon=True, target=self.green_iam_connectivity_monitor_async, args=(test_driver, conn_utils, rds_client, host_id, "GreenHostToken", host, host, test_instance.get_port(), db_name, start_latch, stop, finish_latch, @@ -384,52 +417,119 @@ def test_switchover_async( thread_count += 1 thread_finish_count += 1 - threads.append(Thread( + switchover_requested = Event() + switchover_requested_ns = AtomicInt(0) + trigger_thread = Thread( + daemon=True, target=self.bg_switchover_trigger, - args=(test_utility, info.get_bg_deployment_id(), start_latch, finish_latch, self.results))) + args=(test_utility, info.get_bg_deployment_id(), start_latch, finish_latch, self.results, stop, + switchover_requested, switchover_requested_ns)) + threads.append(trigger_thread) thread_count += 1 thread_finish_count += 1 + # #1352 probe. Each AsyncAwsWrapperConnection keeps its own Blue/Green status store, so only the + # connection that owns the status provider sees the phases: the probe connects first and is also the observer. + blue_endpoint = test_environment.get_database_info().get_cluster_endpoint() \ + if test_environment.get_deployment() == DatabaseEngineDeployment.AURORA else test_instance.get_host() + probe_host, probe_host_choice = choose_probe_host(None, blue_endpoint, test_instance.get_host()) + observation = PluginStatusObservation() + execute_probe = ProbeResult("AsyncWrapperBlueSuspendedExecute") + probes = [execute_probe] + probe_connected = Event() + probe_thread = Thread( + daemon=True, + target=self.wrapper_blue_suspended_execute_probe_async, + args=(test_driver, conn_utils, probe_host, test_instance.get_port(), db_name, observation, + stop, probe_connected, execute_probe)) + start_latch.set_count(thread_count) finish_latch.set_count(thread_finish_count) for result in self.results.values(): result.start_time_ns.set(start_time_ns) - for thread in threads: - thread.start() - - self.logger.debug("All threads started.") - - finish_latch.wait_sec(6 * 60) - self.logger.debug("All threads completed.") - - sleep(12 * 60) - - self.logger.debug("Stopping all threads...") - stop.set() - - for thread in threads: - thread.join(timeout=30) - if thread.is_alive(): - self.logger.debug("Timed out waiting for a thread to stop running...") - - self.logger.debug("Done waiting for threads to stop.") + # An unfixed wrapper's suspended calls log two debug records per pass of their loop; keep them out of the report. + plugin_logger = getLogger(AsyncBlueGreenPlugin.__module__) + plugin_log_level = plugin_logger.level + plugin_logger.setLevel(INFO) + switch_interval = sys.getswitchinterval() + threads_start_ns = perf_counter_ns() + wait_result = "not run" + try: + # conftest released the services container and its lazy re-initialisation isn't locked: build it here, + # before the monitors' first connects race on it. + services_container.get_monitor_service() + probe_thread.start() + if not probe_connected.wait(ASYNC_CONNECT_WAIT_SEC) or not probe_thread.is_alive(): + stop.set() + for line in summary_lines( + test_start_ns, threads_start_ns, 0, "async probe not connected", observation, probes): + print(line) + pytest.fail(failure_message( + MSG_ASYNC_NOT_CONNECTED, + f"connected {probe_connected.is_set()}, alive {probe_thread.is_alive()}, outcome {execute_probe.outcome or '-'}")) + threads.append(probe_thread) - for host_id, result in self.results.items(): - assert result.bg_trigger_time_ns.get() > 0, \ - f"bg_trigger_time for {host_id} was {result.bg_trigger_time_ns.get()}" + try: + threads_start_ns = perf_counter_ns() + for thread in threads: + if thread is not probe_thread: + thread.start() + + self.logger.debug("All threads started.") + observation.probe_host = probe_host + observation.probe_host_choice = f"{probe_host_choice}, the async probe connects before any status" + wait_result = wait_for_switchover( + probes, observation, switchover_requested, switchover_requested_ns, threads_start_ns, + self.is_switchover_completed_seen) + self.logger.debug(f"Done waiting for the switchover: {wait_result}.") + finally: + settle_switchover( + test_utility, info.get_bg_deployment_id(), trigger_thread, stop, switchover_requested, + switchover_requested_ns) + self.logger.debug("Stop set for all threads.") + + alive_count = join_all(threads, THREAD_JOIN_SEC) + self.logger.debug(f"Done waiting for threads to stop, {alive_count} still running.") + + for line in summary_lines( + test_start_ns, threads_start_ns, switchover_requested_ns.get(), wait_result, observation, probes): + print(line) + + for host_id, result in self.results.items(): + assert result.bg_trigger_time_ns.get() > 0, \ + (f"bg_trigger_time for {host_id} was {result.bg_trigger_time_ns.get()}, wait result: {wait_result}, " + f"unhandled exceptions: {', '.join(describe_error(e) for e in self.unhandled_exceptions) or 'none'}") + + self.logger.debug("Test is over.") + failures = evaluate_suspended_call( + "async execute", execute_probe, perf_counter_ns(), observation.plugin_phase_first_seen_ns.get("POST")) + # A monitor still appending to its results can make print_metrics raise; it must not hide the verdict. + try: + self.print_metrics(rds_utils) + except Exception as e: + print(f"print_metrics failed: {describe_error(e)}") + if failures: + pytest.fail("\n".join(failures)) - self.logger.debug("Test is over.") - self.print_metrics(rds_utils) + if len(self.unhandled_exceptions) > 0: + self.log_unhandled_exceptions() + pytest.fail("There were unhandled exceptions.") - if len(self.unhandled_exceptions) > 0: - self.log_unhandled_exceptions() - pytest.fail("There were unhandled exceptions.") + self.assert_test() - self.assert_test() + self.logger.debug("Completed") + finally: + # Any thread still alive may be an unfixed call spinning (the inherited monitors as well as the probes). + # Restoring the interval or the DEBUG level under it floods the output until the harness can't keep up. + # A lane runs one test. + if not any(probe.is_in_flight() for probe in probes) and not any(thread.is_alive() for thread in threads): + sys.setswitchinterval(switch_interval) + plugin_logger.setLevel(plugin_log_level) - self.logger.debug("Completed") + def is_switchover_completed_seen(self) -> bool: + return any(result.green_status_time.get("SWITCHOVER_COMPLETED", 0) for result in self.results.values()) def get_bg_endpoints( self, @@ -571,6 +671,7 @@ def get_wrapper_connect_params( params = conn_utils.get_connect_params(host=host, port=port, dbname=db) params = {**params, **self.get_telemetry_params()} params[WrapperProperties.CLUSTER_ID.name] = self.TEST_CLUSTER_ID + params[WrapperProperties.BG_CONNECT_TIMEOUT_MS.name] = BG_CONNECT_TIMEOUT_MS test_env = TestEnvironment.get_current() engine = test_env.get_engine() db_deployment = test_env.get_deployment() @@ -668,11 +769,14 @@ def _log_and_return_time(_: Any) -> int: else: results.blue_status_time.compute_if_absent(status, _log_and_return_time) + # Each poll must read the status table as it is now: without autocommit, MySQL's REPEATABLE READ + # repeats the first read's snapshot (the plugin sets autocommit on its status connections for this reason). + connect_params = {**conn_utils.get_connect_params(host=host, port=port, dbname=db), "autocommit": True} + def _body(loop: asyncio.AbstractEventLoop) -> None: conn: Any = None try: - conn = loop.run_until_complete(_open_direct_conn_async( - test_driver, conn_utils.get_connect_params(host=host, port=port, dbname=db))) + conn = loop.run_until_complete(_open_direct_conn_async(test_driver, connect_params)) self.logger.debug(f"[AsyncDirectTopology @ {host_id}] Connection opened.") sleep(1) @@ -684,8 +788,7 @@ def _body(loop: asyncio.AbstractEventLoop) -> None: while not stop.is_set() and perf_counter_ns() < end_time_ns: if conn is None: try: - conn = loop.run_until_complete(_open_direct_conn_async( - test_driver, conn_utils.get_connect_params(host=host, port=port, dbname=db))) + conn = loop.run_until_complete(_open_direct_conn_async(test_driver, connect_params)) self.logger.debug(f"[AsyncDirectTopology @ {host_id}] Connection re-opened.") except Exception: sleep(0.1) @@ -912,7 +1015,7 @@ def _body(loop: asyncio.AbstractEventLoop) -> None: conn: Optional[AsyncAwsWrapperConnection] = None try: conn = loop.run_until_complete(_open()) - bg_plugin: Optional[BlueGreenPlugin] = _unwrap_plugin_async(conn, BlueGreenPlugin) + bg_plugin: Optional[AsyncBlueGreenPlugin] = _unwrap_plugin_async(conn, AsyncBlueGreenPlugin) assert bg_plugin is not None, \ f"Unable to find blue/green plugin in wrapper connection for {host}." self.logger.debug(f"[AsyncWrapperBlueExecute @ {host_id}] Connection opened.") @@ -982,7 +1085,7 @@ def _body(loop: asyncio.AbstractEventLoop) -> None: try: conn = loop.run_until_complete(_open()) end_time_ns = perf_counter_ns() - bg_plugin: Optional[BlueGreenPlugin] = _unwrap_plugin_async(conn, BlueGreenPlugin) + bg_plugin: Optional[AsyncBlueGreenPlugin] = _unwrap_plugin_async(conn, AsyncBlueGreenPlugin) assert bg_plugin is not None, \ f"Unable to find blue/green plugin in wrapper connection for {host}." @@ -998,7 +1101,7 @@ def _body(loop: asyncio.AbstractEventLoop) -> None: end_time_ns = perf_counter_ns() if conn is not None: - bg_plugin = _unwrap_plugin_async(conn, BlueGreenPlugin) + bg_plugin = _unwrap_plugin_async(conn, AsyncBlueGreenPlugin) assert bg_plugin is not None, \ f"Unable to find blue/green plugin in wrapper connection for {host}." results.blue_wrapper_connect_times.append( @@ -1025,6 +1128,96 @@ def _body(loop: asyncio.AbstractEventLoop) -> None: finish_latch.count_down() self.logger.debug(f"[AsyncWrapperBlueNewConnection @ {host_id}] Thread is completed.") + # #1352: an async execute that starts in IN_PROGRESS must return once the phase moves on. One coroutine runs + # for the thread's whole life, so the status provider's tasks keep running on this loop. + def wrapper_blue_suspended_execute_probe_async( + self, + test_driver: TestDriver, + conn_utils: ConnectionUtils, + host: str, + port: int, + db: str, + observation: PluginStatusObservation, + stop: Event, + connected: Event, + result: ProbeResult) -> None: + result.probe_host = host + connect_params = self.get_wrapper_connect_params(conn_utils, host, port, db) + + def _read_phase(bg_plugin: AsyncBlueGreenPlugin) -> Optional[str]: + try: + status = bg_plugin._plugin_service.get_status(AsyncBlueGreenStatus, bg_plugin._bg_id) + except Exception: + return None + return None if status is None else status.phase.name + + async def _execute_in_progress(cursor: Any, bg_plugin: AsyncBlueGreenPlugin) -> None: + result.phase_at_start = "IN_PROGRESS" + error: Optional[Exception] = None + result.begin() + try: + # A blocking regression would never yield to this bound. + await asyncio.wait_for(cursor.execute("SELECT 1"), ASYNC_EXECUTE_TIMEOUT_SEC) + except Exception as e: + error = e + result.hold_ns = bg_plugin.get_hold_time_ns() + result.phase_at_end = _read_phase(bg_plugin) + result.end(error) + try: + await cursor.fetchall() + except Exception: + pass # the old blue may drop the connection at POST + + async def _main() -> None: + conn = await _open_wrapper_conn_async(test_driver, connect_params) + provider: Optional[AsyncBlueGreenStatusProvider] = None + try: + bg_plugin: Optional[AsyncBlueGreenPlugin] = _unwrap_plugin_async(conn, AsyncBlueGreenPlugin) + assert bg_plugin is not None, f"Unable to find blue/green plugin in wrapper connection for {host}." + # cursor() is sync and lazy. The warm-up resolves the driver cursor, so the execute in the window is a + # single plugin pass and its hold time isn't reset. + cursor = conn.cursor() + await cursor.execute("SELECT 1") + await cursor.fetchall() + # Every async worker drains the process-wide shutdown hooks when it ends, and several end on their + # first lost connection, mid-switchover. Take this connection's status provider out of that drain + # before the other threads start, and stop it here instead. + provider = AsyncBlueGreenPlugin._status_providers.get(bg_plugin._bg_id) + if provider is not None: + unregister_shutdown_hook(provider.stop) + connected.set() + + executed = False + while not stop.is_set(): + status = bg_plugin._plugin_service.get_status(AsyncBlueGreenStatus, bg_plugin._bg_id) + phase = record_plugin_phase(observation.plugin_phase_first_seen_ns, observation.phase_events, status) + if phase == "IN_PROGRESS" and not executed: + executed = True + # Diagnostic only: shows which branch of the plugin's execute a call that isn't held took. + result.routing_detail = execute_routing_detail(status, bg_plugin._plugin_service) + await _execute_in_progress(cursor, bg_plugin) + elif phase in ("POST", "COMPLETED"): + executed = True # IN_PROGRESS was missed; the check reports not run + await asyncio.sleep(PLUGIN_STATUS_POLL_SEC) + finally: + if provider is not None: + observation.provider_monitors_running = any(monitor.is_running() for monitor in provider._monitors) + try: + await provider.stop() + except Exception: + pass + await _close_conn_async(conn) + + def _body(loop: asyncio.AbstractEventLoop) -> None: + loop.run_until_complete(_main()) + + try: + _run_worker_with_loop(_body) + except Exception as e: + result.outcome = result.outcome or describe_error(e) + finally: + result.done = True + # Blue / Green DNS monitors are purely sync (socket calls); reuse verbatim. def blue_dns_monitor( @@ -1092,7 +1285,7 @@ def _body(loop: asyncio.AbstractEventLoop) -> None: conn = loop.run_until_complete(_open()) self.logger.debug(f"[AsyncWrapperGreenConnectivity @ {host_id}] Connection opened.") - bg_plugin: Optional[BlueGreenPlugin] = _unwrap_plugin_async(conn, BlueGreenPlugin) + bg_plugin: Optional[AsyncBlueGreenPlugin] = _unwrap_plugin_async(conn, AsyncBlueGreenPlugin) assert bg_plugin is not None, \ f"Unable to find blue/green plugin in wrapper connection for {host}." @@ -1273,7 +1466,10 @@ def bg_switchover_trigger( bg_id: str, start_latch: CountDownLatch, finish_latch: CountDownLatch, - results: Any) -> None: + results: Any, + stop: Event, + switchover_requested: Event, + switchover_requested_ns: AtomicInt) -> None: """Trigger BG switchover using RDS API (synchronous boto3 calls).""" try: start_latch.count_down() @@ -1284,6 +1480,12 @@ def bg_switchover_trigger( result.threads_sync_time.set(sync_time_ns) sleep(30) + # The main thread sets stop when it gives up waiting for the request; it then waits for the API only if + # switchover_requested is set, so the check and the set stay adjacent. + if stop.is_set(): + return + switchover_requested_ns.set(perf_counter_ns()) + switchover_requested.set() test_utility.switchover_blue_green_deployment(bg_id) bg_trigger_time_ns = perf_counter_ns() diff --git a/tests/integration/container/utils/blue_green_checks.py b/tests/integration/container/utils/blue_green_checks.py new file mode 100644 index 000000000..be3ede751 --- /dev/null +++ b/tests/integration/container/utils/blue_green_checks.py @@ -0,0 +1,548 @@ +# 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. + +"""Checks shared by the sync and async Blue/Green switchover tests. + +The switchover tests run probes that hit the plugin's PREPARATION, IN_PROGRESS +and POST phases and then judge them here. A suspended call must return once the +phase moves on, without a busy loop, and a new connection to a blue host must go +to the substitute host the plugin's status lists for it. This module holds the +pure logic, so it imports no database driver. +""" + +from __future__ import annotations + +import os +import sys +from dataclasses import dataclass, field +from threading import Event, Lock, get_ident, get_native_id +from time import perf_counter_ns, sleep, thread_time +from typing import (TYPE_CHECKING, Any, Callable, Dict, List, Optional, + Sequence, Tuple) + +from aws_advanced_python_wrapper.blue_green_plugin import ( + BlueGreenRole, SubstituteConnectRouting, SuspendConnectRouting) +from aws_advanced_python_wrapper.hostinfo import HostInfo +from aws_advanced_python_wrapper.utils.properties import WrapperProperties + +if TYPE_CHECKING: + from threading import Thread + + from aws_advanced_python_wrapper.utils.atomic import AtomicInt + +# No bg_id is set in the test's connect params, so the plugin keys its status by the property default, normalized as +# BlueGreenPlugin.__init__ does. +BG_ID = str(WrapperProperties.BG_ID.default_value).strip().lower() +# Pinned in the switchover tests' connect params, at its default. +BG_CONNECT_TIMEOUT_MS = 30_000 +# Twice bg_connect_timeout_ms (pinned to 30 s in the connect params), which caps a correct suspend. Same 60 s as JDBC's +# MAX_TRAFFIC_SUSPENSION_MS (bgSwitchoverTimeoutMs / 3), derived differently. +MAX_TRAFFIC_SUSPENSION_NS = 2 * BG_CONNECT_TIMEOUT_MS * 1_000_000 +# A correct suspend waits on the status condition in 50 ms steps; the unfixed loop burned about a whole core. +MAX_SUSPENDED_CALL_CPU_SEC = 1.0 +# Thread CPU sample taken on a call that is still in flight past MAX_TRAFFIC_SUSPENSION_NS. +SPIN_SAMPLE_SEC = 2.0 +# Past the main thread's sample of a stuck call: the 60 s bound, a 1 s wait tick and the 2 s CPU sample, with headroom. +ASYNC_EXECUTE_TIMEOUT_SEC = MAX_TRAFFIC_SUSPENSION_NS // 1_000_000_000 + 15 +# A spinning thread shares the GIL with the other spinners and still gets far more than this in 2 s; a blocked read gets close to 0. +SPIN_MIN_CPU_SEC = 0.05 +# The plugin publishes each phase for at least 1.7 s (measured), so a 20 ms poll sees every phase. +PLUGIN_STATUS_POLL_SEC = 0.02 +# Poll step for probes waiting on a phase, and the main thread's wait tick. +PROBE_WAIT_STEP_SEC = 0.5 +WAIT_TICK_SEC = 1.0 +# Connect timeout of each routing and connect probe attempt; POST lasts 23 to 26 s (measured). +PROBE_CONNECT_TIMEOUT_SEC = 10 +ROUTING_PROBE_ATTEMPTS = 3 +# The async probe must connect, and own the status provider, before any other thread starts. +ASYNC_CONNECT_WAIT_SEC = 30 +# From thread start: the trigger waits for the start latch and then 30 s before it requests the switchover. +TRIGGER_WAIT_SEC = 150 +# From the switchover request: the plugin reached COMPLETED 41 to 45 s after it (measured). +SWITCHOVER_WAIT_SEC = 240 +# From the switchover request: the RDS API reported SWITCHOVER_COMPLETED after 68 to 73 s (measured). +BG_API_WAIT_SEC = 300 +BG_API_POLL_SEC = 5.0 +TRIGGER_JOIN_SEC = 5 +# One deadline shared by every join. +THREAD_JOIN_SEC = 20 +# See the worst case on the switchover tests' marker. +SWITCHOVER_TEST_TIMEOUT_SEC = 900 +# Lets the main thread get the GIL often while stuck probes spin. +MIN_SWITCH_INTERVAL_SEC = 0.0005 + +PHASE_ORDER = ("PREPARATION", "IN_PROGRESS", "POST", "COMPLETED") + +MSG_SPIN_EXECUTE = "#1352 spin - sync execute suspended in IN_PROGRESS did not return within 60 s" +MSG_SPIN_CONNECT = "#1352 spin - sync connect suspended in IN_PROGRESS did not return within 60 s" +MSG_SPIN_ASYNC_EXECUTE = "#1352 spin - async execute suspended in IN_PROGRESS did not return within 60 s" +MSG_BUSY_LOOP = "#1352 busy loop - a suspended call used more than 1.0 s of its thread CPU" +MSG_ROUTING_POST = "#1352 routing - connection opened in POST went to the dialled host, not the listed substitute" +MSG_ROUTING_PREPARATION = "#1352 routing - connection opened in PREPARATION went to the dialled host, not the listed substitute" +MSG_BLOCKED = "#1352 check inconclusive - a suspended probe is blocked, not spinning" +MSG_UNEXPECTED_HOST = "#1352 check inconclusive - a probe connection went to an unexpected host" +MSG_EXECUTE_NOT_IN_PROGRESS = "#1352 check not run - the execute probe did not start in IN_PROGRESS" +MSG_EXECUTE_NOT_HELD = "#1352 check not run - the execute probe call was not held by the plugin" +MSG_ASYNC_EXECUTE_NOT_IN_PROGRESS = "#1352 check not run - the async execute probe did not start in IN_PROGRESS" +MSG_ASYNC_EXECUTE_NOT_HELD = "#1352 check not run - the async execute probe call was not held by the plugin" +MSG_CONNECT_NOT_IN_PROGRESS = "#1352 check not run - the connect probe did not start in IN_PROGRESS" +MSG_CONNECT_NOT_SUSPENDED = "#1352 check not run - the connect probe was not suspended by the plugin" +MSG_ROUTING_NO_SUBSTITUTE = "#1352 check not run - the routing probe saw no POST status with a substitute routing for its host" +MSG_ROUTING_NO_CONNECTION = "#1352 check not run - the routing probe opened no connection while a substitute was listed" +MSG_DRIVER_HOST_UNREADABLE = "#1352 check not run - the driver host of a probe connection could not be read" +MSG_ASYNC_NOT_CONNECTED = "#1352 check not run - the async execute probe did not connect before the other threads" + +# label -> (spin, not started in IN_PROGRESS, not held); connect has no hold. +_SUSPENDED_CALL_MESSAGES: Dict[str, Tuple[str, str, Optional[str]]] = { + "execute": (MSG_SPIN_EXECUTE, MSG_EXECUTE_NOT_IN_PROGRESS, MSG_EXECUTE_NOT_HELD), + "connect": (MSG_SPIN_CONNECT, MSG_CONNECT_NOT_IN_PROGRESS, None), + "async execute": (MSG_SPIN_ASYNC_EXECUTE, MSG_ASYNC_EXECUTE_NOT_IN_PROGRESS, MSG_ASYNC_EXECUTE_NOT_HELD), +} +_ROUTING_MESSAGES: Dict[str, str] = {"POST": MSG_ROUTING_POST, "PREPARATION": MSG_ROUTING_PREPARATION} + + +@dataclass +class RoutingAttempt: + phase: str + substitute_host: Optional[str] # None when the status listed no substitute for the probe host + driver_host: Optional[str] = None + outcome: str = "" + + +@dataclass +class ProbeResult: + name: str + probe_host: str = "" + phase_at_start: Optional[str] = None + # The plugin phase read right after a suspended call returned. + phase_at_end: Optional[str] = None + start_ns: int = 0 + end_ns: int = 0 # 0 while the call is in flight + hold_ns: int = 0 + cpu_sec: float = 0.0 + outcome: str = "" # "ok", or the exception class and the start of its message + suspend_listed: bool = False + suspend_detail: str = "" + # For the summary only: how the plugin's execute would route this host under the status that started the call. + routing_detail: str = "" + attempts: List[RoutingAttempt] = field(default_factory=list) + native_id: Optional[int] = None + ident: Optional[int] = None + done: bool = False + # Filled by the main thread when the call is still in flight. + spinning: Optional[bool] = None + spin_cpu_delta_sec: Optional[float] = None + frames: Tuple[str, str] = ("none", "none") + _cpu_start_sec: float = field(default=0.0, repr=False) + + def begin(self) -> None: + """Called on the probe's own thread right before the call.""" + self.native_id = get_native_id() + self.ident = get_ident() + self._cpu_start_sec = thread_time() + self.start_ns = perf_counter_ns() + + def end(self, error: Optional[BaseException]) -> None: + """Called on the probe's own thread right after the call.""" + self.cpu_sec = thread_time() - self._cpu_start_sec + self.outcome = "ok" if error is None else describe_error(error) + self.end_ns = perf_counter_ns() + + def in_flight_ns(self, now_ns: int) -> int: + if self.start_ns == 0: + return 0 + return (self.end_ns if self.end_ns > 0 else now_ns) - self.start_ns + + def is_in_flight(self) -> bool: + return self.start_ns > 0 and self.end_ns == 0 + + def is_stuck(self, now_ns: int) -> bool: + return self.is_in_flight() and now_ns - self.start_ns > MAX_TRAFFIC_SUSPENSION_NS + + +@dataclass +class PluginStatusObservation: + """What the plugin published, as the test's observer saw it.""" + plugin_phase_first_seen_ns: Dict[str, int] = field(default_factory=dict) + phase_events: Dict[str, Event] = field(default_factory=lambda: {phase: Event() for phase in PHASE_ORDER}) + status_seen: Event = field(default_factory=Event) + pre_trigger_status: Any = None + error: str = "" + probe_host: Optional[str] = None + probe_host_choice: str = "" + # Async only: whether the status provider's monitors were still running at the end of the window. + provider_monitors_running: Optional[bool] = None + lock: Lock = field(default_factory=Lock) + + +def describe_error(error: BaseException) -> str: + return f"{type(error).__name__}: {str(error)[:200]}" + + +def _clean(text: str) -> str: + return text.replace(";", ",").replace("|", "/").replace("\n", " ") + + +def failure_message(text: str, detail: str) -> str: + return f"{text} ({_clean(detail)})" + + +def _sec(ns: int) -> str: + return f"{ns / 1_000_000_000:.2f} s" + + +def record_plugin_phase( + phase_first_seen_ns: Dict[str, int], + phase_events: Dict[str, Event], + status: Any) -> Optional[str]: + """Record the first time a phase is seen; works for the sync and the aio status classes.""" + if status is None: + return None + name: str = status.phase.name + if name not in phase_first_seen_ns: + phase_first_seen_ns[name] = perf_counter_ns() + event = phase_events.get(name) + if event is not None: + event.set() + return name + + +def wait_for_phase(observation: PluginStatusObservation, phase: str, stop: Event) -> bool: + """Wait for the phase; False on stop or when a later phase was seen without it.""" + later = PHASE_ORDER[PHASE_ORDER.index(phase) + 1:] + while not stop.is_set(): + if observation.phase_events[phase].is_set(): + return True + if any(observation.phase_events[p].is_set() for p in later): + return False + observation.phase_events[phase].wait(PROBE_WAIT_STEP_SEC) + return False + + +def choose_probe_host(status: Any, cluster_endpoint: str, writer_host: str) -> Tuple[str, str]: + if status is None: + return cluster_endpoint, "no pre-trigger status" + if status.get_role(HostInfo(host=cluster_endpoint)) is not None: + return cluster_endpoint, "cluster endpoint" + return writer_host, "writer instance" + + +def get_probe_host( + observation: PluginStatusObservation, + cluster_endpoint: str, + writer_host: str, + switchover_requested: Event, + stop: Event) -> str: + """Wait for the first published status (or the request), then choose the probe host once for every probe.""" + while not (observation.status_seen.is_set() or switchover_requested.is_set() or stop.is_set()): + observation.status_seen.wait(PROBE_WAIT_STEP_SEC) + with observation.lock: + if observation.probe_host is None: + observation.probe_host, observation.probe_host_choice = \ + choose_probe_host(observation.pre_trigger_status, cluster_endpoint, writer_host) + return observation.probe_host + + +def listed_substitute(status: Any, probe_host: str, port: int) -> Optional[str]: + """The substitute host the status lists for the probe host. Reads the routings; never calls is_match, the code under test.""" + if status is None: + return None + host = probe_host.lower() + endpoints = (host, f"{host}:{port}") + for routing in status.connect_routings: + if isinstance(routing, SubstituteConnectRouting) and routing._endpoint is not None \ + and routing._endpoint.lower() in endpoints: + return routing._substitute_host_info.host + return None + + +def connect_suspend_listed(status: Any, host: str) -> Tuple[bool, str]: + if status is None: + return False, "no status" + role = status.get_role(HostInfo(host=host)) + if role != BlueGreenRole.TARGET: + return False, f"role of the connect probe host was {role}" + for routing in status.connect_routings: + if isinstance(routing, SuspendConnectRouting) and routing._endpoint is None \ + and routing._bg_role == BlueGreenRole.TARGET: + return True, "SuspendConnectRouting(None, TARGET) listed" + return False, "no SuspendConnectRouting(None, TARGET) listed" + + +def execute_routing_detail(status: Any, plugin_service: Any) -> str: + """The current host's role and first matching execute routing, as the plugin's execute picks them. Summary only.""" + try: + if status is None: + return "routing: no status" + host_info = plugin_service.current_host_info + role = status.get_role(host_info) + routing = None if role is None else next( + (r for r in status.execute_routings if r.is_match(host_info, role)), None) + host = None if host_info is None else host_info.get_host_and_port() + return (f"current host {host}, role {role}, first matching execute routing {routing}, " + f"roles by endpoint {sorted(status.roles_by_endpoint.keys())}") + except Exception as e: + return f"routing: {describe_error(e)}" + + +def thread_cpu_sec(native_id: Optional[int]) -> Optional[float]: + """utime + stime of one thread of this process (Linux); None where /proc is missing.""" + if native_id is None: + return None + try: + with open(f"/proc/self/task/{native_id}/stat") as stat_file: + stat = stat_file.read() + fields = stat[stat.rindex(")") + 2:].split() + # Fields after the command name start at field 3 (state); utime and stime are fields 14 and 15. + return (int(fields[11]) + int(fields[12])) / os.sysconf("SC_CLK_TCK") + except (OSError, ValueError, IndexError): + return None + + +def stuck_frames(ident: Optional[int]) -> Tuple[str, str]: + """The innermost frame of a thread and its innermost frame in the wrapper, as file:function (never arguments).""" + frame = None if ident is None else sys._current_frames().get(ident) + if frame is None: + return "none", "none" + innermost = f"{os.path.basename(frame.f_code.co_filename)}:{frame.f_code.co_name}" + wrapper_frame = "none" + marker = f"aws_advanced_python_wrapper{os.sep}" + current: Any = frame + while current is not None: + filename = current.f_code.co_filename + if marker in filename: + wrapper_frame = f"{filename[filename.rindex(marker):]}:{current.f_code.co_name}" + break + current = current.f_back + return innermost, wrapper_frame + + +def is_spinning(cpu_before: Optional[float], cpu_after: Optional[float]) -> bool: + if cpu_before is None or cpu_after is None: + return True + return cpu_after - cpu_before >= SPIN_MIN_CPU_SEC + + +def sample_in_flight_probes(probes: Sequence[ProbeResult]) -> None: + """Sample the thread CPU of in-flight probes once over SPIN_SAMPLE_SEC, and lower the switch interval first.""" + if not probes: + return + sys.setswitchinterval(MIN_SWITCH_INTERVAL_SEC) + before = [thread_cpu_sec(probe.native_id) for probe in probes] + sleep(SPIN_SAMPLE_SEC) + for probe, cpu_before in zip(probes, before): + cpu_after = thread_cpu_sec(probe.native_id) + probe.spinning = is_spinning(cpu_before, cpu_after) + probe.spin_cpu_delta_sec = None if cpu_before is None or cpu_after is None else cpu_after - cpu_before + probe.frames = stuck_frames(probe.ident) + + +def _is_settled(probe: ProbeResult, now_ns: int, completed: bool) -> bool: + return probe.done or probe.end_ns > 0 or probe.is_stuck(now_ns) or (probe.start_ns == 0 and completed) + + +def wait_for_switchover( + probes: Sequence[ProbeResult], + observation: PluginStatusObservation, + switchover_requested: Event, + switchover_requested_ns: AtomicInt, + threads_start_ns: int, + completed_seen: Callable[[], bool]) -> str: + """Wait until every probe settled and the switchover completed (or a probe is stuck), within the stated bounds.""" + sampled: List[str] = [] + while True: + now_ns = perf_counter_ns() + requested = switchover_requested.is_set() + if requested and now_ns >= switchover_requested_ns.get() + SWITCHOVER_WAIT_SEC * 1_000_000_000: + reason = f"bound reached: switchover request + {SWITCHOVER_WAIT_SEC} s" + break + if not requested and now_ns >= threads_start_ns + TRIGGER_WAIT_SEC * 1_000_000_000: + reason = f"bound reached: no switchover request {TRIGGER_WAIT_SEC} s after thread start" + break + + newly_stuck = [p for p in probes if p.is_stuck(now_ns) and p.name not in sampled] + if newly_stuck: + sample_in_flight_probes(newly_stuck) + sampled.extend(p.name for p in newly_stuck) + continue + + completed = observation.phase_events["COMPLETED"].is_set() or completed_seen() + if requested and all(_is_settled(p, now_ns, completed) for p in probes) \ + and (completed or any(p.is_stuck(now_ns) for p in probes)): + return "probes settled" + (", switchover completed" if completed else ", a probe is stuck") + sleep(WAIT_TICK_SEC) + + sample_in_flight_probes([p for p in probes if p.is_in_flight() and p.name not in sampled]) + return reason + + +def evaluate_suspended_call( + label: str, + result: ProbeResult, + now_ns: int, + post_first_seen_ns: Optional[int]) -> List[str]: + spin_message, not_run_message, not_held_message = _SUSPENDED_CALL_MESSAGES[label] + # A probe sampled past the bound as blocked gets the inconclusive text; an unsampled or spinning one the defect's. + stuck_message = MSG_BLOCKED if result.spinning is False else spin_message + if result.phase_at_start != "IN_PROGRESS": + return [failure_message(not_run_message, f"{result.name} phase at start {result.phase_at_start}, outcome {result.outcome or '-'}")] + if label == "connect" and not result.suspend_listed: + return [failure_message(MSG_CONNECT_NOT_SUSPENDED, f"{result.name} {result.suspend_detail}")] + + if result.is_in_flight(): + cpu_delta = "unread" if result.spin_cpu_delta_sec is None else f"{result.spin_cpu_delta_sec:.3f} s" + detail = (f"{result.name} on {result.probe_host} in flight {_sec(result.in_flight_ns(now_ns))}, " + f"CPU over {SPIN_SAMPLE_SEC} s sample {cpu_delta}, " + f"innermost frame {result.frames[0]}, wrapper frame {result.frames[1]}") + return [failure_message(stuck_message, detail)] + + returned = f"{result.name} returned after {_sec(result.in_flight_ns(now_ns))} with {result.outcome}" + if result.in_flight_ns(now_ns) > MAX_TRAFFIC_SUSPENSION_NS or result.hold_ns > MAX_TRAFFIC_SUSPENSION_NS: + return [failure_message(stuck_message, f"{returned}, hold {_sec(result.hold_ns)}, over the 60 s bound")] + if result.phase_at_end == "IN_PROGRESS": + # Released by the plugin's suspend timer, not by the phase change. + return [failure_message(not_run_message, f"{returned}, plugin phase still IN_PROGRESS when it returned")] + if not_held_message is not None and result.hold_ns == 0: + return [failure_message(not_held_message, returned)] + if label == "connect" and (post_first_seen_ns is None or result.end_ns < post_first_seen_ns - 1_000_000_000): + return [failure_message(MSG_CONNECT_NOT_SUSPENDED, f"{returned}, before the plugin's POST")] + if result.cpu_sec > MAX_SUSPENDED_CALL_CPU_SEC: + return [failure_message(MSG_BUSY_LOOP, f"{returned}, thread CPU {result.cpu_sec:.3f} s")] + return [] + + +def evaluate_routing(result: ProbeResult, phase_label: str, required: bool, writer_ips: str) -> List[str]: + listed = [a for a in result.attempts if a.substitute_host is not None and a.phase == phase_label] + if result.phase_at_start != phase_label or not listed: + if not required: + return [] + detail = f"{result.name} phase at start {result.phase_at_start}, {len(result.attempts)} attempts, none with a substitute" + return [failure_message(MSG_ROUTING_NO_SUBSTITUTE, detail)] + + succeeded = [a for a in listed if a.outcome == "ok"] + if not succeeded: + return [failure_message(MSG_ROUTING_NO_CONNECTION, f"{result.name} in {phase_label}, last error {listed[-1].outcome}")] + + attempt = succeeded[0] + if attempt.driver_host is None: + return [failure_message(MSG_DRIVER_HOST_UNREADABLE, f"{result.name} in {phase_label}")] + + driver_host = attempt.driver_host.lower() + substitute = (attempt.substitute_host or "").lower() + detail = (f"{result.name} in {phase_label} dialled {result.probe_host}, listed substitute {attempt.substitute_host}, " + f"driver host {attempt.driver_host}, {writer_ips}") + if driver_host == substitute: + return [] + if driver_host == result.probe_host.lower(): + return [failure_message(_ROUTING_MESSAGES[phase_label], detail)] + return [failure_message(MSG_UNEXPECTED_HOST, detail)] + + +def join_all(threads: Sequence[Thread], sec: float) -> int: + """Join every thread within one shared deadline; returns how many are still alive.""" + deadline_ns = perf_counter_ns() + int(sec * 1_000_000_000) + for thread in threads: + remaining = (deadline_ns - perf_counter_ns()) / 1_000_000_000 + if remaining <= 0: + break + thread.join(remaining) + return sum(1 for thread in threads if thread.is_alive()) + + +def wait_for_bg_deployment_status( + test_utility: Any, + bg_id: str, + deadline_ns: int, + poll_sec: float = BG_API_POLL_SEC) -> Optional[str]: + """Poll the RDS API until the deployment reports a finished switchover or the deadline passes. Never raises.""" + status: Optional[str] = None + while True: + try: + deployment = test_utility.get_blue_green_deployment(bg_id) + status = None if deployment is None else deployment.get("Status") + except Exception as e: + print(f"Blue/Green deployment status read failed: {describe_error(e)}") + status = None + if status in ("SWITCHOVER_COMPLETED", "SWITCHOVER_FAILED"): + return status + remaining = (deadline_ns - perf_counter_ns()) / 1_000_000_000 + if remaining <= 0: + print(f"Blue/Green deployment status still {status} at the API wait deadline.") + return status + sleep(min(poll_sec, remaining)) + + +def settle_switchover( + test_utility: Any, + bg_id: str, + trigger_thread: Thread, + stop: Event, + switchover_requested: Event, + switchover_requested_ns: AtomicInt) -> None: + """Stop the trigger if it has not requested the switchover, else wait for the RDS API to finish it. Always sets stop.""" + try: + if not switchover_requested.is_set(): + stop.set() + trigger_thread.join(TRIGGER_JOIN_SEC) + if switchover_requested.is_set(): + wait_for_bg_deployment_status( + test_utility, bg_id, switchover_requested_ns.get() + BG_API_WAIT_SEC * 1_000_000_000) + finally: + stop.set() + + +def summary_lines( + test_start_ns: int, + threads_start_ns: int, + switchover_requested_ns: int, + wait_result: str, + observation: PluginStatusObservation, + probes: Sequence[ProbeResult], + extra: Sequence[str] = ()) -> List[str]: + zero_ns = switchover_requested_ns if switchover_requested_ns > 0 else threads_start_ns + zero_name = "switchover request" if switchover_requested_ns > 0 else "thread start" + lines = [ + "#1352 checks summary", + f" test entry to thread start: {_sec(threads_start_ns - test_start_ns)}", + f" probe host: {observation.probe_host} ({observation.probe_host_choice or 'not chosen'})", + f" wait: {wait_result}", + f" observer error: {observation.error or '-'}", + ] + if observation.provider_monitors_running is not None: + lines.append(f" status provider monitors running at the end of the window: {observation.provider_monitors_running}") + lines.extend(f" {line}" for line in extra) + for phase in PHASE_ORDER: + seen_ns = observation.plugin_phase_first_seen_ns.get(phase) + offset = "not seen" if seen_ns is None else f"{_sec(seen_ns - zero_ns)} after {zero_name}" + lines.append(f" plugin phase {phase}: {offset}") + for probe in probes: + timing = "not started" if probe.start_ns == 0 else f"start {_sec(probe.start_ns - zero_ns)}, " + ( + "in flight" if probe.end_ns == 0 else f"end {_sec(probe.end_ns - zero_ns)}") + line = f" {probe.name}: host {probe.probe_host or '-'}, phase at start {probe.phase_at_start}, " + if probe.phase_at_end is not None: + line += f"phase at end {probe.phase_at_end}, " + line += (f"{timing}, hold {_sec(probe.hold_ns)}, thread CPU {probe.cpu_sec:.3f} s, " + f"outcome {probe.outcome or '-'}") + if probe.suspend_detail: + line += f", suspend listed {probe.suspend_listed} {probe.suspend_detail}" + if probe.routing_detail: + line += f", {probe.routing_detail}" + if probe.spinning is not None: + line += (f", spinning {probe.spinning}, sample CPU {probe.spin_cpu_delta_sec}, " + f"frames {probe.frames[0]} / {probe.frames[1]}") + lines.append(line) + for index, attempt in enumerate(probe.attempts, 1): + lines.append(f" attempt {index} in {attempt.phase}: substitute {attempt.substitute_host}, " + f"driver host {attempt.driver_host}, outcome {attempt.outcome or '-'}") + return lines diff --git a/tests/integration/host/src/test/java/integration/host/TestEnvironment.java b/tests/integration/host/src/test/java/integration/host/TestEnvironment.java index fcd32a3a7..44d3fe4e5 100644 --- a/tests/integration/host/src/test/java/integration/host/TestEnvironment.java +++ b/tests/integration/host/src/test/java/integration/host/TestEnvironment.java @@ -591,6 +591,11 @@ private static void createDbCluster(TestEnvironment env, int numOfInstances) { env.auroraUtil.deleteCluster(env.rdsDbName, env.info.getRequest().getDatabaseEngineDeployment(), false); LOGGER.finer("Deleted cluster " + env.rdsDbName); + // Teardown never runs after a failed create, so the custom parameter group made for it goes here. + if (!StringUtils.isNullOrEmpty(env.info.getClusterParameterGroupName())) { + env.deleteCustomClusterParameterGroup(env.info.getClusterParameterGroupName()); + } + throw new RuntimeException(e); } } @@ -1355,6 +1360,20 @@ private void deleteBlueGreenDeployment() throws InterruptedException { return; } + // Settle an in-flight switchover: the status read below picks the cluster to delete. A deployment still + // switching after 15 minutes still stops the clean-up in deleteBlueGreenDeployment, as in JDBC. + try { + auroraUtil.waitUntilBlueGreenDeploymentHasRightState( + this.info.getBlueGreenDeploymentId(), "available", "switchover_completed", "switchover_failed"); + } catch (RuntimeException e) { + LOGGER.finest("BGD did not settle: " + e.getMessage()); + } + final BlueGreenDeployment settledDeployment = + auroraUtil.getBlueGreenDeployment(this.info.getBlueGreenDeploymentId()); + if (settledDeployment != null) { + blueGreenDeployment = settledDeployment; + } + auroraUtil.deleteBlueGreenDeployment(this.info.getBlueGreenDeploymentId(), true); // Remove extra DB cluster diff --git a/tests/integration/host/src/test/java/integration/util/AuroraTestUtility.java b/tests/integration/host/src/test/java/integration/util/AuroraTestUtility.java index 1b8684ed9..2bba3d415 100644 --- a/tests/integration/host/src/test/java/integration/util/AuroraTestUtility.java +++ b/tests/integration/host/src/test/java/integration/util/AuroraTestUtility.java @@ -912,6 +912,10 @@ public DatabaseEngine getRdsInstanceEngine(final DBInstance instance) { } public String getAuroraParameterGroupFamily(String engine, String engineVersion) { + final String family = queryParameterGroupFamily(engine, engineVersion); + if (!StringUtils.isNullOrEmpty(family)) { + return family; + } switch (engine) { case "aurora-postgresql": return "aurora-postgresql16"; @@ -925,6 +929,29 @@ public String getAuroraParameterGroupFamily(String engine, String engineVersion) } } + /** + * Asks RDS for the parameter group family of an engine version, or returns null when the version is + * empty or the family cannot be determined. + */ + private @Nullable String queryParameterGroupFamily(String engine, String engineVersion) { + if (StringUtils.isNullOrEmpty(engineVersion)) { + return null; + } + try { + final DescribeDbEngineVersionsResponse response = rdsClient.describeDBEngineVersions( + DescribeDbEngineVersionsRequest.builder().engine(engine).engineVersion(engineVersion).build()); + return response.dbEngineVersions().stream() + .map(DBEngineVersion::dbParameterGroupFamily) + .filter(family -> !StringUtils.isNullOrEmpty(family)) + .findFirst() + .orElse(null); + } catch (Exception ex) { + LOGGER.finest(String.format( + "Could not query the parameter group family of %s %s: %s", engine, engineVersion, ex.getMessage())); + return null; + } + } + public List getTestInstancesInfo(final String clusterId) { List dbInstances = getDBInstances(clusterId); List instancesInfo = new ArrayList<>(); @@ -1247,7 +1274,7 @@ public void deleteBlueGreenDeployment(String blueGreenId, boolean waitForComplet return; } - waitUntilBlueGreenDeploymentHasRightState(blueGreenId, "available", "switchover_completed"); + waitUntilBlueGreenDeploymentHasRightState(blueGreenId, "available", "switchover_completed", "switchover_failed"); DeleteBlueGreenDeploymentResponse response = rdsClient.deleteBlueGreenDeployment( DeleteBlueGreenDeploymentRequest.builder() From 7c426bd312ec3d0d5d423e73f02278dc71d50670 Mon Sep 17 00:00:00 2001 From: Juan Lee Date: Thu, 19 Nov 2026 23:37:59 -0700 Subject: [PATCH 2/4] test: simplify the Blue/Green switchover checks and run them in CI The switchover checks are now plain probes with one joined failure message. An execute and a connect started in IN_PROGRESS must return within 60 s and use at most 1.0 s of their thread's CPU, and a returned execute must have been held until the plugin left IN_PROGRESS. The first connection to the blue endpoint opened in POST must reach a green server recorded before the switchover, through a host that is not the dialled name. The PREPARATION sample, the /proc CPU sampling and the message table are gone, the "Green node changed name" assert is removed (certificates can change after the status shows completed, as in the JDBC wrapper), and the timings live in test_timings.py. Four Gradle tasks, test-bgd-{mysql,pg}-aurora-{sync,async}, build one environment and one deployment each, and the Integration Tests workflow runs them in a new job. The harness waits for a deployment to settle through one method that also accepts invalid_configuration, treats a deployment that is gone as settled, and falls back to the PostgreSQL major version for the parameter group family. --- .github/workflows/integration_tests.yml | 53 ++ .../container/test_blue_green_deployment.py | 583 ++++++++-------- .../test_blue_green_deployment_async.py | 316 ++++----- .../container/utils/blue_green_checks.py | 635 +++++------------- .../container/utils/rds_test_utility.py | 25 +- .../container/utils/test_timings.py | 49 ++ tests/integration/host/build.gradle.kts | 106 +++ .../integration/host/TestEnvironment.java | 15 +- .../integration/util/AuroraTestUtility.java | 28 +- 9 files changed, 858 insertions(+), 952 deletions(-) diff --git a/.github/workflows/integration_tests.yml b/.github/workflows/integration_tests.yml index 76519f84d..b0b2f5513 100644 --- a/.github/workflows/integration_tests.yml +++ b/.github/workflows/integration_tests.yml @@ -76,6 +76,59 @@ jobs: path: ./tests/integration/container/reports retention-days: 5 + blue-green-integration-tests: + name: Run Blue/Green Integration Tests + runs-on: ubuntu-latest + timeout-minutes: 120 + strategy: + fail-fast: false + max-parallel: 2 + matrix: + task: [ "mysql-aurora-sync", "mysql-aurora-async", "pg-aurora-sync", "pg-aurora-async" ] + + steps: + - name: 'Clone repository' + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + + - name: 'Set up JDK 8' + uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5 + with: + distribution: 'corretto' + java-version: 8 + + - name: Install poetry + shell: bash + run: | + pipx install poetry==1.8.2 + poetry config virtualenvs.prefer-active-python true + + - name: Install dependencies + run: poetry install + + - name: 'Configure AWS Credentials' + uses: aws-actions/configure-aws-credentials@d979d5b3a71173a29b74b5b88418bfda9437d885 # v6 + with: + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.AWS_DEPLOY_ROLE }} + role-session-name: python_bg_integration_tests + role-duration-seconds: 21600 + aws-region: ${{ secrets.AWS_DEFAULT_REGION }} + + - name: 'Run Blue/Green Integration Tests' + run: | + ./gradlew --no-parallel --no-daemon test-bgd-${{ matrix.task }} --info + env: + RDS_DB_REGION: ${{ secrets.AWS_DEFAULT_REGION }} + MYSQL_VERSION: default + PG_VERSION: default + + - name: 'Archive Blue/Green results' + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: pytest-integration-report-bgd-${{ matrix.task }} + path: ./tests/integration/container/reports + retention-days: 5 + latest-integration-tests: name: Run Latest Integration Tests runs-on: ubuntu-latest diff --git a/tests/integration/container/test_blue_green_deployment.py b/tests/integration/container/test_blue_green_deployment.py index 41893ba6b..96a307af8 100644 --- a/tests/integration/container/test_blue_green_deployment.py +++ b/tests/integration/container/test_blue_green_deployment.py @@ -14,7 +14,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Deque, Dict, List, Optional, Tuple +from typing import TYPE_CHECKING, Any, Deque, Dict, List, Optional, Set, Tuple import mysql.connector import psycopg @@ -29,7 +29,6 @@ import math import socket -import sys from collections import deque from dataclasses import dataclass, field from logging import INFO, getLogger @@ -40,11 +39,13 @@ from tabulate import tabulate # type: ignore[import-untyped] from aws_advanced_python_wrapper import AwsWrapperConnection -from aws_advanced_python_wrapper.blue_green_plugin import (BlueGreenPlugin, +from aws_advanced_python_wrapper.blue_green_plugin import (BlueGreenPhase, + BlueGreenPlugin, BlueGreenRole, BlueGreenStatus) from aws_advanced_python_wrapper.database_dialect import DialectCode from aws_advanced_python_wrapper.driver_info import DriverInfo +from aws_advanced_python_wrapper.hostinfo import HostInfo from aws_advanced_python_wrapper.utils import services_container from aws_advanced_python_wrapper.utils.atomic import AtomicInt from aws_advanced_python_wrapper.utils.concurrent import (ConcurrentDict, @@ -53,27 +54,24 @@ from aws_advanced_python_wrapper.utils.properties import (Properties, WrapperProperties) from aws_advanced_python_wrapper.utils.rds_utils import RdsUtils -from .utils.blue_green_checks import (BG_CONNECT_TIMEOUT_MS, BG_ID, - PLUGIN_STATUS_POLL_SEC, - PROBE_CONNECT_TIMEOUT_SEC, - ROUTING_PROBE_ATTEMPTS, - SWITCHOVER_TEST_TIMEOUT_SEC, - THREAD_JOIN_SEC, PluginStatusObservation, - ProbeResult, RoutingAttempt, - connect_suspend_listed, describe_error, - evaluate_routing, - evaluate_suspended_call, get_probe_host, - join_all, listed_substitute, - record_plugin_phase, settle_switchover, - summary_lines, wait_for_phase, +from .utils.blue_green_checks import (PhaseObserver, ProbeResult, + SwitchoverRequest, settle_switchover, + suspended_call_failures, wait_for_switchover) from .utils.conditions import enable_on_deployments, enable_on_features from .utils.database_engine import DatabaseEngine from .utils.database_engine_deployment import DatabaseEngineDeployment from .utils.driver_helper import DriverHelper from .utils.rds_test_utility import RdsTestUtility +from .utils.retry_helper import retry_until from .utils.test_environment import TestEnvironment from .utils.test_environment_features import TestEnvironmentFeatures +from .utils.test_timings import (BG_PLUGIN_STATUS_POLL_SEC, + BG_PROBE_CONNECT_ATTEMPTS, + BG_PROBE_CONNECT_TIMEOUT_SEC, + BG_PROBE_HOST_ROLE_WAIT_SEC, + BG_SWITCHOVER_PYTEST_TIMEOUT_SEC, + BG_THREAD_JOIN_SEC, BG_WAIT_STEP_SEC) @enable_on_deployments([DatabaseEngineDeployment.AURORA, DatabaseEngineDeployment.RDS_MULTI_AZ_INSTANCE]) @@ -93,6 +91,11 @@ class TestBlueGreenDeployment: PG_RDS_BG_STATUS_QUERY = \ (f"SELECT id, SPLIT_PART(endpoint, '.', 1) as hostId, endpoint, port, role, status, version " f"FROM rds_tools.show_topology('aws_advanced_python_wrapper-{DriverInfo.DRIVER_VERSION}')") + # The server's own host name or address, which the switchover's renames don't change. + MYSQL_SERVER_ID_QUERY = "SELECT @@hostname" + PG_SERVER_ID_QUERY = "SELECT pg_catalog.inet_server_addr()" + # The connect params set no bg_id, so the plugin keys its status by the default, normalized as the plugin does. + PLUGIN_BG_ID = str(WrapperProperties.BG_ID.default_value).strip().lower() results: ConcurrentDict[str, BlueGreenResults] = ConcurrentDict() unhandled_exceptions: Deque[Exception] = deque() mysql_dialect = MySQLDriverDialect(Properties()) @@ -106,11 +109,8 @@ def test_utility(self): def rds_utils(self): return RdsUtils() - # Worst case on any tree: setup + 150 s (latest request) + 300 s (API wait from the request) + 5 s (trigger join) - # + 2 s (CPU sample) + 20 s (joins), so setup + 477 s. The harness's --timeout=600 stays for every other test. - @pytest.mark.timeout(SWITCHOVER_TEST_TIMEOUT_SEC) + @pytest.mark.timeout(BG_SWITCHOVER_PYTEST_TIMEOUT_SEC) def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: TestEnvironment, test_driver): - test_start_ns = perf_counter_ns() self.results.clear() self.unhandled_exceptions.clear() @@ -131,6 +131,7 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: test_environment, test_utility, rds_utils, info.get_bg_deployment_id()) topology_instances_str = '\n'.join(topology_instances) self.logger.debug(f"topology_instances: \n{topology_instances_str}") + green_hosts: List[str] = [] for host in topology_instances: host_id = host[0:host.index(".")] @@ -196,6 +197,8 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_finish_count += 1 if rds_utils.is_green_instance(host): + green_hosts.append(host) + threads.append(Thread( daemon=True, target=self.direct_topology_monitor, @@ -242,50 +245,67 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: thread_count += 1 thread_finish_count += 1 - switchover_requested = Event() - switchover_requested_ns = AtomicInt(0) + assert len(green_hosts) > 0, "Unable to find a green instance in the Blue/Green topology." + green_host = green_hosts[0] + + port = test_instance.get_port() + if test_environment.get_deployment() == DatabaseEngineDeployment.AURORA: + blue_endpoint = test_environment.get_database_info().get_cluster_endpoint() + else: + blue_endpoint = test_instance.get_host() + + # conftest released the services container and its lazy re-initialisation isn't locked: build it here, + # before the threads' first connects race on it. + services_container.get_monitor_service() + blue_server_ids = self.get_server_ids( + test_driver, conn_utils, [instance.get_host() for instance in test_environment.get_instances()], port, + db_name) + green_server_ids = self.get_server_ids(test_driver, conn_utils, green_hosts, port, db_name) + self.logger.debug(f"Blue servers: {blue_server_ids}, green servers: {green_server_ids}") + assert len(blue_server_ids) > 0 + assert len(green_server_ids) > 0 + assert blue_server_ids.isdisjoint(green_server_ids) + assert "None" not in blue_server_ids | green_server_ids + probe_host = self.get_probe_host(test_driver, conn_utils, blue_endpoint, test_instance.get_host(), port, db_name) + self.logger.debug(f"Probe host: {probe_host}") + + request = SwitchoverRequest() trigger_thread = Thread( daemon=True, + name="BlueGreenSwitchoverTrigger", target=self.bg_switchover_trigger, - args=(test_utility, info.get_bg_deployment_id(), start_latch, finish_latch, self.results, stop, - switchover_requested, switchover_requested_ns)) + args=(test_utility, info.get_bg_deployment_id(), start_latch, finish_latch, self.results, stop, request)) threads.append(trigger_thread) thread_count += 1 thread_finish_count += 1 - # #1352 probes. Not counted in the latches: each one starts on the plugin phase it needs. - port = test_instance.get_port() - blue_endpoint = test_environment.get_database_info().get_cluster_endpoint() \ - if test_environment.get_deployment() == DatabaseEngineDeployment.AURORA else test_instance.get_host() - green_endpoint = self.get_green_endpoint(test_environment, test_utility, info.get_bg_deployment_id()) - writer_host = test_instance.get_host() - blue_writer_ip = self.resolve_ip(blue_endpoint) - green_writer_ip = "unresolved" if green_endpoint is None else self.resolve_ip(green_endpoint) - green_host = next((host for host in topology_instances if rds_utils.is_green_instance(host)), None) - observation = PluginStatusObservation() - execute_probe = ProbeResult("WrapperBlueSuspendedExecute") - connect_probe = ProbeResult("WrapperGreenSuspendedConnect", probe_host=green_host or "") - post_probe = ProbeResult("WrapperBlueRouting POST") - preparation_probe = ProbeResult("WrapperBlueRouting PREPARATION") - probes = [execute_probe, connect_probe, post_probe, preparation_probe] - threads.append(Thread( - daemon=True, - target=self.plugin_status_monitor, - args=(observation, switchover_requested, stop))) - threads.append(Thread( + # The observer and the probes don't count down start_latch: each probe starts on the plugin phase it needs. + observer = PhaseObserver() + execute_probe = ProbeResult() + connect_probe = ProbeResult() + routing_probe = ProbeResult() + execute_thread = Thread( daemon=True, + name="BlueGreenExecuteProbe", target=self.wrapper_blue_suspended_execute_probe, - args=(test_driver, conn_utils, blue_endpoint, writer_host, port, db_name, observation, switchover_requested, - stop, execute_probe))) - threads.append(Thread( + args=(test_driver, conn_utils, probe_host, port, db_name, observer, stop, finish_latch, execute_probe)) + connect_thread = Thread( daemon=True, + name="BlueGreenConnectProbe", target=self.wrapper_green_suspended_connect_probe, - args=(test_driver, conn_utils, green_host, port, db_name, observation, stop, connect_probe))) - threads.append(Thread( + args=(test_driver, conn_utils, green_host, port, db_name, observer, stop, finish_latch, connect_probe)) + routing_thread = Thread( daemon=True, + name="BlueGreenRoutingProbe", target=self.wrapper_blue_routing_probe, - args=(test_driver, conn_utils, blue_endpoint, writer_host, port, db_name, observation, switchover_requested, - stop, preparation_probe, post_probe))) + args=(test_driver, conn_utils, probe_host, port, db_name, observer, stop, finish_latch, routing_probe)) + threads.append(Thread( + daemon=True, + name="BlueGreenPhaseObserver", + target=self.plugin_status_monitor, + args=(observer, stop, finish_latch))) + threads.extend([execute_thread, connect_thread, routing_thread]) + thread_finish_count += 4 start_latch.set_count(thread_count) finish_latch.set_count(thread_finish_count) @@ -293,99 +313,128 @@ def test_switchover(self, conn_utils, test_utility, rds_utils, test_environment: for result in self.results.values(): result.start_time_ns.set(start_time_ns) - # An unfixed wrapper's suspended calls log two debug records per pass of their loop; keep them out of the report. + # A suspended call logs at DEBUG on each pass of its wait. One that never returns floods the report until the + # harness can't keep up, so the plugin stays at INFO while any test thread is alive. plugin_logger = getLogger(BlueGreenPlugin.__module__) plugin_log_level = plugin_logger.level plugin_logger.setLevel(INFO) - switch_interval = sys.getswitchinterval() - threads_start_ns = perf_counter_ns() - wait_result = "not run" try: - try: - # conftest released the services container and its lazy re-initialisation isn't locked: build it here, - # before the monitors' first connects race on it. - services_container.get_monitor_service() - for thread in threads: - thread.start() - - self.logger.debug("All threads started.") - wait_result = wait_for_switchover( - probes, observation, switchover_requested, switchover_requested_ns, threads_start_ns, - self.is_switchover_completed_seen) - self.logger.debug(f"Done waiting for the switchover: {wait_result}.") - finally: - settle_switchover( - test_utility, info.get_bg_deployment_id(), trigger_thread, stop, switchover_requested, - switchover_requested_ns) - self.logger.debug("Stop set for all threads.") - - alive_count = join_all(threads, THREAD_JOIN_SEC) + threads_start_ns = perf_counter_ns() + for thread in threads: + thread.start() + + self.logger.debug("All threads started.") + wait_result = wait_for_switchover( + request, threads_start_ns, + [(execute_thread, execute_probe), (connect_thread, connect_probe), (routing_thread, routing_probe)], + lambda: observer.is_seen(BlueGreenPhase.COMPLETED) or self.is_switchover_completed_seen()) + self.logger.debug(f"Done waiting for the switchover: {wait_result}.") + finally: + api_status = settle_switchover(test_utility, info.get_bg_deployment_id(), trigger_thread, stop, request) + self.logger.debug("Stop set for all threads.") + finish_latch.wait_sec(BG_THREAD_JOIN_SEC) + alive_count = sum(1 for thread in threads if thread.is_alive()) self.logger.debug(f"Done waiting for threads to stop, {alive_count} still running.") + if alive_count == 0: + plugin_logger.setLevel(plugin_log_level) - writer_ips = f"green writer IP {green_writer_ip}, blue writer IP {blue_writer_ip}" - not_sampled = [] if preparation_probe.phase_at_start == "PREPARATION" else [f"{preparation_probe.name}: not sampled"] - for line in summary_lines( - test_start_ns, threads_start_ns, switchover_requested_ns.get(), wait_result, observation, probes, - [writer_ips] + not_sampled): - print(line) - - for host_id, result in self.results.items(): - assert result.bg_trigger_time_ns.get() > 0, \ - (f"bg_trigger_time for {host_id} was {result.bg_trigger_time_ns.get()}, wait result: {wait_result}, " - f"unhandled exceptions: {', '.join(describe_error(e) for e in self.unhandled_exceptions) or 'none'}") - - self.logger.debug("Test is over.") - now_ns = perf_counter_ns() - post_first_seen_ns = observation.plugin_phase_first_seen_ns.get("POST") - failures = ( - evaluate_suspended_call("execute", execute_probe, now_ns, post_first_seen_ns) - + evaluate_suspended_call("connect", connect_probe, now_ns, post_first_seen_ns) - + evaluate_routing(post_probe, "POST", required=True, writer_ips=writer_ips) - + evaluate_routing(preparation_probe, "PREPARATION", required=False, writer_ips=writer_ips)) - # A monitor still appending to its results can make print_metrics raise; it must not hide the verdict. - try: - self.print_metrics(rds_utils) - except Exception as e: - print(f"print_metrics failed: {describe_error(e)}") - if failures: - pytest.fail("\n".join(failures)) + self.logger.debug( + f"Blue/Green deployment status: {api_status}. Plugin phases first seen: {observer.first_seen_ns}. " + f"Execute probe: {execute_probe}. Connect probe to {green_host}: {connect_probe}. " + f"Routing probe to {probe_host}: {routing_probe}.") + + assert request.error is None, f"The switchover request failed: {request.error}" + assert api_status == "SWITCHOVER_COMPLETED", \ + f"The Blue/Green deployment status was {api_status} when the test stopped waiting: {wait_result}." + + for host_id, result in self.results.items(): + assert result.bg_trigger_time_ns.get() > 0, \ + f"bg_trigger_time for {host_id} was {result.bg_trigger_time_ns.get()}" + + self.logger.debug("Test is over.") + now_ns = perf_counter_ns() + failures: List[str] = [] + if observer.is_seen(BlueGreenPhase.IN_PROGRESS): + failures.extend(suspended_call_failures("execute", execute_probe, now_ns, check_held=True, check_cpu=True)) + failures.extend(suspended_call_failures("connect", connect_probe, now_ns, check_held=False, check_cpu=True)) + else: + failures.append("The plugin phase IN_PROGRESS was never seen, so the execute and connect probes didn't run.") - if len(self.unhandled_exceptions) > 0: - self.log_unhandled_exceptions() - pytest.fail("There were unhandled exceptions.") + if not observer.is_seen(BlueGreenPhase.POST): + failures.append("The plugin phase POST was never seen, so the routing probe didn't run.") + elif routing_probe.driver_host is None or routing_probe.server_id is None: + failures.append(f"No connection opened in POST to the blue endpoint succeeded ({routing_probe.error}).") + elif routing_probe.driver_host.lower() == probe_host.lower(): + failures.append(f"A connection opened in POST went to the dialled host, not a substitute ({probe_host}).") + elif routing_probe.server_id not in green_server_ids: + failures.append(f"A connection opened in POST reached a server that isn't green ({routing_probe.server_id}).") - self.assert_test() + self.print_metrics(rds_utils) + assert not failures, "\n".join(failures) - self.logger.debug("Completed") - finally: - # Any thread still alive may be an unfixed call spinning (the inherited monitors as well as the probes). - # Restoring the interval or the DEBUG level under it floods the output until the harness can't keep up. - # A lane runs one test. - if not any(probe.is_in_flight() for probe in probes) and not any(thread.is_alive() for thread in threads): - sys.setswitchinterval(switch_interval) - plugin_logger.setLevel(plugin_log_level) + if len(self.unhandled_exceptions) > 0: + self.log_unhandled_exceptions() + pytest.fail("There were unhandled exceptions.") + + self.assert_test() + + self.logger.debug("Completed") def is_switchover_completed_seen(self) -> bool: return any(result.green_status_time.get("SWITCHOVER_COMPLETED", 0) for result in self.results.values()) - def resolve_ip(self, host: str) -> str: - try: - return socket.gethostbyname(host) - except Exception: - return "unresolved" + def get_server_ids( + self, test_driver: TestDriver, conn_utils: ConnectionUtils, hosts: List[str], port: int, db: str) -> Set[str]: + server_ids = set() + for host in hosts: + conn = self.get_direct_connection_with_retry( + test_driver, **conn_utils.get_connect_params(host=host, port=port, dbname=db)) + try: + server_ids.add(self.query_server_id(conn)) + finally: + self.close_connection(conn) + return server_ids - def get_green_endpoint(self, test_env: TestEnvironment, test_utility: RdsTestUtility, bg_id: str) -> Optional[str]: + def query_server_id(self, conn: Connection) -> str: + if TestEnvironment.get_current().get_engine() == DatabaseEngine.MYSQL: + query = self.MYSQL_SERVER_ID_QUERY + else: + query = self.PG_SERVER_ID_QUERY + with conn.cursor() as cursor: + cursor.execute(query) + return str(cursor.fetchall()[0][0]) + + def get_driver_host(self, conn: AwsWrapperConnection) -> str: + """The host the target driver connected to: the dialled name, or the substitute the plugin chose.""" + target = conn.target_connection + if TestEnvironment.get_current().get_engine() == DatabaseEngine.MYSQL: + return str(target.server_host) + return str(target.info.host) + + def get_probe_host( + self, + test_driver: TestDriver, + conn_utils: ConnectionUtils, + blue_endpoint: str, + writer_host: str, + port: int, + db: str) -> str: + """The blue endpoint once the plugin gives it its SOURCE role, else the writer instance. + + The first wrapper connection starts the plugin's status monitors. It connects to the writer instance, as the + monitor threads' connections do. + """ + conn = self.get_wrapper_connection_with_retry( + test_driver, **self.get_wrapper_connect_params(conn_utils, writer_host, port, db)) try: - bg_deployment = test_utility.get_blue_green_deployment(bg_id) - if bg_deployment is None: - return None - if test_env.get_deployment() == DatabaseEngineDeployment.RDS_MULTI_AZ_INSTANCE: - green_instance = test_utility.get_rds_instance_info_by_arn(bg_deployment["Target"]) - return None if green_instance is None else green_instance["Endpoint"]["Address"] - green_cluster = test_utility.get_cluster_by_arn(bg_deployment["Target"]) - return None if green_cluster is None else green_cluster["Endpoint"] - except Exception: - return None + has_source_role = retry_until( + lambda: self.get_plugin_role(blue_endpoint) == BlueGreenRole.SOURCE, + BG_PROBE_HOST_ROLE_WAIT_SEC, BG_WAIT_STEP_SEC) + finally: + self.close_connection(conn) + if has_source_role: + return blue_endpoint + return writer_host def get_bg_endpoints( self, @@ -738,7 +787,6 @@ def get_wrapper_connect_params(self, conn_utils: ConnectionUtils, host: str, por params = conn_utils.get_connect_params(host=host, port=port, dbname=db) params = {**params, **self.get_telemetry_params()} params[WrapperProperties.CLUSTER_ID.name] = self.TEST_CLUSTER_ID - params[WrapperProperties.BG_CONNECT_TIMEOUT_MS.name] = BG_CONNECT_TIMEOUT_MS test_env = TestEnvironment.get_current() engine = test_env.get_engine() db_deployment = test_env.get_deployment() @@ -921,197 +969,166 @@ def wrapper_blue_new_connection_monitor( finish_latch.count_down() self.logger.debug(f"[WrapperBlueNewConnection @ {host_id}] Thread is completed.") - # #1352 observer: the first time each plugin phase is published. Sync status storage is process-wide. - def plugin_status_monitor(self, observation: PluginStatusObservation, switchover_requested: Event, stop: Event) -> None: - while not stop.is_set(): - try: - status = self.read_plugin_status() - if status is not None: - record_plugin_phase(observation.plugin_phase_first_seen_ns, observation.phase_events, status) - if not switchover_requested.is_set(): - observation.pre_trigger_status = status - observation.status_seen.set() - except Exception as e: - observation.error = describe_error(e) - sleep(PLUGIN_STATUS_POLL_SEC) + # Plugin status + # Records the first time each phase is published. The sync status storage is process-wide. + # Needs a stop signal to terminate + def plugin_status_monitor(self, observer: PhaseObserver, stop: Event, finish_latch: CountDownLatch) -> None: + try: + while not stop.is_set(): + observer.record(self.read_plugin_status()) + sleep(BG_PLUGIN_STATUS_POLL_SEC) + except Exception as e: + self.logger.debug(f"[PluginStatus] Thread unhandled exception: {e}") + self.unhandled_exceptions.append(e) + finally: + finish_latch.count_down() + self.logger.debug("[PluginStatus] Thread is completed.") def read_plugin_status(self) -> Optional[BlueGreenStatus]: - return services_container.get_storage_service().get(BlueGreenStatus, BG_ID, register_data_access=False) + return services_container.get_storage_service().get( + BlueGreenStatus, self.PLUGIN_BG_ID, register_data_access=False) def read_plugin_phase(self) -> Optional[str]: - try: - status = self.read_plugin_status() - except Exception: + status = self.read_plugin_status() + if status is None: + return None + return status.phase.name + + def get_plugin_role(self, host: str) -> Optional[BlueGreenRole]: + status = self.read_plugin_status() + if status is None: return None - return None if status is None else status.phase.name + return status.get_role(HostInfo(host=host)) - # #1352: an execute that starts in IN_PROGRESS must return once the phase moves on, without a busy loop. + # Blue host + # Check: an execute started in IN_PROGRESS on a connection opened before it + # Expect: the plugin holds the execute until the phase moves on, without a busy loop + # Can terminate for itself def wrapper_blue_suspended_execute_probe( self, test_driver: TestDriver, conn_utils: ConnectionUtils, - blue_endpoint: str, - writer_host: str, + host: str, port: int, db: str, - observation: PluginStatusObservation, - switchover_requested: Event, + observer: PhaseObserver, stop: Event, + finish_latch: CountDownLatch, result: ProbeResult) -> None: conn = None try: - result.probe_host = get_probe_host(observation, blue_endpoint, writer_host, switchover_requested, stop) - target_driver_connect = DriverHelper.get_connect_func(test_driver) - conn = AwsWrapperConnection.connect( - target_driver_connect, **self.get_wrapper_connect_params(conn_utils, result.probe_host, port, db)) - bg_plugin: Optional[BlueGreenPlugin] = conn._unwrap(BlueGreenPlugin) - assert bg_plugin is not None, f"Unable to find blue/green plugin in wrapper connection for {result.probe_host}." - # cursor() goes through the plugin, so a cursor made inside the window would take the hold. - cursor = conn.cursor() - if not wait_for_phase(observation, "IN_PROGRESS", stop): - return - - status = self.read_plugin_status() - result.phase_at_start = None if status is None else status.phase.name - error: Optional[Exception] = None - result.begin() - try: - cursor.execute("SELECT 1") - except Exception as e: - error = e - result.hold_ns = bg_plugin.get_hold_time_ns() - result.phase_at_end = self.read_plugin_phase() - result.end(error) - try: - cursor.fetchall() - except Exception: - pass # the old blue may drop the connection at POST + conn = self.get_wrapper_connection_with_retry( + test_driver, **self.get_wrapper_connect_params(conn_utils, host, port, db)) + # cursor() goes through the plugin, so a cursor opened in IN_PROGRESS would be held as well. + with conn.cursor() as cursor: + if not observer.wait_for(BlueGreenPhase.IN_PROGRESS, stop): + return + + result.phase_at_start = self.read_plugin_phase() + error: Optional[Exception] = None + result.begin() + try: + cursor.execute("SELECT 1") + except Exception as e: + error = e + result.end(error, self.read_plugin_phase()) + if error is None: + cursor.fetchall() except Exception as e: - result.outcome = result.outcome or describe_error(e) + self.logger.debug(f"[WrapperBlueSuspendedExecute @ {host}] Thread exception: {e}") finally: - result.done = True self.close_connection(conn) + finish_latch.count_down() + self.logger.debug(f"[WrapperBlueSuspendedExecute @ {host}] Thread is completed.") - # #1352: a connect to a green host that starts in IN_PROGRESS must end once the phase moves on, without a busy loop. + # Green host + # Check: a new connection opened in IN_PROGRESS + # Expect: the plugin holds the connect until the phase moves on, without a busy loop + # Can terminate for itself def wrapper_green_suspended_connect_probe( self, test_driver: TestDriver, conn_utils: ConnectionUtils, - green_host: Optional[str], + host: str, port: int, db: str, - observation: PluginStatusObservation, + observer: PhaseObserver, stop: Event, + finish_latch: CountDownLatch, result: ProbeResult) -> None: conn = None try: - if green_host is None: - result.outcome = "no green instance in the topology" - return - params = self.get_wrapper_connect_params(conn_utils, green_host, port, db) - params[WrapperProperties.CONNECT_TIMEOUT_SEC.name] = PROBE_CONNECT_TIMEOUT_SEC + params = self.get_wrapper_connect_params(conn_utils, host, port, db) + params[WrapperProperties.CONNECT_TIMEOUT_SEC.name] = BG_PROBE_CONNECT_TIMEOUT_SEC target_driver_connect = DriverHelper.get_connect_func(test_driver) - if not wait_for_phase(observation, "IN_PROGRESS", stop): + if not observer.wait_for(BlueGreenPhase.IN_PROGRESS, stop): return - status = self.read_plugin_status() - result.phase_at_start = None if status is None else status.phase.name - result.suspend_listed, result.suspend_detail = connect_suspend_listed(status, green_host) + result.phase_at_start = self.read_plugin_phase() error: Optional[Exception] = None result.begin() try: conn = AwsWrapperConnection.connect(target_driver_connect, **params) except Exception as e: error = e - result.phase_at_end = self.read_plugin_phase() - result.end(error) + result.end(error, self.read_plugin_phase()) except Exception as e: - result.outcome = result.outcome or describe_error(e) + self.logger.debug(f"[WrapperGreenSuspendedConnect @ {host}] Thread exception: {e}") finally: - result.done = True self.close_connection(conn) + finish_latch.count_down() + self.logger.debug(f"[WrapperGreenSuspendedConnect @ {host}] Thread is completed.") - # #1352: a new connection to the probe host opened in PREPARATION, then in POST, - # must reach the substitute host the plugin's status lists for it. + # Blue host + # Check: a new connection opened in POST, the host the driver dialled and the server it reaches + # Expect: the plugin substitutes a green host, so the connection reaches a green server + # Can terminate for itself def wrapper_blue_routing_probe( self, test_driver: TestDriver, conn_utils: ConnectionUtils, - blue_endpoint: str, - writer_host: str, + host: str, port: int, db: str, - observation: PluginStatusObservation, - switchover_requested: Event, + observer: PhaseObserver, stop: Event, - preparation_result: ProbeResult, - post_result: ProbeResult) -> None: + finish_latch: CountDownLatch, + result: ProbeResult) -> None: try: - probe_host = get_probe_host(observation, blue_endpoint, writer_host, switchover_requested, stop) - preparation_result.probe_host = probe_host - post_result.probe_host = probe_host - params = self.get_wrapper_connect_params(conn_utils, probe_host, port, db) - params[WrapperProperties.CONNECT_TIMEOUT_SEC.name] = PROBE_CONNECT_TIMEOUT_SEC + params = self.get_wrapper_connect_params(conn_utils, host, port, db) + params[WrapperProperties.CONNECT_TIMEOUT_SEC.name] = BG_PROBE_CONNECT_TIMEOUT_SEC target_driver_connect = DriverHelper.get_connect_func(test_driver) - if wait_for_phase(observation, "PREPARATION", stop): - self.run_routing_attempts(target_driver_connect, params, port, "PREPARATION", 1, preparation_result) - preparation_result.done = True - if wait_for_phase(observation, "POST", stop): - self.run_routing_attempts(target_driver_connect, params, port, "POST", ROUTING_PROBE_ATTEMPTS, post_result) + if not observer.wait_for(BlueGreenPhase.POST, stop): + return + + # The first attempt that connects decides. Retry a connect error only while the plugin is still in POST. + for _ in range(BG_PROBE_CONNECT_ATTEMPTS): + result.phase_at_start = self.read_plugin_phase() + if result.phase_at_start != BlueGreenPhase.POST.name: + return + + conn = None + try: + conn = AwsWrapperConnection.connect(target_driver_connect, **params) + result.driver_host = self.get_driver_host(conn) + result.server_id = self.query_server_id(conn) + return + except Exception as e: + result.error = e + self.logger.debug(f"[WrapperBlueRouting @ {host}] Connect exception: {e}") + finally: + self.close_connection(conn) + sleep(BG_WAIT_STEP_SEC) except Exception as e: - post_result.outcome = post_result.outcome or describe_error(e) + self.logger.debug(f"[WrapperBlueRouting @ {host}] Thread exception: {e}") finally: - preparation_result.done = True - post_result.done = True - - def run_routing_attempts( - self, - target_driver_connect: Any, - params: Dict[str, Any], - port: int, - phase: str, - max_attempts: int, - result: ProbeResult) -> None: - status = self.read_plugin_status() - result.phase_at_start = None if status is None else status.phase.name - if result.phase_at_start != phase: - return - - result.start_ns = perf_counter_ns() - for _ in range(max_attempts): - # Re-read before every attempt: late POST drops the substitutes and connects by name. - status = self.read_plugin_status() - substitute = listed_substitute(status, result.probe_host, port) - attempt = RoutingAttempt("none" if status is None else status.phase.name, substitute) - result.attempts.append(attempt) - if substitute is None: - break - - conn = None - try: - conn = AwsWrapperConnection.connect(target_driver_connect, **params) - if listed_substitute(self.read_plugin_status(), result.probe_host, port) != substitute: - attempt.outcome = "status changed during the attempt" - else: - attempt.driver_host = self.get_driver_host(conn) - attempt.outcome = "ok" - except Exception as e: - attempt.outcome = describe_error(e) - finally: - self.close_connection(conn) - if attempt.outcome == "ok": - break - result.end_ns = perf_counter_ns() + finish_latch.count_down() + self.logger.debug(f"[WrapperBlueRouting @ {host}] Thread is completed.") - def get_driver_host(self, conn: AwsWrapperConnection) -> Optional[str]: - """The host the driver connected to (never hostaddr).""" - try: - target = conn.target_connection - if TestEnvironment.get_current().get_engine() == DatabaseEngine.MYSQL: - return str(target.server_host) - return str(target.info.host) - except Exception: - return None + def is_authentication_failure(self, exception: Exception) -> bool: + # MySQL rejects an IAM token with "Access denied", PostgreSQL with "PAM authentication failed". + error_message = str(exception).lower() + return "access denied" in error_message or "pam authentication failed" in error_message def is_timeout_exception(self, exception: Exception) -> bool: error_message = str(exception).lower() @@ -1353,12 +1370,11 @@ def green_iam_connectivity_monitor( else: self.logger.debug(f"[DirectGreenIamIp{thread_prefix} @ {host_id}] Thread exception: {e}") result_queue.append(TimeHolder(start_ns, perf_counter_ns(), error=str(e))) - # TODO: confirm 'Access Denied' is the error message surfaced in Python. - if notify_on_first_error and "access denied" in str(e).lower(): + if notify_on_first_error and self.is_authentication_failure(e): results.green_node_changed_name_time_ns.compare_and_set(0, perf_counter_ns()) self.logger.debug( f"[DirectGreenIamIp{thread_prefix} @ {host_id}] " - f"Encountered first 'Access denied' exception. Exiting thread...") + f"Encountered the first authentication failure. Exiting thread...") return self.close_connection(conn) @@ -1383,8 +1399,7 @@ def bg_switchover_trigger( finish_latch: CountDownLatch, results: Dict[str, BlueGreenResults], stop: Event, - switchover_requested: Event, - switchover_requested_ns: AtomicInt): + request: SwitchoverRequest): try: start_latch.count_down() @@ -1396,13 +1411,17 @@ def bg_switchover_trigger( result.threads_sync_time.set(sync_time_ns) sleep(30) - # The main thread sets stop when it gives up waiting for the request; it then waits for the API only if - # switchover_requested is set, so the check and the set stay adjacent. + # The main thread sets stop when it gives up waiting for the request, and then waits for the RDS API only + # if the request is marked, so the check and the mark stay together. if stop.is_set(): return - switchover_requested_ns.set(perf_counter_ns()) - switchover_requested.set() - test_utility.switchover_blue_green_deployment(bg_id) + request.requested_ns = perf_counter_ns() + request.requested.set() + try: + test_utility.switchover_blue_green_deployment(bg_id) + except Exception as e: + request.error = e + raise bg_trigger_time_ns = perf_counter_ns() for result in results.values(): @@ -1542,19 +1561,21 @@ def print_node_status_times(self, node: str, results: BlueGreenResults, time_zer self.logger.debug(f"\n{node}:\n{tabulate(table, headers=headers)}") def print_duration_times(self, node: str, title: str, times: Deque[TimeHolder], time_zero_ns: int): + # A monitor thread that hasn't stopped yet can still append to the deque. + samples = list(times) table = [] headers = ["Connect at (ms)", "Connect time/duration (ms)", "Error"] - p99_ns = self.get_percentile([time.end_time_ns - time.start_time_ns for time in times], 99.0) + p99_ns = self.get_percentile([time.end_time_ns - time.start_time_ns for time in samples], 99.0) p99_ms = p99_ns // 1_000_000 table.append(["p99", p99_ms, ""]) - first_connect = times[0] + first_connect = samples[0] table.append([ (first_connect.start_time_ns - time_zero_ns) // 1_000_000, (first_connect.end_time_ns - first_connect.start_time_ns) // 1_000_000, self.get_formatted_error(first_connect.error) ]) - for time_holder in times: + for time_holder in samples: duration_ms = (time_holder.end_time_ns - time_holder.start_time_ns) // 1_000_000 if duration_ms > p99_ms: table.append([ @@ -1563,7 +1584,7 @@ def print_duration_times(self, node: str, title: str, times: Deque[TimeHolder], self.get_formatted_error(time_holder.error) ]) - last_connect = times[-1] + last_connect = samples[-1] table.append([ (last_connect.start_time_ns - time_zero_ns) // 1_000_000, (last_connect.end_time_ns - last_connect.start_time_ns) // 1_000_000, @@ -1611,8 +1632,6 @@ def assert_test(self): # Assertions assert switchover_complete_time_ms != 0, "BG switchover hasn't completed." - assert switchover_complete_time_ms >= max_green_node_changed_name_time_ms, \ - "Green node changed name after SWITCHOVER_COMPLETED." @dataclass diff --git a/tests/integration/container/test_blue_green_deployment_async.py b/tests/integration/container/test_blue_green_deployment_async.py index 6e5edb41a..38902efda 100644 --- a/tests/integration/container/test_blue_green_deployment_async.py +++ b/tests/integration/container/test_blue_green_deployment_async.py @@ -43,8 +43,8 @@ - boto3 RDS Blue/Green switchover/wait calls stay synchronous. - ``sleep()`` calls inside threads remain synchronous (OS threads; no event loop blocked). -- Every thread teardown invokes ``cleanup_async()`` on its own loop so - wrapper background tasks are released cleanly. +- The main thread invokes ``cleanup_async()`` once, after the worker threads + stop, so no worker releases wrapper services the others still use. """ from __future__ import annotations @@ -52,7 +52,6 @@ import asyncio import math import socket -import sys from collections import deque from dataclasses import dataclass, field from logging import INFO, getLogger @@ -69,9 +68,10 @@ AsyncBlueGreenPlugin from aws_advanced_python_wrapper.aio.blue_green_plugin import \ BlueGreenStatus as AsyncBlueGreenStatus -from aws_advanced_python_wrapper.aio.cleanup import unregister_shutdown_hook -from aws_advanced_python_wrapper.aio.wrapper import AsyncAwsWrapperConnection -from aws_advanced_python_wrapper.blue_green_plugin import BlueGreenRole +from aws_advanced_python_wrapper.aio.wrapper import (AsyncAwsWrapperConnection, + AsyncAwsWrapperCursor) +from aws_advanced_python_wrapper.blue_green_plugin import (BlueGreenPhase, + BlueGreenRole) from aws_advanced_python_wrapper.database_dialect import DialectCode from aws_advanced_python_wrapper.driver_info import DriverInfo from aws_advanced_python_wrapper.utils import services_container @@ -84,12 +84,8 @@ from tests.integration.container.utils.async_connection_helpers import \ cleanup_async from tests.integration.container.utils.blue_green_checks import ( - ASYNC_CONNECT_WAIT_SEC, ASYNC_EXECUTE_TIMEOUT_SEC, BG_CONNECT_TIMEOUT_MS, - MSG_ASYNC_NOT_CONNECTED, PLUGIN_STATUS_POLL_SEC, - SWITCHOVER_TEST_TIMEOUT_SEC, THREAD_JOIN_SEC, PluginStatusObservation, - ProbeResult, choose_probe_host, describe_error, evaluate_suspended_call, - execute_routing_detail, failure_message, join_all, record_plugin_phase, - settle_switchover, summary_lines, wait_for_switchover) + PhaseObserver, ProbeResult, SwitchoverRequest, settle_switchover, + suspended_call_failures, wait_for_switchover) from tests.integration.container.utils.conditions import ( enable_on_deployments, enable_on_features) from tests.integration.container.utils.database_engine import DatabaseEngine @@ -100,10 +96,12 @@ from tests.integration.container.utils.test_environment import TestEnvironment from tests.integration.container.utils.test_environment_features import \ TestEnvironmentFeatures +from tests.integration.container.utils.test_timings import ( + BG_ASYNC_EXECUTE_TIMEOUT_SEC, BG_ASYNC_PROBE_CONNECT_WAIT_SEC, + BG_PLUGIN_STATUS_POLL_SEC, BG_PROBE_CONNECT_ATTEMPTS, + BG_SWITCHOVER_PYTEST_TIMEOUT_SEC, BG_THREAD_JOIN_SEC) if TYPE_CHECKING: - from aws_advanced_python_wrapper.aio.blue_green_plugin import \ - AsyncBlueGreenStatusProvider from tests.integration.container.utils.connection_utils import \ ConnectionUtils from tests.integration.container.utils.test_driver import TestDriver @@ -193,20 +191,14 @@ def _run_worker_with_loop( hangs. This helper encapsulates the correct shape: one loop per thread for - the thread's entire lifetime, with ``cleanup_async()`` always invoked - on that same loop in ``finally`` so wrapper background tasks are - released even if the body raises. + the thread's entire lifetime. It does not invoke ``cleanup_async()``: + that releases process-wide services, so the main thread invokes it + once, after every worker stopped. """ loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: - try: - body(loop) - finally: - try: - loop.run_until_complete(cleanup_async()) - except Exception: - pass + body(loop) finally: loop.close() @@ -277,15 +269,11 @@ def test_utility(self): def rds_utils(self): return RdsUtils() - # Worst case on any tree: setup + 30 s (async probe connect) + 150 s (latest request) + 300 s (API wait from the - # request) + 5 s (trigger join) + 2 s (CPU sample) + 20 s (joins), so setup + 507 s. The harness's --timeout=600 - # stays for every other test. - @pytest.mark.timeout(SWITCHOVER_TEST_TIMEOUT_SEC) + @pytest.mark.timeout(BG_SWITCHOVER_PYTEST_TIMEOUT_SEC) def test_switchover_async( self, conn_utils: ConnectionUtils, test_utility: RdsTestUtility, rds_utils: RdsUtils, test_environment: TestEnvironment, test_driver: TestDriver): - test_start_ns = perf_counter_ns() self.results.clear() self.unhandled_exceptions.clear() @@ -417,31 +405,32 @@ def test_switchover_async( thread_count += 1 thread_finish_count += 1 - switchover_requested = Event() - switchover_requested_ns = AtomicInt(0) + request = SwitchoverRequest() trigger_thread = Thread( daemon=True, + name="BlueGreenSwitchoverTrigger", target=self.bg_switchover_trigger, - args=(test_utility, info.get_bg_deployment_id(), start_latch, finish_latch, self.results, stop, - switchover_requested, switchover_requested_ns)) + args=(test_utility, info.get_bg_deployment_id(), start_latch, finish_latch, self.results, stop, request)) threads.append(trigger_thread) thread_count += 1 thread_finish_count += 1 - # #1352 probe. Each AsyncAwsWrapperConnection keeps its own Blue/Green status store, so only the - # connection that owns the status provider sees the phases: the probe connects first and is also the observer. - blue_endpoint = test_environment.get_database_info().get_cluster_endpoint() \ - if test_environment.get_deployment() == DatabaseEngineDeployment.AURORA else test_instance.get_host() - probe_host, probe_host_choice = choose_probe_host(None, blue_endpoint, test_instance.get_host()) - observation = PluginStatusObservation() - execute_probe = ProbeResult("AsyncWrapperBlueSuspendedExecute") - probes = [execute_probe] + # Each AsyncAwsWrapperConnection keeps its own Blue/Green status store, so only the connection that owns the + # status provider sees the phases: the probe connects before the other threads start and is also the observer. + if test_environment.get_deployment() == DatabaseEngineDeployment.AURORA: + blue_endpoint = test_environment.get_database_info().get_cluster_endpoint() + else: + blue_endpoint = test_instance.get_host() + observer = PhaseObserver() + execute_probe = ProbeResult() probe_connected = Event() probe_thread = Thread( daemon=True, + name="BlueGreenAsyncExecuteProbe", target=self.wrapper_blue_suspended_execute_probe_async, - args=(test_driver, conn_utils, probe_host, test_instance.get_port(), db_name, observation, - stop, probe_connected, execute_probe)) + args=(test_driver, conn_utils, blue_endpoint, test_instance.get_port(), db_name, observer, stop, + finish_latch, probe_connected, execute_probe)) + thread_finish_count += 1 start_latch.set_count(thread_count) finish_latch.set_count(thread_finish_count) @@ -449,69 +438,64 @@ def test_switchover_async( for result in self.results.values(): result.start_time_ns.set(start_time_ns) - # An unfixed wrapper's suspended calls log two debug records per pass of their loop; keep them out of the report. + # A suspended call logs at DEBUG on each pass of its wait. One that never returns floods the report until the + # harness can't keep up, so the plugin stays at INFO while any test thread is alive. plugin_logger = getLogger(AsyncBlueGreenPlugin.__module__) plugin_log_level = plugin_logger.level plugin_logger.setLevel(INFO) - switch_interval = sys.getswitchinterval() - threads_start_ns = perf_counter_ns() - wait_result = "not run" + # conftest released the services container and its lazy re-initialisation isn't locked: build it here, + # before the threads' first connects race on it. + services_container.get_monitor_service() + probe_thread.start() + if not probe_connected.wait(BG_ASYNC_PROBE_CONNECT_WAIT_SEC): + stop.set() + pytest.fail( + f"The async execute probe didn't connect to {blue_endpoint} within " + f"{BG_ASYNC_PROBE_CONNECT_WAIT_SEC:.0f} s.") + try: - # conftest released the services container and its lazy re-initialisation isn't locked: build it here, - # before the monitors' first connects race on it. - services_container.get_monitor_service() - probe_thread.start() - if not probe_connected.wait(ASYNC_CONNECT_WAIT_SEC) or not probe_thread.is_alive(): - stop.set() - for line in summary_lines( - test_start_ns, threads_start_ns, 0, "async probe not connected", observation, probes): - print(line) - pytest.fail(failure_message( - MSG_ASYNC_NOT_CONNECTED, - f"connected {probe_connected.is_set()}, alive {probe_thread.is_alive()}, outcome {execute_probe.outcome or '-'}")) + threads_start_ns = perf_counter_ns() + for thread in threads: + thread.start() + + self.logger.debug("All threads started.") + wait_result = wait_for_switchover( + request, threads_start_ns, [(probe_thread, execute_probe)], + lambda: observer.is_seen(BlueGreenPhase.COMPLETED) or self.is_switchover_completed_seen()) + self.logger.debug(f"Done waiting for the switchover: {wait_result}.") + finally: + api_status = settle_switchover(test_utility, info.get_bg_deployment_id(), trigger_thread, stop, request) + self.logger.debug("Stop set for all threads.") + finish_latch.wait_sec(BG_THREAD_JOIN_SEC) threads.append(probe_thread) - - try: - threads_start_ns = perf_counter_ns() - for thread in threads: - if thread is not probe_thread: - thread.start() - - self.logger.debug("All threads started.") - observation.probe_host = probe_host - observation.probe_host_choice = f"{probe_host_choice}, the async probe connects before any status" - wait_result = wait_for_switchover( - probes, observation, switchover_requested, switchover_requested_ns, threads_start_ns, - self.is_switchover_completed_seen) - self.logger.debug(f"Done waiting for the switchover: {wait_result}.") - finally: - settle_switchover( - test_utility, info.get_bg_deployment_id(), trigger_thread, stop, switchover_requested, - switchover_requested_ns) - self.logger.debug("Stop set for all threads.") - - alive_count = join_all(threads, THREAD_JOIN_SEC) + alive_count = sum(1 for thread in threads if thread.is_alive()) self.logger.debug(f"Done waiting for threads to stop, {alive_count} still running.") + if alive_count == 0: + plugin_logger.setLevel(plugin_log_level) + + try: + self.logger.debug( + f"Blue/Green deployment status: {api_status}. Plugin phases first seen: {observer.first_seen_ns}. " + f"Execute probe on {blue_endpoint}: {execute_probe}.") - for line in summary_lines( - test_start_ns, threads_start_ns, switchover_requested_ns.get(), wait_result, observation, probes): - print(line) + assert request.error is None, f"The switchover request failed: {request.error}" + assert api_status == "SWITCHOVER_COMPLETED", \ + f"The Blue/Green deployment status was {api_status} when the test stopped waiting: {wait_result}." for host_id, result in self.results.items(): assert result.bg_trigger_time_ns.get() > 0, \ - (f"bg_trigger_time for {host_id} was {result.bg_trigger_time_ns.get()}, wait result: {wait_result}, " - f"unhandled exceptions: {', '.join(describe_error(e) for e in self.unhandled_exceptions) or 'none'}") + f"bg_trigger_time for {host_id} was {result.bg_trigger_time_ns.get()}" self.logger.debug("Test is over.") - failures = evaluate_suspended_call( - "async execute", execute_probe, perf_counter_ns(), observation.plugin_phase_first_seen_ns.get("POST")) - # A monitor still appending to its results can make print_metrics raise; it must not hide the verdict. - try: - self.print_metrics(rds_utils) - except Exception as e: - print(f"print_metrics failed: {describe_error(e)}") - if failures: - pytest.fail("\n".join(failures)) + failures: List[str] = [] + if observer.is_seen(BlueGreenPhase.IN_PROGRESS): + failures.extend(suspended_call_failures( + "async execute", execute_probe, perf_counter_ns(), check_held=True, check_cpu=False)) + else: + failures.append("The plugin phase IN_PROGRESS was never seen, so the execute probe didn't run.") + + self.print_metrics(rds_utils) + assert not failures, "\n".join(failures) if len(self.unhandled_exceptions) > 0: self.log_unhandled_exceptions() @@ -521,12 +505,11 @@ def test_switchover_async( self.logger.debug("Completed") finally: - # Any thread still alive may be an unfixed call spinning (the inherited monitors as well as the probes). - # Restoring the interval or the DEBUG level under it floods the output until the harness can't keep up. - # A lane runs one test. - if not any(probe.is_in_flight() for probe in probes) and not any(thread.is_alive() for thread in threads): - sys.setswitchinterval(switch_interval) - plugin_logger.setLevel(plugin_log_level) + # Released once, after the workers stopped: a release in a worker stops the services the others still use. + try: + asyncio.run(cleanup_async()) + except Exception as e: + self.logger.debug(f"cleanup_async failed: {e}") def is_switchover_completed_seen(self) -> bool: return any(result.green_status_time.get("SWITCHOVER_COMPLETED", 0) for result in self.results.values()) @@ -671,7 +654,6 @@ def get_wrapper_connect_params( params = conn_utils.get_connect_params(host=host, port=port, dbname=db) params = {**params, **self.get_telemetry_params()} params[WrapperProperties.CLUSTER_ID.name] = self.TEST_CLUSTER_ID - params[WrapperProperties.BG_CONNECT_TIMEOUT_MS.name] = BG_CONNECT_TIMEOUT_MS test_env = TestEnvironment.get_current() engine = test_env.get_engine() db_deployment = test_env.get_deployment() @@ -696,6 +678,11 @@ def get_wrapper_connect_params( return params + def is_authentication_failure(self, exception: Exception) -> bool: + # MySQL rejects an IAM token with "Access denied", PostgreSQL with "PAM authentication failed". + error_message = str(exception).lower() + return "access denied" in error_message or "pam authentication failed" in error_message + def is_timeout_exception(self, exception: Exception) -> bool: error_message = str(exception).lower() timeout_keywords = [ @@ -1128,8 +1115,10 @@ def _body(loop: asyncio.AbstractEventLoop) -> None: finish_latch.count_down() self.logger.debug(f"[AsyncWrapperBlueNewConnection @ {host_id}] Thread is completed.") - # #1352: an async execute that starts in IN_PROGRESS must return once the phase moves on. One coroutine runs - # for the thread's whole life, so the status provider's tasks keep running on this loop. + # Blue host + # Check: an execute started in IN_PROGRESS on the connection that owns the status provider + # Expect: the plugin holds the execute until the phase moves on + # Needs a stop signal to terminate: the status provider's tasks run on this thread's loop until then def wrapper_blue_suspended_execute_probe_async( self, test_driver: TestDriver, @@ -1137,75 +1126,58 @@ def wrapper_blue_suspended_execute_probe_async( host: str, port: int, db: str, - observation: PluginStatusObservation, + observer: PhaseObserver, stop: Event, + finish_latch: CountDownLatch, connected: Event, result: ProbeResult) -> None: - result.probe_host = host connect_params = self.get_wrapper_connect_params(conn_utils, host, port, db) - def _read_phase(bg_plugin: AsyncBlueGreenPlugin) -> Optional[str]: - try: - status = bg_plugin._plugin_service.get_status(AsyncBlueGreenStatus, bg_plugin._bg_id) - except Exception: - return None - return None if status is None else status.phase.name + def _read_status(bg_plugin: AsyncBlueGreenPlugin) -> Optional[AsyncBlueGreenStatus]: + return bg_plugin._plugin_service.get_status(AsyncBlueGreenStatus, bg_plugin._bg_id) + + async def _open_with_retry() -> AsyncAwsWrapperConnection: + for _ in range(BG_PROBE_CONNECT_ATTEMPTS - 1): + try: + return await _open_wrapper_conn_async(test_driver, connect_params) + except Exception as e: + self.logger.debug(f"[AsyncWrapperBlueSuspendedExecute @ {host}] Connect exception: {e}") + return await _open_wrapper_conn_async(test_driver, connect_params) - async def _execute_in_progress(cursor: Any, bg_plugin: AsyncBlueGreenPlugin) -> None: - result.phase_at_start = "IN_PROGRESS" + async def _execute_in_progress(cursor: AsyncAwsWrapperCursor, bg_plugin: AsyncBlueGreenPlugin) -> None: + result.phase_at_start = BlueGreenPhase.IN_PROGRESS.name error: Optional[Exception] = None result.begin() try: - # A blocking regression would never yield to this bound. - await asyncio.wait_for(cursor.execute("SELECT 1"), ASYNC_EXECUTE_TIMEOUT_SEC) + await asyncio.wait_for(cursor.execute("SELECT 1"), BG_ASYNC_EXECUTE_TIMEOUT_SEC) except Exception as e: error = e - result.hold_ns = bg_plugin.get_hold_time_ns() - result.phase_at_end = _read_phase(bg_plugin) - result.end(error) + result.end(error, observer.record(_read_status(bg_plugin))) + # The old blue may drop the connection at POST; the loop must keep running for the status provider. try: await cursor.fetchall() - except Exception: - pass # the old blue may drop the connection at POST + except Exception as e: + self.logger.debug(f"[AsyncWrapperBlueSuspendedExecute @ {host}] Fetch exception: {e}") async def _main() -> None: - conn = await _open_wrapper_conn_async(test_driver, connect_params) - provider: Optional[AsyncBlueGreenStatusProvider] = None + conn = await _open_with_retry() + bg_plugin: Optional[AsyncBlueGreenPlugin] = _unwrap_plugin_async(conn, AsyncBlueGreenPlugin) try: - bg_plugin: Optional[AsyncBlueGreenPlugin] = _unwrap_plugin_async(conn, AsyncBlueGreenPlugin) assert bg_plugin is not None, f"Unable to find blue/green plugin in wrapper connection for {host}." - # cursor() is sync and lazy. The warm-up resolves the driver cursor, so the execute in the window is a - # single plugin pass and its hold time isn't reset. - cursor = conn.cursor() - await cursor.execute("SELECT 1") - await cursor.fetchall() - # Every async worker drains the process-wide shutdown hooks when it ends, and several end on their - # first lost connection, mid-switchover. Take this connection's status provider out of that drain - # before the other threads start, and stop it here instead. - provider = AsyncBlueGreenPlugin._status_providers.get(bg_plugin._bg_id) - if provider is not None: - unregister_shutdown_hook(provider.stop) - connected.set() - - executed = False - while not stop.is_set(): - status = bg_plugin._plugin_service.get_status(AsyncBlueGreenStatus, bg_plugin._bg_id) - phase = record_plugin_phase(observation.plugin_phase_first_seen_ns, observation.phase_events, status) - if phase == "IN_PROGRESS" and not executed: - executed = True - # Diagnostic only: shows which branch of the plugin's execute a call that isn't held took. - result.routing_detail = execute_routing_detail(status, bg_plugin._plugin_service) - await _execute_in_progress(cursor, bg_plugin) - elif phase in ("POST", "COMPLETED"): - executed = True # IN_PROGRESS was missed; the check reports not run - await asyncio.sleep(PLUGIN_STATUS_POLL_SEC) + # Entering the cursor creates the driver cursor through the plugin, before IN_PROGRESS. + async with conn.cursor() as cursor: + connected.set() + while not stop.is_set(): + phase = observer.record(_read_status(bg_plugin)) + if phase == BlueGreenPhase.IN_PROGRESS.name and result.start_ns == 0: + await _execute_in_progress(cursor, bg_plugin) + await asyncio.sleep(BG_PLUGIN_STATUS_POLL_SEC) finally: - if provider is not None: - observation.provider_monitors_running = any(monitor.is_running() for monitor in provider._monitors) - try: + # Stop the status provider's tasks on the loop they run on. + if bg_plugin is not None: + provider = AsyncBlueGreenPlugin._status_providers.get(bg_plugin._bg_id) + if provider is not None: await provider.stop() - except Exception: - pass await _close_conn_async(conn) def _body(loop: asyncio.AbstractEventLoop) -> None: @@ -1214,9 +1186,10 @@ def _body(loop: asyncio.AbstractEventLoop) -> None: try: _run_worker_with_loop(_body) except Exception as e: - result.outcome = result.outcome or describe_error(e) + self.logger.debug(f"[AsyncWrapperBlueSuspendedExecute @ {host}] Thread exception: {e}") finally: - result.done = True + finish_latch.count_down() + self.logger.debug(f"[AsyncWrapperBlueSuspendedExecute @ {host}] Thread is completed.") # Blue / Green DNS monitors are purely sync (socket calls); reuse verbatim. @@ -1433,12 +1406,12 @@ def _body(loop: asyncio.AbstractEventLoop) -> None: f"[AsyncDirectGreenIamIp{thread_prefix} @ {host_id}] " f"Thread exception: {e}") result_queue.append(TimeHolder(start_ns, perf_counter_ns(), error=str(e))) - if notify_on_first_error and "access denied" in str(e).lower(): + if notify_on_first_error and self.is_authentication_failure(e): results.green_node_changed_name_time_ns.compare_and_set( 0, perf_counter_ns()) self.logger.debug( f"[AsyncDirectGreenIamIp{thread_prefix} @ {host_id}] " - f"Encountered first 'Access denied' exception. Exiting thread...") + f"Encountered the first authentication failure. Exiting thread...") return self.close_connection_async(conn, loop) @@ -1468,8 +1441,7 @@ def bg_switchover_trigger( finish_latch: CountDownLatch, results: Any, stop: Event, - switchover_requested: Event, - switchover_requested_ns: AtomicInt) -> None: + request: SwitchoverRequest) -> None: """Trigger BG switchover using RDS API (synchronous boto3 calls).""" try: start_latch.count_down() @@ -1480,13 +1452,17 @@ def bg_switchover_trigger( result.threads_sync_time.set(sync_time_ns) sleep(30) - # The main thread sets stop when it gives up waiting for the request; it then waits for the API only if - # switchover_requested is set, so the check and the set stay adjacent. + # The main thread sets stop when it gives up waiting for the request, and then waits for the RDS API only + # if the request is marked, so the check and the mark stay together. if stop.is_set(): return - switchover_requested_ns.set(perf_counter_ns()) - switchover_requested.set() - test_utility.switchover_blue_green_deployment(bg_id) + request.requested_ns = perf_counter_ns() + request.requested.set() + try: + test_utility.switchover_blue_green_deployment(bg_id) + except Exception as e: + request.error = e + raise bg_trigger_time_ns = perf_counter_ns() for result in results.values(): @@ -1632,19 +1608,21 @@ def print_node_status_times( def print_duration_times( self, node: str, title: str, times: Deque[TimeHolder], time_zero_ns: int) -> None: + # A monitor thread that hasn't stopped yet can still append to the deque. + samples = list(times) table = [] headers = ["Connect at (ms)", "Connect time/duration (ms)", "Error"] - p99_ns = self.get_percentile([time.end_time_ns - time.start_time_ns for time in times], 99.0) + p99_ns = self.get_percentile([time.end_time_ns - time.start_time_ns for time in samples], 99.0) p99_ms = p99_ns // 1_000_000 table.append(["p99", p99_ms, ""]) - first_connect = times[0] + first_connect = samples[0] table.append([ (first_connect.start_time_ns - time_zero_ns) // 1_000_000, (first_connect.end_time_ns - first_connect.start_time_ns) // 1_000_000, self.get_formatted_error(first_connect.error) ]) - for time_holder in times: + for time_holder in samples: duration_ms = (time_holder.end_time_ns - time_holder.start_time_ns) // 1_000_000 if duration_ms > p99_ms: table.append([ @@ -1653,7 +1631,7 @@ def print_duration_times( self.get_formatted_error(time_holder.error) ]) - last_connect = times[-1] + last_connect = samples[-1] table.append([ (last_connect.start_time_ns - time_zero_ns) // 1_000_000, (last_connect.end_time_ns - last_connect.start_time_ns) // 1_000_000, @@ -1699,5 +1677,3 @@ def assert_test(self) -> None: self.logger.debug(f"switchover_complete_time: {switchover_complete_time_ms} ms") assert switchover_complete_time_ms != 0, "BG switchover hasn't completed." - assert switchover_complete_time_ms >= max_green_node_changed_name_time_ms, \ - "Green node changed name after SWITCHOVER_COMPLETED." diff --git a/tests/integration/container/utils/blue_green_checks.py b/tests/integration/container/utils/blue_green_checks.py index be3ede751..17de43a86 100644 --- a/tests/integration/container/utils/blue_green_checks.py +++ b/tests/integration/container/utils/blue_green_checks.py @@ -12,537 +12,208 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Checks shared by the sync and async Blue/Green switchover tests. - -The switchover tests run probes that hit the plugin's PREPARATION, IN_PROGRESS -and POST phases and then judge them here. A suspended call must return once the -phase moves on, without a busy loop, and a new connection to a blue host must go -to the substitute host the plugin's status lists for it. This module holds the -pure logic, so it imports no database driver. -""" +"""Probe results and waits shared by the sync and async Blue/Green switchover tests.""" from __future__ import annotations -import os -import sys from dataclasses import dataclass, field -from threading import Event, Lock, get_ident, get_native_id +from threading import Condition, Event from time import perf_counter_ns, sleep, thread_time from typing import (TYPE_CHECKING, Any, Callable, Dict, List, Optional, Sequence, Tuple) -from aws_advanced_python_wrapper.blue_green_plugin import ( - BlueGreenRole, SubstituteConnectRouting, SuspendConnectRouting) -from aws_advanced_python_wrapper.hostinfo import HostInfo -from aws_advanced_python_wrapper.utils.properties import WrapperProperties +from aws_advanced_python_wrapper.blue_green_plugin import BlueGreenPhase +from .test_timings import (BG_API_STATUS_WAIT_SEC, + BG_SUSPENDED_CALL_MAX_CPU_SEC, + BG_SUSPENDED_CALL_MAX_SEC, + BG_SWITCHOVER_REQUEST_WAIT_SEC, + BG_SWITCHOVER_WAIT_SEC, BG_TRIGGER_JOIN_SEC, + BG_WAIT_STEP_SEC) if TYPE_CHECKING: from threading import Thread - from aws_advanced_python_wrapper.utils.atomic import AtomicInt - -# No bg_id is set in the test's connect params, so the plugin keys its status by the property default, normalized as -# BlueGreenPlugin.__init__ does. -BG_ID = str(WrapperProperties.BG_ID.default_value).strip().lower() -# Pinned in the switchover tests' connect params, at its default. -BG_CONNECT_TIMEOUT_MS = 30_000 -# Twice bg_connect_timeout_ms (pinned to 30 s in the connect params), which caps a correct suspend. Same 60 s as JDBC's -# MAX_TRAFFIC_SUSPENSION_MS (bgSwitchoverTimeoutMs / 3), derived differently. -MAX_TRAFFIC_SUSPENSION_NS = 2 * BG_CONNECT_TIMEOUT_MS * 1_000_000 -# A correct suspend waits on the status condition in 50 ms steps; the unfixed loop burned about a whole core. -MAX_SUSPENDED_CALL_CPU_SEC = 1.0 -# Thread CPU sample taken on a call that is still in flight past MAX_TRAFFIC_SUSPENSION_NS. -SPIN_SAMPLE_SEC = 2.0 -# Past the main thread's sample of a stuck call: the 60 s bound, a 1 s wait tick and the 2 s CPU sample, with headroom. -ASYNC_EXECUTE_TIMEOUT_SEC = MAX_TRAFFIC_SUSPENSION_NS // 1_000_000_000 + 15 -# A spinning thread shares the GIL with the other spinners and still gets far more than this in 2 s; a blocked read gets close to 0. -SPIN_MIN_CPU_SEC = 0.05 -# The plugin publishes each phase for at least 1.7 s (measured), so a 20 ms poll sees every phase. -PLUGIN_STATUS_POLL_SEC = 0.02 -# Poll step for probes waiting on a phase, and the main thread's wait tick. -PROBE_WAIT_STEP_SEC = 0.5 -WAIT_TICK_SEC = 1.0 -# Connect timeout of each routing and connect probe attempt; POST lasts 23 to 26 s (measured). -PROBE_CONNECT_TIMEOUT_SEC = 10 -ROUTING_PROBE_ATTEMPTS = 3 -# The async probe must connect, and own the status provider, before any other thread starts. -ASYNC_CONNECT_WAIT_SEC = 30 -# From thread start: the trigger waits for the start latch and then 30 s before it requests the switchover. -TRIGGER_WAIT_SEC = 150 -# From the switchover request: the plugin reached COMPLETED 41 to 45 s after it (measured). -SWITCHOVER_WAIT_SEC = 240 -# From the switchover request: the RDS API reported SWITCHOVER_COMPLETED after 68 to 73 s (measured). -BG_API_WAIT_SEC = 300 -BG_API_POLL_SEC = 5.0 -TRIGGER_JOIN_SEC = 5 -# One deadline shared by every join. -THREAD_JOIN_SEC = 20 -# See the worst case on the switchover tests' marker. -SWITCHOVER_TEST_TIMEOUT_SEC = 900 -# Lets the main thread get the GIL often while stuck probes spin. -MIN_SWITCH_INTERVAL_SEC = 0.0005 - -PHASE_ORDER = ("PREPARATION", "IN_PROGRESS", "POST", "COMPLETED") - -MSG_SPIN_EXECUTE = "#1352 spin - sync execute suspended in IN_PROGRESS did not return within 60 s" -MSG_SPIN_CONNECT = "#1352 spin - sync connect suspended in IN_PROGRESS did not return within 60 s" -MSG_SPIN_ASYNC_EXECUTE = "#1352 spin - async execute suspended in IN_PROGRESS did not return within 60 s" -MSG_BUSY_LOOP = "#1352 busy loop - a suspended call used more than 1.0 s of its thread CPU" -MSG_ROUTING_POST = "#1352 routing - connection opened in POST went to the dialled host, not the listed substitute" -MSG_ROUTING_PREPARATION = "#1352 routing - connection opened in PREPARATION went to the dialled host, not the listed substitute" -MSG_BLOCKED = "#1352 check inconclusive - a suspended probe is blocked, not spinning" -MSG_UNEXPECTED_HOST = "#1352 check inconclusive - a probe connection went to an unexpected host" -MSG_EXECUTE_NOT_IN_PROGRESS = "#1352 check not run - the execute probe did not start in IN_PROGRESS" -MSG_EXECUTE_NOT_HELD = "#1352 check not run - the execute probe call was not held by the plugin" -MSG_ASYNC_EXECUTE_NOT_IN_PROGRESS = "#1352 check not run - the async execute probe did not start in IN_PROGRESS" -MSG_ASYNC_EXECUTE_NOT_HELD = "#1352 check not run - the async execute probe call was not held by the plugin" -MSG_CONNECT_NOT_IN_PROGRESS = "#1352 check not run - the connect probe did not start in IN_PROGRESS" -MSG_CONNECT_NOT_SUSPENDED = "#1352 check not run - the connect probe was not suspended by the plugin" -MSG_ROUTING_NO_SUBSTITUTE = "#1352 check not run - the routing probe saw no POST status with a substitute routing for its host" -MSG_ROUTING_NO_CONNECTION = "#1352 check not run - the routing probe opened no connection while a substitute was listed" -MSG_DRIVER_HOST_UNREADABLE = "#1352 check not run - the driver host of a probe connection could not be read" -MSG_ASYNC_NOT_CONNECTED = "#1352 check not run - the async execute probe did not connect before the other threads" - -# label -> (spin, not started in IN_PROGRESS, not held); connect has no hold. -_SUSPENDED_CALL_MESSAGES: Dict[str, Tuple[str, str, Optional[str]]] = { - "execute": (MSG_SPIN_EXECUTE, MSG_EXECUTE_NOT_IN_PROGRESS, MSG_EXECUTE_NOT_HELD), - "connect": (MSG_SPIN_CONNECT, MSG_CONNECT_NOT_IN_PROGRESS, None), - "async execute": (MSG_SPIN_ASYNC_EXECUTE, MSG_ASYNC_EXECUTE_NOT_IN_PROGRESS, MSG_ASYNC_EXECUTE_NOT_HELD), -} -_ROUTING_MESSAGES: Dict[str, str] = {"POST": MSG_ROUTING_POST, "PREPARATION": MSG_ROUTING_PREPARATION} - + from .rds_test_utility import RdsTestUtility -@dataclass -class RoutingAttempt: - phase: str - substitute_host: Optional[str] # None when the status listed no substitute for the probe host - driver_host: Optional[str] = None - outcome: str = "" +_NS_PER_SEC = 1_000_000_000 +_PHASES_PAST_IN_PROGRESS = (BlueGreenPhase.POST.name, BlueGreenPhase.COMPLETED.name) @dataclass class ProbeResult: - name: str - probe_host: str = "" + """What one probe saw. A phase is the name of the plugin's phase, read from its status store.""" phase_at_start: Optional[str] = None - # The plugin phase read right after a suspended call returned. phase_at_end: Optional[str] = None start_ns: int = 0 - end_ns: int = 0 # 0 while the call is in flight - hold_ns: int = 0 + end_ns: int = 0 cpu_sec: float = 0.0 - outcome: str = "" # "ok", or the exception class and the start of its message - suspend_listed: bool = False - suspend_detail: str = "" - # For the summary only: how the plugin's execute would route this host under the status that started the call. - routing_detail: str = "" - attempts: List[RoutingAttempt] = field(default_factory=list) - native_id: Optional[int] = None - ident: Optional[int] = None - done: bool = False - # Filled by the main thread when the call is still in flight. - spinning: Optional[bool] = None - spin_cpu_delta_sec: Optional[float] = None - frames: Tuple[str, str] = ("none", "none") + error: Optional[Exception] = None + driver_host: Optional[str] = None + server_id: Optional[str] = None _cpu_start_sec: float = field(default=0.0, repr=False) def begin(self) -> None: - """Called on the probe's own thread right before the call.""" - self.native_id = get_native_id() - self.ident = get_ident() + """Call on the probe's own thread right before the call.""" self._cpu_start_sec = thread_time() self.start_ns = perf_counter_ns() - def end(self, error: Optional[BaseException]) -> None: - """Called on the probe's own thread right after the call.""" + def end(self, error: Optional[Exception], phase_at_end: Optional[str]) -> None: + """Call on the probe's own thread right after the call.""" self.cpu_sec = thread_time() - self._cpu_start_sec - self.outcome = "ok" if error is None else describe_error(error) + self.error = error + self.phase_at_end = phase_at_end self.end_ns = perf_counter_ns() - def in_flight_ns(self, now_ns: int) -> int: - if self.start_ns == 0: - return 0 - return (self.end_ns if self.end_ns > 0 else now_ns) - self.start_ns - def is_in_flight(self) -> bool: return self.start_ns > 0 and self.end_ns == 0 + def duration_sec(self, now_ns: int) -> float: + """How long the call took, or has taken so far while it is in flight.""" + if self.start_ns == 0: + return 0.0 + if self.end_ns == 0: + return (now_ns - self.start_ns) / _NS_PER_SEC + return (self.end_ns - self.start_ns) / _NS_PER_SEC + def is_stuck(self, now_ns: int) -> bool: - return self.is_in_flight() and now_ns - self.start_ns > MAX_TRAFFIC_SUSPENSION_NS + return self.is_in_flight() and self.duration_sec(now_ns) > BG_SUSPENDED_CALL_MAX_SEC + + +def suspended_call_failures( + label: str, probe: ProbeResult, now_ns: int, check_held: bool, check_cpu: bool) -> List[str]: + """Judge a call that a probe started in IN_PROGRESS. It gets at most one failure line. + + :param label: names the call in the line, for example "execute". + :param probe: the probe's result. + :param now_ns: when the test stopped waiting, from perf_counter_ns(). + :param check_held: whether the plugin must hold the call until the phase moves on. + :param check_cpu: whether the call must stay within BG_SUSPENDED_CALL_MAX_CPU_SEC of its thread's CPU. + :return: the failure line, or no line when the call passed. + """ + if probe.phase_at_start != BlueGreenPhase.IN_PROGRESS.name: + return [f"The {label} probe didn't start in IN_PROGRESS (phase {probe.phase_at_start})."] + if probe.duration_sec(now_ns) > BG_SUSPENDED_CALL_MAX_SEC: + return [f"The {label} started in IN_PROGRESS didn't return within {BG_SUSPENDED_CALL_MAX_SEC:.0f} s."] + if check_held and isinstance(probe.error, TimeoutError): + return [f"bg_connect_timeout_ms released the {label} while the plugin phase was still IN_PROGRESS."] + if check_held and probe.phase_at_end not in _PHASES_PAST_IN_PROGRESS: + return [f"The {label} returned in plugin phase {probe.phase_at_end}, so the plugin didn't hold it."] + if check_cpu and probe.cpu_sec > BG_SUSPENDED_CALL_MAX_CPU_SEC: + return [f"The suspended {label} used more than {BG_SUSPENDED_CALL_MAX_CPU_SEC} s of its thread's CPU."] + return [] -@dataclass -class PluginStatusObservation: - """What the plugin published, as the test's observer saw it.""" - plugin_phase_first_seen_ns: Dict[str, int] = field(default_factory=dict) - phase_events: Dict[str, Event] = field(default_factory=lambda: {phase: Event() for phase in PHASE_ORDER}) - status_seen: Event = field(default_factory=Event) - pre_trigger_status: Any = None - error: str = "" - probe_host: Optional[str] = None - probe_host_choice: str = "" - # Async only: whether the status provider's monitors were still running at the end of the window. - provider_monitors_running: Optional[bool] = None - lock: Lock = field(default_factory=Lock) - - -def describe_error(error: BaseException) -> str: - return f"{type(error).__name__}: {str(error)[:200]}" - - -def _clean(text: str) -> str: - return text.replace(";", ",").replace("|", "/").replace("\n", " ") - - -def failure_message(text: str, detail: str) -> str: - return f"{text} ({_clean(detail)})" - - -def _sec(ns: int) -> str: - return f"{ns / 1_000_000_000:.2f} s" - - -def record_plugin_phase( - phase_first_seen_ns: Dict[str, int], - phase_events: Dict[str, Event], - status: Any) -> Optional[str]: - """Record the first time a phase is seen; works for the sync and the aio status classes.""" - if status is None: - return None - name: str = status.phase.name - if name not in phase_first_seen_ns: - phase_first_seen_ns[name] = perf_counter_ns() - event = phase_events.get(name) - if event is not None: - event.set() - return name - - -def wait_for_phase(observation: PluginStatusObservation, phase: str, stop: Event) -> bool: - """Wait for the phase; False on stop or when a later phase was seen without it.""" - later = PHASE_ORDER[PHASE_ORDER.index(phase) + 1:] - while not stop.is_set(): - if observation.phase_events[phase].is_set(): - return True - if any(observation.phase_events[p].is_set() for p in later): - return False - observation.phase_events[phase].wait(PROBE_WAIT_STEP_SEC) - return False - - -def choose_probe_host(status: Any, cluster_endpoint: str, writer_host: str) -> Tuple[str, str]: - if status is None: - return cluster_endpoint, "no pre-trigger status" - if status.get_role(HostInfo(host=cluster_endpoint)) is not None: - return cluster_endpoint, "cluster endpoint" - return writer_host, "writer instance" - - -def get_probe_host( - observation: PluginStatusObservation, - cluster_endpoint: str, - writer_host: str, - switchover_requested: Event, - stop: Event) -> str: - """Wait for the first published status (or the request), then choose the probe host once for every probe.""" - while not (observation.status_seen.is_set() or switchover_requested.is_set() or stop.is_set()): - observation.status_seen.wait(PROBE_WAIT_STEP_SEC) - with observation.lock: - if observation.probe_host is None: - observation.probe_host, observation.probe_host_choice = \ - choose_probe_host(observation.pre_trigger_status, cluster_endpoint, writer_host) - return observation.probe_host - - -def listed_substitute(status: Any, probe_host: str, port: int) -> Optional[str]: - """The substitute host the status lists for the probe host. Reads the routings; never calls is_match, the code under test.""" - if status is None: - return None - host = probe_host.lower() - endpoints = (host, f"{host}:{port}") - for routing in status.connect_routings: - if isinstance(routing, SubstituteConnectRouting) and routing._endpoint is not None \ - and routing._endpoint.lower() in endpoints: - return routing._substitute_host_info.host - return None - - -def connect_suspend_listed(status: Any, host: str) -> Tuple[bool, str]: - if status is None: - return False, "no status" - role = status.get_role(HostInfo(host=host)) - if role != BlueGreenRole.TARGET: - return False, f"role of the connect probe host was {role}" - for routing in status.connect_routings: - if isinstance(routing, SuspendConnectRouting) and routing._endpoint is None \ - and routing._bg_role == BlueGreenRole.TARGET: - return True, "SuspendConnectRouting(None, TARGET) listed" - return False, "no SuspendConnectRouting(None, TARGET) listed" - - -def execute_routing_detail(status: Any, plugin_service: Any) -> str: - """The current host's role and first matching execute routing, as the plugin's execute picks them. Summary only.""" - try: +class PhaseObserver: + """The first time a test thread saw each phase the plugin published.""" + + def __init__(self) -> None: + self.first_seen_ns: Dict[str, int] = {} + self._latest_phase_value = 0 + self._changed = Condition() + + def record(self, status: Any) -> Optional[str]: + """Record a status read from the plugin. Works for the sync and the aio status classes. + + :param status: the status, or None when the plugin has published none. + :return: the name of the status's phase, or None when there is no status. + """ if status is None: - return "routing: no status" - host_info = plugin_service.current_host_info - role = status.get_role(host_info) - routing = None if role is None else next( - (r for r in status.execute_routings if r.is_match(host_info, role)), None) - host = None if host_info is None else host_info.get_host_and_port() - return (f"current host {host}, role {role}, first matching execute routing {routing}, " - f"roles by endpoint {sorted(status.roles_by_endpoint.keys())}") - except Exception as e: - return f"routing: {describe_error(e)}" - - -def thread_cpu_sec(native_id: Optional[int]) -> Optional[float]: - """utime + stime of one thread of this process (Linux); None where /proc is missing.""" - if native_id is None: - return None - try: - with open(f"/proc/self/task/{native_id}/stat") as stat_file: - stat = stat_file.read() - fields = stat[stat.rindex(")") + 2:].split() - # Fields after the command name start at field 3 (state); utime and stime are fields 14 and 15. - return (int(fields[11]) + int(fields[12])) / os.sysconf("SC_CLK_TCK") - except (OSError, ValueError, IndexError): - return None - - -def stuck_frames(ident: Optional[int]) -> Tuple[str, str]: - """The innermost frame of a thread and its innermost frame in the wrapper, as file:function (never arguments).""" - frame = None if ident is None else sys._current_frames().get(ident) - if frame is None: - return "none", "none" - innermost = f"{os.path.basename(frame.f_code.co_filename)}:{frame.f_code.co_name}" - wrapper_frame = "none" - marker = f"aws_advanced_python_wrapper{os.sep}" - current: Any = frame - while current is not None: - filename = current.f_code.co_filename - if marker in filename: - wrapper_frame = f"{filename[filename.rindex(marker):]}:{current.f_code.co_name}" - break - current = current.f_back - return innermost, wrapper_frame - - -def is_spinning(cpu_before: Optional[float], cpu_after: Optional[float]) -> bool: - if cpu_before is None or cpu_after is None: - return True - return cpu_after - cpu_before >= SPIN_MIN_CPU_SEC - - -def sample_in_flight_probes(probes: Sequence[ProbeResult]) -> None: - """Sample the thread CPU of in-flight probes once over SPIN_SAMPLE_SEC, and lower the switch interval first.""" - if not probes: - return - sys.setswitchinterval(MIN_SWITCH_INTERVAL_SEC) - before = [thread_cpu_sec(probe.native_id) for probe in probes] - sleep(SPIN_SAMPLE_SEC) - for probe, cpu_before in zip(probes, before): - cpu_after = thread_cpu_sec(probe.native_id) - probe.spinning = is_spinning(cpu_before, cpu_after) - probe.spin_cpu_delta_sec = None if cpu_before is None or cpu_after is None else cpu_after - cpu_before - probe.frames = stuck_frames(probe.ident) - - -def _is_settled(probe: ProbeResult, now_ns: int, completed: bool) -> bool: - return probe.done or probe.end_ns > 0 or probe.is_stuck(now_ns) or (probe.start_ns == 0 and completed) + return None + phase = status.phase + with self._changed: + if phase.name not in self.first_seen_ns: + self.first_seen_ns[phase.name] = perf_counter_ns() + self._latest_phase_value = max(self._latest_phase_value, phase.phase_value) + self._changed.notify_all() + return phase.name + + def is_seen(self, phase: BlueGreenPhase) -> bool: + return phase.name in self.first_seen_ns + + def wait_for(self, phase: BlueGreenPhase, stop: Event) -> bool: + """Wait until the phase is seen. + + :param phase: the phase to wait for. + :param stop: ends the wait when set. + :return: True when the phase was seen; False on stop, or when a later phase was seen without it. + """ + with self._changed: + while not stop.is_set(): + if phase.name in self.first_seen_ns: + return True + if self._latest_phase_value > phase.phase_value: + return False + self._changed.wait(BG_WAIT_STEP_SEC) + return False + + +@dataclass +class SwitchoverRequest: + """Filled by the trigger thread. requested_ns is set before requested, and error only after it.""" + requested: Event = field(default_factory=Event) + requested_ns: int = 0 + error: Optional[Exception] = None + + +def _is_settled(thread: Thread, result: ProbeResult, now_ns: int) -> bool: + return not thread.is_alive() or result.end_ns > 0 or result.is_stuck(now_ns) def wait_for_switchover( - probes: Sequence[ProbeResult], - observation: PluginStatusObservation, - switchover_requested: Event, - switchover_requested_ns: AtomicInt, + request: SwitchoverRequest, threads_start_ns: int, - completed_seen: Callable[[], bool]) -> str: - """Wait until every probe settled and the switchover completed (or a probe is stuck), within the stated bounds.""" - sampled: List[str] = [] - while True: + probes: Sequence[Tuple[Thread, ProbeResult]], + is_completed: Callable[[], bool]) -> str: + """Wait until the switchover completed and every probe ended, or a probe is stuck, within the bounds. + + :param request: the trigger thread's switchover request. + :param threads_start_ns: when the test threads started, from perf_counter_ns(). + :param probes: each probe's thread and result. + :param is_completed: tells whether a test thread saw the switchover complete. + :return: why the wait ended. + """ + request_deadline_ns = threads_start_ns + int(BG_SWITCHOVER_REQUEST_WAIT_SEC * _NS_PER_SEC) + switchover_wait_ns = int(BG_SWITCHOVER_WAIT_SEC * _NS_PER_SEC) + while request.error is None: now_ns = perf_counter_ns() - requested = switchover_requested.is_set() - if requested and now_ns >= switchover_requested_ns.get() + SWITCHOVER_WAIT_SEC * 1_000_000_000: - reason = f"bound reached: switchover request + {SWITCHOVER_WAIT_SEC} s" - break - if not requested and now_ns >= threads_start_ns + TRIGGER_WAIT_SEC * 1_000_000_000: - reason = f"bound reached: no switchover request {TRIGGER_WAIT_SEC} s after thread start" - break - - newly_stuck = [p for p in probes if p.is_stuck(now_ns) and p.name not in sampled] - if newly_stuck: - sample_in_flight_probes(newly_stuck) - sampled.extend(p.name for p in newly_stuck) - continue - - completed = observation.phase_events["COMPLETED"].is_set() or completed_seen() - if requested and all(_is_settled(p, now_ns, completed) for p in probes) \ - and (completed or any(p.is_stuck(now_ns) for p in probes)): - return "probes settled" + (", switchover completed" if completed else ", a probe is stuck") - sleep(WAIT_TICK_SEC) - - sample_in_flight_probes([p for p in probes if p.is_in_flight() and p.name not in sampled]) - return reason - - -def evaluate_suspended_call( - label: str, - result: ProbeResult, - now_ns: int, - post_first_seen_ns: Optional[int]) -> List[str]: - spin_message, not_run_message, not_held_message = _SUSPENDED_CALL_MESSAGES[label] - # A probe sampled past the bound as blocked gets the inconclusive text; an unsampled or spinning one the defect's. - stuck_message = MSG_BLOCKED if result.spinning is False else spin_message - if result.phase_at_start != "IN_PROGRESS": - return [failure_message(not_run_message, f"{result.name} phase at start {result.phase_at_start}, outcome {result.outcome or '-'}")] - if label == "connect" and not result.suspend_listed: - return [failure_message(MSG_CONNECT_NOT_SUSPENDED, f"{result.name} {result.suspend_detail}")] - - if result.is_in_flight(): - cpu_delta = "unread" if result.spin_cpu_delta_sec is None else f"{result.spin_cpu_delta_sec:.3f} s" - detail = (f"{result.name} on {result.probe_host} in flight {_sec(result.in_flight_ns(now_ns))}, " - f"CPU over {SPIN_SAMPLE_SEC} s sample {cpu_delta}, " - f"innermost frame {result.frames[0]}, wrapper frame {result.frames[1]}") - return [failure_message(stuck_message, detail)] - - returned = f"{result.name} returned after {_sec(result.in_flight_ns(now_ns))} with {result.outcome}" - if result.in_flight_ns(now_ns) > MAX_TRAFFIC_SUSPENSION_NS or result.hold_ns > MAX_TRAFFIC_SUSPENSION_NS: - return [failure_message(stuck_message, f"{returned}, hold {_sec(result.hold_ns)}, over the 60 s bound")] - if result.phase_at_end == "IN_PROGRESS": - # Released by the plugin's suspend timer, not by the phase change. - return [failure_message(not_run_message, f"{returned}, plugin phase still IN_PROGRESS when it returned")] - if not_held_message is not None and result.hold_ns == 0: - return [failure_message(not_held_message, returned)] - if label == "connect" and (post_first_seen_ns is None or result.end_ns < post_first_seen_ns - 1_000_000_000): - return [failure_message(MSG_CONNECT_NOT_SUSPENDED, f"{returned}, before the plugin's POST")] - if result.cpu_sec > MAX_SUSPENDED_CALL_CPU_SEC: - return [failure_message(MSG_BUSY_LOOP, f"{returned}, thread CPU {result.cpu_sec:.3f} s")] - return [] - - -def evaluate_routing(result: ProbeResult, phase_label: str, required: bool, writer_ips: str) -> List[str]: - listed = [a for a in result.attempts if a.substitute_host is not None and a.phase == phase_label] - if result.phase_at_start != phase_label or not listed: - if not required: - return [] - detail = f"{result.name} phase at start {result.phase_at_start}, {len(result.attempts)} attempts, none with a substitute" - return [failure_message(MSG_ROUTING_NO_SUBSTITUTE, detail)] - - succeeded = [a for a in listed if a.outcome == "ok"] - if not succeeded: - return [failure_message(MSG_ROUTING_NO_CONNECTION, f"{result.name} in {phase_label}, last error {listed[-1].outcome}")] - - attempt = succeeded[0] - if attempt.driver_host is None: - return [failure_message(MSG_DRIVER_HOST_UNREADABLE, f"{result.name} in {phase_label}")] - - driver_host = attempt.driver_host.lower() - substitute = (attempt.substitute_host or "").lower() - detail = (f"{result.name} in {phase_label} dialled {result.probe_host}, listed substitute {attempt.substitute_host}, " - f"driver host {attempt.driver_host}, {writer_ips}") - if driver_host == substitute: - return [] - if driver_host == result.probe_host.lower(): - return [failure_message(_ROUTING_MESSAGES[phase_label], detail)] - return [failure_message(MSG_UNEXPECTED_HOST, detail)] - - -def join_all(threads: Sequence[Thread], sec: float) -> int: - """Join every thread within one shared deadline; returns how many are still alive.""" - deadline_ns = perf_counter_ns() + int(sec * 1_000_000_000) - for thread in threads: - remaining = (deadline_ns - perf_counter_ns()) / 1_000_000_000 - if remaining <= 0: - break - thread.join(remaining) - return sum(1 for thread in threads if thread.is_alive()) - - -def wait_for_bg_deployment_status( - test_utility: Any, - bg_id: str, - deadline_ns: int, - poll_sec: float = BG_API_POLL_SEC) -> Optional[str]: - """Poll the RDS API until the deployment reports a finished switchover or the deadline passes. Never raises.""" - status: Optional[str] = None - while True: - try: - deployment = test_utility.get_blue_green_deployment(bg_id) - status = None if deployment is None else deployment.get("Status") - except Exception as e: - print(f"Blue/Green deployment status read failed: {describe_error(e)}") - status = None - if status in ("SWITCHOVER_COMPLETED", "SWITCHOVER_FAILED"): - return status - remaining = (deadline_ns - perf_counter_ns()) / 1_000_000_000 - if remaining <= 0: - print(f"Blue/Green deployment status still {status} at the API wait deadline.") - return status - sleep(min(poll_sec, remaining)) + if not request.requested.is_set(): + if now_ns > request_deadline_ns: + return f"no switchover request within {BG_SWITCHOVER_REQUEST_WAIT_SEC:.0f} s of thread start" + elif now_ns > request.requested_ns + switchover_wait_ns: + return f"the switchover did not finish within {BG_SWITCHOVER_WAIT_SEC:.0f} s of the request" + elif all(_is_settled(thread, result, now_ns) for thread, result in probes): + if is_completed(): + return "the switchover completed" + if any(result.is_stuck(now_ns) for _, result in probes): + return "a probe is stuck" + sleep(BG_WAIT_STEP_SEC) + return "the switchover request failed" def settle_switchover( - test_utility: Any, + test_utility: RdsTestUtility, bg_id: str, trigger_thread: Thread, stop: Event, - switchover_requested: Event, - switchover_requested_ns: AtomicInt) -> None: - """Stop the trigger if it has not requested the switchover, else wait for the RDS API to finish it. Always sets stop.""" + request: SwitchoverRequest) -> Optional[str]: + """Stop the trigger if it has not requested the switchover, else wait for the RDS API to finish it. Always sets stop. + + :param test_utility: reads the deployment from the RDS API. + :param bg_id: the Blue/Green deployment ID. + :param trigger_thread: the thread that requests the switchover. + :param stop: the stop signal of every test thread. + :param request: the trigger thread's switchover request. + :return: the deployment status the RDS API reported last. None when no switchover request succeeded, or when no + read found the deployment. + """ try: - if not switchover_requested.is_set(): + if not request.requested.is_set(): stop.set() - trigger_thread.join(TRIGGER_JOIN_SEC) - if switchover_requested.is_set(): - wait_for_bg_deployment_status( - test_utility, bg_id, switchover_requested_ns.get() + BG_API_WAIT_SEC * 1_000_000_000) + trigger_thread.join(BG_TRIGGER_JOIN_SEC) + if not request.requested.is_set() or request.error is not None: + return None + remaining_sec = BG_API_STATUS_WAIT_SEC - (perf_counter_ns() - request.requested_ns) / _NS_PER_SEC + return test_utility.wait_until_blue_green_deployment_has_desired_status( + bg_id, remaining_sec, "SWITCHOVER_COMPLETED", "SWITCHOVER_FAILED") finally: stop.set() - - -def summary_lines( - test_start_ns: int, - threads_start_ns: int, - switchover_requested_ns: int, - wait_result: str, - observation: PluginStatusObservation, - probes: Sequence[ProbeResult], - extra: Sequence[str] = ()) -> List[str]: - zero_ns = switchover_requested_ns if switchover_requested_ns > 0 else threads_start_ns - zero_name = "switchover request" if switchover_requested_ns > 0 else "thread start" - lines = [ - "#1352 checks summary", - f" test entry to thread start: {_sec(threads_start_ns - test_start_ns)}", - f" probe host: {observation.probe_host} ({observation.probe_host_choice or 'not chosen'})", - f" wait: {wait_result}", - f" observer error: {observation.error or '-'}", - ] - if observation.provider_monitors_running is not None: - lines.append(f" status provider monitors running at the end of the window: {observation.provider_monitors_running}") - lines.extend(f" {line}" for line in extra) - for phase in PHASE_ORDER: - seen_ns = observation.plugin_phase_first_seen_ns.get(phase) - offset = "not seen" if seen_ns is None else f"{_sec(seen_ns - zero_ns)} after {zero_name}" - lines.append(f" plugin phase {phase}: {offset}") - for probe in probes: - timing = "not started" if probe.start_ns == 0 else f"start {_sec(probe.start_ns - zero_ns)}, " + ( - "in flight" if probe.end_ns == 0 else f"end {_sec(probe.end_ns - zero_ns)}") - line = f" {probe.name}: host {probe.probe_host or '-'}, phase at start {probe.phase_at_start}, " - if probe.phase_at_end is not None: - line += f"phase at end {probe.phase_at_end}, " - line += (f"{timing}, hold {_sec(probe.hold_ns)}, thread CPU {probe.cpu_sec:.3f} s, " - f"outcome {probe.outcome or '-'}") - if probe.suspend_detail: - line += f", suspend listed {probe.suspend_listed} {probe.suspend_detail}" - if probe.routing_detail: - line += f", {probe.routing_detail}" - if probe.spinning is not None: - line += (f", spinning {probe.spinning}, sample CPU {probe.spin_cpu_delta_sec}, " - f"frames {probe.frames[0]} / {probe.frames[1]}") - lines.append(line) - for index, attempt in enumerate(probe.attempts, 1): - lines.append(f" attempt {index} in {attempt.phase}: substitute {attempt.substitute_host}, " - f"driver host {attempt.driver_host}, outcome {attempt.outcome or '-'}") - return lines diff --git a/tests/integration/container/utils/rds_test_utility.py b/tests/integration/container/utils/rds_test_utility.py index e42ed3736..e80590fd2 100644 --- a/tests/integration/container/utils/rds_test_utility.py +++ b/tests/integration/container/utils/rds_test_utility.py @@ -49,7 +49,8 @@ from aws_advanced_python_wrapper.utils.log import Logger from aws_advanced_python_wrapper.utils.messages import Messages from tests.integration.container.utils.test_timings import ( - CLUSTER_STATUS_TIMEOUT_SEC, FAILOVER_DEADLINE_SEC, FAILOVER_MAX_REISSUES, + BG_API_STATUS_POLL_INTERVAL_SEC, CLUSTER_STATUS_TIMEOUT_SEC, + FAILOVER_DEADLINE_SEC, FAILOVER_MAX_REISSUES, FAILOVER_REISSUE_MIN_REMAINING_SEC, INITIAL_WRITER_POLL_INTERVAL_SEC, INITIAL_WRITER_REVERT_WINDOW_SEC, INSTANCE_STATUS_POLL_INTERVAL_SEC, INSTANCE_UP_TIMEOUT_SEC, WRITER_CHANGE_TIMEOUT_SEC, @@ -838,6 +839,28 @@ def get_blue_green_deployment(self, bg_id: str) -> Optional[Any]: except self._client.exceptions.BlueGreenDeploymentNotFoundFault: return None + def wait_until_blue_green_deployment_has_desired_status( + self, bg_id: str, timeout_sec: float, *desired_statuses: str) -> Optional[str]: + """Wait until the Blue/Green deployment reaches one of the statuses, or the timeout passes. + + :param bg_id: the Blue/Green deployment ID. + :param timeout_sec: how long to wait, in seconds. + :param desired_statuses: the RDS API statuses to wait for, for example ``SWITCHOVER_COMPLETED``. + :return: the last status read, or None when no read found the deployment. + """ + stop_time = timeit.default_timer() + timeout_sec + status = None + while True: + try: + deployment = self.get_blue_green_deployment(bg_id) + if deployment is not None: + status = deployment.get("Status") + except Exception as e: + self.logger.debug(f"Blue/Green deployment {bg_id} status read failed: {e}") + if status in desired_statuses or timeit.default_timer() > stop_time: + return status + sleep(BG_API_STATUS_POLL_INTERVAL_SEC) + def switchover_blue_green_deployment(self, bg_id: str): try: self._client.switchover_blue_green_deployment(BlueGreenDeploymentIdentifier=bg_id) diff --git a/tests/integration/container/utils/test_timings.py b/tests/integration/container/utils/test_timings.py index 26d342859..ef184856b 100644 --- a/tests/integration/container/utils/test_timings.py +++ b/tests/integration/container/utils/test_timings.py @@ -122,3 +122,52 @@ # with extra topology-refresh polling, so 900 s is a sensible ceiling # that catches genuine hangs without hiding real timeouts. FAILOVER_WITHIN_TRANSACTION_PYTEST_TIMEOUT_SEC: int = 900 + +# ───── Blue/Green switchover tests ───────────────────────────────────── +# The plugin holds a call it suspends in IN_PROGRESS for at most +# ``bg_connect_timeout_ms`` (30 s by default) per pass, so a correct call +# returns within two of those. +BG_SUSPENDED_CALL_MAX_SEC: float = 60 +# A suspended call waits on the plugin's status in 50 ms steps, which uses +# far less of its thread's CPU than this. +BG_SUSPENDED_CALL_MAX_CPU_SEC: float = 1.0 +# The plugin publishes each phase for at least 1.7 s, so the test's status +# observer sees every phase at this poll rate. +BG_PLUGIN_STATUS_POLL_SEC: float = 0.02 +# How long a test thread sleeps between two checks of what it waits for. +BG_WAIT_STEP_SEC: float = 0.5 +# How long the sync test waits for the plugin to give the blue cluster +# endpoint its SOURCE role before it chooses the probes' host. +BG_PROBE_HOST_ROLE_WAIT_SEC: float = 20 +# Connect attempts, and the connect timeout of each, for a probe connection. +# POST lasts 23 to 26 s. +BG_PROBE_CONNECT_ATTEMPTS: int = 3 +BG_PROBE_CONNECT_TIMEOUT_SEC: int = 10 +# How long the async test waits for its probe to connect. That connection +# owns the status provider, so the other threads start after it. +BG_ASYNC_PROBE_CONNECT_WAIT_SEC: float = 30 +# Ends an async probe execute that never returns, past +# BG_SUSPENDED_CALL_MAX_SEC, so the probe's loop can stop the status provider. +BG_ASYNC_EXECUTE_TIMEOUT_SEC: float = 75 +# From thread start: the trigger waits for the other threads to be ready, +# then 30 s, before it requests the switchover. +BG_SWITCHOVER_REQUEST_WAIT_SEC: float = 150 +# From the request: the plugin published COMPLETED 41 to 45 s after it. +BG_SWITCHOVER_WAIT_SEC: float = 240 +# From the request: the RDS API reported SWITCHOVER_COMPLETED 68 to 73 s +# after it. +BG_API_STATUS_WAIT_SEC: float = 300 +BG_API_STATUS_POLL_INTERVAL_SEC: float = 5 +# How long the main thread waits for the trigger thread after it stopped +# waiting for the request. +BG_TRIGGER_JOIN_SEC: float = 5 +# One deadline for every test thread to stop. +BG_THREAD_JOIN_SEC: float = 20 +# The switchover tests' marker overrides the harness's 600 s ``--timeout``. +# pytest-timeout's thread method counts the setup hook as well. Worst case: +# setup + 30 s (async probe connect; the sync role wait is 20 s) + 150 s +# (latest request) + 300 s (RDS API wait from the request) + 20 s (thread +# stop) = setup + 500 s, so a run fits when setup takes under 400 s. Setup +# takes about a minute on a healthy cluster; its own waits allow up to +# 1500 s (cluster status 600 s, instances 300 s and 300 s, DNS 300 s). +BG_SWITCHOVER_PYTEST_TIMEOUT_SEC: int = 900 diff --git a/tests/integration/host/build.gradle.kts b/tests/integration/host/build.gradle.kts index c84e90f8e..1c77f68c4 100644 --- a/tests/integration/host/build.gradle.kts +++ b/tests/integration/host/build.gradle.kts @@ -544,6 +544,112 @@ tasks.register("test-bgd-pg-aurora") { } } +// One environment and one Blue/Green deployment each: a deployment switches over once. + +tasks.register("test-bgd-mysql-aurora-sync") { + group = "verification" + filter.includeTestsMatching("integration.host.TestRunner.runTests") + environment("FILTER", "test_switchover and not test_switchover_async") + doFirst { + systemProperty("exclude-docker", "true") + systemProperty("exclude-performance", "true") + systemProperty("exclude-pg-driver", "true") + systemProperty("exclude-pg-engine", "true") + systemProperty("exclude-multi-az-instance", "true") + systemProperty("exclude-failover", "true") + systemProperty("exclude-secrets-manager", "true") + systemProperty("exclude-instances-1", "true") + systemProperty("exclude-instances-3", "true") + systemProperty("exclude-instances-5", "true") + systemProperty("exclude-multi-az-cluster", "true") + systemProperty("exclude-python-3-10", "true") + systemProperty("exclude-python-3-11", "true") + systemProperty("exclude-python-3-13", "true") + systemProperty("exclude-python-3-14", "true") + systemProperty("exclude-traces-telemetry", "true") + systemProperty("exclude-metrics-telemetry", "true") + systemProperty("exclude-async-drivers", "true") + systemProperty("test-bg-only", "true") + } +} + +tasks.register("test-bgd-mysql-aurora-async") { + group = "verification" + filter.includeTestsMatching("integration.host.TestRunner.runTests") + environment("FILTER", "test_switchover_async") + doFirst { + systemProperty("exclude-docker", "true") + systemProperty("exclude-performance", "true") + systemProperty("exclude-pg-driver", "true") + systemProperty("exclude-pg-engine", "true") + systemProperty("exclude-multi-az-instance", "true") + systemProperty("exclude-failover", "true") + systemProperty("exclude-secrets-manager", "true") + systemProperty("exclude-instances-1", "true") + systemProperty("exclude-instances-3", "true") + systemProperty("exclude-instances-5", "true") + systemProperty("exclude-multi-az-cluster", "true") + systemProperty("exclude-python-3-10", "true") + systemProperty("exclude-python-3-11", "true") + systemProperty("exclude-python-3-13", "true") + systemProperty("exclude-python-3-14", "true") + systemProperty("exclude-traces-telemetry", "true") + systemProperty("exclude-metrics-telemetry", "true") + systemProperty("exclude-sync-drivers", "true") + systemProperty("test-bg-only", "true") + } +} + +tasks.register("test-bgd-pg-aurora-sync") { + group = "verification" + filter.includeTestsMatching("integration.host.TestRunner.runTests") + environment("FILTER", "test_switchover and not test_switchover_async") + doFirst { + systemProperty("exclude-docker", "true") + systemProperty("exclude-performance", "true") + systemProperty("exclude-mysql-driver", "true") + systemProperty("exclude-mysql-engine", "true") + systemProperty("exclude-multi-az-instance", "true") + systemProperty("exclude-failover", "true") + systemProperty("exclude-secrets-manager", "true") + systemProperty("exclude-instances-1", "true") + systemProperty("exclude-instances-3", "true") + systemProperty("exclude-instances-5", "true") + systemProperty("exclude-multi-az-cluster", "true") + systemProperty("exclude-python-3-10", "true") + systemProperty("exclude-python-3-11", "true") + systemProperty("exclude-python-3-13", "true") + systemProperty("exclude-python-3-14", "true") + systemProperty("exclude-async-drivers", "true") + systemProperty("test-bg-only", "true") + } +} + +tasks.register("test-bgd-pg-aurora-async") { + group = "verification" + filter.includeTestsMatching("integration.host.TestRunner.runTests") + environment("FILTER", "test_switchover_async") + doFirst { + systemProperty("exclude-docker", "true") + systemProperty("exclude-performance", "true") + systemProperty("exclude-mysql-driver", "true") + systemProperty("exclude-mysql-engine", "true") + systemProperty("exclude-multi-az-instance", "true") + systemProperty("exclude-failover", "true") + systemProperty("exclude-secrets-manager", "true") + systemProperty("exclude-instances-1", "true") + systemProperty("exclude-instances-3", "true") + systemProperty("exclude-instances-5", "true") + systemProperty("exclude-multi-az-cluster", "true") + systemProperty("exclude-python-3-10", "true") + systemProperty("exclude-python-3-11", "true") + systemProperty("exclude-python-3-13", "true") + systemProperty("exclude-python-3-14", "true") + systemProperty("exclude-sync-drivers", "true") + systemProperty("test-bg-only", "true") + } +} + // Debug tasks.register("debug-all-environments") { diff --git a/tests/integration/host/src/test/java/integration/host/TestEnvironment.java b/tests/integration/host/src/test/java/integration/host/TestEnvironment.java index 44d3fe4e5..313b4321f 100644 --- a/tests/integration/host/src/test/java/integration/host/TestEnvironment.java +++ b/tests/integration/host/src/test/java/integration/host/TestEnvironment.java @@ -591,11 +591,6 @@ private static void createDbCluster(TestEnvironment env, int numOfInstances) { env.auroraUtil.deleteCluster(env.rdsDbName, env.info.getRequest().getDatabaseEngineDeployment(), false); LOGGER.finer("Deleted cluster " + env.rdsDbName); - // Teardown never runs after a failed create, so the custom parameter group made for it goes here. - if (!StringUtils.isNullOrEmpty(env.info.getClusterParameterGroupName())) { - env.deleteCustomClusterParameterGroup(env.info.getClusterParameterGroupName()); - } - throw new RuntimeException(e); } } @@ -1360,14 +1355,8 @@ private void deleteBlueGreenDeployment() throws InterruptedException { return; } - // Settle an in-flight switchover: the status read below picks the cluster to delete. A deployment still - // switching after 15 minutes still stops the clean-up in deleteBlueGreenDeployment, as in JDBC. - try { - auroraUtil.waitUntilBlueGreenDeploymentHasRightState( - this.info.getBlueGreenDeploymentId(), "available", "switchover_completed", "switchover_failed"); - } catch (RuntimeException e) { - LOGGER.finest("BGD did not settle: " + e.getMessage()); - } + // Let an in-flight switchover settle first: the status read below picks the cluster to delete. + auroraUtil.waitUntilBlueGreenDeploymentSettled(this.info.getBlueGreenDeploymentId()); final BlueGreenDeployment settledDeployment = auroraUtil.getBlueGreenDeployment(this.info.getBlueGreenDeploymentId()); if (settledDeployment != null) { diff --git a/tests/integration/host/src/test/java/integration/util/AuroraTestUtility.java b/tests/integration/host/src/test/java/integration/util/AuroraTestUtility.java index 2bba3d415..dce86f4c0 100644 --- a/tests/integration/host/src/test/java/integration/util/AuroraTestUtility.java +++ b/tests/integration/host/src/test/java/integration/util/AuroraTestUtility.java @@ -918,7 +918,11 @@ public String getAuroraParameterGroupFamily(String engine, String engineVersion) } switch (engine) { case "aurora-postgresql": - return "aurora-postgresql16"; + if (StringUtils.isNullOrEmpty(engineVersion)) { + return "aurora-postgresql16"; + } + // The PostgreSQL families are named after the major version, for example "aurora-postgresql17". + return "aurora-postgresql" + engineVersion.split("\\.")[0]; case "aurora-mysql": if (StringUtils.isNullOrEmpty(engineVersion) || engineVersion.contains("8.0")) { return "aurora-mysql8.0"; @@ -1204,9 +1208,20 @@ public String createBlueGreenDeployment(String name, String sourceArn) { return blueGreenId; } + /** Waits until a switchover in flight has settled, so the deployment can be deleted. */ + public void waitUntilBlueGreenDeploymentSettled(String blueGreenId) { + waitUntilBlueGreenDeploymentHasRightState( + blueGreenId, "available", "switchover_completed", "switchover_failed", "invalid_configuration"); + } + public void waitUntilBlueGreenDeploymentHasRightState(String blueGreenId, String... allowedStatuses) { - String status = getBlueGreenDeployment(blueGreenId).status(); + BlueGreenDeployment deployment = getBlueGreenDeployment(blueGreenId); + if (deployment == null) { + LOGGER.finest("BGD " + blueGreenId + " not found."); + return; + } + String status = deployment.status(); LOGGER.finest("BGD status: " + status + ", waiting for status: " + String.join(", ", allowedStatuses)); final Set allowedStatusSet = Arrays.stream(allowedStatuses) .map(String::toLowerCase) @@ -1218,7 +1233,12 @@ public void waitUntilBlueGreenDeploymentHasRightState(String blueGreenId, String } catch (InterruptedException ex) { throw new RuntimeException(ex); } - String tmpStatus = getBlueGreenDeployment(blueGreenId).status(); + deployment = getBlueGreenDeployment(blueGreenId); + if (deployment == null) { + LOGGER.finest("BGD " + blueGreenId + " not found."); + return; + } + String tmpStatus = deployment.status(); if (!tmpStatus.equalsIgnoreCase(status)) { LOGGER.finest("BGD status (waiting): " + tmpStatus); } @@ -1274,7 +1294,7 @@ public void deleteBlueGreenDeployment(String blueGreenId, boolean waitForComplet return; } - waitUntilBlueGreenDeploymentHasRightState(blueGreenId, "available", "switchover_completed", "switchover_failed"); + waitUntilBlueGreenDeploymentSettled(blueGreenId); DeleteBlueGreenDeploymentResponse response = rdsClient.deleteBlueGreenDeployment( DeleteBlueGreenDeploymentRequest.builder() From 13d75b19fac0df2a045e41c4cd39c0de832a0a4c Mon Sep 17 00:00:00 2001 From: Juan Lee Date: Thu, 19 Nov 2026 23:36:59 -0700 Subject: [PATCH 3/4] fix: Blue/Green status monitor keeps the host name as the IAM host when it reconnects by IP --- .../aio/blue_green_plugin.py | 2 +- .../blue_green_plugin.py | 2 +- tests/unit/test_aio_blue_green_monitor.py | 21 +++++++++++++++- tests/unit/test_blue_green_plugin.py | 24 +++++++++++++++++-- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/aws_advanced_python_wrapper/aio/blue_green_plugin.py b/aws_advanced_python_wrapper/aio/blue_green_plugin.py index b1e53519a..7460cae37 100644 --- a/aws_advanced_python_wrapper/aio/blue_green_plugin.py +++ b/aws_advanced_python_wrapper/aio/blue_green_plugin.py @@ -961,7 +961,7 @@ async def _open_connection_task(self) -> None: ip_host_info = copy(host_info) ip_host_info.host = ip_address props_copy = copy(self._props) - WrapperProperties.IAM_HOST.set(props_copy, ip_host_info.host) + WrapperProperties.IAM_HOST.set(props_copy, host_info.host) logger.debug("BlueGreenStatusMonitor.OpeningConnectionWithIp", self._bg_role, ip_host_info.host) self._connection = await self._plugin_service.force_connect(ip_host_info, props_copy) diff --git a/aws_advanced_python_wrapper/blue_green_plugin.py b/aws_advanced_python_wrapper/blue_green_plugin.py index fb36b4aab..5fb8d7523 100644 --- a/aws_advanced_python_wrapper/blue_green_plugin.py +++ b/aws_advanced_python_wrapper/blue_green_plugin.py @@ -953,7 +953,7 @@ def _open_connection_task(self): ip_host_info = copy(host_info) ip_host_info.host = ip_address props_copy = copy(self._props) - WrapperProperties.IAM_HOST.set(props_copy, ip_host_info.host) + WrapperProperties.IAM_HOST.set(props_copy, host_info.host) logger.debug( "BlueGreenStatusMonitor.OpeningConnectionWithIp", self._bg_role, ip_host_info.host) diff --git a/tests/unit/test_aio_blue_green_monitor.py b/tests/unit/test_aio_blue_green_monitor.py index 47d23865d..748619282 100644 --- a/tests/unit/test_aio_blue_green_monitor.py +++ b/tests/unit/test_aio_blue_green_monitor.py @@ -42,11 +42,14 @@ from aws_advanced_python_wrapper.errors import AwsWrapperError from aws_advanced_python_wrapper.hostinfo import HostInfo, HostRole from aws_advanced_python_wrapper.utils.concurrent import ConcurrentDict -from aws_advanced_python_wrapper.utils.properties import Properties +from aws_advanced_python_wrapper.utils.iam_utils import IamAuthUtils +from aws_advanced_python_wrapper.utils.properties import (Properties, + WrapperProperties) from aws_advanced_python_wrapper.utils.value_container import ValueContainer # Realistic RDS endpoints so RdsUtils DNS heuristics fire as they would in prod. BLUE_WRITER = "bg-blue.cluster-abc123.us-east-2.rds.amazonaws.com" +BLUE_WRITER_IP = "10.0.1.10" BLUE_READER_CLUSTER = "bg-blue.cluster-ro-abc123.us-east-2.rds.amazonaws.com" GREEN_WRITER = "bg-green.cluster-abc123.us-east-2.rds.amazonaws.com" @@ -726,6 +729,22 @@ async def _body(): assert monitor.is_running() is False +def test_monitor_reconnect_by_ip_sets_iam_host_to_host_name() -> None: + svc = _mock_plugin_service() + monitor = AsyncBlueGreenStatusMonitor( + BlueGreenRole.SOURCE, "bg", HostInfo(BLUE_WRITER, 5432), svc, Properties(), {}, None) + monitor._connection_host_info = HostInfo(BLUE_WRITER, 5432) + monitor._connected_ip_address = BLUE_WRITER_IP + monitor.use_ip_address = True + + _run(monitor._open_connection_task()) + + host_info, props = svc.force_connect.call_args.args + assert host_info.host == BLUE_WRITER_IP + assert WrapperProperties.IAM_HOST.get(props) == BLUE_WRITER + assert IamAuthUtils.get_iam_host(props, host_info) == BLUE_WRITER + + def test_monitor_poll_hands_interim_to_processor(monkeypatch) -> None: published: List[BlueGreenInterimStatus] = [] diff --git a/tests/unit/test_blue_green_plugin.py b/tests/unit/test_blue_green_plugin.py index 3f8b1eeea..df4247aa1 100644 --- a/tests/unit/test_blue_green_plugin.py +++ b/tests/unit/test_blue_green_plugin.py @@ -18,12 +18,14 @@ import pytest from aws_advanced_python_wrapper.blue_green_plugin import ( - BlueGreenPhase, BlueGreenRole, BlueGreenStatus, SubstituteConnectRouting, - SuspendUntilCorrespondingHostFoundConnectRouting) + BlueGreenPhase, BlueGreenRole, BlueGreenStatus, BlueGreenStatusMonitor, + SubstituteConnectRouting, SuspendUntilCorrespondingHostFoundConnectRouting) +from aws_advanced_python_wrapper.database_dialect import BlueGreenDialect from aws_advanced_python_wrapper.federated_plugin import FederatedAuthPlugin from aws_advanced_python_wrapper.hostinfo import HostInfo from aws_advanced_python_wrapper.okta_plugin import OktaAuthPlugin from aws_advanced_python_wrapper.utils.concurrent import ConcurrentDict +from aws_advanced_python_wrapper.utils.iam_utils import IamAuthUtils from aws_advanced_python_wrapper.utils.messages import Messages from aws_advanced_python_wrapper.utils.properties import (Properties, WrapperProperties) @@ -31,6 +33,7 @@ BG_ID = "1" PORT = 3306 BLUE_HOST = "database-1.cluster-c1a2b3c4d5e6.us-east-1.rds.amazonaws.com" +BLUE_IP = "10.0.1.10" GREEN_HOST = "database-1-green-abc123.cluster-c1a2b3c4d5e6.us-east-1.rds.amazonaws.com" GREEN_IP = "10.0.1.20" @@ -62,6 +65,23 @@ def test_substitute_connect_routing_to_an_ip_sets_the_iam_host_for_federated_and assert WrapperProperties.IAM_HOST.get(rerouted_props) == GREEN_HOST +def test_status_monitor_reconnect_by_ip_sets_iam_host_to_host_name(): + plugin_service = MagicMock() + plugin_service.database_dialect = MagicMock(spec=BlueGreenDialect) + monitor = BlueGreenStatusMonitor( + BlueGreenRole.SOURCE, BG_ID, HostInfo(BLUE_HOST, PORT), plugin_service, Properties(), {}) + monitor._connection_host_info = HostInfo(BLUE_HOST, PORT) + monitor._connected_ip_address = BLUE_IP + monitor.use_ip_address.set() + + monitor._open_connection_task() + + host_info, props = plugin_service.force_connect.call_args.args + assert host_info.host == BLUE_IP + assert WrapperProperties.IAM_HOST.get(props) == BLUE_HOST + assert IamAuthUtils.get_iam_host(props, host_info) == BLUE_HOST + + def _corresponding_hosts() -> ConcurrentDict[str, Tuple[HostInfo, Optional[HostInfo]]]: corresponding_hosts: ConcurrentDict[str, Tuple[HostInfo, Optional[HostInfo]]] = ConcurrentDict() corresponding_hosts.put_all({BLUE_HOST: (HostInfo(BLUE_HOST, PORT), HostInfo(GREEN_HOST, PORT))}) From 03ff35042ea2f73041008d70a6915b843051a186 Mon Sep 17 00:00:00 2001 From: Juan Lee Date: Tue, 24 Nov 2026 13:28:11 -0700 Subject: [PATCH 4/4] ci: run the Blue/Green integration tests in their own workflow Move the blue-green-integration-tests job out of integration_tests.yml into blue_green_integration_tests.yml. The new workflow keeps the Integration Tests triggers (pushes to main, skipping docs-only changes, and workflow_dispatch), so the switchover tests still run on every merge to main. integration_tests.yml is the same as on main again. --- .../blue_green_integration_tests.yml | 77 +++++++++++++++++++ .github/workflows/integration_tests.yml | 53 ------------- 2 files changed, 77 insertions(+), 53 deletions(-) create mode 100644 .github/workflows/blue_green_integration_tests.yml diff --git a/.github/workflows/blue_green_integration_tests.yml b/.github/workflows/blue_green_integration_tests.yml new file mode 100644 index 000000000..b8b0bc8d0 --- /dev/null +++ b/.github/workflows/blue_green_integration_tests.yml @@ -0,0 +1,77 @@ +name: Blue/Green Integration Tests + +on: + workflow_dispatch: + push: + branches: + - main + paths-ignore: + - '**/*.md' + - '**/*.jpg' + - '**/README.txt' + - '**/LICENSE.txt' + - 'docs/**' + - 'ISSUE_TEMPLATE/**' + - '**/remove-old-artifacts.yml' + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +permissions: + id-token: write # This is required for requesting the JWT + contents: read # This is required for actions/checkout + +jobs: + blue-green-integration-tests: + name: Run Blue/Green Integration Tests + runs-on: ubuntu-latest + timeout-minutes: 120 + strategy: + fail-fast: false + max-parallel: 2 + matrix: + task: [ "mysql-aurora-sync", "mysql-aurora-async", "pg-aurora-sync", "pg-aurora-async" ] + + steps: + - name: 'Clone repository' + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + + - name: 'Set up JDK 8' + uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5 + with: + distribution: 'corretto' + java-version: 8 + + - name: Install poetry + shell: bash + run: | + pipx install poetry==1.8.2 + poetry config virtualenvs.prefer-active-python true + + - name: Install dependencies + run: poetry install + + - name: 'Configure AWS Credentials' + uses: aws-actions/configure-aws-credentials@d979d5b3a71173a29b74b5b88418bfda9437d885 # v6 + with: + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.AWS_DEPLOY_ROLE }} + role-session-name: python_bg_integration_tests + role-duration-seconds: 21600 + aws-region: ${{ secrets.AWS_DEFAULT_REGION }} + + - name: 'Run Blue/Green Integration Tests' + run: | + ./gradlew --no-parallel --no-daemon test-bgd-${{ matrix.task }} --info + env: + RDS_DB_REGION: ${{ secrets.AWS_DEFAULT_REGION }} + MYSQL_VERSION: default + PG_VERSION: default + + - name: 'Archive Blue/Green results' + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: pytest-integration-report-bgd-${{ matrix.task }} + path: ./tests/integration/container/reports + retention-days: 5 diff --git a/.github/workflows/integration_tests.yml b/.github/workflows/integration_tests.yml index b0b2f5513..76519f84d 100644 --- a/.github/workflows/integration_tests.yml +++ b/.github/workflows/integration_tests.yml @@ -76,59 +76,6 @@ jobs: path: ./tests/integration/container/reports retention-days: 5 - blue-green-integration-tests: - name: Run Blue/Green Integration Tests - runs-on: ubuntu-latest - timeout-minutes: 120 - strategy: - fail-fast: false - max-parallel: 2 - matrix: - task: [ "mysql-aurora-sync", "mysql-aurora-async", "pg-aurora-sync", "pg-aurora-async" ] - - steps: - - name: 'Clone repository' - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - - - name: 'Set up JDK 8' - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5 - with: - distribution: 'corretto' - java-version: 8 - - - name: Install poetry - shell: bash - run: | - pipx install poetry==1.8.2 - poetry config virtualenvs.prefer-active-python true - - - name: Install dependencies - run: poetry install - - - name: 'Configure AWS Credentials' - uses: aws-actions/configure-aws-credentials@d979d5b3a71173a29b74b5b88418bfda9437d885 # v6 - with: - role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.AWS_DEPLOY_ROLE }} - role-session-name: python_bg_integration_tests - role-duration-seconds: 21600 - aws-region: ${{ secrets.AWS_DEFAULT_REGION }} - - - name: 'Run Blue/Green Integration Tests' - run: | - ./gradlew --no-parallel --no-daemon test-bgd-${{ matrix.task }} --info - env: - RDS_DB_REGION: ${{ secrets.AWS_DEFAULT_REGION }} - MYSQL_VERSION: default - PG_VERSION: default - - - name: 'Archive Blue/Green results' - if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: pytest-integration-report-bgd-${{ matrix.task }} - path: ./tests/integration/container/reports - retention-days: 5 - latest-integration-tests: name: Run Latest Integration Tests runs-on: ubuntu-latest