2012-07-02 20:07:39 +00:00
|
|
|
{- git-annex assistant pending transfer queue
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-07-25 18:54:09 +00:00
|
|
|
module Assistant.TransferQueue (
|
|
|
|
TransferQueue,
|
|
|
|
Schedule(..),
|
|
|
|
newTransferQueue,
|
2012-07-27 15:47:34 +00:00
|
|
|
getTransferQueue,
|
2012-07-25 18:54:09 +00:00
|
|
|
queueTransfers,
|
2012-09-18 18:24:51 +00:00
|
|
|
queueTransfersMatching,
|
2012-09-18 01:05:50 +00:00
|
|
|
queueDeferredDownloads,
|
2012-07-25 18:54:09 +00:00
|
|
|
queueTransfer,
|
|
|
|
queueTransferAt,
|
2012-08-23 19:22:23 +00:00
|
|
|
queueTransferWhenSmall,
|
2012-08-08 21:55:56 +00:00
|
|
|
getNextTransfer,
|
2012-08-29 20:30:40 +00:00
|
|
|
getMatchingTransfers,
|
2012-08-29 19:56:47 +00:00
|
|
|
dequeueTransfers,
|
2012-07-25 18:54:09 +00:00
|
|
|
) where
|
2012-07-02 20:07:39 +00:00
|
|
|
|
|
|
|
import Common.Annex
|
2012-07-05 16:21:22 +00:00
|
|
|
import Assistant.DaemonStatus
|
2012-10-30 18:11:14 +00:00
|
|
|
import Assistant.Types.DaemonStatus
|
2012-07-02 20:07:39 +00:00
|
|
|
import Logs.Transfer
|
|
|
|
import Types.Remote
|
2012-07-05 16:21:22 +00:00
|
|
|
import qualified Remote
|
2012-08-26 19:39:02 +00:00
|
|
|
import qualified Types.Remote as Remote
|
2012-10-09 16:18:41 +00:00
|
|
|
import Annex.Wanted
|
2012-07-02 20:07:39 +00:00
|
|
|
|
|
|
|
import Control.Concurrent.STM
|
2012-07-28 22:47:24 +00:00
|
|
|
import qualified Data.Map as M
|
2012-07-02 20:07:39 +00:00
|
|
|
|
2012-07-25 17:12:34 +00:00
|
|
|
data TransferQueue = TransferQueue
|
2012-08-31 16:57:24 +00:00
|
|
|
{ queuesize :: TVar Int
|
2012-07-27 15:47:34 +00:00
|
|
|
, queuelist :: TVar [(Transfer, TransferInfo)]
|
2012-09-18 01:05:50 +00:00
|
|
|
, deferreddownloads :: TVar [(Key, AssociatedFile)]
|
2012-07-25 17:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data Schedule = Next | Later
|
|
|
|
deriving (Eq)
|
2012-07-02 20:07:39 +00:00
|
|
|
|
|
|
|
newTransferQueue :: IO TransferQueue
|
2012-07-27 15:47:34 +00:00
|
|
|
newTransferQueue = atomically $ TransferQueue
|
2012-08-31 16:57:24 +00:00
|
|
|
<$> newTVar 0
|
2012-07-27 15:47:34 +00:00
|
|
|
<*> newTVar []
|
2012-09-18 01:05:50 +00:00
|
|
|
<*> newTVar []
|
2012-07-27 15:47:34 +00:00
|
|
|
|
|
|
|
{- Reads the queue's content without blocking or changing it. -}
|
|
|
|
getTransferQueue :: TransferQueue -> IO [(Transfer, TransferInfo)]
|
|
|
|
getTransferQueue q = atomically $ readTVar $ queuelist q
|
2012-07-02 20:07:39 +00:00
|
|
|
|
2012-07-25 18:02:50 +00:00
|
|
|
stubInfo :: AssociatedFile -> Remote -> TransferInfo
|
2012-09-18 01:05:50 +00:00
|
|
|
stubInfo f r = stubTransferInfo
|
|
|
|
{ transferRemote = Just r
|
2012-07-02 20:07:39 +00:00
|
|
|
, associatedFile = f
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:18:41 +00:00
|
|
|
{- Adds transfers to queue for some of the known remotes.
|
|
|
|
- Honors preferred content settings, only transferring wanted files. -}
|
2012-07-25 17:12:34 +00:00
|
|
|
queueTransfers :: Schedule -> TransferQueue -> DaemonStatusHandle -> Key -> AssociatedFile -> Direction -> Annex ()
|
2012-09-18 18:24:51 +00:00
|
|
|
queueTransfers = queueTransfersMatching (const True)
|
|
|
|
|
|
|
|
{- Adds transfers to queue for some of the known remotes, that match a
|
2012-10-09 16:18:41 +00:00
|
|
|
- condition. Honors preferred content settings. -}
|
2012-09-18 18:24:51 +00:00
|
|
|
queueTransfersMatching :: (UUID -> Bool) -> Schedule -> TransferQueue -> DaemonStatusHandle -> Key -> AssociatedFile -> Direction -> Annex ()
|
2012-10-09 16:18:41 +00:00
|
|
|
queueTransfersMatching matching schedule q dstatus k f direction
|
|
|
|
| direction == Download = whenM (wantGet f) go
|
|
|
|
| otherwise = go
|
2012-07-05 16:21:22 +00:00
|
|
|
where
|
2012-10-09 16:18:41 +00:00
|
|
|
go = do
|
|
|
|
rs <- sufficientremotes
|
2012-10-14 18:47:01 +00:00
|
|
|
=<< syncRemotes <$> liftIO (getDaemonStatus dstatus)
|
2012-10-09 16:18:41 +00:00
|
|
|
let matchingrs = filter (matching . Remote.uuid) rs
|
|
|
|
if null matchingrs
|
|
|
|
then defer
|
|
|
|
else forM_ matchingrs $ \r -> liftIO $
|
|
|
|
enqueue schedule q dstatus (gentransfer r) (stubInfo f r)
|
2012-07-23 03:16:56 +00:00
|
|
|
sufficientremotes rs
|
2012-09-18 01:05:50 +00:00
|
|
|
{- Queue downloads from all remotes that
|
|
|
|
- have the key, with the cheapest ones first.
|
|
|
|
- More expensive ones will only be tried if
|
|
|
|
- downloading from a cheap one fails. -}
|
2012-07-17 16:17:01 +00:00
|
|
|
| direction == Download = do
|
|
|
|
uuids <- Remote.keyLocations k
|
2012-07-23 03:16:56 +00:00
|
|
|
return $ filter (\r -> uuid r `elem` uuids) rs
|
2012-10-09 16:18:41 +00:00
|
|
|
{- Upload to all remotes that want the content. -}
|
|
|
|
| otherwise = filterM (wantSend f . Remote.uuid) $
|
|
|
|
filter (not . Remote.readonly) rs
|
2012-07-05 16:21:22 +00:00
|
|
|
gentransfer r = Transfer
|
|
|
|
{ transferDirection = direction
|
|
|
|
, transferKey = k
|
2012-07-05 20:34:20 +00:00
|
|
|
, transferUUID = Remote.uuid r
|
2012-07-05 16:21:22 +00:00
|
|
|
}
|
2012-09-18 01:05:50 +00:00
|
|
|
defer
|
|
|
|
{- Defer this download, as no known remote has the key. -}
|
|
|
|
| direction == Download = void $ liftIO $ atomically $
|
|
|
|
modifyTVar' (deferreddownloads q) $
|
|
|
|
\l -> (k, f):l
|
|
|
|
| otherwise = noop
|
|
|
|
|
|
|
|
{- Queues any deferred downloads that can now be accomplished, leaving
|
|
|
|
- any others in the list to try again later. -}
|
|
|
|
queueDeferredDownloads :: Schedule -> TransferQueue -> DaemonStatusHandle -> Annex ()
|
|
|
|
queueDeferredDownloads schedule q dstatus = do
|
|
|
|
l <- liftIO $ atomically $ swapTVar (deferreddownloads q) []
|
2012-10-14 19:09:37 +00:00
|
|
|
rs <- syncRemotes <$> liftIO (getDaemonStatus dstatus)
|
2012-09-18 01:05:50 +00:00
|
|
|
left <- filterM (queue rs) l
|
|
|
|
unless (null left) $
|
|
|
|
liftIO $ atomically $ modifyTVar' (deferreddownloads q) $
|
|
|
|
\new -> new ++ left
|
|
|
|
where
|
|
|
|
queue rs (k, f) = do
|
|
|
|
uuids <- Remote.keyLocations k
|
|
|
|
let sources = filter (\r -> uuid r `elem` uuids) rs
|
|
|
|
unless (null sources) $
|
|
|
|
forM_ sources $ \r -> liftIO $
|
|
|
|
enqueue schedule q dstatus
|
|
|
|
(gentransfer r) (stubInfo f r)
|
|
|
|
return $ null sources
|
|
|
|
where
|
|
|
|
gentransfer r = Transfer
|
|
|
|
{ transferDirection = Download
|
|
|
|
, transferKey = k
|
|
|
|
, transferUUID = Remote.uuid r
|
|
|
|
}
|
2012-07-25 17:12:34 +00:00
|
|
|
|
2012-07-28 22:47:24 +00:00
|
|
|
enqueue :: Schedule -> TransferQueue -> DaemonStatusHandle -> Transfer -> TransferInfo -> IO ()
|
|
|
|
enqueue schedule q dstatus t info
|
2012-08-31 16:57:24 +00:00
|
|
|
| schedule == Next = go (new:)
|
|
|
|
| otherwise = go (\l -> l++[new])
|
2012-07-25 17:12:34 +00:00
|
|
|
where
|
2012-07-27 15:47:34 +00:00
|
|
|
new = (t, info)
|
2012-08-31 16:57:24 +00:00
|
|
|
go modlist = do
|
2012-07-28 22:47:24 +00:00
|
|
|
atomically $ do
|
|
|
|
void $ modifyTVar' (queuesize q) succ
|
|
|
|
void $ modifyTVar' (queuelist q) modlist
|
2012-07-29 12:52:57 +00:00
|
|
|
void $ notifyTransfer dstatus
|
2012-07-05 16:21:22 +00:00
|
|
|
|
2012-07-25 17:12:34 +00:00
|
|
|
{- Adds a transfer to the queue. -}
|
2012-07-28 22:47:24 +00:00
|
|
|
queueTransfer :: Schedule -> TransferQueue -> DaemonStatusHandle -> AssociatedFile -> Transfer -> Remote -> IO ()
|
|
|
|
queueTransfer schedule q dstatus f t remote =
|
|
|
|
enqueue schedule q dstatus t (stubInfo f remote)
|
2012-07-02 20:07:39 +00:00
|
|
|
|
2012-07-25 17:12:34 +00:00
|
|
|
{- Blocks until the queue is no larger than a given size, and then adds a
|
|
|
|
- transfer to the queue. -}
|
2012-08-08 21:55:56 +00:00
|
|
|
queueTransferAt :: Int -> Schedule -> TransferQueue -> DaemonStatusHandle -> AssociatedFile -> Transfer -> Remote -> IO ()
|
2012-07-28 22:47:24 +00:00
|
|
|
queueTransferAt wantsz schedule q dstatus f t remote = do
|
|
|
|
atomically $ do
|
|
|
|
sz <- readTVar (queuesize q)
|
2012-09-13 04:57:52 +00:00
|
|
|
unless (sz <= wantsz) $
|
|
|
|
retry -- blocks until queuesize changes
|
2012-07-28 22:47:24 +00:00
|
|
|
enqueue schedule q dstatus t (stubInfo f remote)
|
2012-07-02 20:07:39 +00:00
|
|
|
|
2012-08-23 19:22:23 +00:00
|
|
|
queueTransferWhenSmall :: TransferQueue -> DaemonStatusHandle -> AssociatedFile -> Transfer -> Remote -> IO ()
|
|
|
|
queueTransferWhenSmall = queueTransferAt 10 Later
|
|
|
|
|
2012-08-31 16:57:24 +00:00
|
|
|
{- Blocks until a pending transfer is available in the queue,
|
2012-07-29 17:37:26 +00:00
|
|
|
- and removes it.
|
|
|
|
-
|
|
|
|
- Checks that it's acceptable, before adding it to the
|
|
|
|
- the currentTransfers map. If it's not acceptable, it's discarded.
|
|
|
|
-
|
|
|
|
- This is done in a single STM transaction, so there is no window
|
|
|
|
- where an observer sees an inconsistent status. -}
|
|
|
|
getNextTransfer :: TransferQueue -> DaemonStatusHandle -> (TransferInfo -> Bool) -> IO (Maybe (Transfer, TransferInfo))
|
|
|
|
getNextTransfer q dstatus acceptable = atomically $ do
|
2012-08-31 16:57:24 +00:00
|
|
|
sz <- readTVar (queuesize q)
|
|
|
|
if sz < 1
|
|
|
|
then retry -- blocks until queuesize changes
|
|
|
|
else do
|
|
|
|
(r@(t,info):rest) <- readTVar (queuelist q)
|
|
|
|
writeTVar (queuelist q) rest
|
|
|
|
void $ modifyTVar' (queuesize q) pred
|
|
|
|
if acceptable info
|
|
|
|
then do
|
|
|
|
adjustTransfersSTM dstatus $
|
|
|
|
M.insertWith' const t info
|
|
|
|
return $ Just r
|
|
|
|
else return Nothing
|
2012-08-08 21:55:56 +00:00
|
|
|
|
2012-08-29 20:30:40 +00:00
|
|
|
{- Moves transfers matching a condition from the queue, to the
|
|
|
|
- currentTransfers map. -}
|
|
|
|
getMatchingTransfers :: TransferQueue -> DaemonStatusHandle -> (Transfer -> Bool) -> IO [(Transfer, TransferInfo)]
|
|
|
|
getMatchingTransfers q dstatus c = atomically $ do
|
|
|
|
ts <- dequeueTransfersSTM q c
|
|
|
|
unless (null ts) $
|
|
|
|
adjustTransfersSTM dstatus $ \m -> M.union m $ M.fromList ts
|
|
|
|
return ts
|
|
|
|
|
2012-08-29 19:56:47 +00:00
|
|
|
{- Removes transfers matching a condition from the queue, and returns the
|
|
|
|
- removed transfers. -}
|
|
|
|
dequeueTransfers :: TransferQueue -> DaemonStatusHandle -> (Transfer -> Bool) -> IO [(Transfer, TransferInfo)]
|
|
|
|
dequeueTransfers q dstatus c = do
|
2012-08-29 20:30:40 +00:00
|
|
|
removed <- atomically $ dequeueTransfersSTM q c
|
2012-08-29 19:56:47 +00:00
|
|
|
unless (null removed) $
|
2012-08-10 20:00:24 +00:00
|
|
|
notifyTransfer dstatus
|
2012-08-29 19:56:47 +00:00
|
|
|
return removed
|
2012-08-29 20:30:40 +00:00
|
|
|
|
|
|
|
dequeueTransfersSTM :: TransferQueue -> (Transfer -> Bool) -> STM [(Transfer, TransferInfo)]
|
|
|
|
dequeueTransfersSTM q c = do
|
|
|
|
(removed, ts) <- partition (c . fst)
|
|
|
|
<$> readTVar (queuelist q)
|
|
|
|
void $ writeTVar (queuesize q) (length ts)
|
|
|
|
void $ writeTVar (queuelist q) ts
|
|
|
|
return removed
|