use async_trait::async_trait;
use chrono::NaiveDate;
use mongodb::bson::{Bson, Document, doc};
use mongodb::error::{ErrorKind, WriteFailure};
use mongodb::options::{ClientOptions, FindOptions, IndexOptions};
use mongodb::{Client, Collection, Cursor, Database, IndexModel};
use super::{
ProcessFilter, ProcessStore, SpendScope, UsageTotals, counters, day_number, stored_amount,
stored_micro_usd, stored_total,
};
use crate::bus::SpendDelta;
use crate::error::{KernelError, Result};
use crate::partition::PartitionId;
use crate::process::ProcessMeta;
use crate::sched::QueueClass;
use crate::wake::WakeSet;
const PROCESSES: &str = "adk_kernel_processes";
const SPEND: &str = "adk_kernel_spend";
pub(super) const BACKEND: &str = "mongodb";
const DUPLICATE_KEY: i32 = 11000;
const UPSERT_ATTEMPTS: u32 = 5;
pub(super) fn store_error(action: &str, error: &dyn std::fmt::Display) -> KernelError {
KernelError::Store { backend: BACKEND, reason: format!("could not {action}: {error}") }
}
pub(super) async fn connect(connection_string: &str, database_name: &str) -> Result<Database> {
let options = ClientOptions::parse(connection_string)
.await
.map_err(|error| store_error("parse the MongoDB connection string", &error))?;
let client = Client::with_options(options)
.map_err(|error| store_error("build the MongoDB client", &error))?;
Ok(client.database(database_name))
}
pub(super) async fn create_indexes(
collection: &Collection<Document>,
indexes: &[(&str, Document)],
) -> Result<()> {
for (name, keys) in indexes {
let model = IndexModel::builder()
.keys(keys.clone())
.options(IndexOptions::builder().name((*name).to_string()).build())
.build();
collection
.create_index(model)
.await
.map_err(|error| store_error(&format!("create the index {name}"), &error))?;
}
Ok(())
}
pub(super) async fn collect(mut cursor: Cursor<Document>, action: &str) -> Result<Vec<Document>> {
let mut documents = Vec::new();
while cursor.advance().await.map_err(|error| store_error(action, &error))? {
documents.push(cursor.deserialize_current().map_err(|error| store_error(action, &error))?);
}
Ok(documents)
}
pub(super) fn text<'a>(document: &'a Document, field: &str) -> Result<&'a str> {
document.get_str(field).map_err(|_| KernelError::Store {
backend: BACKEND,
reason: format!("a stored document has no text field `{field}`"),
})
}
pub(super) fn optional_text(document: &Document, field: &str) -> Option<String> {
document.get_str(field).ok().map(str::to_string)
}
pub(super) fn read_i64(document: &Document, field: &str) -> Option<i64> {
match document.get(field)? {
Bson::Int32(value) => Some(i64::from(*value)),
Bson::Int64(value) => Some(*value),
Bson::Double(value) => Some(*value as i64),
_ => None,
}
}
pub(super) fn json<T: serde::de::DeserializeOwned>(what: &str, text: &str) -> Result<T> {
serde_json::from_str(text).map_err(|error| KernelError::Store {
backend: BACKEND,
reason: format!("a stored {what} is corrupt: {error}"),
})
}
pub(super) fn find_limit(limit: Option<usize>) -> Option<i64> {
limit.map(|limit| i64::try_from(limit).unwrap_or(i64::MAX))
}
pub(super) fn sorted(sort: Document, limit: Option<usize>) -> FindOptions {
FindOptions::builder().sort(sort).limit(find_limit(limit)).build()
}
pub(super) fn is_duplicate_key(error: &mongodb::error::Error) -> bool {
match error.kind.as_ref() {
ErrorKind::Write(WriteFailure::WriteError(write)) => write.code == DUPLICATE_KEY,
ErrorKind::Command(command) => command.code == DUPLICATE_KEY,
_ => false,
}
}
pub(super) async fn retry_upsert<T, F, Fut>(action: &str, mut upsert: F) -> Result<T>
where
F: FnMut() -> Fut,
Fut: std::future::IntoFuture<Output = mongodb::error::Result<T>>,
{
let mut attempt = 1;
loop {
match upsert().await {
Err(error) if is_duplicate_key(&error) && attempt < UPSERT_ATTEMPTS => attempt += 1,
result => return result.map_err(|error| store_error(action, &error)),
}
}
}
#[derive(Debug, Clone)]
pub struct MongoProcessStore {
db: Database,
}
impl MongoProcessStore {
pub fn from_database(db: Database) -> Self {
Self { db }
}
pub async fn connect(connection_string: &str, database_name: &str) -> Result<Self> {
Ok(Self::from_database(connect(connection_string, database_name).await?))
}
pub fn database(&self) -> &Database {
&self.db
}
pub async fn migrate(&self) -> Result<()> {
create_indexes(
&self.processes(),
&[
("adk_kernel_processes_partition", doc! { "partition": 1, "_id": 1 }),
("adk_kernel_processes_queue", doc! { "queue": 1, "_id": 1 }),
("adk_kernel_processes_parent", doc! { "parent": 1, "_id": 1 }),
("adk_kernel_processes_suspend", doc! { "suspend_requested": 1, "_id": 1 }),
],
)
.await?;
create_indexes(
&self.spend_totals(),
&[
("adk_kernel_spend_pid", doc! { "pid": 1, "day": 1 }),
("adk_kernel_spend_partition", doc! { "partition": 1, "day": 1 }),
],
)
.await
}
fn processes(&self) -> Collection<Document> {
self.db.collection(PROCESSES)
}
fn spend_totals(&self) -> Collection<Document> {
self.db.collection(SPEND)
}
async fn update(&self, pid: &str, update: Document) -> Result<()> {
let result = self
.processes()
.update_one(doc! { "_id": pid }, update)
.await
.map_err(|error| store_error("update the process table", &error))?;
if result.matched_count == 0 {
return Err(KernelError::NotFound { pid: pid.into() });
}
Ok(())
}
}
fn encode(meta: &ProcessMeta) -> Result<Document> {
let mut document = doc! {
"_id": &meta.pid,
"partition": meta.partition.as_str(),
"queue": meta.queue.as_str(),
"kind": serde_json::to_string(&meta.kind)?,
"labels": serde_json::to_string(&meta.labels)?,
"suspend_requested": meta.suspend_requested,
};
if let Some(parent) = &meta.parent {
document.insert("parent", parent);
}
if let Some(wake) = &meta.wake {
document.insert("wake", serde_json::to_string(wake)?);
}
if let Some(suspension) = &meta.suspended_by {
document.insert("suspended_by", serde_json::to_string(suspension)?);
}
if let Some(item) = &meta.agenda_item {
document.insert("agenda_item", item);
}
Ok(document)
}
fn decode(document: &Document) -> Result<ProcessMeta> {
let pid = text(document, "_id")?.to_string();
let structured = |field: &str| optional_text(document, field);
Ok(ProcessMeta {
parent: optional_text(document, "parent"),
partition: PartitionId::new(text(document, "partition")?),
queue: QueueClass::from_name(text(document, "queue")?),
kind: json("process kind", text(document, "kind")?)?,
wake: structured("wake").map(|wake| json("wake set", &wake)).transpose()?,
suspended_by: structured("suspended_by")
.map(|suspension| json("suspension", &suspension))
.transpose()?,
suspend_requested: document.get_bool("suspend_requested").unwrap_or(false),
agenda_item: optional_text(document, "agenda_item"),
labels: json("label set", text(document, "labels")?)?,
pid,
})
}
#[async_trait]
impl ProcessStore for MongoProcessStore {
async fn put(&self, meta: ProcessMeta) -> Result<()> {
let document = encode(&meta)?;
let processes = self.processes();
retry_upsert("write the process table", || {
processes.replace_one(doc! { "_id": &meta.pid }, document.clone()).upsert(true)
})
.await?;
Ok(())
}
async fn get(&self, pid: &str) -> Result<Option<ProcessMeta>> {
let document = self
.processes()
.find_one(doc! { "_id": pid })
.await
.map_err(|error| store_error("read the process table", &error))?;
document.as_ref().map(decode).transpose()
}
async fn list(&self, filter: &ProcessFilter) -> Result<Vec<ProcessMeta>> {
if filter.limit == Some(0) {
return Ok(Vec::new());
}
let mut query = Document::new();
if let Some(partition) = &filter.partition {
query.insert("partition", partition.as_str());
}
if let Some(queue) = &filter.queue {
query.insert("queue", queue.as_str());
}
if let Some(parent) = &filter.parent {
query.insert("parent", parent);
}
if let Some(requested) = filter.suspend_requested {
query.insert("suspend_requested", requested);
}
let cursor = self
.processes()
.find(query)
.with_options(sorted(doc! { "_id": 1 }, filter.limit))
.await
.map_err(|error| store_error("list processes", &error))?;
collect(cursor, "list processes").await?.iter().map(decode).collect()
}
async fn set_wake(&self, pid: &str, wake: Option<WakeSet>) -> Result<()> {
let update = match &wake {
Some(wake) => doc! { "$set": { "wake": serde_json::to_string(wake)? } },
None => doc! { "$unset": { "wake": "" } },
};
self.update(pid, update).await
}
async fn set_queue(&self, pid: &str, queue: QueueClass) -> Result<()> {
self.update(pid, doc! { "$set": { "queue": queue.as_str() } }).await
}
async fn delete(&self, pid: &str) -> Result<bool> {
let result = self
.processes()
.delete_one(doc! { "_id": pid })
.await
.map_err(|error| store_error("delete from the process table", &error))?;
Ok(result.deleted_count == 1)
}
async fn set_suspend_requested(&self, pid: &str, requested: bool) -> Result<()> {
self.update(pid, doc! { "$set": { "suspend_requested": requested } }).await
}
async fn add_spend(
&self,
pid: &str,
partition: &PartitionId,
day: NaiveDate,
delta: SpendDelta,
) -> Result<()> {
let mut increments = doc! { "micro_usd": stored_micro_usd(BACKEND, &delta)? };
for (name, amount) in counters(&delta) {
increments.insert(format!("counters.{name}"), stored_amount(BACKEND, name, amount)?);
}
let day = day_number(day);
let key = doc! { "pid": pid, "partition": partition.as_str(), "day": day };
let update = doc! {
"$inc": increments,
"$setOnInsert": { "pid": pid, "partition": partition.as_str(), "day": day },
};
let totals = self.spend_totals();
retry_upsert("add to the spend totals", || {
totals.update_one(doc! { "_id": key.clone() }, update.clone()).upsert(true)
})
.await?;
Ok(())
}
async fn spend(
&self,
scope: &SpendScope,
from: NaiveDate,
to_inclusive: NaiveDate,
) -> Result<u64> {
let mut filter = match scope {
SpendScope::Process(pid) => doc! { "pid": pid },
SpendScope::Partition(partition) => doc! { "partition": partition.as_str() },
};
filter.insert("day", doc! { "$gte": day_number(from), "$lte": day_number(to_inclusive) });
let pipeline = [
doc! { "$match": filter },
doc! { "$group": { "_id": Bson::Null, "total": { "$sum": "$micro_usd" } } },
];
let cursor = self
.spend_totals()
.aggregate(pipeline)
.await
.map_err(|error| store_error("sum the spend totals", &error))?;
let rows = collect(cursor, "sum the spend totals").await?;
let sum = rows.first().and_then(|row| read_i64(row, "total")).unwrap_or(0);
stored_total(BACKEND, sum)
}
async fn usage(
&self,
scope: &SpendScope,
from: NaiveDate,
to_inclusive: NaiveDate,
) -> Result<UsageTotals> {
let mut filter = match scope {
SpendScope::Process(pid) => doc! { "pid": pid },
SpendScope::Partition(partition) => doc! { "partition": partition.as_str() },
};
filter.insert("day", doc! { "$gte": day_number(from), "$lte": day_number(to_inclusive) });
let cursor = self
.spend_totals()
.find(filter)
.await
.map_err(|error| store_error("read the usage totals", &error))?;
let mut totals = UsageTotals::default();
for row in collect(cursor, "read the usage totals").await? {
let micro_usd = stored_total(BACKEND, read_i64(&row, "micro_usd").unwrap_or(0))?;
totals.micro_usd = totals.micro_usd.saturating_add(micro_usd);
let Ok(counters) = row.get_document("counters") else { continue };
for name in counters.keys() {
let amount = stored_total(BACKEND, read_i64(counters, name).unwrap_or(0))?;
totals.add_counter(name, amount);
}
}
Ok(totals)
}
}