server/gardening: refactor scMetadataResourceVersion in SchemaCache

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8377
GitOrigin-RevId: 999a5112f8940b267c2765f4bed39bf6151f18f0
This commit is contained in:
Rakesh Emmadi 2023-03-28 18:56:08 +05:30 committed by hasura-bot
parent bc7225fd1a
commit bfca9fd986
14 changed files with 103 additions and 103 deletions

View File

@ -366,7 +366,7 @@ resolvePostgresConnInfo env dbUrlConf (fromMaybe 1 -> retries) = do
-- but that aren't needed in the rest of the application. -- but that aren't needed in the rest of the application.
data AppInit = AppInit data AppInit = AppInit
{ aiTLSAllowListRef :: TLSAllowListRef Hasura, { aiTLSAllowListRef :: TLSAllowListRef Hasura,
aiMetadata :: Metadata aiMetadataWithResourceVersion :: MetadataWithResourceVersion
} }
-- | Initializes or migrates the catalog and creates the 'AppEnv' required to -- | Initializes or migrates the catalog and creates the 'AppEnv' required to
@ -416,7 +416,7 @@ initialiseAppEnv env BasicConnectionInfo {..} serveOptions@ServeOptions {..} liv
(liftIO . PG.destroyPGPool) (liftIO . PG.destroyPGPool)
-- Migrate the catalog and fetch the metdata. -- Migrate the catalog and fetch the metdata.
metadata <- metadataWithVersion <-
lift $ lift $
flip onException (flushLogger loggerCtx) $ flip onException (flushLogger loggerCtx) $
migrateCatalogAndFetchMetadata migrateCatalogAndFetchMetadata
@ -427,6 +427,7 @@ initialiseAppEnv env BasicConnectionInfo {..} serveOptions@ServeOptions {..} liv
soExtensionsSchema soExtensionsSchema
-- Create the TLSAllowListRef and the HTTP Manager. -- Create the TLSAllowListRef and the HTTP Manager.
let metadata = _mwrvMetadata metadataWithVersion
tlsAllowListRef <- liftIO $ createTLSAllowListRef $ networkTlsAllowlist $ _metaNetwork metadata tlsAllowListRef <- liftIO $ createTLSAllowListRef $ networkTlsAllowlist $ _metaNetwork metadata
httpManager <- liftIO $ mkHttpManager (readTLSAllowList tlsAllowListRef) mempty httpManager <- liftIO $ mkHttpManager (readTLSAllowList tlsAllowListRef) mempty
@ -454,7 +455,7 @@ initialiseAppEnv env BasicConnectionInfo {..} serveOptions@ServeOptions {..} liv
pure pure
( AppInit ( AppInit
{ aiTLSAllowListRef = tlsAllowListRef, { aiTLSAllowListRef = tlsAllowListRef,
aiMetadata = metadata aiMetadataWithResourceVersion = metadataWithVersion
}, },
AppEnv AppEnv
{ appEnvPort = soPort, { appEnvPort = soPort,
@ -524,7 +525,7 @@ initialiseAppContext env serveOptions@ServeOptions {..} AppInit {..} = do
serverConfigCtx serverConfigCtx
(mkPgSourceResolver pgLogger) (mkPgSourceResolver pgLogger)
mkMSSQLSourceResolver mkMSSQLSourceResolver
aiMetadata aiMetadataWithResourceVersion
appEnvManager appEnvManager
-- Build the RebuildableAppContext. -- Build the RebuildableAppContext.
@ -546,7 +547,7 @@ migrateCatalogAndFetchMetadata ::
Maybe (SourceConnConfiguration ('Postgres 'Vanilla)) -> Maybe (SourceConnConfiguration ('Postgres 'Vanilla)) ->
MaintenanceMode () -> MaintenanceMode () ->
ExtensionsSchema -> ExtensionsSchema ->
m Metadata m MetadataWithResourceVersion
migrateCatalogAndFetchMetadata migrateCatalogAndFetchMetadata
logger logger
pool pool
@ -575,9 +576,9 @@ migrateCatalogAndFetchMetadata
slInfo = A.toJSON err slInfo = A.toJSON err
} }
liftIO (throwErrJExit DatabaseMigrationError err) liftIO (throwErrJExit DatabaseMigrationError err)
Right (migrationResult, metadata) -> do Right (migrationResult, metadataWithVersion) -> do
unLogger logger migrationResult unLogger logger migrationResult
pure metadata pure metadataWithVersion
-- | Build the original 'RebuildableSchemaCache'. -- | Build the original 'RebuildableSchemaCache'.
-- --
@ -591,7 +592,7 @@ buildFirstSchemaCache ::
ServerConfigCtx -> ServerConfigCtx ->
SourceResolver ('Postgres 'Vanilla) -> SourceResolver ('Postgres 'Vanilla) ->
SourceResolver ('MSSQL) -> SourceResolver ('MSSQL) ->
Metadata -> MetadataWithResourceVersion ->
HTTP.Manager -> HTTP.Manager ->
m RebuildableSchemaCache m RebuildableSchemaCache
buildFirstSchemaCache buildFirstSchemaCache
@ -600,14 +601,14 @@ buildFirstSchemaCache
serverConfigCtx serverConfigCtx
pgSourceResolver pgSourceResolver
mssqlSourceResolver mssqlSourceResolver
metadata metadataWithVersion
httpManager = do httpManager = do
let cacheBuildParams = CacheBuildParams httpManager pgSourceResolver mssqlSourceResolver let cacheBuildParams = CacheBuildParams httpManager pgSourceResolver mssqlSourceResolver
buildReason = CatalogSync buildReason = CatalogSync
result <- result <-
runExceptT $ runExceptT $
runCacheBuild cacheBuildParams $ runCacheBuild cacheBuildParams $
buildRebuildableSchemaCacheWithReason buildReason logger env metadata serverConfigCtx buildRebuildableSchemaCacheWithReason buildReason logger env metadataWithVersion serverConfigCtx
result `onLeft` \err -> do result `onLeft` \err -> do
-- TODO: we used to bundle the first schema cache build with the catalog -- TODO: we used to bundle the first schema cache build with the catalog
-- migration, using the same error handler for both, meaning that an -- migration, using the same error handler for both, meaning that an

View File

@ -86,7 +86,7 @@ TODO: Reference to open issue or rfc?
class Monad m => MonadMetadataStorage m where class Monad m => MonadMetadataStorage m where
-- Metadata -- Metadata
fetchMetadataResourceVersion :: m (Either QErr MetadataResourceVersion) fetchMetadataResourceVersion :: m (Either QErr MetadataResourceVersion)
fetchMetadata :: m (Either QErr (Metadata, MetadataResourceVersion)) fetchMetadata :: m (Either QErr MetadataWithResourceVersion)
fetchMetadataNotifications :: MetadataResourceVersion -> InstanceId -> m (Either QErr [(MetadataResourceVersion, CacheInvalidations)]) fetchMetadataNotifications :: MetadataResourceVersion -> InstanceId -> m (Either QErr [(MetadataResourceVersion, CacheInvalidations)])
setMetadata :: MetadataResourceVersion -> Metadata -> m (Either QErr MetadataResourceVersion) setMetadata :: MetadataResourceVersion -> Metadata -> m (Either QErr MetadataResourceVersion)
notifySchemaCacheSync :: MetadataResourceVersion -> InstanceId -> CacheInvalidations -> m (Either QErr ()) notifySchemaCacheSync :: MetadataResourceVersion -> InstanceId -> CacheInvalidations -> m (Either QErr ())

View File

@ -141,7 +141,7 @@ action/function.
buildRebuildableSchemaCache :: buildRebuildableSchemaCache ::
Logger Hasura -> Logger Hasura ->
Env.Environment -> Env.Environment ->
Metadata -> MetadataWithResourceVersion ->
ServerConfigCtx -> ServerConfigCtx ->
CacheBuild RebuildableSchemaCache CacheBuild RebuildableSchemaCache
buildRebuildableSchemaCache = buildRebuildableSchemaCache =
@ -151,13 +151,13 @@ buildRebuildableSchemaCacheWithReason ::
BuildReason -> BuildReason ->
Logger Hasura -> Logger Hasura ->
Env.Environment -> Env.Environment ->
Metadata -> MetadataWithResourceVersion ->
ServerConfigCtx -> ServerConfigCtx ->
CacheBuild RebuildableSchemaCache CacheBuild RebuildableSchemaCache
buildRebuildableSchemaCacheWithReason reason logger env metadata serverConfigCtx = do buildRebuildableSchemaCacheWithReason reason logger env metadataWithVersion serverConfigCtx = do
result <- result <-
flip runReaderT reason $ flip runReaderT reason $
Inc.build (buildSchemaCacheRule logger env) (metadata, serverConfigCtx, initialInvalidationKeys, Nothing) Inc.build (buildSchemaCacheRule logger env) (metadataWithVersion, serverConfigCtx, initialInvalidationKeys, Nothing)
pure $ RebuildableSchemaCache (Inc.result result) initialInvalidationKeys (Inc.rebuildRule result) pure $ RebuildableSchemaCache (Inc.result result) initialInvalidationKeys (Inc.rebuildRule result)
@ -230,13 +230,13 @@ instance
buildSchemaCacheWithOptions buildReason invalidations metadata = CacheRWT do buildSchemaCacheWithOptions buildReason invalidations metadata = CacheRWT do
serverConfigCtx <- ask serverConfigCtx <- ask
(RebuildableSchemaCache lastBuiltSC invalidationKeys rule, oldInvalidations) <- get (RebuildableSchemaCache lastBuiltSC invalidationKeys rule, oldInvalidations) <- get
let metadataVersion = scMetadataResourceVersion lastBuiltSC let metadataWithVersion = MetadataWithResourceVersion metadata $ scMetadataResourceVersion lastBuiltSC
newInvalidationKeys = invalidateKeys invalidations invalidationKeys newInvalidationKeys = invalidateKeys invalidations invalidationKeys
result <- result <-
runCacheBuildM $ runCacheBuildM $
flip runReaderT buildReason $ flip runReaderT buildReason $
Inc.build rule (metadata, serverConfigCtx, newInvalidationKeys, Nothing) Inc.build rule (metadataWithVersion, serverConfigCtx, newInvalidationKeys, Nothing)
let schemaCache = (Inc.result result) {scMetadataResourceVersion = metadataVersion} let schemaCache = Inc.result result
prunedInvalidationKeys = pruneInvalidationKeys schemaCache newInvalidationKeys prunedInvalidationKeys = pruneInvalidationKeys schemaCache newInvalidationKeys
!newCache = RebuildableSchemaCache schemaCache prunedInvalidationKeys (Inc.rebuildRule result) !newCache = RebuildableSchemaCache schemaCache prunedInvalidationKeys (Inc.rebuildRule result)
!newInvalidations = oldInvalidations <> invalidations !newInvalidations = oldInvalidations <> invalidations
@ -254,7 +254,7 @@ instance
( rebuildableSchemaCache ( rebuildableSchemaCache
{ lastBuiltSchemaCache = { lastBuiltSchemaCache =
(lastBuiltSchemaCache rebuildableSchemaCache) (lastBuiltSchemaCache rebuildableSchemaCache)
{ scMetadataResourceVersion = Just resourceVersion { scMetadataResourceVersion = resourceVersion
} }
}, },
invalidations invalidations
@ -334,8 +334,8 @@ buildSchemaCacheRule ::
) => ) =>
Logger Hasura -> Logger Hasura ->
Env.Environment -> Env.Environment ->
(Metadata, ServerConfigCtx, InvalidationKeys, Maybe StoredIntrospection) `arr` SchemaCache (MetadataWithResourceVersion, ServerConfigCtx, InvalidationKeys, Maybe StoredIntrospection) `arr` SchemaCache
buildSchemaCacheRule logger env = proc (metadataNoDefaults, serverConfigCtx, invalidationKeys, storedIntrospection) -> do buildSchemaCacheRule logger env = proc (MetadataWithResourceVersion metadataNoDefaults resourceVersion, serverConfigCtx, invalidationKeys, storedIntrospection) -> do
invalidationKeysDep <- Inc.newDependency -< invalidationKeys invalidationKeysDep <- Inc.newDependency -< invalidationKeys
let metadataDefaults = _sccMetadataDefaults serverConfigCtx let metadataDefaults = _sccMetadataDefaults serverConfigCtx
metadata@Metadata {..} = overrideMetadataDefaults metadataNoDefaults metadataDefaults metadata@Metadata {..} = overrideMetadataDefaults metadataNoDefaults metadataDefaults
@ -426,7 +426,7 @@ buildSchemaCacheRule logger env = proc (metadataNoDefaults, serverConfigCtx, inv
<> inconsistentQueryCollections, <> inconsistentQueryCollections,
scApiLimits = _metaApiLimits, scApiLimits = _metaApiLimits,
scMetricsConfig = _metaMetricsConfig, scMetricsConfig = _metaMetricsConfig,
scMetadataResourceVersion = Nothing, scMetadataResourceVersion = resourceVersion,
scSetGraphqlIntrospectionOptions = _metaSetGraphqlIntrospectionOptions, scSetGraphqlIntrospectionOptions = _metaSetGraphqlIntrospectionOptions,
scTlsAllowlist = networkTlsAllowlist _metaNetwork, scTlsAllowlist = networkTlsAllowlist _metaNetwork,
scQueryCollections = _metaQueryCollections, scQueryCollections = _metaQueryCollections,

View File

@ -309,7 +309,7 @@ runCacheBuildM m = do
data RebuildableSchemaCache = RebuildableSchemaCache data RebuildableSchemaCache = RebuildableSchemaCache
{ lastBuiltSchemaCache :: SchemaCache, { lastBuiltSchemaCache :: SchemaCache,
_rscInvalidationMap :: InvalidationKeys, _rscInvalidationMap :: InvalidationKeys,
_rscRebuild :: Inc.Rule (ReaderT BuildReason CacheBuild) (Metadata, ServerConfigCtx, InvalidationKeys, Maybe StoredIntrospection) SchemaCache _rscRebuild :: Inc.Rule (ReaderT BuildReason CacheBuild) (MetadataWithResourceVersion, ServerConfigCtx, InvalidationKeys, Maybe StoredIntrospection) SchemaCache
} }
bindErrorA :: bindErrorA ::

View File

@ -20,6 +20,7 @@ import Hasura.Prelude
import Hasura.RQL.Types.Metadata import Hasura.RQL.Types.Metadata
import Hasura.RQL.Types.SchemaCache import Hasura.RQL.Types.SchemaCache
( MetadataResourceVersion (..), ( MetadataResourceVersion (..),
MetadataWithResourceVersion (..),
initialResourceVersion, initialResourceVersion,
) )
import Hasura.RQL.Types.SchemaCache.Build (CacheInvalidations) import Hasura.RQL.Types.SchemaCache.Build (CacheInvalidations)
@ -40,7 +41,7 @@ fetchMetadataFromCatalog = do
[Identity (PG.ViaJSON metadata)] -> pure metadata [Identity (PG.ViaJSON metadata)] -> pure metadata
_ -> throw500 "multiple rows in hdb_metadata table" _ -> throw500 "multiple rows in hdb_metadata table"
fetchMetadataAndResourceVersionFromCatalog :: PG.TxE QErr (Metadata, MetadataResourceVersion) fetchMetadataAndResourceVersionFromCatalog :: PG.TxE QErr MetadataWithResourceVersion
fetchMetadataAndResourceVersionFromCatalog = do fetchMetadataAndResourceVersionFromCatalog = do
rows <- rows <-
PG.withQE PG.withQE
@ -50,7 +51,7 @@ fetchMetadataAndResourceVersionFromCatalog = do
|] |]
() ()
True True
case rows of uncurry MetadataWithResourceVersion <$> case rows of
[] -> pure (emptyMetadata, initialResourceVersion) [] -> pure (emptyMetadata, initialResourceVersion)
[(PG.ViaJSON metadata, resourceVersion)] -> pure (metadata, MetadataResourceVersion resourceVersion) [(PG.ViaJSON metadata, resourceVersion)] -> pure (metadata, MetadataResourceVersion resourceVersion)
_ -> throw500 "multiple rows in hdb_metadata table" _ -> throw500 "multiple rows in hdb_metadata table"

View File

@ -102,6 +102,7 @@ module Hasura.RQL.Types.SchemaCache
MetadataResourceVersion (..), MetadataResourceVersion (..),
showMetadataResourceVersion, showMetadataResourceVersion,
initialResourceVersion, initialResourceVersion,
MetadataWithResourceVersion (..),
getLogicalModelBoolExpDeps, getLogicalModelBoolExpDeps,
getBoolExpDeps, getBoolExpDeps,
InlinedAllowlist, InlinedAllowlist,
@ -177,6 +178,12 @@ initialResourceVersion = MetadataResourceVersion 0
showMetadataResourceVersion :: MetadataResourceVersion -> Text showMetadataResourceVersion :: MetadataResourceVersion -> Text
showMetadataResourceVersion (MetadataResourceVersion version) = tshow version showMetadataResourceVersion (MetadataResourceVersion version) = tshow version
data MetadataWithResourceVersion = MetadataWithResourceVersion
{ _mwrvMetadata :: Metadata,
_mwrvResourceVersion :: MetadataResourceVersion
}
deriving (Eq)
mkParentDep :: mkParentDep ::
forall b. forall b.
Backend b => Backend b =>
@ -553,7 +560,7 @@ data SchemaCache = SchemaCache
scEndpoints :: EndpointTrie GQLQueryWithText, scEndpoints :: EndpointTrie GQLQueryWithText,
scApiLimits :: ApiLimit, scApiLimits :: ApiLimit,
scMetricsConfig :: MetricsConfig, scMetricsConfig :: MetricsConfig,
scMetadataResourceVersion :: Maybe MetadataResourceVersion, scMetadataResourceVersion :: MetadataResourceVersion,
scSetGraphqlIntrospectionOptions :: SetGraphqlIntrospectionOptions, scSetGraphqlIntrospectionOptions :: SetGraphqlIntrospectionOptions,
scTlsAllowlist :: [TlsAllow], scTlsAllowlist :: [TlsAllow],
scQueryCollections :: QueryCollections, scQueryCollections :: QueryCollections,

View File

@ -397,7 +397,7 @@ runMetadataQuery ::
runMetadataQuery appContext schemaCache RQLMetadata {..} = do runMetadataQuery appContext schemaCache RQLMetadata {..} = do
appEnv@AppEnv {..} <- askAppEnv appEnv@AppEnv {..} <- askAppEnv
let logger = _lsLogger appEnvLoggers let logger = _lsLogger appEnvLoggers
(metadata, currentResourceVersion) <- Tracing.newSpan "fetchMetadata" $ liftEitherM fetchMetadata MetadataWithResourceVersion metadata currentResourceVersion <- Tracing.newSpan "fetchMetadata" $ liftEitherM fetchMetadata
let exportsMetadata = \case let exportsMetadata = \case
RMV1 (RMExportMetadata _) -> True RMV1 (RMExportMetadata _) -> True
RMV2 (RMV2ExportMetadata _) -> True RMV2 (RMV2ExportMetadata _) -> True

View File

@ -51,6 +51,7 @@ import Hasura.RQL.Types.Metadata
import Hasura.RQL.Types.Permission import Hasura.RQL.Types.Permission
import Hasura.RQL.Types.QueryCollection import Hasura.RQL.Types.QueryCollection
import Hasura.RQL.Types.ScheduledTrigger import Hasura.RQL.Types.ScheduledTrigger
import Hasura.RQL.Types.SchemaCache
import Hasura.RQL.Types.SchemaCache.Build import Hasura.RQL.Types.SchemaCache.Build
import Hasura.RQL.Types.Source import Hasura.RQL.Types.Source
import Hasura.RemoteSchema.MetadataAPI import Hasura.RemoteSchema.MetadataAPI
@ -207,7 +208,7 @@ runQuery appContext sc query = do
else acMetadataDefaults appContext else acMetadataDefaults appContext
serverConfigCtx = buildServerConfigCtx appEnv appContext serverConfigCtx = buildServerConfigCtx appEnv appContext
(metadata, currentResourceVersion) <- liftEitherM fetchMetadata MetadataWithResourceVersion metadata currentResourceVersion <- liftEitherM fetchMetadata
((result, updatedMetadata), updatedCache, invalidations) <- ((result, updatedMetadata), updatedCache, invalidations) <-
runQueryM (acEnvironment appContext) query runQueryM (acEnvironment appContext) query
-- TODO: remove this straight runReaderT that provides no actual new info -- TODO: remove this straight runReaderT that provides no actual new info

View File

@ -42,6 +42,7 @@ import Hasura.RQL.DML.Types
) )
import Hasura.RQL.DML.Update import Hasura.RQL.DML.Update
import Hasura.RQL.Types.Metadata import Hasura.RQL.Types.Metadata
import Hasura.RQL.Types.SchemaCache (MetadataWithResourceVersion (MetadataWithResourceVersion))
import Hasura.RQL.Types.SchemaCache.Build import Hasura.RQL.Types.SchemaCache.Build
import Hasura.RQL.Types.Source import Hasura.RQL.Types.Source
import Hasura.SQL.Backend import Hasura.SQL.Backend
@ -123,7 +124,7 @@ runQuery appContext schemaCache rqlQuery = do
throw400 NotSupported "Cannot run write queries when read-only mode is enabled" throw400 NotSupported "Cannot run write queries when read-only mode is enabled"
let serverConfigCtx = buildServerConfigCtx appEnv appContext let serverConfigCtx = buildServerConfigCtx appEnv appContext
(metadata, currentResourceVersion) <- Tracing.newSpan "fetchMetadata" $ liftEitherM fetchMetadata MetadataWithResourceVersion metadata currentResourceVersion <- Tracing.newSpan "fetchMetadata" $ liftEitherM fetchMetadata
((result, updatedMetadata), updatedCache, invalidations) <- ((result, updatedMetadata), updatedCache, invalidations) <-
runQueryM (acEnvironment appContext) rqlQuery runQueryM (acEnvironment appContext) rqlQuery
-- We can use defaults here unconditionally, since there is no MD export function in V2Query -- We can use defaults here unconditionally, since there is no MD export function in V2Query

