2012-11-05 21:43:17 +00:00
|
|
|
{- git over XMPP
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-03-15 21:52:41 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2012-11-05 21:43:17 +00:00
|
|
|
module Assistant.XMPP.Git where
|
|
|
|
|
|
|
|
import Assistant.Common
|
2012-11-08 18:02:37 +00:00
|
|
|
import Assistant.NetMessager
|
|
|
|
import Assistant.Types.NetMessager
|
2012-11-05 21:43:17 +00:00
|
|
|
import Assistant.XMPP
|
|
|
|
import Assistant.XMPP.Buddies
|
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Assistant.Alert
|
|
|
|
import Assistant.MakeRemote
|
|
|
|
import Assistant.Sync
|
2012-11-10 18:38:50 +00:00
|
|
|
import qualified Command.Sync
|
|
|
|
import qualified Annex.Branch
|
2012-11-05 21:43:17 +00:00
|
|
|
import Annex.UUID
|
2013-04-30 17:22:55 +00:00
|
|
|
import Logs.UUID
|
assistant: Get back in sync with XMPP remotes after network reconnection, and on startup.
Make manualPull send push requests over XMPP.
When reconnecting with remotes, those that are XMPP remotes cannot
immediately be pulled from and scanned, so instead maintain a set of
(probably) desynced remotes, and put XMPP remotes on it. (This set could be
used in other ways later, if we can detect we're out of sync with other
types of remotes.)
The merger handles detecting when a XMPP push is received from a desynced
remote, and triggers a scan then, if they have in fact diverged.
This has one known bug: A single XMPP remote can have multiple clients
behind it. When this happens, only the UUID of one client is recorded
as the UUID of the XMPP remote. Pushes from the other XMPP clients will not
trigger a scan. If the client whose UUID is expected responds to the push
request, it'll work, but when that client is offline, we're SOL.
2013-03-06 19:09:31 +00:00
|
|
|
import Annex.TaggedPush
|
2013-05-21 22:24:29 +00:00
|
|
|
import Annex.CatFile
|
2012-11-05 21:43:17 +00:00
|
|
|
import Config
|
2012-11-06 20:36:44 +00:00
|
|
|
import Git
|
2012-11-08 20:44:23 +00:00
|
|
|
import qualified Git.Branch
|
2013-04-23 15:38:52 +00:00
|
|
|
import Config.Files
|
2012-11-05 21:43:17 +00:00
|
|
|
import qualified Types.Remote as Remote
|
2013-03-07 02:18:44 +00:00
|
|
|
import qualified Remote as Remote
|
|
|
|
import Remote.List
|
2012-11-09 16:51:54 +00:00
|
|
|
import Utility.FileMode
|
2013-02-13 18:30:04 +00:00
|
|
|
import Utility.Shell
|
2013-05-11 22:23:41 +00:00
|
|
|
import Utility.Env
|
2012-11-05 21:43:17 +00:00
|
|
|
|
|
|
|
import Network.Protocol.XMPP
|
|
|
|
import qualified Data.Text as T
|
2012-11-06 04:52:35 +00:00
|
|
|
import System.Posix.Types
|
2012-11-06 20:36:44 +00:00
|
|
|
import System.Process (std_in, std_out, std_err)
|
2012-11-06 14:14:00 +00:00
|
|
|
import Control.Concurrent
|
2012-11-14 15:53:23 +00:00
|
|
|
import System.Timeout
|
2012-11-06 14:14:00 +00:00
|
|
|
import qualified Data.ByteString as B
|
2012-11-09 19:03:16 +00:00
|
|
|
import qualified Data.Map as M
|
2012-11-06 04:52:35 +00:00
|
|
|
|
2013-05-21 01:58:59 +00:00
|
|
|
{- Largest chunk of data to send in a single XMPP message. -}
|
|
|
|
chunkSize :: Int
|
|
|
|
chunkSize = 4096
|
|
|
|
|
|
|
|
{- How long to wait for an expected message before assuming the other side
|
|
|
|
- has gone away and canceling a push.
|
|
|
|
-
|
|
|
|
- This needs to be long enough to allow a message of up to 2+ times
|
|
|
|
- chunkSize to propigate up to a XMPP server, perhaps across to another
|
|
|
|
- server, and back down to us. On the other hand, other XMPP pushes can be
|
|
|
|
- delayed for running until the timeout is reached, so it should not be
|
|
|
|
- excessive.
|
|
|
|
-}
|
|
|
|
xmppTimeout :: Int
|
|
|
|
xmppTimeout = 120000000 -- 120 seconds
|
|
|
|
|
2012-11-05 21:43:17 +00:00
|
|
|
finishXMPPPairing :: JID -> UUID -> Assistant ()
|
|
|
|
finishXMPPPairing jid u = void $ alertWhile alert $
|
|
|
|
makeXMPPGitRemote buddy (baseJID jid) u
|
|
|
|
where
|
|
|
|
buddy = T.unpack $ buddyName jid
|
|
|
|
alert = pairRequestAcknowledgedAlert buddy Nothing
|
|
|
|
|
2012-11-10 03:43:08 +00:00
|
|
|
gitXMPPLocation :: JID -> String
|
|
|
|
gitXMPPLocation jid = "xmpp::" ++ T.unpack (formatJID $ baseJID jid)
|
|
|
|
|
2012-11-05 21:43:17 +00:00
|
|
|
makeXMPPGitRemote :: String -> JID -> UUID -> Assistant Bool
|
|
|
|
makeXMPPGitRemote buddyname jid u = do
|
2012-11-10 03:43:08 +00:00
|
|
|
remote <- liftAnnex $ addRemote $
|
|
|
|
makeGitRemote buddyname $ gitXMPPLocation jid
|
2012-11-09 18:34:06 +00:00
|
|
|
liftAnnex $ storeUUID (remoteConfig (Remote.repo remote) "uuid") u
|
2013-03-07 02:18:44 +00:00
|
|
|
liftAnnex $ void remoteListRefresh
|
|
|
|
remote' <- liftAnnex $ fromMaybe (error "failed to add remote")
|
|
|
|
<$> Remote.byName (Just buddyname)
|
2013-04-08 19:36:09 +00:00
|
|
|
syncRemote remote'
|
2012-11-05 21:43:17 +00:00
|
|
|
return True
|
|
|
|
|
2012-11-10 18:38:50 +00:00
|
|
|
{- Pushes over XMPP, communicating with a specific client.
|
|
|
|
- Runs an arbitrary IO action to push, which should run git-push with
|
|
|
|
- an xmpp:: url.
|
2012-11-06 04:52:35 +00:00
|
|
|
-
|
2012-11-09 16:51:54 +00:00
|
|
|
- To handle xmpp:: urls, git push will run git-remote-xmpp, which is
|
|
|
|
- injected into its PATH, and in turn runs git-annex xmppgit. The
|
|
|
|
- dataflow them becomes:
|
2012-11-06 04:52:35 +00:00
|
|
|
-
|
|
|
|
- git push <--> git-annex xmppgit <--> xmppPush <-------> xmpp
|
|
|
|
- |
|
|
|
|
- git receive-pack <--> xmppReceivePack <---------------> xmpp
|
|
|
|
-
|
|
|
|
- The pipe between git-annex xmppgit and us is set up and communicated
|
2012-11-10 04:08:15 +00:00
|
|
|
- using two environment variables, relayIn and relayOut, that are set
|
|
|
|
- to the file descriptors to use. Another, relayControl, is used to
|
|
|
|
- propigate the exit status of git receive-pack.
|
2012-11-06 04:52:35 +00:00
|
|
|
-
|
|
|
|
- We listen at the other end of the pipe and relay to and from XMPP.
|
|
|
|
-}
|
2013-05-21 15:06:49 +00:00
|
|
|
xmppPush :: ClientID -> (Git.Repo -> IO Bool) -> Assistant Bool
|
2013-05-22 19:13:31 +00:00
|
|
|
xmppPush cid gitpush = do
|
2013-04-30 17:22:55 +00:00
|
|
|
u <- liftAnnex getUUID
|
|
|
|
sendNetMessage $ Pushing cid (StartingPush u)
|
2012-11-06 14:46:58 +00:00
|
|
|
|
|
|
|
(Fd inf, writepush) <- liftIO createPipe
|
|
|
|
(readpush, Fd outf) <- liftIO createPipe
|
|
|
|
(Fd controlf, writecontrol) <- liftIO createPipe
|
|
|
|
|
2013-05-09 19:23:40 +00:00
|
|
|
tmpdir <- gettmpdir
|
2012-11-09 16:51:54 +00:00
|
|
|
installwrapper tmpdir
|
|
|
|
|
2012-11-06 14:46:58 +00:00
|
|
|
env <- liftIO getEnvironment
|
2012-11-09 16:51:54 +00:00
|
|
|
path <- liftIO getSearchPath
|
2012-11-09 19:03:16 +00:00
|
|
|
let myenv = M.fromList
|
2013-04-23 00:24:53 +00:00
|
|
|
[ ("PATH", intercalate [searchPathSeparator] $ tmpdir:path)
|
2012-11-06 14:46:58 +00:00
|
|
|
, (relayIn, show inf)
|
|
|
|
, (relayOut, show outf)
|
|
|
|
, (relayControl, show controlf)
|
|
|
|
]
|
2012-11-09 19:03:16 +00:00
|
|
|
`M.union` M.fromList env
|
2012-11-06 14:46:58 +00:00
|
|
|
|
|
|
|
inh <- liftIO $ fdToHandle readpush
|
|
|
|
outh <- liftIO $ fdToHandle writepush
|
|
|
|
controlh <- liftIO $ fdToHandle writecontrol
|
2012-11-06 04:52:35 +00:00
|
|
|
|
2013-04-10 22:39:56 +00:00
|
|
|
t1 <- forkIO <~> toxmpp 0 inh
|
2012-11-08 20:44:23 +00:00
|
|
|
t2 <- forkIO <~> fromxmpp outh controlh
|
2012-11-06 14:46:58 +00:00
|
|
|
|
2012-11-09 19:03:16 +00:00
|
|
|
{- This can take a long time to run, so avoid running it in the
|
|
|
|
- Annex monad. Also, override environment. -}
|
|
|
|
g <- liftAnnex gitRepo
|
2012-11-10 18:38:50 +00:00
|
|
|
r <- liftIO $ gitpush $ g { gitEnv = Just $ M.toList myenv }
|
2012-11-09 19:03:16 +00:00
|
|
|
|
2012-11-10 03:27:07 +00:00
|
|
|
liftIO $ do
|
|
|
|
mapM_ killThread [t1, t2]
|
|
|
|
mapM_ hClose [inh, outh, controlh]
|
2013-05-27 01:27:44 +00:00
|
|
|
mapM_ closeFd [Fd inf, Fd outf, Fd controlf]
|
2013-05-22 01:40:37 +00:00
|
|
|
|
2012-11-10 03:43:08 +00:00
|
|
|
return r
|
2012-11-06 14:46:58 +00:00
|
|
|
where
|
2013-04-10 22:39:56 +00:00
|
|
|
toxmpp seqnum inh = do
|
2012-11-09 21:40:59 +00:00
|
|
|
b <- liftIO $ B.hGetSome inh chunkSize
|
2012-11-08 20:44:23 +00:00
|
|
|
if B.null b
|
|
|
|
then liftIO $ killThread =<< myThreadId
|
2013-04-10 22:39:56 +00:00
|
|
|
else do
|
|
|
|
let seqnum' = succ seqnum
|
|
|
|
sendNetMessage $ Pushing cid $
|
|
|
|
SendPackOutput seqnum' b
|
|
|
|
toxmpp seqnum' inh
|
2013-05-21 01:58:59 +00:00
|
|
|
|
2013-05-21 15:06:49 +00:00
|
|
|
fromxmpp outh controlh = withPushMessagesInSequence cid SendPack handle
|
2013-05-21 01:58:59 +00:00
|
|
|
where
|
|
|
|
handle (Just (Pushing _ (ReceivePackOutput _ b))) =
|
|
|
|
liftIO $ writeChunk outh b
|
|
|
|
handle (Just (Pushing _ (ReceivePackDone exitcode))) =
|
|
|
|
liftIO $ do
|
|
|
|
hPrint controlh exitcode
|
|
|
|
hFlush controlh
|
|
|
|
handle (Just _) = noop
|
|
|
|
handle Nothing = do
|
|
|
|
debug ["timeout waiting for git receive-pack output via XMPP"]
|
|
|
|
-- Send a synthetic exit code to git-annex
|
|
|
|
-- xmppgit, which will exit and cause git push
|
|
|
|
-- to die.
|
|
|
|
liftIO $ do
|
|
|
|
hPrint controlh (ExitFailure 1)
|
|
|
|
hFlush controlh
|
2013-05-22 01:40:37 +00:00
|
|
|
killThread =<< myThreadId
|
2013-05-21 01:58:59 +00:00
|
|
|
|
2012-11-09 16:51:54 +00:00
|
|
|
installwrapper tmpdir = liftIO $ do
|
|
|
|
createDirectoryIfMissing True tmpdir
|
|
|
|
let wrapper = tmpdir </> "git-remote-xmpp"
|
|
|
|
program <- readProgramFile
|
|
|
|
writeFile wrapper $ unlines
|
2013-05-06 19:58:13 +00:00
|
|
|
[ shebang_local
|
2012-11-09 16:51:54 +00:00
|
|
|
, "exec " ++ program ++ " xmppgit"
|
|
|
|
]
|
|
|
|
modifyFileMode wrapper $ addModes executeModes
|
2013-05-21 01:58:59 +00:00
|
|
|
|
2013-05-09 19:23:40 +00:00
|
|
|
{- Use GIT_ANNEX_TMP_DIR if set, since that may be a better temp
|
|
|
|
- dir (ie, not on a crippled filesystem where we can't make
|
|
|
|
- the wrapper executable). -}
|
|
|
|
gettmpdir = do
|
|
|
|
v <- liftIO $ getEnv "GIT_ANNEX_TMP_DIR"
|
|
|
|
case v of
|
|
|
|
Nothing -> do
|
|
|
|
tmp <- liftAnnex $ fromRepo gitAnnexTmpDir
|
|
|
|
return $ tmp </> "xmppgit"
|
|
|
|
Just d -> return $ d </> "xmppgit"
|
2012-11-06 04:52:35 +00:00
|
|
|
|
2012-11-10 04:08:15 +00:00
|
|
|
type EnvVar = String
|
2012-11-06 04:52:35 +00:00
|
|
|
|
2012-11-10 04:08:15 +00:00
|
|
|
envVar :: String -> EnvVar
|
|
|
|
envVar s = "GIT_ANNEX_XMPPGIT_" ++ s
|
2012-11-06 04:52:35 +00:00
|
|
|
|
2012-11-10 04:08:15 +00:00
|
|
|
relayIn :: EnvVar
|
|
|
|
relayIn = envVar "IN"
|
2012-11-06 04:52:35 +00:00
|
|
|
|
2012-11-10 04:08:15 +00:00
|
|
|
relayOut :: EnvVar
|
|
|
|
relayOut = envVar "OUT"
|
|
|
|
|
|
|
|
relayControl :: EnvVar
|
|
|
|
relayControl = envVar "CONTROL"
|
|
|
|
|
|
|
|
relayHandle :: EnvVar -> IO Handle
|
2012-11-06 14:14:00 +00:00
|
|
|
relayHandle var = do
|
2012-11-06 04:52:35 +00:00
|
|
|
v <- getEnv var
|
|
|
|
case readish =<< v of
|
|
|
|
Nothing -> error $ var ++ " not set"
|
2012-11-06 14:14:00 +00:00
|
|
|
Just n -> fdToHandle $ Fd n
|
2012-11-06 04:52:35 +00:00
|
|
|
|
2012-11-09 21:40:59 +00:00
|
|
|
{- Called by git-annex xmppgit.
|
|
|
|
-
|
|
|
|
- git-push is talking to us on stdin
|
|
|
|
- we're talking to git-push on stdout
|
|
|
|
- git-receive-pack is talking to us on relayIn (via XMPP)
|
|
|
|
- we're talking to git-receive-pack on relayOut (via XMPP)
|
2012-11-10 04:02:55 +00:00
|
|
|
- git-receive-pack's exit code will be passed to us on relayControl
|
2012-11-09 21:40:59 +00:00
|
|
|
-}
|
2012-11-06 04:52:35 +00:00
|
|
|
xmppGitRelay :: IO ()
|
|
|
|
xmppGitRelay = do
|
2012-11-10 03:12:54 +00:00
|
|
|
flip relay stdout =<< relayHandle relayIn
|
|
|
|
relay stdin =<< relayHandle relayOut
|
2012-11-10 04:02:55 +00:00
|
|
|
code <- hGetLine =<< relayHandle relayControl
|
|
|
|
exitWith $ fromMaybe (ExitFailure 1) $ readish code
|
2012-11-10 03:12:54 +00:00
|
|
|
where
|
|
|
|
{- Is it possible to set up pipes and not need to copy the data
|
|
|
|
- ourselves? See splice(2) -}
|
|
|
|
relay fromh toh = void $ forkIO $ forever $ do
|
|
|
|
b <- B.hGetSome fromh chunkSize
|
|
|
|
when (B.null b) $ do
|
|
|
|
hClose fromh
|
|
|
|
hClose toh
|
|
|
|
killThread =<< myThreadId
|
2012-11-10 04:24:26 +00:00
|
|
|
writeChunk toh b
|
2012-11-05 21:43:17 +00:00
|
|
|
|
2012-11-08 18:02:37 +00:00
|
|
|
{- Relays git receive-pack stdin and stdout via XMPP, as well as propigating
|
|
|
|
- its exit status to XMPP. -}
|
2013-05-21 15:06:49 +00:00
|
|
|
xmppReceivePack :: ClientID -> Assistant Bool
|
2013-05-22 19:13:31 +00:00
|
|
|
xmppReceivePack cid = do
|
2012-11-06 20:36:44 +00:00
|
|
|
repodir <- liftAnnex $ fromRepo repoPath
|
|
|
|
let p = (proc "git" ["receive-pack", repodir])
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = Inherit
|
|
|
|
}
|
2012-11-10 04:13:55 +00:00
|
|
|
(Just inh, Just outh, _, pid) <- liftIO $ createProcess p
|
|
|
|
readertid <- forkIO <~> relayfromxmpp inh
|
2013-04-10 22:39:56 +00:00
|
|
|
relaytoxmpp 0 outh
|
2012-11-10 04:13:55 +00:00
|
|
|
code <- liftIO $ waitForProcess pid
|
2012-11-10 16:18:00 +00:00
|
|
|
void $ sendNetMessage $ Pushing cid $ ReceivePackDone code
|
2012-11-06 20:36:44 +00:00
|
|
|
liftIO $ do
|
2012-11-09 21:40:59 +00:00
|
|
|
killThread readertid
|
2012-11-10 03:27:07 +00:00
|
|
|
hClose inh
|
|
|
|
hClose outh
|
2012-11-06 20:36:44 +00:00
|
|
|
return $ code == ExitSuccess
|
2012-11-06 20:08:36 +00:00
|
|
|
where
|
2013-04-10 22:39:56 +00:00
|
|
|
relaytoxmpp seqnum outh = do
|
2012-11-09 21:40:59 +00:00
|
|
|
b <- liftIO $ B.hGetSome outh chunkSize
|
2012-11-10 03:27:07 +00:00
|
|
|
-- empty is EOF, so exit
|
|
|
|
unless (B.null b) $ do
|
2013-04-10 22:39:56 +00:00
|
|
|
let seqnum' = succ seqnum
|
|
|
|
sendNetMessage $ Pushing cid $ ReceivePackOutput seqnum' b
|
|
|
|
relaytoxmpp seqnum' outh
|
2013-05-21 15:06:49 +00:00
|
|
|
relayfromxmpp inh = withPushMessagesInSequence cid ReceivePack handle
|
2013-05-21 01:58:59 +00:00
|
|
|
where
|
|
|
|
handle (Just (Pushing _ (SendPackOutput _ b))) =
|
|
|
|
liftIO $ writeChunk inh b
|
|
|
|
handle (Just _) = noop
|
|
|
|
handle Nothing = do
|
|
|
|
debug ["timeout waiting for git send-pack output via XMPP"]
|
|
|
|
-- closing the handle will make git receive-pack exit
|
|
|
|
liftIO $ do
|
|
|
|
hClose inh
|
|
|
|
killThread =<< myThreadId
|
2012-11-08 20:44:23 +00:00
|
|
|
|
2013-04-30 17:22:55 +00:00
|
|
|
xmppRemotes :: ClientID -> UUID -> Assistant [Remote]
|
|
|
|
xmppRemotes cid theiruuid = case baseJID <$> parseJID cid of
|
2012-11-08 20:44:23 +00:00
|
|
|
Nothing -> return []
|
|
|
|
Just jid -> do
|
2012-11-10 03:43:08 +00:00
|
|
|
let loc = gitXMPPLocation jid
|
2013-04-30 17:22:55 +00:00
|
|
|
um <- liftAnnex uuidMap
|
|
|
|
filter (matching loc . Remote.repo) . filter (knownuuid um) . syncGitRemotes
|
2012-11-10 03:43:08 +00:00
|
|
|
<$> getDaemonStatus
|
2012-11-08 20:44:23 +00:00
|
|
|
where
|
2012-11-10 03:43:08 +00:00
|
|
|
matching loc r = repoIsUrl r && repoLocation r == loc
|
2013-04-30 17:22:55 +00:00
|
|
|
knownuuid um r = Remote.uuid r == theiruuid || M.member theiruuid um
|
2012-11-08 20:44:23 +00:00
|
|
|
|
2013-05-22 19:13:31 +00:00
|
|
|
{- Returns the ClientID that it pushed to. -}
|
|
|
|
runPush :: (Remote -> Assistant ()) -> NetMessage -> Assistant (Maybe ClientID)
|
|
|
|
runPush checkcloudrepos (Pushing cid (PushRequest theiruuid)) =
|
2012-11-10 18:38:50 +00:00
|
|
|
go =<< liftAnnex (inRepo Git.Branch.current)
|
|
|
|
where
|
2013-05-22 19:13:31 +00:00
|
|
|
go Nothing = return Nothing
|
2012-11-10 18:38:50 +00:00
|
|
|
go (Just branch) = do
|
2013-04-30 17:22:55 +00:00
|
|
|
rs <- xmppRemotes cid theiruuid
|
2012-11-10 18:38:50 +00:00
|
|
|
liftAnnex $ Annex.Branch.commit "update"
|
|
|
|
(g, u) <- liftAnnex $ (,)
|
|
|
|
<$> gitRepo
|
|
|
|
<*> getUUID
|
|
|
|
liftIO $ Command.Sync.updateBranch (Command.Sync.syncBranch branch) g
|
2013-03-06 20:29:19 +00:00
|
|
|
selfjid <- ((T.unpack <$>) . xmppClientID) <$> getDaemonStatus
|
2013-05-22 19:13:31 +00:00
|
|
|
if null rs
|
|
|
|
then return Nothing
|
|
|
|
else do
|
|
|
|
forM_ rs $ \r -> do
|
|
|
|
void $ alertWhile (syncAlert [r]) $
|
|
|
|
xmppPush cid (taggedPush u selfjid branch r)
|
|
|
|
checkcloudrepos r
|
|
|
|
return $ Just cid
|
|
|
|
runPush checkcloudrepos (Pushing cid (StartingPush theiruuid)) = do
|
2013-04-30 17:22:55 +00:00
|
|
|
rs <- xmppRemotes cid theiruuid
|
2013-05-22 19:13:31 +00:00
|
|
|
if null rs
|
|
|
|
then return Nothing
|
|
|
|
else do
|
|
|
|
void $ alertWhile (syncAlert rs) $
|
|
|
|
xmppReceivePack cid
|
|
|
|
mapM_ checkcloudrepos rs
|
|
|
|
return $ Just cid
|
|
|
|
runPush _ _ = return Nothing
|
2012-11-09 21:40:59 +00:00
|
|
|
|
2013-05-21 22:24:29 +00:00
|
|
|
{- Check if any of the shas that can be pushed are ones we do not
|
|
|
|
- have.
|
|
|
|
-
|
|
|
|
- (Older clients send no shas, so when there are none, always
|
|
|
|
- request a push.)
|
|
|
|
-}
|
XMPP: Be better at responding to CanPush messages when busy with something else.
Observed: With 2 xmpp clients, one would sometimes stop responding
to CanPush messages. Often it was in the middle of a receive-pack
of its own (or was waiting for a failed one to time out).
Now these are always immediately responded to, which is fine; the point
of CanPush is to find out if there's another client out there that's
interested in our push.
Also, in queueNetPushMessage, queue push initiation messages when
we're already running the side of the push they would initiate.
Before, these messages were sent into the netMessagesPush channel,
which was wrong. The xmpp send-pack and receive-pack code discarded
such messages.
This still doesn't make XMPP push 100% robust. In testing, I am seeing
it sometimes try to run two send-packs, or two receive-packs at once
to the same client (probably because the client sent two requests).
Also, I'm seeing rather a lot of cases where it stalls out until it
runs into the 120 second timeout and cancels a push.
And finally, there seems to be a bug in runPush. I have logs that
show it running its setup action, but never its cleanup action.
How is this possible given its use of E.bracket? Either some exception
is finding its way through, or the action somehow stalls forever.
When this happens, one of the 2 clients stops syncing.
2013-05-21 04:59:38 +00:00
|
|
|
handlePushNotice :: NetMessage -> Assistant ()
|
2013-05-21 22:24:29 +00:00
|
|
|
handlePushNotice (Pushing cid (CanPush theiruuid shas)) =
|
|
|
|
unlessM (null <$> xmppRemotes cid theiruuid) $
|
|
|
|
if null shas
|
|
|
|
then go
|
|
|
|
else ifM (haveall shas)
|
|
|
|
( debug ["ignoring CanPush with known shas"]
|
|
|
|
, go
|
|
|
|
)
|
|
|
|
where
|
|
|
|
go = do
|
XMPP: Be better at responding to CanPush messages when busy with something else.
Observed: With 2 xmpp clients, one would sometimes stop responding
to CanPush messages. Often it was in the middle of a receive-pack
of its own (or was waiting for a failed one to time out).
Now these are always immediately responded to, which is fine; the point
of CanPush is to find out if there's another client out there that's
interested in our push.
Also, in queueNetPushMessage, queue push initiation messages when
we're already running the side of the push they would initiate.
Before, these messages were sent into the netMessagesPush channel,
which was wrong. The xmpp send-pack and receive-pack code discarded
such messages.
This still doesn't make XMPP push 100% robust. In testing, I am seeing
it sometimes try to run two send-packs, or two receive-packs at once
to the same client (probably because the client sent two requests).
Also, I'm seeing rather a lot of cases where it stalls out until it
runs into the 120 second timeout and cancels a push.
And finally, there seems to be a bug in runPush. I have logs that
show it running its setup action, but never its cleanup action.
How is this possible given its use of E.bracket? Either some exception
is finding its way through, or the action somehow stalls forever.
When this happens, one of the 2 clients stops syncing.
2013-05-21 04:59:38 +00:00
|
|
|
u <- liftAnnex getUUID
|
|
|
|
sendNetMessage $ Pushing cid (PushRequest u)
|
2013-05-21 22:24:29 +00:00
|
|
|
haveall l = liftAnnex $ not <$> anyM donthave l
|
|
|
|
donthave sha = isNothing <$> catObjectDetails sha
|
XMPP: Be better at responding to CanPush messages when busy with something else.
Observed: With 2 xmpp clients, one would sometimes stop responding
to CanPush messages. Often it was in the middle of a receive-pack
of its own (or was waiting for a failed one to time out).
Now these are always immediately responded to, which is fine; the point
of CanPush is to find out if there's another client out there that's
interested in our push.
Also, in queueNetPushMessage, queue push initiation messages when
we're already running the side of the push they would initiate.
Before, these messages were sent into the netMessagesPush channel,
which was wrong. The xmpp send-pack and receive-pack code discarded
such messages.
This still doesn't make XMPP push 100% robust. In testing, I am seeing
it sometimes try to run two send-packs, or two receive-packs at once
to the same client (probably because the client sent two requests).
Also, I'm seeing rather a lot of cases where it stalls out until it
runs into the 120 second timeout and cancels a push.
And finally, there seems to be a bug in runPush. I have logs that
show it running its setup action, but never its cleanup action.
How is this possible given its use of E.bracket? Either some exception
is finding its way through, or the action somehow stalls forever.
When this happens, one of the 2 clients stops syncing.
2013-05-21 04:59:38 +00:00
|
|
|
handlePushNotice _ = noop
|
|
|
|
|
2012-11-10 04:24:26 +00:00
|
|
|
writeChunk :: Handle -> B.ByteString -> IO ()
|
|
|
|
writeChunk h b = do
|
|
|
|
B.hPut h b
|
|
|
|
hFlush h
|
2012-11-11 17:38:28 +00:00
|
|
|
|
2013-05-21 01:58:59 +00:00
|
|
|
{- Gets NetMessages for a PushSide, ensures they are in order,
|
|
|
|
- and runs an action to handle each in turn. The action will be passed
|
|
|
|
- Nothing on timeout.
|
2012-11-11 17:38:28 +00:00
|
|
|
-
|
2013-05-21 01:58:59 +00:00
|
|
|
- Does not currently reorder messages, but does ensure that any
|
|
|
|
- duplicate messages, or messages not in the sequence, are discarded.
|
2012-11-11 17:38:28 +00:00
|
|
|
-}
|
2013-05-21 15:06:49 +00:00
|
|
|
withPushMessagesInSequence :: ClientID -> PushSide -> (Maybe NetMessage -> Assistant ()) -> Assistant ()
|
|
|
|
withPushMessagesInSequence cid side a = loop 0
|
2013-05-21 01:58:59 +00:00
|
|
|
where
|
|
|
|
loop seqnum = do
|
2013-05-21 15:06:49 +00:00
|
|
|
m <- timeout xmppTimeout <~> waitInbox cid side
|
2013-05-21 01:58:59 +00:00
|
|
|
let go s = a m >> loop s
|
2013-07-08 16:53:08 +00:00
|
|
|
let next = seqnum + 1
|
2013-05-21 01:58:59 +00:00
|
|
|
case extractSequence =<< m of
|
|
|
|
Just seqnum'
|
2013-07-08 16:53:08 +00:00
|
|
|
| seqnum' == next -> go next
|
2013-05-21 01:58:59 +00:00
|
|
|
| seqnum' == 0 -> go seqnum
|
|
|
|
| seqnum' == seqnum -> do
|
|
|
|
debug ["ignoring duplicate sequence number", show seqnum]
|
|
|
|
loop seqnum
|
|
|
|
| otherwise -> do
|
2013-07-08 16:53:08 +00:00
|
|
|
debug ["ignoring out of order sequence number", show seqnum', "expected", show next]
|
2013-05-21 01:58:59 +00:00
|
|
|
loop seqnum
|
|
|
|
Nothing -> go seqnum
|
|
|
|
|
|
|
|
extractSequence :: NetMessage -> Maybe Int
|
|
|
|
extractSequence (Pushing _ (ReceivePackOutput seqnum _)) = Just seqnum
|
|
|
|
extractSequence (Pushing _ (SendPackOutput seqnum _)) = Just seqnum
|
|
|
|
extractSequence _ = Nothing
|