From 463974e8be36556d36ea4fb9858deff4d618bc06 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 25 Oct 2026 19:46:49 +0100 Subject: [PATCH] perf(svc-db-postgres): wait for TCP auth once per postgres container, not per consumer Every database consumer resolved the postgres container and waited for TCP auth again, although the container was created once in 00_core with a handler flush and is not restarted later in the play. 01_init now includes the resolve-and-wait block (moved unchanged to 01_init/ready.yml) only when the service|container|port target differs from the last verified one; a container-manager-restart handler forgets the target so a Docker daemon restart forces the wait again. About 67 s per full production run. Co-Authored-By: Claude Opus 5.5 (1M context) --- roles/svc-db-postgres/defaults/main.yml | 1 + roles/svc-db-postgres/handlers/main.yml | 6 +++ roles/svc-db-postgres/tasks/01_init.yml | 46 +++++-------------- roles/svc-db-postgres/tasks/01_init/ready.yml | 42 +++++++++++++++++ 4 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 roles/svc-db-postgres/handlers/main.yml create mode 100644 roles/svc-db-postgres/tasks/01_init/ready.yml diff --git a/roles/svc-db-postgres/defaults/main.yml b/roles/svc-db-postgres/defaults/main.yml index 51edfa273f..040788b4cf 100644 --- a/roles/svc-db-postgres/defaults/main.yml +++ b/roles/svc-db-postgres/defaults/main.yml @@ -1,3 +1,4 @@ --- database_init: false # When true a database is initialized database_consumer_id: "svc-db-postgres" # Self consumer +postgres_verified_target: "" diff --git a/roles/svc-db-postgres/handlers/main.yml b/roles/svc-db-postgres/handlers/main.yml new file mode 100644 index 0000000000..d9a0208a61 --- /dev/null +++ b/roles/svc-db-postgres/handlers/main.yml @@ -0,0 +1,6 @@ +--- +- name: 🧊 Forget the Postgres container whose TCP auth was verified + ansible.builtin.set_fact: + postgres_verified_target: "" + changed_when: false + listen: container-manager-restart diff --git a/roles/svc-db-postgres/tasks/01_init.yml b/roles/svc-db-postgres/tasks/01_init.yml index ad258a13c8..c281d099fb 100644 --- a/roles/svc-db-postgres/tasks/01_init.yml +++ b/roles/svc-db-postgres/tasks/01_init.yml @@ -12,42 +12,18 @@ changed_when: false no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" -- name: "🔍 Postgres | Resolve hosting node + container id" - ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/sys-svc-compose/tasks/utils/swarm/resolve_host_cid.yml') }}" +- name: "🔍 Postgres | Resolve the container and wait for TCP auth once per container" + ansible.builtin.include_tasks: 01_init/ready.yml + when: postgres_verified_target != postgres_target vars: - resolve_service_key: "{{ entity_name }}" - resolve_service_name: "{{ lookup('database', database_consumer_id, 'service_name') }}" - resolve_output_prefix: POSTGRES - resolve_compose_cid: "{{ lookup('database', database_consumer_id, 'container') }}" - -- name: ⏳ Wait until postgres TCP auth works - block: - - name: ⏳ Wait for Postgres TCP auth - no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" - ansible.builtin.shell: > # nocheck: container-exec-resolver POSTGRES_LOCAL_CID resolved at delegate_to runtime - container exec -e PGPASSWORD={{ POSTGRES_PASSWORD | quote }} {{ POSTGRES_LOCAL_CID | quote }} sh -lc - 'psql -h "{{ POSTGRES_LOCAL_HOST }}" -p "{{ POSTGRES_PORT }}" - -U postgres -d postgres -c "SELECT 1"' - delegate_to: "{{ POSTGRES_HOST_NODE }}" - register: pg_ready - until: pg_ready.rc == 0 - retries: 30 - delay: 5 - changed_when: false - - rescue: - - name: "💾 Postgres | Dump pg_hba.conf into the diagnostics folder" # nocheck: rescue-extra-diagnostics pg_hba.conf is postgres-specific auth config the generic rescue script cannot capture, and it is what a failed TCP-auth wait needs - ansible.builtin.script: "{{ lookup('path_absolute', 'roles/svc-db-postgres/files/shell/dump_pg_hba.sh') }}" - environment: - POSTGRES_CONTAINER_ADDRESS: "{{ POSTGRES_LOCAL_CID }}" - PGPASSWORD: "{{ POSTGRES_PASSWORD }}" - delegate_to: "{{ POSTGRES_HOST_NODE }}" - changed_when: false - failed_when: false - no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" - - name: "🛑 Postgres | fail after pg_hba dump" - ansible.builtin.fail: - msg: "Postgres TCP auth never became ready; pg_hba.conf was dumped into the diagnostics folder (the workflow-level rescue capture collects the full snapshot)" + postgres_target: >- + {{ + [ + lookup('database', database_consumer_id, 'service_name'), + lookup('database', database_consumer_id, 'container'), + POSTGRES_PORT + ] | join('|') + }} - name: "📁 Create database: {{ database_name }}" community.postgresql.postgresql_db: diff --git a/roles/svc-db-postgres/tasks/01_init/ready.yml b/roles/svc-db-postgres/tasks/01_init/ready.yml new file mode 100644 index 0000000000..9226b230e6 --- /dev/null +++ b/roles/svc-db-postgres/tasks/01_init/ready.yml @@ -0,0 +1,42 @@ +--- +- name: "🔍 Postgres | Resolve hosting node + container id" + ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/sys-svc-compose/tasks/utils/swarm/resolve_host_cid.yml') }}" + vars: + resolve_service_key: "{{ entity_name }}" + resolve_service_name: "{{ lookup('database', database_consumer_id, 'service_name') }}" + resolve_output_prefix: POSTGRES + resolve_compose_cid: "{{ lookup('database', database_consumer_id, 'container') }}" + +- name: ⏳ Wait until postgres TCP auth works + block: + - name: ⏳ Wait for Postgres TCP auth + no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" + ansible.builtin.shell: > # nocheck: container-exec-resolver POSTGRES_LOCAL_CID resolved at delegate_to runtime + container exec -e PGPASSWORD={{ POSTGRES_PASSWORD | quote }} {{ POSTGRES_LOCAL_CID | quote }} sh -lc + 'psql -h "{{ POSTGRES_LOCAL_HOST }}" -p "{{ POSTGRES_PORT }}" + -U postgres -d postgres -c "SELECT 1"' + delegate_to: "{{ POSTGRES_HOST_NODE }}" + register: pg_ready + until: pg_ready.rc == 0 + retries: 30 + delay: 5 + changed_when: false + + rescue: + - name: "💾 Postgres | Dump pg_hba.conf into the diagnostics folder" # nocheck: rescue-extra-diagnostics pg_hba.conf is postgres-specific auth config the generic rescue script cannot capture, and it is what a failed TCP-auth wait needs + ansible.builtin.script: "{{ lookup('path_absolute', 'roles/svc-db-postgres/files/shell/dump_pg_hba.sh') }}" + environment: + POSTGRES_CONTAINER_ADDRESS: "{{ POSTGRES_LOCAL_CID }}" + PGPASSWORD: "{{ POSTGRES_PASSWORD }}" + delegate_to: "{{ POSTGRES_HOST_NODE }}" + changed_when: false + failed_when: false + no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" + - name: "🛑 Postgres | fail after pg_hba dump" + ansible.builtin.fail: + msg: "Postgres TCP auth never became ready; pg_hba.conf was dumped into the diagnostics folder (the workflow-level rescue capture collects the full snapshot)" + +- name: "🧊 Postgres | Remember the container whose TCP auth works" + ansible.builtin.set_fact: + postgres_verified_target: "{{ postgres_target }}" + changed_when: false