2022-03-16 03:39:21 +03:00
|
|
|
module Main
|
|
|
|
( main,
|
|
|
|
)
|
|
|
|
where
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2021-09-08 16:06:24 +03:00
|
|
|
import Control.Concurrent.Extended qualified as C
|
2020-07-14 22:00:58 +03:00
|
|
|
import Control.Exception
|
2021-09-08 16:06:24 +03:00
|
|
|
import Control.Monad.Trans.Managed (ManagedT (..), lowerManagedT)
|
2023-07-20 14:18:18 +03:00
|
|
|
import Data.Aeson qualified as J
|
2021-09-08 16:06:24 +03:00
|
|
|
import Data.ByteString.Char8 qualified as BC
|
|
|
|
import Data.Environment qualified as Env
|
|
|
|
import Data.Int (Int64)
|
|
|
|
import Data.Kind (Type)
|
|
|
|
import Data.Text.Conversions (convertText)
|
|
|
|
import Data.Time.Clock (getCurrentTime)
|
|
|
|
import Data.Time.Clock.POSIX (getPOSIXTime)
|
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-02-09 01:41:09 +03:00
|
|
|
import GHC.Debug.Stub
|
2021-09-08 16:06:24 +03:00
|
|
|
import GHC.TypeLits (Symbol)
|
2019-11-26 15:14:21 +03:00
|
|
|
import Hasura.App
|
2023-02-24 21:09:36 +03:00
|
|
|
import Hasura.App.State
|
2023-03-30 19:31:50 +03:00
|
|
|
( AppEnv (..),
|
|
|
|
Loggers (..),
|
|
|
|
)
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.Backends.Postgres.Connection.MonadTx
|
|
|
|
import Hasura.Backends.Postgres.Connection.Settings
|
2021-09-08 16:06:24 +03:00
|
|
|
import Hasura.GC qualified as GC
|
|
|
|
import Hasura.Logging (Hasura, LogLevel (..), defaultEnabledEngineLogTypes)
|
Clean metadata arguments
## Description
Thanks to #1664, the Metadata API types no longer require a `ToJSON` instance. This PR follows up with a cleanup of the types of the arguments to the metadata API:
- whenever possible, it moves those argument types to where they're used (RQL.DDL.*)
- it removes all unrequired instances (mostly `ToJSON`)
This PR does not attempt to do it for _all_ such argument types. For some of the metadata operations, the type used to describe the argument to the API and used to represent the value in the metadata are one and the same (like for `CreateEndpoint`). Sometimes, the two types are intertwined in complex ways (`RemoteRelationship` and `RemoteRelationshipDef`). In the spirit of only doing uncontroversial cleaning work, this PR only moves types that are not used outside of RQL.DDL.
Furthermore, this is a small step towards separating the different types all jumbled together in RQL.Types.
## Notes
This PR also improves several `FromJSON` instances to make use of `withObject`, and to use a human readable string instead of a type name in error messages whenever possible. For instance:
- before: `expected Object for Object, but encountered X`
after: `expected Object for add computed field, but encountered X`
- before: `Expecting an object for update query`
after: `expected Object for update query, but encountered X`
This PR also renames `CreateFunctionPermission` to `FunctionPermissionArgument`, to remove the quite surprising `type DropFunctionPermission = CreateFunctionPermission`.
This PR also deletes some dead code, mostly in RQL.DML.
This PR also moves a PG-specific source resolving function from DDL.Schema.Source to the only place where it is used: App.hs.
https://github.com/hasura/graphql-engine-mono/pull/1844
GitOrigin-RevId: a594521194bb7fe6a111b02a9e099896f9fed59c
2021-07-27 13:41:42 +03:00
|
|
|
import Hasura.Prelude
|
2019-11-20 21:21:30 +03:00
|
|
|
import Hasura.RQL.DDL.Schema
|
2023-04-05 11:57:19 +03:00
|
|
|
import Hasura.Server.App (CEConsoleType (OSSConsole))
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Server.Init
|
2021-09-08 16:06:24 +03:00
|
|
|
import Hasura.Server.Metrics (ServerMetricsSpec, createServerMetrics)
|
|
|
|
import Hasura.Server.Migrate (downgradeCatalog)
|
2022-07-24 00:18:01 +03:00
|
|
|
import Hasura.Server.Prometheus (makeDummyPrometheusMetrics)
|
2019-11-26 15:14:21 +03:00
|
|
|
import Hasura.Server.Version
|
2022-11-22 04:36:35 +03:00
|
|
|
import Hasura.ShutdownLatch
|
2023-03-23 18:51:18 +03:00
|
|
|
import Hasura.Tracing (sampleAlways)
|
2023-02-20 16:43:34 +03:00
|
|
|
import System.Environment (getEnvironment, lookupEnv, unsetEnv)
|
2021-09-08 16:06:24 +03:00
|
|
|
import System.Exit qualified as Sys
|
|
|
|
import System.Metrics qualified as EKG
|
2023-05-22 11:01:18 +03:00
|
|
|
import System.Monitor.Heartbeat
|
2021-09-08 16:06:24 +03:00
|
|
|
import System.Posix.Signals qualified as Signals
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2023-03-30 17:30:19 +03:00
|
|
|
{-# ANN main ("HLINT: ignore avoid getEnvironment" :: String) #-}
|
2018-06-27 16:11:32 +03:00
|
|
|
main :: IO ()
|
2023-05-22 11:01:18 +03:00
|
|
|
main = maybeWithGhcDebug $ monitorHeartbeatMain $ do
|
2022-03-14 21:31:46 +03:00
|
|
|
catch
|
|
|
|
do
|
|
|
|
env <- Env.getEnvironment
|
2023-02-20 16:43:34 +03:00
|
|
|
clearEnvironment
|
|
|
|
args <- parseArgs env
|
2022-03-14 21:31:46 +03:00
|
|
|
runApp env args
|
|
|
|
(\(ExitException _code msg) -> BC.putStrLn msg >> Sys.exitFailure)
|
2023-02-20 16:43:34 +03:00
|
|
|
where
|
|
|
|
-- Since the handling of environment variables works differently between the
|
|
|
|
-- Cloud version and the OSSS version we clear the process environment to
|
|
|
|
-- avoid accidentally reading directly from the operating system environment
|
|
|
|
-- variables.
|
|
|
|
clearEnvironment :: IO ()
|
|
|
|
clearEnvironment = getEnvironment >>= traverse_ \(v, _) -> unsetEnv v
|
2018-07-27 12:34:50 +03:00
|
|
|
|
2022-07-01 06:39:39 +03:00
|
|
|
runApp :: Env.Environment -> HGEOptions (ServeOptions Hasura) -> IO ()
|
2023-03-23 18:51:18 +03:00
|
|
|
runApp env (HGEOptions rci metadataDbUrl hgeCmd) = do
|
2020-11-24 09:10:04 +03:00
|
|
|
initTime <- liftIO getCurrentTime
|
|
|
|
|
2021-10-13 19:38:56 +03:00
|
|
|
case hgeCmd of
|
2023-03-23 00:40:26 +03:00
|
|
|
HCServe serveOptions@ServeOptions {..} -> do
|
|
|
|
let poolSettings =
|
|
|
|
PostgresPoolSettings
|
2023-07-28 13:52:27 +03:00
|
|
|
{ ppsMaxConnections = Just $ PG.cpConns soConnParams,
|
|
|
|
ppsTotalMaxConnections = Nothing,
|
|
|
|
ppsIdleTimeout = Just $ PG.cpIdleTime soConnParams,
|
|
|
|
ppsRetries = _pciRetries rci <|> Just 1,
|
|
|
|
ppsPoolTimeout = PG.cpTimeout soConnParams,
|
|
|
|
ppsConnectionLifetime = PG.cpMbLifetime soConnParams
|
2023-03-23 00:40:26 +03:00
|
|
|
}
|
|
|
|
basicConnectionInfo <-
|
|
|
|
initBasicConnectionInfo
|
|
|
|
env
|
|
|
|
metadataDbUrl
|
|
|
|
rci
|
|
|
|
(Just poolSettings)
|
|
|
|
(PG.cpAllowPrepare soConnParams)
|
|
|
|
soTxIso
|
2021-08-06 00:07:17 +03:00
|
|
|
(ekgStore, serverMetrics) <- liftIO $ do
|
|
|
|
store <- EKG.newStore @AppMetricsSpec
|
2021-09-22 18:34:53 +03:00
|
|
|
void $ EKG.register (EKG.subset GcSubset store) EKG.registerGcMetrics
|
2020-11-12 12:25:48 +03:00
|
|
|
|
2020-09-08 21:13:35 +03:00
|
|
|
let getTimeMs :: IO Int64
|
|
|
|
getTimeMs = (round . (* 1000)) `fmap` getPOSIXTime
|
2021-09-22 18:34:53 +03:00
|
|
|
void $ EKG.register store $ EKG.registerCounter ServerTimestampMs () getTimeMs
|
2020-09-08 21:13:35 +03:00
|
|
|
|
2021-08-06 00:07:17 +03:00
|
|
|
serverMetrics <-
|
|
|
|
liftIO $ createServerMetrics $ EKG.subset ServerSubset store
|
|
|
|
|
|
|
|
pure (EKG.subset EKG.emptyOf store, serverMetrics)
|
2020-11-12 12:25:48 +03:00
|
|
|
|
2023-03-23 18:51:18 +03:00
|
|
|
prometheusMetrics <- makeDummyPrometheusMetrics
|
2022-07-24 00:18:01 +03:00
|
|
|
|
2023-03-27 13:25:55 +03:00
|
|
|
-- It'd be nice if we didn't have to call lowerManagedT twice here, but
|
|
|
|
-- there is a data dependency problem since the call to runAppM below
|
|
|
|
-- depends on appCtx.
|
|
|
|
runManagedT (initialiseAppEnv env basicConnectionInfo serveOptions Nothing serverMetrics prometheusMetrics sampleAlways) \(appInit, appEnv) -> do
|
2020-12-21 21:56:00 +03:00
|
|
|
-- Catches the SIGTERM signal and initiates a graceful shutdown.
|
|
|
|
-- Graceful shutdown for regular HTTP requests is already implemented in
|
|
|
|
-- Warp, and is triggered by invoking the 'closeSocket' callback.
|
|
|
|
-- We only catch the SIGTERM signal once, that is, if the user hits CTRL-C
|
|
|
|
-- once again, we terminate the process immediately.
|
2023-03-27 13:25:55 +03:00
|
|
|
void $ Signals.installHandler Signals.sigTERM (Signals.CatchOnce (shutdownGracefully $ appEnvShutdownLatch appEnv)) Nothing
|
|
|
|
void $ Signals.installHandler Signals.sigINT (Signals.CatchOnce (shutdownGracefully $ appEnvShutdownLatch appEnv)) Nothing
|
2020-12-28 15:56:00 +03:00
|
|
|
|
2023-02-24 21:09:36 +03:00
|
|
|
let Loggers _ logger _ = appEnvLoggers appEnv
|
2021-04-06 06:25:02 +03:00
|
|
|
|
2020-12-21 21:56:00 +03:00
|
|
|
_idleGCThread <-
|
2023-05-24 16:51:56 +03:00
|
|
|
C.forkImmortal "ourIdleGC" logger
|
|
|
|
$ GC.ourIdleGC logger (seconds 0.3) (seconds 10) (seconds 60)
|
2020-12-28 15:56:00 +03:00
|
|
|
|
2023-03-27 13:25:55 +03:00
|
|
|
runAppM appEnv do
|
|
|
|
appStateRef <- initialiseAppContext env serveOptions appInit
|
2023-05-24 16:51:56 +03:00
|
|
|
lowerManagedT
|
|
|
|
$ runHGEServer (const $ pure ()) appStateRef initTime Nothing OSSConsole ekgStore
|
2018-12-19 14:38:33 +03:00
|
|
|
HCExport -> do
|
2023-03-23 00:40:26 +03:00
|
|
|
metadataConnection <- initMetadataConnectionInfo env metadataDbUrl rci
|
|
|
|
res <- runTxWithMinimalPool metadataConnection fetchMetadataFromCatalog
|
2022-03-14 21:31:46 +03:00
|
|
|
either (throwErrJExit MetadataExportError) printJSON res
|
2018-12-19 14:38:33 +03:00
|
|
|
HCClean -> do
|
2023-03-23 00:40:26 +03:00
|
|
|
metadataConnection <- initMetadataConnectionInfo env metadataDbUrl rci
|
|
|
|
res <- runTxWithMinimalPool metadataConnection dropHdbCatalogSchema
|
2020-11-24 09:10:04 +03:00
|
|
|
let cleanSuccessMsg = "successfully cleaned graphql-engine related data"
|
2022-03-14 21:31:46 +03:00
|
|
|
either (throwErrJExit MetadataCleanError) (const $ liftIO $ putStrLn cleanSuccessMsg) res
|
2020-02-07 14:03:12 +03:00
|
|
|
HCDowngrade opts -> do
|
2023-07-28 13:52:27 +03:00
|
|
|
let poolSettings = setPostgresPoolSettings {ppsRetries = _pciRetries rci <|> Just 1}
|
2023-03-23 00:40:26 +03:00
|
|
|
BasicConnectionInfo {..} <- initBasicConnectionInfo env metadataDbUrl rci (Just poolSettings) False PG.ReadCommitted
|
|
|
|
res <- runTxWithMinimalPool bciMetadataConnInfo $ downgradeCatalog bciDefaultPostgres opts initTime
|
2022-03-14 21:31:46 +03:00
|
|
|
either (throwErrJExit DowngradeProcessError) (liftIO . print) res
|
2020-01-23 00:55:55 +03:00
|
|
|
HCVersion -> liftIO $ putStrLn $ "Hasura GraphQL Engine: " ++ convertText currentVersion
|
2019-11-26 15:14:21 +03:00
|
|
|
where
|
2020-12-21 21:56:00 +03:00
|
|
|
runTxWithMinimalPool connInfo tx = lowerManagedT $ do
|
2020-11-24 09:10:04 +03:00
|
|
|
minimalPool <- mkMinimalPool connInfo
|
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
|
|
|
liftIO $ runExceptT $ PG.runTx minimalPool (PG.ReadCommitted, Nothing) tx
|
2020-11-24 09:10:04 +03:00
|
|
|
|
|
|
|
mkMinimalPool connInfo = do
|
|
|
|
pgLogger <- _lsPgLogger <$> mkLoggers defaultEnabledEngineLogTypes LevelInfo
|
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
|
|
|
let connParams = PG.defaultConnParams {PG.cpConns = 1}
|
2023-07-20 14:18:18 +03:00
|
|
|
liftIO $ PG.initPGPool connInfo J.Null connParams pgLogger
|
2021-08-06 00:07:17 +03:00
|
|
|
|
|
|
|
-- | A specification of all EKG metrics tracked in `runApp`.
|
|
|
|
data
|
|
|
|
AppMetricsSpec ::
|
|
|
|
Symbol -> -- Metric name
|
|
|
|
EKG.MetricType -> -- Metric type, e.g. Counter, Gauge
|
|
|
|
Type -> -- Tag structure
|
|
|
|
Type
|
|
|
|
where
|
|
|
|
ServerSubset ::
|
|
|
|
ServerMetricsSpec name metricType tags ->
|
|
|
|
AppMetricsSpec name metricType tags
|
|
|
|
GcSubset ::
|
|
|
|
EKG.GcMetrics name metricType tags ->
|
|
|
|
AppMetricsSpec name metricType tags
|
|
|
|
ServerTimestampMs ::
|
|
|
|
AppMetricsSpec "ekg.server_timestamp_ms" 'EKG.CounterType ()
|
2023-02-09 01:41:09 +03:00
|
|
|
|
|
|
|
-- | 'withGhcDebug' but conditional on the environment variable
|
|
|
|
-- @HASURA_GHC_DEBUG=true@. When this is set a debug socket will be opened,
|
|
|
|
-- otherwise the server will start normally. This must only be called once and
|
|
|
|
-- it's argument should be the program's @main@
|
|
|
|
maybeWithGhcDebug :: IO a -> IO a
|
|
|
|
maybeWithGhcDebug theMain = do
|
|
|
|
lookupEnv "HASURA_GHC_DEBUG" >>= \case
|
|
|
|
Just "true" -> do
|
|
|
|
putStrLn "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
|
|
putStrLn "!!!!! Opening a ghc-debug socket !!!!!"
|
|
|
|
putStrLn "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
|
|
withGhcDebug theMain
|
|
|
|
_ -> theMain
|