graphql-engine/server/src-lib/Hasura/GraphQL/Execute/Plan.hs
Vamshi Surabhi ce243f5899
multiplexed subscriptions (#1934)
* add types to represent unparsed http gql requests

This will help when we add caching of frequently used ASTs

* query plan caching

* move livequery to execute

* add multiplexed module

* session variable can be customised depending on the context

Previously the value was always "current_setting('hasura.user')"

* get rid of typemap requirement in reusable plan

* subscriptions are multiplexed when possible

* use lazytx for introspection to avoid acquiring a pg connection

* refactor to make execute a completely decoupled module

* don't issue a transaction for a query

* don't use current setting for explained sql

* move postgres related types to a different module

* validate variableValues on postgres before multiplexing subs

* don't user current_setting for queries over ws

* plan_cache is only visible when developer flag is enabled

* introduce 'batch size' when multiplexing subscriptions

* bump stackage to 13.16

* fix schema_stitching test case error code

* store hashes instead of actual responses for subscriptions

* internal api to dump subscriptions state

* remove PlanCache from SchemaCacheRef

* allow live query options to be configured on server startup

* capture metrics for multiplexed subscriptions

* more metrics captured for multiplexed subs

* switch to tvar based hashmap for faster snapshotting

* livequery modules do not expose internal details

* fix typo in live query env vars

* switch to hasura's pg-client-hs
2019-04-17 15:18:41 +05:30

86 lines
2.2 KiB
Haskell

module Hasura.GraphQL.Execute.Plan
( ReusablePlan(..)
, PlanCache
, getPlan
, addPlan
, initPlanCache
, clearPlanCache
, dumpPlanCache
) where
import qualified Hasura.Cache as Cache
import Hasura.Prelude
import qualified Data.Aeson as J
import qualified Hasura.GraphQL.Execute.LiveQuery as LQ
import qualified Hasura.GraphQL.Execute.Query as EQ
import qualified Hasura.GraphQL.Transport.HTTP.Protocol as GH
import Hasura.RQL.Types
data PlanId
= PlanId
{ _piSchemaCacheVersion :: !SchemaCacheVer
, _piRole :: !RoleName
, _piOperationName :: !(Maybe GH.OperationName)
, _piQuery :: !GH.GQLQueryText
} deriving (Show, Eq, Generic)
instance Hashable PlanId
instance J.ToJSON PlanId where
toJSON (PlanId scVer rn opNameM query) =
J.object
[ "schema_cache_version" J..= scVer
, "role" J..= rn
, "operation" J..= opNameM
, "query" J..= query
]
newtype PlanCache
= PlanCache
{ _unPlanCache :: Cache.UnboundedCache PlanId ReusablePlan
}
data ReusablePlan
= RPQuery !EQ.ReusableQueryPlan
| RPSubs !LQ.SubsPlan
instance J.ToJSON ReusablePlan where
toJSON = \case
RPQuery queryPlan -> J.toJSON queryPlan
RPSubs subsPlan -> J.toJSON subsPlan
initPlanCache :: IO PlanCache
initPlanCache = PlanCache <$> Cache.initCache
getPlan
:: SchemaCacheVer -> RoleName -> Maybe GH.OperationName -> GH.GQLQueryText
-> PlanCache -> IO (Maybe ReusablePlan)
getPlan schemaVer rn opNameM q (PlanCache planCache) =
Cache.lookup planCache planId
where
planId = PlanId schemaVer rn opNameM q
addPlan
:: SchemaCacheVer -> RoleName -> Maybe GH.OperationName -> GH.GQLQueryText
-> ReusablePlan -> PlanCache -> IO ()
addPlan schemaVer rn opNameM q queryPlan (PlanCache planCache) =
Cache.insert planCache planId queryPlan
where
planId = PlanId schemaVer rn opNameM q
clearPlanCache :: PlanCache -> IO ()
clearPlanCache (PlanCache planCache) =
Cache.clearCache planCache
dumpPlanCache :: PlanCache -> IO J.Value
dumpPlanCache (PlanCache cache) =
J.toJSON <$> Cache.mapCache dumpEntry cache
where
dumpEntry (planId, plan) =
J.object
[ "id" J..= planId
, "plan" J..= plan
]