Skip to content

Commit f354a4f

Browse files
committed
Debugging
1 parent cd55056 commit f354a4f

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

src/Share/Postgres.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ import Share.Utils.Logging qualified as Logging
100100
import Share.Web.App
101101
import Share.Web.Errors (ErrorID (..), SomeServerError, ToServerError (..), internalServerError, respondError, someServerError)
102102
import System.CPUTime (getCPUTime)
103+
import Unison.Debug qualified as UDebug
103104

104105
data TransactionError e
105106
= Unrecoverable SomeServerError
@@ -333,7 +334,9 @@ instance QueryA (Transaction e) where
333334
statement q s = do
334335
transactionStatement q s
335336

336-
unrecoverableError e = Transaction (pure (Left (Unrecoverable (someServerError e))))
337+
unrecoverableError e = do
338+
UDebug.debugM UDebug.Temp "Unrecoverable error in transaction: " e
339+
Transaction (pure (Left (Unrecoverable (someServerError e))))
337340

338341
instance QueryM (Transaction e) where
339342
transactionUnsafeIO io = Transaction (Right <$> liftIO io)
@@ -342,7 +345,9 @@ instance QueryA (Session e) where
342345
statement q s = do
343346
lift $ Session.statement q s
344347

345-
unrecoverableError e = throwError (Unrecoverable (someServerError e))
348+
unrecoverableError e = do
349+
UDebug.debugM UDebug.Temp "Unrecoverable error in transaction: " e
350+
throwError (Unrecoverable (someServerError e))
346351

347352
instance QueryM (Session e) where
348353
transactionUnsafeIO io = lift $ liftIO io
@@ -355,7 +360,8 @@ instance QueryA (Pipeline e) where
355360
instance (QueryM m) => QueryA (ReaderT e m) where
356361
statement q s = lift $ statement q s
357362

358-
unrecoverableError e = lift $ unrecoverableError e
363+
unrecoverableError e = do
364+
lift $ unrecoverableError e
359365

360366
instance (QueryM m) => QueryM (ReaderT e m) where
361367
transactionUnsafeIO io = lift $ transactionUnsafeIO io

src/Share/Web/UCM/SyncV2/Impl.hs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Control.Concurrent.STM qualified as STM
88
import Control.Concurrent.STM.TBMQueue qualified as STM
99
import Control.Monad.Except (ExceptT (ExceptT))
1010
import Control.Monad.Trans.Except (runExceptT)
11-
import Data.Conduit.Combinators qualified as Conduit
11+
import Data.List.NonEmpty qualified as NEL
1212
import Servant
1313
import Servant.Conduit (ConduitToSourceIO (..))
1414
import Servant.Types.SourceT (SourceT (..))
@@ -31,16 +31,20 @@ import Share.Web.Errors
3131
import Share.Web.UCM.Sync.HashJWT qualified as HashJWT
3232
import Share.Web.UCM.SyncV2.Queries qualified as SSQ
3333
import U.Codebase.Sqlite.Orphans ()
34-
import U.Codebase.Sqlite.TempEntity (TempEntity)
3534
import Unison.Debug qualified as Debug
36-
import Unison.Hash32 (Hash32)
3735
import Unison.Share.API.Hash (HashJWTClaims (..))
3836
import Unison.SyncV2.API qualified as SyncV2
39-
import Unison.SyncV2.Types (DownloadEntitiesChunk (..), EntityChunk (..), ErrorChunk (..))
37+
import Unison.SyncV2.Types (DownloadEntitiesChunk (..), EntityChunk (..), ErrorChunk (..), StreamInitInfo (..))
4038
import Unison.SyncV2.Types qualified as SyncV2
4139
import UnliftIO qualified
4240
import UnliftIO.Async qualified as Async
4341

42+
batchSize :: Int32
43+
batchSize = 1000
44+
45+
streamSettings :: StreamInitInfo
46+
streamSettings = StreamInitInfo {version = SyncV2.Version 1, entitySorting = SyncV2.Unsorted, numEntities = Nothing}
47+
4448
server :: Maybe UserId -> SyncV2.Routes WebAppServer
4549
server mayUserId =
4650
SyncV2.Routes
@@ -59,7 +63,7 @@ parseBranchRef (SyncV2.BranchRef branchRef) =
5963
parseRelease = fmap Left . eitherToMaybe $ IDs.fromText @ProjectReleaseShortHand branchRef
6064

