Skip to content

Commit e6ca36d

Browse files
rakeshkkyhasura-bot
authored andcommitted
server: insert updated metadata and schema sync notifications in a transaction
PR-URL: hasura/graphql-engine-mono#10558 GitOrigin-RevId: e3bb0ef2378162a5fdccc7131a584f4200404afd
1 parent 8a16671 commit e6ca36d

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

server/src-lib/Hasura/App.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,11 +812,16 @@ instance MonadMetadataStorage AppM where
812812
fetchMetadataResourceVersion = runInSeparateTx fetchMetadataResourceVersionFromCatalog
813813
fetchMetadata = runInSeparateTx fetchMetadataAndResourceVersionFromCatalog
814814
fetchMetadataNotifications a b = runInSeparateTx $ fetchMetadataNotificationsFromCatalog a b
815-
setMetadata r = runInSeparateTx . setMetadataInCatalog r
816-
notifySchemaCacheSync a b c = runInSeparateTx $ notifySchemaCacheSyncTx a b c
815+
817816
getCatalogState = runInSeparateTx getCatalogStateTx
818817
setCatalogState a b = runInSeparateTx $ setCatalogStateTx a b
819818

819+
updateMetadataAndNotifySchemaSync instanceId resourceVersion metadata cacheInvalidations =
820+
runInSeparateTx $ do
821+
newResourceVersion <- setMetadataInCatalog resourceVersion metadata
822+
notifySchemaCacheSyncTx newResourceVersion instanceId cacheInvalidations
823+
pure newResourceVersion
824+
820825
-- stored source introspection is not available in this distribution
821826
fetchSourceIntrospection _ = pure $ Right Nothing
822827
storeSourceIntrospection _ _ = pure $ Right ()

server/src-lib/Hasura/Metadata/Class.hs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,17 @@ class (Monad m) => MonadMetadataStorage m where
9898
fetchMetadataResourceVersion :: m (Either QErr MetadataResourceVersion)
9999
fetchMetadata :: m (Either QErr MetadataWithResourceVersion)
100100
fetchMetadataNotifications :: MetadataResourceVersion -> InstanceId -> m (Either QErr [(MetadataResourceVersion, CacheInvalidations)])
101-
setMetadata :: MetadataResourceVersion -> Metadata -> m (Either QErr MetadataResourceVersion)
102-
notifySchemaCacheSync :: MetadataResourceVersion -> InstanceId -> CacheInvalidations -> m (Either QErr ())
101+
103102
getCatalogState :: m (Either QErr CatalogState)
104103

104+
-- This function is used to update the metadata in the metadata storage with schema sync notifications.
105+
updateMetadataAndNotifySchemaSync ::
106+
InstanceId ->
107+
MetadataResourceVersion ->
108+
Metadata ->
109+
CacheInvalidations ->
110+
m (Either QErr MetadataResourceVersion)
111+
105112
-- the `setCatalogState` function is used by the console and CLI to store its state
106113
-- it is disabled when maintenance mode is on
107114
setCatalogState :: CatalogStateType -> Value -> m (Either QErr ())
@@ -157,11 +164,12 @@ instance (MonadMetadataStorage m, MonadTrans t, Monad (t m)) => MonadMetadataSto
157164
fetchMetadataResourceVersion = lift fetchMetadataResourceVersion
158165
fetchMetadata = lift fetchMetadata
159166
fetchMetadataNotifications a b = lift $ fetchMetadataNotifications a b
160-
setMetadata r = lift . setMetadata r
161-
notifySchemaCacheSync a b c = lift $ notifySchemaCacheSync a b c
167+
162168
getCatalogState = lift getCatalogState
163169
setCatalogState a b = lift $ setCatalogState a b
164170

171+
updateMetadataAndNotifySchemaSync a b c d = lift $ updateMetadataAndNotifySchemaSync a b c d
172+
165173
fetchSourceIntrospection = lift . fetchSourceIntrospection
166174
storeSourceIntrospection a b = lift $ storeSourceIntrospection a b
167175

server/src-lib/Hasura/Server/API/Metadata.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,22 @@ runMetadataQuery appContext schemaCache closeWebsocketsOnMetadataChange RQLMetad
158158
$ SchemaSyncLog L.LevelInfo TTMetadataApi
159159
$ String
160160
$ "Attempting to insert new metadata in storage"
161+
161162
newResourceVersion <-
162-
Tracing.newSpan "setMetadata"
163+
Tracing.newSpan "updateMetadataAndNotifySchemaSync"
163164
$ liftEitherM
164-
$ setMetadata (fromMaybe currentResourceVersion _rqlMetadataResourceVersion) modMetadata
165+
$ updateMetadataAndNotifySchemaSync appEnvInstanceId (fromMaybe currentResourceVersion _rqlMetadataResourceVersion) modMetadata cacheInvalidations
166+
165167
L.unLoggerTracing logger
166168
$ SchemaSyncLog L.LevelInfo TTMetadataApi
167169
$ String
168170
$ "Successfully inserted new metadata in storage with resource version: "
169171
<> showMetadataResourceVersion newResourceVersion
170172

171-
-- notify schema cache sync
172-
Tracing.newSpan "notifySchemaCacheSync"
173-
$ liftEitherM
174-
$ notifySchemaCacheSync newResourceVersion appEnvInstanceId cacheInvalidations
175173
L.unLoggerTracing logger
176174
$ SchemaSyncLog L.LevelInfo TTMetadataApi
177175
$ String
178-
$ "Inserted schema cache sync notification at resource version:"
176+
$ "Inserted schema cache sync notification at resource version: "
179177
<> showMetadataResourceVersion newResourceVersion
180178

181179
-- save sources introspection to stored-introspection DB

server/src-lib/Hasura/Server/API/Query.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,18 @@ runQuery appContext sc query = do
217217
then case appEnvEnableMaintenanceMode of
218218
MaintenanceModeDisabled -> do
219219
-- set modified metadata in storage
220-
newResourceVersion <- liftEitherM $ setMetadata currentResourceVersion updatedMetadata
221-
-- notify schema cache sync
222-
liftEitherM $ notifySchemaCacheSync newResourceVersion appEnvInstanceId invalidations
220+
newResourceVersion <-
221+
liftEitherM
222+
$ updateMetadataAndNotifySchemaSync appEnvInstanceId currentResourceVersion updatedMetadata invalidations
223+
224+
-- save sources introspection to stored-introspection DB
225+
saveSourcesIntrospection logger sourcesIntrospection newResourceVersion
223226

224227
(_, modSchemaCache', _, _, _) <-
225228
Tracing.newSpan "setMetadataResourceVersionInSchemaCache"
226229
$ setMetadataResourceVersionInSchemaCache newResourceVersion
227230
& runCacheRWT dynamicConfig modSchemaCache
228231

229-
-- save sources introspection to stored-introspection DB
230-
saveSourcesIntrospection logger sourcesIntrospection newResourceVersion
231232
-- run schema registry action
232233
for_ schemaRegistryAction $ \action -> do
233234
liftIO $ action newResourceVersion (scInconsistentObjs (lastBuiltSchemaCache modSchemaCache')) updatedMetadata

server/src-lib/Hasura/Server/API/V2Query.hs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,26 +132,21 @@ runQuery appContext schemaCache rqlQuery = do
132132
if queryModifiesSchema rqlQuery
133133
then case appEnvEnableMaintenanceMode of
134134
MaintenanceModeDisabled -> do
135-
-- set modified metadata in storage
135+
-- set modified metadata in storage and notify schema sync
136136
newResourceVersion <-
137-
Tracing.newSpan "setMetadata"
137+
Tracing.newSpan "updateMetadataAndNotifySchemaSync"
138138
$ liftEitherM
139-
$ setMetadata currentResourceVersion updatedMetadata
139+
$ updateMetadataAndNotifySchemaSync appEnvInstanceId currentResourceVersion updatedMetadata invalidations
140140

141-
-- notify schema cache sync
142-
Tracing.newSpan "notifySchemaCacheSync"
143-
$ liftEitherM
144-
$ notifySchemaCacheSync newResourceVersion appEnvInstanceId invalidations
141+
-- save sources introspection to stored-introspection DB
142+
Tracing.newSpan "storeSourcesIntrospection"
143+
$ saveSourcesIntrospection (_lsLogger appEnvLoggers) sourcesIntrospection newResourceVersion
145144

146145
(_, modSchemaCache', _, _, _) <-
147146
Tracing.newSpan "setMetadataResourceVersionInSchemaCache"
148147
$ setMetadataResourceVersionInSchemaCache newResourceVersion
149148
& runCacheRWT dynamicConfig modSchemaCache
150149

151-
-- save sources introspection to stored-introspection DB
152-
Tracing.newSpan "storeSourcesIntrospection"
153-
$ saveSourcesIntrospection (_lsLogger appEnvLoggers) sourcesIntrospection newResourceVersion
154-
155150
-- run schema registry action
156151
Tracing.newSpan "runSchemaRegistryAction"
157152
$ for_ schemaRegistryAction

0 commit comments

Comments
 (0)