split fediChordJoin into general purpose and bootstrapping part

This commit is contained in:
Trolli Schmittlauch 2020-05-26 11:01:11 +02:00
parent 43eb04dfea
commit 702684b1a9
2 changed files with 24 additions and 15 deletions

View file

@ -38,6 +38,7 @@ module Hash2Pub.FediChord (
, FediChordConf(..) , FediChordConf(..)
, fediChordInit , fediChordInit
, fediChordJoin , fediChordJoin
, fediChordBootstrapJoin
, nodeStateInit , nodeStateInit
, mkServerSocket , mkServerSocket
, mkSendSocket , mkSendSocket
@ -110,11 +111,13 @@ nodeStateInit conf = do
} }
pure initialState pure initialState
fediChordJoin :: LocalNodeState -- ^ the local 'NodeState' -- | Join a new node into the DHT, using a provided bootstrap node as initial cache seed
-- for resolving the new node's position.
fediChordBootstrapJoin :: LocalNodeState -- ^ the local 'NodeState'
-> (String, PortNumber) -- ^ domain and port of a bootstrapping node -> (String, PortNumber) -- ^ domain and port of a bootstrapping node
-> IO (Either String LocalNodeState) -- ^ the joined 'NodeState' after a -> IO (Either String LocalNodeState) -- ^ the joined 'NodeState' after a
-- successful join, otherwise an error message -- successful join, otherwise an error message
fediChordJoin ns (joinHost, joinPort) = do fediChordBootstrapJoin ns (joinHost, joinPort) = do
-- can be invoked multiple times with all known bootstrapping nodes until successfully joined -- can be invoked multiple times with all known bootstrapping nodes until successfully joined
sock <- mkSendSocket joinHost joinPort sock <- mkSendSocket joinHost joinPort
-- 1. get routed to placement of own ID until FOUND: -- 1. get routed to placement of own ID until FOUND:
@ -133,20 +136,26 @@ fediChordJoin ns (joinHost, joinPort) = do
Just (FORWARD resultset) -> foldr' (addCacheEntryPure now) cacheAcc resultset Just (FORWARD resultset) -> foldr' (addCacheEntryPure now) cacheAcc resultset
) )
initCache bootstrapResponse initCache bootstrapResponse
fediChordJoin bootstrapCache ns
-- | join a node to the DHT, using the provided cache snapshot for resolving the new
-- node's position.
fediChordJoin :: NodeCache -- ^ a snapshot of the NodeCache to
-- use for ID lookup
-> LocalNodeState -- ^ the local 'NodeState'
-> IO (Either String LocalNodeState) -- ^ the joined 'NodeState' after a
-- successful join, otherwise an error message
fediChordJoin cacheSnapshot ns = do
-- get routed to the currently responsible node, based on the response -- get routed to the currently responsible node, based on the response
-- from the bootstrapping node -- from the bootstrapping node
currentlyResponsible <- queryIdLookupLoop bootstrapCache ns $ getNid ns currentlyResponsible <- queryIdLookupLoop cacheSnapshot ns $ getNid ns
-- do actual join -- 2. then send a join to the currently responsible node
joinResult <- requestJoin currentlyResponsible ns joinResult <- requestJoin currentlyResponsible ns
case joinResult of case joinResult of
Nothing -> pure . Left $ "Error joining on " <> show currentlyResponsible Nothing -> pure . Left $ "Error joining on " <> show currentlyResponsible
Just joinedNS -> pure . Right $ joinedNS Just joinedNS -> pure . Right $ joinedNS
-- 2. then send a join to the currently responsible node
-- after successful join, finally add own node to the cache
-- | cache updater thread that waits for incoming NodeCache update instructions on -- | cache updater thread that waits for incoming NodeCache update instructions on
-- the node's cacheWriteQueue and then modifies the NodeCache as the single writer. -- the node's cacheWriteQueue and then modifies the NodeCache as the single writer.
cacheWriter :: LocalNodeState -> IO () cacheWriter :: LocalNodeState -> IO ()

View file

@ -18,7 +18,7 @@ main = do
-- currently no masking is necessary, as there is nothing to clean up -- currently no masking is necessary, as there is nothing to clean up
cacheWriterThread <- forkIO $ cacheWriter thisNode cacheWriterThread <- forkIO $ cacheWriter thisNode
-- idea: list of bootstrapping nodes, try joining within a timeout -- idea: list of bootstrapping nodes, try joining within a timeout
joinedState <- fediChordJoin thisNode $ head . confBootstrapNodes $ conf joinedState <- fediChordBootstrapJoin thisNode $ head . confBootstrapNodes $ conf
-- stop main thread from terminating during development -- stop main thread from terminating during development
getChar getChar
pure () pure ()