6165
downloadEntitiesStreamImpl :: Maybe UserId -> SyncV2.DownloadEntitiesRequest -> WebApp (SourceIO SyncV2.DownloadEntitiesChunk)
62-
downloadEntitiesStreamImpl mayCallerUserId (SyncV2.DownloadEntitiesRequest {causalHash = causalHashJWT, branchRef, knownHashes= _todo}) = do
66+
downloadEntitiesStreamImpl mayCallerUserId (SyncV2.DownloadEntitiesRequest {causalHash = causalHashJWT, branchRef, knownHashes = _todo}) = do
6367
either emitErr id <$> runExceptT do
6468
addRequestTag "branch-ref" (SyncV2.unBranchRef branchRef)
6569
HashJWTClaims {hash = causalHash} <- lift (HashJWT.verifyHashJWT mayCallerUserId causalHashJWT >>= either respondError pure)
@@ -83,24 +87,35 @@ downloadEntitiesStreamImpl mayCallerUserId (SyncV2.DownloadEntitiesRequest {caus
8387
authZToken <- lift AuthZ.checkDownloadFromProjectBranchCodebase `whenLeftM` \_err -> throwError (SyncV2.DownloadEntitiesNoReadPermission branchRef)
8488
let codebaseLoc = Codebase.codebaseLocationForProjectBranchCodebase projectOwnerUserId contributorId
8589
pure $ Codebase.codebaseEnv authZToken codebaseLoc
86-
q <- liftIO $ STM.newTBMQueueIO 10
90+
q <- UnliftIO.atomically $ do
91+
q <- STM.newTBMQueue 10
92+
STM.writeTBMQueue q (NEL.singleton $ InitialC $ streamSettings)
93+
pure q
8794
streamResults <- lift $ UnliftIO.toIO do
8895
Logging.logInfoText "Starting download entities stream"
8996
Codebase.runCodebaseTransaction codebase $ do
97+
Debug.debugM Debug.Temp "Getting IDs for:" causalHash
9098
(_bhId, causalId) <- CausalQ.expectCausalIdsOf id (hash32ToCausalHash causalHash)
99+
Debug.debugM Debug.Temp "Getting deps of" causalId
91100
cursor <- SSQ.allSerializedDependenciesOfCausalCursor causalId
92-
Cursor.foldBatched cursor 1000 \batch -> do
93-
PG.transactionUnsafeIO $ STM.atomically $ STM.writeTBMQueue q batch
101+
Debug.debugLogM Debug.Temp "Got cursor"
102+
Cursor.foldBatched cursor batchSize \batch -> do
103+
Debug.debugLogM Debug.Temp "Emitting batch"
104+
let entityChunkBatch = batch <&> \(entityCBOR, hash) -> EntityC (EntityChunk {hash, entityCBOR})
105+
PG.transactionUnsafeIO $ STM.atomically $ STM.writeTBMQueue q entityChunkBatch
94106
PG.transactionUnsafeIO $ STM.atomically $ STM.closeTBMQueue q
95-
pure $ sourceIOWithAsync streamResults $ conduitToSourceIO do
107+
liftIO $ Async.async streamResults
108+
-- pure $ sourceIOWithAsync streamResults $ conduitToSourceIO do
109+
pure $ conduitToSourceIO do
96110
stream q
97-
Conduit..| ( Conduit.iterM \case
98-
InitialC init -> Debug.debugM Debug.Temp "Initial " init
99-
EntityC ec -> Debug.debugM Debug.Temp "Chunk " ec
100-
ErrorC err -> Debug.debugM Debug.Temp "Error " err
101-
)
102111
where
103-
stream :: STM.TBMQueue (NonEmpty (SyncV2.CBORBytes TempEntity, Hash32)) -> ConduitT () DownloadEntitiesChunk IO ()
112+
-- Conduit..| ( Conduit.iterM \case
113+
-- InitialC init -> Debug.debugM Debug.Temp "Initial " init
114+
-- EntityC ec -> Debug.debugM Debug.Temp "Chunk " ec
115+
-- ErrorC err -> Debug.debugM Debug.Temp "Error " err
116+
-- )
117+
118+
stream :: STM.TBMQueue (NonEmpty DownloadEntitiesChunk) -> ConduitT () DownloadEntitiesChunk IO ()
104119
stream q = do
105120
let loop :: ConduitT () DownloadEntitiesChunk IO ()
106121
loop = do
@@ -111,18 +126,18 @@ downloadEntitiesStreamImpl mayCallerUserId (SyncV2.DownloadEntitiesRequest {caus
111126
Debug.debugLogM Debug.Temp "Queue closed. finishing up!"
112127
pure ()
113128
Just batch -> do
114-
let chunks = batch <&> \(entityCBOR, hash) -> EntityC (EntityChunk {hash, entityCBOR})
115-
Debug.debugLogM Debug.Temp $ "Emitting chunk of " <> show (length chunks) <> " entities"
116-
yieldMany chunks
129+
Debug.debugLogM Debug.Temp $ "Emitting chunk of " <> show (length batch) <> " entities"
130+
yieldMany batch
117131
loop
132+
118133
loop
119134
Debug.debugLogM Debug.Temp "Done!"
120135

121136
emitErr :: SyncV2.DownloadEntitiesError -> SourceIO SyncV2.DownloadEntitiesChunk
122137
emitErr err = SourceT.source [ErrorC (ErrorChunk err)]
123138

124139
-- | Run an IO action in the background while streaming the results.
125-
sourceIOWithAsync :: IO a -> SourceIO r -> SourceIO r
126-
sourceIOWithAsync action (SourceT k) =
140+
_sourceIOWithAsync :: IO a -> SourceIO r -> SourceIO r
141+
_sourceIOWithAsync action (SourceT k) =
127142
SourceT \k' ->
128143
Async.withAsync action \_ -> k k'

0 commit comments

Comments
 (0)