2019-03-12 08:46:27 +03:00
|
|
|
module Hasura.Server.SchemaUpdate
|
2020-11-24 09:10:04 +03:00
|
|
|
( startSchemaSyncListenerThread,
|
|
|
|
startSchemaSyncProcessorThread,
|
2022-11-06 01:37:04 +03:00
|
|
|
SchemaSyncThreadType (..),
|
2020-11-24 09:10:04 +03:00
|
|
|
)
|
2019-03-12 08:46:27 +03:00
|
|
|
where
|
|
|
|
|
2021-05-11 18:18:31 +03:00
|
|
|
import Control.Concurrent.Extended qualified as C
|
|
|
|
import Control.Concurrent.STM qualified as STM
|
|
|
|
import Control.Immortal qualified as Immortal
|
2021-06-08 15:55:18 +03:00
|
|
|
import Control.Monad.Loops qualified as L
|
2021-05-11 18:18:31 +03:00
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
|
|
import Control.Monad.Trans.Managed (ManagedT)
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Aeson.Casing
|
2023-04-26 18:42:13 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2021-05-11 18:18:31 +03:00
|
|
|
import Data.HashSet qualified as HS
|
2023-03-24 12:40:10 +03:00
|
|
|
import Data.Text qualified as T
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
import Database.PG.Query qualified as PG
|
2023-03-17 13:29:07 +03:00
|
|
|
import Hasura.App.State
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2019-03-12 08:46:27 +03:00
|
|
|
import Hasura.Logging
|
2020-12-14 07:30:19 +03:00
|
|
|
import Hasura.Metadata.Class
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Prelude
|
2021-04-06 06:25:02 +03:00
|
|
|
import Hasura.RQL.DDL.Schema (runCacheRWT)
|
Remove `ServerConfigCtx`.
### Description
This PR removes `ServerConfigCtx` and `HasServerConfigCtx`. Instead, it favours different approaches:
- when the code was only using one field, it passes that field explicitly (usually `SQLGenCtx` or `CheckFeatureFlag`)
- when the code was using several fields, but in only one function, it inlines
- for the cache build, it introduces `CacheStaticConfig` and `CacheDynamicConfig`, which are subsets of `AppEnv` and `AppContext` respectively
The main goal of this is to help with the modularization of the engine: as `ServerConfigCtx` had fields whose types were imported from several unrelated parts of the engine, using it tied together parts of the engine that should not be aware of one another (such as tying together `Hasura.LogicalModel` and `Hasura.GraphQL.Schema`).
The bulk of this PR is a change to the cache build, as a follow up to #8509: instead of giving the entire `ServerConfigCtx` as a incremental rule argument, we only give the new `CacheDynamicConfig` struct, which has fewer fields. The other required fields, that were coming from the `AppEnv`, are now given via the `HasCacheStaticConfig` constraint, which is a "subset" of `HasAppEnv`.
(Some further work could include moving `StringifyNumbers` out of `GraphQL.Schema.Options`, given how it is used all across the codebase, including in `RQL.DML`.)
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8513
GitOrigin-RevId: 818cbcd71494e3cd946b06adbb02ca328a8a298e
2023-04-04 18:59:58 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.Cache.Config
|
2021-04-06 06:25:02 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.Catalog
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType (BackendType (..))
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.RQL.Types.SchemaCache.Build
|
|
|
|
import Hasura.RQL.Types.Source
|
2022-09-14 15:59:37 +03:00
|
|
|
import Hasura.SQL.BackendMap qualified as BackendMap
|
2023-03-17 13:29:07 +03:00
|
|
|
import Hasura.Server.AppStateRef
|
|
|
|
( AppStateRef,
|
|
|
|
getAppContext,
|
2023-03-30 19:31:50 +03:00
|
|
|
getRebuildableSchemaCacheWithVersion,
|
2022-03-09 01:59:28 +03:00
|
|
|
withSchemaCacheUpdate,
|
|
|
|
)
|
2023-03-17 13:29:07 +03:00
|
|
|
import Hasura.Server.Logging
|
2022-04-22 17:50:01 +03:00
|
|
|
import Hasura.Server.Types
|
harmonize network manager handling
## Description
### I want to speak to the `Manager`
Oh boy. This PR is both fairly straightforward and overreaching, so let's break it down.
For most network access, we need a [`HTTP.Manager`](https://hackage.haskell.org/package/http-client-0.1.0.0/docs/Network-HTTP-Client-Manager.html). It is created only once, at the top level, when starting the engine, and is then threaded through the application to wherever we need to make a network call. As of main, the way we do this is not standardized: most of the GraphQL execution code passes it "manually" as a function argument throughout the code. We also have a custom monad constraint, `HasHttpManagerM`, that describes a monad's ability to provide a manager. And, finally, several parts of the code store the manager in some kind of argument structure, such as `RunT`'s `RunCtx`.
This PR's first goal is to harmonize all of this: we always create the manager at the root, and we already have it when we do our very first `runReaderT`. Wouldn't it make sense for the rest of the code to not manually pass it anywhere, to not store it anywhere, but to always rely on the current monad providing it? This is, in short, what this PR does: it implements a constraint on the base monads, so that they provide the manager, and removes most explicit passing from the code.
### First come, first served
One way this PR goes a tiny bit further than "just" doing the aforementioned harmonization is that it starts the process of implementing the "Services oriented architecture" roughly outlined in this [draft document](https://docs.google.com/document/d/1FAigqrST0juU1WcT4HIxJxe1iEBwTuBZodTaeUvsKqQ/edit?usp=sharing). Instead of using the existing `HasHTTPManagerM`, this PR revamps it into the `ProvidesNetwork` service.
The idea is, again, that we should make all "external" dependencies of the engine, all things that the core of the engine doesn't care about, a "service". This allows us to define clear APIs for features, to choose different implementations based on which version of the engine we're running, harmonizes our many scattered monadic constraints... Which is why this service is called "Network": we can refine it, moving forward, to be the constraint that defines how all network communication is to operate, instead of relying on disparate classes constraint or hardcoded decisions. A comment in the code clarifies this intent.
### Side-effects? In my Haskell?
This PR also unavoidably touches some other aspects of the codebase. One such example: it introduces `Hasura.App.AppContext`, named after `HasuraPro.Context.AppContext`: a name for the reader structure at the base level. It also transforms `Handler` from a type alias to a newtype, as `Handler` is where we actually enforce HTTP limits; but without `Handler` being a distinct type, any code path could simply do a `runExceptT $ runReader` and forget to enforce them.
(As a rule of thumb, i am starting to consider any straggling `runReaderT` or `runExceptT` as a code smell: we should not stack / unstack monads haphazardly, and every layer should be an opaque `newtype` with a corresponding run function.)
## Further work
In several places, i have left TODOs when i have encountered things that suggest that we should do further unrelated cleanups. I'll write down the follow-up steps, either in the aforementioned document or on slack. But, in short, at a glance, in approximate order, we could:
- delete `ExecutionCtx` as it is only a subset of `ServerCtx`, and remove one more `runReaderT` call
- delete `ServerConfigCtx` as it is only a subset of `ServerCtx`, and remove it from `RunCtx`
- remove `ServerCtx` from `HandlerCtx`, and make it part of `AppContext`, or even make it the `AppContext` altogether (since, at least for the OSS version, `AppContext` is there again only a subset)
- remove `CacheBuildParams` and `CacheBuild` altogether, as they're just a distinct stack that is a `ReaderT` on top of `IO` that contains, you guessed it, the same thing as `ServerCtx`
- move `RunT` out of `RQL.Types` and rename it, since after the previous cleanups **it only contains `UserInfo`**; it could be bundled with the authentication service, made a small implementation detail in `Hasura.Server.Auth`
- rename `PGMetadaStorageT` to something a bit more accurate, such as `App`, and enforce its IO base
This would significantly simply our complex stack. From there, or in parallel, we can start moving existing dependencies as Services. For the purpose of supporting read replicas entitlement, we could move `MonadResolveSource` to a `SourceResolver` service, as attempted in #7653, and transform `UserAuthenticationM` into a `Authentication` service.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7736
GitOrigin-RevId: 68cce710eb9e7d752bda1ba0c49541d24df8209f
2023-02-22 18:53:52 +03:00
|
|
|
import Hasura.Services
|
2022-09-21 21:01:48 +03:00
|
|
|
import Refined (NonNegative, Refined, unrefine)
|
2019-03-12 08:46:27 +03:00
|
|
|
|
|
|
|
data ThreadError
|
2020-12-14 07:30:19 +03:00
|
|
|
= TEPayloadParse !Text
|
2019-03-12 08:46:27 +03:00
|
|
|
| TEQueryError !QErr
|
2023-06-28 10:54:05 +03:00
|
|
|
deriving (Generic)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2023-06-28 10:54:05 +03:00
|
|
|
instance ToJSON ThreadError where
|
|
|
|
toJSON =
|
|
|
|
genericToJSON
|
|
|
|
defaultOptions
|
|
|
|
{ constructorTagModifier = snakeCase . drop 2,
|
|
|
|
sumEncoding = TaggedObject "type" "info"
|
|
|
|
}
|
|
|
|
toEncoding =
|
|
|
|
genericToEncoding
|
|
|
|
defaultOptions
|
|
|
|
{ constructorTagModifier = snakeCase . drop 2,
|
|
|
|
sumEncoding = TaggedObject "type" "info"
|
|
|
|
}
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2020-11-24 09:10:04 +03:00
|
|
|
logThreadStarted ::
|
2019-11-26 15:14:21 +03:00
|
|
|
(MonadIO m) =>
|
2020-11-24 09:10:04 +03:00
|
|
|
Logger Hasura ->
|
|
|
|
InstanceId ->
|
2022-11-06 01:37:04 +03:00
|
|
|
SchemaSyncThreadType ->
|
2020-11-24 09:10:04 +03:00
|
|
|
Immortal.Thread ->
|
|
|
|
m ()
|
|
|
|
logThreadStarted logger instanceId threadType thread =
|
2020-12-08 17:22:31 +03:00
|
|
|
let msg = tshow threadType <> " thread started"
|
2023-05-24 16:51:56 +03:00
|
|
|
in unLogger logger
|
|
|
|
$ StartupLog LevelInfo "schema-sync"
|
|
|
|
$ object
|
|
|
|
[ "instance_id" .= getInstanceId instanceId,
|
|
|
|
"thread_id" .= show (Immortal.threadId thread),
|
|
|
|
"message" .= msg
|
|
|
|
]
|
2020-11-24 09:10:04 +03:00
|
|
|
|
|
|
|
{- Note [Schema Cache Sync]
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
When multiple graphql-engine instances are serving on same metadata storage,
|
|
|
|
each instance should have schema cache in sync with latest metadata. Somehow
|
|
|
|
all instances should communicate each other when any request has modified metadata.
|
|
|
|
|
2021-04-06 06:25:02 +03:00
|
|
|
We track the metadata schema version in postgres and poll for this
|
|
|
|
value in a thread. When the schema version has changed, the instance
|
|
|
|
will update its local metadata schema and remove any invalidated schema cache data.
|
2020-11-24 09:10:04 +03:00
|
|
|
|
2021-04-06 06:25:02 +03:00
|
|
|
The following steps take place when an API request made to update metadata:
|
2020-11-24 09:10:04 +03:00
|
|
|
|
2021-04-06 06:25:02 +03:00
|
|
|
1. After handling the request we insert the new metadata schema json
|
|
|
|
into a postgres tablealong with a schema version.
|
|
|
|
|
|
|
|
2. On start up, before initialising schema cache, an async thread is
|
|
|
|
invoked to continuously poll the Postgres notifications table for
|
|
|
|
the latest metadata schema version. The schema version is pushed to
|
|
|
|
a shared `TMVar`.
|
|
|
|
|
|
|
|
3. Before starting API server, another async thread is invoked to
|
|
|
|
process events pushed by the listener thread via the `TMVar`. If
|
|
|
|
the instance's schema version is not current with the freshly
|
|
|
|
updated TMVar version then we update the local metadata.
|
2020-11-24 09:10:04 +03:00
|
|
|
|
|
|
|
Why we need two threads if we can capture and reload schema cache in a single thread?
|
|
|
|
|
|
|
|
If we want to implement schema sync in a single async thread we have to invoke the same
|
|
|
|
after initialising schema cache. We may loose events that published after schema cache
|
|
|
|
init and before invoking the thread. In such case, schema cache is not in sync with metadata.
|
|
|
|
So we choose two threads in which one will start listening before schema cache init and the
|
|
|
|
other after it.
|
|
|
|
|
|
|
|
What happens if listen connection to Postgres is lost?
|
|
|
|
|
|
|
|
Listener thread will keep trying to establish connection to Postgres for every one second.
|
|
|
|
Once connection established, it pushes @'SSEListenStart' event with time. We aren't sure
|
|
|
|
about any metadata modify requests made in meanwhile. So we reload schema cache unconditionally
|
|
|
|
if listen started after schema cache init start time.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
-- | An async thread which listen to Postgres notify to enable schema syncing
|
|
|
|
-- See Note [Schema Cache Sync]
|
|
|
|
startSchemaSyncListenerThread ::
|
2023-05-24 16:51:56 +03:00
|
|
|
(C.ForkableMonadIO m) =>
|
2021-04-06 06:25:02 +03:00
|
|
|
Logger Hasura ->
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
PG.PGPool ->
|
2019-03-12 08:46:27 +03:00
|
|
|
InstanceId ->
|
2022-09-21 21:01:48 +03:00
|
|
|
Refined NonNegative Milliseconds ->
|
2021-04-06 06:25:02 +03:00
|
|
|
STM.TMVar MetadataResourceVersion ->
|
|
|
|
ManagedT m (Immortal.Thread)
|
2021-04-07 12:59:48 +03:00
|
|
|
startSchemaSyncListenerThread logger pool instanceId interval metaVersionRef = do
|
2019-03-12 08:46:27 +03:00
|
|
|
-- Start listener thread
|
2021-05-14 12:38:37 +03:00
|
|
|
listenerThread <-
|
2023-05-24 16:51:56 +03:00
|
|
|
C.forkManagedT "SchemeUpdate.listener" logger
|
|
|
|
$ listener logger pool metaVersionRef (unrefine interval)
|
2020-11-24 09:10:04 +03:00
|
|
|
logThreadStarted logger instanceId TTListener listenerThread
|
2021-04-06 06:25:02 +03:00
|
|
|
pure listenerThread
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2020-11-24 09:10:04 +03:00
|
|
|
-- | An async thread which processes the schema sync events
|
|
|
|
-- See Note [Schema Cache Sync]
|
2021-02-18 19:46:14 +03:00
|
|
|
startSchemaSyncProcessorThread ::
|
2020-12-28 15:56:00 +03:00
|
|
|
( C.ForkableMonadIO m,
|
Clean `AppEnv` and `AppContext` passing, remove `RunT`, reduce `ServerConfigCtx` uses
## Description
This PR does several different things that happen to overlap; the most important being:
- it removes `RunT`: it was redundant in places where we already had `Handler`, and only used in one other place, `SchemaUpdate`, for which a local `SchemaUpdateT` is more than enough;
- it reduces the number of places where we create a `ServerConfigCtx`, since now `HasServerConfigCtx` can be implemented directly by `SchemaUpdateT` and `Handler` based on the full `AppContext`;
- it drastically reduces the number of arguments we pass around in the app init code, by introducing `HasAppEnv`;
- it simplifies `HandlerCtx` to reduce duplication
In doing so, this changes paves the way towards removing `ServerConfigCtx`, since there are only very few places where we construct it: we can now introduce smaller classes than `HasServerConfigCtx`, that expose only a relevant subset of fields, and implement them where we now implement `HasServerConfigCtx`.
This PR is loosely based on ideas in #8337, that are no longer applicable due to the changes introduced in #8159. A challenge of this PR was the postgres tests, which were running in `PGMetadataStorageAppT CacheBuild` :scream_cat:
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8392
GitOrigin-RevId: b90c1359066d20dbea329c87762ccdd1217b4d69
2023-03-21 13:44:21 +03:00
|
|
|
HasAppEnv m,
|
Remove `ServerConfigCtx`.
### Description
This PR removes `ServerConfigCtx` and `HasServerConfigCtx`. Instead, it favours different approaches:
- when the code was only using one field, it passes that field explicitly (usually `SQLGenCtx` or `CheckFeatureFlag`)
- when the code was using several fields, but in only one function, it inlines
- for the cache build, it introduces `CacheStaticConfig` and `CacheDynamicConfig`, which are subsets of `AppEnv` and `AppContext` respectively
The main goal of this is to help with the modularization of the engine: as `ServerConfigCtx` had fields whose types were imported from several unrelated parts of the engine, using it tied together parts of the engine that should not be aware of one another (such as tying together `Hasura.LogicalModel` and `Hasura.GraphQL.Schema`).
The bulk of this PR is a change to the cache build, as a follow up to #8509: instead of giving the entire `ServerConfigCtx` as a incremental rule argument, we only give the new `CacheDynamicConfig` struct, which has fewer fields. The other required fields, that were coming from the `AppEnv`, are now given via the `HasCacheStaticConfig` constraint, which is a "subset" of `HasAppEnv`.
(Some further work could include moving `StringifyNumbers` out of `GraphQL.Schema.Options`, given how it is used all across the codebase, including in `RQL.DML`.)
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8513
GitOrigin-RevId: 818cbcd71494e3cd946b06adbb02ca328a8a298e
2023-04-04 18:59:58 +03:00
|
|
|
HasCacheStaticConfig m,
|
2023-02-03 04:03:23 +03:00
|
|
|
MonadMetadataStorage m,
|
harmonize network manager handling
## Description
### I want to speak to the `Manager`
Oh boy. This PR is both fairly straightforward and overreaching, so let's break it down.
For most network access, we need a [`HTTP.Manager`](https://hackage.haskell.org/package/http-client-0.1.0.0/docs/Network-HTTP-Client-Manager.html). It is created only once, at the top level, when starting the engine, and is then threaded through the application to wherever we need to make a network call. As of main, the way we do this is not standardized: most of the GraphQL execution code passes it "manually" as a function argument throughout the code. We also have a custom monad constraint, `HasHttpManagerM`, that describes a monad's ability to provide a manager. And, finally, several parts of the code store the manager in some kind of argument structure, such as `RunT`'s `RunCtx`.
This PR's first goal is to harmonize all of this: we always create the manager at the root, and we already have it when we do our very first `runReaderT`. Wouldn't it make sense for the rest of the code to not manually pass it anywhere, to not store it anywhere, but to always rely on the current monad providing it? This is, in short, what this PR does: it implements a constraint on the base monads, so that they provide the manager, and removes most explicit passing from the code.
### First come, first served
One way this PR goes a tiny bit further than "just" doing the aforementioned harmonization is that it starts the process of implementing the "Services oriented architecture" roughly outlined in this [draft document](https://docs.google.com/document/d/1FAigqrST0juU1WcT4HIxJxe1iEBwTuBZodTaeUvsKqQ/edit?usp=sharing). Instead of using the existing `HasHTTPManagerM`, this PR revamps it into the `ProvidesNetwork` service.
The idea is, again, that we should make all "external" dependencies of the engine, all things that the core of the engine doesn't care about, a "service". This allows us to define clear APIs for features, to choose different implementations based on which version of the engine we're running, harmonizes our many scattered monadic constraints... Which is why this service is called "Network": we can refine it, moving forward, to be the constraint that defines how all network communication is to operate, instead of relying on disparate classes constraint or hardcoded decisions. A comment in the code clarifies this intent.
### Side-effects? In my Haskell?
This PR also unavoidably touches some other aspects of the codebase. One such example: it introduces `Hasura.App.AppContext`, named after `HasuraPro.Context.AppContext`: a name for the reader structure at the base level. It also transforms `Handler` from a type alias to a newtype, as `Handler` is where we actually enforce HTTP limits; but without `Handler` being a distinct type, any code path could simply do a `runExceptT $ runReader` and forget to enforce them.
(As a rule of thumb, i am starting to consider any straggling `runReaderT` or `runExceptT` as a code smell: we should not stack / unstack monads haphazardly, and every layer should be an opaque `newtype` with a corresponding run function.)
## Further work
In several places, i have left TODOs when i have encountered things that suggest that we should do further unrelated cleanups. I'll write down the follow-up steps, either in the aforementioned document or on slack. But, in short, at a glance, in approximate order, we could:
- delete `ExecutionCtx` as it is only a subset of `ServerCtx`, and remove one more `runReaderT` call
- delete `ServerConfigCtx` as it is only a subset of `ServerCtx`, and remove it from `RunCtx`
- remove `ServerCtx` from `HandlerCtx`, and make it part of `AppContext`, or even make it the `AppContext` altogether (since, at least for the OSS version, `AppContext` is there again only a subset)
- remove `CacheBuildParams` and `CacheBuild` altogether, as they're just a distinct stack that is a `ReaderT` on top of `IO` that contains, you guessed it, the same thing as `ServerCtx`
- move `RunT` out of `RQL.Types` and rename it, since after the previous cleanups **it only contains `UserInfo`**; it could be bundled with the authentication service, made a small implementation detail in `Hasura.Server.Auth`
- rename `PGMetadaStorageT` to something a bit more accurate, such as `App`, and enforce its IO base
This would significantly simply our complex stack. From there, or in parallel, we can start moving existing dependencies as Services. For the purpose of supporting read replicas entitlement, we could move `MonadResolveSource` to a `SourceResolver` service, as attempted in #7653, and transform `UserAuthenticationM` into a `Authentication` service.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7736
GitOrigin-RevId: 68cce710eb9e7d752bda1ba0c49541d24df8209f
2023-02-22 18:53:52 +03:00
|
|
|
MonadResolveSource m,
|
2023-03-17 13:29:07 +03:00
|
|
|
ProvidesNetwork m
|
2021-09-24 01:56:37 +03:00
|
|
|
) =>
|
2023-03-17 13:29:07 +03:00
|
|
|
AppStateRef impl ->
|
2021-07-27 08:41:16 +03:00
|
|
|
STM.TVar Bool ->
|
2020-12-21 21:56:00 +03:00
|
|
|
ManagedT m Immortal.Thread
|
Clean `AppEnv` and `AppContext` passing, remove `RunT`, reduce `ServerConfigCtx` uses
## Description
This PR does several different things that happen to overlap; the most important being:
- it removes `RunT`: it was redundant in places where we already had `Handler`, and only used in one other place, `SchemaUpdate`, for which a local `SchemaUpdateT` is more than enough;
- it reduces the number of places where we create a `ServerConfigCtx`, since now `HasServerConfigCtx` can be implemented directly by `SchemaUpdateT` and `Handler` based on the full `AppContext`;
- it drastically reduces the number of arguments we pass around in the app init code, by introducing `HasAppEnv`;
- it simplifies `HandlerCtx` to reduce duplication
In doing so, this changes paves the way towards removing `ServerConfigCtx`, since there are only very few places where we construct it: we can now introduce smaller classes than `HasServerConfigCtx`, that expose only a relevant subset of fields, and implement them where we now implement `HasServerConfigCtx`.
This PR is loosely based on ideas in #8337, that are no longer applicable due to the changes introduced in #8159. A challenge of this PR was the postgres tests, which were running in `PGMetadataStorageAppT CacheBuild` :scream_cat:
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8392
GitOrigin-RevId: b90c1359066d20dbea329c87762ccdd1217b4d69
2023-03-21 13:44:21 +03:00
|
|
|
startSchemaSyncProcessorThread appStateRef logTVar = do
|
|
|
|
AppEnv {..} <- lift askAppEnv
|
|
|
|
let logger = _lsLogger appEnvLoggers
|
|
|
|
-- Start processor thread
|
|
|
|
processorThread <-
|
2023-05-24 16:51:56 +03:00
|
|
|
C.forkManagedT "SchemeUpdate.processor" logger
|
|
|
|
$ processor appEnvMetadataVersionRef appStateRef logTVar
|
Clean `AppEnv` and `AppContext` passing, remove `RunT`, reduce `ServerConfigCtx` uses
## Description
This PR does several different things that happen to overlap; the most important being:
- it removes `RunT`: it was redundant in places where we already had `Handler`, and only used in one other place, `SchemaUpdate`, for which a local `SchemaUpdateT` is more than enough;
- it reduces the number of places where we create a `ServerConfigCtx`, since now `HasServerConfigCtx` can be implemented directly by `SchemaUpdateT` and `Handler` based on the full `AppContext`;
- it drastically reduces the number of arguments we pass around in the app init code, by introducing `HasAppEnv`;
- it simplifies `HandlerCtx` to reduce duplication
In doing so, this changes paves the way towards removing `ServerConfigCtx`, since there are only very few places where we construct it: we can now introduce smaller classes than `HasServerConfigCtx`, that expose only a relevant subset of fields, and implement them where we now implement `HasServerConfigCtx`.
This PR is loosely based on ideas in #8337, that are no longer applicable due to the changes introduced in #8159. A challenge of this PR was the postgres tests, which were running in `PGMetadataStorageAppT CacheBuild` :scream_cat:
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8392
GitOrigin-RevId: b90c1359066d20dbea329c87762ccdd1217b4d69
2023-03-21 13:44:21 +03:00
|
|
|
logThreadStarted logger appEnvInstanceId TTProcessor processorThread
|
|
|
|
pure processorThread
|
2020-11-24 09:10:04 +03:00
|
|
|
|
2021-04-06 06:25:02 +03:00
|
|
|
-- TODO: This is also defined in multitenant, consider putting it in a library somewhere
|
|
|
|
forcePut :: STM.TMVar a -> a -> IO ()
|
|
|
|
forcePut v a = STM.atomically $ STM.tryTakeTMVar v >> STM.putTMVar v a
|
|
|
|
|
|
|
|
schemaVersionCheckHandler ::
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
PG.PGPool -> STM.TMVar MetadataResourceVersion -> IO (Either QErr ())
|
2021-04-06 06:25:02 +03:00
|
|
|
schemaVersionCheckHandler pool metaVersionRef =
|
2021-06-08 15:55:18 +03:00
|
|
|
runExceptT
|
2023-05-24 16:51:56 +03:00
|
|
|
( PG.runTx pool (PG.RepeatableRead, Nothing)
|
|
|
|
$ fetchMetadataResourceVersionFromCatalog
|
2021-07-27 08:41:16 +03:00
|
|
|
)
|
|
|
|
>>= \case
|
|
|
|
Right version -> Right <$> forcePut metaVersionRef version
|
|
|
|
Left err -> pure $ Left err
|
2021-06-08 15:55:18 +03:00
|
|
|
|
2021-07-27 08:41:16 +03:00
|
|
|
data ErrorState = ErrorState
|
|
|
|
{ _esLastErrorSeen :: !(Maybe QErr),
|
|
|
|
_esLastMetadataVersion :: !(Maybe MetadataResourceVersion)
|
|
|
|
}
|
2022-07-01 14:47:20 +03:00
|
|
|
deriving (Eq)
|
2021-06-08 15:55:18 +03:00
|
|
|
|
|
|
|
-- NOTE: The ErrorState type is to be used mainly for the `listener` method below.
|
|
|
|
-- This will help prevent logging the same error with the same MetadataResourceVersion
|
|
|
|
-- multiple times consecutively. When the `listener` is in ErrorState we don't log the
|
|
|
|
-- next error until the resource version has changed/updated.
|
|
|
|
|
2021-07-27 08:41:16 +03:00
|
|
|
defaultErrorState :: ErrorState
|
|
|
|
defaultErrorState = ErrorState Nothing Nothing
|
2021-06-08 15:55:18 +03:00
|
|
|
|
|
|
|
-- | NOTE: this can be updated to use lenses
|
2021-07-27 08:41:16 +03:00
|
|
|
updateErrorInState :: ErrorState -> QErr -> MetadataResourceVersion -> ErrorState
|
|
|
|
updateErrorInState es qerr mrv =
|
|
|
|
es
|
|
|
|
{ _esLastErrorSeen = Just qerr,
|
|
|
|
_esLastMetadataVersion = Just mrv
|
|
|
|
}
|
2021-06-08 15:55:18 +03:00
|
|
|
|
|
|
|
isInErrorState :: ErrorState -> Bool
|
2021-07-27 08:41:16 +03:00
|
|
|
isInErrorState es =
|
|
|
|
(isJust . _esLastErrorSeen) es && (isJust . _esLastMetadataVersion) es
|
2021-06-08 15:55:18 +03:00
|
|
|
|
2021-07-27 08:41:16 +03:00
|
|
|
toLogError :: ErrorState -> QErr -> MetadataResourceVersion -> Bool
|
|
|
|
toLogError es qerr mrv = not $ isQErrLastSeen || isMetadataResourceVersionLastSeen
|
|
|
|
where
|
|
|
|
isQErrLastSeen = case _esLastErrorSeen es of
|
|
|
|
Just lErrS -> lErrS == qerr
|
|
|
|
Nothing -> False
|
|
|
|
|
|
|
|
isMetadataResourceVersionLastSeen = case _esLastMetadataVersion es of
|
|
|
|
Just lMRV -> lMRV == mrv
|
|
|
|
Nothing -> False
|
2021-04-06 06:25:02 +03:00
|
|
|
|
2020-11-24 09:10:04 +03:00
|
|
|
-- | An IO action that listens to postgres for events and pushes them to a Queue, in a loop forever.
|
|
|
|
listener ::
|
2023-05-24 16:51:56 +03:00
|
|
|
(MonadIO m) =>
|
2021-04-06 06:25:02 +03:00
|
|
|
Logger Hasura ->
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
PG.PGPool ->
|
2021-04-06 06:25:02 +03:00
|
|
|
STM.TMVar MetadataResourceVersion ->
|
|
|
|
Milliseconds ->
|
|
|
|
m void
|
2021-07-27 08:41:16 +03:00
|
|
|
listener logger pool metaVersionRef interval = L.iterateM_ listenerLoop defaultErrorState
|
2021-06-08 15:55:18 +03:00
|
|
|
where
|
|
|
|
listenerLoop errorState = do
|
|
|
|
mrv <- liftIO $ STM.atomically $ STM.tryTakeTMVar metaVersionRef
|
|
|
|
resp <- liftIO $ schemaVersionCheckHandler pool metaVersionRef
|
|
|
|
let metadataVersion = fromMaybe initialResourceVersion mrv
|
|
|
|
nextErr <- case resp of
|
|
|
|
Left respErr -> do
|
2021-07-27 08:41:16 +03:00
|
|
|
if (toLogError errorState respErr metadataVersion)
|
2021-06-08 15:55:18 +03:00
|
|
|
then do
|
|
|
|
logError logger TTListener $ TEQueryError respErr
|
|
|
|
logInfo logger TTListener $ object ["metadataResourceVersion" .= toJSON metadataVersion]
|
2021-07-27 08:41:16 +03:00
|
|
|
pure $ updateErrorInState errorState respErr metadataVersion
|
2021-06-08 15:55:18 +03:00
|
|
|
else do
|
|
|
|
pure errorState
|
|
|
|
Right _ -> do
|
2023-05-24 16:51:56 +03:00
|
|
|
when (isInErrorState errorState)
|
|
|
|
$ logInfo logger TTListener
|
|
|
|
$ object ["message" .= ("SchemaSync Restored..." :: Text)]
|
2021-07-27 08:41:16 +03:00
|
|
|
pure defaultErrorState
|
2021-06-08 15:55:18 +03:00
|
|
|
liftIO $ C.sleep $ milliseconds interval
|
|
|
|
pure nextErr
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2020-03-05 20:59:26 +03:00
|
|
|
-- | An IO action that processes events from Queue, in a loop forever.
|
2021-04-06 06:25:02 +03:00
|
|
|
processor ::
|
2023-03-17 13:29:07 +03:00
|
|
|
forall m void impl.
|
2020-12-14 07:30:19 +03:00
|
|
|
( C.ForkableMonadIO m,
|
Clean `AppEnv` and `AppContext` passing, remove `RunT`, reduce `ServerConfigCtx` uses
## Description
This PR does several different things that happen to overlap; the most important being:
- it removes `RunT`: it was redundant in places where we already had `Handler`, and only used in one other place, `SchemaUpdate`, for which a local `SchemaUpdateT` is more than enough;
- it reduces the number of places where we create a `ServerConfigCtx`, since now `HasServerConfigCtx` can be implemented directly by `SchemaUpdateT` and `Handler` based on the full `AppContext`;
- it drastically reduces the number of arguments we pass around in the app init code, by introducing `HasAppEnv`;
- it simplifies `HandlerCtx` to reduce duplication
In doing so, this changes paves the way towards removing `ServerConfigCtx`, since there are only very few places where we construct it: we can now introduce smaller classes than `HasServerConfigCtx`, that expose only a relevant subset of fields, and implement them where we now implement `HasServerConfigCtx`.
This PR is loosely based on ideas in #8337, that are no longer applicable due to the changes introduced in #8159. A challenge of this PR was the postgres tests, which were running in `PGMetadataStorageAppT CacheBuild` :scream_cat:
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8392
GitOrigin-RevId: b90c1359066d20dbea329c87762ccdd1217b4d69
2023-03-21 13:44:21 +03:00
|
|
|
HasAppEnv m,
|
Remove `ServerConfigCtx`.
### Description
This PR removes `ServerConfigCtx` and `HasServerConfigCtx`. Instead, it favours different approaches:
- when the code was only using one field, it passes that field explicitly (usually `SQLGenCtx` or `CheckFeatureFlag`)
- when the code was using several fields, but in only one function, it inlines
- for the cache build, it introduces `CacheStaticConfig` and `CacheDynamicConfig`, which are subsets of `AppEnv` and `AppContext` respectively
The main goal of this is to help with the modularization of the engine: as `ServerConfigCtx` had fields whose types were imported from several unrelated parts of the engine, using it tied together parts of the engine that should not be aware of one another (such as tying together `Hasura.LogicalModel` and `Hasura.GraphQL.Schema`).
The bulk of this PR is a change to the cache build, as a follow up to #8509: instead of giving the entire `ServerConfigCtx` as a incremental rule argument, we only give the new `CacheDynamicConfig` struct, which has fewer fields. The other required fields, that were coming from the `AppEnv`, are now given via the `HasCacheStaticConfig` constraint, which is a "subset" of `HasAppEnv`.
(Some further work could include moving `StringifyNumbers` out of `GraphQL.Schema.Options`, given how it is used all across the codebase, including in `RQL.DML`.)
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8513
GitOrigin-RevId: 818cbcd71494e3cd946b06adbb02ca328a8a298e
2023-04-04 18:59:58 +03:00
|
|
|
HasCacheStaticConfig m,
|
2023-02-03 04:03:23 +03:00
|
|
|
MonadMetadataStorage m,
|
harmonize network manager handling
## Description
### I want to speak to the `Manager`
Oh boy. This PR is both fairly straightforward and overreaching, so let's break it down.
For most network access, we need a [`HTTP.Manager`](https://hackage.haskell.org/package/http-client-0.1.0.0/docs/Network-HTTP-Client-Manager.html). It is created only once, at the top level, when starting the engine, and is then threaded through the application to wherever we need to make a network call. As of main, the way we do this is not standardized: most of the GraphQL execution code passes it "manually" as a function argument throughout the code. We also have a custom monad constraint, `HasHttpManagerM`, that describes a monad's ability to provide a manager. And, finally, several parts of the code store the manager in some kind of argument structure, such as `RunT`'s `RunCtx`.
This PR's first goal is to harmonize all of this: we always create the manager at the root, and we already have it when we do our very first `runReaderT`. Wouldn't it make sense for the rest of the code to not manually pass it anywhere, to not store it anywhere, but to always rely on the current monad providing it? This is, in short, what this PR does: it implements a constraint on the base monads, so that they provide the manager, and removes most explicit passing from the code.
### First come, first served
One way this PR goes a tiny bit further than "just" doing the aforementioned harmonization is that it starts the process of implementing the "Services oriented architecture" roughly outlined in this [draft document](https://docs.google.com/document/d/1FAigqrST0juU1WcT4HIxJxe1iEBwTuBZodTaeUvsKqQ/edit?usp=sharing). Instead of using the existing `HasHTTPManagerM`, this PR revamps it into the `ProvidesNetwork` service.
The idea is, again, that we should make all "external" dependencies of the engine, all things that the core of the engine doesn't care about, a "service". This allows us to define clear APIs for features, to choose different implementations based on which version of the engine we're running, harmonizes our many scattered monadic constraints... Which is why this service is called "Network": we can refine it, moving forward, to be the constraint that defines how all network communication is to operate, instead of relying on disparate classes constraint or hardcoded decisions. A comment in the code clarifies this intent.
### Side-effects? In my Haskell?
This PR also unavoidably touches some other aspects of the codebase. One such example: it introduces `Hasura.App.AppContext`, named after `HasuraPro.Context.AppContext`: a name for the reader structure at the base level. It also transforms `Handler` from a type alias to a newtype, as `Handler` is where we actually enforce HTTP limits; but without `Handler` being a distinct type, any code path could simply do a `runExceptT $ runReader` and forget to enforce them.
(As a rule of thumb, i am starting to consider any straggling `runReaderT` or `runExceptT` as a code smell: we should not stack / unstack monads haphazardly, and every layer should be an opaque `newtype` with a corresponding run function.)
## Further work
In several places, i have left TODOs when i have encountered things that suggest that we should do further unrelated cleanups. I'll write down the follow-up steps, either in the aforementioned document or on slack. But, in short, at a glance, in approximate order, we could:
- delete `ExecutionCtx` as it is only a subset of `ServerCtx`, and remove one more `runReaderT` call
- delete `ServerConfigCtx` as it is only a subset of `ServerCtx`, and remove it from `RunCtx`
- remove `ServerCtx` from `HandlerCtx`, and make it part of `AppContext`, or even make it the `AppContext` altogether (since, at least for the OSS version, `AppContext` is there again only a subset)
- remove `CacheBuildParams` and `CacheBuild` altogether, as they're just a distinct stack that is a `ReaderT` on top of `IO` that contains, you guessed it, the same thing as `ServerCtx`
- move `RunT` out of `RQL.Types` and rename it, since after the previous cleanups **it only contains `UserInfo`**; it could be bundled with the authentication service, made a small implementation detail in `Hasura.Server.Auth`
- rename `PGMetadaStorageT` to something a bit more accurate, such as `App`, and enforce its IO base
This would significantly simply our complex stack. From there, or in parallel, we can start moving existing dependencies as Services. For the purpose of supporting read replicas entitlement, we could move `MonadResolveSource` to a `SourceResolver` service, as attempted in #7653, and transform `UserAuthenticationM` into a `Authentication` service.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7736
GitOrigin-RevId: 68cce710eb9e7d752bda1ba0c49541d24df8209f
2023-02-22 18:53:52 +03:00
|
|
|
MonadResolveSource m,
|
2023-03-17 13:29:07 +03:00
|
|
|
ProvidesNetwork m
|
2021-09-24 01:56:37 +03:00
|
|
|
) =>
|
2021-04-06 06:25:02 +03:00
|
|
|
STM.TMVar MetadataResourceVersion ->
|
2023-03-17 13:29:07 +03:00
|
|
|
AppStateRef impl ->
|
2021-07-27 08:41:16 +03:00
|
|
|
STM.TVar Bool ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m void
|
2019-03-12 08:46:27 +03:00
|
|
|
processor
|
2021-04-06 06:25:02 +03:00
|
|
|
metaVersionRef
|
2023-03-17 13:29:07 +03:00
|
|
|
appStateRef
|
Clean `AppEnv` and `AppContext` passing, remove `RunT`, reduce `ServerConfigCtx` uses
## Description
This PR does several different things that happen to overlap; the most important being:
- it removes `RunT`: it was redundant in places where we already had `Handler`, and only used in one other place, `SchemaUpdate`, for which a local `SchemaUpdateT` is more than enough;
- it reduces the number of places where we create a `ServerConfigCtx`, since now `HasServerConfigCtx` can be implemented directly by `SchemaUpdateT` and `Handler` based on the full `AppContext`;
- it drastically reduces the number of arguments we pass around in the app init code, by introducing `HasAppEnv`;
- it simplifies `HandlerCtx` to reduce duplication
In doing so, this changes paves the way towards removing `ServerConfigCtx`, since there are only very few places where we construct it: we can now introduce smaller classes than `HasServerConfigCtx`, that expose only a relevant subset of fields, and implement them where we now implement `HasServerConfigCtx`.
This PR is loosely based on ideas in #8337, that are no longer applicable due to the changes introduced in #8159. A challenge of this PR was the postgres tests, which were running in `PGMetadataStorageAppT CacheBuild` :scream_cat:
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8392
GitOrigin-RevId: b90c1359066d20dbea329c87762ccdd1217b4d69
2023-03-21 13:44:21 +03:00
|
|
|
logTVar = forever do
|
2021-04-06 06:25:02 +03:00
|
|
|
metaVersion <- liftIO $ STM.atomically $ STM.takeTMVar metaVersionRef
|
Clean `AppEnv` and `AppContext` passing, remove `RunT`, reduce `ServerConfigCtx` uses
## Description
This PR does several different things that happen to overlap; the most important being:
- it removes `RunT`: it was redundant in places where we already had `Handler`, and only used in one other place, `SchemaUpdate`, for which a local `SchemaUpdateT` is more than enough;
- it reduces the number of places where we create a `ServerConfigCtx`, since now `HasServerConfigCtx` can be implemented directly by `SchemaUpdateT` and `Handler` based on the full `AppContext`;
- it drastically reduces the number of arguments we pass around in the app init code, by introducing `HasAppEnv`;
- it simplifies `HandlerCtx` to reduce duplication
In doing so, this changes paves the way towards removing `ServerConfigCtx`, since there are only very few places where we construct it: we can now introduce smaller classes than `HasServerConfigCtx`, that expose only a relevant subset of fields, and implement them where we now implement `HasServerConfigCtx`.
This PR is loosely based on ideas in #8337, that are no longer applicable due to the changes introduced in #8159. A challenge of this PR was the postgres tests, which were running in `PGMetadataStorageAppT CacheBuild` :scream_cat:
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8392
GitOrigin-RevId: b90c1359066d20dbea329c87762ccdd1217b4d69
2023-03-21 13:44:21 +03:00
|
|
|
refreshSchemaCache metaVersion appStateRef TTProcessor logTVar
|
|
|
|
|
2021-04-06 06:25:02 +03:00
|
|
|
refreshSchemaCache ::
|
2020-12-14 07:30:19 +03:00
|
|
|
( MonadIO m,
|
2021-04-06 06:25:02 +03:00
|
|
|
MonadBaseControl IO m,
|
Clean `AppEnv` and `AppContext` passing, remove `RunT`, reduce `ServerConfigCtx` uses
## Description
This PR does several different things that happen to overlap; the most important being:
- it removes `RunT`: it was redundant in places where we already had `Handler`, and only used in one other place, `SchemaUpdate`, for which a local `SchemaUpdateT` is more than enough;
- it reduces the number of places where we create a `ServerConfigCtx`, since now `HasServerConfigCtx` can be implemented directly by `SchemaUpdateT` and `Handler` based on the full `AppContext`;
- it drastically reduces the number of arguments we pass around in the app init code, by introducing `HasAppEnv`;
- it simplifies `HandlerCtx` to reduce duplication
In doing so, this changes paves the way towards removing `ServerConfigCtx`, since there are only very few places where we construct it: we can now introduce smaller classes than `HasServerConfigCtx`, that expose only a relevant subset of fields, and implement them where we now implement `HasServerConfigCtx`.
This PR is loosely based on ideas in #8337, that are no longer applicable due to the changes introduced in #8159. A challenge of this PR was the postgres tests, which were running in `PGMetadataStorageAppT CacheBuild` :scream_cat:
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8392
GitOrigin-RevId: b90c1359066d20dbea329c87762ccdd1217b4d69
2023-03-21 13:44:21 +03:00
|
|
|
HasAppEnv m,
|
Remove `ServerConfigCtx`.
### Description
This PR removes `ServerConfigCtx` and `HasServerConfigCtx`. Instead, it favours different approaches:
- when the code was only using one field, it passes that field explicitly (usually `SQLGenCtx` or `CheckFeatureFlag`)
- when the code was using several fields, but in only one function, it inlines
- for the cache build, it introduces `CacheStaticConfig` and `CacheDynamicConfig`, which are subsets of `AppEnv` and `AppContext` respectively
The main goal of this is to help with the modularization of the engine: as `ServerConfigCtx` had fields whose types were imported from several unrelated parts of the engine, using it tied together parts of the engine that should not be aware of one another (such as tying together `Hasura.LogicalModel` and `Hasura.GraphQL.Schema`).
The bulk of this PR is a change to the cache build, as a follow up to #8509: instead of giving the entire `ServerConfigCtx` as a incremental rule argument, we only give the new `CacheDynamicConfig` struct, which has fewer fields. The other required fields, that were coming from the `AppEnv`, are now given via the `HasCacheStaticConfig` constraint, which is a "subset" of `HasAppEnv`.
(Some further work could include moving `StringifyNumbers` out of `GraphQL.Schema.Options`, given how it is used all across the codebase, including in `RQL.DML`.)
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8513
GitOrigin-RevId: 818cbcd71494e3cd946b06adbb02ca328a8a298e
2023-04-04 18:59:58 +03:00
|
|
|
HasCacheStaticConfig m,
|
2021-04-06 06:25:02 +03:00
|
|
|
MonadMetadataStorage m,
|
harmonize network manager handling
## Description
### I want to speak to the `Manager`
Oh boy. This PR is both fairly straightforward and overreaching, so let's break it down.
For most network access, we need a [`HTTP.Manager`](https://hackage.haskell.org/package/http-client-0.1.0.0/docs/Network-HTTP-Client-Manager.html). It is created only once, at the top level, when starting the engine, and is then threaded through the application to wherever we need to make a network call. As of main, the way we do this is not standardized: most of the GraphQL execution code passes it "manually" as a function argument throughout the code. We also have a custom monad constraint, `HasHttpManagerM`, that describes a monad's ability to provide a manager. And, finally, several parts of the code store the manager in some kind of argument structure, such as `RunT`'s `RunCtx`.
This PR's first goal is to harmonize all of this: we always create the manager at the root, and we already have it when we do our very first `runReaderT`. Wouldn't it make sense for the rest of the code to not manually pass it anywhere, to not store it anywhere, but to always rely on the current monad providing it? This is, in short, what this PR does: it implements a constraint on the base monads, so that they provide the manager, and removes most explicit passing from the code.
### First come, first served
One way this PR goes a tiny bit further than "just" doing the aforementioned harmonization is that it starts the process of implementing the "Services oriented architecture" roughly outlined in this [draft document](https://docs.google.com/document/d/1FAigqrST0juU1WcT4HIxJxe1iEBwTuBZodTaeUvsKqQ/edit?usp=sharing). Instead of using the existing `HasHTTPManagerM`, this PR revamps it into the `ProvidesNetwork` service.
The idea is, again, that we should make all "external" dependencies of the engine, all things that the core of the engine doesn't care about, a "service". This allows us to define clear APIs for features, to choose different implementations based on which version of the engine we're running, harmonizes our many scattered monadic constraints... Which is why this service is called "Network": we can refine it, moving forward, to be the constraint that defines how all network communication is to operate, instead of relying on disparate classes constraint or hardcoded decisions. A comment in the code clarifies this intent.
### Side-effects? In my Haskell?
This PR also unavoidably touches some other aspects of the codebase. One such example: it introduces `Hasura.App.AppContext`, named after `HasuraPro.Context.AppContext`: a name for the reader structure at the base level. It also transforms `Handler` from a type alias to a newtype, as `Handler` is where we actually enforce HTTP limits; but without `Handler` being a distinct type, any code path could simply do a `runExceptT $ runReader` and forget to enforce them.
(As a rule of thumb, i am starting to consider any straggling `runReaderT` or `runExceptT` as a code smell: we should not stack / unstack monads haphazardly, and every layer should be an opaque `newtype` with a corresponding run function.)
## Further work
In several places, i have left TODOs when i have encountered things that suggest that we should do further unrelated cleanups. I'll write down the follow-up steps, either in the aforementioned document or on slack. But, in short, at a glance, in approximate order, we could:
- delete `ExecutionCtx` as it is only a subset of `ServerCtx`, and remove one more `runReaderT` call
- delete `ServerConfigCtx` as it is only a subset of `ServerCtx`, and remove it from `RunCtx`
- remove `ServerCtx` from `HandlerCtx`, and make it part of `AppContext`, or even make it the `AppContext` altogether (since, at least for the OSS version, `AppContext` is there again only a subset)
- remove `CacheBuildParams` and `CacheBuild` altogether, as they're just a distinct stack that is a `ReaderT` on top of `IO` that contains, you guessed it, the same thing as `ServerCtx`
- move `RunT` out of `RQL.Types` and rename it, since after the previous cleanups **it only contains `UserInfo`**; it could be bundled with the authentication service, made a small implementation detail in `Hasura.Server.Auth`
- rename `PGMetadaStorageT` to something a bit more accurate, such as `App`, and enforce its IO base
This would significantly simply our complex stack. From there, or in parallel, we can start moving existing dependencies as Services. For the purpose of supporting read replicas entitlement, we could move `MonadResolveSource` to a `SourceResolver` service, as attempted in #7653, and transform `UserAuthenticationM` into a `Authentication` service.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7736
GitOrigin-RevId: 68cce710eb9e7d752bda1ba0c49541d24df8209f
2023-02-22 18:53:52 +03:00
|
|
|
MonadResolveSource m,
|
|
|
|
ProvidesNetwork m
|
2021-09-24 01:56:37 +03:00
|
|
|
) =>
|
2021-07-27 08:41:16 +03:00
|
|
|
MetadataResourceVersion ->
|
2023-03-17 13:29:07 +03:00
|
|
|
AppStateRef impl ->
|
2022-11-06 01:37:04 +03:00
|
|
|
SchemaSyncThreadType ->
|
2021-07-27 08:41:16 +03:00
|
|
|
STM.TVar Bool ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m ()
|
2019-03-12 08:46:27 +03:00
|
|
|
refreshSchemaCache
|
2021-04-06 06:25:02 +03:00
|
|
|
resourceVersion
|
2023-03-17 13:29:07 +03:00
|
|
|
appStateRef
|
2019-03-12 08:46:27 +03:00
|
|
|
threadType
|
2021-07-27 08:41:16 +03:00
|
|
|
logTVar = do
|
2023-04-27 17:30:23 +03:00
|
|
|
AppEnv {..} <- askAppEnv
|
Clean `AppEnv` and `AppContext` passing, remove `RunT`, reduce `ServerConfigCtx` uses
## Description
This PR does several different things that happen to overlap; the most important being:
- it removes `RunT`: it was redundant in places where we already had `Handler`, and only used in one other place, `SchemaUpdate`, for which a local `SchemaUpdateT` is more than enough;
- it reduces the number of places where we create a `ServerConfigCtx`, since now `HasServerConfigCtx` can be implemented directly by `SchemaUpdateT` and `Handler` based on the full `AppContext`;
- it drastically reduces the number of arguments we pass around in the app init code, by introducing `HasAppEnv`;
- it simplifies `HandlerCtx` to reduce duplication
In doing so, this changes paves the way towards removing `ServerConfigCtx`, since there are only very few places where we construct it: we can now introduce smaller classes than `HasServerConfigCtx`, that expose only a relevant subset of fields, and implement them where we now implement `HasServerConfigCtx`.
This PR is loosely based on ideas in #8337, that are no longer applicable due to the changes introduced in #8159. A challenge of this PR was the postgres tests, which were running in `PGMetadataStorageAppT CacheBuild` :scream_cat:
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8392
GitOrigin-RevId: b90c1359066d20dbea329c87762ccdd1217b4d69
2023-03-21 13:44:21 +03:00
|
|
|
let logger = _lsLogger appEnvLoggers
|
2023-05-24 16:51:56 +03:00
|
|
|
respErr <- runExceptT
|
|
|
|
$ withSchemaCacheUpdate appStateRef logger (Just logTVar)
|
|
|
|
$ do
|
2023-07-06 18:34:50 +03:00
|
|
|
rebuildableCache <- liftIO $ getRebuildableSchemaCacheWithVersion appStateRef
|
Clean `AppEnv` and `AppContext` passing, remove `RunT`, reduce `ServerConfigCtx` uses
## Description
This PR does several different things that happen to overlap; the most important being:
- it removes `RunT`: it was redundant in places where we already had `Handler`, and only used in one other place, `SchemaUpdate`, for which a local `SchemaUpdateT` is more than enough;
- it reduces the number of places where we create a `ServerConfigCtx`, since now `HasServerConfigCtx` can be implemented directly by `SchemaUpdateT` and `Handler` based on the full `AppContext`;
- it drastically reduces the number of arguments we pass around in the app init code, by introducing `HasAppEnv`;
- it simplifies `HandlerCtx` to reduce duplication
In doing so, this changes paves the way towards removing `ServerConfigCtx`, since there are only very few places where we construct it: we can now introduce smaller classes than `HasServerConfigCtx`, that expose only a relevant subset of fields, and implement them where we now implement `HasServerConfigCtx`.
This PR is loosely based on ideas in #8337, that are no longer applicable due to the changes introduced in #8159. A challenge of this PR was the postgres tests, which were running in `PGMetadataStorageAppT CacheBuild` :scream_cat:
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8392
GitOrigin-RevId: b90c1359066d20dbea329c87762ccdd1217b4d69
2023-03-21 13:44:21 +03:00
|
|
|
appContext <- liftIO $ getAppContext appStateRef
|
2023-04-27 17:30:23 +03:00
|
|
|
let dynamicConfig = buildCacheDynamicConfig appContext
|
2023-06-06 11:49:53 +03:00
|
|
|
-- the instance which triggered the schema sync event would have stored
|
|
|
|
-- the source introspection, hence we can ignore it here
|
2023-06-26 13:55:11 +03:00
|
|
|
(msg, cache, _, _sourcesIntrospection, _schemaRegistryAction) <-
|
Remove `ServerConfigCtx`.
### Description
This PR removes `ServerConfigCtx` and `HasServerConfigCtx`. Instead, it favours different approaches:
- when the code was only using one field, it passes that field explicitly (usually `SQLGenCtx` or `CheckFeatureFlag`)
- when the code was using several fields, but in only one function, it inlines
- for the cache build, it introduces `CacheStaticConfig` and `CacheDynamicConfig`, which are subsets of `AppEnv` and `AppContext` respectively
The main goal of this is to help with the modularization of the engine: as `ServerConfigCtx` had fields whose types were imported from several unrelated parts of the engine, using it tied together parts of the engine that should not be aware of one another (such as tying together `Hasura.LogicalModel` and `Hasura.GraphQL.Schema`).
The bulk of this PR is a change to the cache build, as a follow up to #8509: instead of giving the entire `ServerConfigCtx` as a incremental rule argument, we only give the new `CacheDynamicConfig` struct, which has fewer fields. The other required fields, that were coming from the `AppEnv`, are now given via the `HasCacheStaticConfig` constraint, which is a "subset" of `HasAppEnv`.
(Some further work could include moving `StringifyNumbers` out of `GraphQL.Schema.Options`, given how it is used all across the codebase, including in `RQL.DML`.)
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8513
GitOrigin-RevId: 818cbcd71494e3cd946b06adbb02ca328a8a298e
2023-04-04 18:59:58 +03:00
|
|
|
runCacheRWT dynamicConfig rebuildableCache $ do
|
2021-04-06 06:25:02 +03:00
|
|
|
schemaCache <- askSchemaCache
|
2023-03-28 16:26:08 +03:00
|
|
|
let engineResourceVersion = scMetadataResourceVersion schemaCache
|
|
|
|
unless (engineResourceVersion == resourceVersion) $ do
|
2023-05-24 16:51:56 +03:00
|
|
|
logInfo logger threadType
|
|
|
|
$ String
|
|
|
|
$ T.unwords
|
|
|
|
[ "Received metadata resource version:",
|
|
|
|
showMetadataResourceVersion resourceVersion <> ",",
|
|
|
|
"different from the current engine resource version:",
|
|
|
|
showMetadataResourceVersion engineResourceVersion <> ".",
|
|
|
|
"Trying to update the schema cache."
|
|
|
|
]
|
2023-03-28 16:26:08 +03:00
|
|
|
|
|
|
|
MetadataWithResourceVersion metadata latestResourceVersion <- liftEitherM fetchMetadata
|
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
logInfo logger threadType
|
|
|
|
$ String
|
|
|
|
$ T.unwords
|
|
|
|
[ "Fetched metadata with resource version:",
|
|
|
|
showMetadataResourceVersion latestResourceVersion
|
|
|
|
]
|
2023-03-28 16:26:08 +03:00
|
|
|
|
|
|
|
notifications <- liftEitherM $ fetchMetadataNotifications engineResourceVersion appEnvInstanceId
|
|
|
|
|
|
|
|
case notifications of
|
|
|
|
[] -> do
|
2023-05-24 16:51:56 +03:00
|
|
|
logInfo logger threadType
|
|
|
|
$ String
|
|
|
|
$ T.unwords
|
|
|
|
[ "Fetched metadata notifications and received no notifications. Not updating the schema cache.",
|
|
|
|
"Only setting resource version:",
|
|
|
|
showMetadataResourceVersion latestResourceVersion,
|
|
|
|
"in schema cache"
|
|
|
|
]
|
2023-03-28 16:26:08 +03:00
|
|
|
setMetadataResourceVersionInSchemaCache latestResourceVersion
|
|
|
|
_ -> do
|
2023-05-24 16:51:56 +03:00
|
|
|
logInfo logger threadType
|
|
|
|
$ String "Fetched metadata notifications and received some notifications. Updating the schema cache."
|
2023-03-28 16:26:08 +03:00
|
|
|
let cacheInvalidations =
|
|
|
|
if any ((== (engineResourceVersion + 1)) . fst) notifications
|
|
|
|
then -- If (engineResourceVersion + 1) is in the list of notifications then
|
|
|
|
-- we know that we haven't missed any.
|
|
|
|
mconcat $ snd <$> notifications
|
|
|
|
else -- Otherwise we may have missed some notifications so we need to invalidate the
|
|
|
|
-- whole cache.
|
|
|
|
|
|
|
|
CacheInvalidations
|
|
|
|
{ ciMetadata = True,
|
|
|
|
ciRemoteSchemas = HS.fromList $ getAllRemoteSchemas schemaCache,
|
2023-04-26 18:42:13 +03:00
|
|
|
ciSources = HS.fromList $ HashMap.keys $ scSources schemaCache,
|
2023-03-28 16:26:08 +03:00
|
|
|
ciDataConnectors =
|
2023-05-24 16:51:56 +03:00
|
|
|
maybe mempty (HS.fromList . HashMap.keys . unBackendInfoWrapper)
|
|
|
|
$ BackendMap.lookup @'DataConnector
|
|
|
|
$ scBackendCache schemaCache
|
2023-03-28 16:26:08 +03:00
|
|
|
}
|
2023-07-17 11:10:46 +03:00
|
|
|
buildSchemaCacheWithOptions CatalogSync cacheInvalidations metadata (Just latestResourceVersion)
|
2023-03-28 16:26:08 +03:00
|
|
|
setMetadataResourceVersionInSchemaCache latestResourceVersion
|
2023-05-24 16:51:56 +03:00
|
|
|
logInfo logger threadType
|
|
|
|
$ String
|
|
|
|
$ "Schema cache updated with resource version: "
|
|
|
|
<> showMetadataResourceVersion latestResourceVersion
|
2021-04-06 06:25:02 +03:00
|
|
|
pure (msg, cache)
|
|
|
|
onLeft respErr (logError logger threadType . TEQueryError)
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2022-11-06 01:37:04 +03:00
|
|
|
logInfo :: (MonadIO m) => Logger Hasura -> SchemaSyncThreadType -> Value -> m ()
|
2019-03-12 08:46:27 +03:00
|
|
|
logInfo logger threadType val =
|
2023-05-24 16:51:56 +03:00
|
|
|
unLogger logger
|
|
|
|
$ SchemaSyncLog LevelInfo threadType val
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2022-11-06 01:37:04 +03:00
|
|
|
logError :: (MonadIO m, ToJSON a) => Logger Hasura -> SchemaSyncThreadType -> a -> m ()
|
2019-03-12 08:46:27 +03:00
|
|
|
logError logger threadType err =
|
2023-05-24 16:51:56 +03:00
|
|
|
unLogger logger
|
|
|
|
$ SchemaSyncLog LevelError threadType
|
|
|
|
$ object ["error" .= toJSON err]
|