Skip to content

Commit

Permalink
chore(rethinkdb): QueryMap: One-shot (#10005)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Krick <[email protected]>
  • Loading branch information
mattkrick authored Jul 19, 2024
1 parent 7100a23 commit 28553e4
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 22 deletions.
8 changes: 6 additions & 2 deletions packages/server/__tests__/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ const persistFunction = (text: string) => {
}

const persistQuery = async (query: string) => {
const r = await getRethink()
const pg = getKysely()
const id = persistFunction(query.trim())
const record = {
id,
query,
createdAt: new Date()
}
await r.table('QueryMap').insert(record, {conflict: 'replace'}).run()
await pg
.insertInto('QueryMap')
.values(record)
.onConflict((oc) => oc.doNothing())
.execute()
return id
}

Expand Down
4 changes: 0 additions & 4 deletions packages/server/database/rethinkDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ export type RethinkSchema = {
type: PushInvitation
index: 'userId'
}
QueryMap: {
type: any
index: ''
}
MeetingTemplate: {
type: MeetingTemplate
index: 'teamId' | 'orgId'
Expand Down
11 changes: 8 additions & 3 deletions packages/server/graphql/CompiledQueryCache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tracer from 'dd-trace'
import {GraphQLSchema, parse} from 'graphql'
import {CompiledQuery} from 'graphql-jit'
import getRethink from '../database/rethinkDriver'
import getKysely from '../postgres/getKysely'
import {MutationResolvers, QueryResolvers, Resolver} from './public/resolverTypes'
import {tracedCompileQuery} from './traceGraphQL'

Expand Down Expand Up @@ -42,8 +42,13 @@ export default class CompiledQueryCache {
async fromID(docId: string, schema: GraphQLSchema) {
const compiledQuery = this.store[docId]
if (compiledQuery) return compiledQuery
const r = await getRethink()
let queryString = await r.table('QueryMap').get(docId)('query').default(null).run()
const pg = getKysely()
const queryStringRes = await pg
.selectFrom('QueryMap')
.select('query')
.where('id', '=', docId)
.executeTakeFirst()
let queryString = queryStringRes?.query
if (!queryString && !__PRODUCTION__) {
// try/catch block required for building the toolbox
try {
Expand Down
11 changes: 8 additions & 3 deletions packages/server/graphql/DocumentCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import {DocumentNode, parse} from 'graphql'
import getRethink from '../database/rethinkDriver'
import getKysely from '../postgres/getKysely'

export default class DocumentCache {
store = {} as {[docId: string]: DocumentNode}
Expand All @@ -16,8 +16,13 @@ export default class DocumentCache {
// looks up query string for a persisted query, parses into an AST, caches and returns it
let document = this.store[docId]
if (!document) {
const r = await getRethink()
let queryString = await r.table('QueryMap').get(docId)('query').default(null).run()
const pg = getKysely()
const queryStringRes = await pg
.selectFrom('QueryMap')
.select('query')
.where('id', '=', docId)
.executeTakeFirst()
let queryString = queryStringRes?.query
if (!queryString && !__PRODUCTION__) {
// In development, use the frequently changing queryMap to look up persisted queries by hash
const queryMap = require('../../../queryMap.json')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {Kysely, PostgresDialect, sql} from 'kysely'
import {Client} from 'pg'
import {r} from 'rethinkdb-ts'
import connectRethinkDB from '../../database/connectRethinkDB'
import getPg from '../getPg'
import getPgConfig from '../getPgConfig'

export async function up() {
await connectRethinkDB()
const pg = new Kysely<any>({
dialect: new PostgresDialect({
pool: getPg()
})
})
await sql`
CREATE TABLE IF NOT EXISTS "QueryMap" (
"id" VARCHAR(24) PRIMARY KEY,
"query" TEXT NOT NULL,
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
)
`.execute(pg)
const queries = await r
.table('QueryMap')
.filter((row) => row('createdAt').default(null).ne(null))
.run()
await pg.insertInto('QueryMap').values(queries).execute()
}

export async function down() {
const client = new Client(getPgConfig())
await client.connect()
await client.query(`
DROP TABLE "QueryMap";
` /* Do undo magic */)
await client.end()
}
13 changes: 5 additions & 8 deletions scripts/toolboxSrc/preDeploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dotenv from 'dotenv'
import dotenvExpand from 'dotenv-expand'
import getKysely from 'parabol-server/postgres/getKysely'
import path from 'path'
import getRethink from '../../packages/server/database/rethinkDriver'
import queryMap from '../../queryMap.json'
import getProjectRoot from '../webpack/utils/getProjectRoot'
import {applyEnvVarsToClientAssets} from './applyEnvVarsToClientAssets'
Expand All @@ -21,13 +21,9 @@ const storePersistedQueries = async () => {
createdAt: now
}))

const r = await getRethink()
const res = await r.table('QueryMap').insert(records, {conflict: 'replace'}).run()
// without this sleep RethinkDB closes the connection before the query completes. It doesn't make sense!
await new Promise((resolve) => setTimeout(resolve, 50))
await r.getPoolMaster()?.drain()

console.log(`🔗 QueryMap Persistence Complete: ${res.inserted} records added`)
const pg = getKysely()
const res = await pg.insertInto('QueryMap').values(records).onConflict((oc) => oc.doNothing()).returning('id').execute()
console.log(`🔗 QueryMap Persistence Complete: ${res.length} records added`)
}

const preDeploy = async () => {
Expand All @@ -41,6 +37,7 @@ const preDeploy = async () => {

// The we can prime the DB & CDN
await Promise.all([storePersistedQueries(), primeIntegrations(), pushToCDN()])
await getKysely().destroy()
console.log(`🚀 Predeploy Complete`)
process.exit()
}
Expand Down
2 changes: 0 additions & 2 deletions scripts/toolboxSrc/primeIntegrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ const upsertGlobalIntegrationProvidersFromEnv = async () => {

const primeIntegrations = async () => {
console.log('⛓️ Prime Integrationgs Started')
const pg = getPg()
await upsertGlobalIntegrationProvidersFromEnv()
await pg.end()
console.log('⛓️ Prime Integrations Complete')
}

Expand Down

0 comments on commit 28553e4

Please sign in to comment.