From edf66e1b517354c22282b0c9d18461545ae6b875 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Thu, 2 Jul 2020 01:36:31 +0200 Subject: [PATCH 1/2] add som debug prints --- app/Main.hs | 1 + src/Hash2Pub/DHTProtocol.hs | 1 + src/Hash2Pub/FediChord.hs | 8 +++++++- src/Hash2Pub/FediChordTypes.hs | 2 ++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/Main.hs b/app/Main.hs index c712f55..36e79c5 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -35,6 +35,7 @@ main = do -- handle unsuccessful join putStrLn $ err <> " Error joining, start listening for incoming requests anyways" + print =<< readTVarIO thisNode wait =<< async (fediMainThreads serverSock thisNode) -- TODO: periodic retry ) diff --git a/src/Hash2Pub/DHTProtocol.hs b/src/Hash2Pub/DHTProtocol.hs index e562b98..f431da9 100644 --- a/src/Hash2Pub/DHTProtocol.hs +++ b/src/Hash2Pub/DHTProtocol.hs @@ -246,6 +246,7 @@ handleIncomingRequest :: LocalNodeStateSTM -- ^ the handling -> SockAddr -- ^ source address of the request -> IO () handleIncomingRequest nsSTM sendQ msgSet sourceAddr = do + putStrLn $ "handling incoming request: " <> show msgSet ns <- readTVarIO nsSTM -- add nodestate to cache now <- getPOSIXTime diff --git a/src/Hash2Pub/FediChord.hs b/src/Hash2Pub/FediChord.hs index 061a74f..6667a3e 100644 --- a/src/Hash2Pub/FediChord.hs +++ b/src/Hash2Pub/FediChord.hs @@ -150,7 +150,6 @@ fediChordBootstrapJoin nsSTM (joinHost, joinPort) = Just (FORWARD resultset) -> foldr' (addCacheEntryPure now) cacheAcc resultset ) initCache bootstrapResponse - putStrLn "initialised bootstrap cache" fediChordJoin bootstrapCache nsSTM ) `catch` (\e -> pure . Left $ "Error at bootstrap joining: " <> displayException (e :: IOException)) @@ -192,6 +191,7 @@ maxEntryAge = 600 -- | Periodically iterate through cache, clean up expired entries and verify unverified ones cacheVerifyThread :: LocalNodeStateSTM -> IO () cacheVerifyThread nsSTM = forever $ do + putStrLn "cache verify run: begin" -- get cache (ns, cache) <- atomically $ do ns <- readTVar nsSTM @@ -239,10 +239,12 @@ cacheVerifyThread nsSTM = forever $ do let nodesToQuery targetID = case queryLocalCache latestNs latestCache (lNumBestNodes latestNs) targetID of FOUND node -> [node] FORWARD nodeSet -> remoteNode <$> Set.elems nodeSet + print $ checkCacheSliceInvariants latestNs latestCache forM_ (checkCacheSliceInvariants latestNs latestCache) (\targetID -> forkIO $ sendQueryIdMessages targetID latestNs (Just (1 + jEntriesPerSlice latestNs)) (nodesToQuery targetID) >> pure () -- ask for 1 entry more than j because of querying the middle ) + putStrLn "cache verify run: end" threadDelay $ 10^6 * round maxEntryAge `div` 20 @@ -309,6 +311,8 @@ stabiliseThread :: LocalNodeStateSTM -> IO () stabiliseThread nsSTM = forever $ do ns <- readTVarIO nsSTM + putStrLn "stabilise run: begin" + -- iterate through the same snapshot, collect potential new neighbours -- and nodes to be deleted, and modify these changes only at the end of -- each stabilise run. @@ -356,6 +360,7 @@ stabiliseThread nsSTM = forever $ do writeTVar nsSTM $ addSuccessors [nextEntry] latestNs ) + putStrLn "stabilise run: end" -- TODO: make delay configurable threadDelay (60 * 10^6) where @@ -420,6 +425,7 @@ sendThread sock sendQ = forever $ do -- | Sets up and manages the main server threads of FediChord fediMainThreads :: Socket -> LocalNodeStateSTM -> IO () fediMainThreads sock nsSTM = do + (\x -> putStrLn $ "launching threads, ns: " <> show x) =<< readTVarIO nsSTM sendQ <- newTQueueIO recvQ <- newTQueueIO -- concurrently launch all handler threads, if one of them throws an exception diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index fbd3295..660eb49 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -80,6 +80,8 @@ import qualified Network.ByteOrder as NetworkBytes import Hash2Pub.Utils +import Debug.Trace (trace) + -- define protocol constants From e06c53ff7cd334f94c8ee097ff20f4996f6b0900 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Thu, 2 Jul 2020 01:38:51 +0200 Subject: [PATCH 2/2] stylish fixes --- src/Hash2Pub/FediChord.hs | 6 +++--- test/FediChordSpec.hs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Hash2Pub/FediChord.hs b/src/Hash2Pub/FediChord.hs index 6667a3e..152abd9 100644 --- a/src/Hash2Pub/FediChord.hs +++ b/src/Hash2Pub/FediChord.hs @@ -231,7 +231,7 @@ cacheVerifyThread nsSTM = forever $ do ) pong else pure () ) - + -- check the cache invariant per slice and, if necessary, do a single lookup to the -- middle of each slice not verifying the invariant latestNs <- readTVarIO nsSTM @@ -243,7 +243,7 @@ cacheVerifyThread nsSTM = forever $ do forM_ (checkCacheSliceInvariants latestNs latestCache) (\targetID -> forkIO $ sendQueryIdMessages targetID latestNs (Just (1 + jEntriesPerSlice latestNs)) (nodesToQuery targetID) >> pure () -- ask for 1 entry more than j because of querying the middle ) - + putStrLn "cache verify run: end" threadDelay $ 10^6 * round maxEntryAge `div` 20 @@ -255,7 +255,7 @@ checkCacheSliceInvariants :: LocalNodeState -> NodeCache -> [NodeID] -- ^ list of middle IDs of slices not -- ^ fulfilling the invariant -checkCacheSliceInvariants ns +checkCacheSliceInvariants ns | isJoined ns = checkPredecessorSlice jEntries (getNid ns) startBound lastPred <> checkSuccessorSlice jEntries (getNid ns) startBound lastSucc | otherwise = const [] where diff --git a/test/FediChordSpec.hs b/test/FediChordSpec.hs index 545c3dd..1e94628 100644 --- a/test/FediChordSpec.hs +++ b/test/FediChordSpec.hs @@ -276,7 +276,7 @@ spec = do ownNode <- setNid ownId <$> exampleLocalNode let (FORWARD qResult) = queryLocalCache ownNode bootstrapCache 2 ownId remoteNode (head $ Set.elems qResult) `shouldBe` bootstrapNode - + -- some example data