nopace-postgres 0.3.0

Transactional PostgreSQL store for nopace
Documentation
use std::sync::Arc;

use nopace_core::{
    AdmissionRequest, Completion, CompletionResult, LeaseRenewal, LeaseRenewalResult, PartitionKey,
    PolicyId, ResetReport, Retention, ScopeKey, ScopeState, Store, StoreDecision, StorePeek,
    StoreSnapshot, collect_at, decide_at, finish_at, peek_at, renew_at, reset_policy, snapshot_at,
};
use thiserror::Error;
use tokio::sync::Mutex;
use tokio_postgres::{Client, NoTls};

#[derive(Clone)]
pub struct PostgresStore {
    client: Arc<Mutex<Client>>,
    retention: Retention,
}

impl PostgresStore {
    pub async fn connect(connection_string: &str) -> Result<Self, PostgresError> {
        let (client, connection) = tokio_postgres::connect(connection_string, NoTls).await?;
        tokio::spawn(async move {
            if let Err(error) = connection.await {
                eprintln!("nopace postgres connection failed: {error}");
            }
        });
        Self::from_client(client).await
    }

    pub async fn from_client(mut client: Client) -> Result<Self, PostgresError> {
        let transaction = client.transaction().await?;
        transaction
            .batch_execute(
                "SELECT pg_advisory_xact_lock(6934058159269381729); \
                 CREATE TABLE IF NOT EXISTS nopace_scope_state_v1 ( \
                     scope TEXT PRIMARY KEY, \
                     state BYTEA NOT NULL, \
                     updated_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp() \
                 )",
            )
            .await?;
        transaction.commit().await?;
        Ok(Self {
            client: Arc::new(Mutex::new(client)),
            retention: Retention::default(),
        })
    }

    pub fn with_retention(mut self, retention: Retention) -> Self {
        self.retention = retention;
        self
    }

    pub async fn collect(&self) -> Result<u64, PostgresError> {
        let mut client = self.client.lock().await;
        let transaction = client.transaction().await?;
        let rows = transaction.query(
            "SELECT scope, state, floor(extract(epoch FROM clock_timestamp()) * 1000)::bigint AS now_ms FROM nopace_scope_state_v1 FOR UPDATE",
            &[],
        ).await?;
        let mut changed = 0;
        for row in rows {
            let scope: String = row.get("scope");
            let bytes: Vec<u8> = row.get("state");
            let now: i64 = row.get("now_ms");
            let mut state: ScopeState = bincode::deserialize(&bytes)?;
            collect_at(&mut state, now.max(0) as u64, self.retention);
            let bytes = bincode::serialize(&state)?;
            transaction.execute(
                "UPDATE nopace_scope_state_v1 SET state = $2, updated_at = clock_timestamp() WHERE scope = $1",
                &[&scope, &bytes],
            ).await?;
            changed += 1;
        }
        transaction.commit().await?;
        Ok(changed)
    }

    pub async fn snapshot(&self, scope: &ScopeKey) -> Result<Option<StoreSnapshot>, PostgresError> {
        let client = self.client.lock().await;
        let row = client
            .query_opt(
                "SELECT state FROM nopace_scope_state_v1 WHERE scope = $1",
                &[&scope.as_str()],
            )
            .await?;
        row.map(|row| {
            bincode::deserialize::<ScopeState>(&row.get::<_, Vec<u8>>("state"))
                .map(|state| snapshot_at(&state))
        })
        .transpose()
        .map_err(PostgresError::from)
    }

    pub async fn reset_policy(
        &self,
        scope: &ScopeKey,
        policy: &PolicyId,
        partition: Option<&PartitionKey>,
    ) -> Result<ResetReport, PostgresError> {
        self.transaction(scope, |state, _| reset_policy(state, policy, partition))
            .await
    }

    async fn transaction<T>(
        &self,
        scope: &ScopeKey,
        operation: impl FnOnce(&mut ScopeState, u64) -> T,
    ) -> Result<T, PostgresError> {
        let mut client = self.client.lock().await;
        let transaction = client.transaction().await?;
        let empty = bincode::serialize(&ScopeState::default())?;
        let row = transaction
            .query_one(
                r#"
            INSERT INTO nopace_scope_state_v1(scope, state)
            VALUES ($1, $2)
            ON CONFLICT (scope) DO UPDATE SET scope = EXCLUDED.scope
            RETURNING state, floor(extract(epoch FROM clock_timestamp()) * 1000)::bigint AS now_ms
            "#,
                &[&scope.as_str(), &empty],
            )
            .await?;
        let bytes: Vec<u8> = row.get("state");
        let now: i64 = row.get("now_ms");
        let mut state: ScopeState = bincode::deserialize(&bytes)?;
        let result = operation(&mut state, now.max(0) as u64);
        let bytes = bincode::serialize(&state)?;
        transaction.execute(
            "UPDATE nopace_scope_state_v1 SET state = $2, updated_at = clock_timestamp() WHERE scope = $1",
            &[&scope.as_str(), &bytes],
        ).await?;
        transaction.commit().await?;
        Ok(result)
    }
}

impl Store for PostgresStore {
    type Error = PostgresError;

    async fn decide(&self, request: AdmissionRequest) -> Result<StoreDecision, Self::Error> {
        let scope = request.scope.clone();
        self.transaction(&scope, |state, now| {
            decide_at(state, &request, now, self.retention)
        })
        .await
    }

    async fn peek(&self, request: AdmissionRequest) -> Result<StorePeek, Self::Error> {
        let client = self.client.lock().await;
        let row = client
            .query_opt(
                "SELECT state, floor(extract(epoch FROM clock_timestamp()) * 1000)::bigint AS now_ms FROM nopace_scope_state_v1 WHERE scope = $1",
                &[&request.scope.as_str()],
            )
            .await?;
        let (state, now) = match row {
            Some(row) => (
                bincode::deserialize::<ScopeState>(&row.get::<_, Vec<u8>>("state"))?,
                row.get::<_, i64>("now_ms").max(0) as u64,
            ),
            None => (ScopeState::default(), nopace_core::unix_millis()),
        };
        Ok(peek_at(&state, &request, now))
    }

    async fn renew(&self, renewal: LeaseRenewal) -> Result<LeaseRenewalResult, Self::Error> {
        let scope = renewal.scope.clone();
        self.transaction(&scope, |state, now| {
            renew_at(state, &renewal, now, self.retention)
        })
        .await
    }

    async fn finish(&self, completion: Completion) -> Result<CompletionResult, Self::Error> {
        let scope = completion.scope.clone();
        self.transaction(&scope, |state, now| {
            finish_at(state, &completion, now, self.retention)
        })
        .await
    }
}

#[derive(Debug, Error)]
pub enum PostgresError {
    #[error("postgres operation failed: {0}")]
    Postgres(#[from] tokio_postgres::Error),
    #[error("stored nopace state is invalid: {0}")]
    Serialization(#[from] bincode::Error),
}