|
18 | 18 | sleep_until_postgrest_full_reload, |
19 | 19 | sleep_until_postgrest_scache_reload, |
20 | 20 | wait_until_exit, |
| 21 | + wait_until_status_code, |
21 | 22 | ) |
22 | 23 |
|
23 | 24 |
|
@@ -105,6 +106,88 @@ def sleep(): |
105 | 106 | t.join() |
106 | 107 |
|
107 | 108 |
|
| 109 | +def test_pool_flushes_metric(defaultenv): |
| 110 | + "Should increase pool flushes metric when the pool is flushed" |
| 111 | + |
| 112 | + with run(env=defaultenv, port=freeport()) as postgrest: |
| 113 | + response = postgrest.admin.get("/metrics") |
| 114 | + assert response.status_code == 200 |
| 115 | + before = float( |
| 116 | + re.search(r"pgrst_db_pool_flushes_total ([0-9.e+-]+)", response.text).group( |
| 117 | + 1 |
| 118 | + ) |
| 119 | + ) |
| 120 | + |
| 121 | + postgrest.process.send_signal(signal.SIGUSR1) |
| 122 | + sleep_until_postgrest_scache_reload() |
| 123 | + |
| 124 | + response = postgrest.admin.get("/metrics") |
| 125 | + assert response.status_code == 200 |
| 126 | + after = float( |
| 127 | + re.search(r"pgrst_db_pool_flushes_total ([0-9.e+-]+)", response.text).group( |
| 128 | + 1 |
| 129 | + ) |
| 130 | + ) |
| 131 | + assert after == before + 1 |
| 132 | + |
| 133 | + |
| 134 | +def test_pool_flushes_metric_with_schema_cache_retries(defaultenv, metapostgrest): |
| 135 | + "Should flush the pool exactly once even when schema cache reload retries" |
| 136 | + |
| 137 | + role = "timeout_authenticator" |
| 138 | + app_name = "pool-flush-retry" |
| 139 | + env = { |
| 140 | + **defaultenv, |
| 141 | + "PGUSER": role, |
| 142 | + "PGAPPNAME": app_name, |
| 143 | + "PGRST_INTERNAL_SCHEMA_CACHE_QUERY_SLEEP": "50", |
| 144 | + } |
| 145 | + |
| 146 | + with run(env=env, port=freeport()) as postgrest: |
| 147 | + response = postgrest.admin.get("/metrics") |
| 148 | + assert response.status_code == 200 |
| 149 | + before = float( |
| 150 | + re.search(r"pgrst_db_pool_flushes_total ([0-9.e+-]+)", response.text).group( |
| 151 | + 1 |
| 152 | + ) |
| 153 | + ) |
| 154 | + |
| 155 | + try: |
| 156 | + # Force schema cache reload failures to trigger retries. |
| 157 | + set_statement_timeout(metapostgrest, role, 20) |
| 158 | + postgrest.process.send_signal(signal.SIGUSR1) |
| 159 | + |
| 160 | + postgrest.wait_until_scache_starts_loading(max_seconds=2) |
| 161 | + |
| 162 | + # Give retry loop time to run and verify it doesn't flush the pool. |
| 163 | + time.sleep(1) |
| 164 | + |
| 165 | + response = postgrest.admin.get("/metrics") |
| 166 | + assert response.status_code == 200 |
| 167 | + during_retries = float( |
| 168 | + re.search( |
| 169 | + r"pgrst_db_pool_flushes_total ([0-9.e+-]+)", response.text |
| 170 | + ).group(1) |
| 171 | + ) |
| 172 | + assert during_retries == before |
| 173 | + |
| 174 | + reset_statement_timeout(metapostgrest, role) |
| 175 | + # Ensure next retry establishes fresh sessions with the reset timeout. |
| 176 | + metapostgrest.session.get(f"/rpc/terminate_pgrst?appname={app_name}") |
| 177 | + wait_until_status_code(postgrest.admin.baseurl + "/ready", 12, 200) |
| 178 | + finally: |
| 179 | + reset_statement_timeout(metapostgrest, role) |
| 180 | + |
| 181 | + response = postgrest.admin.get("/metrics") |
| 182 | + assert response.status_code == 200 |
| 183 | + after = float( |
| 184 | + re.search(r"pgrst_db_pool_flushes_total ([0-9.e+-]+)", response.text).group( |
| 185 | + 1 |
| 186 | + ) |
| 187 | + ) |
| 188 | + assert after == before + 1 |
| 189 | + |
| 190 | + |
108 | 191 | def test_random_port_bound(defaultenv): |
109 | 192 | "PostgREST should bind to a random port when PGRST_SERVER_PORT is 0." |
110 | 193 |
|
@@ -1458,6 +1541,7 @@ def test_admin_metrics(defaultenv): |
1458 | 1541 | assert "pgrst_db_pool_waiting" in response.text |
1459 | 1542 | assert "pgrst_db_pool_available" in response.text |
1460 | 1543 | assert "pgrst_db_pool_timeouts_total" in response.text |
| 1544 | + assert "pgrst_db_pool_flushes_total" in response.text |
1461 | 1545 |
|
1462 | 1546 |
|
1463 | 1547 | def test_schema_cache_startup_load_with_in_db_config(defaultenv, metapostgrest): |
|
0 commit comments