-
Notifications
You must be signed in to change notification settings - Fork 332
retry Redis connection in case of network errors #2512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
111eaa0
retry Redis connection in case of network errors
stefanwire 24b92c4
add changelog
stefanwire 8e53e7a
ormolu and other changes
stefanwire 02b02a8
rename reconnectingRedis to robustClient
stefanwire 23f2fce
use unlessM instead of if success
stefanwire fc694ce
replace 'maybe (pure ())' by whenJust
stefanwire 6d241bf
incorporate review comments
stefanwire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| retry gundeck's Redis connection in case of network errors such as IP changes or network outages |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| {-# LANGUAGE OverloadedStrings #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
| {-# LANGUAGE TemplateHaskell #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
|
|
||
| -- This file is part of the Wire Server implementation. | ||
| -- | ||
| -- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com> | ||
| -- | ||
| -- This program is free software: you can redistribute it and/or modify it under | ||
| -- the terms of the GNU Affero General Public License as published by the Free | ||
| -- Software Foundation, either version 3 of the License, or (at your option) any | ||
| -- later version. | ||
| -- | ||
| -- This program is distributed in the hope that it will be useful, but WITHOUT | ||
| -- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| -- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
| -- details. | ||
| -- | ||
| -- You should have received a copy of the GNU Affero General Public License along | ||
| -- with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| module Gundeck.Redis | ||
| ( RobustConnection, | ||
| rrConnection, | ||
| rrReconnect, | ||
| connectRobust, | ||
| runRobust, | ||
| PingException, | ||
| ) | ||
| where | ||
|
|
||
| import Control.Concurrent.Extra (once) | ||
| import Control.Lens | ||
| import qualified Control.Monad.Catch as Catch | ||
| import Control.Retry | ||
| import Database.Redis | ||
| import Imports | ||
| import qualified System.Logger as Log | ||
| import System.Logger.Extended | ||
| import UnliftIO.Exception | ||
|
|
||
| -- | Connection to Redis which allows reconnecting. | ||
| type RobustConnection = MVar ReConnection | ||
|
|
||
| data ReConnection = ReConnection | ||
| { -- | established (and potentially breaking) connection to Redis | ||
| _rrConnection :: Connection, | ||
| -- | action which can be called to reconnect to Redis | ||
| _rrReconnect :: IO () | ||
| } | ||
|
|
||
| makeLenses ''ReConnection | ||
|
|
||
| -- | Connection to Redis which can be reestablished on connection errors. | ||
| -- | ||
| -- Reconnecting even when Redis IPs change as long as the DNS name remains | ||
| -- constant. The server type (cluster or not) and the connection information of | ||
| -- the initial connection are used when reconnecting. | ||
| -- | ||
| -- Throws 'ConnectError', 'ConnectTimeout', 'ConnectionLostException', | ||
| -- 'PingException', or 'IOException' if retry policy is finite. | ||
| connectRobust :: | ||
| Logger -> | ||
| -- | e. g., @exponentialBackoff 50000@ | ||
| RetryPolicy -> | ||
| -- | action returning a fresh initial 'Connection', e. g., @(connect connInfo)@ or @(connectCluster connInfo)@ | ||
| IO Connection -> | ||
| IO RobustConnection | ||
| connectRobust l retryStrategy connectLowLevel = do | ||
| robustConnection <- newEmptyMVar @IO @ReConnection | ||
| reconnectRedis robustConnection | ||
| pure robustConnection | ||
| where | ||
| reconnectRedis robustConnection = do | ||
| conn <- connectLowLevel | ||
|
|
||
| Log.info l $ Log.msg (Log.val "lazy connection established, running ping...") | ||
| -- FUTUREWORK: With ping, we only verify that a single node is running as | ||
| -- opposed to verifying that all nodes of the cluster are up and running. | ||
| -- It remains unclear how cluster health can be verified in hedis. | ||
| void . runRedis conn $ do | ||
| res <- ping | ||
| case res of | ||
| Left r -> throwIO $ PingException r | ||
| Right _ -> pure () | ||
| Log.info l $ Log.msg (Log.val "ping went through") | ||
|
|
||
| reconnectOnce <- | ||
| once $ -- avoid concurrent attempts to reconnect | ||
| recovering -- retry connecting, e. g., with exponential back-off | ||
| retryStrategy | ||
| [ const $ Catch.Handler (\(e :: ConnectError) -> logEx (Log.err l) e "Redis not in cluster mode" >> pure True), | ||
| const $ Catch.Handler (\(e :: ConnectTimeout) -> logEx (Log.err l) e "timeout when connecting to Redis" >> pure True), | ||
| const $ Catch.Handler (\(e :: ConnectionLostException) -> logEx (Log.err l) e "Redis connection lost during request" >> pure True), | ||
| const $ Catch.Handler (\(e :: PingException) -> logEx (Log.err l) e "pinging Redis failed" >> pure True), | ||
| const $ Catch.Handler (\(e :: IOException) -> logEx (Log.err l) e "network error when connecting to Redis" >> pure True) | ||
| ] | ||
| $ const $ | ||
| reconnectRedis robustConnection | ||
| let newReConnection = ReConnection {_rrConnection = conn, _rrReconnect = reconnectOnce} | ||
| unlessM (tryPutMVar robustConnection newReConnection) $ | ||
| void $ swapMVar robustConnection newReConnection | ||
|
|
||
| -- | Run a 'Redis' action through a 'RobustConnection'. | ||
| -- | ||
| -- Blocks on connection errors as long as the connection is not reestablished. | ||
| -- Without externally enforcing timeouts, this may lead to leaking threads. | ||
| runRobust :: RobustConnection -> Redis a -> IO a | ||
| runRobust mvar action = do | ||
| robustConnection <- readMVar mvar | ||
| catches | ||
| (runRedis (_rrConnection robustConnection) action) | ||
| [ Handler (\(_ :: ConnectionLostException) -> reconnectRetry robustConnection), -- Redis connection lost during request | ||
| Handler (\(_ :: IOException) -> reconnectRetry robustConnection) -- Redis unreachable | ||
|
Comment on lines
+114
to
+115
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add logs here? Without this it is a little confusing why the lazy connection starts restarting. Also, it would be nice to know which type of errors are causing a connection restart. |
||
| ] | ||
| where | ||
| reconnectRetry robustConnection = do | ||
| _rrReconnect robustConnection | ||
| runRobust mvar action | ||
|
|
||
| logEx :: Show e => ((Msg -> Msg) -> IO ()) -> e -> ByteString -> IO () | ||
| logEx lLevel e description = lLevel $ Log.msg $ Log.val $ description <> ": " <> fromString (show e) | ||
|
|
||
| data PingException = PingException Reply deriving (Show) | ||
|
|
||
| instance Exception PingException | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To the reviewer: this line effectively pollutes the
Redispackage namespace in this module, however, I deemed this is justifiable, since the namespace is artificial and functions of both modules logically belong to Redis anyway.