View File

@ -203,4 +203,4 @@ logInconsistentMetadata logger objs =
updateMetadataVersionGauge :: MonadIO m => Gauge -> RebuildableSchemaCache -> m () updateMetadataVersionGauge :: MonadIO m => Gauge -> RebuildableSchemaCache -> m ()
updateMetadataVersionGauge metadataVersionGauge schemaCache = do updateMetadataVersionGauge metadataVersionGauge schemaCache = do
let metadataVersion = scMetadataResourceVersion . lastBuiltSchemaCache $ schemaCache let metadataVersion = scMetadataResourceVersion . lastBuiltSchemaCache $ schemaCache
liftIO $ traverse_ (Gauge.set metadataVersionGauge . getMetadataResourceVersion) metadataVersion liftIO $ Gauge.set metadataVersionGauge $ getMetadataResourceVersion metadataVersion

View File

@ -50,6 +50,7 @@ import Hasura.RQL.Types.CustomTypes
import Hasura.RQL.Types.Metadata import Hasura.RQL.Types.Metadata
import Hasura.RQL.Types.Network import Hasura.RQL.Types.Network
import Hasura.RQL.Types.OpenTelemetry (emptyOpenTelemetryConfig) import Hasura.RQL.Types.OpenTelemetry (emptyOpenTelemetryConfig)
import Hasura.RQL.Types.SchemaCache
import Hasura.RQL.Types.SourceCustomization import Hasura.RQL.Types.SourceCustomization
import Hasura.SQL.AnyBackend qualified as AB import Hasura.SQL.AnyBackend qualified as AB
import Hasura.SQL.Backend import Hasura.SQL.Backend
@ -112,7 +113,7 @@ migrateCatalog ::
ExtensionsSchema -> ExtensionsSchema ->
MaintenanceMode () -> MaintenanceMode () ->
UTCTime -> UTCTime ->
m (MigrationResult, Metadata) m (MigrationResult, MetadataWithResourceVersion)
migrateCatalog maybeDefaultSourceConfig extensionsSchema maintenanceMode migrationTime = do migrateCatalog maybeDefaultSourceConfig extensionsSchema maintenanceMode migrationTime = do
catalogSchemaExists <- doesSchemaExist (SchemaName "hdb_catalog") catalogSchemaExists <- doesSchemaExist (SchemaName "hdb_catalog")
versionTableExists <- doesTableExist (SchemaName "hdb_catalog") (TableName "hdb_version") versionTableExists <- doesTableExist (SchemaName "hdb_catalog") (TableName "hdb_version")
@ -135,8 +136,8 @@ migrateCatalog maybeDefaultSourceConfig extensionsSchema maintenanceMode migrati
True -> case versionTableExists of True -> case versionTableExists of
False -> initialize False False -> initialize False
True -> migrateFrom =<< liftTx getCatalogVersion True -> migrateFrom =<< liftTx getCatalogVersion
metadata <- liftTx fetchMetadataFromCatalog metadataWithVersion <- liftTx fetchMetadataAndResourceVersionFromCatalog
pure (migrationResult, metadata) pure (migrationResult, metadataWithVersion)
where where
-- initializes the catalog, creating the schema if necessary -- initializes the catalog, creating the schema if necessary
initialize :: Bool -> m MigrationResult initialize :: Bool -> m MigrationResult

