git-annex/Assistant.hs

165 lines
6.1 KiB
Haskell
Raw Normal View History

2012-06-13 16:36:33 +00:00
{- git-annex assistant daemon
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-
- Overview of threads and MVars, etc:
-
- Thread 1: parent
- The initial thread run, double forks to background, starts other
- threads, and then stops, waiting for them to terminate,
- or for a ctrl-c.
2012-06-13 23:32:09 +00:00
- Thread 2: watcher
2012-06-13 16:36:33 +00:00
- Notices new files, and calls handlers for events, queuing changes.
- Thread 3: inotify internal
- Used by haskell inotify library to ensure inotify event buffer is
- kept drained.
2012-06-13 23:32:09 +00:00
- Thread 4: inotify startup scanner
- Scans the tree and registers inotify watches for each directory.
2012-06-13 16:36:33 +00:00
- A MVar lock is used to prevent other inotify handlers from running
- until this is complete.
- Thread 5: committer
- Waits for changes to occur, and runs the git queue to update its
- index, then commits. Also queues Transfer events to send added
- files to other remotes.
- Thread 6: pusher
- Waits for commits to be made, and pushes updated branches to remotes,
- in parallel. (Forks a process for each git push.)
2012-06-25 20:38:12 +00:00
- Thread 7: push retryer
- Runs every 30 minutes when there are failed pushes, and retries
- them.
- Thread 8: merger
- Waits for pushes to be received from remotes, and merges the
- updated branches into the current branch.
- (This uses inotify on .git/refs/heads, so there are additional
- inotify threads associated with it, too.)
- Thread 9: transfer watcher
- Watches for transfer information files being created and removed,
- and maintains the DaemonStatus currentTransfers map.
- (This uses inotify on .git/annex/transfer/, so there are
- additional inotify threads associated with it, too.)
2012-07-05 20:34:20 +00:00
- Thread 10: transferrer
- Waits for Transfers to be queued and does them.
- Thread 11: status logger
2012-06-13 16:36:33 +00:00
- Wakes up periodically and records the daemon's status to disk.
2012-07-05 20:34:20 +00:00
- Thread 12: sanity checker
2012-06-13 23:32:09 +00:00
- Wakes up periodically (rarely) and does sanity checks.
- Thread 13: mount watcher
- Either uses dbus to watch for drive mount events, or, when
- there's no dbus, polls to find newly mounted filesystems.
- Once a filesystem that contains a remote is mounted, updates
- state about that remote, pulls from it, and queues a push to it,
- as well as an update, and queues it onto the
- ConnectedRemoteChan
- Thread 14: transfer scanner
- Does potentially expensive checks to find data that needs to be
- transferred from or to remotes, and queues Transfers.
- Uses the ScanRemotes map.
2012-06-13 16:36:33 +00:00
-
- ThreadState: (MVar)
- The Annex state is stored here, which allows resuscitating the
2012-06-28 17:04:02 +00:00
- Annex monad in IO actions run by the watcher and committer
2012-06-13 16:36:33 +00:00
- threads. Thus, a single state is shared amoung the threads, and
- only one at a time can access it.
- DaemonStatusHandle: (MVar)
- The daemon's current status. This MVar should only be manipulated
- from inside the Annex monad, which ensures it's accessed only
- after the ThreadState MVar.
- ChangeChan: (STM TChan)
- Changes are indicated by writing to this channel. The committer
- reads from it.
- CommitChan: (STM TChan)
- Commits are indicated by writing to this channel. The pusher reads
- from it.
- FailedPushMap (STM TMVar)
- Failed pushes are indicated by writing to this TMVar. The push
- retrier blocks until they're available.
- TransferQueue (STM TChan)
- Transfers to make are indicated by writing to this channel.
- TransferSlots (QSemN)
- Count of the number of currently available transfer slots.
- Updated by the transfer watcher, this allows other threads
- to block until a slot is available.
- This MVar should only be manipulated from inside the Annex monad,
- which ensures it's accessed only after the ThreadState MVar.
- ScanRemotes (STM TMVar)
- Remotes that have been disconnected, and should be scanned
- are indicated by writing to this TMVar.
2012-06-13 16:36:33 +00:00
-}
{-# LANGUAGE CPP #-}
2012-06-13 16:36:33 +00:00
module Assistant where
import Assistant.Common
2012-06-13 16:36:33 +00:00
import Assistant.ThreadedMonad
import Assistant.DaemonStatus
2012-06-19 06:40:21 +00:00
import Assistant.Changes
2012-06-22 17:39:44 +00:00
import Assistant.Commits
2012-06-25 20:38:12 +00:00
import Assistant.Pushes
import Assistant.ScanRemotes
import Assistant.TransferQueue
import Assistant.TransferSlots
2012-06-25 20:10:10 +00:00
import Assistant.Threads.Watcher
import Assistant.Threads.Committer
import Assistant.Threads.Pusher
import Assistant.Threads.Merger
import Assistant.Threads.TransferWatcher
2012-07-05 20:34:20 +00:00
import Assistant.Threads.Transferrer
2012-06-25 20:10:10 +00:00
import Assistant.Threads.SanityChecker
import Assistant.Threads.MountWatcher
import Assistant.Threads.TransferScanner
#ifdef WITH_WEBAPP
import Assistant.Threads.WebApp
#endif
2012-06-13 16:36:33 +00:00
import qualified Utility.Daemon
import Utility.LogFile
import Utility.ThreadScheduler
2012-06-13 16:36:33 +00:00
import Control.Concurrent
startDaemon :: Bool -> Bool -> Annex ()
startDaemon assistant foreground
2012-06-13 16:36:33 +00:00
| foreground = do
showStart (if assistant then "assistant" else "watch") "."
2012-06-13 16:36:33 +00:00
go id
| otherwise = do
logfd <- liftIO . openLog =<< fromRepo gitAnnexLogFile
pidfile <- fromRepo gitAnnexPidFile
go $ Utility.Daemon.daemonize logfd (Just pidfile) False
where
2012-06-29 00:01:03 +00:00
go daemonize = withThreadState $ \st -> do
checkCanWatch
2012-06-13 16:36:33 +00:00
dstatus <- startDaemonStatus
2012-06-29 00:01:03 +00:00
liftIO $ daemonize $ run dstatus st
run dstatus st = do
changechan <- newChangeChan
commitchan <- newCommitChan
pushmap <- newFailedPushMap
transferqueue <- newTransferQueue
transferslots <- newTransferSlots
scanremotes <- newScanRemoteMap
2012-07-20 16:01:28 +00:00
mapM_ forkIO
[ commitThread st changechan commitchan transferqueue dstatus
, pushThread st dstatus commitchan pushmap
2012-06-29 00:01:03 +00:00
, pushRetryThread st pushmap
, mergeThread st
, transferWatcherThread st dstatus
, transfererThread st dstatus transferqueue transferslots
2012-06-29 00:01:03 +00:00
, daemonStatusThread st dstatus
, sanityCheckerThread st dstatus transferqueue changechan
, mountWatcherThread st dstatus scanremotes
, transferScannerThread st scanremotes transferqueue
#ifdef WITH_WEBAPP
, webAppThread st dstatus
#endif
, watchThread st dstatus transferqueue changechan
2012-06-29 00:01:03 +00:00
]
debug "assistant"
["all git-annex assistant threads started"]
2012-06-29 00:01:03 +00:00
waitForTermination
2012-06-13 16:36:33 +00:00
stopDaemon :: Annex ()
stopDaemon = liftIO . Utility.Daemon.stopDaemon =<< fromRepo gitAnnexPidFile