hive-router 0.2.0

GraphQL router for Federation, part of the Hive platform
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
use crate::config::coprocessor::CoprocessorConfig;
use crate::http_utils::body::read_body_stream;
use crate::telemetry::logging::targets;
use crate::telemetry::traces::spans::coprocessor::CoprocessorSpan;
use crate::telemetry::TelemetryContext;
use http::{Method as HttpMethod, Uri};
use ntex::http::HeaderMap;
use ntex::web::{self, DefaultError};
use std::ops::ControlFlow;
use std::sync::Arc;
use tracing::{debug, error, Instrument};

use crate::executor::coprocessor::client::CoprocessorClient;
use crate::executor::coprocessor::error::CoprocessorError;
use crate::executor::coprocessor::stage::Stage;
use crate::executor::coprocessor::stages::graphql::{
    GraphqlAnalysisInput, GraphqlAnalysisStage, GraphqlRequestInput, GraphqlRequestStage,
    GraphqlResponseInput, GraphqlResponseStage,
};
use crate::executor::coprocessor::stages::router::{
    RouterRequestInput, RouterRequestStage, RouterResponseInput, RouterResponseStage,
};
use crate::executor::execution::plan::FailedExecutionResult;
use crate::executor::plugins::hooks::on_graphql_params::GraphQLParams;
use crate::executor::request_context::{
    RequestContextError, RequestContextExt, RequestContextPatch, SharedRequestContext,
};
use crate::executor::response::graphql_error::GraphQLError;

pub struct CoprocessorRuntime {
    router_request: Option<StageRuntime<RouterRequestStage>>,
    router_response: Option<StageRuntime<RouterResponseStage>>,
    graphql_request: Option<StageRuntime<GraphqlRequestStage>>,
    graphql_analysis: Option<StageRuntime<GraphqlAnalysisStage>>,
    graphql_response: Option<StageRuntime<GraphqlResponseStage>>,
    body_size_limit: usize,
}

#[derive(Default)]
pub struct PerformedMutations {
    pub body: bool,
    pub headers: bool,
    pub context: bool,
}

struct StageRuntime<S: Stage> {
    client: Arc<CoprocessorClient>,
    stage: S,
    telemetry_context: Arc<TelemetryContext>,
}

pub struct MutableRequestState<'a> {
    pub method: &'a HttpMethod,
    pub uri: &'a Uri,
    pub headers: &'a mut HeaderMap,
}

impl<A: Stage> StageRuntime<A> {
    fn new(
        client: Arc<CoprocessorClient>,
        stage: A,
        telemetry_context: Arc<TelemetryContext>,
    ) -> Self {
        Self {
            client,
            stage,
            telemetry_context,
        }
    }

    async fn execute<'a>(
        &self,
        input: &mut A::Input<'a>,
        shared_context: &SharedRequestContext,
    ) -> Result<ControlFlow<web::HttpResponse, PerformedMutations>, CoprocessorError> {
        let result = self.execute_internal(input, shared_context).await;

        if result.is_err() {
            let stage_name = self.stage.stage_name();
            let metrics = &self.telemetry_context.metrics.coprocessor;
            metrics.record_error(stage_name);
        }

        result
    }

    async fn execute_internal<'a>(
        &self,
        input: &mut A::Input<'a>,
        shared_context: &SharedRequestContext,
    ) -> Result<ControlFlow<web::HttpResponse, PerformedMutations>, CoprocessorError> {
        let mut performed_mutations = PerformedMutations::default();

        // Skip remote call when condition says stage should not run
        if !self.stage.should_run(input)? {
            return Ok(ControlFlow::Continue(performed_mutations));
        }

        let stage_name = self.stage.stage_name();
        let metrics = &self.telemetry_context.metrics.coprocessor;
        metrics.record_request(stage_name);

        let start = std::time::Instant::now();
        let id = uuid::Uuid::new_v4().to_string();
        let span = CoprocessorSpan::new(stage_name, &id).span;

        async {
          // Build stage payload and call the coprocessor
          // TODO: Include `request_id` in coprocessor payloads for log correlation
          //       once Dotan's logging PR is merged.
          let request = self.stage.build_request(input, &id, shared_context)?;
            debug!(
                target: targets::COPROCESSOR,
                coprocessor_id = %id,
                coprocessor_stage = stage_name,
                "Sending coprocessor request"
            );

            let response = self.client.send(request.body).await?;

            metrics.record_duration(stage_name, start.elapsed().as_secs_f64());

            if !response.status().is_success() {
                return Err(CoprocessorError::UnexpectedStatus(response.status()));
            }

            // Parse the response
            let mut parsed = self.stage.parse_response(response.body())?;

            if parsed.body.is_some() {
                performed_mutations.body = true;
            }
            if parsed.headers.is_some() {
                performed_mutations.headers = true;
            }
            // Handle possible break decision first
            match self.stage.break_output(parsed) {
                Ok(ControlFlow::Continue(p)) => {
                    parsed = p;
                }
                Ok(ControlFlow::Break(response)) => {
                    debug!(
                        target: targets::COPROCESSOR,
                        coprocessor_id = %id,
                        coprocessor_stage = stage_name,
                        status_code = %response.status(),
                        "Coprocessor short-circuited the request"
                    );

                    return Ok(ControlFlow::Break(response));
                }
                Err(err) => {
                    return Err(err);
                }
            }

            if let Some(context_patch_json) = parsed.context.take() {
                performed_mutations.context = true;
                let context_patch = A::parse_json_body(&context_patch_json)?;
                let patch: RequestContextPatch =
                    sonic_rs::from_str(context_patch.as_ref()).map_err(RequestContextError::Json)?;
                let mut context = shared_context.read_lock()?;
                context.for_coprocessor().apply_patch(patch)?;
            }

            if performed_mutations.body || performed_mutations.headers || performed_mutations.context {
                debug!(
                    target: targets::COPROCESSOR,
                    coprocessor_id = %id,
                    coprocessor_stage = stage_name,
                    body = performed_mutations.body,
                    headers = performed_mutations.headers,
                    context = performed_mutations.context,
                    "Coprocessor mutated the request"
                );
            }

            // Apply mutations only when flow continues
            self.stage.apply_mutations(parsed, input)?;

            // Continue the pipeline.
            Ok(ControlFlow::Continue(performed_mutations))
        }
        .instrument(span)
        .await
        .inspect_err(|err| {
            error!(target: targets::COPROCESSOR, error = %err, coprocessor_id = %id, coprocessor_stage = stage_name, "Coprocessor failure");
        })
    }
}

