-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
DataProxyEngine.ts
443 lines (372 loc) · 13.7 KB
/
DataProxyEngine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import Debug from '@prisma/debug'
import { DMMF } from '@prisma/generator-helper'
import type {
BatchQueryEngineResult,
EngineConfig,
EngineEventType,
GetConfigResult,
InlineDatasource,
InteractiveTransactionOptions,
RequestBatchOptions,
RequestOptions,
} from '../common/Engine'
import { Engine } from '../common/Engine'
import { PrismaClientUnknownRequestError } from '../common/errors/PrismaClientUnknownRequestError'
import { prismaGraphQLToJSError } from '../common/errors/utils/prismaGraphQLToJSError'
import { EventEmitter } from '../common/types/Events'
import { EngineMetricsOptions, Metrics, MetricsOptionsJson, MetricsOptionsPrometheus } from '../common/types/Metrics'
import {
QueryEngineBatchRequest,
QueryEngineRequestHeaders,
QueryEngineResult,
QueryEngineResultBatchQueryResult,
} from '../common/types/QueryEngine'
import type * as Tx from '../common/types/Transaction'
import { DataProxyError } from './errors/DataProxyError'
import { ForcedRetryError } from './errors/ForcedRetryError'
import { InvalidDatasourceError } from './errors/InvalidDatasourceError'
import { NotImplementedYetError } from './errors/NotImplementedYetError'
import { SchemaMissingError } from './errors/SchemaMissingError'
import { responseToError } from './errors/utils/responseToError'
import { backOff } from './utils/backOff'
import { getClientVersion } from './utils/getClientVersion'
import { request } from './utils/request'
const MAX_RETRIES = 10
// to defer the execution of promises in the constructor
const P = Promise.resolve()
const debug = Debug('prisma:client:dataproxyEngine')
type DataProxyTxInfoPayload = {
endpoint: string
}
type DataProxyTxInfo = Tx.Info<DataProxyTxInfoPayload>
export class DataProxyEngine extends Engine {
private inlineSchema: string
readonly inlineSchemaHash: string
private inlineDatasources: Record<string, InlineDatasource>
private config: EngineConfig
private logEmitter: EventEmitter
private env: { [k in string]?: string }
private clientVersion: string
readonly remoteClientVersion: Promise<string>
readonly headers: { Authorization: string }
readonly host: string
constructor(config: EngineConfig) {
super()
this.config = config
this.env = { ...this.config.env, ...process.env }
this.inlineSchema = config.inlineSchema ?? ''
this.inlineDatasources = config.inlineDatasources ?? {}
this.inlineSchemaHash = config.inlineSchemaHash ?? ''
this.clientVersion = config.clientVersion ?? 'unknown'
this.logEmitter = config.logEmitter
const [host, apiKey] = this.extractHostAndApiKey()
this.remoteClientVersion = P.then(() => getClientVersion(this.config))
this.headers = { Authorization: `Bearer ${apiKey}` }
this.host = host
debug('host', this.host)
}
version() {
// QE is remote, we don't need to know the exact commit SHA
return 'unknown'
}
async start() {}
async stop() {}
on(event: EngineEventType, listener: (args?: any) => any): void {
if (event === 'beforeExit') {
// TODO: hook into the process
throw new NotImplementedYetError('beforeExit event is not yet supported', {
clientVersion: this.clientVersion,
})
} else {
this.logEmitter.on(event, listener)
}
}
private async url(s: string) {
return `https://${this.host}/${await this.remoteClientVersion}/${this.inlineSchemaHash}/${s}`
}
// TODO: looks like activeProvider is the only thing
// used externally; verify that
async getConfig() {
return Promise.resolve({
datasources: [
{
activeProvider: this.config.activeProvider,
},
],
} as GetConfigResult)
}
getDmmf(): Promise<DMMF.Document> {
// This code path should not be reachable, as it is handled upstream in `getPrismaClient`.
throw new NotImplementedYetError('getDmmf is not yet supported', {
clientVersion: this.clientVersion,
})
}
private async uploadSchema() {
const response = await request(await this.url('schema'), {
method: 'PUT',
headers: this.headers,
body: this.inlineSchema,
clientVersion: this.clientVersion,
})
if (!response.ok) {
debug('schema response status', response.status)
}
const err = await responseToError(response, this.clientVersion)
if (err) {
this.logEmitter.emit('warn', { message: `Error while uploading schema: ${err.message}` })
throw err
} else {
this.logEmitter.emit('info', {
message: `Schema (re)uploaded (hash: ${this.inlineSchemaHash})`,
})
}
}
request<T>({ query, headers = {}, transaction }: RequestOptions<DataProxyTxInfoPayload>) {
this.logEmitter.emit('query', { query })
// TODO: `elapsed`?
return this.requestInternal<T>({ query, variables: {} }, headers, transaction)
}
async requestBatch<T>({
queries,
headers = {},
transaction,
}: RequestBatchOptions): Promise<BatchQueryEngineResult<T>[]> {
const isTransaction = Boolean(transaction)
this.logEmitter.emit('query', {
query: `Batch${isTransaction ? ' in transaction' : ''} (${queries.length}):\n${queries.join('\n')}`,
})
const body: QueryEngineBatchRequest = {
batch: queries.map((query) => ({ query, variables: {} })),
transaction: isTransaction,
isolationLevel: transaction?.isolationLevel,
}
const { batchResult, elapsed } = await this.requestInternal<T, true>(body, headers)
return batchResult.map((result) => {
if ('errors' in result && result.errors.length > 0) {
return prismaGraphQLToJSError(result.errors[0], this.clientVersion!)
}
return {
data: result as T,
elapsed,
}
})
}
private requestInternal<T, Batch extends boolean = false>(
body: Record<string, any>,
headers: QueryEngineRequestHeaders,
itx?: InteractiveTransactionOptions<DataProxyTxInfoPayload>,
): Promise<
Batch extends true ? { batchResult: QueryEngineResultBatchQueryResult<T>[]; elapsed: number } : QueryEngineResult<T>
> {
return this.withRetry({
actionGerund: 'querying',
callback: async ({ logHttpCall }) => {
const url = itx ? `${itx.payload.endpoint}/graphql` : await this.url('graphql')
logHttpCall(url)
const response = await request(url, {
method: 'POST',
headers: { ...runtimeHeadersToHttpHeaders(headers), ...this.headers },
body: JSON.stringify(body),
clientVersion: this.clientVersion,
})
if (!response.ok) {
debug('graphql response status', response.status)
}
const e = await responseToError(response, this.clientVersion)
await this.handleError(e)
const data = await response.json()
// TODO: headers contain `x-elapsed` and it needs to be returned
if (data.errors) {
if (data.errors.length === 1) {
throw prismaGraphQLToJSError(data.errors[0], this.config.clientVersion!)
} else {
throw new PrismaClientUnknownRequestError(data.errors, { clientVersion: this.config.clientVersion! })
}
}
return data
},
})
}
/**
* Send START, COMMIT, or ROLLBACK to the Query Engine
* @param action START, COMMIT, or ROLLBACK
* @param headers headers for tracing
* @param options to change the default timeouts
* @param info transaction information for the QE
*/
transaction(action: 'start', headers: Tx.TransactionHeaders, options?: Tx.Options): Promise<DataProxyTxInfo>
transaction(action: 'commit', headers: Tx.TransactionHeaders, info: DataProxyTxInfo): Promise<undefined>
transaction(action: 'rollback', headers: Tx.TransactionHeaders, info: DataProxyTxInfo): Promise<undefined>
async transaction(action: any, headers: Tx.TransactionHeaders, arg?: any) {
const actionToGerund = {
start: 'starting',
commit: 'committing',
rollback: 'rolling back',
}
return this.withRetry({
actionGerund: `${actionToGerund[action]} transaction`,
callback: async ({ logHttpCall }) => {
if (action === 'start') {
const body = JSON.stringify({
max_wait: arg?.maxWait ?? 2000, // default
timeout: arg?.timeout ?? 5000, // default
isolation_level: arg?.isolationLevel,
})
const url = await this.url('transaction/start')
logHttpCall(url)
const response = await request(url, {
method: 'POST',
headers: { ...runtimeHeadersToHttpHeaders(headers), ...this.headers },
body,
clientVersion: this.clientVersion,
})
const err = await responseToError(response, this.clientVersion)
await this.handleError(err)
const json = await response.json()
const id = json.id as string
const endpoint = json['data-proxy'].endpoint as string
return { id, payload: { endpoint } }
} else {
const url = `${arg.payload.endpoint}/${action}`
logHttpCall(url)
const response = await request(url, {
method: 'POST',
headers: { ...runtimeHeadersToHttpHeaders(headers), ...this.headers },
clientVersion: this.clientVersion,
})
const err = await responseToError(response, this.clientVersion)
await this.handleError(err)
return undefined
}
},
})
}
private extractHostAndApiKey() {
const datasources = this.mergeOverriddenDatasources()
const mainDatasourceName = Object.keys(datasources)[0]
const mainDatasource = datasources[mainDatasourceName]
const dataProxyURL = this.resolveDatasourceURL(mainDatasourceName, mainDatasource)
let url: URL
try {
url = new URL(dataProxyURL)
} catch {
throw new InvalidDatasourceError('Could not parse URL of the datasource', {
clientVersion: this.clientVersion,
})
}
const { protocol, host, searchParams } = url
if (protocol !== 'prisma:') {
throw new InvalidDatasourceError('Datasource URL must use prisma:// protocol when --data-proxy is used', {
clientVersion: this.clientVersion,
})
}
const apiKey = searchParams.get('api_key')
if (apiKey === null || apiKey.length < 1) {
throw new InvalidDatasourceError('No valid API key found in the datasource URL', {
clientVersion: this.clientVersion,
})
}
return [host, apiKey]
}
private mergeOverriddenDatasources(): Record<string, InlineDatasource> {
if (this.config.datasources === undefined) {
return this.inlineDatasources
}
const finalDatasources = { ...this.inlineDatasources }
for (const override of this.config.datasources) {
if (!this.inlineDatasources[override.name]) {
throw new Error(`Unknown datasource: ${override.name}`)
}
finalDatasources[override.name] = {
url: {
fromEnvVar: null,
value: override.url,
},
}
}
return finalDatasources
}
private resolveDatasourceURL(name: string, datasource: InlineDatasource): string {
if (datasource.url.value) {
return datasource.url.value
}
if (datasource.url.fromEnvVar) {
const envVar = datasource.url.fromEnvVar
const loadedEnvURL = this.env[envVar]
if (loadedEnvURL === undefined) {
throw new InvalidDatasourceError(
`Datasource "${name}" references an environment variable "${envVar}" that is not set`,
{
clientVersion: this.clientVersion,
},
)
}
return loadedEnvURL
}
throw new InvalidDatasourceError(
`Datasource "${name}" specification is invalid: both value and fromEnvVar are null`,
{
clientVersion: this.clientVersion,
},
)
}
metrics(options: MetricsOptionsJson): Promise<Metrics>
metrics(options: MetricsOptionsPrometheus): Promise<string>
metrics(options: EngineMetricsOptions): Promise<Metrics> | Promise<string> {
throw new NotImplementedYetError('Metric are not yet supported for Data Proxy', {
clientVersion: this.clientVersion,
})
}
private async withRetry<T>(args: {
callback: (api: { logHttpCall: (url: string) => void }) => Promise<T>
actionGerund: string
}): Promise<T> {
for (let attempt = 0; ; attempt++) {
const logHttpCall = (url: string) => {
this.logEmitter.emit('info', {
message: `Calling ${url} (n=${attempt})`,
})
}
try {
return await args.callback({ logHttpCall })
} catch (e) {
if (!(e instanceof DataProxyError)) throw e
if (!e.isRetryable) throw e
if (attempt >= MAX_RETRIES) {
if (e instanceof ForcedRetryError) {
throw e.cause
} else {
throw e
}
}
this.logEmitter.emit('warn', {
message: `Attempt ${attempt + 1}/${MAX_RETRIES} failed for ${args.actionGerund}: ${e.message ?? '(unknown)'}`,
})
const delay = await backOff(attempt)
this.logEmitter.emit('warn', { message: `Retrying after ${delay}ms` })
}
}
}
private async handleError(error: DataProxyError | undefined): Promise<void> {
if (error instanceof SchemaMissingError) {
await this.uploadSchema()
throw new ForcedRetryError({
clientVersion: this.clientVersion,
cause: error,
})
} else if (error) {
throw error
}
}
}
/**
* Convert runtime headers to HTTP headers expected by the Data Proxy by removing the transactionId runtime header.
*/
function runtimeHeadersToHttpHeaders(headers: QueryEngineRequestHeaders): Record<string, string | undefined> {
if (headers.transactionId) {
const httpHeaders = { ...headers }
delete httpHeaders.transactionId
return httpHeaders
}
return headers
}