/
engine.rs
514 lines (439 loc) Β· 17.1 KB
/
engine.rs
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use crate::{error::ApiError, log_callback::LogCallback, logger::Logger};
use futures::FutureExt;
use psl::{common::preview_features::PreviewFeature, dml::Datamodel};
use query_core::{
executor,
schema::{QuerySchema, QuerySchemaRenderer},
schema_builder, set_parent_context_from_json_str, QueryExecutor, TxId,
};
use query_engine_metrics::{MetricFormat, MetricRegistry};
use request_handlers::{GraphQLSchemaRenderer, GraphQlHandler, TxInput};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
collections::{BTreeMap, HashMap},
future::Future,
panic::AssertUnwindSafe,
path::PathBuf,
sync::Arc,
};
use tokio::sync::RwLock;
use tracing::{field, instrument::WithSubscriber, Instrument, Span};
use tracing_subscriber::filter::LevelFilter;
use user_facing_errors::Error;
use napi::{Env, JsFunction, JsUnknown};
use napi_derive::napi;
/// The main query engine used by JS
#[napi]
pub struct QueryEngine {
inner: RwLock<Inner>,
logger: Logger,
}
/// The state of the engine.
enum Inner {
/// Not connected, holding all data to form a connection.
Builder(EngineBuilder),
/// A connected engine, holding all data to disconnect and form a new
/// connection. Allows querying when on this state.
Connected(ConnectedEngine),
}
/// Holding the information to reconnect the engine if needed.
#[derive(Debug, Clone)]
struct EngineDatamodel {
#[allow(unused)]
ast: Datamodel,
raw: String,
}
/// Everything needed to connect to the database and have the core running.
struct EngineBuilder {
datamodel: EngineDatamodel,
config: psl::Configuration,
config_dir: PathBuf,
env: HashMap<String, String>,
}
/// Internal structure for querying and reconnecting with the engine.
struct ConnectedEngine {
datamodel: EngineDatamodel,
query_schema: Arc<QuerySchema>,
executor: crate::Executor,
config_dir: PathBuf,
env: HashMap<String, String>,
metrics: Option<MetricRegistry>,
}
/// Returned from the `serverInfo` method in javascript.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ServerInfo {
commit: String,
version: String,
primary_connector: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct MetricOptions {
format: MetricFormat,
#[serde(default)]
global_labels: HashMap<String, String>,
}
impl MetricOptions {
fn is_json_format(&self) -> bool {
self.format == MetricFormat::Json
}
}
impl ConnectedEngine {
/// The schema AST for Query Engine core.
pub fn query_schema(&self) -> &Arc<QuerySchema> {
&self.query_schema
}
/// The query executor.
pub fn executor(&self) -> &(dyn QueryExecutor + Send + Sync) {
&*self.executor
}
}
/// Parameters defining the construction of an engine.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ConstructorOptions {
datamodel: String,
log_level: String,
#[serde(default)]
log_queries: bool,
#[serde(default)]
datasource_overrides: BTreeMap<String, String>,
#[serde(default)]
env: serde_json::Value,
config_dir: PathBuf,
#[serde(default)]
ignore_env_var_errors: bool,
}
impl Inner {
/// Returns a builder if the engine is not connected
fn as_builder(&self) -> crate::Result<&EngineBuilder> {
match self {
Inner::Builder(ref builder) => Ok(builder),
Inner::Connected(_) => Err(ApiError::AlreadyConnected),
}
}
/// Returns the engine if connected
fn as_engine(&self) -> crate::Result<&ConnectedEngine> {
match self {
Inner::Builder(_) => Err(ApiError::NotConnected),
Inner::Connected(ref engine) => Ok(engine),
}
}
}
#[napi]
impl QueryEngine {
/// Parse a validated datamodel and configuration to allow connecting later on.
#[napi(constructor)]
pub fn new(napi_env: Env, options: JsUnknown, callback: JsFunction) -> napi::Result<Self> {
let log_callback = LogCallback::new(napi_env, callback)?;
log_callback.unref(&napi_env)?;
let ConstructorOptions {
datamodel,
log_level,
log_queries,
datasource_overrides,
env,
config_dir,
ignore_env_var_errors,
} = napi_env.from_js_value(options)?;
let env = stringify_env_values(env)?; // we cannot trust anything JS sends us from process.env
let overrides: Vec<(_, _)> = datasource_overrides.into_iter().collect();
let mut schema = psl::validate(datamodel.into());
let config = &mut schema.configuration;
schema
.diagnostics
.to_result()
.map_err(|err| ApiError::conversion(err, schema.db.source()))?;
if !ignore_env_var_errors {
config
.resolve_datasource_urls_from_env(&overrides, |key| env.get(key).map(ToString::to_string))
.map_err(|err| ApiError::conversion(err, schema.db.source()))?;
}
config
.validate_that_one_datasource_is_provided()
.map_err(|errors| ApiError::conversion(errors, schema.db.source()))?;
let enable_metrics = config.preview_features().contains(PreviewFeature::Metrics);
let enable_tracing = config.preview_features().contains(PreviewFeature::Tracing);
let ast = psl::lift(&schema);
let datamodel = EngineDatamodel {
ast,
raw: schema.db.source().to_owned(),
};
let builder = EngineBuilder {
datamodel,
config: schema.configuration,
config_dir,
env,
};
let log_level = log_level.parse::<LevelFilter>().unwrap();
let logger = Logger::new(log_queries, log_level, log_callback, enable_metrics, enable_tracing);
// Describe metrics adds all the descriptions and default values for our metrics
// this needs to run once our metrics pipeline has been configured and it needs to
// use the correct logging subscriber(our dispatch) so that the metrics recorder recieves
// it
if enable_metrics {
napi_env.execute_tokio_future(
async {
query_engine_metrics::describe_metrics();
Ok(())
}
.with_subscriber(logger.dispatcher()),
|&mut _env, _data| Ok(()),
)?;
}
Ok(Self {
inner: RwLock::new(Inner::Builder(builder)),
logger,
})
}
/// Connect to the database, allow queries to be run.
#[napi]
pub async fn connect(&self, trace: String) -> napi::Result<()> {
let dispatcher = self.logger.dispatcher();
async_panic_to_js_error(async {
let span = tracing::info_span!("prisma:engine:connect");
let _ = set_parent_context_from_json_str(&span, &trace);
let mut inner = self.inner.write().await;
let builder = inner.as_builder()?;
let engine = async move {
// We only support one data source & generator at the moment, so take the first one (default not exposed yet).
let data_source = builder
.config
.datasources
.first()
.ok_or_else(|| ApiError::configuration("No valid data source found"))?;
let preview_features: Vec<_> = builder.config.preview_features().iter().collect();
let url = data_source
.load_url_with_config_dir(&builder.config_dir, |key| builder.env.get(key).map(ToString::to_string))
.map_err(|err| crate::error::ApiError::Conversion(err, builder.datamodel.raw.clone()))?;
let (db_name, executor) = executor::load(data_source, &preview_features, &url).await?;
let connector = executor.primary_connector();
connector.get_connection().await?;
// Build internal data model
let schema = psl::parse_schema_parserdb(&*builder.datamodel.raw).unwrap();
let internal_data_model = prisma_models::convert(&schema, db_name);
let query_schema = schema_builder::build(
internal_data_model,
true, // enable raw queries
data_source.active_connector,
preview_features,
data_source.referential_integrity(),
);
Ok(ConnectedEngine {
datamodel: builder.datamodel.clone(),
query_schema: Arc::new(query_schema),
executor,
config_dir: builder.config_dir.clone(),
env: builder.env.clone(),
metrics: self.logger.metrics(),
}) as crate::Result<ConnectedEngine>
}
.instrument(span)
.await?;
*inner = Inner::Connected(engine);
Ok(())
})
.with_subscriber(dispatcher)
.await?;
Ok(())
}
/// Disconnect and drop the core. Can be reconnected later with `#connect`.
#[napi]
pub async fn disconnect(&self, trace: String) -> napi::Result<()> {
let dispatcher = self.logger.dispatcher();
async_panic_to_js_error(async {
let span = tracing::info_span!("prisma:engine:disconnect");
let _ = set_parent_context_from_json_str(&span, &trace);
async {
let mut inner = self.inner.write().await;
let engine = inner.as_engine()?;
let config = psl::parse_configuration(&engine.datamodel.raw)
.map_err(|errors| ApiError::conversion(errors, &engine.datamodel.raw))?;
let builder = EngineBuilder {
datamodel: engine.datamodel.clone(),
config,
config_dir: engine.config_dir.clone(),
env: engine.env.clone(),
};
*inner = Inner::Builder(builder);
Ok(())
}
.instrument(span)
.await
})
.with_subscriber(dispatcher)
.await
}
/// If connected, sends a query to the core and returns the response.
#[napi]
pub async fn query(&self, body: String, trace: String, tx_id: Option<String>) -> napi::Result<String> {
async_panic_to_js_error(async {
let inner = self.inner.read().await;
let engine = inner.as_engine()?;
let query = serde_json::from_str(&body)?;
let dispatcher = self.logger.dispatcher();
async move {
let span = if tx_id.is_none() {
tracing::info_span!("prisma:engine", user_facing = true)
} else {
Span::none()
};
let trace_id = set_parent_context_from_json_str(&span, &trace);
let handler = GraphQlHandler::new(engine.executor(), engine.query_schema());
let response = handler
.handle(query, tx_id.map(TxId::from), trace_id)
.instrument(span)
.await;
Ok(serde_json::to_string(&response)?)
}
.with_subscriber(dispatcher)
.await
})
.await
}
/// If connected, attempts to start a transaction in the core and returns its ID.
#[napi]
pub async fn start_transaction(&self, input: String, trace: String) -> napi::Result<String> {
async_panic_to_js_error(async {
let inner = self.inner.read().await;
let engine = inner.as_engine()?;
let dispatcher = self.logger.dispatcher();
async move {
let span = tracing::info_span!("prisma:engine:itx_runner", user_facing = true, itx_id = field::Empty);
set_parent_context_from_json_str(&span, &trace);
let input: TxInput = serde_json::from_str(&input)?;
match engine
.executor()
.start_tx(
engine.query_schema().clone(),
input.max_wait,
input.timeout,
input.isolation_level,
)
.instrument(span)
.await
{
Ok(tx_id) => Ok(json!({ "id": tx_id.to_string() }).to_string()),
Err(err) => Ok(map_known_error(err)?),
}
}
.with_subscriber(dispatcher)
.await
})
.await
}
/// If connected, attempts to commit a transaction with id `tx_id` in the core.
#[napi]
pub async fn commit_transaction(&self, tx_id: String, _trace: String) -> napi::Result<String> {
async_panic_to_js_error(async {
let inner = self.inner.read().await;
let engine = inner.as_engine()?;
let dispatcher = self.logger.dispatcher();
async move {
match engine.executor().commit_tx(TxId::from(tx_id)).await {
Ok(_) => Ok("{}".to_string()),
Err(err) => Ok(map_known_error(err)?),
}
}
.with_subscriber(dispatcher)
.await
})
.await
}
/// If connected, attempts to roll back a transaction with id `tx_id` in the core.
#[napi]
pub async fn rollback_transaction(&self, tx_id: String, _trace: String) -> napi::Result<String> {
async_panic_to_js_error(async {
let inner = self.inner.read().await;
let engine = inner.as_engine()?;
let dispatcher = self.logger.dispatcher();
async move {
match engine.executor().rollback_tx(TxId::from(tx_id)).await {
Ok(_) => Ok("{}".to_string()),
Err(err) => Ok(map_known_error(err)?),
}
}
.with_subscriber(dispatcher)
.await
})
.await
}
/// Loads the query schema. Only available when connected.
#[napi]
pub async fn sdl_schema(&self) -> napi::Result<String> {
async_panic_to_js_error(async move {
let inner = self.inner.read().await;
let engine = inner.as_engine()?;
Ok(GraphQLSchemaRenderer::render(engine.query_schema().clone()))
})
.await
}
#[napi]
pub async fn metrics(&self, json_options: String) -> napi::Result<String> {
async_panic_to_js_error(async move {
let inner = self.inner.read().await;
let engine = inner.as_engine()?;
let options: MetricOptions = serde_json::from_str(&json_options)?;
if let Some(metrics) = &engine.metrics {
if options.is_json_format() {
let engine_metrics = metrics.to_json(options.global_labels);
let res = serde_json::to_string(&engine_metrics)?;
Ok(res)
} else {
Ok(metrics.to_prometheus(options.global_labels))
}
} else {
Err(ApiError::Configuration(
"Metrics is not enabled. First set it in the preview features.".to_string(),
)
.into())
}
})
.await
}
}
fn map_known_error(err: query_core::CoreError) -> crate::Result<String> {
let user_error: user_facing_errors::Error = err.into();
let value = serde_json::to_string(&user_error)?;
Ok(value)
}
fn stringify_env_values(origin: serde_json::Value) -> crate::Result<HashMap<String, String>> {
use serde_json::Value;
let msg = match origin {
Value::Object(map) => {
let mut result: HashMap<String, String> = HashMap::new();
for (key, val) in map.into_iter() {
match val {
Value::Null => continue,
Value::String(val) => {
result.insert(key, val);
}
val => {
result.insert(key, val.to_string());
}
}
}
return Ok(result);
}
Value::Null => return Ok(Default::default()),
Value::Bool(_) => "Expected an object for the env constructor parameter, got a boolean.",
Value::Number(_) => "Expected an object for the env constructor parameter, got a number.",
Value::String(_) => "Expected an object for the env constructor parameter, got a string.",
Value::Array(_) => "Expected an object for the env constructor parameter, got an array.",
};
Err(ApiError::JsonDecode(msg.to_string()))
}
async fn async_panic_to_js_error<F, R>(fut: F) -> napi::Result<R>
where
F: Future<Output = napi::Result<R>>,
{
match AssertUnwindSafe(fut).catch_unwind().await {
Ok(result) => result,
Err(err) => match Error::extract_panic_message(err) {
Some(message) => Err(napi::Error::from_reason(format!("PANIC: {}", message))),
None => Err(napi::Error::from_reason("PANIC: unknown panic".to_string())),
},
}
}