fix: listener silent fail on replica

Update hasql-notifications to include the fix on
https://github.com/diogob/hasql-notifications/issues/24.

Which now reveals the following error:

```
$ postgrest-with-postgresql-16 --replica -f test/spec/fixtures/load.sql postgrest-run

17/May/2024:18:35:38 -0500: Successfully connected to PostgreSQL 16.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 13.2.0, 64-bit
17/May/2024:18:35:38 -0500: Could not listen for notifications on the "pgrst" channel. ERROR:  cannot execute LISTEN during recovery
17/May/2024:18:35:38 -0500: Retrying listening for notifications...
```

This is still not good because the LISTEN channel will be retried
forever without a backoff.
This commit is contained in:
steve-chavez
2024-05-18 23:33:04 -05:00
committed by Steve Chavez
parent aa75412932
commit 756aad7827
8 changed files with 32 additions and 17 deletions
+1
View File
@@ -37,6 +37,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #3424, Admin `/live` and `/ready` now differentiates a failure as 500 status - @steve-chavez - #3424, Admin `/live` and `/ready` now differentiates a failure as 500 status - @steve-chavez
+ 503 status is still given when postgREST is in a recovering state + 503 status is still given when postgREST is in a recovering state
- #3478, Media Types are parsed case insensitively - @develop7 - #3478, Media Types are parsed case insensitively - @develop7
- #2781, Fix listener silently failing on read replica - @steve-chavez
### Deprecated ### Deprecated
+1 -1
View File
@@ -1 +1 @@
index-state: hackage.haskell.org 2024-04-15T20:28:44Z index-state: hackage.haskell.org 2024-05-17T23:41:49Z
+3 -3
View File
@@ -38,7 +38,7 @@ let
# Notes: # Notes:
# - When adding a new package version here, update cabal. # - When adding a new package version here, update cabal.
# + Update postgrest.cabal with the package version # + Update postgrest.cabal with the package version
# + Update cabal.project.freeze. Just set it to the current timestamp then run `cabal build`. It will tell you the correct timestamp for the index state. # + Update the index-state in cabal.project.freeze. Run `cabal update` which should return the latest index state.
# - When adding a new package version here, you have to update stack. # - When adding a new package version here, you have to update stack.
# + To update stack.yaml add: # + To update stack.yaml add:
# extra-deps: # extra-deps:
@@ -67,8 +67,8 @@ let
hasql-notifications = lib.dontCheck (prev.callHackageDirect hasql-notifications = lib.dontCheck (prev.callHackageDirect
{ {
pkg = "hasql-notifications"; pkg = "hasql-notifications";
ver = "0.2.1.1"; ver = "0.2.2.0";
sha256 = "sha256-oPhKA/pSQGJvgQyhsi7CVr9iDT7uWpKUz0iJfXsaxXo="; sha256 = "sha256-73OQ9/su2qvO7HavF3xuuNWLXSXyB9reBUQDaHys06I=";
} }
{ } { }
); );
+1 -1
View File
@@ -109,7 +109,7 @@ library
, gitrev >= 1.2 && < 1.4 , gitrev >= 1.2 && < 1.4
, hasql >= 1.6.1.1 && < 1.7 , hasql >= 1.6.1.1 && < 1.7
, hasql-dynamic-statements >= 0.3.1 && < 0.4 , hasql-dynamic-statements >= 0.3.1 && < 0.4
, hasql-notifications >= 0.2.1.1 && < 0.3 , hasql-notifications >= 0.2.2.0 && < 0.3
, hasql-pool >= 1.0.1 && < 1.1 , hasql-pool >= 1.0.1 && < 1.1
, hasql-transaction >= 1.0.1 && < 1.1 , hasql-transaction >= 1.0.1 && < 1.1
, heredoc >= 0.2 && < 0.3 , heredoc >= 0.2 && < 0.3
+6 -4
View File
@@ -542,23 +542,25 @@ listener appState@AppState{stateObserver=observer, stateMainThreadId=mainThreadI
dbOrError <- acquire $ toUtf8 (addFallbackAppName prettyVersion configDbUri) dbOrError <- acquire $ toUtf8 (addFallbackAppName prettyVersion configDbUri)
case dbOrError of case dbOrError of
Right db -> do Right db -> do
observer $ DBListenerStart dbChannel
SQL.listen db $ SQL.toPgIdentifier dbChannel SQL.listen db $ SQL.toPgIdentifier dbChannel
observer $ DBListenStart dbChannel
SQL.waitForNotifications handleNotification db SQL.waitForNotifications handleNotification db
Left err -> do Left err -> do
observer $ DBListenerFail dbChannel err observer $ DBListenFail dbChannel (Left err)
exitFailure exitFailure
where where
handleFinally dbChannel False err = do handleFinally dbChannel False err = do
observer $ DBListenerFailRecoverObs False dbChannel err observer $ DBListenFail dbChannel (Right err)
killThread mainThreadId killThread mainThreadId
handleFinally dbChannel True err = do handleFinally dbChannel True err = do
-- if the thread dies, we try to recover -- if the thread dies, we try to recover
observer $ DBListenerFailRecoverObs True dbChannel err observer $ DBListenFail dbChannel (Right err)
-- assume the pool connection was also lost, call the connection worker -- assume the pool connection was also lost, call the connection worker
connectionWorker appState connectionWorker appState
-- retry the listener -- retry the listener
observer DBListenRetry
listener appState conf listener appState conf
handleNotification channel msg = handleNotification channel msg =
+12 -8
View File
@@ -40,9 +40,9 @@ data Observation
| SchemaCacheLoadedObs Double | SchemaCacheLoadedObs Double
| ConnectionRetryObs Int | ConnectionRetryObs Int
| ConnectionPgVersionErrorObs SQL.UsageError | ConnectionPgVersionErrorObs SQL.UsageError
| DBListenerStart Text | DBListenStart Text
| DBListenerFail Text SQL.ConnectionError | DBListenFail Text (Either SQL.ConnectionError (Either SomeException ()))
| DBListenerFailRecoverObs Bool Text (Either SomeException ()) | DBListenRetry
| DBListenerGotSCacheMsg ByteString | DBListenerGotSCacheMsg ByteString
| DBListenerGotConfigMsg ByteString | DBListenerGotConfigMsg ByteString
| ConfigReadErrorObs SQL.UsageError | ConfigReadErrorObs SQL.UsageError
@@ -97,12 +97,16 @@ observationMessage = \case
"Attempting to reconnect to the database in " <> (show delay::Text) <> " seconds..." "Attempting to reconnect to the database in " <> (show delay::Text) <> " seconds..."
ConnectionPgVersionErrorObs usageErr -> ConnectionPgVersionErrorObs usageErr ->
jsonMessage usageErr jsonMessage usageErr
DBListenerStart channel -> do DBListenStart channel -> do
"Listening for notifications on the " <> show channel <> " channel" "Listening for notifications on the " <> show channel <> " channel"
DBListenerFail channel err -> do DBListenFail channel listenErr ->
"Could not listen for notifications on the " <> channel <> " channel. " <> show err "Failed listening for notifications on the " <> show channel <> " channel. " <> (
DBListenerFailRecoverObs recover channel err -> case listenErr of
"Could not listen for notifications on the " <> channel <> " channel. " <> showListenerError err <> (if recover then " Retrying listening for notifications.." else mempty) Left err -> show err
Right err -> showListenerError err
)
DBListenRetry ->
"Retrying listening for notifications..."
DBListenerGotSCacheMsg channel -> DBListenerGotSCacheMsg channel ->
"Received a schema cache reload message on the " <> show channel <> " channel" "Received a schema cache reload message on the " <> show channel <> " channel"
DBListenerGotConfigMsg channel -> DBListenerGotConfigMsg channel ->
+1
View File
@@ -11,4 +11,5 @@ nix:
extra-deps: extra-deps:
- fuzzyset-0.2.4 - fuzzyset-0.2.4
- hasql-notifications-0.2.2.0
- hasql-pool-1.0.1 - hasql-pool-1.0.1
+7
View File
@@ -11,6 +11,13 @@ packages:
size: 574 size: 574
original: original:
hackage: fuzzyset-0.2.4 hackage: fuzzyset-0.2.4
- completed:
hackage: hasql-notifications-0.2.2.0@sha256:a4e591ef3f06647b056567d3b66948c4a85371f05deb5434edb6ce190f7c845d,2021
pantry-tree:
sha256: bd7192a5e82ef6dbac711c3433408a0330c8db1cd3482be1ccd4fbd0a63bc2f6
size: 452
original:
hackage: hasql-notifications-0.2.2.0
- completed: - completed:
hackage: hasql-pool-1.0.1@sha256:3cfb4c7153a6c536ac7e126c17723e6d26ee03794954deed2d72bcc826d05a40,2302 hackage: hasql-pool-1.0.1@sha256:3cfb4c7153a6c536ac7e126c17723e6d26ee03794954deed2d72bcc826d05a40,2302
pantry-tree: pantry-tree: