Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manage schema cache when horizontally scaled (closes #1182) #1574

Merged
merged 25 commits into from
Mar 12, 2019

Conversation

rakeshkky
Copy link
Member

Description

What component does this PR affect?

  • Server
  • Console
  • CLI
  • Docs
  • Community Content
  • Build System

Requires changes from other components? If yes, please mark the components:

  • Server
  • Console
  • CLI
  • Docs
  • Community Content
  • Build System

Related Issue

close #1182

Solution and Design

  1. Haskel library pg-client-hs has been updated to expose a function that helps listen to postgres notifications over a channel in this PR
  2. The server records an event in a table hdb_catalog.hdb_cache_update_event whenever any /v1/query (that changes metadata) is requested. A trigger notifies a cache update event via hasura_cache_update channel
  3. The server runs two concurrent threads namely listener and processor. The listener thread listens to events on hasura_cache_update channel and pushed into a Queue. The processor thread fetches events from that Queue and processes it. Thus server rebuilds schema cache from database and updates.

Type

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Docs update
  • Community content

Checklist:

  • I have read the contributing guide and my code conforms to the guidelines.
  • This change requires a change in the documentation.
  • I have updated the documentation accordingly.
  • I have added required tests.

@rakeshkky rakeshkky added the c/server Related to server label Feb 8, 2019
@rakeshkky rakeshkky self-assigned this Feb 8, 2019
@rakeshkky rakeshkky requested a review from 0x777 February 8, 2019 09:00
@hasura-bot
Copy link
Contributor

Review app for commit 428f70d deployed to Heroku: https://hge-ci-pull-1574.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1574-428f70d

@coco98
Copy link
Contributor

coco98 commented Feb 8, 2019

We should add tests for this? Will require some interesting black magic from @shahidhk @arvi3411301 I think ;)

server/src-exec/Main.hs Outdated Show resolved Hide resolved
server/src-exec/Main.hs Outdated Show resolved Hide resolved
server/src-lib/Hasura/Server/App.hs Outdated Show resolved Hide resolved
server/src-lib/Hasura/Server/App.hs Outdated Show resolved Hide resolved
server/src-lib/Hasura/Server/Query.hs Outdated Show resolved Hide resolved
-> UserInfo -> SchemaCache -> HTTP.Manager
-> RQLQuery -> m (BL.ByteString, SchemaCache)
runQuery pool isoL userInfo sc hMgr query = do
runQuery pool isoL serverId userInfo sc hMgr query = do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should return (BL.ByteString, Maybe (SchemaCache, Timestamp). Just (SC, TS) implies that the schemacache has to be replaced. After this we don't have to use queryNeedsReload in the locations where runQuery is being called.

@@ -403,3 +403,32 @@ CREATE TABLE hdb_catalog.remote_schemas (
definition JSON,
comment TEXT
);

CREATE TABLE hdb_catalog.hdb_cache_update_event (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are recording schema updates. So let's call it hdb_schema_update_event (cascade the changes through out the PR).

-- Postgres notification handler
notifyHandler notif = do
let eventChannel = PGChannel $ bsToTxt $ PQ.notifyRelname notif
when (eventChannel == pgChannel) $
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do this check again?

onError e = do
logError logger threadType $ TEQueryError e
-- Push a listen failed event to queue
STM.atomically $ STM.writeTQueue eventsQueue CUEListenFail
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't emit a 'listen error' event. Emit an event once the connection is re-established after an error. You'll probably need to expose this functionality from pg-client-hs, probably listen can take something like this as an argument:

data NotifyHandler 
  = NotifyHandler 
  { _ehOnInit :: IO ()
  , _ehOnMessage :: Notify -> IO ()
  }

cacheUpdateEventProcessor pool logger httpManager eventsQueue
cacheRef lk serverId cacheInit = do
-- Initiate previous event IO reference
prevEventRef <- IORef.newIORef Nothing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to store the previous event ref after the above suggested change (emit event on reconnection).

Resolve Conflicts:
	server/src-exec/Main.hs
	server/src-lib/Hasura/Server/Init.hs
Resolve Conflicts:
	server/src-exec/Main.hs
	server/src-exec/Ops.hs
	server/src-lib/Hasura/Server/App.hs
	server/src-lib/Hasura/Server/Init.hs
	server/src-lib/Hasura/Server/Query.hs
	server/src-rsr/migrate_from_9_to_10.sql
@hasura-bot
Copy link
Contributor

Review app for commit 933b734 deployed to Heroku: https://hge-ci-pull-1574.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1574-933b734

@rakeshkky rakeshkky requested a review from shahidhk as a code owner March 7, 2019 06:21
@hasura-bot
Copy link
Contributor

Review app for commit e9c7c2b deployed to Heroku: https://hge-ci-pull-1574.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1574-e9c7c2b

@hasura-bot
Copy link
Contributor

Review app for commit 1632dd1 deployed to Heroku: https://hge-ci-pull-1574.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1574-1632dd1

@hasura-bot
Copy link
Contributor

Review app for commit bf30119 deployed to Heroku: https://hge-ci-pull-1574.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1574-bf30119

@hasura-bot
Copy link
Contributor

Review app for commit 054b26a deployed to Heroku: https://hge-ci-pull-1574.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1574-054b26a

@hasura-bot
Copy link
Contributor

Review app for commit 4472a21 deployed to Heroku: https://hge-ci-pull-1574.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1574-4472a21

@hasura-bot
Copy link
Contributor

Review app for commit 6338341 deployed to Heroku: https://hge-ci-pull-1574.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1574-6338341

@hasura-bot
Copy link
Contributor

Review app for commit 4a74839 deployed to Heroku: https://hge-ci-pull-1574.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1574-4a74839

@hasura-bot
Copy link
Contributor

Review app for commit 524bf91 deployed to Heroku: https://hge-ci-pull-1574.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1574-524bf91

Copy link
Member

@shahidhk shahidhk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shahidhk shahidhk merged commit e32f5a1 into hasura:master Mar 12, 2019
@hasura-bot
Copy link
Contributor

Review app https://hge-ci-pull-1574.herokuapp.com is deleted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c/server Related to server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

improve schema management when horizontally scaled
6 participants