algeon-query-runtime 0.1.0

Native query runtime, lifecycle, ledger, and observation owners.
Documentation
//! Grant-backed allocator domains.
//!
//! Attempt grants are immutable, so allocator domains are created once with
//! `grant_bytes == ceiling_bytes == grant` and never resized.  Construction is
//! intentionally one-shot: the admission envelope owns the hard domain and
//! passes that value to every derived context.  In particular, this module
//! does not keep process-global weak registries or identity-based creation
//! locks to rediscover a resource after ownership has been established.

use std::sync::Arc;

use query_planning::error::{QueryError, QueryErrorCode, Result};

use super::MemoryGrant;
use crate::native_error::cudf_query_error;

/// Creates the authoritative hard allocation domain for one admitted attempt.
///
/// The domain grant and ceiling equal the immutable admission grant. The
/// caller retains this domain through the admission envelope and shares it
/// with derived contexts.
pub(super) fn create_grant_memory_domain(
    runtime: &cudf::Runtime,
    grant: &MemoryGrant,
) -> Result<Arc<cudf::QueryMemoryDomain>> {
    let grant_bytes = grant_budget_usize(grant, "query_engine.runtime.grant_memory_domain")?;
    let manager = runtime.device_memory_manager().map_err(|error| {
        cudf_query_error(
            "query_engine.runtime.grant_memory_domain",
            "failed to resolve device memory manager for grant query memory domain",
            error,
        )
        .with_fact("grant_id", grant.grant_id())
        .with_fact("admission_request_id", grant.admission_request_id())
    })?;
    let domain = manager
        .query_memory_domain(grant.grant_id(), cudf::QueryMemoryDomainOptions::new(grant_bytes))
        .map_err(|error| {
            cudf_query_error(
                "query_engine.runtime.grant_memory_domain",
                "failed to create grant query memory domain",
                error,
            )
            .with_fact("grant_id", grant.grant_id())
            .with_fact("admission_request_id", grant.admission_request_id())
            .with_fact("budget_bytes", grant.budget_bytes())
        })?;
    Ok(Arc::new(domain))
}

fn grant_budget_usize(grant: &MemoryGrant, operation: &'static str) -> Result<usize> {
    usize::try_from(grant.budget_bytes()).map_err(|_error| {
        QueryError::new(QueryErrorCode::InvalidPlan, operation, "memory grant budget exceeds platform usize range")
            .with_fact("grant_id", grant.grant_id())
            .with_fact("admission_request_id", grant.admission_request_id())
            .with_fact("budget_bytes", grant.budget_bytes())
    })
}