Line data Source code
1 : use std::sync::Arc;
2 :
3 : use axum::extract::State;
4 : use axum::response::Response;
5 : use compute_api::requests::ConfigurationRequest;
6 : use compute_api::responses::{ComputeStatus, ComputeStatusResponse};
7 : use http::StatusCode;
8 : use tokio::task;
9 : use tracing::info;
10 :
11 : use crate::compute::{ComputeNode, ParsedSpec};
12 : use crate::http::JsonResponse;
13 : use crate::http::extract::Json;
14 :
15 : // Accept spec in JSON format and request compute configuration. If anything
16 : // goes wrong after we set the compute status to `ConfigurationPending` and
17 : // update compute state with new spec, we basically leave compute in the
18 : // potentially wrong state. That said, it's control-plane's responsibility to
19 : // watch compute state after reconfiguration request and to clean restart in
20 : // case of errors.
21 0 : pub(in crate::http) async fn configure(
22 0 : State(compute): State<Arc<ComputeNode>>,
23 0 : request: Json<ConfigurationRequest>,
24 0 : ) -> Response {
25 0 : let pspec = match ParsedSpec::try_from(request.0.spec) {
26 0 : Ok(p) => p,
27 0 : Err(e) => return JsonResponse::error(StatusCode::BAD_REQUEST, e),
28 : };
29 :
30 : // XXX: wrap state update under lock in a code block. Otherwise, we will try
31 : // to `Send` `mut state` into the spawned thread bellow, which will cause
32 : // the following rustc error:
33 : //
34 : // error: future cannot be sent between threads safely
35 : {
36 0 : let mut state = compute.state.lock().unwrap();
37 0 : if !matches!(state.status, ComputeStatus::Empty | ComputeStatus::Running) {
38 0 : return JsonResponse::invalid_status(state.status);
39 0 : }
40 :
41 : // Pass the tracing span to the main thread that performs the startup,
42 : // so that the start_compute operation is considered a child of this
43 : // configure request for tracing purposes.
44 0 : state.startup_span = Some(tracing::Span::current());
45 :
46 0 : if compute.params.lakebase_mode {
47 0 : ComputeNode::set_spec(&compute.params, &mut state, pspec);
48 0 : } else {
49 0 : state.pspec = Some(pspec);
50 0 : }
51 :
52 0 : state.set_status(ComputeStatus::ConfigurationPending, &compute.state_changed);
53 0 : drop(state);
54 : }
55 :
56 : // Spawn a blocking thread to wait for compute to become Running. This is
57 : // needed to not block the main pool of workers and to be able to serve
58 : // other requests while some particular request is waiting for compute to
59 : // finish configuration.
60 0 : let c = compute.clone();
61 0 : let completed = task::spawn_blocking(move || {
62 0 : let mut state = c.state.lock().unwrap();
63 0 : while state.status != ComputeStatus::Running {
64 0 : state = c.state_changed.wait(state).unwrap();
65 0 : info!(
66 0 : "waiting for compute to become {}, current status: {}",
67 : ComputeStatus::Running,
68 0 : state.status
69 : );
70 :
71 0 : if state.status == ComputeStatus::Failed {
72 0 : let err = state.error.as_ref().map_or("unknown error", |x| x);
73 0 : let msg = format!("compute configuration failed: {err:?}");
74 0 : return Err(msg);
75 0 : }
76 : }
77 :
78 0 : Ok(())
79 0 : })
80 0 : .await
81 0 : .unwrap();
82 :
83 0 : if let Err(e) = completed {
84 0 : return JsonResponse::error(StatusCode::INTERNAL_SERVER_ERROR, e);
85 0 : }
86 :
87 : // Return current compute state if everything went well.
88 0 : let state = compute.state.lock().unwrap().clone();
89 0 : let body = ComputeStatusResponse::from(&state);
90 :
91 0 : JsonResponse::success(StatusCode::OK, body)
92 0 : }
|