impl CoprocessorRuntime {
    pub fn from_config(
        config: &CoprocessorConfig,
        telemetry_context: Arc<TelemetryContext>,
        body_size_limit: usize,
    ) -> Result<Self, CoprocessorError> {
        let client = Arc::new(CoprocessorClient::new(
            config.clone(),
            telemetry_context.clone(),
        )?);

        let router_request = config
            .stages
            .router
            .request
            .as_ref()
            .map(RouterRequestStage::from_config)
            .transpose()?
            .map(|adapter| StageRuntime::new(client.clone(), adapter, telemetry_context.clone()));

        let router_response = config
            .stages
            .router
            .response
            .as_ref()
            .map(RouterResponseStage::from_config)
            .transpose()?
            .map(|adapter| StageRuntime::new(client.clone(), adapter, telemetry_context.clone()));

        let graphql_request = config
            .stages
            .graphql
            .request
            .as_ref()
            .map(GraphqlRequestStage::from_config)
            .transpose()?
            .map(|adapter| StageRuntime::new(client.clone(), adapter, telemetry_context.clone()));

        let graphql_response = config
            .stages
            .graphql
            .response
            .as_ref()
            .map(GraphqlResponseStage::from_config)
            .transpose()?
            .map(|adapter| StageRuntime::new(client.clone(), adapter, telemetry_context.clone()));

        let graphql_analysis = config
            .stages
            .graphql
            .analysis
            .as_ref()
            .map(GraphqlAnalysisStage::from_config)
            .transpose()?
            .map(|adapter| StageRuntime::new(client, adapter, telemetry_context));

        Ok(Self {
            router_request,
            router_response,
            graphql_request,
            graphql_analysis,
            graphql_response,
            body_size_limit,
        })
    }

    pub async fn on_router_request(
        &self,
        mut req: web::WebRequest<DefaultError>,
    ) -> ControlFlow<web::WebResponse, web::WebRequest<DefaultError>> {
        let Some(stage) = &self.router_request else {
            return ControlFlow::Continue(req);
        };

        // We read the request body only when this stage needs to include body
        let request_body = if stage.stage.include_body() {
            let body_stream = web::types::Payload(req.take_payload());
            let new_body = match read_body_stream(&req, body_stream, self.body_size_limit).await {
                Ok(body) => body,
                // We deliberately do not map to CoprocessorError here,
                // to follow the same logic for status codes
                Err(err) => {
                    error!(target: targets::COPROCESSOR, error = %err, coprocessor_stage = stage.stage.stage_name(), "coprocessor stage failed");

                    let response =
                        build_router_stage_error_response(err.status_code(), err.error_code());
                    return ControlFlow::Break(req.into_response(response));
                }
            };

            Some(new_body)
        } else {
            None
        };

        let shared_context = match req.read_request_context() {
            Ok(context) => context,
            Err(error) => {
                let error = CoprocessorError::from(error);
                let response =
                    build_router_stage_error_response(error.status_code(), error.error_code());
                return ControlFlow::Break(req.into_response(response));
            }
        };

        let mut input = RouterRequestInput::new(req, request_body);
        match stage
            .execute(&mut input, &shared_context)
            .await
            .unwrap_or_else(|err| {
            error!(target: targets::COPROCESSOR, error = %err, coprocessor_stage = stage.stage.stage_name(), "coprocessor stage failed");
            ControlFlow::Break(build_router_stage_error_response(
                err.status_code(),
                err.error_code(),
            ))
            }) {
            ControlFlow::Continue(_) => {
                // On continue, restore the original body only when coprocessor did not replace it
                input.restore_request_body_if_unchanged();
                ControlFlow::Continue(input.request)
            }
            ControlFlow::Break(response) => {
                // On break, we return immediately and skip body restoration to avoid unnecessary work
                ControlFlow::Break(input.request.into_response(response))
            }
        }
    }

