Skip to content

Commit 08d6ea4

Browse files
ecthiendercodingkarthik
authored andcommitted
server: changes catalog initialization and logging for pro customization (hasura#5139)
* new typeclass to abstract the logic of QueryLog-ing * abstract the logic of logging websocket-server logs introduce a MonadWSLog typeclass * move catalog initialization to init step expose a helper function to migrate catalog create schema cache in initialiseCtx * expose various modules and functions for pro
1 parent eee2402 commit 08d6ea4

File tree

9 files changed

+71
-107
lines changed

9 files changed

+71
-107
lines changed

server/commit_diff.txt

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,25 +207,6 @@ Date: Tue Jun 23 20:51:34 2020 +0530
207207

208208
Co-authored-by: Tirumarai Selvan <[email protected]>
209209

210-
commit a7a60c2dfe42f9257d57d748a231bd47b164337b
211-
Author: Anon Ray <[email protected]>
212-
Date: Fri Jun 19 12:12:32 2020 +0530
213-
214-
server: changes catalog initialization and logging for pro customization (#5139)
215-
216-
* new typeclass to abstract the logic of QueryLog-ing
217-
218-
* abstract the logic of logging websocket-server logs
219-
220-
introduce a MonadWSLog typeclass
221-
222-
* move catalog initialization to init step
223-
224-
expose a helper function to migrate catalog
225-
create schema cache in initialiseCtx
226-
227-
* expose various modules and functions for pro
228-
229210
(Done, but we should re-visit this, if we do query plan caching)
230211
commit 20cbe9cfd3e90b91d3f4faf370b081fc3859cbde
231212
Author: Auke Booij <[email protected]>

server/graphql-engine.cabal

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,13 @@ library
300300

301301
-- exposed for Pro
302302
, Hasura.GraphQL.Execute
303+
, Hasura.GraphQL.Execute.Query
303304
, Hasura.GraphQL.Execute.LiveQuery
304305
-- , Hasura.GraphQL.Validate
305306
, Hasura.GraphQL.Transport.HTTP
306307
, Hasura.GraphQL.Transport.WebSocket.Protocol
307308
, Hasura.GraphQL.Transport.WebSocket.Server
309+
, Hasura.GraphQL.Logging
308310

309311
, Hasura.RQL.Types
310312
, Hasura.RQL.Types.Run
@@ -320,6 +322,9 @@ library
320322
, Data.Aeson.Ordered
321323
, Data.TByteString
322324

325+
, Network.Wai.Extended
326+
, Control.Concurrent.Extended
327+
323328
other-modules: Hasura.Incremental.Select
324329
, Hasura.Incremental.Internal.Cache
325330
, Hasura.Incremental.Internal.Dependency
@@ -411,7 +416,6 @@ library
411416
, Hasura.GraphQL.Explain
412417
, Hasura.GraphQL.Execute.Inline
413418
, Hasura.GraphQL.Execute.Plan
414-
, Hasura.GraphQL.Execute.Query
415419
, Hasura.GraphQL.Execute.Types
416420
, Hasura.GraphQL.Execute.Mutation
417421
, Hasura.GraphQL.Execute.Resolve
@@ -433,7 +437,6 @@ library
433437
-- , Hasura.GraphQL.Resolve.Select
434438
, Hasura.GraphQL.RemoteServer
435439
, Hasura.GraphQL.Context
436-
, Hasura.GraphQL.Logging
437440

438441
, Hasura.GraphQL.Parser
439442
, Hasura.GraphQL.Parser.Class
@@ -460,8 +463,6 @@ library
460463
, Hasura.Eventing.ScheduledTrigger
461464
, Hasura.Eventing.Common
462465

463-
, Network.Wai.Extended
464-
, Control.Concurrent.Extended
465466
, Control.Lens.Extended
466467
, Data.Aeson.Extended
467468
, Data.GADT.Compare.Extended
@@ -482,7 +483,6 @@ library
482483
, Hasura.SQL.Value
483484

484485
, Network.URI.Extended
485-
, Network.Wai.Extended
486486
, Network.Wai.Handler.WebSockets.Custom
487487

488488
executable graphql-engine

server/src-lib/Hasura/GraphQL/Execute.hs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import Hasura.EncJSON
5656
import Hasura.GraphQL.Logging
5757
import Hasura.GraphQL.Transport.HTTP.Protocol
5858
import Hasura.HTTP
59+
import Hasura.GraphQL.RemoteServer (execRemoteGQ')
5960
import Hasura.RQL.DDL.Headers
6061
import Hasura.RQL.Types
6162
import Hasura.Server.Utils (RequestId, mkClientHeadersForward,
@@ -348,6 +349,7 @@ execRemoteGQ
348349
, MonadIO m
349350
, MonadError QErr m
350351
, MonadReader ExecutionCtx m
352+
, MonadQueryLog m
351353
)
352354
=> RequestId
353355
-> UserInfo
@@ -361,40 +363,8 @@ execRemoteGQ reqId userInfo reqHdrs q rsi opDef = do
361363
execCtx <- ask
362364
let logger = _ecxLogger execCtx
363365
manager = _ecxHttpManager execCtx
364-
opTy = G._todType opDef
365-
when (opTy == G.OperationTypeSubscription) $
366-
throw400 NotSupported "subscription to remote server is not supported"
367-
confHdrs <- makeHeadersFromConf hdrConf
368-
let clientHdrs = bool [] (mkClientHeadersForward reqHdrs) fwdClientHdrs
369-
-- filter out duplicate headers
370-
-- priority: conf headers > resolved userinfo vars > client headers
371-
hdrMaps = [ Map.fromList confHdrs
372-
, Map.fromList userInfoToHdrs
373-
, Map.fromList clientHdrs
374-
]
375-
headers = Map.toList $ foldr Map.union Map.empty hdrMaps
376-
finalHeaders = addDefaultHeaders headers
377-
initReqE <- liftIO $ try $ HTTP.parseRequest (show url)
378-
initReq <- either httpThrow pure initReqE
379-
let req = initReq
380-
{ HTTP.method = "POST"
381-
, HTTP.requestHeaders = finalHeaders
382-
, HTTP.requestBody = HTTP.RequestBodyLBS (J.encode q)
383-
, HTTP.responseTimeout = HTTP.responseTimeoutMicro (timeout * 1000000)
384-
}
385-
386-
L.unLogger logger $ QueryLog q Nothing reqId
387-
(time, res) <- withElapsedTime $ liftIO $ try $ HTTP.httpLbs req manager
388-
resp <- either httpThrow return res
389-
let !httpResp = HttpResponse (encJFromLBS $ resp ^. Wreq.responseBody) $ mkSetCookieHeaders resp
366+
opType = G._todType opDef
367+
logQueryLog logger q Nothing reqId
368+
(time, respHdrs, resp) <- execRemoteGQ' manager userInfo reqHdrs q rsi opType
369+
let !httpResp = HttpResponse (encJFromLBS resp) respHdrs
390370
return (time, httpResp)
391-
392-
where
393-
RemoteSchemaInfo url hdrConf fwdClientHdrs timeout = rsi
394-
httpThrow :: (MonadError QErr m) => HTTP.HttpException -> m a
395-
httpThrow = \case
396-
HTTP.HttpExceptionRequest _req content -> throw500 $ T.pack . show $ content
397-
HTTP.InvalidUrlException _url reason -> throw500 $ T.pack . show $ reason
398-
399-
userInfoToHdrs = userInfoToList userInfo
400-
& map (CI.mk . unUTF8 . fromText *** unUTF8 . fromText)

server/src-lib/Hasura/GraphQL/Transport/HTTP.hs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-- | Execution of GraphQL queries over HTTP transport
2+
{-# LANGUAGE RecordWildCards #-}
23
module Hasura.GraphQL.Transport.HTTP
34
( runGQ
45
, runGQBatched
@@ -12,7 +13,7 @@ module Hasura.GraphQL.Transport.HTTP
1213
) where
1314

1415
import Hasura.EncJSON
15-
import Hasura.GraphQL.Logging
16+
import Hasura.GraphQL.Logging (MonadQueryLog (..))
1617
import Hasura.GraphQL.Transport.HTTP.Protocol
1718
import Hasura.HTTP
1819
import Hasura.Prelude
@@ -40,6 +41,7 @@ runGQ
4041
, MonadError QErr m
4142
, MonadReader E.ExecutionCtx m
4243
, E.MonadGQLExecutionCheck m
44+
, MonadQueryLog m
4345
)
4446
=> RequestId
4547
-> UserInfo
@@ -114,6 +116,7 @@ runGQBatched
114116
, MonadError QErr m
115117
, MonadReader E.ExecutionCtx m
116118
, E.MonadGQLExecutionCheck m
119+
, MonadQueryLog m
117120
)
118121
=> RequestId
119122
-> ResponseInternalErrorsConfig
@@ -147,6 +150,7 @@ runQueryDB
147150
:: ( MonadIO m
148151
, MonadError QErr m
149152
, MonadReader E.ExecutionCtx m
153+
, MonadQueryLog m
150154
)
151155
=> RequestId
152156
-> GQLReqUnparsed
@@ -156,10 +160,10 @@ runQueryDB
156160
-- ^ Also return 'Mutation' when the operation was a mutation, and the time
157161
-- spent in the PG query; for telemetry.
158162
runQueryDB reqId query _userInfo (tx, genSql) = do
163+
-- log the generated SQL and the graphql query
159164
E.ExecutionCtx logger _ pgExecCtx _ _ _ _ _ <- ask
165+
logQueryLog logger query (Just genSql) reqId
160166
(telemTimeIO, respE) <- withElapsedTime $ liftIO $ runExceptT $ do
161-
-- log the generated SQL and the graphql query
162-
L.unLogger logger $ QueryLog query (Just genSql) reqId
163167
runQueryTx pgExecCtx tx
164168
resp <- liftEither respE
165169
let !json = encodeGQResp $ GQSuccess $ encJToLBS resp
@@ -170,6 +174,7 @@ runMutationDB
170174
:: ( MonadIO m
171175
, MonadError QErr m
172176
, MonadReader E.ExecutionCtx m
177+
, MonadQueryLog m
173178
)
174179
=> RequestId
175180
-> GQLReqUnparsed
@@ -180,9 +185,9 @@ runMutationDB
180185
-- spent in the PG query; for telemetry.
181186
runMutationDB reqId query userInfo tx = do
182187
E.ExecutionCtx logger _ pgExecCtx _ _ _ _ _ <- ask
188+
-- log the graphql query
189+
logQueryLog logger query Nothing reqId
183190
(telemTimeIO, respE) <- withElapsedTime $ liftIO $ runExceptT $ do
184-
-- log the graphql query
185-
L.unLogger logger $ QueryLog query Nothing reqId
186191
runLazyTx pgExecCtx Q.ReadWrite $ withUserInfo userInfo tx
187192
resp <- liftEither respE
188193
let !json = encodeGQResp $ GQSuccess $ encJToLBS resp
@@ -194,6 +199,7 @@ runHasuraGQ
194199
:: ( MonadIO m
195200
, MonadError QErr m
196201
, MonadReader E.ExecutionCtx m
202+
, MonadQueryLog m
197203
)
198204
=> RequestId
199205
-> GQLReqUnparsed
@@ -203,19 +209,19 @@ runHasuraGQ
203209
-- ^ Also return 'Mutation' when the operation was a mutation, and the time
204210
-- spent in the PG query; for telemetry.
205211
runHasuraGQ reqId query userInfo resolvedOp = do
206-
E.ExecutionCtx logger _ pgExecCtx _ _ _ _ _ <- ask
212+
(E.ExecutionCtx logger _ pgExecCtx _ _ _ _ _) <- ask
213+
logQuery' logger
207214
(telemTimeIO, respE) <- withElapsedTime $ liftIO $ runExceptT $ case resolvedOp of
208-
E.ExOpQuery tx genSql -> do
215+
E.ExOpQuery tx _genSql -> do
209216
-- log the generated SQL and the graphql query
210-
L.unLogger logger $ QueryLog query genSql reqId
217+
-- L.unLogger logger $ QueryLog query genSql reqId
211218
([],) <$> runQueryTx pgExecCtx tx
212219
E.ExOpMutation respHeaders tx -> do
213-
-- log the graphql query
214-
L.unLogger logger $ QueryLog query Nothing reqId
215220
(respHeaders,) <$> runLazyTx pgExecCtx Q.ReadWrite (withUserInfo userInfo tx)
216221
E.ExOpSubs _ ->
217222
throw400 UnexpectedPayload
218223
"subscriptions are not supported over HTTP, use websockets instead"
224+
219225
(respHdrs, resp) <- liftEither respE
220226
let !json = encodeGQResp $ GQSuccess $ encJToLBS resp
221227
telemQueryType = case resolvedOp of E.ExOpMutation{} -> Telem.Mutation ; _ -> Telem.Query

server/src-lib/Hasura/GraphQL/Transport/WebSocket.hs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import qualified Data.Text.Encoding as TE
2727
import qualified Data.Time.Clock as TC
2828
import qualified Database.PG.Query as Q
2929
import qualified Language.GraphQL.Draft.Syntax as G
30+
import qualified ListT
3031
import qualified Network.HTTP.Client as H
3132
import qualified Network.HTTP.Types as H
3233
import qualified Network.Wai.Extended as Wai
@@ -36,11 +37,10 @@ import qualified StmContainers.Map as STMMap
3637
import Control.Concurrent.Extended (sleep)
3738
import Control.Exception.Lifted
3839
import Data.String
39-
import GHC.AssertNF
40-
import qualified ListT
40+
-- import GHC.AssertNF
4141

4242
import Hasura.EncJSON
43-
import Hasura.GraphQL.Logging
43+
import Hasura.GraphQL.Logging (MonadQueryLog (..))
4444
import Hasura.GraphQL.Transport.HTTP.Protocol
4545
import Hasura.GraphQL.Transport.WebSocket.Protocol
4646
import Hasura.HTTP
@@ -121,8 +121,13 @@ sendMsgWithMetadata wsConn msg (LQ.LiveQueryMetadata execTime) =
121121
liftIO $ WS.sendMsg wsConn $ WS.WSQueueResponse bs wsInfo
122122
where
123123
bs = encodeServerMsg msg
124+
(msgType, operationId) = case msg of
125+
(SMData (DataMsg opId _)) -> (Just SMT_GQL_DATA, Just opId)
126+
_ -> (Nothing, Nothing)
124127
wsInfo = Just $! WS.WSEventInfo
125-
{ WS._wseiQueryExecutionTime = Just $! realToFrac execTime
128+
{ WS._wseiEventType = msgType
129+
, WS._wseiOperationId = operationId
130+
, WS._wseiQueryExecutionTime = Just $! realToFrac execTime
126131
, WS._wseiResponseSize = Just $! BL.length bs
127132
}
128133

@@ -326,8 +331,7 @@ onStart serverEnv wsConn (StartMsg opId q) = catchAndIgnore $ do
326331
planCache userInfo sqlGenCtx sc scVer queryType httpMgr reqHdrs (q, reqParsed)
327332

328333
(telemCacheHit, execPlan) <- either (withComplete . preExecErr requestId) return execPlanE
329-
let execCtx = E.ExecutionCtx logger sqlGenCtx pgExecCtx
330-
planCache sc scVer httpMgr enableAL
334+
let execCtx = E.ExecutionCtx logger sqlGenCtx pgExecCtx planCache sc scVer httpMgr enableAL
331335

332336
case execPlan of
333337
E.QueryExecutionPlan queryPlan -> do
@@ -354,15 +358,15 @@ onStart serverEnv wsConn (StartMsg opId q) = catchAndIgnore $ do
354358
case subscriptionPlan of
355359
E.ExecStepDB lqOp -> do
356360
-- log the graphql query
357-
L.unLogger logger $ QueryLog q Nothing requestId
361+
logQueryLog logger q Nothing requestId
358362
let subscriberMetadata = LQ.mkSubscriberMetadata $ J.object
359363
[ "websocket_id" J..= WS.getWSId wsConn
360364
, "operation_id" J..= opId
361365
]
362366
-- NOTE!: we mask async exceptions higher in the call stack, but it's
363367
-- crucial we don't lose lqId after addLiveQuery returns successfully.
364368
!lqId <- liftIO $ LQ.addLiveQuery logger subscriberMetadata lqMap lqOp liveQOnChange
365-
let !opName = _grOperationName q
369+
-- let !opName = _grOperationName q -- TODO: uncomment this
366370
-- liftIO $ $assertNFHere $! (lqId, opName) -- so we don't write thunks to mutable vars
367371
liftIO $ STM.atomically $
368372
-- NOTE: see crucial `lookup` check above, ensuring this doesn't clobber:
@@ -381,7 +385,7 @@ onStart serverEnv wsConn (StartMsg opId q) = catchAndIgnore $ do
381385
execQueryOrMut timerTot telemQueryType telemCacheHit telemLocality genSql requestId q' action = do
382386
logOpEv ODStarted (Just requestId)
383387
-- log the generated SQL and the graphql query
384-
L.unLogger logger $ QueryLog q' genSql requestId
388+
logQueryLog logger q' genSql requestId
385389
(withElapsedTime $ liftIO $ runExceptT action) >>= \case
386390
(_, Left err) -> postExecErr requestId err
387391
(telemTimeIO_DT, Right encJson) -> do
@@ -405,7 +409,8 @@ onStart serverEnv wsConn (StartMsg opId q) = catchAndIgnore $ do
405409
runLazyTx pgExecCtx Q.ReadWrite $ withUserInfo userInfo opTx
406410
E.ExOpSubs lqOp -> do
407411
-- log the graphql query
408-
L.unLogger logger $ QueryLog query Nothing reqId
412+
-- L.unLogger logger $ QueryLog query Nothing reqId
413+
logQueryLog logger query Nothing reqId
409414
let subscriberMetadata = LQ.mkSubscriberMetadata $ J.object
410415
[ "websocket_id" J..= WS.getWSId wsConn
411416
, "operation_id" J..= opId
@@ -426,8 +431,8 @@ onStart serverEnv wsConn (StartMsg opId q) = catchAndIgnore $ do
426431
execQueryOrMut telemQueryType genSql action = do
427432
logOpEv ODStarted (Just reqId)
428433
-- log the generated SQL and the graphql query
429-
L.unLogger logger $ QueryLog query genSql reqId
430-
(withElapsedTime $ liftIO $ runExceptT action) >>= \case
434+
logQueryLog logger query genSql reqId
435+
(withElapsedTime $ runExceptT action) >>= \case
431436
(_, Left err) -> postExecErr reqId err
432437
(telemTimeIO_DT, Right encJson) -> do
433438
-- Telemetry. NOTE: don't time network IO:

server/src-lib/Hasura/GraphQL/Transport/WebSocket/Protocol.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Hasura.GraphQL.Transport.WebSocket.Protocol
66
, StopMsg(..)
77
, ClientMsg(..)
88
, ServerMsg(..)
9+
, ServerMsgType(..)
910
, encodeServerMsg
1011
, DataMsg(..)
1112
, ErrorMsg(..)

0 commit comments

Comments
 (0)