View File

@ -287,78 +287,65 @@ refreshSchemaCache
(msg, cache, _) <- (msg, cache, _) <-
runCacheRWT serverConfigCtx rebuildableCache $ do runCacheRWT serverConfigCtx rebuildableCache $ do
schemaCache <- askSchemaCache schemaCache <- askSchemaCache
case scMetadataResourceVersion schemaCache of let engineResourceVersion = scMetadataResourceVersion schemaCache
-- While starting up, the metadata resource version is set to nothing, so we want to set the version unless (engineResourceVersion == resourceVersion) $ do
-- without fetching the database metadata (as we have already fetched it during the startup, so, we logInfo logger threadType $
-- skip fetching it twice) String $
Nothing -> do T.unwords
setMetadataResourceVersionInSchemaCache resourceVersion [ "Received metadata resource version:",
logInfo logger threadType $ showMetadataResourceVersion resourceVersion <> ",",
String $ "different from the current engine resource version:",
T.unwords showMetadataResourceVersion engineResourceVersion <> ".",
[ "Received metadata resource version:", "Trying to update the schema cache."
showMetadataResourceVersion resourceVersion, ]
"as an initial version. Not updating the schema cache."
] MetadataWithResourceVersion metadata latestResourceVersion <- liftEitherM fetchMetadata
Just engineResourceVersion ->
unless (engineResourceVersion == resourceVersion) $ do logInfo logger threadType $
String $
T.unwords
[ "Fetched metadata with resource version:",
showMetadataResourceVersion latestResourceVersion
]
notifications <- liftEitherM $ fetchMetadataNotifications engineResourceVersion appEnvInstanceId
case notifications of
[] -> do
logInfo logger threadType $ logInfo logger threadType $
String $ String $
T.unwords T.unwords
[ "Received metadata resource version:", [ "Fetched metadata notifications and received no notifications. Not updating the schema cache.",
showMetadataResourceVersion resourceVersion <> ",", "Only setting resource version:",
"different from the current engine resource version:", showMetadataResourceVersion latestResourceVersion,
showMetadataResourceVersion engineResourceVersion <> ".", "in schema cache"
"Trying to update the schema cache."
] ]
setMetadataResourceVersionInSchemaCache latestResourceVersion
_ -> do
logInfo logger threadType $
String "Fetched metadata notifications and received some notifications. Updating the schema cache."
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.
(metadata, latestResourceVersion) <- liftEitherM fetchMetadata CacheInvalidations
{ ciMetadata = True,
ciRemoteSchemas = HS.fromList $ getAllRemoteSchemas schemaCache,
ciSources = HS.fromList $ HM.keys $ scSources schemaCache,
ciDataConnectors =
maybe mempty (HS.fromList . HM.keys . unBackendInfoWrapper) $
BackendMap.lookup @'DataConnector $
scBackendCache schemaCache
}
buildSchemaCacheWithOptions CatalogSync cacheInvalidations metadata
setMetadataResourceVersionInSchemaCache latestResourceVersion
logInfo logger threadType $ logInfo logger threadType $
String $ String $
T.unwords "Schema cache updated with resource version: " <> showMetadataResourceVersion latestResourceVersion
[ "Fetched metadata with resource version:",
showMetadataResourceVersion latestResourceVersion
]
notifications <- liftEitherM $ fetchMetadataNotifications engineResourceVersion appEnvInstanceId
case notifications of
[] -> do
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"
]
setMetadataResourceVersionInSchemaCache latestResourceVersion
_ -> do
logInfo logger threadType $
String "Fetched metadata notifications and received some notifications. Updating the schema cache."
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,
ciSources = HS.fromList $ HM.keys $ scSources schemaCache,
ciDataConnectors =
maybe mempty (HS.fromList . HM.keys . unBackendInfoWrapper) $
BackendMap.lookup @'DataConnector $
scBackendCache schemaCache
}
buildSchemaCacheWithOptions CatalogSync cacheInvalidations metadata
setMetadataResourceVersionInSchemaCache latestResourceVersion
logInfo logger threadType $
String $
"Schema cache updated with resource version: " <> showMetadataResourceVersion latestResourceVersion
pure (msg, cache) pure (msg, cache)
onLeft respErr (logError logger threadType . TEQueryError) onLeft respErr (logError logger threadType . TEQueryError)

