blob: 42216703c91b495f1feb1f6d48683a57d4974a26 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
//! Safe diagnostic projections over original canonical Agent facts only.
use crate::store::invalid;
use maka_agent_facts::{diagnostics::DiagnosticDetail, durable::*};
use maka_event_log::{
StoreError,
connection::Query,
root_store::DataWriter,
writeback::{Notification, Prepared, Statement},
};
use rusqlite::Connection;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize, de::DeserializeOwned};
mod bootstrap;
mod index;
mod read;
pub(crate) use bootstrap::ensure_owner;
pub(crate) use index::index_fact;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DiagnosticEntry {
pub sequence: u64,
pub event_id: FactId,
pub recorded_at: UnixMillis,
pub detail: DiagnosticDetail,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DiagnosticPage {
pub through: u64,
pub next_before: Option<u64>,
pub entries: Vec<DiagnosticEntry>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DiagnosticHead {
pub through: u64,
pub latest: Option<u64>,
}
#[derive(Clone)]
struct Scope {
owner: RunOwner,
session: String,
}
#[derive(Clone)]
pub struct DiagnosticsStore {
data: DataWriter,
scope: Scope,
}
impl DiagnosticsStore {
pub fn new(data: DataWriter, owner: RunOwner, session: String) -> Result<Self, StoreError> {
maka_agent_facts::interaction::entity_id(&session).map_err(invalid)?;
if owner.deployment.is_nil() {
return Err(invalid("nil diagnostics deployment"));
}
owner
.namespace
.validate()
.map_err(|error| invalid(error.to_string()))?;
Ok(Self {
data,
scope: Scope { owner, session },
})
}
pub async fn page(
&self,
through: Option<u64>,
before: Option<u64>,
limit: u32,
max_bytes: usize,
) -> Result<DiagnosticPage, StoreError> {
if !(1..=128).contains(&limit) || max_bytes == 0 {
return Err(invalid("invalid diagnostic page limit or JSON budget"));
}
let query = ReadPage {
scope: self.scope.clone(),
through,
before,
limit,
max_bytes,
};
if let Some(page) = self.data.query_barrier(TryReadPage(query.clone())).await? {
return Ok(page);
}
Box::pin(self.data.write(CatchUpAndPage(query))).await
}
pub async fn head(&self) -> Result<DiagnosticHead, StoreError> {
if let Some(head) = self
.data
.query_barrier(TryReadHead(self.scope.clone()))
.await?
{
return Ok(head);
}
Box::pin(self.data.write(CatchUpAndHead(self.scope.clone()))).await
}
pub async fn rebuild(&self) -> Result<(), StoreError> {
self.data.write(Rebuild(self.scope.clone())).await
}
}
#[derive(Clone)]
struct ReadPage {
scope: Scope,
through: Option<u64>,
before: Option<u64>,
limit: u32,
max_bytes: usize,
}
struct TryReadPage(ReadPage);
impl Query for TryReadPage {
type Output = Option<DiagnosticPage>;
fn execute(self, c: &Connection) -> Result<Self::Output, StoreError> {
read::try_page(c, self.0)
}
}
struct TryReadHead(Scope);
impl Query for TryReadHead {
type Output = Option<DiagnosticHead>;
fn execute(self, c: &Connection) -> Result<Self::Output, StoreError> {
read::try_head(c, self.0)
}
}
struct CatchUpAndPage(ReadPage);
impl Statement for CatchUpAndPage {
type Value = DiagnosticPage;
type Output = DiagnosticPage;
fn execute(&self, c: &Connection) -> Result<Prepared<Self::Value>, StoreError> {
let changed = index::catch_up_scope(c, &self.0.scope)?;
let page = read::page(c, self.0.clone())?;
Ok(Prepared {
value: page,
changed,
notification: Notification::NONE,
})
}
fn committed(value: Self::Value) -> Self::Output {
value
}
fn isolated_rejection(error: &StoreError) -> bool {
matches!(
error,
StoreError::InvalidTransition(_) | StoreError::EventConflict
)
}
}
struct CatchUpAndHead(Scope);
impl Statement for CatchUpAndHead {
type Value = DiagnosticHead;
type Output = DiagnosticHead;
fn execute(&self, c: &Connection) -> Result<Prepared<Self::Value>, StoreError> {
let changed = index::catch_up_scope(c, &self.0)?;
let head = read::head(c, self.0.clone())?;
Ok(Prepared {
value: head,
changed,
notification: Notification::NONE,
})
}
fn committed(value: Self::Value) -> Self::Output {
value
}
fn isolated_rejection(error: &StoreError) -> bool {
matches!(
error,
StoreError::InvalidTransition(_) | StoreError::EventConflict
)
}
}
struct Rebuild(Scope);
impl Statement for Rebuild {
type Value = ();
type Output = ();
fn execute(&self, c: &Connection) -> Result<Prepared<()>, StoreError> {
index::rebuild(c, &self.0)?;
Ok(Prepared {
value: (),
changed: true,
notification: Notification::METADATA.with_sessions([self.0.session.clone()]),
})
}
fn committed(_: ()) {}
fn isolated_rejection(error: &StoreError) -> bool {
matches!(
error,
StoreError::InvalidTransition(_) | StoreError::EventConflict
)
}
}
fn signed(value: u64) -> Result<i64, StoreError> {
i64::try_from(value).map_err(|_| invalid("diagnostic sequence exceeds SQLite range"))
}
fn unsigned(value: i64) -> Result<u64, StoreError> {
u64::try_from(value).map_err(|_| invalid("negative diagnostic source number"))
}
fn identity<T: DeserializeOwned>(value: String) -> Result<T, StoreError> {
Ok(serde_json::from_value(serde_json::Value::String(value))?)
}