summaryrefslogtreecommitdiff
path: root/Assistant/Threads/TransferScanner.hs
blob: d3436bd254bcd620a728e931be4d942c4f17cf5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
{- git-annex assistant thread to scan remotes to find needed transfers
 -
 - Copyright 2012 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Assistant.Threads.TransferScanner where

import Assistant.Common
import Assistant.ScanRemotes
import Assistant.TransferQueue
import Assistant.ThreadedMonad
import Assistant.DaemonStatus
import Assistant.Alert
import Logs.Transfer
import Logs.Location
import Logs.Web (webUUID)
import qualified Remote
import qualified Types.Remote as Remote
import Utility.ThreadScheduler
import qualified Git.LsFiles as LsFiles
import Command
import Annex.Content

import qualified Data.Set as S

thisThread :: ThreadName
thisThread = "TransferScanner"

{- This thread waits until a remote needs to be scanned, to find transfers
 - that need to be made, to keep data in sync.
 -}
transferScannerThread :: ThreadState -> DaemonStatusHandle -> ScanRemoteMap -> TransferQueue -> NamedThread
transferScannerThread st dstatus scanremotes transferqueue = thread $ do
	startupScan
	go S.empty
	where
		thread = NamedThread thisThread
		go scanned = do
			threadDelaySeconds (Seconds 2)
			(rs, infos) <- unzip <$> getScanRemote scanremotes
			if any fullScan infos || any (`S.notMember` scanned) rs
				then do
					expensiveScan st dstatus transferqueue rs
					go $ scanned `S.union` S.fromList rs
				else do
					mapM_ (failedTransferScan st dstatus transferqueue) rs
					go scanned
		{- All available remotes are scanned in full on startup,
		 - for multiple reasons, including:
		 -
		 - * This may be the first run, and there may be remotes
		 -   already in place, that need to be synced.
		 - * We may have run before, and scanned a remote, but
		 -   only been in a subdirectory of the git remote, and so
		 -   not synced it all.
		 - * We may have run before, and had transfers queued,
		 -   and then the system (or us) crashed, and that info was
		 -   lost.
		 -}
		startupScan = addScanRemotes scanremotes True
			=<< knownRemotes <$> getDaemonStatus dstatus

{- This is a cheap scan for failed transfers involving a remote. -}
failedTransferScan :: ThreadState -> DaemonStatusHandle -> TransferQueue -> Remote -> IO ()
failedTransferScan st dstatus transferqueue r = do
	failed <- runThreadState st $ getFailedTransfers (Remote.uuid r)
	runThreadState st $ mapM_ removeFailedTransfer $ map fst failed
	mapM_ retry failed
	where
		retry (t, info)
			| transferDirection t == Download = do
				{- Check if the remote still has the key.
				 - If not, relies on the expensiveScan to
				 - get it queued from some other remote. -}
				whenM (runThreadState st $ remoteHas r $ transferKey t) $
					requeue t info
			| otherwise = do
				{- The Transferrer checks when uploading
				 - that the remote doesn't already have the
				 - key, so it's not redundantly checked
				 - here. -}
				requeue t info
		requeue t info = queueTransferWhenSmall
			transferqueue dstatus (associatedFile info) t r

{- This is a expensive scan through the full git work tree, finding
 - files to download from or upload to any of the remotes.
 - 
 - The scan is blocked when the transfer queue gets too large. -}
expensiveScan :: ThreadState -> DaemonStatusHandle -> TransferQueue -> [Remote] -> IO ()
expensiveScan st dstatus transferqueue rs = unless onlyweb $ do
	liftIO $ debug thisThread ["starting scan of", show visiblers]
	void $ alertWhile dstatus (scanAlert visiblers) $ do
		g <- runThreadState st $ fromRepo id
		files <- LsFiles.inRepo [] g
		go files
		return True
	liftIO $ debug thisThread ["finished scan of", show visiblers]
	where
		onlyweb = all (== webUUID) $ map Remote.uuid rs
		visiblers = let rs' = filter (not . Remote.readonly) rs
			in if null rs' then rs else rs'
		go [] = noop
		go (f:fs) = do
			mapM_ (enqueue f) =<< catMaybes <$> runThreadState st
				(ifAnnexed f findtransfers $ return [])
			go fs
		enqueue f (r, t) = do
			debug thisThread ["queuing", show t]
			queueTransferWhenSmall transferqueue dstatus (Just f) t r
		findtransfers (key, _) = do
			locs <- loggedLocations key
			let use a = return $ map (a key locs) rs
			ifM (inAnnex key)
				( use $ check Upload False
				, use $ check Download True
				)
		check direction want key locs r
			| direction == Upload && Remote.readonly r = Nothing
			| (Remote.uuid r `elem` locs) == want = Just
				(r, Transfer direction (Remote.uuid r) key)
			| otherwise = Nothing

remoteHas :: Remote -> Key -> Annex Bool
remoteHas r key = elem
	<$> pure (Remote.uuid r)
	<*> loggedLocations key