View File

@ -34,6 +34,7 @@ import Hasura.RQL.DDL.Schema.Cache.Common
import Hasura.RQL.Types.Common import Hasura.RQL.Types.Common
import Hasura.RQL.Types.Metadata (emptyMetadataDefaults) import Hasura.RQL.Types.Metadata (emptyMetadataDefaults)
import Hasura.RQL.Types.ResizePool import Hasura.RQL.Types.ResizePool
import Hasura.RQL.Types.SchemaCache
import Hasura.RQL.Types.SchemaCache.Build import Hasura.RQL.Types.SchemaCache.Build
import Hasura.Server.Init import Hasura.Server.Init
import Hasura.Server.Init.FeatureFlag as FF import Hasura.Server.Init.FeatureFlag as FF
@ -140,12 +141,12 @@ main = do
-- why are we building the schema cache here? it's already built in initialiseContext -- why are we building the schema cache here? it's already built in initialiseContext
(metadata, schemaCache) <- run do (metadata, schemaCache) <- run do
metadata <- metadataWithVersion <-
snd snd
<$> (liftEitherM . runExceptT . _pecRunTx pgContext (PGExecCtxInfo (Tx PG.ReadWrite Nothing) InternalRawQuery)) <$> (liftEitherM . runExceptT . _pecRunTx pgContext (PGExecCtxInfo (Tx PG.ReadWrite Nothing) InternalRawQuery))
(migrateCatalog (Just sourceConfig) defaultPostgresExtensionsSchema maintenanceMode =<< liftIO getCurrentTime) (migrateCatalog (Just sourceConfig) defaultPostgresExtensionsSchema maintenanceMode =<< liftIO getCurrentTime)
schemaCache <- runCacheBuild cacheBuildParams $ buildRebuildableSchemaCache logger envMap metadata serverConfigCtx schemaCache <- runCacheBuild cacheBuildParams $ buildRebuildableSchemaCache logger envMap metadataWithVersion serverConfigCtx
pure (metadata, schemaCache) pure (_mwrvMetadata metadataWithVersion, schemaCache)
cacheRef <- newMVar schemaCache cacheRef <- newMVar schemaCache
pure $ NT (run . flip MigrateSuite.runCacheRefT (serverConfigCtx, cacheRef) . fmap fst . runMetadataT metadata emptyMetadataDefaults) pure $ NT (run . flip MigrateSuite.runCacheRefT (serverConfigCtx, cacheRef) . fmap fst . runMetadataT metadata emptyMetadataDefaults)

View File

@ -129,8 +129,8 @@ suite srcConfig pgExecCtx pgConnInfo = do
migrateCatalogAndBuildCache env time = do migrateCatalogAndBuildCache env time = do
serverConfigCtx <- askServerConfigCtx serverConfigCtx <- askServerConfigCtx
(migrationResult, metadata) <- runTx' pgExecCtx $ migrateCatalog (Just srcConfig) (ExtensionsSchema "public") MaintenanceModeDisabled time (migrationResult, metadataWithVersion) <- runTx' pgExecCtx $ migrateCatalog (Just srcConfig) (ExtensionsSchema "public") MaintenanceModeDisabled time
(,migrationResult) <$> runCacheBuildM (buildRebuildableSchemaCache logger env metadata serverConfigCtx) (,migrationResult) <$> runCacheBuildM (buildRebuildableSchemaCache logger env metadataWithVersion serverConfigCtx)
dropAndInit env time = lift do dropAndInit env time = lift do
scVar <- asks snd scVar <- asks snd