use std::sync::{Arc, Mutex};
use type_bridge_contract::diagnostic::{Diagnostic, DiagnosticCategory, DiagnosticCode};
use type_bridge_contract::limits::MAX_REMOTE_ENVELOPE_BYTES;
use type_bridge_contract::query_remote::RemoteCapabilities;
use type_bridge_contract::sdk_diagnostic::SdkExecutionDiagnostic;
use crate::_registry::DescriptorRegistry;
use crate::error::OrmError;
use crate::execution_diagnostic::lower_remote_query_diagnostic;
use crate::match_request::{ValidatedMatchRequest, ValidatedMatchResult};
use crate::query_execution_limits::{QueryExecutionDeadline, QueryExecutionResourceLimits};
use crate::query_v2_model_remote::{
PendingRemoteModelQueryV2, RemoteModelQueryV2Error, prepare_remote_model_query_v2_with_budget,
};
use crate::query_v2_prepared::QueryAuthority;
use crate::session::backend::AnswerCancellation;
use crate::session::staged::{Transaction, TransactionOptions};
use crate::session::staged_proxy::ProxyStaging;
use crate::session::{
Database, ProxyEndpoint, ProxyHttpTransport, StagedExchangeFn, TransactionContext,
};
use type_bridge_contract::sdk_diagnostic::SdkProviderOperation;
#[derive(Debug, thiserror::Error)]
pub enum QueryTargetError {
#[error(transparent)]
Direct(#[from] OrmError),
#[error(transparent)]
Prepare(RemoteModelQueryV2Error),
#[error(transparent)]
Reply(RemoteModelQueryV2Error),
#[error("{0:?}")]
Interrupted(SdkExecutionDiagnostic),
}
impl From<SdkExecutionDiagnostic> for QueryTargetError {
fn from(error: SdkExecutionDiagnostic) -> Self {
Self::Interrupted(error)
}
}
pub struct ProxyQueryConnection {
http: Option<ProxyHttpTransport>,
advertisement: Arc<[u8]>,
authority: Arc<QueryAuthority>,
answer_limits: QueryExecutionResourceLimits,
staged_advertisement: Mutex<Option<Arc<[u8]>>>,
}
impl std::fmt::Debug for ProxyQueryConnection {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("ProxyQueryConnection")
.field("built_in_route", &self.http.is_some())
.finish_non_exhaustive()
}
}
impl ProxyQueryConnection {
pub async fn connect(
endpoint: &ProxyEndpoint,
advertisement: Option<Vec<u8>>,
authority: Arc<QueryAuthority>,
connection_limits: QueryExecutionResourceLimits,
answer_limits: QueryExecutionResourceLimits,
cancellation: &AnswerCancellation,
) -> Result<Self, SdkExecutionDiagnostic> {
let deadline = QueryExecutionDeadline::for_limits(connection_limits.effective());
let http = ProxyHttpTransport::open(endpoint)?;
let advertisement = match advertisement {
Some(pinned) => pinned,
None => http.capabilities(deadline, cancellation).await?,
};
Self::new(Some(http), advertisement, authority, answer_limits)
}
pub fn with_exchange(
advertisement: Vec<u8>,
authority: Arc<QueryAuthority>,
answer_limits: QueryExecutionResourceLimits,
) -> Result<Self, SdkExecutionDiagnostic> {
Self::new(None, advertisement, authority, answer_limits)
}
pub fn new(
http: Option<ProxyHttpTransport>,
advertisement: Vec<u8>,
authority: Arc<QueryAuthority>,
answer_limits: QueryExecutionResourceLimits,
) -> Result<Self, SdkExecutionDiagnostic> {
RemoteCapabilities::decode(&advertisement).map_err(lower_remote_query_diagnostic)?;
Ok(Self {
http,
advertisement: advertisement.into(),
authority,
answer_limits: answer_limits.effective(),
staged_advertisement: Mutex::new(None),
})
}
pub fn with_staged_advertisement(
self,
staged_advertisement: Vec<u8>,
) -> Result<Self, SdkExecutionDiagnostic> {
self.check_staged(&staged_advertisement)?;
*self.staged_cache() = Some(staged_advertisement.into());
Ok(self)
}
#[must_use]
pub fn staged_advertisement(&self) -> Option<Arc<[u8]>> {
self.staged_cache().clone()
}
fn staged_cache(&self) -> std::sync::MutexGuard<'_, Option<Arc<[u8]>>> {
self.staged_advertisement
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
fn check_staged(&self, staged: &[u8]) -> Result<(), SdkExecutionDiagnostic> {
let pinned = RemoteCapabilities::decode(&self.advertisement)
.map_err(lower_remote_query_diagnostic)?;
let staged = RemoteCapabilities::decode(staged).map_err(lower_remote_query_diagnostic)?;
if staged.executor() != pinned.executor()
|| staged.reply_key() != pinned.reply_key()
|| staged.reply_key_id() != pinned.reply_key_id()
{
return Err(lower_remote_query_diagnostic(Diagnostic::new(
DiagnosticCategory::InvalidContract,
DiagnosticCode::new("query_remote_v3_advertisement_mismatch")
.expect("the static mismatch code is canonical"),
"the staged advertisement's executor or reply key differs from the connection's",
)));
}
Ok(())
}
#[must_use]
pub const fn answer_limits(&self) -> QueryExecutionResourceLimits {
self.answer_limits
}
pub async fn staged_transaction(
&self,
options: TransactionOptions,
cancellation: &AnswerCancellation,
) -> Result<Transaction, SdkExecutionDiagnostic> {
let http = self.http.clone().ok_or_else(|| {
SdkExecutionDiagnostic::provider_failure(SdkProviderOperation::Connect)
})?;
let advertisement = match self.staged_advertisement() {
Some(advertisement) => advertisement,
None => {
let deadline = QueryExecutionDeadline::for_limits(self.answer_limits);
let fetched = http.staged_capabilities(deadline, cancellation).await?;
self.check_staged(&fetched)?;
let fetched: Arc<[u8]> = fetched.into();
*self.staged_cache() = Some(Arc::clone(&fetched));
fetched
}
};
self.staged(Some(http), None, &advertisement, options)
}
pub fn staged_transaction_with_exchange(
&self,
staged_advertisement: &[u8],
exchange: StagedExchangeFn,
options: TransactionOptions,
) -> Result<Transaction, SdkExecutionDiagnostic> {
self.check_staged(staged_advertisement)?;
self.staged_cache()
.get_or_insert_with(|| staged_advertisement.into());
self.staged(None, Some(exchange), staged_advertisement, options)
}
fn staged(
&self,
http: Option<ProxyHttpTransport>,
exchange: Option<StagedExchangeFn>,
advertisement: &[u8],
options: TransactionOptions,
) -> Result<Transaction, SdkExecutionDiagnostic> {
let proxy = ProxyStaging::new(
http,
exchange,
advertisement,
self.authority.declared().clone(),
self.answer_limits,
)
.map_err(lower_remote_query_diagnostic)?;
Ok(Transaction::new_proxy(proxy, options))
}
#[must_use]
pub const fn has_built_in_route(&self) -> bool {
self.http.is_some()
}
pub fn prepare(
&self,
registry: &DescriptorRegistry,
validated: ValidatedMatchRequest,
resources: QueryExecutionResourceLimits,
cancellation: &AnswerCancellation,
deadline: QueryExecutionDeadline,
) -> Result<PendingRemoteModelQueryV2, QueryTargetError> {
deadline.check(cancellation)?;
let resources = resources.constrained_by(self.answer_limits);
let pending = prepare_remote_model_query_v2_with_budget(
&self.authority,
registry,
validated,
&self.advertisement,
resources.remote(),
deadline,
cancellation,
)
.map_err(QueryTargetError::Prepare)?;
deadline.check(cancellation)?;
Ok(pending)
}
pub fn finish(
pending: &PendingRemoteModelQueryV2,
response: &[u8],
cancellation: &AnswerCancellation,
deadline: QueryExecutionDeadline,
) -> Result<(ValidatedMatchRequest, ValidatedMatchResult), QueryTargetError> {
deadline.check(cancellation)?;
let claimed = pending
.claim_reply_with_cancellation(cancellation)
.map_err(QueryTargetError::Reply)?;
let (request, result, _registry) = claimed
.decode_with_cancellation(response, cancellation)
.map_err(QueryTargetError::Reply)?;
deadline.check(cancellation)?;
Ok((request, result))
}
}
#[derive(Clone, Copy)]
pub enum QueryTarget<'a> {
Database(&'a Database),
Transaction(&'a TransactionContext),
Proxy(&'a ProxyQueryConnection),
}
impl QueryTarget<'_> {
#[must_use]
pub const fn answer_limits(&self) -> Option<QueryExecutionResourceLimits> {
match self {
Self::Database(database) => database.answer_limits(),
Self::Transaction(transaction) => transaction.answer_limits(),
Self::Proxy(proxy) => Some(proxy.answer_limits),
}
}
#[must_use]
pub const fn constrain(
&self,
requested: QueryExecutionResourceLimits,
) -> QueryExecutionResourceLimits {
match self.answer_limits() {
Some(ceiling) => requested.constrained_by(ceiling),
None => requested.effective(),
}
}
#[must_use]
pub fn budget(
&self,
resources: QueryExecutionResourceLimits,
deadline: QueryExecutionDeadline,
) -> (QueryExecutionResourceLimits, QueryExecutionDeadline) {
match self.answer_limits() {
Some(ceiling) => (
resources.constrained_by(ceiling),
deadline.earliest(QueryExecutionDeadline::for_limits(ceiling)),
),
None => (resources.effective(), deadline),
}
}
pub async fn execute(
&self,
registry: &DescriptorRegistry,
validated: ValidatedMatchRequest,
resources: QueryExecutionResourceLimits,
cancellation: &AnswerCancellation,
deadline: QueryExecutionDeadline,
) -> Result<(ValidatedMatchRequest, ValidatedMatchResult), QueryTargetError> {
let (resources, deadline) = self.budget(resources, deadline);
match self {
Self::Database(database) => {
let result = database
.execute_match_with_limits(
registry,
&validated,
resources.direct_with_deadline(cancellation.clone(), deadline),
)
.await?;
Ok((validated, result))
}
Self::Transaction(transaction) => {
let result = transaction
.execute_match_with_limits(
registry,
&validated,
resources.direct_with_deadline(cancellation.clone(), deadline),
)
.await?;
Ok((validated, result))
}
Self::Proxy(proxy) => {
let Some(http) = &proxy.http else {
return Err(caller_owned_exchange().into());
};
let pending =
proxy.prepare(registry, validated, resources, cancellation, deadline)?;
let response = http
.exchange(
pending.request_bytes().to_vec(),
MAX_REMOTE_ENVELOPE_BYTES,
deadline,
cancellation,
)
.await?;
ProxyQueryConnection::finish(&pending, &response, cancellation, deadline)
}
}
}
}
fn caller_owned_exchange() -> SdkExecutionDiagnostic {
SdkExecutionDiagnostic::invalid_input(
type_bridge_contract::sdk_diagnostic::SdkDiagnosticCode::new("proxy_exchange_caller_owned")
.expect("static proxy diagnostic code is canonical"),
type_bridge_contract::sdk_diagnostic::SdkDiagnosticMessage::new(
"This proxy connection's exchange is performed by the caller",
)
.expect("static proxy diagnostic message is valid"),
)
}