diff --git a/compute_tools/src/compute.rs b/compute_tools/src/compute.rs index dca6becd39..da5ad00da6 100644 --- a/compute_tools/src/compute.rs +++ b/compute_tools/src/compute.rs @@ -67,8 +67,9 @@ pub struct ComputeNode { pub struct ComputeState { pub start_time: DateTime, pub status: ComputeStatus, - /// Timestamp of the last Postgres activity - pub last_active: DateTime, + /// Timestamp of the last Postgres activity. It could be `None` if + /// compute wasn't used since start. + pub last_active: Option>, pub error: Option, pub pspec: Option, pub metrics: ComputeMetrics, @@ -79,7 +80,7 @@ impl ComputeState { Self { start_time: Utc::now(), status: ComputeStatus::Empty, - last_active: Utc::now(), + last_active: None, error: None, pspec: None, metrics: ComputeMetrics::default(), diff --git a/compute_tools/src/http/openapi_spec.yaml b/compute_tools/src/http/openapi_spec.yaml index cc8f074a50..2680269756 100644 --- a/compute_tools/src/http/openapi_spec.yaml +++ b/compute_tools/src/http/openapi_spec.yaml @@ -181,8 +181,8 @@ components: ComputeState: type: object required: + - start_time - status - - last_active properties: start_time: type: string @@ -195,11 +195,13 @@ components: $ref: '#/components/schemas/ComputeStatus' last_active: type: string - description: The last detected compute activity timestamp in UTC and RFC3339 format. + description: | + The last detected compute activity timestamp in UTC and RFC3339 format. + It could be empty if compute was never used by user since start. example: "2022-10-12T07:20:50.52Z" error: type: string - description: Text of the error during compute startup, if any. + description: Text of the error during compute startup or reconfiguration, if any. example: "" tenant: type: string @@ -222,9 +224,12 @@ components: ComputeStatus: type: string enum: + - empty - init - failed - running + - configuration_pending + - configuration example: running # diff --git a/compute_tools/src/monitor.rs b/compute_tools/src/monitor.rs index a30b52aed4..d2e7b698dd 100644 --- a/compute_tools/src/monitor.rs +++ b/compute_tools/src/monitor.rs @@ -74,7 +74,7 @@ fn watch_compute_activity(compute: &ComputeNode) { // Found non-idle backend, so the last activity is NOW. // Save it and exit the for loop. Also clear the idle backend // `state_change` timestamps array as it doesn't matter now. - last_active = Utc::now(); + last_active = Some(Utc::now()); idle_backs.clear(); break; } @@ -82,15 +82,16 @@ fn watch_compute_activity(compute: &ComputeNode) { // Get idle backend `state_change` with the max timestamp. if let Some(last) = idle_backs.iter().max() { - last_active = *last; + last_active = Some(*last); } } // Update the last activity in the shared state if we got a more recent one. let mut state = compute.state.lock().unwrap(); + // NB: `Some()` is always greater than `None`. if last_active > state.last_active { state.last_active = last_active; - debug!("set the last compute activity time to: {}", last_active); + debug!("set the last compute activity time to: {:?}", last_active); } } Err(e) => { diff --git a/libs/compute_api/src/responses.rs b/libs/compute_api/src/responses.rs index c409563b56..d181c018b1 100644 --- a/libs/compute_api/src/responses.rs +++ b/libs/compute_api/src/responses.rs @@ -19,7 +19,7 @@ pub struct ComputeStatusResponse { pub timeline: Option, pub status: ComputeStatus, #[serde(serialize_with = "rfc3339_serialize")] - pub last_active: DateTime, + pub last_active: Option>, pub error: Option, } @@ -29,7 +29,7 @@ pub struct ComputeState { pub status: ComputeStatus, /// Timestamp of the last Postgres activity #[serde(serialize_with = "rfc3339_serialize")] - pub last_active: DateTime, + pub last_active: Option>, pub error: Option, } @@ -54,11 +54,15 @@ pub enum ComputeStatus { Failed, } -fn rfc3339_serialize(x: &DateTime, s: S) -> Result +fn rfc3339_serialize(x: &Option>, s: S) -> Result where S: Serializer, { - x.to_rfc3339().serialize(s) + if let Some(x) = x { + x.to_rfc3339().serialize(s) + } else { + s.serialize_none() + } } /// Response of the /metrics.json API