2012-06-19 06:40:21 +00:00
|
|
|
{- git-annex assistant commit thread
|
2012-06-13 16:36:33 +00:00
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.Committer where
|
|
|
|
|
|
|
|
import Common.Annex
|
2012-06-19 06:40:21 +00:00
|
|
|
import Assistant.Changes
|
2012-06-13 16:36:33 +00:00
|
|
|
import Assistant.ThreadedMonad
|
2012-06-19 06:40:21 +00:00
|
|
|
import Assistant.Watcher
|
2012-06-19 04:23:14 +00:00
|
|
|
import qualified Annex
|
2012-06-13 16:36:33 +00:00
|
|
|
import qualified Annex.Queue
|
|
|
|
import qualified Git.Command
|
2012-06-19 06:40:21 +00:00
|
|
|
import qualified Git.HashObject
|
|
|
|
import Git.Types
|
preliminary deferring of file adds to commit time
Defer adding files to the annex until commit time, when during a batch
operation, a bundle of files will be available. This will allow for
checking a them all with a single lsof call.
The tricky part is that adding the file causes a symlink change inotify.
So I made it wait for an appropriate number of symlink changes to be
received before continuing with the commit. This avoids any delay
in the commit process. It is possible that some unrelated symlink change is
made; if that happens it'll commit it and delay committing the newly added
symlink for 1 second. This seems ok. I do rely on the expected symlink
change event always being received, but only when the add succeeds.
Another way to do it might be to directly stage the symlink, and then
ignore the redundant symlink change event. That would involve some
redundant work, and perhaps an empty commit, but if this code turns
out to have some bug, that'd be the best way to avoid it.
FWIW, this change seems to, as a bonus, have produced better grouping
of batch changes into single commits. Before, a large batch change would
result in a series of commits, with the first containing only one file,
and each of the rest bundling a number of files. Now, the added wait for
the symlink changes to arrive gives time for additional add changes to
be processed, all within the same commit.
2012-06-15 20:27:44 +00:00
|
|
|
import qualified Command.Add
|
2012-06-13 21:54:23 +00:00
|
|
|
import Utility.ThreadScheduler
|
2012-06-16 02:35:29 +00:00
|
|
|
import qualified Utility.Lsof as Lsof
|
2012-06-19 06:40:21 +00:00
|
|
|
import qualified Utility.DirWatcher as DirWatcher
|
2012-06-20 20:07:14 +00:00
|
|
|
import Types.KeySource
|
2012-06-13 16:36:33 +00:00
|
|
|
|
|
|
|
import Data.Time.Clock
|
2012-06-16 02:35:29 +00:00
|
|
|
import Data.Tuple.Utils
|
|
|
|
import qualified Data.Set as S
|
2012-06-20 23:04:16 +00:00
|
|
|
import Data.Either
|
2012-06-13 16:36:33 +00:00
|
|
|
|
|
|
|
{- This thread makes git commits at appropriate times. -}
|
2012-06-20 23:04:16 +00:00
|
|
|
commitThread :: ThreadState -> ChangeChan -> IO ()
|
|
|
|
commitThread st changechan = runEvery (Seconds 1) $ do
|
2012-06-13 21:54:23 +00:00
|
|
|
-- We already waited one second as a simple rate limiter.
|
2012-06-20 23:04:16 +00:00
|
|
|
-- Next, wait until at least one change is available for
|
|
|
|
-- processing.
|
|
|
|
changes <- getChanges changechan
|
2012-06-13 16:36:33 +00:00
|
|
|
-- Now see if now's a good time to commit.
|
|
|
|
time <- getCurrentTime
|
2012-06-20 23:04:16 +00:00
|
|
|
if shouldCommit time changes
|
preliminary deferring of file adds to commit time
Defer adding files to the annex until commit time, when during a batch
operation, a bundle of files will be available. This will allow for
checking a them all with a single lsof call.
The tricky part is that adding the file causes a symlink change inotify.
So I made it wait for an appropriate number of symlink changes to be
received before continuing with the commit. This avoids any delay
in the commit process. It is possible that some unrelated symlink change is
made; if that happens it'll commit it and delay committing the newly added
symlink for 1 second. This seems ok. I do rely on the expected symlink
change event always being received, but only when the add succeeds.
Another way to do it might be to directly stage the symlink, and then
ignore the redundant symlink change event. That would involve some
redundant work, and perhaps an empty commit, but if this code turns
out to have some bug, that'd be the best way to avoid it.
FWIW, this change seems to, as a bonus, have produced better grouping
of batch changes into single commits. Before, a large batch change would
result in a series of commits, with the first containing only one file,
and each of the rest bundling a number of files. Now, the added wait for
the symlink changes to arrive gives time for additional add changes to
be processed, all within the same commit.
2012-06-15 20:27:44 +00:00
|
|
|
then do
|
2012-06-20 23:04:16 +00:00
|
|
|
readychanges <- handleAdds st changechan changes
|
|
|
|
if shouldCommit time readychanges
|
|
|
|
then do
|
|
|
|
void $ tryIO $ runThreadState st commitStaged
|
|
|
|
else refillChanges changechan readychanges
|
|
|
|
else refillChanges changechan changes
|
2012-06-13 16:36:33 +00:00
|
|
|
|
|
|
|
commitStaged :: Annex ()
|
|
|
|
commitStaged = do
|
|
|
|
Annex.Queue.flush
|
|
|
|
inRepo $ Git.Command.run "commit"
|
|
|
|
[ Param "--allow-empty-message"
|
|
|
|
, Param "-m", Param ""
|
|
|
|
-- Empty commits may be made if tree changes cancel
|
|
|
|
-- each other out, etc
|
|
|
|
, Param "--allow-empty"
|
|
|
|
-- Avoid running the usual git-annex pre-commit hook;
|
|
|
|
-- watch does the same symlink fixing, and we don't want
|
|
|
|
-- to deal with unlocked files in these commits.
|
|
|
|
, Param "--quiet"
|
|
|
|
]
|
|
|
|
|
|
|
|
{- Decide if now is a good time to make a commit.
|
|
|
|
- Note that the list of change times has an undefined order.
|
|
|
|
-
|
preliminary deferring of file adds to commit time
Defer adding files to the annex until commit time, when during a batch
operation, a bundle of files will be available. This will allow for
checking a them all with a single lsof call.
The tricky part is that adding the file causes a symlink change inotify.
So I made it wait for an appropriate number of symlink changes to be
received before continuing with the commit. This avoids any delay
in the commit process. It is possible that some unrelated symlink change is
made; if that happens it'll commit it and delay committing the newly added
symlink for 1 second. This seems ok. I do rely on the expected symlink
change event always being received, but only when the add succeeds.
Another way to do it might be to directly stage the symlink, and then
ignore the redundant symlink change event. That would involve some
redundant work, and perhaps an empty commit, but if this code turns
out to have some bug, that'd be the best way to avoid it.
FWIW, this change seems to, as a bonus, have produced better grouping
of batch changes into single commits. Before, a large batch change would
result in a series of commits, with the first containing only one file,
and each of the rest bundling a number of files. Now, the added wait for
the symlink changes to arrive gives time for additional add changes to
be processed, all within the same commit.
2012-06-15 20:27:44 +00:00
|
|
|
- Current strategy: If there have been 10 changes within the past second,
|
2012-06-13 16:36:33 +00:00
|
|
|
- a batch activity is taking place, so wait for later.
|
|
|
|
-}
|
|
|
|
shouldCommit :: UTCTime -> [Change] -> Bool
|
|
|
|
shouldCommit now changes
|
|
|
|
| len == 0 = False
|
|
|
|
| len > 10000 = True -- avoid bloating queue too much
|
|
|
|
| length (filter thisSecond changes) < 10 = True
|
|
|
|
| otherwise = False -- batch activity
|
|
|
|
where
|
|
|
|
len = length changes
|
|
|
|
thisSecond c = now `diffUTCTime` changeTime c <= 1
|
2012-06-16 00:44:34 +00:00
|
|
|
|
|
|
|
{- If there are PendingAddChanges, the files have not yet actually been
|
|
|
|
- added to the annex (probably), and that has to be done now, before
|
|
|
|
- committing.
|
|
|
|
-
|
|
|
|
- Deferring the adds to this point causes batches to be bundled together,
|
|
|
|
- which allows faster checking with lsof that the files are not still open
|
|
|
|
- for write by some other process.
|
|
|
|
-
|
|
|
|
- When a file is added, Inotify will notice the new symlink. So this waits
|
|
|
|
- for additional Changes to arrive, so that the symlink has hopefully been
|
2012-06-20 20:07:14 +00:00
|
|
|
- staged before returning, and will be committed immediately.
|
|
|
|
-
|
|
|
|
- OTOH, for kqueue, eventsCoalesce, so instead the symlink is directly
|
2012-06-20 23:04:16 +00:00
|
|
|
- created and staged.
|
|
|
|
-
|
|
|
|
- Returns a list of all changes that are ready to be committed.
|
|
|
|
- Any pending adds that are not ready yet are put back into the ChangeChan,
|
|
|
|
- where they will be retried later.
|
2012-06-16 00:44:34 +00:00
|
|
|
-}
|
2012-06-20 23:04:16 +00:00
|
|
|
handleAdds :: ThreadState -> ChangeChan -> [Change] -> IO [Change]
|
|
|
|
handleAdds st changechan cs = returnWhen (null pendingadds) $ do
|
|
|
|
(postponed, toadd) <- partitionEithers <$>
|
|
|
|
safeToAdd st pendingadds
|
|
|
|
|
|
|
|
unless (null postponed) $
|
|
|
|
refillChanges changechan postponed
|
|
|
|
|
|
|
|
returnWhen (null toadd) $ do
|
|
|
|
added <- catMaybes <$> forM toadd add
|
|
|
|
if (DirWatcher.eventsCoalesce || null added)
|
|
|
|
then return $ added ++ otherchanges
|
|
|
|
else do
|
|
|
|
r <- handleAdds st changechan
|
2012-06-19 06:40:21 +00:00
|
|
|
=<< getChanges changechan
|
2012-06-20 23:04:16 +00:00
|
|
|
return $ r ++ added ++ otherchanges
|
2012-06-16 00:44:34 +00:00
|
|
|
where
|
2012-06-20 23:04:16 +00:00
|
|
|
(pendingadds, otherchanges) = partition isPendingAddChange cs
|
|
|
|
|
|
|
|
returnWhen c a
|
|
|
|
| c = return otherchanges
|
|
|
|
| otherwise = a
|
2012-06-16 00:44:34 +00:00
|
|
|
|
2012-06-20 23:04:16 +00:00
|
|
|
add :: Change -> IO (Maybe Change)
|
|
|
|
add change@(PendingAddChange { keySource = ks }) = do
|
2012-06-21 00:05:40 +00:00
|
|
|
r <- catchMaybeIO $ sanitycheck ks $ runThreadState st $ do
|
2012-06-20 23:04:16 +00:00
|
|
|
showStart "add" $ keyFilename ks
|
|
|
|
handle (finishedChange change) (keyFilename ks)
|
|
|
|
=<< Command.Add.ingest ks
|
|
|
|
return $ maybeMaybe r
|
|
|
|
add _ = return Nothing
|
2012-06-16 00:44:34 +00:00
|
|
|
|
2012-06-20 23:04:16 +00:00
|
|
|
maybeMaybe (Just j@(Just _)) = j
|
|
|
|
maybeMaybe _ = Nothing
|
2012-06-16 00:44:34 +00:00
|
|
|
|
2012-06-20 23:04:16 +00:00
|
|
|
handle _ _ Nothing = do
|
2012-06-16 00:44:34 +00:00
|
|
|
showEndFail
|
2012-06-20 23:04:16 +00:00
|
|
|
return Nothing
|
|
|
|
handle change file (Just key) = do
|
2012-06-19 06:40:21 +00:00
|
|
|
link <- Command.Add.link file key True
|
|
|
|
when DirWatcher.eventsCoalesce $ do
|
|
|
|
sha <- inRepo $
|
|
|
|
Git.HashObject.hashObject BlobObject link
|
|
|
|
stageSymlink file sha
|
2012-06-16 00:44:34 +00:00
|
|
|
showEndOk
|
2012-06-20 23:04:16 +00:00
|
|
|
return $ Just change
|
2012-06-16 02:35:29 +00:00
|
|
|
|
2012-06-21 00:05:40 +00:00
|
|
|
{- Check that the keysource's keyFilename still exists,
|
|
|
|
- and is still a hard link to its contentLocation,
|
|
|
|
- before ingesting it. -}
|
|
|
|
sanitycheck keysource a = do
|
|
|
|
fs <- getSymbolicLinkStatus $ keyFilename keysource
|
|
|
|
ks <- getSymbolicLinkStatus $ contentLocation keysource
|
|
|
|
if deviceID ks == deviceID fs && fileID ks == fileID fs
|
|
|
|
then a
|
|
|
|
else return Nothing
|
|
|
|
|
2012-06-20 23:04:16 +00:00
|
|
|
{- PendingAddChanges can Either be Right to be added now,
|
|
|
|
- or are unsafe, and must be Left for later.
|
|
|
|
-
|
|
|
|
- Check by running lsof on the temp directory, which
|
|
|
|
- the KeySources are locked down in.
|
2012-06-16 02:35:29 +00:00
|
|
|
-}
|
2012-06-20 23:04:16 +00:00
|
|
|
safeToAdd :: ThreadState -> [Change] -> IO [Either Change Change]
|
|
|
|
safeToAdd st changes = runThreadState st $
|
|
|
|
ifM (Annex.getState Annex.force)
|
|
|
|
( allRight changes -- force bypasses lsof check
|
2012-06-19 04:23:14 +00:00
|
|
|
, do
|
|
|
|
tmpdir <- fromRepo gitAnnexTmpDir
|
2012-06-20 23:04:16 +00:00
|
|
|
openfiles <- S.fromList . map fst3 . filter openwrite <$>
|
2012-06-19 04:23:14 +00:00
|
|
|
liftIO (Lsof.queryDir tmpdir)
|
2012-06-20 23:04:16 +00:00
|
|
|
|
|
|
|
let checked = map (check openfiles) changes
|
|
|
|
|
|
|
|
{- If new events are received when files are closed,
|
|
|
|
- there's no need to retry any changes that cannot
|
|
|
|
- be done now. -}
|
|
|
|
if DirWatcher.closingTracked
|
|
|
|
then do
|
|
|
|
mapM_ canceladd $ lefts checked
|
|
|
|
allRight $ rights checked
|
|
|
|
else return checked
|
2012-06-19 04:23:14 +00:00
|
|
|
)
|
2012-06-16 02:35:29 +00:00
|
|
|
where
|
2012-06-20 23:04:16 +00:00
|
|
|
check openfiles change@(PendingAddChange { keySource = ks })
|
|
|
|
| S.member (contentLocation ks) openfiles = Left change
|
|
|
|
check _ change = Right change
|
|
|
|
|
|
|
|
canceladd (PendingAddChange { keySource = ks }) = do
|
|
|
|
warning $ keyFilename ks
|
|
|
|
++ " still has writers, not adding"
|
2012-06-20 20:07:14 +00:00
|
|
|
-- remove the hard link
|
2012-06-20 23:04:16 +00:00
|
|
|
void $ liftIO $ tryIO $
|
|
|
|
removeFile $ contentLocation ks
|
|
|
|
canceladd _ = noop
|
2012-06-16 02:35:29 +00:00
|
|
|
|
|
|
|
openwrite (_file, mode, _pid) =
|
|
|
|
mode == Lsof.OpenWriteOnly || mode == Lsof.OpenReadWrite
|
2012-06-20 23:04:16 +00:00
|
|
|
|
|
|
|
allRight = return . map Right
|