1acdd18ea8
* Deal with clock skew, both forwards and backwards, when logging information to the git-annex branch. * GIT_ANNEX_VECTOR_CLOCK can now be set to a fixed value (eg 1) rather than needing to be advanced each time a new change is made. * Misuse of GIT_ANNEX_VECTOR_CLOCK will no longer confuse git-annex. When changing a file in the git-annex branch, the vector clock to use is now determined by first looking at the current time (or GIT_ANNEX_VECTOR_CLOCK when set), and comparing it to the newest vector clock already in use in that file. If a newer time stamp was already in use, advance it forward by a second instead. When the clock is set to a time in the past, this avoids logging with an old timestamp, which would risk that log line later being ignored in favor of "newer" line that is really not newer. When a log entry has been made with a clock that was set far ahead in the future, this avoids newer information being logged with an older timestamp and so being ignored in favor of that future-timestamped information. Once all clocks get fixed, this will result in the vector clocks being incremented, until finally enough time has passed that time gets back ahead of the vector clock value, and then it will return to usual operation. (This latter situation is not ideal, but it seems the best that can be done. The issue with it is, since all writers will be incrementing the last vector clock they saw, there's no way to tell when one writer made a write significantly later in time than another, so the earlier write might arbitrarily be picked when merging. This problem is why git-annex uses timestamps in the first place, rather than pure vector clocks.) Advancing forward by 1 second is somewhat arbitrary. setDead advances a timestamp by just 1 picosecond, and the vector clock could too. But then it would interfere with setDead, which wants to be overrulled by any change. So it could use 2 picoseconds or something, but that seems weird. It could just as well advance it forward by a minute or whatever, but then it would be harder for real time to catch up with the vector clock when forward clock slew had happened. A complication is that many log files contain several different peices of information, and it may be best to only use vector clocks for the same peice of information. For example, a key's location log file contains InfoPresent/InfoMissing for each UUID, and it only looks at the vector clocks for the UUID that is being changed, and not other UUIDs. Although exactly where the dividing line is can be hard to determine. Consider metadata logs, where a field "tag" can have multiple values set at different times. Should it advance forward past the last tag? Probably. What about when a different field is set, should it look at the clocks of other fields? Perhaps not, but currently it does, and this does not seems like it will cause any problems. Another one I'm not entirely sure about is the export log, which is keyed by (fromuuid, touuid). So if multiple repos are exporting to the same remote, different vector clocks can be used for that remote. It looks like that's probably ok, because it does not try to determine what order things occurred when there was an export conflict. Sponsored-by: Jochen Bartl on Patreon
164 lines
5.5 KiB
Haskell
164 lines
5.5 KiB
Haskell
{- git-annex general metadata storage log and per-remote metadata storage log.
|
|
-
|
|
- A line of the log will look like "timestamp field [+-]value [...]"
|
|
-
|
|
- (In the per-remote log, each field is prefixed with "uuid:")
|
|
-
|
|
- Note that unset values are preserved. Consider this case:
|
|
-
|
|
- We have:
|
|
-
|
|
- 100 foo +x
|
|
- 200 foo -x
|
|
-
|
|
- An unmerged remote has:
|
|
-
|
|
- 150 foo +x
|
|
-
|
|
- After union merge, because the foo -x was preserved, we know that
|
|
- after the other remote redundantly set foo +x, it was unset,
|
|
- and so foo currently has no value.
|
|
-
|
|
- Copyright 2014-2020 Joey Hess <id@joeyh.name>
|
|
-
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
-}
|
|
|
|
module Logs.MetaData (
|
|
getCurrentMetaData,
|
|
parseCurrentMetaData,
|
|
getCurrentRemoteMetaData,
|
|
addMetaData,
|
|
addRemoteMetaData,
|
|
addMetaDataClocked,
|
|
currentMetaData,
|
|
copyMetaData,
|
|
) where
|
|
|
|
import Annex.Common
|
|
import Types.MetaData
|
|
import Types.RemoteState
|
|
import Annex.MetaData.StandardFields
|
|
import Annex.VectorClock
|
|
import qualified Annex.Branch
|
|
import qualified Annex
|
|
import Logs
|
|
import Utility.TimeStamp
|
|
import Logs.MetaData.Pure
|
|
|
|
import qualified Data.Set as S
|
|
import qualified Data.Map as M
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
{- Go through the log from oldest to newest, and combine it all
|
|
- into a single MetaData representing the current state.
|
|
-
|
|
- Automatically generates a lastchanged metadata for each field that's
|
|
- currently set, based on timestamps in the log.
|
|
-}
|
|
getCurrentMetaData :: Key -> Annex MetaData
|
|
getCurrentMetaData = getCurrentMetaData' metaDataLogFile
|
|
|
|
getCurrentMetaData' :: (GitConfig -> Key -> RawFilePath) -> Key -> Annex MetaData
|
|
getCurrentMetaData' getlogfile k = do
|
|
config <- Annex.getGitConfig
|
|
parseCurrentMetaData <$> Annex.Branch.get (getlogfile config k)
|
|
|
|
parseCurrentMetaData :: L.ByteString -> MetaData
|
|
parseCurrentMetaData content =
|
|
let ls = S.toAscList $ parseLog content
|
|
loggedmeta = logToCurrentMetaData ls
|
|
in currentMetaData $ unionMetaData loggedmeta
|
|
(lastchanged ls loggedmeta)
|
|
where
|
|
lastchanged [] _ = emptyMetaData
|
|
lastchanged ls (MetaData currentlyset) =
|
|
let m = foldl' (flip M.union) M.empty (map genlastchanged ls)
|
|
in MetaData $
|
|
-- Add a overall lastchanged using the oldest log
|
|
-- item (log is in ascending order).
|
|
M.insert lastChangedField (lastchangedval $ Prelude.last ls) $
|
|
M.mapKeys mkLastChangedField $
|
|
-- Only include fields that are currently set.
|
|
m `M.intersection` currentlyset
|
|
-- Makes each field have the timestamp as its value.
|
|
genlastchanged l =
|
|
let MetaData m = value l
|
|
ts = lastchangedval l
|
|
in M.map (const ts) m
|
|
lastchangedval l = S.singleton $ toMetaValue $ encodeBS $ showts $
|
|
case changed l of
|
|
VectorClock t -> t
|
|
Unknown -> 0
|
|
showts = formatPOSIXTime "%F@%H-%M-%S"
|
|
|
|
getCurrentRemoteMetaData :: RemoteStateHandle -> Key -> Annex RemoteMetaData
|
|
getCurrentRemoteMetaData (RemoteStateHandle u) k = extractRemoteMetaData u <$>
|
|
getCurrentMetaData' remoteMetaDataLogFile k
|
|
|
|
{- Adds in some metadata, which can override existing values, or unset
|
|
- them, but otherwise leaves any existing metadata as-is. -}
|
|
addMetaData :: Key -> MetaData -> Annex ()
|
|
addMetaData = addMetaData' (Annex.Branch.RegardingUUID []) metaDataLogFile
|
|
|
|
addMetaData' :: Annex.Branch.RegardingUUID -> (GitConfig -> Key -> RawFilePath) -> Key -> MetaData -> Annex ()
|
|
addMetaData' ru getlogfile k metadata =
|
|
addMetaDataClocked' ru getlogfile k metadata =<< currentVectorClock
|
|
|
|
{- Reusing the same CandidateVectorClock when making changes to the metadata
|
|
- of multiple keys is a nice optimisation. The same metadata lines
|
|
- will tend to be generated across the different log files, and so
|
|
- git will be able to pack the data more efficiently. -}
|
|
addMetaDataClocked :: Key -> MetaData -> CandidateVectorClock -> Annex ()
|
|
addMetaDataClocked = addMetaDataClocked' (Annex.Branch.RegardingUUID []) metaDataLogFile
|
|
|
|
addMetaDataClocked' :: Annex.Branch.RegardingUUID -> (GitConfig -> Key -> RawFilePath) -> Key -> MetaData -> CandidateVectorClock -> Annex ()
|
|
addMetaDataClocked' ru getlogfile k d@(MetaData m) c
|
|
| d == emptyMetaData = noop
|
|
| otherwise = do
|
|
config <- Annex.getGitConfig
|
|
Annex.Branch.change ru (getlogfile config k) $ \b ->
|
|
let s = parseLog b
|
|
c' = advanceVectorClock c (map changed (S.toList s))
|
|
ent = LogEntry c' metadata
|
|
in buildLog $ simplifyLog $ S.insert ent s
|
|
where
|
|
metadata = MetaData $ M.filterWithKey (\f _ -> not (isLastChangedField f)) m
|
|
|
|
addRemoteMetaData :: Key -> RemoteStateHandle -> MetaData -> Annex ()
|
|
addRemoteMetaData k (RemoteStateHandle u) m =
|
|
addMetaData' (Annex.Branch.RegardingUUID [u]) remoteMetaDataLogFile k $ fromRemoteMetaData $
|
|
RemoteMetaData u m
|
|
|
|
getMetaDataLog :: Key -> Annex (Log MetaData)
|
|
getMetaDataLog key = do
|
|
config <- Annex.getGitConfig
|
|
readLog $ metaDataLogFile config key
|
|
|
|
{- Copies the metadata from the old key to the new key.
|
|
-
|
|
- The exact content of the metadata file is copied, so that the timestamps
|
|
- remain the same, and because this is more space-efficient in the git
|
|
- repository.
|
|
-
|
|
- Any metadata already attached to the new key is not preserved.
|
|
-
|
|
- Returns True when metadata was copied.
|
|
-}
|
|
copyMetaData :: Key -> Key -> Annex Bool
|
|
copyMetaData oldkey newkey
|
|
| oldkey == newkey = return False
|
|
| otherwise = do
|
|
l <- getMetaDataLog oldkey
|
|
if logToCurrentMetaData (S.toAscList l) == emptyMetaData
|
|
then return False
|
|
else do
|
|
config <- Annex.getGitConfig
|
|
Annex.Branch.change
|
|
(Annex.Branch.RegardingUUID [])
|
|
(metaDataLogFile config newkey)
|
|
(const $ buildLog l)
|
|
return True
|
|
|
|
readLog :: RawFilePath -> Annex (Log MetaData)
|
|
readLog = parseLog <$$> Annex.Branch.get
|