    pub async fn on_router_response(&self, response: web::WebResponse) -> web::WebResponse {
        let Some(stage) = &self.router_response else {
            return response;
        };

        let shared_context = match response.request().read_request_context() {
            Ok(context) => context,
            Err(error) => {
                let error = CoprocessorError::from(error);
                let fallback =
                    build_router_stage_error_response(error.status_code(), error.error_code());
                return response.into_response(fallback);
            }
        };

        let mut input = RouterResponseInput::new(response);

        match stage
            .execute(&mut input, &shared_context)
            .await
            .unwrap_or_else(|err| error_to_break(stage, err))
        {
            ControlFlow::Continue(_) => input.response,
            ControlFlow::Break(response) => input.response.into_response(response),
        }
    }

    pub async fn on_graphql_request(
        &self,
        request: &web::HttpRequest,
        request_headers: &mut HeaderMap,
        graphql_request: &mut GraphQLParams,
        sdl_fn: impl FnOnce() -> Arc<str>,
    ) -> Result<ControlFlow<web::HttpResponse, PerformedMutations>, CoprocessorError> {
        let Some(stage) = &self.graphql_request else {
            return Ok(ControlFlow::Continue(Default::default()));
        };

        let shared_context = request.read_request_context()?;
        let sdl = stage.stage.include_sdl().then(sdl_fn);
        let mut input =
            GraphqlRequestInput::new(request, request_headers, graphql_request, sdl.as_deref());
        stage.execute(&mut input, &shared_context).await
    }

    pub async fn on_graphql_analysis(
        &self,
        request: MutableRequestState<'_>,
        graphql_request: &GraphQLParams,
        context: &SharedRequestContext,
        sdl_fn: impl FnOnce() -> Arc<str>,
    ) -> Result<ControlFlow<web::HttpResponse, PerformedMutations>, CoprocessorError> {
        let Some(stage) = &self.graphql_analysis else {
            return Ok(ControlFlow::Continue(Default::default()));
        };

        let sdl = stage.stage.include_sdl().then(sdl_fn);
        let mut input = GraphqlAnalysisInput::new(request, graphql_request, sdl.as_deref());
        stage.execute(&mut input, context).await
    }

    pub async fn on_graphql_response(
        &self,
        response: web::HttpResponse,
        request: &web::HttpRequest,
        // We return `Option<T>` instead of `T`, because of error handling of the caller.
        // If I would return `T`, the caller would have to be wrapped with duplicated
        // error handling logic.
        // With `Option<T>`, the coprocessor runtime handles the case where sdl is not available.
        sdl_fn: impl FnOnce() -> Option<Arc<str>>,
    ) -> Result<ControlFlow<web::HttpResponse, web::HttpResponse>, CoprocessorError> {
        let Some(stage) = &self.graphql_response else {
            return Ok(ControlFlow::Continue(response));
        };

        let sdl = stage.stage.include_sdl().then(sdl_fn).flatten();
        let shared_context = request.read_request_context()?;
        let mut input = GraphqlResponseInput::new(response, request, sdl.as_deref());
        Ok(stage
            .execute(&mut input, &shared_context)
            .await?
            .map_continue(|_| input.response))
    }
}

fn error_to_break<A: Stage>(
    stage: &StageRuntime<A>,
    err: CoprocessorError,
) -> ControlFlow<web::HttpResponse, PerformedMutations> {
    // Stage error metrics and specific logs are already recorded in execute() where possible,
    // but keeping a fallback log here correctly reports the failure that causes short-circuit
    // in case it's not caught earlier, and always breaks the request.
    error!(target: targets::COPROCESSOR, error = %err, coprocessor_stage = stage.stage.stage_name(), "coprocessor stage failed");
    ControlFlow::Break(web::HttpResponse::new(err.status_code()))
}

fn build_router_stage_error_response(
    status: http::StatusCode,
    code: &'static str,
) -> web::HttpResponse {
    let body = FailedExecutionResult {
        errors: vec![GraphQLError::from_message_and_code(
            "Internal server error",
            code,
        )],
    }
    .serialize();

    web::HttpResponse::build(status)
        .header(http::header::CONTENT_TYPE, "application/json")
        .body(body)
}