laziness fix

Now scanning runs fully interleaved with transferring.
This commit is contained in:
Joey Hess 2012-07-25 14:54:09 -04:00
parent b665ffe36f
commit 95c80b6440
2 changed files with 37 additions and 23 deletions

View file

@ -13,7 +13,6 @@ import Assistant.TransferQueue
import Assistant.ThreadedMonad import Assistant.ThreadedMonad
import Logs.Transfer import Logs.Transfer
import Logs.Location import Logs.Location
import Types.Remote
import qualified Remote import qualified Remote
import Utility.ThreadScheduler import Utility.ThreadScheduler
import qualified Git.LsFiles as LsFiles import qualified Git.LsFiles as LsFiles
@ -25,41 +24,48 @@ thisThread = "TransferScanner"
{- This thread waits until a remote needs to be scanned, to find transfers {- This thread waits until a remote needs to be scanned, to find transfers
- that need to be made, to keep data in sync. - that need to be made, to keep data in sync.
-
- Remotes are scanned in the background; the scan is blocked when the
- transfer queue gets too large.
-} -}
transferScannerThread :: ThreadState -> ScanRemoteMap -> TransferQueue -> IO () transferScannerThread :: ThreadState -> ScanRemoteMap -> TransferQueue -> IO ()
transferScannerThread st scanremotes transferqueue = do transferScannerThread st scanremotes transferqueue = do
runEvery (Seconds 2) $ do runEvery (Seconds 2) $ do
r <- getScanRemote scanremotes r <- getScanRemote scanremotes
liftIO $ debug thisThread ["starting scan of", show r] liftIO $ debug thisThread ["starting scan of", show r]
needtransfer <- runThreadState st $ scan r scan st transferqueue r
forM_ needtransfer $ \(f, t) ->
queueTransferAt smallsize Later transferqueue f t r
liftIO $ debug thisThread ["finished scan of", show r] liftIO $ debug thisThread ["finished scan of", show r]
where where
smallsize = 10
{- This is a naive scan through the git work tree. -} {- This is a naive scan through the git work tree.
scan :: Remote -> Annex [(AssociatedFile, Transfer)] -
scan r = do - The scan is blocked when the transfer queue gets too large. -}
files <- inRepo $ LsFiles.inRepo [] scan :: ThreadState -> TransferQueue -> Remote -> IO ()
catMaybes <$> forM files (whenAnnexed go) scan st transferqueue r = do
g <- runThreadState st $ fromRepo id
files <- LsFiles.inRepo [] g
go files
where where
u = Remote.uuid r go [] = return ()
go (f:fs) = do
go file (key, _) = v <- runThreadState st $ whenAnnexed check f
ifM (inAnnex key) case v of
( check Upload False =<< remotehas key Nothing -> noop
, check Download True =<< remotehas key Just t -> do
debug thisThread ["queuing", show t]
enqueue f t
go fs
where
check _ (key, _) = ifM (inAnnex key)
( helper key Upload False =<< remotehas key
, helper key Download True =<< remotehas key
) )
where helper key direction x y
check direction x y
| x == y = return $ | x == y = return $
Just (Just file, Transfer direction u key) Just $ Transfer direction u key
| otherwise = return Nothing | otherwise = return Nothing
u = Remote.uuid r
enqueue f t = queueTransferAt smallsize Later transferqueue (Just f) t r
smallsize = 10
{- Look directly in remote for the key when it's cheap; {- Look directly in remote for the key when it's cheap;
- otherwise rely on the location log. -} - otherwise rely on the location log. -}
remotehas key remotehas key

View file

@ -5,7 +5,15 @@
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
module Assistant.TransferQueue where module Assistant.TransferQueue (
TransferQueue,
Schedule(..),
newTransferQueue,
queueTransfers,
queueTransfer,
queueTransferAt,
getNextTransfer
) where
import Common.Annex import Common.Annex
import Assistant.DaemonStatus import Assistant.DaemonStatus