Skip to content

Commit dc113cf

Browse files
committed
fix: Flush pool as late as possible during schema cache reloading
retryingSchemaCacheLoad flushes the pool upon every retry before it starts reloading the schema. This is too early as schema reloading might take some time during which new connections might be acquired. The consequence is that: * upon successful schema cache reload we might have some connections created with the old schema cache * we close connections upon each retry and under load we will keep closing and re-opening connections until schema cache load succeeds This change is to make sure we flush the pool only after successful schema cache querying but before loading (so that connections acquired during loading wait for it and do not interfere with timing the loading process).
1 parent 42842e3 commit dc113cf

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/PostgREST/AppState.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,6 @@ retryingSchemaCacheLoad appState@AppState{stateObserver=observer, stateMainThrea
315315
let delay = fromMaybe 0 rsPreviousDelay `div` oneSecondInUs
316316
observer $ ConnectionRetryObs delay
317317

318-
flushPool appState
319-
320318
(,) <$> qPgVersion <*> (qInDbConfig *> qSchemaCache)
321319
)
322320
where
@@ -365,6 +363,10 @@ retryingSchemaCacheLoad appState@AppState{stateObserver=observer, stateMainThrea
365363
-- IORef on putSchemaCache. This is why schema cache status is marked as pending here to signal the Admin server (using isPending) that we're on a recovery state.
366364
markSchemaCachePending appState
367365
putSchemaCache appState $ Just sCache
366+
-- Flush the pool after loading the schema cache to reset any stale session cache entries
367+
-- We do it after successfully querying the schema cache
368+
-- and after marking sCacheStatus as pending,
369+
flushPool appState
368370
observer $ SchemaCacheQueriedObs resultTime
369371
observer . uncurry SchemaCacheLoadedObs =<< timeItT (evaluate $ showSummary sCache)
370372
markSchemaCacheLoaded appState

test/io/test_io.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ def test_log_level(level, defaultenv):
781781
response = postgrest.session.get("/")
782782
assert response.status_code == 200
783783

784-
output = postgrest.read_stdout(nlines=7)
784+
output = postgrest.read_stdout(nlines=9)
785785

786786
if level == "crit":
787787
assert len(output) == 0
@@ -819,7 +819,7 @@ def test_log_level(level, defaultenv):
819819
r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 \d+ "" "python-requests/.+"',
820820
],
821821
)
822-
assert len(output) == 7
822+
assert len(output) == 9
823823
assert any("Connection" and "is available" in line for line in output)
824824
assert any("Connection" and "is used" in line for line in output)
825825

@@ -1456,7 +1456,7 @@ def test_db_error_logging_to_stderr(level, defaultenv, metapostgrest):
14561456
assert response.status_code == 500
14571457

14581458
# ensure the message appears on the logs
1459-
output = postgrest.read_stdout(nlines=6)
1459+
output = postgrest.read_stdout(nlines=8)
14601460

14611461
if level == "crit":
14621462
assert len(output) == 0
@@ -1673,7 +1673,7 @@ def test_log_pool_req_observation(level, defaultenv):
16731673

16741674
if level == "debug":
16751675
output = postgrest.read_stdout(nlines=7)
1676-
assert len(output) == 6
1676+
assert len(output) == 7
16771677
match_log(output, [pool_req, pool_req_fullfill])
16781678
elif level == "info":
16791679
output = postgrest.read_stdout(nlines=4)

test/observability/Observation/SchemaCacheSpec.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ spec = describe "Server started with metrics enabled" $ do
2727
waitFor (1 * sec) "SchemaCacheLoadedObs" $ \x -> [ o | o@SchemaCacheLoadedObs{} <- pure x ]
2828

2929

30-
it "Should flush pool multiple times when schema reloading retries" $ do
30+
it "Should flush pool once when schema reloading retries" $ do
3131
SpecState{specAppState = appState, specObsChan} <- getState
3232
let waitFor = waitForObs specObsChan
3333

@@ -36,16 +36,14 @@ spec = describe "Server started with metrics enabled" $ do
3636
AppState.putConfig appState $ cfg { configDbSchemas = pure "bad_schema" }
3737
AppState.schemaCacheLoader appState
3838

39-
waitFor (1 * sec) "PoolFlushed 1" $ \x -> [ o | o@PoolFlushed <- pure x ]
4039
waitFor (1 * sec) "SchemaCacheErrorObs" $ \x -> [ o | o@SchemaCacheErrorObs{} <- pure x ]
4140

4241
-- Restore configuration
4342
AppState.putConfig appState cfg
4443

4544
-- Wait for 2 seconds so that retry can happen
46-
waitFor (2 * sec) "PoolFlushed 2" $ \x -> [ o | o@PoolFlushed <- pure x ]
45+
waitFor (2 * sec) "PoolFlushed" $ \x -> [ o | o@PoolFlushed <- pure x ]
4746
waitFor (1 * sec) "SchemaCacheQueriedObs" $ \x -> [ o | o@SchemaCacheQueriedObs{} <- pure x ]
4847
waitFor (1 * sec) "SchemaCacheLoadedObs" $ \x -> [ o | o@SchemaCacheLoadedObs{} <- pure x ]
49-
5048
where
5149
sec = 1000000

0 commit comments

Comments
 (0)