2021-02-03 17:19:47 +00:00
|
|
|
{- Stall detection for transfers.
|
|
|
|
-
|
2024-01-18 21:11:56 +00:00
|
|
|
- Copyright 2020-2024 Joey Hess <id@joeyh.name>
|
2021-02-03 17:19:47 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2024-01-19 19:14:26 +00:00
|
|
|
module Annex.StallDetection (
|
|
|
|
getStallDetection,
|
|
|
|
detectStalls,
|
|
|
|
StallDetection,
|
|
|
|
) where
|
2021-02-03 17:19:47 +00:00
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import Types.StallDetection
|
2024-01-19 19:14:26 +00:00
|
|
|
import Types.Direction
|
|
|
|
import Types.Remote (gitconfig)
|
2021-02-03 17:19:47 +00:00
|
|
|
import Utility.Metered
|
|
|
|
import Utility.HumanTime
|
|
|
|
import Utility.DataUnits
|
|
|
|
import Utility.ThreadScheduler
|
|
|
|
|
|
|
|
import Control.Concurrent.STM
|
2021-02-03 19:35:32 +00:00
|
|
|
import Control.Monad.IO.Class (MonadIO)
|
2024-01-18 21:11:56 +00:00
|
|
|
import Data.Time.Clock
|
2021-02-03 17:19:47 +00:00
|
|
|
|
2024-01-19 19:14:26 +00:00
|
|
|
getStallDetection :: Direction -> Remote -> Maybe StallDetection
|
|
|
|
getStallDetection Download r =
|
|
|
|
remoteAnnexStallDetectionDownload (gitconfig r)
|
|
|
|
<|> remoteAnnexStallDetection (gitconfig r)
|
|
|
|
getStallDetection Upload r =
|
|
|
|
remoteAnnexStallDetectionUpload (gitconfig r)
|
|
|
|
<|> remoteAnnexStallDetection (gitconfig r)
|
|
|
|
|
2021-02-03 19:35:32 +00:00
|
|
|
{- This may be safely canceled (with eg uninterruptibleCancel),
|
|
|
|
- as long as the passed action can be safely canceled. -}
|
|
|
|
detectStalls :: (Monad m, MonadIO m) => Maybe StallDetection -> TVar (Maybe BytesProcessed) -> m () -> m ()
|
2021-02-03 17:19:47 +00:00
|
|
|
detectStalls Nothing _ _ = noop
|
2021-02-03 19:35:32 +00:00
|
|
|
detectStalls (Just StallDetectionDisabled) _ _ = noop
|
2024-01-19 16:46:36 +00:00
|
|
|
detectStalls (Just (StallDetection bwrate@(BwRate _minsz duration))) metervar onstall = do
|
2024-01-18 21:11:56 +00:00
|
|
|
-- If the progress is being updated, but less frequently than
|
|
|
|
-- the specified duration, a stall would be incorrectly detected.
|
|
|
|
--
|
|
|
|
-- For example, consider the case of a remote that does
|
|
|
|
-- not support progress updates, but is chunked with a large chunk
|
|
|
|
-- size. In that case, progress is only updated after each chunk.
|
|
|
|
--
|
|
|
|
-- So, wait for the first update, and see how long it takes.
|
2024-01-19 16:46:36 +00:00
|
|
|
-- When it's longer than the duration (or close to it),
|
|
|
|
-- upscale the duration and minsz accordingly.
|
2024-01-18 21:11:56 +00:00
|
|
|
starttime <- liftIO getCurrentTime
|
|
|
|
v <- waitforfirstupdate =<< readMeterVar metervar
|
|
|
|
endtime <- liftIO getCurrentTime
|
|
|
|
let timepassed = floor (endtime `diffUTCTime` starttime)
|
2024-01-19 16:46:36 +00:00
|
|
|
let BwRate scaledminsz scaledduration = upscale bwrate timepassed
|
2024-01-18 21:11:56 +00:00
|
|
|
detectStalls' scaledminsz scaledduration metervar onstall v
|
|
|
|
where
|
|
|
|
minwaitsecs = Seconds $
|
|
|
|
min 60 (fromIntegral (durationSeconds duration))
|
|
|
|
waitforfirstupdate startval = do
|
|
|
|
liftIO $ threadDelaySeconds minwaitsecs
|
|
|
|
v <- readMeterVar metervar
|
|
|
|
if v > startval
|
|
|
|
then return v
|
|
|
|
else waitforfirstupdate startval
|
2021-02-03 17:19:47 +00:00
|
|
|
detectStalls (Just ProbeStallDetection) metervar onstall = do
|
|
|
|
-- Only do stall detection once the progress is confirmed to be
|
|
|
|
-- consistently updating. After the first update, it needs to
|
|
|
|
-- advance twice within 30 seconds. With that established,
|
|
|
|
-- if no data at all is sent for a 60 second period, it's
|
|
|
|
-- assumed to be a stall.
|
2024-01-18 21:11:56 +00:00
|
|
|
v <- readMeterVar metervar >>= waitforfirstupdate
|
2021-02-03 17:19:47 +00:00
|
|
|
ontimelyadvance v $ \v' -> ontimelyadvance v' $
|
|
|
|
detectStalls' 1 duration metervar onstall
|
|
|
|
where
|
|
|
|
duration = Duration 60
|
|
|
|
|
|
|
|
delay = Seconds (fromIntegral (durationSeconds duration) `div` 2)
|
|
|
|
|
|
|
|
waitforfirstupdate startval = do
|
2021-02-03 19:35:32 +00:00
|
|
|
liftIO $ threadDelaySeconds delay
|
2024-01-18 21:11:56 +00:00
|
|
|
v <- readMeterVar metervar
|
2021-02-03 17:19:47 +00:00
|
|
|
if v > startval
|
|
|
|
then return v
|
|
|
|
else waitforfirstupdate startval
|
|
|
|
|
|
|
|
ontimelyadvance v cont = do
|
2021-02-03 19:35:32 +00:00
|
|
|
liftIO $ threadDelaySeconds delay
|
2024-01-18 21:11:56 +00:00
|
|
|
v' <- readMeterVar metervar
|
2021-02-03 17:19:47 +00:00
|
|
|
when (v' > v) $
|
|
|
|
cont v'
|
|
|
|
|
|
|
|
detectStalls'
|
2021-02-03 19:35:32 +00:00
|
|
|
:: (Monad m, MonadIO m)
|
|
|
|
=> ByteSize
|
2021-02-03 17:19:47 +00:00
|
|
|
-> Duration
|
|
|
|
-> TVar (Maybe BytesProcessed)
|
2021-02-03 19:35:32 +00:00
|
|
|
-> m ()
|
2021-02-03 17:19:47 +00:00
|
|
|
-> Maybe ByteSize
|
2021-02-03 19:35:32 +00:00
|
|
|
-> m ()
|
2021-02-03 17:19:47 +00:00
|
|
|
detectStalls' minsz duration metervar onstall st = do
|
2021-02-03 19:35:32 +00:00
|
|
|
liftIO $ threadDelaySeconds delay
|
2021-02-03 17:19:47 +00:00
|
|
|
-- Get whatever progress value was reported most recently, if any.
|
2024-01-18 21:11:56 +00:00
|
|
|
v <- readMeterVar metervar
|
2021-02-03 17:19:47 +00:00
|
|
|
let cont = detectStalls' minsz duration metervar onstall v
|
|
|
|
case (st, v) of
|
|
|
|
(Nothing, _) -> cont
|
|
|
|
(_, Nothing) -> cont
|
|
|
|
(Just prev, Just sofar)
|
|
|
|
-- Just in case a progress meter somehow runs
|
|
|
|
-- backwards, or a second progress meter was
|
|
|
|
-- started and is at a smaller value than
|
|
|
|
-- the previous one.
|
|
|
|
| prev > sofar -> cont
|
|
|
|
| sofar - prev < minsz -> onstall
|
|
|
|
| otherwise -> cont
|
|
|
|
where
|
|
|
|
delay = Seconds (fromIntegral (durationSeconds duration))
|
2024-01-18 21:11:56 +00:00
|
|
|
|
|
|
|
readMeterVar
|
|
|
|
:: MonadIO m
|
|
|
|
=> TVar (Maybe BytesProcessed)
|
|
|
|
-> m (Maybe ByteSize)
|
|
|
|
readMeterVar metervar = liftIO $ atomically $
|
|
|
|
fmap fromBytesProcessed <$> readTVar metervar
|
2024-01-19 16:46:36 +00:00
|
|
|
|
|
|
|
-- Scale up the minsz and duration to match the observed time that passed
|
|
|
|
-- between progress updates. This allows for some variation in the transfer
|
|
|
|
-- rate causing later progress updates to happen less frequently.
|
|
|
|
upscale :: BwRate -> Integer -> BwRate
|
|
|
|
upscale input@(BwRate minsz duration) timepassedsecs
|
|
|
|
| timepassedsecs > dsecs `div` allowedvariation = BwRate
|
|
|
|
(ceiling (fromIntegral minsz * scale))
|
|
|
|
(Duration (ceiling (fromIntegral dsecs * scale)))
|
|
|
|
| otherwise = input
|
|
|
|
where
|
2024-01-19 19:14:26 +00:00
|
|
|
scale = max (1 :: Double) $
|
2024-01-19 16:46:36 +00:00
|
|
|
(fromIntegral timepassedsecs / fromIntegral (max dsecs 1))
|
|
|
|
* fromIntegral allowedvariation
|
|
|
|
|
|
|
|
dsecs = durationSeconds duration
|
|
|
|
|
|
|
|
-- Setting this too low will make normal bandwidth variations be
|
|
|
|
-- considered to be stalls, while setting it too high will make
|
|
|
|
-- stalls not be detected for much longer than the expected
|
|
|
|
-- duration.
|
|
|
|
--
|
|
|
|
-- For example, a BwRate of 20MB/1m, when the first progress
|
|
|
|
-- update takes 10m to arrive, is scaled to 600MB/30m. That 30m
|
|
|
|
-- is a reasonable since only 3 chunks get sent in that amount of
|
|
|
|
-- time at that rate. If allowedvariation = 10, that would
|
|
|
|
-- be 2000MB/100m, which seems much too long to wait to detect a
|
|
|
|
-- stall.
|
|
|
|
allowedvariation = 3
|