Skip to content

Commit a218f49

Browse files
committed
Debugging
1 parent f0abad8 commit a218f49

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

src/Share/Web/Impl.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ server =
7373
:<|> Sync.server -- Deprecated path
7474
:<|> Sync.server
7575
:<|> UCMProjects.server
76-
:<|> Admin.server
7776
:<|> SyncStream.server
77+
:<|> Admin.server

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ module Share.Web.UCM.SyncStream.Impl (server) where
55

66
import Conduit
77
import Control.Concurrent.STM qualified as STM
8+
import Control.Concurrent.STM.TBMQueue qualified as STM
89
import Control.Monad.Except (ExceptT (ExceptT))
910
import Control.Monad.Trans.Except (runExceptT)
10-
import Data.ByteString.Lazy qualified as BL
11+
import Data.Conduit.Combinators qualified as Conduit
1112
import Servant
1213
import Servant.Conduit (ConduitToSourceIO (..))
1314
import Servant.Types.SourceT qualified as SourceT
@@ -28,6 +29,8 @@ import Share.Web.Errors
2829
import Share.Web.UCM.Sync.HashJWT qualified as HashJWT
2930
import Share.Web.UCM.SyncStream.Queries qualified as SSQ
3031
import U.Codebase.Sqlite.Orphans ()
32+
import U.Codebase.Sqlite.TempEntity (TempEntity)
33+
import Unison.Debug qualified as Debug
3134
import Unison.Hash32 (Hash32)
3235
import Unison.Share.API.Hash (HashJWTClaims (..))
3336
import Unison.SyncV2.API qualified as SyncV2
@@ -78,34 +81,44 @@ downloadEntitiesStreamImpl mayCallerUserId (SyncV2.DownloadEntitiesRequest {caus
7881
authZToken <- lift AuthZ.checkDownloadFromProjectBranchCodebase `whenLeftM` \_err -> throwError (SyncV2.DownloadEntitiesNoReadPermission branchRef)
7982
let codebaseLoc = Codebase.codebaseLocationForProjectBranchCodebase projectOwnerUserId contributorId
8083
pure $ Codebase.codebaseEnv authZToken codebaseLoc
81-
q <- liftIO $ STM.newTBQueueIO 10
84+
q <- liftIO $ STM.newTBMQueueIO 10
8285
streamResults <- lift $ UnliftIO.toIO do
86+
Debug.debugLogM Debug.Temp "Starting source Stream"
8387
Codebase.runCodebaseTransaction codebase $ do
8488
(_bhId, causalId) <- CausalQ.expectCausalIdsOf id (hash32ToCausalHash causalHash)
8589
cursor <- SSQ.allSerializedDependenciesOfCausalCursor causalId
8690
Cursor.foldBatched cursor 1000 \batch -> do
87-
PG.transactionUnsafeIO $ STM.atomically $ STM.writeTBQueue q batch
91+
Debug.debugLogM Debug.Temp "Source stream batch"
92+
PG.transactionUnsafeIO $ STM.atomically $ STM.writeTBMQueue q batch
93+
PG.transactionUnsafeIO $ STM.atomically $ STM.closeTBMQueue q
8894
pure $ conduitToSourceIO do
8995
handle <- liftIO $ Async.async streamResults
9096
stream q handle
97+
Conduit..| ( Conduit.iterM \case
98+
EntityChunk {hash} -> Debug.debugM Debug.Temp "Chunk " hash
99+
ErrorChunk err -> Debug.debugM Debug.Temp "Error " err
100+
)
91101
where
92-
stream :: STM.TBQueue (NonEmpty (Hash32, ByteString)) -> Async.Async () -> ConduitT () DownloadEntitiesChunk IO ()
93-
stream q async = do
102+
stream :: STM.TBMQueue (NonEmpty (SyncV2.CBORBytes TempEntity, Hash32)) -> (Async.Async a) -> ConduitT () DownloadEntitiesChunk IO ()
103+
stream q handle = do
94104
let loop :: ConduitT () DownloadEntitiesChunk IO ()
95105
loop = do
96-
next <- liftIO . STM.atomically $ do
97-
STM.tryReadTBQueue q >>= \case
98-
Nothing -> do
99-
Async.waitSTM async $> Nothing
100-
Just batch -> do
101-
pure $ Just batch
102-
case next of
103-
Nothing -> pure ()
106+
Debug.debugLogM Debug.Temp "Waiting for batch..."
107+
liftIO (STM.atomically (STM.readTBMQueue q)) >>= \case
108+
-- The queue is closed.
109+
Nothing -> do
110+
Debug.debugLogM Debug.Temp "Queue closed. finishing up!"
111+
pure ()
104112
Just batch -> do
105-
let chunks = batch <&> \(hash, bytes) -> EntityChunk {hash, entityCBOR = SyncV2.CBORBytes $ BL.fromStrict bytes}
113+
let chunks = batch <&> \(entityCBOR, hash) -> EntityChunk {hash, entityCBOR}
114+
Debug.debugLogM Debug.Temp $ "Emitting chunk of " <> show (length chunks) <> " entities"
106115
yieldMany chunks
107116
loop
108117
loop
118+
Debug.debugLogM Debug.Temp "Waiting for worker thread to finish"
119+
-- It _should_ have terminated by now, but just in case, cancel it.
120+
Async.cancel handle
121+
Debug.debugLogM Debug.Temp "Done!"
109122

110123
emitErr :: SyncV2.DownloadEntitiesError -> SourceIO SyncV2.DownloadEntitiesChunk
111124
emitErr err = SourceT.source [ErrorChunk err]

src/Share/Web/UCM/SyncStream/Queries.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import Share.Postgres.Cursors (PGCursor)
1111
import Share.Postgres.Cursors qualified as PGCursor
1212
import Share.Postgres.IDs
1313
import Share.Prelude
14+
import U.Codebase.Sqlite.TempEntity (TempEntity)
1415
import Unison.Hash32 (Hash32)
16+
import Unison.SyncV2.Types (CBORBytes)
1517

1618
allHashDependenciesOfCausalCursor :: CausalId -> CodebaseM e (PGCursor Text)
1719
allHashDependenciesOfCausalCursor cid = do
@@ -180,7 +182,7 @@ allHashDependenciesOfCausalCursor cid = do
180182
JOIN component_hashes ON tc.component_hash_id = component_hashes.id
181183
|]
182184

183-
allSerializedDependenciesOfCausalCursor :: CausalId -> CodebaseM e (PGCursor (Hash32, ByteString))
185+
allSerializedDependenciesOfCausalCursor :: CausalId -> CodebaseM e (PGCursor (CBORBytes TempEntity, Hash32))
184186
allSerializedDependenciesOfCausalCursor cid = do
185187
ownerUserId <- asks codebaseOwner
186188
PGCursor.newRowCursor
@@ -217,7 +219,7 @@ allSerializedDependenciesOfCausalCursor cid = do
217219
JOIN branch_hashes bh ON tc.causal_namespace_hash_id = bh.id
218220
-- WHERE NOT EXISTS (SELECT FROM namespace_ownership no WHERE no.user_id = to_codebase_user_id AND no.namespace_hash_id = tc.causal_namespace_hash_id)
219221
), all_patches(patch_id, patch_hash) AS (
220-
SELECT DISTINCT patch.id
222+
SELECT DISTINCT patch.id, patch.hash
221223
FROM all_namespaces an
222224
JOIN namespace_patches np ON an.namespace_hash_id = np.namespace_hash_id
223225
JOIN patches patch ON np.patch_id = patch.id

0 commit comments

Comments
 (0)