-
Notifications
You must be signed in to change notification settings - Fork 212
/
execute_operation.rs
172 lines (148 loc) Β· 5.93 KB
/
execute_operation.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
use std::time::Instant;
use super::pipeline::QueryPipeline;
use crate::{IrSerializer, Operation, QueryGraph, QueryGraphBuilder, QueryInterpreter, ResponseData};
use connector::{Connection, ConnectionLike, Connector};
use futures::future;
use query_engine_metrics::{
histogram, increment_counter, metrics, PRISMA_CLIENT_QUERIES_HISTOGRAM_MS, PRISMA_CLIENT_QUERIES_TOTAL,
};
use schema::QuerySchemaRef;
use tracing::Instrument;
use tracing_futures::WithSubscriber;
pub async fn execute_single_operation(
query_schema: QuerySchemaRef,
conn: &mut dyn ConnectionLike,
operation: &Operation,
trace_id: Option<String>,
) -> crate::Result<ResponseData> {
let operation_timer = Instant::now();
let interpreter = QueryInterpreter::new(conn);
let (query_graph, serializer) = QueryGraphBuilder::new(query_schema.clone()).build(operation.clone())?;
increment_counter!(PRISMA_CLIENT_QUERIES_TOTAL);
let result = QueryPipeline::new(query_graph, interpreter, serializer)
.execute(trace_id)
.await;
histogram!(PRISMA_CLIENT_QUERIES_HISTOGRAM_MS, operation_timer.elapsed());
result
}
pub async fn execute_many_operations(
query_schema: QuerySchemaRef,
conn: &mut dyn ConnectionLike,
operations: &[Operation],
trace_id: Option<String>,
) -> crate::Result<Vec<crate::Result<ResponseData>>> {
let queries = operations
.iter()
.map(|operation| QueryGraphBuilder::new(query_schema.clone()).build(operation.clone()))
.collect::<std::result::Result<Vec<_>, _>>()?;
let mut results = Vec::with_capacity(queries.len());
for (i, (query_graph, serializer)) in queries.into_iter().enumerate() {
increment_counter!(PRISMA_CLIENT_QUERIES_TOTAL);
let operation_timer = Instant::now();
let interpreter = QueryInterpreter::new(conn);
let result = QueryPipeline::new(query_graph, interpreter, serializer)
.execute(trace_id.clone())
.await;
histogram!(PRISMA_CLIENT_QUERIES_HISTOGRAM_MS, operation_timer.elapsed());
match result {
Ok(result) => results.push(Ok(result)),
Err(error) => {
return Err(crate::CoreError::BatchError {
request_idx: i,
error: Box::new(error),
});
}
}
}
Ok(results)
}
pub async fn execute_single_self_contained<C: Connector + Send + Sync>(
connector: &C,
query_schema: QuerySchemaRef,
operation: Operation,
trace_id: Option<String>,
force_transactions: bool,
) -> crate::Result<ResponseData> {
let (query_graph, serializer) = QueryGraphBuilder::new(query_schema).build(operation)?;
let connection_name = connector.name();
let conn_span = info_span!(
"prisma:engine:connection",
user_facing = true,
"db.type" = connection_name.as_str()
);
let conn = connector.get_connection().instrument(conn_span).await?;
execute_self_contained(conn, query_graph, serializer, force_transactions, trace_id).await
}
pub async fn execute_many_self_contained<C: Connector + Send + Sync>(
connector: &C,
query_schema: QuerySchemaRef,
operations: &[Operation],
trace_id: Option<String>,
force_transactions: bool,
) -> crate::Result<Vec<crate::Result<ResponseData>>> {
let mut futures = Vec::with_capacity(operations.len());
let dispatcher = crate::get_current_dispatcher();
for op in operations {
match QueryGraphBuilder::new(query_schema.clone()).build(op.clone()) {
Ok((graph, serializer)) => {
increment_counter!(PRISMA_CLIENT_QUERIES_TOTAL);
let connection_name = connector.name();
let conn_span = info_span!(
"prisma:engine:connection",
user_facing = true,
"db.type" = connection_name.as_str()
);
let conn = connector.get_connection().instrument(conn_span).await?;
futures.push(tokio::spawn(
execute_self_contained(conn, graph, serializer, force_transactions, trace_id.clone())
.with_subscriber(dispatcher.clone()),
));
}
// This looks unnecessary, but is the simplest way to preserve ordering of results for the batch.
Err(err) => futures.push(tokio::spawn(async move { Err(err.into()) })),
}
}
let responses: Vec<_> = future::join_all(futures)
.await
.into_iter()
.map(|res| res.expect("IO Error in tokio::spawn"))
.collect();
Ok(responses)
}
/// Execute the operation as a self-contained operation, if necessary wrapped in a transaction.
async fn execute_self_contained(
mut conn: Box<dyn Connection>,
graph: QueryGraph,
serializer: IrSerializer,
force_transactions: bool,
trace_id: Option<String>,
) -> crate::Result<ResponseData> {
let operation_timer = Instant::now();
let result = if force_transactions || graph.needs_transaction() {
let mut tx = conn.start_transaction(None).await?;
let result = execute_on(tx.as_connection_like(), graph, serializer, trace_id).await;
if result.is_ok() {
tx.commit().await?;
} else {
tx.rollback().await?;
}
result
} else {
execute_on(conn.as_connection_like(), graph, serializer, trace_id).await
};
histogram!(PRISMA_CLIENT_QUERIES_HISTOGRAM_MS, operation_timer.elapsed());
result
}
// Simplest execution on anything that's a ConnectionLike. Caller decides handling of connections and transactions.
async fn execute_on(
conn: &mut dyn ConnectionLike,
graph: QueryGraph,
serializer: IrSerializer,
trace_id: Option<String>,
) -> crate::Result<ResponseData> {
increment_counter!(PRISMA_CLIENT_QUERIES_TOTAL);
let interpreter = QueryInterpreter::new(conn);
QueryPipeline::new(graph, interpreter, serializer)
.execute(trace_id)
.await
}