-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathexecution.rs
More file actions
379 lines (345 loc) · 13.7 KB
/
execution.rs
File metadata and controls
379 lines (345 loc) · 13.7 KB
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
//! Tests for this functionality are still mostly in the `crate::services::supergraph::tests` module.
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Instant;
use futures::FutureExt;
use futures::stream::StreamExt;
use tokio::sync::mpsc;
use tokio::sync::mpsc::error::SendError;
use tokio_stream::wrappers::ReceiverStream;
use tower::Layer;
use tower::Service;
use tower::ServiceExt as _;
use tracing::Span;
use tracing::field;
use tracing_futures::Instrument;
use crate::Context;
use crate::Notify;
use crate::apollo_studio_interop::UsageReporting;
use crate::context::OPERATION_NAME;
use crate::graphql;
use crate::graphql::Response;
use crate::plugins::authentication::APOLLO_AUTHENTICATION_JWT_CLAIMS;
use crate::plugins::subscription::SUBSCRIPTION_ERROR_EXTENSION_KEY;
use crate::plugins::subscription::SubscriptionConfig;
use crate::plugins::telemetry::tracing::apollo_telemetry::APOLLO_PRIVATE_DURATION_NS;
use crate::query_planner::subscription::OPENED_SUBSCRIPTIONS;
use crate::query_planner::subscription::SUBSCRIPTION_EVENT_SPAN_NAME;
use crate::query_planner::subscription::SubscriptionHandle;
use crate::services::ExecutionRequest;
use crate::services::SupergraphRequest;
use crate::services::execution;
use crate::services::execution::QueryPlan;
use crate::services::subgraph::BoxGqlStream;
pub(crate) const SUBSCRIPTION_CONFIG_RELOAD_EXTENSION_CODE: &str = "SUBSCRIPTION_CONFIG_RELOAD";
pub(crate) const SUBSCRIPTION_SCHEMA_RELOAD_EXTENSION_CODE: &str = "SUBSCRIPTION_SCHEMA_RELOAD";
const SUBSCRIPTION_JWT_EXPIRED_EXTENSION_CODE: &str = "SUBSCRIPTION_JWT_EXPIRED";
const SUBSCRIPTION_EXECUTION_ERROR_EXTENSION_CODE: &str = "SUBSCRIPTION_EXECUTION_ERROR";
/// The execution side of the subscriptions implementation starts up a side-channel task used to
/// handle messages received from the subgraph that we subscribed to.
pub(crate) struct SubscriptionExecutionLayer {
notify: Notify<String, graphql::Response>,
}
impl SubscriptionExecutionLayer {
pub(crate) fn new(notify: Notify<String, graphql::Response>) -> Self {
Self { notify }
}
}
impl<S> Layer<S> for SubscriptionExecutionLayer {
type Service = SubscriptionExecutionService<S>;
fn layer(&self, inner: S) -> Self::Service {
SubscriptionExecutionService {
inner,
notify: self.notify.clone(),
}
}
}
#[derive(Clone)]
pub(crate) struct SubscriptionExecutionService<S> {
inner: S,
notify: Notify<String, graphql::Response>,
}
impl<S> Service<ExecutionRequest> for SubscriptionExecutionService<S>
where
S: Service<ExecutionRequest, Response = execution::Response, Error = tower::BoxError>
+ Clone
+ Send
+ 'static,
S::Future: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
fn poll_ready(
&mut self,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, mut req: ExecutionRequest) -> Self::Future {
let inner = self.inner.clone();
let mut inner = std::mem::replace(&mut self.inner, inner);
if req.query_plan.is_subscription() {
let notify = self.notify.clone();
let context = req.context.clone();
let (subs_tx, subs_rx) = mpsc::channel(1);
let query_plan = req.query_plan.clone();
let execution_service_cloned = inner.clone();
let cloned_supergraph_req =
clone_supergraph_request(&req.supergraph_request, context.clone());
// Spawn the side-channel task for subscription.
tokio::spawn(async move {
subscription_task(
execution_service_cloned,
context,
query_plan,
subs_rx,
notify,
cloned_supergraph_req,
)
.await;
});
req.subscription_tx = subs_tx.into();
}
inner.call(req)
}
}
// WARN: you might be tempted to change this to pub(crate) because its fields are pub(crate) and
// there's no actual use in having it marked pub _apart_ from it being used in the
// execution::Request builder and, more importantly, the fake_builder we get from BuildStructor.
// Customers use that in unit tests for their plugins, and they'll fail to compile if we mark this
// pub(crate)
pub struct SubscriptionTaskParams {
pub(crate) client_sender: tokio::sync::mpsc::Sender<Response>,
pub(crate) subscription_handle: SubscriptionHandle,
pub(crate) subscription_config: SubscriptionConfig,
pub(crate) stream_rx: ReceiverStream<BoxGqlStream>,
}
fn subscription_fatal_error(message: impl Into<String>, extension_code: &str) -> Response {
Response::builder()
.subscribed(false)
.extension(SUBSCRIPTION_ERROR_EXTENSION_KEY, true)
.error(
graphql::Error::builder()
.message(message)
.extension_code(extension_code)
.build(),
)
.build()
}
/// The execution-side end of the subscriptions side-channel, which propagates messages from the
/// subgraph to the client.
///
/// This end receives the messages from the subgraph, executes query plans to resolve federated
/// data in those messages, and sends the response back on a channel that is part of the eventual
/// response.
async fn subscription_task(
execution_service: impl Service<
ExecutionRequest,
Response = execution::Response,
Error = tower::BoxError,
> + Clone,
context: Context,
query_plan: Arc<QueryPlan>,
mut rx: mpsc::Receiver<SubscriptionTaskParams>,
notify: Notify<String, graphql::Response>,
supergraph_req: SupergraphRequest,
) {
let sub_params = match rx.recv().await {
Some(sub_params) => sub_params,
None => {
return;
}
};
let subscription_config = sub_params.subscription_config;
let subscription_handle = sub_params.subscription_handle;
let mut receiver = sub_params.stream_rx;
let sender = sub_params.client_sender;
// Get the rest of the query_plan to execute for subscription events
let query_plan = match &*query_plan.root {
crate::query_planner::PlanNode::Subscription { rest, .. } => rest.clone().map(|r| {
Arc::new(QueryPlan {
usage_reporting: query_plan.usage_reporting.clone(),
root: Arc::new(*r),
formatted_query_plan: query_plan.formatted_query_plan.clone(),
query: query_plan.query.clone(),
query_metrics: query_plan.query_metrics,
estimated_size: Default::default(),
})
}),
_ => {
let _ = sender
.send(subscription_fatal_error(
"cannot execute the subscription event",
SUBSCRIPTION_EXECUTION_ERROR_EXTENSION_CODE,
))
.await;
return;
}
};
let limit_is_set = subscription_config.max_opened_subscriptions.is_some();
let mut subscription_handle = subscription_handle.clone();
let operation_signature = context
.extensions()
.with_lock(|lock| {
lock.get::<Arc<UsageReporting>>()
.map(|usage_reporting| usage_reporting.get_stats_report_key().clone())
})
.unwrap_or_default();
let operation_name = context
.get::<_, String>(OPERATION_NAME)
.ok()
.flatten()
.unwrap_or_default();
let mut receiver = match receiver.next().await {
Some(receiver) => receiver,
None => {
tracing::trace!("receiver channel closed");
return;
}
};
if limit_is_set {
OPENED_SUBSCRIPTIONS.fetch_add(1, Ordering::Relaxed);
}
let mut configuration_updated_rx = notify.subscribe_configuration();
let mut schema_updated_rx = notify.subscribe_schema();
let mut timeout = if supergraph_req
.context
.get_json_value(APOLLO_AUTHENTICATION_JWT_CLAIMS)
.is_some()
{
let expires_in =
crate::plugins::authentication::jwks::jwt_expires_in(&supergraph_req.context);
tokio::time::sleep(expires_in).boxed()
} else {
futures::future::pending().boxed()
};
loop {
tokio::select! {
// We prefer to specify the order of checks within the select
biased;
_ = subscription_handle.closed_signal.recv() => {
break;
}
_ = &mut timeout => {
let _ = sender.send(subscription_fatal_error("subscription closed because the JWT has expired", SUBSCRIPTION_JWT_EXPIRED_EXTENSION_CODE)).await;
break;
},
message = receiver.next() => {
match message {
Some(mut val) => {
val.created_at = Some(Instant::now());
let res = dispatch_subscription_event(&supergraph_req, execution_service.clone(), query_plan.as_ref(), context.clone(), val, sender.clone())
.instrument(tracing::info_span!(SUBSCRIPTION_EVENT_SPAN_NAME,
graphql.operation.name = %operation_name,
otel.kind = "INTERNAL",
apollo_private.operation_signature = %operation_signature,
apollo_private.duration_ns = field::Empty,)
).await;
if let Err(err) = res {
tracing::error!("cannot send the subscription to the client: {err:?}");
break;
}
}
None => break,
}
}
Some(_new_configuration) = configuration_updated_rx.next() => {
let _ = sender
.send(subscription_fatal_error("subscription has been closed due to a configuration reload", SUBSCRIPTION_CONFIG_RELOAD_EXTENSION_CODE))
.await;
}
Some(_new_schema) = schema_updated_rx.next() => {
let _ = sender
.send(subscription_fatal_error("subscription has been closed due to a schema reload", SUBSCRIPTION_SCHEMA_RELOAD_EXTENSION_CODE))
.await;
break;
}
}
}
drop(sender);
tracing::trace!("Leaving the task for subscription");
if limit_is_set {
OPENED_SUBSCRIPTIONS.fetch_sub(1, Ordering::Relaxed);
}
}
async fn dispatch_subscription_event(
supergraph_req: &SupergraphRequest,
execution_service: impl Service<
ExecutionRequest,
Response = execution::Response,
Error = tower::BoxError,
> + Clone,
query_plan: Option<&Arc<QueryPlan>>,
context: Context,
mut val: graphql::Response,
sender: mpsc::Sender<Response>,
) -> Result<(), SendError<Response>> {
let start = Instant::now();
let span = Span::current();
let res = match query_plan {
Some(query_plan) => {
let cloned_supergraph_req = clone_supergraph_request(
&supergraph_req.supergraph_request,
supergraph_req.context.clone(),
);
let execution_request = ExecutionRequest::internal_builder()
.supergraph_request(cloned_supergraph_req.supergraph_request)
.query_plan(query_plan.clone())
.context(context)
.source_stream_value(val.data.take().unwrap_or_default())
.build()
.await;
let execution_response = execution_service.oneshot(execution_request).await;
let next_response = match execution_response {
Ok(mut execution_response) => execution_response.next_response().await,
Err(err) => {
tracing::error!("cannot execute the subscription event: {err:?}");
let _ = sender
.send(subscription_fatal_error(
"cannot execute the subscription event",
SUBSCRIPTION_EXECUTION_ERROR_EXTENSION_CODE,
))
.await;
return Ok(());
}
};
if let Some(mut next_response) = next_response {
next_response.created_at = val.created_at;
next_response.subscribed = val.subscribed;
val.errors.append(&mut next_response.errors);
next_response.errors = val.errors;
sender.send(next_response).await
} else {
Ok(())
}
}
None => sender.send(val).await,
};
span.record(
APOLLO_PRIVATE_DURATION_NS,
start.elapsed().as_nanos() as i64,
);
res
}
fn clone_supergraph_request(
req: &http::Request<graphql::Request>,
context: Context,
) -> SupergraphRequest {
let mut cloned_supergraph_req = SupergraphRequest::builder()
.extensions(req.body().extensions.clone())
.and_query(req.body().query.clone())
.context(context)
.method(req.method().clone())
.and_operation_name(req.body().operation_name.clone())
.uri(req.uri().clone())
.variables(req.body().variables.clone());
for (header_name, header_value) in req.headers().clone() {
if let Some(header_name) = header_name {
cloned_supergraph_req = cloned_supergraph_req.header(header_name, header_value);
}
}
cloned_supergraph_req
.build()
.expect("cloning an existing supergraph response should not fail")
}