2012-07-06 20:39:07 +00:00
|
|
|
{- git-annex assistant transfer slots
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-07-06 20:39:07 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-07-06 20:39:07 +00:00
|
|
|
-}
|
|
|
|
|
2013-11-12 18:54:02 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2012-07-06 20:39:07 +00:00
|
|
|
module Assistant.TransferSlots where
|
|
|
|
|
2012-10-30 18:34:48 +00:00
|
|
|
import Assistant.Common
|
2012-08-28 21:17:09 +00:00
|
|
|
import Utility.ThreadScheduler
|
2012-10-30 18:34:48 +00:00
|
|
|
import Assistant.Types.TransferSlots
|
2012-08-28 21:17:09 +00:00
|
|
|
import Assistant.DaemonStatus
|
2013-03-19 22:46:29 +00:00
|
|
|
import Assistant.TransferrerPool
|
|
|
|
import Assistant.Types.TransferrerPool
|
2013-10-26 20:54:49 +00:00
|
|
|
import Assistant.Types.TransferQueue
|
|
|
|
import Assistant.TransferQueue
|
|
|
|
import Assistant.Alert
|
|
|
|
import Assistant.Alert.Utility
|
|
|
|
import Assistant.Commits
|
|
|
|
import Assistant.Drop
|
2016-08-03 16:37:12 +00:00
|
|
|
import Types.Transfer
|
2012-08-28 21:17:09 +00:00
|
|
|
import Logs.Transfer
|
2013-10-26 20:54:49 +00:00
|
|
|
import Logs.Location
|
|
|
|
import qualified Git
|
|
|
|
import qualified Remote
|
|
|
|
import qualified Types.Remote as Remote
|
|
|
|
import Annex.Content
|
|
|
|
import Annex.Wanted
|
2015-02-28 21:23:13 +00:00
|
|
|
import Annex.Path
|
2013-12-01 19:37:51 +00:00
|
|
|
import Utility.Batch
|
2015-10-08 20:55:11 +00:00
|
|
|
import Types.NumCopies
|
2012-08-28 21:17:09 +00:00
|
|
|
|
2013-10-26 20:54:49 +00:00
|
|
|
import qualified Data.Map as M
|
2012-08-10 22:42:44 +00:00
|
|
|
import qualified Control.Exception as E
|
2012-07-06 20:39:07 +00:00
|
|
|
import Control.Concurrent
|
2012-10-05 21:02:51 +00:00
|
|
|
import qualified Control.Concurrent.MSemN as MSemN
|
2013-11-12 18:54:02 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-10-26 20:54:49 +00:00
|
|
|
import System.Posix.Process (getProcessGroupIDOf)
|
2013-11-12 18:54:02 +00:00
|
|
|
import System.Posix.Signals (signalProcessGroup, sigTERM, sigKILL)
|
2013-12-11 03:19:18 +00:00
|
|
|
#else
|
2017-10-25 23:46:28 +00:00
|
|
|
import System.Win32.Process (terminateProcessById)
|
2013-11-12 18:54:02 +00:00
|
|
|
#endif
|
2012-07-06 20:39:07 +00:00
|
|
|
|
2013-03-19 22:46:29 +00:00
|
|
|
type TransferGenerator = Assistant (Maybe (Transfer, TransferInfo, Transferrer -> Assistant ()))
|
2012-10-30 19:39:15 +00:00
|
|
|
|
2012-08-28 21:17:09 +00:00
|
|
|
{- Waits until a transfer slot becomes available, then runs a
|
|
|
|
- TransferGenerator, and then runs the transfer action in its own thread.
|
2012-08-12 16:36:08 +00:00
|
|
|
-}
|
2013-12-01 19:37:51 +00:00
|
|
|
inTransferSlot :: FilePath -> BatchCommandMaker -> TransferGenerator -> Assistant ()
|
|
|
|
inTransferSlot program batchmaker gen = do
|
2012-10-30 19:39:15 +00:00
|
|
|
flip MSemN.wait 1 <<~ transferSlots
|
2013-12-01 19:37:51 +00:00
|
|
|
runTransferThread program batchmaker =<< gen
|
2012-08-12 16:36:08 +00:00
|
|
|
|
2012-08-28 21:17:09 +00:00
|
|
|
{- Runs a TransferGenerator, and its transfer action,
|
|
|
|
- without waiting for a slot to become available. -}
|
2013-12-01 19:37:51 +00:00
|
|
|
inImmediateTransferSlot :: FilePath -> BatchCommandMaker -> TransferGenerator -> Assistant ()
|
|
|
|
inImmediateTransferSlot program batchmaker gen = do
|
2012-10-30 19:39:15 +00:00
|
|
|
flip MSemN.signal (-1) <<~ transferSlots
|
2013-12-01 19:37:51 +00:00
|
|
|
runTransferThread program batchmaker =<< gen
|
2012-08-12 16:36:08 +00:00
|
|
|
|
2012-08-28 21:17:09 +00:00
|
|
|
{- Runs a transfer action, in an already allocated transfer slot.
|
|
|
|
- Once it finishes, frees the transfer slot.
|
|
|
|
-
|
|
|
|
- Note that the action is subject to being killed when the transfer
|
2012-08-10 22:42:44 +00:00
|
|
|
- is canceled or paused.
|
|
|
|
-
|
|
|
|
- A PauseTransfer exception is handled by letting the action be killed,
|
|
|
|
- then pausing the thread until a ResumeTransfer exception is raised,
|
|
|
|
- then rerunning the action.
|
|
|
|
-}
|
2013-12-01 19:37:51 +00:00
|
|
|
runTransferThread :: FilePath -> BatchCommandMaker -> Maybe (Transfer, TransferInfo, Transferrer -> Assistant ()) -> Assistant ()
|
|
|
|
runTransferThread _ _ Nothing = flip MSemN.signal 1 <<~ transferSlots
|
|
|
|
runTransferThread program batchmaker (Just (t, info, a)) = do
|
2012-10-30 19:39:15 +00:00
|
|
|
d <- getAssistant id
|
2013-03-19 22:46:29 +00:00
|
|
|
aio <- asIO1 a
|
2013-12-01 19:37:51 +00:00
|
|
|
tid <- liftIO $ forkIO $ runTransferThread' program batchmaker d aio
|
2012-10-30 19:39:15 +00:00
|
|
|
updateTransferInfo t $ info { transferTid = Just tid }
|
2012-10-30 21:14:26 +00:00
|
|
|
|
2013-12-01 19:37:51 +00:00
|
|
|
runTransferThread' :: FilePath -> BatchCommandMaker -> AssistantData -> (Transferrer -> IO ()) -> IO ()
|
|
|
|
runTransferThread' program batchmaker d run = go
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2013-03-19 22:46:29 +00:00
|
|
|
go = catchPauseResume $
|
2013-12-01 19:37:51 +00:00
|
|
|
withTransferrer program batchmaker (transferrerPool d)
|
2013-03-19 22:46:29 +00:00
|
|
|
run
|
|
|
|
pause = catchPauseResume $
|
|
|
|
runEvery (Seconds 86400) noop
|
2012-10-31 06:34:03 +00:00
|
|
|
{- Note: This must use E.try, rather than E.catch.
|
|
|
|
- When E.catch is used, and has called go in its exception
|
|
|
|
- handler, Control.Concurrent.throwTo will block sometimes
|
|
|
|
- when signaling. Using E.try avoids the problem. -}
|
|
|
|
catchPauseResume a' = do
|
|
|
|
r <- E.try a' :: IO (Either E.SomeException ())
|
|
|
|
case r of
|
|
|
|
Left e -> case E.fromException e of
|
|
|
|
Just PauseTransfer -> pause
|
|
|
|
Just ResumeTransfer -> go
|
2012-10-30 21:14:26 +00:00
|
|
|
_ -> done
|
2012-10-31 06:34:03 +00:00
|
|
|
_ -> done
|
2012-11-05 23:39:08 +00:00
|
|
|
done = runAssistant d $
|
2012-10-31 06:34:03 +00:00
|
|
|
flip MSemN.signal 1 <<~ transferSlots
|
2013-10-26 20:54:49 +00:00
|
|
|
|
|
|
|
{- By the time this is called, the daemonstatus's currentTransfers map should
|
|
|
|
- already have been updated to include the transfer. -}
|
|
|
|
genTransfer :: Transfer -> TransferInfo -> TransferGenerator
|
2014-01-23 20:37:08 +00:00
|
|
|
genTransfer t info = case transferRemote info of
|
2018-06-04 18:31:55 +00:00
|
|
|
Just remote -> ifM (unpluggedremovabledrive remote)
|
|
|
|
( do
|
|
|
|
-- optimisation, since the transfer would fail
|
2013-10-26 20:54:49 +00:00
|
|
|
liftAnnex $ recordFailedTransfer t info
|
|
|
|
void $ removeTransfer t
|
|
|
|
return Nothing
|
2018-06-04 18:31:55 +00:00
|
|
|
, ifM (liftAnnex $ shouldTransfer t info)
|
2013-10-26 20:54:49 +00:00
|
|
|
( do
|
|
|
|
debug [ "Transferring:" , describeTransfer t info ]
|
|
|
|
notifyTransfer
|
2014-01-23 20:37:08 +00:00
|
|
|
return $ Just (t, info, go remote)
|
2013-10-26 20:54:49 +00:00
|
|
|
, do
|
|
|
|
debug [ "Skipping unnecessary transfer:",
|
|
|
|
describeTransfer t info ]
|
|
|
|
void $ removeTransfer t
|
|
|
|
finishedTransfer t (Just info)
|
|
|
|
return Nothing
|
|
|
|
)
|
2018-06-04 18:31:55 +00:00
|
|
|
)
|
2013-10-26 20:54:49 +00:00
|
|
|
_ -> return Nothing
|
|
|
|
where
|
|
|
|
direction = transferDirection t
|
|
|
|
isdownload = direction == Download
|
|
|
|
|
2018-06-04 18:31:55 +00:00
|
|
|
unpluggedremovabledrive remote = Git.repoIsLocalUnknown
|
|
|
|
<$> liftAnnex (Remote.getRepo remote)
|
|
|
|
|
2013-10-26 20:54:49 +00:00
|
|
|
{- Alerts are only shown for successful transfers.
|
|
|
|
- Transfers can temporarily fail for many reasons,
|
|
|
|
- so there's no point in bothering the user about
|
|
|
|
- those. The assistant should recover.
|
|
|
|
-
|
|
|
|
- After a successful upload, handle dropping it from
|
|
|
|
- here, if desired. In this case, the remote it was
|
|
|
|
- uploaded to is known to have it.
|
|
|
|
-
|
|
|
|
- Also, after a successful transfer, the location
|
|
|
|
- log has changed. Indicate that a commit has been
|
|
|
|
- made, in order to queue a push of the git-annex
|
|
|
|
- branch out to remotes that did not participate
|
|
|
|
- in the transfer.
|
|
|
|
-
|
|
|
|
- If the process failed, it could have crashed,
|
|
|
|
- so remove the transfer from the list of current
|
|
|
|
- transfers, just in case it didn't stop
|
|
|
|
- in a way that lets the TransferWatcher do its
|
|
|
|
- usual cleanup. However, first check if something else is
|
|
|
|
- running the transfer, to avoid removing active transfers.
|
|
|
|
-}
|
2014-05-19 20:19:33 +00:00
|
|
|
go remote transferrer = ifM (liftIO $ performTransfer transferrer t info)
|
2013-10-26 20:54:49 +00:00
|
|
|
( do
|
2017-03-10 17:12:24 +00:00
|
|
|
case associatedFile info of
|
|
|
|
AssociatedFile Nothing -> noop
|
|
|
|
AssociatedFile (Just af) -> void $
|
|
|
|
addAlert $ makeAlertFiller True $
|
|
|
|
transferFileAlert direction True af
|
2013-10-26 20:54:49 +00:00
|
|
|
unless isdownload $
|
|
|
|
handleDrops
|
|
|
|
("object uploaded to " ++ show remote)
|
|
|
|
True (transferKey t)
|
|
|
|
(associatedFile info)
|
2015-10-08 21:58:32 +00:00
|
|
|
[mkVerifiedCopy RecentlyVerifiedCopy remote]
|
2013-10-26 20:54:49 +00:00
|
|
|
void recordCommit
|
|
|
|
, whenM (liftAnnex $ isNothing <$> checkTransfer t) $
|
|
|
|
void $ removeTransfer t
|
|
|
|
)
|
|
|
|
|
|
|
|
{- Called right before a transfer begins, this is a last chance to avoid
|
|
|
|
- unnecessary transfers.
|
|
|
|
-
|
|
|
|
- For downloads, we obviously don't need to download if the already
|
|
|
|
- have the object.
|
|
|
|
-
|
|
|
|
- Smilarly, for uploads, check if the remote is known to already have
|
|
|
|
- the object.
|
|
|
|
-
|
|
|
|
- Also, uploads get queued to all remotes, in order of cost.
|
|
|
|
- This may mean, for example, that an object is uploaded over the LAN
|
|
|
|
- to a locally paired client, and once that upload is done, a more
|
|
|
|
- expensive transfer remote no longer wants the object. (Since
|
|
|
|
- all the clients have it already.) So do one last check if this is still
|
|
|
|
- preferred content.
|
|
|
|
-
|
|
|
|
- We'll also do one last preferred content check for downloads. An
|
|
|
|
- example of a case where this could be needed is if a download is queued
|
|
|
|
- for a file that gets moved out of an archive directory -- but before
|
|
|
|
- that download can happen, the file is put back in the archive.
|
|
|
|
-}
|
|
|
|
shouldTransfer :: Transfer -> TransferInfo -> Annex Bool
|
|
|
|
shouldTransfer t info
|
|
|
|
| transferDirection t == Download =
|
2014-01-23 20:37:08 +00:00
|
|
|
(not <$> inAnnex key) <&&> wantGet True (Just key) file
|
2013-10-26 20:54:49 +00:00
|
|
|
| transferDirection t == Upload = case transferRemote info of
|
|
|
|
Nothing -> return False
|
|
|
|
Just r -> notinremote r
|
2014-01-23 20:37:08 +00:00
|
|
|
<&&> wantSend True (Just key) file (Remote.uuid r)
|
2013-10-26 20:54:49 +00:00
|
|
|
| otherwise = return False
|
|
|
|
where
|
|
|
|
key = transferKey t
|
|
|
|
file = associatedFile info
|
|
|
|
|
|
|
|
{- Trust the location log to check if the remote already has
|
|
|
|
- the key. This avoids a roundtrip to the remote. -}
|
|
|
|
notinremote r = notElem (Remote.uuid r) <$> loggedLocations key
|
|
|
|
|
|
|
|
{- Queue uploads of files downloaded to us, spreading them
|
|
|
|
- out to other reachable remotes.
|
|
|
|
-
|
|
|
|
- Downloading a file may have caused a remote to not want it;
|
|
|
|
- so check for drops from remotes.
|
|
|
|
-
|
|
|
|
- Uploading a file may cause the local repo, or some other remote to not
|
|
|
|
- want it; handle that too.
|
|
|
|
-}
|
|
|
|
finishedTransfer :: Transfer -> Maybe TransferInfo -> Assistant ()
|
|
|
|
finishedTransfer t (Just info)
|
|
|
|
| transferDirection t == Download =
|
|
|
|
whenM (liftAnnex $ inAnnex $ transferKey t) $ do
|
|
|
|
dodrops False
|
2014-01-23 20:51:16 +00:00
|
|
|
void $ queueTransfersMatching (/= transferUUID t)
|
2013-10-26 20:54:49 +00:00
|
|
|
"newly received object"
|
|
|
|
Later (transferKey t) (associatedFile info) Upload
|
|
|
|
| otherwise = dodrops True
|
|
|
|
where
|
|
|
|
dodrops fromhere = handleDrops
|
|
|
|
("drop wanted after " ++ describeTransfer t info)
|
2015-10-08 20:55:11 +00:00
|
|
|
fromhere (transferKey t) (associatedFile info) []
|
2013-10-26 20:54:49 +00:00
|
|
|
finishedTransfer _ _ = noop
|
|
|
|
|
|
|
|
{- Pause a running transfer. -}
|
|
|
|
pauseTransfer :: Transfer -> Assistant ()
|
|
|
|
pauseTransfer = cancelTransfer True
|
|
|
|
|
|
|
|
{- Cancel a running transfer. -}
|
|
|
|
cancelTransfer :: Bool -> Transfer -> Assistant ()
|
|
|
|
cancelTransfer pause t = do
|
|
|
|
m <- getCurrentTransfers
|
|
|
|
unless pause $
|
|
|
|
{- remove queued transfer -}
|
|
|
|
void $ dequeueTransfers $ equivilantTransfer t
|
|
|
|
{- stop running transfer -}
|
|
|
|
maybe noop stop (M.lookup t m)
|
|
|
|
where
|
|
|
|
stop info = do
|
|
|
|
{- When there's a thread associated with the
|
|
|
|
- transfer, it's signaled first, to avoid it
|
|
|
|
- displaying any alert about the transfer having
|
|
|
|
- failed when the transfer process is killed. -}
|
|
|
|
liftIO $ maybe noop signalthread $ transferTid info
|
|
|
|
liftIO $ maybe noop killproc $ transferPid info
|
|
|
|
if pause
|
|
|
|
then void $ alterTransferInfo t $
|
|
|
|
\i -> i { transferPaused = True }
|
|
|
|
else void $ removeTransfer t
|
|
|
|
signalthread tid
|
|
|
|
| pause = throwTo tid PauseTransfer
|
|
|
|
| otherwise = killThread tid
|
|
|
|
killproc pid = void $ tryIO $ do
|
2013-11-12 18:54:02 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2014-02-13 18:00:15 +00:00
|
|
|
{- In order to stop helper processes like rsync,
|
|
|
|
- kill the whole process group of the process
|
|
|
|
- running the transfer. -}
|
2013-10-26 20:54:49 +00:00
|
|
|
g <- getProcessGroupIDOf pid
|
2013-12-11 03:32:10 +00:00
|
|
|
let signal sig = void $ tryIO $ signalProcessGroup sig g
|
|
|
|
signal sigTERM
|
2014-02-13 18:00:15 +00:00
|
|
|
threadDelay 50000 -- 0.05 second grace period
|
2013-12-11 03:32:10 +00:00
|
|
|
signal sigKILL
|
2013-11-12 18:54:02 +00:00
|
|
|
#else
|
2017-10-25 23:46:28 +00:00
|
|
|
terminateProcessById pid
|
2013-11-12 18:54:02 +00:00
|
|
|
#endif
|
2013-10-26 20:54:49 +00:00
|
|
|
|
|
|
|
{- Start or resume a transfer. -}
|
|
|
|
startTransfer :: Transfer -> Assistant ()
|
|
|
|
startTransfer t = do
|
|
|
|
m <- getCurrentTransfers
|
|
|
|
maybe startqueued go (M.lookup t m)
|
|
|
|
where
|
|
|
|
go info = maybe (start info) resume $ transferTid info
|
|
|
|
startqueued = do
|
|
|
|
is <- map snd <$> getMatchingTransfers (== t)
|
|
|
|
maybe noop start $ headMaybe is
|
|
|
|
resume tid = do
|
|
|
|
alterTransferInfo t $ \i -> i { transferPaused = False }
|
|
|
|
liftIO $ throwTo tid ResumeTransfer
|
|
|
|
start info = do
|
2015-02-28 21:23:13 +00:00
|
|
|
program <- liftIO programPath
|
2013-12-01 19:37:51 +00:00
|
|
|
batchmaker <- liftIO getBatchCommandMaker
|
|
|
|
inImmediateTransferSlot program batchmaker $
|
2013-10-26 20:54:49 +00:00
|
|
|
genTransfer t info
|
|
|
|
|
|
|
|
getCurrentTransfers :: Assistant TransferMap
|
|
|
|
getCurrentTransfers = currentTransfers <$> getDaemonStatus
|